载入中...
搜索中...
未找到
Math.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Module.h"
4
5#include <cstdint>
6#include <random>
7#include <string>
8
9namespace eve::math {
10
11class Vec2;
12class Vec3;
13class Mat4;
14
21class Math : public Module {
22public:
24 Math();
25 ~Math() override = default;
26
27 Vec2 *newVec2(float x = 0.f, float y = 0.f);
28 Vec3 *newVec3(float x = 0.f, float y = 0.f, float z = 0.f);
29 Mat4 *newMat4();
30 Mat4 *newMat4Translation(float x, float y, float z);
31 Mat4 *newMat4Scale(float sx, float sy, float sz);
32 Mat4 *newMat4RotationZ(float radians);
33
34 // --- scalar ---
35 float clamp(float x, float lo, float hi) const;
36 float lerp(float a, float b, float t) const;
37 float smoothstep(float edge0, float edge1, float x) const;
38 float remap(float x, float inMin, float inMax, float outMin, float outMax) const;
39 float degToRad(float deg) const;
40 float radToDeg(float rad) const;
41 float sign(float x) const;
42 float fract(float x) const;
43 float approach(float current, float target, float maxDelta) const;
44 float wrap(float x, float lo, float hi) const;
45 float pingPong(float t, float length) const;
46
48 float inverseLerp(float a, float b, float x) const;
49 float smootherstep(float edge0, float edge1, float x) const;
51 float bias(float t, float b) const;
52 float gain(float t, float g) const;
58 float ease(float t, const std::string &kind) const;
59 float step(float edge, float x) const;
60 float quantize(float x, float stepSize) const;
61 float snap(float x, float grid) const;
62
63 // --- geometry (float components) ---
64 float length2(float x, float y) const;
65 float length3(float x, float y, float z) const;
66 float distance2(float x1, float y1, float x2, float y2) const;
67 float distance3(float x1, float y1, float z1, float x2, float y2, float z2) const;
68 float dot2(float x1, float y1, float x2, float y2) const;
69 float dot3(float x1, float y1, float z1, float x2, float y2, float z2) const;
70 float cross2(float x1, float y1, float x2, float y2) const;
71 float angle2(float x, float y) const;
72 float angleBetween2(float x1, float y1, float x2, float y2) const;
73 float lerpAngle(float a, float b, float t) const;
74 float normalize2X(float x, float y) const;
75 float normalize2Y(float x, float y) const;
76 float normalize3X(float x, float y, float z) const;
77 float normalize3Y(float x, float y, float z) const;
78 float normalize3Z(float x, float y, float z) const;
79
81 float rotate2X(float x, float y, float radians) const;
82 float rotate2Y(float x, float y, float radians) const;
83 float polarX(float radius, float radians) const;
84 float polarY(float radius, float radians) const;
85 float cartesianRadius(float x, float y) const;
86 float cartesianAngle(float x, float y) const;
87
88 // --- 2D hit / overlap / ray (picking & collision detection) ---
89 bool pointInCircle(float px, float py, float cx, float cy, float radius) const;
90 bool pointInRect(float px, float py, float rx, float ry, float rw, float rh) const;
91 bool circlesOverlap(float x1, float y1, float r1, float x2, float y2, float r2) const;
92 bool rectsOverlap(float x1, float y1, float w1, float h1, float x2, float y2, float w2,
93 float h2) const;
94 bool circleRectOverlap(float cx, float cy, float radius, float rx, float ry, float rw,
95 float rh) const;
97 bool segmentsIntersect(float ax, float ay, float bx, float by, float cx, float cy, float dx,
98 float dy) const;
103 float raycastCircle2(float ox, float oy, float dx, float dy, float cx, float cy,
104 float radius) const;
106 float raycastRect2(float ox, float oy, float dx, float dy, float rx, float ry, float rw,
107 float rh) const;
108 float closestPointOnSegment2X(float px, float py, float ax, float ay, float bx, float by) const;
109 float closestPointOnSegment2Y(float px, float py, float ax, float ay, float bx, float by) const;
110
111 // --- 3D hit / overlap / ray ---
112 bool pointInSphere(float px, float py, float pz, float cx, float cy, float cz,
113 float radius) const;
115 bool pointInBox(float px, float py, float pz, float minX, float minY, float minZ, float maxX,
116 float maxY, float maxZ) const;
117 bool spheresOverlap(float x1, float y1, float z1, float r1, float x2, float y2, float z2,
118 float r2) const;
119 bool boxesOverlap(float minAx, float minAy, float minAz, float maxAx, float maxAy, float maxAz,
120 float minBx, float minBy, float minBz, float maxBx, float maxBy,
121 float maxBz) const;
122 float raycastSphere(float ox, float oy, float oz, float dx, float dy, float dz, float cx,
123 float cy, float cz, float radius) const;
124 float raycastBox(float ox, float oy, float oz, float dx, float dy, float dz, float minX,
125 float minY, float minZ, float maxX, float maxY, float maxZ) const;
130 float raycastPlane(float ox, float oy, float oz, float dx, float dy, float dz, float px,
131 float py, float pz, float nx, float ny, float nz) const;
132 float closestPointOnSegment3X(float px, float py, float pz, float ax, float ay, float az,
133 float bx, float by, float bz) const;
134 float closestPointOnSegment3Y(float px, float py, float pz, float ax, float ay, float az,
135 float bx, float by, float bz) const;
136 float closestPointOnSegment3Z(float px, float py, float pz, float ax, float ay, float az,
137 float bx, float by, float bz) const;
138
140 float bilinear(float v00, float v10, float v01, float v11, float u, float v) const;
141
142 // --- random ---
143 void setRandomSeed(uint32_t seed);
145 uint32_t getRandomSeed() const;
146 float random();
147 float randomRange(float min, float max);
148 int randomInt(int min, int maxInclusive);
150 float randomGaussian(float mean, float stddev);
151
152 // --- deterministic hash [0,1] (grid / WFC / tile seeds) ---
153 float hash1(float x) const;
154 float hash2(float x, float y) const;
155 float hash3(float x, float y, float z) const;
156
157 // --- noise [0, 1] ---
158 float noise1(float x) const;
159 float noise2(float x, float y) const;
160 float noise3(float x, float y, float z) const;
161 float perlin2(float x, float y) const;
162 float perlin3(float x, float y, float z) const;
163
168 float fbm2(float x, float y, int octaves = 4, float lacunarity = 2.f,
169 float gain = 0.5f) const;
170 float fbm3(float x, float y, float z, int octaves = 4, float lacunarity = 2.f,
171 float gain = 0.5f) const;
172 float ridged2(float x, float y, int octaves = 4, float lacunarity = 2.f,
173 float gain = 0.5f) const;
174 float ridged3(float x, float y, float z, int octaves = 4, float lacunarity = 2.f,
175 float gain = 0.5f) const;
176 float turbulence2(float x, float y, int octaves = 4, float lacunarity = 2.f,
177 float gain = 0.5f) const;
178
183 float voronoi2(float x, float y) const;
184 float voronoiEdge2(float x, float y) const;
185
190 float warpNoise2(float x, float y, float warpAmp = 1.f) const;
191
192 // --- bezier ---
193 float bezierQuadratic(float t, float p0, float p1, float p2) const;
194 float bezierCubic(float t, float p0, float p1, float p2, float p3) const;
195 float bezierQuadratic2X(float t, float x0, float y0, float x1, float y1, float x2,
196 float y2) const;
197 float bezierQuadratic2Y(float t, float x0, float y0, float x1, float y1, float x2,
198 float y2) const;
199 float bezierCubic2X(float t, float x0, float y0, float x1, float y1, float x2, float y2,
200 float x3, float y3) const;
201 float bezierCubic2Y(float t, float x0, float y0, float x1, float y1, float x2, float y2,
202 float x3, float y3) const;
203
204private:
205 uint32_t seed_ = 1;
206 std::mt19937 rng_;
207};
208
209} // namespace eve::math
float cx
Definition CardTypes.cpp:31
float cy
Definition CardTypes.cpp:32
uint32_t seed
Tok kind
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
float u
Definition Grass.cpp:234
int x
Definition Grass.cpp:135
std::vector< Colorf > px
uint32_t a
uint32_t b
int v
float step
Definition TreeMesh.cpp:196
Column-major 4x4 matrix wrapping glm::mat4.
Definition Mat4.h:11
Math module — glm-backed vectors/matrices, noise, bezier, random. Script: math <- eve....
Definition Math.h:21
bool pointInSphere(float px, float py, float pz, float cx, float cy, float cz, float radius) const
Definition Math.cpp:411
float bezierQuadratic(float t, float p0, float p1, float p2) const
Definition Math.cpp:649
bool spheresOverlap(float x1, float y1, float z1, float r1, float x2, float y2, float z2, float r2) const
Definition Math.cpp:422
Mat4 * newMat4Scale(float sx, float sy, float sz)
Definition Math.cpp:97
float warpNoise2(float x, float y, float warpAmp=1.f) const
Domain warp: sample noise at (x,y) + warpAmp * (noise-0.5). Useful for organic terrain / caves.
Definition Math.cpp:643
float normalize3X(float x, float y, float z) const
Definition Math.cpp:262
bool boxesOverlap(float minAx, float minAy, float minAz, float maxAx, float maxAy, float maxAz, float minBx, float minBy, float minBz, float maxBx, float maxBy, float maxBz) const
Definition Math.cpp:430
float normalize3Z(float x, float y, float z) const
Definition Math.cpp:270
float perlin3(float x, float y, float z) const
Definition Math.cpp:556
float voronoi2(float x, float y) const
Worley / Voronoi F1 distance in [0, ~1.5] (cell size 1). voronoiEdge2 = F2 - F1 (cell borders).
Definition Math.cpp:631
float approach(float current, float target, float maxDelta) const
Definition Math.cpp:139
float turbulence2(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:618
float closestPointOnSegment2Y(float px, float py, float ax, float ay, float bx, float by) const
Definition Math.cpp:405
Mat4 * newMat4Translation(float x, float y, float z)
Definition Math.cpp:91
float normalize2Y(float x, float y) const
Definition Math.cpp:258
float length3(float x, float y, float z) const
Definition Math.cpp:222
float ease(float t, const std::string &kind) const
Easing on [0,1]. kind: "linear"|"inQuad"|"outQuad"|"inOutQuad"|"inCubic"|"outCubic"|"inOutCubic"| "in...
Definition Math.cpp:184
float bezierQuadratic2X(float t, float x0, float y0, float x1, float y1, float x2, float y2) const
Definition Math.cpp:663
float angle2(float x, float y) const
Definition Math.cpp:240
float raycastPlane(float ox, float oy, float oz, float dx, float dy, float dz, float px, float py, float pz, float nx, float ny, float nz) const
Ray vs infinite plane through (px,py,pz) with normal (nx,ny,nz). Returns parametric t,...
Definition Math.cpp:479
float raycastBox(float ox, float oy, float oz, float dx, float dy, float dz, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) const
Definition Math.cpp:455
float angleBetween2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:242
float cartesianAngle(float x, float y) const
Definition Math.cpp:287
float normalize3Y(float x, float y, float z) const
Definition Math.cpp:266
float closestPointOnSegment2X(float px, float py, float ax, float ay, float bx, float by) const
Definition Math.cpp:400
bool circleRectOverlap(float cx, float cy, float radius, float rx, float ry, float rw, float rh) const
Definition Math.cpp:310
float smootherstep(float edge0, float edge1, float x) const
Definition Math.cpp:165
float closestPointOnSegment3Y(float px, float py, float pz, float ax, float ay, float az, float bx, float by, float bz) const
Definition Math.cpp:492
float random()
Definition Math.cpp:523
float fract(float x) const
Definition Math.cpp:137
float noise1(float x) const
Definition Math.cpp:549
Mat4 * newMat4()
Definition Math.cpp:89
float smoothstep(float edge0, float edge1, float x) const
Definition Math.cpp:116
float degToRad(float deg) const
Definition Math.cpp:128
~Math() override=default
float randomRange(float min, float max)
Definition Math.cpp:528
bool rectsOverlap(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) const
Definition Math.cpp:305
float normalize2X(float x, float y) const
Definition Math.cpp:254
float snap(float x, float grid) const
Definition Math.cpp:216
float polarX(float radius, float radians) const
Definition Math.cpp:284
float rotate2X(float x, float y, float radians) const
Rotate (x,y) by radians around origin.
Definition Math.cpp:275
float wrap(float x, float lo, float hi) const
Definition Math.cpp:145
float voronoiEdge2(float x, float y) const
Definition Math.cpp:637
float sign(float x) const
Definition Math.cpp:131
float cross2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:239
float clamp(float x, float lo, float hi) const
Definition Math.cpp:109
float closestPointOnSegment3X(float px, float py, float pz, float ax, float ay, float az, float bx, float by, float bz) const
Definition Math.cpp:487
float rotate2Y(float x, float y, float radians) const
Definition Math.cpp:279
float closestPointOnSegment3Z(float px, float py, float pz, float ax, float ay, float az, float bx, float by, float bz) const
Definition Math.cpp:497
float bezierCubic2Y(float t, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) const
Definition Math.cpp:678
float noise3(float x, float y, float z) const
Definition Math.cpp:551
float perlin2(float x, float y) const
Definition Math.cpp:555
bool pointInCircle(float px, float py, float cx, float cy, float radius) const
Definition Math.cpp:289
float hash2(float x, float y) const
Definition Math.cpp:546
float bezierQuadratic2Y(float t, float x0, float y0, float x1, float y1, float x2, float y2) const
Definition Math.cpp:668
float dot3(float x1, float y1, float z1, float x2, float y2, float z2) const
Definition Math.cpp:235
float noise2(float x, float y) const
Definition Math.cpp:550
bool pointInBox(float px, float py, float pz, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) const
Inclusive AABB test against [min,max] on each axis.
Definition Math.cpp:417
void setRandomSeedFromTime()
Definition Math.cpp:514
Vec3 * newVec3(float x=0.f, float y=0.f, float z=0.f)
Definition Math.cpp:87
float ridged3(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:602
uint32_t getRandomSeed() const
Definition Math.cpp:521
float bezierCubic(float t, float p0, float p1, float p2, float p3) const
Definition Math.cpp:655
float gain(float t, float g) const
Definition Math.cpp:178
float bilinear(float v00, float v10, float v01, float v11, float u, float v) const
Bilinear sample of 4 corners (v00,v10,v01,v11) with u,v in [0,1].
Definition Math.cpp:503
float ridged2(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:586
float cartesianRadius(float x, float y) const
Definition Math.cpp:286
float pingPong(float t, float length) const
Definition Math.cpp:154
float hash1(float x) const
Definition Math.cpp:545
float distance2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:226
float bias(float t, float b) const
Schlick bias/gain — shape [0,1] distributions (procgen falloff).
Definition Math.cpp:171
float raycastRect2(float ox, float oy, float dx, float dy, float rx, float ry, float rw, float rh) const
Ray vs axis-aligned rect (x,y,w,h). Returns parametric t >= 0, else -1.
Definition Math.cpp:359
float distance3(float x1, float y1, float z1, float x2, float y2, float z2) const
Definition Math.cpp:230
void setRandomSeed(uint32_t seed)
Definition Math.cpp:509
float length2(float x, float y) const
Definition Math.cpp:221
float radToDeg(float rad) const
Definition Math.cpp:129
float inverseLerp(float a, float b, float x) const
Inverse of lerp: t such that lerp(a,b,t) ≈ x.
Definition Math.cpp:160
bool segmentsIntersect(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy) const
True if segments AB and CD intersect (including endpoints).
Definition Math.cpp:319
float remap(float x, float inMin, float inMax, float outMin, float outMax) const
Definition Math.cpp:122
Mat4 * newMat4RotationZ(float radians)
Definition Math.cpp:103
Vec2 * newVec2(float x=0.f, float y=0.f)
Definition Math.cpp:86
float randomGaussian(float mean, float stddev)
Box-Muller Gaussian (mean, stddev).
Definition Math.cpp:540
float lerpAngle(float a, float b, float t) const
Definition Math.cpp:246
float bezierCubic2X(float t, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) const
Definition Math.cpp:673
float fbm3(float x, float y, float z, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Definition Math.cpp:573
float dot2(float x1, float y1, float x2, float y2) const
Definition Math.cpp:234
float raycastCircle2(float ox, float oy, float dx, float dy, float cx, float cy, float radius) const
Ray vs circle. Hit point = (ox,oy) + t*(dx,dy). Returns t >= 0 on hit, else -1. Direction need not be...
Definition Math.cpp:341
float polarY(float radius, float radians) const
Definition Math.cpp:285
bool pointInRect(float px, float py, float rx, float ry, float rw, float rh) const
Definition Math.cpp:294
float lerp(float a, float b, float t) const
Definition Math.cpp:114
float quantize(float x, float stepSize) const
Definition Math.cpp:211
bool circlesOverlap(float x1, float y1, float r1, float x2, float y2, float r2) const
Definition Math.cpp:298
float fbm2(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Fractal Brownian Motion / ridged / turbulence. octaves >= 1; lacunarity ~2; gain/persistence ~0....
Definition Math.cpp:560
float raycastSphere(float ox, float oy, float oz, float dx, float dy, float dz, float cx, float cy, float cz, float radius) const
Definition Math.cpp:437
int randomInt(int min, int maxInclusive)
Definition Math.cpp:534
float hash3(float x, float y, float z) const
Definition Math.cpp:547
2D float vector (script-facing math module value).
Definition Vec2.h:8
3D float vector (script-facing math module value).
Definition Vec3.h:8