载入中...
搜索中...
未找到
Math.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/Module.h"
6
7#include <cstdint>
8#include <random>
9#include <string>
10
11namespace eve::math {
12
13class Vec2;
14class Vec3;
15class Mat4;
16
24public:
27 Math();
29 ~Math() override = default;
30
32 Vec2 *newVec2(float x = 0.f, float y = 0.f);
34 Vec3 *newVec3(float x = 0.f, float y = 0.f, float z = 0.f);
36 Mat4 *newMat4();
38 Mat4 *newMat4Translation(float x, float y, float z);
40 Mat4 *newMat4Scale(float sx, float sy, float sz);
42 Mat4 *newMat4RotationZ(float radians);
43
44 // --- scalar ---
46 float clamp(float x, float lo, float hi) const;
48 float lerp(float a, float b, float t) const;
50 float smoothstep(float edge0, float edge1, float x) const;
52 float remap(float x, float inMin, float inMax, float outMin, float outMax) const;
54 float degToRad(float deg) const;
56 float radToDeg(float rad) const;
58 float sign(float x) const;
60 float fract(float x) const;
62 float approach(float current, float target, float maxDelta) const;
64 float wrap(float x, float lo, float hi) const;
66 float pingPong(float t, float length) const;
67
70 float inverseLerp(float a, float b, float x) const;
72 float smootherstep(float edge0, float edge1, float x) const;
74 float bias(float t, float b) const;
76 float gain(float t, float g) const;
82 float ease(float t, const std::string &kind) const;
84 float step(float edge, float x) const;
86 float quantize(float x, float stepSize) const;
88 float snap(float x, float grid) const;
89
90 // --- geometry (float components) ---
92 float length2(float x, float y) const;
94 float length3(float x, float y, float z) const;
96 float distance2(float x1, float y1, float x2, float y2) const;
98 float distance3(float x1, float y1, float z1, float x2, float y2, float z2) const;
100 float dot2(float x1, float y1, float x2, float y2) const;
102 float dot3(float x1, float y1, float z1, float x2, float y2, float z2) const;
104 float cross2(float x1, float y1, float x2, float y2) const;
106 float angle2(float x, float y) const;
108 float angleBetween2(float x1, float y1, float x2, float y2) const;
110 float lerpAngle(float a, float b, float t) const;
112 float normalize2X(float x, float y) const;
114 float normalize2Y(float x, float y) const;
116 float normalize3X(float x, float y, float z) const;
118 float normalize3Y(float x, float y, float z) const;
120 float normalize3Z(float x, float y, float z) const;
121
122 // --- steering (stateless vector calculations) ---
124 Vec2 *steeringSeek2(float x, float y, float targetX, float targetY, float maxSpeed) const;
126 Vec3 *steeringSeek3(float x, float y, float z, float targetX, float targetY, float targetZ,
127 float maxSpeed) const;
129 Vec2 *steeringFlee2(float x, float y, float targetX, float targetY, float maxSpeed) const;
131 Vec3 *steeringFlee3(float x, float y, float z, float targetX, float targetY, float targetZ,
132 float maxSpeed) const;
134 Vec2 *steeringArrive2(float x, float y, float targetX, float targetY, float maxSpeed,
135 float slowRadius, float stopRadius) const;
137 Vec3 *steeringArrive3(float x, float y, float z, float targetX, float targetY, float targetZ,
138 float maxSpeed, float slowRadius, float stopRadius) const;
140 Vec2 *steeringSeparation2(float x, float y, const std::string &neighbors, float radius,
141 float maxAcceleration) const;
143 Vec3 *steeringSeparation3(float x, float y, float z, const std::string &neighbors,
144 float radius, float maxAcceleration) const;
146 int steeringPathTarget2(float x, float y, const std::string &points, int current,
147 float tolerance) const;
149 int steeringPathTarget3(float x, float y, float z, const std::string &points, int current,
150 float tolerance) const;
152 Vec2 *steeringAvoid2(float x, float y, float velocityX, float velocityY, float obstacleX,
153 float obstacleY, float obstacleRadius, float lookAhead,
154 float maxAcceleration) const;
156 Vec3 *steeringAvoid3(float x, float y, float z, float velocityX, float velocityY,
157 float velocityZ, float obstacleX, float obstacleY, float obstacleZ,
158 float obstacleRadius, float lookAhead, float maxAcceleration) const;
159
161 float rotate2X(float x, float y, float radians) const;
163 float rotate2Y(float x, float y, float radians) const;
165 float polarX(float radius, float radians) const;
167 float polarY(float radius, float radians) const;
169 float cartesianRadius(float x, float y) const;
171 float cartesianAngle(float x, float y) const;
172
173 // --- 2D hit / overlap / ray (picking & collision detection) ---
175 bool pointInCircle(float px, float py, float cx, float cy, float radius) const;
177 bool pointInRect(float px, float py, float rx, float ry, float rw, float rh) const;
179 bool circlesOverlap(float x1, float y1, float r1, float x2, float y2, float r2) const;
181 bool rectsOverlap(float x1, float y1, float w1, float h1, float x2, float y2, float w2,
182 float h2) const;
184 bool circleRectOverlap(float cx, float cy, float radius, float rx, float ry, float rw,
185 float rh) const;
187 bool segmentsIntersect(float ax, float ay, float bx, float by, float cx, float cy, float dx,
188 float dy) const;
193 float raycastCircle2(float ox, float oy, float dx, float dy, float cx, float cy,
194 float radius) const;
196 float raycastRect2(float ox, float oy, float dx, float dy, float rx, float ry, float rw,
197 float rh) const;
199 float closestPointOnSegment2X(float px, float py, float ax, float ay, float bx, float by) const;
201 float closestPointOnSegment2Y(float px, float py, float ax, float ay, float bx, float by) const;
202
203 // --- 3D hit / overlap / ray ---
205 bool pointInSphere(float px, float py, float pz, float cx, float cy, float cz,
206 float radius) const;
208 bool pointInBox(float px, float py, float pz, float minX, float minY, float minZ, float maxX,
209 float maxY, float maxZ) const;
211 bool spheresOverlap(float x1, float y1, float z1, float r1, float x2, float y2, float z2,
212 float r2) const;
214 bool boxesOverlap(float minAx, float minAy, float minAz, float maxAx, float maxAy, float maxAz,
215 float minBx, float minBy, float minBz, float maxBx, float maxBy,
216 float maxBz) const;
218 float raycastSphere(float ox, float oy, float oz, float dx, float dy, float dz, float cx,
219 float cy, float cz, float radius) const;
221 float raycastBox(float ox, float oy, float oz, float dx, float dy, float dz, float minX,
222 float minY, float minZ, float maxX, float maxY, float maxZ) const;
227 float raycastPlane(float ox, float oy, float oz, float dx, float dy, float dz, float px,
228 float py, float pz, float nx, float ny, float nz) const;
230 float closestPointOnSegment3X(float px, float py, float pz, float ax, float ay, float az,
231 float bx, float by, float bz) const;
233 float closestPointOnSegment3Y(float px, float py, float pz, float ax, float ay, float az,
234 float bx, float by, float bz) const;
236 float closestPointOnSegment3Z(float px, float py, float pz, float ax, float ay, float az,
237 float bx, float by, float bz) const;
238
240 float bilinear(float v00, float v10, float v01, float v11, float u, float v) const;
241
242 // --- random ---
244 void setRandomSeed(uint32_t seed);
246 void setRandomSeedFromTime();
248 uint32_t getRandomSeed() const;
250 float random();
252 float randomRange(float min, float max);
254 int randomInt(int min, int maxInclusive);
256 float randomGaussian(float mean, float stddev);
257
258 // --- deterministic hash [0,1] (grid / WFC / tile seeds) ---
260 float hash1(float x) const;
262 float hash2(float x, float y) const;
264 float hash3(float x, float y, float z) const;
265
266 // --- noise [0, 1] ---
268 float noise1(float x) const;
270 float noise2(float x, float y) const;
272 float noise3(float x, float y, float z) const;
274 float perlin2(float x, float y) const;
276 float perlin3(float x, float y, float z) const;
277
282 float fbm2(float x, float y, int octaves = 4, float lacunarity = 2.f,
283 float gain = 0.5f) const;
285 float fbm3(float x, float y, float z, int octaves = 4, float lacunarity = 2.f,
286 float gain = 0.5f) const;
288 float ridged2(float x, float y, int octaves = 4, float lacunarity = 2.f,
289 float gain = 0.5f) const;
291 float ridged3(float x, float y, float z, int octaves = 4, float lacunarity = 2.f,
292 float gain = 0.5f) const;
294 float turbulence2(float x, float y, int octaves = 4, float lacunarity = 2.f,
295 float gain = 0.5f) const;
296
301 float voronoi2(float x, float y) const;
303 float voronoiEdge2(float x, float y) const;
304
309 float warpNoise2(float x, float y, float warpAmp = 1.f) const;
310
311 // --- bezier ---
313 float bezierQuadratic(float t, float p0, float p1, float p2) const;
315 float bezierCubic(float t, float p0, float p1, float p2, float p3) const;
317 float bezierQuadratic2X(float t, float x0, float y0, float x1, float y1, float x2,
318 float y2) const;
320 float bezierQuadratic2Y(float t, float x0, float y0, float x1, float y1, float x2,
321 float y2) const;
323 float bezierCubic2X(float t, float x0, float y0, float x1, float y1, float x2, float y2,
324 float x3, float y3) const;
326 float bezierCubic2Y(float t, float x0, float y0, float x1, float y1, float x2, float y2,
327 float x3, float y3) const;
328
329private:
330 uint32_t seed_ = 1;
331 std::mt19937 rng_;
332};
333
334} // namespace eve::math
LogicalId target
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
int bz
Definition CaveMesh.cpp:114
int ax
Definition CaveMesh.cpp:113
int ay
Definition CaveMesh.cpp:113
int bx
Definition CaveMesh.cpp:114
float length
Definition CaveMesh.cpp:94
int az
Definition CaveMesh.cpp:113
int by
Definition CaveMesh.cpp:114
float py
float nx
float nz
float ny
float pz
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
tensor::Graph g
Definition GpuGraph.cpp:7
float u
Definition Grass.cpp:233
float v
std::vector< Colorf > px
TokenKind kind
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float radius
std::uint32_t seed
Definition PointSet.cpp:807
std::shared_ptr< const std::vector< glm::vec2 > > points
float t
std::unordered_map< std::uint32_t, std::uint32_t > remap
const RoadEdge * edge
double current
float dz
float dy
float dx
float bias
float targetX
Battle::Random random
float step
Definition TreeMesh.cpp:314
double oy
double ox
EVENGINE_API_FOUNDATION public API.
Definition Module.h:46
Column-major 4x4 matrix wrapping glm::mat4.
Definition Mat4.h:13
Math module — glm-backed vectors/matrices, noise, bezier, random. Script: math <- eve....
Definition Math.h:23
~Math() override=default
Releases module state; caller-owned Vec/Mat pointers are not freed.
2D float vector (script-facing math module value).
Definition Vec2.h:10
3D float vector (script-facing math module value).
Definition Vec3.h:10