1 module raylib.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" 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-2022 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 EPSILON = 0.000001f;
71 
72 enum DEG2RAD = PI / 180.0f;
73 
74 enum RAD2DEG = 180.0f / PI;
75 
76 // Get float vector for Matrix
77 
78 extern (D) auto MatrixToFloat(T)(auto ref T mat)
79 {
80     return MatrixToFloatV(mat).v;
81 }
82 
83 // Get float vector for Vector3
84 
85 extern (D) auto Vector3ToFloat(T)(auto ref T vec)
86 {
87     return Vector3ToFloatV(vec).v;
88 }
89 
90 //----------------------------------------------------------------------------------
91 // Types and Structures Definition
92 //----------------------------------------------------------------------------------
93 
94 // Vector2 type
95 
96 // Vector3 type
97 
98 // Vector4 type
99 
100 // Quaternion type
101 
102 // Matrix type (OpenGL style 4x4 - right handed, column major)
103 
104 // Matrix first row (4 components)
105 // Matrix second row (4 components)
106 // Matrix third row (4 components)
107 // Matrix fourth row (4 components)
108 
109 // NOTE: Helper types to be used instead of array return types for *ToFloat functions
110 struct float3
111 {
112     float[3] v;
113 }
114 
115 struct float16
116 {
117     float[16] v;
118 }
119 
120 // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), floor(), fminf(), fmaxf(), fabs()
121 
122 //----------------------------------------------------------------------------------
123 // Module Functions Definition - Utils math
124 //----------------------------------------------------------------------------------
125 
126 // Clamp float value
127 float Clamp(float value, float min, float max);
128 
129 // Calculate linear interpolation between two floats
130 float Lerp(float start, float end, float amount);
131 
132 // Normalize input value within input range
133 float Normalize(float value, float start, float end);
134 
135 // Remap input value within input range to output range
136 float Remap(
137     float value,
138     float inputStart,
139     float inputEnd,
140     float outputStart,
141     float outputEnd);
142 
143 // Wrap input value from min to max
144 float Wrap(float value, float min, float max);
145 
146 // Check whether two given floats are almost equal
147 int FloatEquals(float x, float y);
148 
149 //----------------------------------------------------------------------------------
150 // Module Functions Definition - Vector2 math
151 //----------------------------------------------------------------------------------
152 
153 // Vector with components value 0.0f
154 Vector2 Vector2Zero();
155 
156 // Vector with components value 1.0f
157 Vector2 Vector2One();
158 
159 // Add two vectors (v1 + v2)
160 Vector2 Vector2Add(Vector2 v1, Vector2 v2);
161 
162 // Add vector and float value
163 Vector2 Vector2AddValue(Vector2 v, float add);
164 
165 // Subtract two vectors (v1 - v2)
166 Vector2 Vector2Subtract(Vector2 v1, Vector2 v2);
167 
168 // Subtract vector by float value
169 Vector2 Vector2SubtractValue(Vector2 v, float sub);
170 
171 // Calculate vector length
172 float Vector2Length(Vector2 v);
173 
174 // Calculate vector square length
175 float Vector2LengthSqr(Vector2 v);
176 
177 // Calculate two vectors dot product
178 float Vector2DotProduct(Vector2 v1, Vector2 v2);
179 
180 // Calculate distance between two vectors
181 float Vector2Distance(Vector2 v1, Vector2 v2);
182 
183 // Calculate square distance between two vectors
184 float Vector2DistanceSqr(Vector2 v1, Vector2 v2);
185 
186 // Calculate angle from two vectors
187 float Vector2Angle(Vector2 v1, Vector2 v2);
188 
189 // Scale vector (multiply by value)
190 Vector2 Vector2Scale(Vector2 v, float scale);
191 
192 // Multiply vector by vector
193 Vector2 Vector2Multiply(Vector2 v1, Vector2 v2);
194 
195 // Negate vector
196 Vector2 Vector2Negate(Vector2 v);
197 
198 // Divide vector by vector
199 Vector2 Vector2Divide(Vector2 v1, Vector2 v2);
200 
201 // Normalize provided vector
202 Vector2 Vector2Normalize(Vector2 v);
203 
204 // Transforms a Vector2 by a given Matrix
205 Vector2 Vector2Transform(Vector2 v, Matrix mat);
206 
207 // Calculate linear interpolation between two vectors
208 Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount);
209 
210 // Calculate reflected vector to normal
211 
212 // Dot product
213 Vector2 Vector2Reflect(Vector2 v, Vector2 normal);
214 
215 // Rotate vector by angle
216 Vector2 Vector2Rotate(Vector2 v, float angle);
217 
218 // Move Vector towards target
219 Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance);
220 
221 // Invert the given vector
222 Vector2 Vector2Invert(Vector2 v);
223 
224 // Clamp the components of the vector between
225 // min and max values specified by the given vectors
226 Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max);
227 
228 // Clamp the magnitude of the vector between two min and max values
229 Vector2 Vector2ClampValue(Vector2 v, float min, float max);
230 
231 // Check whether two given vectors are almost equal
232 int Vector2Equals(Vector2 p, Vector2 q);
233 
234 //----------------------------------------------------------------------------------
235 // Module Functions Definition - Vector3 math
236 //----------------------------------------------------------------------------------
237 
238 // Vector with components value 0.0f
239 Vector3 Vector3Zero();
240 
241 // Vector with components value 1.0f
242 Vector3 Vector3One();
243 
244 // Add two vectors
245 Vector3 Vector3Add(Vector3 v1, Vector3 v2);
246 
247 // Add vector and float value
248 Vector3 Vector3AddValue(Vector3 v, float add);
249 
250 // Subtract two vectors
251 Vector3 Vector3Subtract(Vector3 v1, Vector3 v2);
252 
253 // Subtract vector by float value
254 Vector3 Vector3SubtractValue(Vector3 v, float sub);
255 
256 // Multiply vector by scalar
257 Vector3 Vector3Scale(Vector3 v, float scalar);
258 
259 // Multiply vector by vector
260 Vector3 Vector3Multiply(Vector3 v1, Vector3 v2);
261 
262 // Calculate two vectors cross product
263 Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2);
264 
265 // Calculate one vector perpendicular vector
266 
267 // Cross product between vectors
268 Vector3 Vector3Perpendicular(Vector3 v);
269 
270 // Calculate vector length
271 float Vector3Length(const Vector3 v);
272 
273 // Calculate vector square length
274 float Vector3LengthSqr(const Vector3 v);
275 
276 // Calculate two vectors dot product
277 float Vector3DotProduct(Vector3 v1, Vector3 v2);
278 
279 // Calculate distance between two vectors
280 float Vector3Distance(Vector3 v1, Vector3 v2);
281 
282 // Calculate square distance between two vectors
283 float Vector3DistanceSqr(Vector3 v1, Vector3 v2);
284 
285 // Calculate angle between two vectors
286 float Vector3Angle(Vector3 v1, Vector3 v2);
287 
288 // Negate provided vector (invert direction)
289 Vector3 Vector3Negate(Vector3 v);
290 
291 // Divide vector by vector
292 Vector3 Vector3Divide(Vector3 v1, Vector3 v2);
293 
294 // Normalize provided vector
295 Vector3 Vector3Normalize(Vector3 v);
296 
297 // Orthonormalize provided vectors
298 // Makes vectors normalized and orthogonal to each other
299 // Gram-Schmidt function implementation
300 
301 // Vector3Normalize(*v1);
302 
303 // Vector3CrossProduct(*v1, *v2)
304 
305 // Vector3Normalize(vn1);
306 
307 // Vector3CrossProduct(vn1, *v1)
308 void Vector3OrthoNormalize(Vector3* v1, Vector3* v2);
309 
310 // Transforms a Vector3 by a given Matrix
311 Vector3 Vector3Transform(Vector3 v, Matrix mat);
312 
313 // Transform a vector by quaternion rotation
314 Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q);
315 
316 // Rotates a vector around an axis
317 
318 // Using Euler-Rodrigues Formula
319 // Ref.: https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula
320 
321 // Vector3Normalize(axis);
322 
323 // Vector3CrossProduct(w, v)
324 
325 // Vector3CrossProduct(w, wv)
326 
327 // Vector3Scale(wv, 2 * a)
328 
329 // Vector3Scale(wwv, 2)
330 Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle);
331 
332 // Calculate linear interpolation between two vectors
333 Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount);
334 
335 // Calculate reflected vector to normal
336 
337 // I is the original vector
338 // N is the normal of the incident plane
339 // R = I - (2*N*(DotProduct[I, N]))
340 Vector3 Vector3Reflect(Vector3 v, Vector3 normal);
341 
342 // Get min value for each pair of components
343 Vector3 Vector3Min(Vector3 v1, Vector3 v2);
344 
345 // Get max value for each pair of components
346 Vector3 Vector3Max(Vector3 v1, Vector3 v2);
347 
348 // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
349 // NOTE: Assumes P is on the plane of the triangle
350 
351 // Vector3Subtract(b, a)
352 // Vector3Subtract(c, a)
353 // Vector3Subtract(p, a)
354 // Vector3DotProduct(v0, v0)
355 // Vector3DotProduct(v0, v1)
356 // Vector3DotProduct(v1, v1)
357 // Vector3DotProduct(v2, v0)
358 // Vector3DotProduct(v2, v1)
359 Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c);
360 
361 // Projects a Vector3 from screen space into object space
362 // NOTE: We are avoiding calling other raymath functions despite available
363 
364 // Calculate unproject matrix (multiply view patrix by projection matrix) and invert it
365 // MatrixMultiply(view, projection);
366 
367 // Calculate inverted matrix -> MatrixInvert(matViewProj);
368 // Cache the matrix values (speed optimization)
369 
370 // Calculate the invert determinant (inlined to avoid double-caching)
371 
372 // Create quaternion from source point
373 
374 // Multiply quat point by unproject matrix
375 // QuaternionTransform(quat, matViewProjInv)
376 
377 // Normalized world points in vectors
378 Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view);
379 
380 // Get Vector3 as float array
381 float3 Vector3ToFloatV(Vector3 v);
382 
383 // Invert the given vector
384 Vector3 Vector3Invert(Vector3 v);
385 
386 // Clamp the components of the vector between
387 // min and max values specified by the given vectors
388 Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max);
389 
390 // Clamp the magnitude of the vector between two values
391 Vector3 Vector3ClampValue(Vector3 v, float min, float max);
392 
393 // Check whether two given vectors are almost equal
394 int Vector3Equals(Vector3 p, Vector3 q);
395 
396 // Compute the direction of a refracted ray where v specifies the
397 // normalized direction of the incoming ray, n specifies the
398 // normalized normal vector of the interface of two optical media,
399 // and r specifies the ratio of the refractive index of the medium
400 // from where the ray comes to the refractive index of the medium
401 // on the other side of the surface
402 Vector3 Vector3Refract(Vector3 v, Vector3 n, float r);
403 
404 //----------------------------------------------------------------------------------
405 // Module Functions Definition - Matrix math
406 //----------------------------------------------------------------------------------
407 
408 // Compute matrix determinant
409 
410 // Cache the matrix values (speed optimization)
411 float MatrixDeterminant(Matrix mat);
412 
413 // Get the trace of the matrix (sum of the values along the diagonal)
414 float MatrixTrace(Matrix mat);
415 
416 // Transposes provided matrix
417 Matrix MatrixTranspose(Matrix mat);
418 
419 // Invert provided matrix
420 
421 // Cache the matrix values (speed optimization)
422 
423 // Calculate the invert determinant (inlined to avoid double-caching)
424 Matrix MatrixInvert(Matrix mat);
425 
426 // Get identity matrix
427 Matrix MatrixIdentity();
428 
429 // Add two matrices
430 Matrix MatrixAdd(Matrix left, Matrix right);
431 
432 // Subtract two matrices (left - right)
433 Matrix MatrixSubtract(Matrix left, Matrix right);
434 
435 // Get two matrix multiplication
436 // NOTE: When multiplying matrices... the order matters!
437 Matrix MatrixMultiply(Matrix left, Matrix right);
438 
439 // Get translation matrix
440 Matrix MatrixTranslate(float x, float y, float z);
441 
442 // Create rotation matrix from axis and angle
443 // NOTE: Angle should be provided in radians
444 Matrix MatrixRotate(Vector3 axis, float angle);
445 
446 // Get x-rotation matrix
447 // NOTE: Angle must be provided in radians
448 
449 // MatrixIdentity()
450 Matrix MatrixRotateX(float angle);
451 
452 // Get y-rotation matrix
453 // NOTE: Angle must be provided in radians
454 
455 // MatrixIdentity()
456 Matrix MatrixRotateY(float angle);
457 
458 // Get z-rotation matrix
459 // NOTE: Angle must be provided in radians
460 
461 // MatrixIdentity()
462 Matrix MatrixRotateZ(float angle);
463 
464 // Get xyz-rotation matrix
465 // NOTE: Angle must be provided in radians
466 
467 // MatrixIdentity()
468 Matrix MatrixRotateXYZ(Vector3 angle);
469 
470 // Get zyx-rotation matrix
471 // NOTE: Angle must be provided in radians
472 Matrix MatrixRotateZYX(Vector3 angle);
473 
474 // Get scaling matrix
475 Matrix MatrixScale(float x, float y, float z);
476 
477 // Get perspective projection matrix
478 Matrix MatrixFrustum(
479     double left,
480     double right,
481     double bottom,
482     double top,
483     double near,
484     double far);
485 
486 // Get perspective projection matrix
487 // NOTE: Fovy angle must be provided in radians
488 
489 // MatrixFrustum(-right, right, -top, top, near, far);
490 Matrix MatrixPerspective(double fovy, double aspect, double near, double far);
491 
492 // Get orthographic projection matrix
493 Matrix MatrixOrtho(
494     double left,
495     double right,
496     double bottom,
497     double top,
498     double near,
499     double far);
500 
501 // Get camera look-at matrix (view matrix)
502 
503 // Vector3Subtract(eye, target)
504 
505 // Vector3Normalize(vz)
506 
507 // Vector3CrossProduct(up, vz)
508 
509 // Vector3Normalize(x)
510 
511 // Vector3CrossProduct(vz, vx)
512 
513 // Vector3DotProduct(vx, eye)
514 // Vector3DotProduct(vy, eye)
515 // Vector3DotProduct(vz, eye)
516 Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up);
517 
518 // Get float array of matrix data
519 float16 MatrixToFloatV(Matrix mat);
520 
521 //----------------------------------------------------------------------------------
522 // Module Functions Definition - Quaternion math
523 //----------------------------------------------------------------------------------
524 
525 // Add two quaternions
526 Quaternion QuaternionAdd(Quaternion q1, Quaternion q2);
527 
528 // Add quaternion and float value
529 Quaternion QuaternionAddValue(Quaternion q, float add);
530 
531 // Subtract two quaternions
532 Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2);
533 
534 // Subtract quaternion and float value
535 Quaternion QuaternionSubtractValue(Quaternion q, float sub);
536 
537 // Get identity quaternion
538 Quaternion QuaternionIdentity();
539 
540 // Computes the length of a quaternion
541 float QuaternionLength(Quaternion q);
542 
543 // Normalize provided quaternion
544 Quaternion QuaternionNormalize(Quaternion q);
545 
546 // Invert provided quaternion
547 Quaternion QuaternionInvert(Quaternion q);
548 
549 // Calculate two quaternion multiplication
550 Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2);
551 
552 // Scale quaternion by float value
553 Quaternion QuaternionScale(Quaternion q, float mul);
554 
555 // Divide two quaternions
556 Quaternion QuaternionDivide(Quaternion q1, Quaternion q2);
557 
558 // Calculate linear interpolation between two quaternions
559 Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount);
560 
561 // Calculate slerp-optimized interpolation between two quaternions
562 
563 // QuaternionLerp(q1, q2, amount)
564 
565 // QuaternionNormalize(q);
566 Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount);
567 
568 // Calculates spherical linear interpolation between two quaternions
569 Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount);
570 
571 // Calculate quaternion based on the rotation from one vector to another
572 
573 // Vector3DotProduct(from, to)
574 // Vector3CrossProduct(from, to)
575 
576 // QuaternionNormalize(q);
577 // NOTE: Normalize to essentially nlerp the original and identity to 0.5
578 Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to);
579 
580 // Get a quaternion for a given rotation matrix
581 Quaternion QuaternionFromMatrix(Matrix mat);
582 
583 // Get a matrix for a given quaternion
584 
585 // MatrixIdentity()
586 Matrix QuaternionToMatrix(Quaternion q);
587 
588 // Get rotation quaternion for an angle and axis
589 // NOTE: Angle must be provided in radians
590 
591 // Vector3Normalize(axis)
592 
593 // QuaternionNormalize(q);
594 Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle);
595 
596 // Get the rotation angle and axis for a given quaternion
597 
598 // QuaternionNormalize(q);
599 
600 // This occurs when the angle is zero.
601 // Not a problem: just set an arbitrary normalized axis.
602 void QuaternionToAxisAngle(Quaternion q, Vector3* outAxis, float* outAngle);
603 
604 // Get the quaternion equivalent to Euler angles
605 // NOTE: Rotation order is ZYX
606 Quaternion QuaternionFromEuler(float pitch, float yaw, float roll);
607 
608 // Get the Euler angles equivalent to quaternion (roll, pitch, yaw)
609 // NOTE: Angles are returned in a Vector3 struct in radians
610 
611 // Roll (x-axis rotation)
612 
613 // Pitch (y-axis rotation)
614 
615 // Yaw (z-axis rotation)
616 Vector3 QuaternionToEuler(Quaternion q);
617 
618 // Transform a quaternion given a transformation matrix
619 Quaternion QuaternionTransform(Quaternion q, Matrix mat);
620 
621 // Check whether two given quaternions are almost equal
622 int QuaternionEquals(Quaternion p, Quaternion q);
623 
624 // RAYMATH_H