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