载入中...
搜索中...
未找到
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;
59 [[nodiscard]] float smoothMax(float a, float b, float width) const;
61 float remap(float x, float inMin, float inMax, float outMin, float outMax) const;
63 float degToRad(float deg) const;
65 float radToDeg(float rad) const;
67 float sign(float x) const;
69 float fract(float x) const;
71 float approach(float current, float target, float maxDelta) const;
73 float wrap(float x, float lo, float hi) const;
75 float pingPong(float t, float length) const;
76
79 float inverseLerp(float a, float b, float x) const;
81 float smootherstep(float edge0, float edge1, float x) const;
83 float bias(float t, float b) const;
85 float gain(float t, float g) const;
91 float ease(float t, const std::string &kind) const;
93 float step(float edge, float x) const;
95 float quantize(float x, float stepSize) const;
97 float snap(float x, float grid) const;
98
99 // --- geometry (float components) ---
101 float length2(float x, float y) const;
103 float length3(float x, float y, float z) const;
105 float distance2(float x1, float y1, float x2, float y2) const;
107 float distance3(float x1, float y1, float z1, float x2, float y2, float z2) const;
109 float dot2(float x1, float y1, float x2, float y2) const;
111 float dot3(float x1, float y1, float z1, float x2, float y2, float z2) const;
113 float cross2(float x1, float y1, float x2, float y2) const;
115 float angle2(float x, float y) const;
117 float angleBetween2(float x1, float y1, float x2, float y2) const;
119 float lerpAngle(float a, float b, float t) const;
121 float normalize2X(float x, float y) const;
123 float normalize2Y(float x, float y) const;
125 float normalize3X(float x, float y, float z) const;
127 float normalize3Y(float x, float y, float z) const;
129 float normalize3Z(float x, float y, float z) const;
130
131 // --- steering (stateless vector calculations) ---
133 Vec2 *steeringSeek2(float x, float y, float targetX, float targetY, float maxSpeed) const;
135 Vec3 *steeringSeek3(float x, float y, float z, float targetX, float targetY, float targetZ,
136 float maxSpeed) const;
138 Vec2 *steeringFlee2(float x, float y, float targetX, float targetY, float maxSpeed) const;
140 Vec3 *steeringFlee3(float x, float y, float z, float targetX, float targetY, float targetZ,
141 float maxSpeed) const;
143 Vec2 *steeringArrive2(float x, float y, float targetX, float targetY, float maxSpeed,
144 float slowRadius, float stopRadius) const;
146 Vec3 *steeringArrive3(float x, float y, float z, float targetX, float targetY, float targetZ,
147 float maxSpeed, float slowRadius, float stopRadius) const;
149 Vec2 *steeringSeparation2(float x, float y, const std::string &neighbors, float radius,
150 float maxAcceleration) const;
152 Vec3 *steeringSeparation3(float x, float y, float z, const std::string &neighbors,
153 float radius, float maxAcceleration) const;
155 int steeringPathTarget2(float x, float y, const std::string &points, int current,
156 float tolerance) const;
158 int steeringPathTarget3(float x, float y, float z, const std::string &points, int current,
159 float tolerance) const;
161 Vec2 *steeringAvoid2(float x, float y, float velocityX, float velocityY, float obstacleX,
162 float obstacleY, float obstacleRadius, float lookAhead,
163 float maxAcceleration) const;
165 Vec3 *steeringAvoid3(float x, float y, float z, float velocityX, float velocityY,
166 float velocityZ, float obstacleX, float obstacleY, float obstacleZ,
167 float obstacleRadius, float lookAhead, float maxAcceleration) const;
168
170 float rotate2X(float x, float y, float radians) const;
172 float rotate2Y(float x, float y, float radians) const;
174 float polarX(float radius, float radians) const;
176 float polarY(float radius, float radians) const;
178 float cartesianRadius(float x, float y) const;
180 float cartesianAngle(float x, float y) const;
181
182 // --- 2D hit / overlap / ray (picking & collision detection) ---
184 bool pointInCircle(float px, float py, float cx, float cy, float radius) const;
186 bool pointInRect(float px, float py, float rx, float ry, float rw, float rh) const;
188 bool circlesOverlap(float x1, float y1, float r1, float x2, float y2, float r2) const;
190 bool rectsOverlap(float x1, float y1, float w1, float h1, float x2, float y2, float w2,
191 float h2) const;
193 bool circleRectOverlap(float cx, float cy, float radius, float rx, float ry, float rw,
194 float rh) const;
196 bool segmentsIntersect(float ax, float ay, float bx, float by, float cx, float cy, float dx,
197 float dy) const;
202 float raycastCircle2(float ox, float oy, float dx, float dy, float cx, float cy,
203 float radius) const;
205 float raycastRect2(float ox, float oy, float dx, float dy, float rx, float ry, float rw,
206 float rh) const;
208 float closestPointOnSegment2X(float px, float py, float ax, float ay, float bx, float by) const;
210 float closestPointOnSegment2Y(float px, float py, float ax, float ay, float bx, float by) const;
211
212 // --- 3D hit / overlap / ray ---
214 bool pointInSphere(float px, float py, float pz, float cx, float cy, float cz,
215 float radius) const;
217 bool pointInBox(float px, float py, float pz, float minX, float minY, float minZ, float maxX,
218 float maxY, float maxZ) const;
220 bool spheresOverlap(float x1, float y1, float z1, float r1, float x2, float y2, float z2,
221 float r2) const;
223 bool boxesOverlap(float minAx, float minAy, float minAz, float maxAx, float maxAy, float maxAz,
224 float minBx, float minBy, float minBz, float maxBx, float maxBy,
225 float maxBz) const;
227 float raycastSphere(float ox, float oy, float oz, float dx, float dy, float dz, float cx,
228 float cy, float cz, float radius) const;
230 float raycastBox(float ox, float oy, float oz, float dx, float dy, float dz, float minX,
231 float minY, float minZ, float maxX, float maxY, float maxZ) const;
236 float raycastPlane(float ox, float oy, float oz, float dx, float dy, float dz, float px,
237 float py, float pz, float nx, float ny, float nz) const;
239 float closestPointOnSegment3X(float px, float py, float pz, float ax, float ay, float az,
240 float bx, float by, float bz) const;
242 float closestPointOnSegment3Y(float px, float py, float pz, float ax, float ay, float az,
243 float bx, float by, float bz) const;
245 float closestPointOnSegment3Z(float px, float py, float pz, float ax, float ay, float az,
246 float bx, float by, float bz) const;
247
249 float bilinear(float v00, float v10, float v01, float v11, float u, float v) const;
250
251 // --- random ---
253 void setRandomSeed(uint32_t seed);
255 void setRandomSeedFromTime();
257 uint32_t getRandomSeed() const;
259 float random();
261 float randomRange(float min, float max);
263 int randomInt(int min, int maxInclusive);
265 float randomGaussian(float mean, float stddev);
266
267 // --- deterministic hash [0,1] (grid / WFC / tile seeds) ---
269 float hash1(float x) const;
271 float hash2(float x, float y) const;
273 float hash3(float x, float y, float z) const;
274
275 // --- noise [0, 1] (noisecreater / NoiseCreator family) ---
280 float noise1(float x) const;
282 float noise2(float x, float y) const;
284 float noise3(float x, float y, float z) const;
286 float simplex1(float x) const;
288 float simplex2(float x, float y) const;
290 float simplex3(float x, float y, float z) const;
292 float perlin2(float x, float y) const;
294 float perlin3(float x, float y, float z) const;
296 float white2(float x, float y) const;
298 float value2(float x, float y) const;
299
304 float fbm2(float x, float y, int octaves = 4, float lacunarity = 2.f,
305 float gain = 0.5f) const;
307 float fbm3(float x, float y, float z, int octaves = 4, float lacunarity = 2.f,
308 float gain = 0.5f) const;
310 float ridged2(float x, float y, int octaves = 4, float lacunarity = 2.f,
311 float gain = 0.5f) const;
313 float ridged3(float x, float y, float z, int octaves = 4, float lacunarity = 2.f,
314 float gain = 0.5f) const;
316 float turbulence2(float x, float y, int octaves = 4, float lacunarity = 2.f,
317 float gain = 0.5f) const;
319 float billow2(float x, float y, int octaves = 4, float lacunarity = 2.f, float gain = 0.5f) const;
320
325 float voronoi2(float x, float y) const;
327 float voronoiEdge2(float x, float y) const;
329 float worley2(float x, float y) const;
330
335 float warpNoise2(float x, float y, float warpAmp = 1.f) const;
339 float warpNoise2Deep(float x, float y, float warpAmp = 1.f) const;
340
342 float voronoiF2_2(float x, float y) const;
344 float worleyManhattan2(float x, float y) const;
346 float worleyChebyshev2(float x, float y) const;
348 float worleySmooth2(float x, float y, float smoothness) const;
349
351 float curlNoise2X(float x, float y, float eps) const;
353 float curlNoise2Y(float x, float y, float eps) const;
354
356 float hexDistance2(float x, float y) const;
358 float hexEdge2(float x, float y, float width, float soft) const;
359
361 float gyroid3(float x, float y, float z) const;
363 float gyroidFbm3(float x, float y, float z, int octaves) const;
364
366 float swiss2(float x, float y, int octaves, float lacunarity, float gain, float warpStrength) const;
368 float hybrid2(float x, float y, int octaves, float lacunarity, float gain, float offset) const;
370 float jordan2(float x, float y, int octaves, float lacunarity, float gain, float warpStrength) const;
371
373 float gabor2(float x, float y, float dirX, float dirY, float frequency) const;
375 float gaborFbm2(float x, float y, int octaves, float dirX, float dirY) const;
376
378 float valueDx2(float x, float y, float eps) const;
380 float valueDy2(float x, float y, float eps) const;
382 float valueSlope2(float x, float y, float eps) const;
383
385 float isoLine2(float x, float y, int levels, float width) const;
387 float banding2(float x, float y, int bands) const;
389 float fbmColor2(float x, float y, int channel, int octaves, float lacunarity, float gain) const;
390
392 float white3(float x, float y, float z) const;
394 float value3(float x, float y, float z) const;
396 float worley3(float x, float y, float z) const;
397
398 // --- bezier ---
400 float bezierQuadratic(float t, float p0, float p1, float p2) const;
402 float bezierCubic(float t, float p0, float p1, float p2, float p3) const;
404 float bezierQuadratic2X(float t, float x0, float y0, float x1, float y1, float x2,
405 float y2) const;
407 float bezierQuadratic2Y(float t, float x0, float y0, float x1, float y1, float x2,
408 float y2) const;
410 float bezierCubic2X(float t, float x0, float y0, float x1, float y1, float x2, float y2,
411 float x3, float y3) const;
413 float bezierCubic2Y(float t, float x0, float y0, float x1, float y1, float x2, float y2,
414 float x3, float y3) const;
415
416private:
417 uint32_t seed_ = 1;
418 std::mt19937 rng_;
419};
420
421} // namespace eve::math
double z
double y
double x
LogicalId target
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
float maxAcceleration
tensor::Graph g
Definition GpuGraph.cpp:7
float soft[4]
float u
Definition Grass.cpp:233
float v
std::vector< Colorf > px
std::uint32_t width
float length
size_t offset
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
glm::dvec3 dz
std::shared_ptr< const std::vector< glm::vec2 > > points
float t
std::unordered_map< std::uint32_t, std::uint32_t > remap
std::string kind
double current
float dy
Definition Shadow.cpp:151
float bias
Definition Shadow.cpp:149
float targetX
Battle::Random random
uint32_t seed
Definition TreeMesh.cpp:276
float step
Definition TreeMesh.cpp:451
double oy
double ox
double gain
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
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
float smoothMax(float a, float b, float width)
C1 polynomial maximum for blending two height fields.
Definition SmoothMax.h:24