1 module raymath;
2 
3 import raylib;
4 /**********************************************************************************************
5 *
6 *   raymath v1.5 - Math functions to work with Vector2, Vector3, Matrix and Quaternions
7 *
8 *   CONFIGURATION:
9 *
10 *   #define RAYMATH_IMPLEMENTATION
11 *       Generates the implementation of the library into the included file.
12 *       If not defined, the library is in header only mode and can be included in other headers
13 *       or source files without problems. But only ONE file should hold the implementation.
14 *
15 *   #define RAYMATH_STATIC_INLINE
16 *       Define static inline functions code, so #include header suffices for use.
17 *       This may use up lots of memory.
18 *
19 *   CONVENTIONS:
20 *
21 *     - Functions are always self-contained, no function use another raymath function inside,
22 *       required code is directly re-implemented inside
23 *     - Functions input parameters are always received by value (2 unavoidable exceptions)
24 *     - Functions use always a "result" anmed variable for return
25 *     - Functions are always defined inline
26 *     - Angles are always in radians (DEG2RAD/RAD2DEG macros provided for convenience)
27 *
28 *
29 *   LICENSE: zlib/libpng
30 *
31 *   Copyright (c) 2015-2021 Ramon Santamaria (@raysan5)
32 *
33 *   This software is provided "as-is", without any express or implied warranty. In no event
34 *   will the authors be held liable for any damages arising from the use of this software.
35 *
36 *   Permission is granted to anyone to use this software for any purpose, including commercial
37 *   applications, and to alter it and redistribute it freely, subject to the following restrictions:
38 *
39 *     1. The origin of this software must not be misrepresented; you must not claim that you
40 *     wrote the original software. If you use this software in a product, an acknowledgment
41 *     in the product documentation would be appreciated but is not required.
42 *
43 *     2. Altered source versions must be plainly marked as such, and must not be misrepresented
44 *     as being the original software.
45 *
46 *     3. This notice may not be removed or altered from any source distribution.
47 *
48 **********************************************************************************************/
49 
50 extern (C) @nogc nothrow:
51 
52 // Function specifiers definition
53 
54 // We are building raylib as a Win32 shared library (.dll).
55 
56 // We are using raylib as a Win32 shared library (.dll)
57 
58 // Provide external definition
59 
60 // Functions may be inlined, no external out-of-line definition
61 
62 // plain inline not supported by tinycc (See issue #435) // Functions may be inlined or external definition used
63 
64 //----------------------------------------------------------------------------------
65 // Defines and Macros
66 //----------------------------------------------------------------------------------
67 
68 enum PI = 3.14159265358979323846f;
69 
70 enum DEG2RAD = PI / 180.0f;
71 
72 enum RAD2DEG = 180.0f / PI;
73 
74 // Get float vector for Matrix
75 
76 extern (D) auto MatrixToFloat(T)(auto ref T mat)
77 {
78     return MatrixToFloatV(mat).v;
79 }
80 
81 // Get float vector for Vector3
82 
83 extern (D) auto Vector3ToFloat(T)(auto ref T vec)
84 {
85     return Vector3ToFloatV(vec).v;
86 }
87 
88 //----------------------------------------------------------------------------------
89 // Types and Structures Definition
90 //----------------------------------------------------------------------------------
91 
92 // Vector2 type
93 
94 // Vector3 type
95 
96 // Vector4 type
97 
98 // Quaternion type
99 
100 // Matrix type (OpenGL style 4x4 - right handed, column major)
101 
102 // Matrix first row (4 components)
103 // Matrix second row (4 components)
104 // Matrix third row (4 components)
105 // Matrix fourth row (4 components)
106 
107 // NOTE: Helper types to be used instead of array return types for *ToFloat functions
108 struct float3
109 {
110     float[3] v;
111 }
112 
113 struct float16
114 {
115     float[16] v;
116 }
117 
118 // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), fminf(), fmaxf(), fabs()
119 
120 //----------------------------------------------------------------------------------
121 // Module Functions Definition - Utils math
122 //----------------------------------------------------------------------------------
123 
124 // Clamp float value
125 float Clamp(float value, float min, float max);
126 
127 // Calculate linear interpolation between two floats
128 float Lerp(float start, float end, float amount);
129 
130 // Normalize input value within input range
131 float Normalize(float value, float start, float end);
132 
133 // Remap input value within input range to output range
134 float Remap(
135     float value,
136     float inputStart,
137     float inputEnd,
138     float outputStart,
139     float outputEnd);
140 
141 //----------------------------------------------------------------------------------
142 // Module Functions Definition - Vector2 math
143 //----------------------------------------------------------------------------------
144 
145 // Vector with components value 0.0f
146 Vector2 Vector2Zero();
147 
148 // Vector with components value 1.0f
149 Vector2 Vector2One();
150 
151 // Add two vectors (v1 + v2)
152 Vector2 Vector2Add(Vector2 v1, Vector2 v2);
153 
154 // Add vector and float value
155 Vector2 Vector2AddValue(Vector2 v, float add);
156 
157 // Subtract two vectors (v1 - v2)
158 Vector2 Vector2Subtract(Vector2 v1, Vector2 v2);
159 
160 // Subtract vector by float value
161 Vector2 Vector2SubtractValue(Vector2 v, float sub);
162 
163 // Calculate vector length
164 float Vector2Length(Vector2 v);
165 
166 // Calculate vector square length
167 float Vector2LengthSqr(Vector2 v);
168 
169 // Calculate two vectors dot product
170 float Vector2DotProduct(Vector2 v1, Vector2 v2);
171 
172 // Calculate distance between two vectors
173 float Vector2Distance(Vector2 v1, Vector2 v2);
174 
175 // Calculate angle from two vectors in X-axis
176 float Vector2Angle(Vector2 v1, Vector2 v2);
177 
178 // Scale vector (multiply by value)
179 Vector2 Vector2Scale(Vector2 v, float scale);
180 
181 // Multiply vector by vector
182 Vector2 Vector2Multiply(Vector2 v1, Vector2 v2);
183 
184 // Negate vector
185 Vector2 Vector2Negate(Vector2 v);
186 
187 // Divide vector by vector
188 Vector2 Vector2Divide(Vector2 v1, Vector2 v2);
189 
190 // Normalize provided vector
191 Vector2 Vector2Normalize(Vector2 v);
192 
193 // Calculate linear interpolation between two vectors
194 Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount);
195 
196 // Calculate reflected vector to normal
197 
198 // Dot product
199 Vector2 Vector2Reflect(Vector2 v, Vector2 normal);
200 
201 // Rotate vector by angle
202 Vector2 Vector2Rotate(Vector2 v, float angle);
203 
204 // Move Vector towards target
205 Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance);
206 
207 //----------------------------------------------------------------------------------
208 // Module Functions Definition - Vector3 math
209 //----------------------------------------------------------------------------------
210 
211 // Vector with components value 0.0f
212 Vector3 Vector3Zero();
213 
214 // Vector with components value 1.0f
215 Vector3 Vector3One();
216 
217 // Add two vectors
218 Vector3 Vector3Add(Vector3 v1, Vector3 v2);
219 
220 // Add vector and float value
221 Vector3 Vector3AddValue(Vector3 v, float add);
222 
223 // Subtract two vectors
224 Vector3 Vector3Subtract(Vector3 v1, Vector3 v2);
225 
226 // Subtract vector by float value
227 Vector3 Vector3SubtractValue(Vector3 v, float sub);
228 
229 // Multiply vector by scalar
230 Vector3 Vector3Scale(Vector3 v, float scalar);
231 
232 // Multiply vector by vector
233 Vector3 Vector3Multiply(Vector3 v1, Vector3 v2);
234 
235 // Calculate two vectors cross product
236 Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2);
237 
238 // Calculate one vector perpendicular vector
239 
240 // Cross product between vectors
241 Vector3 Vector3Perpendicular(Vector3 v);
242 
243 // Calculate vector length
244 float Vector3Length(const Vector3 v);
245 
246 // Calculate vector square length
247 float Vector3LengthSqr(const Vector3 v);
248 
249 // Calculate two vectors dot product
250 float Vector3DotProduct(Vector3 v1, Vector3 v2);
251 
252 // Calculate distance between two vectors
253 float Vector3Distance(Vector3 v1, Vector3 v2);
254 
255 // Calculate angle between two vectors in XY and XZ
256 
257 // Angle in XZ
258 // Angle in XY
259 Vector2 Vector3Angle(Vector3 v1, Vector3 v2);
260 
261 // Negate provided vector (invert direction)
262 Vector3 Vector3Negate(Vector3 v);
263 
264 // Divide vector by vector
265 Vector3 Vector3Divide(Vector3 v1, Vector3 v2);
266 
267 // Normalize provided vector
268 Vector3 Vector3Normalize(Vector3 v);
269 
270 // Orthonormalize provided vectors
271 // Makes vectors normalized and orthogonal to each other
272 // Gram-Schmidt function implementation
273 
274 // Vector3Normalize(*v1);
275 
276 // Vector3CrossProduct(*v1, *v2)
277 
278 // Vector3Normalize(vn1);
279 
280 // Vector3CrossProduct(vn1, *v1)
281 void Vector3OrthoNormalize(Vector3* v1, Vector3* v2);
282 
283 // Transforms a Vector3 by a given Matrix
284 Vector3 Vector3Transform(Vector3 v, Matrix mat);
285 
286 // Transform a vector by quaternion rotation
287 Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q);
288 
289 // Calculate linear interpolation between two vectors
290 Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount);
291 
292 // Calculate reflected vector to normal
293 
294 // I is the original vector
295 // N is the normal of the incident plane
296 // R = I - (2*N*(DotProduct[I, N]))
297 Vector3 Vector3Reflect(Vector3 v, Vector3 normal);
298 
299 // Get min value for each pair of components
300 Vector3 Vector3Min(Vector3 v1, Vector3 v2);
301 
302 // Get max value for each pair of components
303 Vector3 Vector3Max(Vector3 v1, Vector3 v2);
304 
305 // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
306 // NOTE: Assumes P is on the plane of the triangle
307 
308 // Vector3Subtract(b, a)
309 // Vector3Subtract(c, a)
310 // Vector3Subtract(p, a)
311 // Vector3DotProduct(v0, v0)
312 // Vector3DotProduct(v0, v1)
313 // Vector3DotProduct(v1, v1)
314 // Vector3DotProduct(v2, v0)
315 // Vector3DotProduct(v2, v1)
316 Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c);
317 
318 // Projects a Vector3 from screen space into object space
319 // NOTE: We are avoiding calling other raymath functions despite available
320 
321 // Calculate unproject matrix (multiply view patrix by projection matrix) and invert it
322 // MatrixMultiply(view, projection);
323 
324 // Calculate inverted matrix -> MatrixInvert(matViewProj);
325 // Cache the matrix values (speed optimization)
326 
327 // Calculate the invert determinant (inlined to avoid double-caching)
328 
329 // Create quaternion from source point
330 
331 // Multiply quat point by unproject matrix
332 // QuaternionTransform(quat, matViewProjInv)
333 
334 // Normalized world points in vectors
335 Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view);
336 
337 // Get Vector3 as float array
338 float3 Vector3ToFloatV(Vector3 v);
339 
340 //----------------------------------------------------------------------------------
341 // Module Functions Definition - Matrix math
342 //----------------------------------------------------------------------------------
343 
344 // Compute matrix determinant
345 
346 // Cache the matrix values (speed optimization)
347 float MatrixDeterminant(Matrix mat);
348 
349 // Get the trace of the matrix (sum of the values along the diagonal)
350 float MatrixTrace(Matrix mat);
351 
352 // Transposes provided matrix
353 Matrix MatrixTranspose(Matrix mat);
354 
355 // Invert provided matrix
356 
357 // Cache the matrix values (speed optimization)
358 
359 // Calculate the invert determinant (inlined to avoid double-caching)
360 Matrix MatrixInvert(Matrix mat);
361 
362 // Normalize provided matrix
363 
364 // Cache the matrix values (speed optimization)
365 
366 // MatrixDeterminant(mat)
367 Matrix MatrixNormalize(Matrix mat);
368 
369 // Get identity matrix
370 Matrix MatrixIdentity();
371 
372 // Add two matrices
373 Matrix MatrixAdd(Matrix left, Matrix right);
374 
375 // Subtract two matrices (left - right)
376 Matrix MatrixSubtract(Matrix left, Matrix right);
377 
378 // Get two matrix multiplication
379 // NOTE: When multiplying matrices... the order matters!
380 Matrix MatrixMultiply(Matrix left, Matrix right);
381 
382 // Get translation matrix
383 Matrix MatrixTranslate(float x, float y, float z);
384 
385 // Create rotation matrix from axis and angle
386 // NOTE: Angle should be provided in radians
387 Matrix MatrixRotate(Vector3 axis, float angle);
388 
389 // Get x-rotation matrix (angle in radians)
390 
391 // MatrixIdentity()
392 Matrix MatrixRotateX(float angle);
393 
394 // Get y-rotation matrix (angle in radians)
395 
396 // MatrixIdentity()
397 Matrix MatrixRotateY(float angle);
398 
399 // Get z-rotation matrix (angle in radians)
400 
401 // MatrixIdentity()
402 Matrix MatrixRotateZ(float angle);
403 
404 // Get xyz-rotation matrix (angles in radians)
405 
406 // MatrixIdentity()
407 Matrix MatrixRotateXYZ(Vector3 ang);
408 
409 // Get zyx-rotation matrix (angles in radians)
410 Matrix MatrixRotateZYX(Vector3 ang);
411 
412 // Get scaling matrix
413 Matrix MatrixScale(float x, float y, float z);
414 
415 // Get perspective projection matrix
416 Matrix MatrixFrustum(
417     double left,
418     double right,
419     double bottom,
420     double top,
421     double near,
422     double far);
423 
424 // Get perspective projection matrix
425 // NOTE: Angle should be provided in radians
426 
427 // MatrixFrustum(-right, right, -top, top, near, far);
428 Matrix MatrixPerspective(double fovy, double aspect, double near, double far);
429 
430 // Get orthographic projection matrix
431 Matrix MatrixOrtho(
432     double left,
433     double right,
434     double bottom,
435     double top,
436     double near,
437     double far);
438 
439 // Get camera look-at matrix (view matrix)
440 
441 // Vector3Subtract(eye, target)
442 
443 // Vector3Normalize(vz)
444 
445 // Vector3CrossProduct(up, vz)
446 
447 // Vector3Normalize(x)
448 
449 // Vector3CrossProduct(vz, vx)
450 
451 // Vector3DotProduct(vx, eye)
452 // Vector3DotProduct(vy, eye)
453 // Vector3DotProduct(vz, eye)
454 Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up);
455 
456 // Get float array of matrix data
457 float16 MatrixToFloatV(Matrix mat);
458 
459 //----------------------------------------------------------------------------------
460 // Module Functions Definition - Quaternion math
461 //----------------------------------------------------------------------------------
462 
463 // Add two quaternions
464 Quaternion QuaternionAdd(Quaternion q1, Quaternion q2);
465 
466 // Add quaternion and float value
467 Quaternion QuaternionAddValue(Quaternion q, float add);
468 
469 // Subtract two quaternions
470 Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2);
471 
472 // Subtract quaternion and float value
473 Quaternion QuaternionSubtractValue(Quaternion q, float sub);
474 
475 // Get identity quaternion
476 Quaternion QuaternionIdentity();
477 
478 // Computes the length of a quaternion
479 float QuaternionLength(Quaternion q);
480 
481 // Normalize provided quaternion
482 Quaternion QuaternionNormalize(Quaternion q);
483 
484 // Invert provided quaternion
485 Quaternion QuaternionInvert(Quaternion q);
486 
487 // Calculate two quaternion multiplication
488 Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2);
489 
490 // Scale quaternion by float value
491 Quaternion QuaternionScale(Quaternion q, float mul);
492 
493 // Divide two quaternions
494 Quaternion QuaternionDivide(Quaternion q1, Quaternion q2);
495 
496 // Calculate linear interpolation between two quaternions
497 Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount);
498 
499 // Calculate slerp-optimized interpolation between two quaternions
500 
501 // QuaternionLerp(q1, q2, amount)
502 
503 // QuaternionNormalize(q);
504 Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount);
505 
506 // Calculates spherical linear interpolation between two quaternions
507 Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount);
508 
509 // Calculate quaternion based on the rotation from one vector to another
510 
511 // Vector3DotProduct(from, to)
512 // Vector3CrossProduct(from, to)
513 
514 // QuaternionNormalize(q);
515 // NOTE: Normalize to essentially nlerp the original and identity to 0.5
516 Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to);
517 
518 // Get a quaternion for a given rotation matrix
519 Quaternion QuaternionFromMatrix(Matrix mat);
520 
521 // Get a matrix for a given quaternion
522 
523 // MatrixIdentity()
524 Matrix QuaternionToMatrix(Quaternion q);
525 
526 // Get rotation quaternion for an angle and axis
527 // NOTE: angle must be provided in radians
528 
529 // Vector3Normalize(axis)
530 
531 // QuaternionNormalize(q);
532 Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle);
533 
534 // Get the rotation angle and axis for a given quaternion
535 
536 // QuaternionNormalize(q);
537 
538 // This occurs when the angle is zero.
539 // Not a problem: just set an arbitrary normalized axis.
540 void QuaternionToAxisAngle(Quaternion q, Vector3* outAxis, float* outAngle);
541 
542 // Get the quaternion equivalent to Euler angles
543 // NOTE: Rotation order is ZYX
544 Quaternion QuaternionFromEuler(float pitch, float yaw, float roll);
545 
546 // Get the Euler angles equivalent to quaternion (roll, pitch, yaw)
547 // NOTE: Angles are returned in a Vector3 struct in radians
548 
549 // Roll (x-axis rotation)
550 
551 // Pitch (y-axis rotation)
552 
553 // Yaw (z-axis rotation)
554 Vector3 QuaternionToEuler(Quaternion q);
555 
556 // Transform a quaternion given a transformation matrix
557 Quaternion QuaternionTransform(Quaternion q, Matrix mat);
558 
559 // RAYMATH_H