raymath.d 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. module raymath;
  2. import raylib;
  3. /**********************************************************************************************
  4. *
  5. * raymath v1.2 - Math functions to work with Vector3, Matrix and Quaternions
  6. *
  7. * CONFIGURATION:
  8. *
  9. * #define RAYMATH_IMPLEMENTATION
  10. * Generates the implementation of the library into the included file.
  11. * If not defined, the library is in header only mode and can be included in other headers
  12. * or source files without problems. But only ONE file should hold the implementation.
  13. *
  14. * #define RAYMATH_HEADER_ONLY
  15. * Define static inline functions code, so #include header suffices for use.
  16. * This may use up lots of memory.
  17. *
  18. * #define RAYMATH_STANDALONE
  19. * Avoid raylib.h header inclusion in this file.
  20. * Vector3 and Matrix data types are defined internally in raymath module.
  21. *
  22. *
  23. * LICENSE: zlib/libpng
  24. *
  25. * Copyright (c) 2015-2020 Ramon Santamaria (@raysan5)
  26. *
  27. * This software is provided "as-is", without any express or implied warranty. In no event
  28. * will the authors be held liable for any damages arising from the use of this software.
  29. *
  30. * Permission is granted to anyone to use this software for any purpose, including commercial
  31. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  32. *
  33. * 1. The origin of this software must not be misrepresented; you must not claim that you
  34. * wrote the original software. If you use this software in a product, an acknowledgment
  35. * in the product documentation would be appreciated but is not required.
  36. *
  37. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  38. * as being the original software.
  39. *
  40. * 3. This notice may not be removed or altered from any source distribution.
  41. *
  42. **********************************************************************************************/
  43. extern (C):
  44. //#define RAYMATH_STANDALONE // NOTE: To use raymath as standalone lib, just uncomment this line
  45. //#define RAYMATH_HEADER_ONLY // NOTE: To compile functions as static inline, uncomment this line
  46. // Required for structs: Vector3, Matrix
  47. // We are building raylib as a Win32 shared library (.dll).
  48. // We are using raylib as a Win32 shared library (.dll)
  49. // Provide external definition
  50. // Functions may be inlined, no external out-of-line definition
  51. // plain inline not supported by tinycc (See issue #435) // Functions may be inlined or external definition used
  52. //----------------------------------------------------------------------------------
  53. // Defines and Macros
  54. //----------------------------------------------------------------------------------
  55. // Return float vector for Matrix
  56. extern (D) auto MatrixToFloat(T)(auto ref T mat)
  57. {
  58. return MatrixToFloatV(mat).v;
  59. }
  60. // Return float vector for Vector3
  61. extern (D) auto Vector3ToFloat(T)(auto ref T vec)
  62. {
  63. return Vector3ToFloatV(vec).v;
  64. }
  65. //----------------------------------------------------------------------------------
  66. // Types and Structures Definition
  67. //----------------------------------------------------------------------------------
  68. // Vector2 type
  69. // Vector3 type
  70. // Quaternion type
  71. // Matrix type (OpenGL style 4x4 - right handed, column major)
  72. // NOTE: Helper types to be used instead of array return types for *ToFloat functions
  73. struct float3
  74. {
  75. float[3] v;
  76. }
  77. struct float16
  78. {
  79. float[16] v;
  80. }
  81. // Required for: sinf(), cosf(), sqrtf(), tan(), fabs()
  82. //----------------------------------------------------------------------------------
  83. // Module Functions Definition - Utils math
  84. //----------------------------------------------------------------------------------
  85. // Clamp float value
  86. float Clamp (float value, float min, float max);
  87. // Calculate linear interpolation between two floats
  88. float Lerp (float start, float end, float amount);
  89. //----------------------------------------------------------------------------------
  90. // Module Functions Definition - Vector2 math
  91. //----------------------------------------------------------------------------------
  92. // Vector with components value 0.0f
  93. Vector2 Vector2Zero ();
  94. // Vector with components value 1.0f
  95. Vector2 Vector2One ();
  96. // Add two vectors (v1 + v2)
  97. Vector2 Vector2Add (Vector2 v1, Vector2 v2);
  98. // Subtract two vectors (v1 - v2)
  99. Vector2 Vector2Subtract (Vector2 v1, Vector2 v2);
  100. // Calculate vector length
  101. float Vector2Length (Vector2 v);
  102. // Calculate two vectors dot product
  103. float Vector2DotProduct (Vector2 v1, Vector2 v2);
  104. // Calculate distance between two vectors
  105. float Vector2Distance (Vector2 v1, Vector2 v2);
  106. // Calculate angle from two vectors in X-axis
  107. float Vector2Angle (Vector2 v1, Vector2 v2);
  108. // Scale vector (multiply by value)
  109. Vector2 Vector2Scale (Vector2 v, float scale);
  110. // Multiply vector by vector
  111. Vector2 Vector2MultiplyV (Vector2 v1, Vector2 v2);
  112. // Negate vector
  113. Vector2 Vector2Negate (Vector2 v);
  114. // Divide vector by a float value
  115. Vector2 Vector2Divide (Vector2 v, float div);
  116. // Divide vector by vector
  117. Vector2 Vector2DivideV (Vector2 v1, Vector2 v2);
  118. // Normalize provided vector
  119. Vector2 Vector2Normalize (Vector2 v);
  120. // Calculate linear interpolation between two vectors
  121. Vector2 Vector2Lerp (Vector2 v1, Vector2 v2, float amount);
  122. // Rotate Vector by float in Degrees.
  123. Vector2 Vector2Rotate (Vector2 v, float degs);
  124. //----------------------------------------------------------------------------------
  125. // Module Functions Definition - Vector3 math
  126. //----------------------------------------------------------------------------------
  127. // Vector with components value 0.0f
  128. Vector3 Vector3Zero ();
  129. // Vector with components value 1.0f
  130. Vector3 Vector3One ();
  131. // Add two vectors
  132. Vector3 Vector3Add (Vector3 v1, Vector3 v2);
  133. // Subtract two vectors
  134. Vector3 Vector3Subtract (Vector3 v1, Vector3 v2);
  135. // Multiply vector by scalar
  136. Vector3 Vector3Scale (Vector3 v, float scalar);
  137. // Multiply vector by vector
  138. Vector3 Vector3Multiply (Vector3 v1, Vector3 v2);
  139. // Calculate two vectors cross product
  140. Vector3 Vector3CrossProduct (Vector3 v1, Vector3 v2);
  141. // Calculate one vector perpendicular vector
  142. Vector3 Vector3Perpendicular (Vector3 v);
  143. // Calculate vector length
  144. float Vector3Length (const Vector3 v);
  145. // Calculate two vectors dot product
  146. float Vector3DotProduct (Vector3 v1, Vector3 v2);
  147. // Calculate distance between two vectors
  148. float Vector3Distance (Vector3 v1, Vector3 v2);
  149. // Negate provided vector (invert direction)
  150. Vector3 Vector3Negate (Vector3 v);
  151. // Divide vector by a float value
  152. Vector3 Vector3Divide (Vector3 v, float div);
  153. // Divide vector by vector
  154. Vector3 Vector3DivideV (Vector3 v1, Vector3 v2);
  155. // Normalize provided vector
  156. Vector3 Vector3Normalize (Vector3 v);
  157. // Orthonormalize provided vectors
  158. // Makes vectors normalized and orthogonal to each other
  159. // Gram-Schmidt function implementation
  160. void Vector3OrthoNormalize (Vector3* v1, Vector3* v2);
  161. // Transforms a Vector3 by a given Matrix
  162. Vector3 Vector3Transform (Vector3 v, Matrix mat);
  163. // Transform a vector by quaternion rotation
  164. Vector3 Vector3RotateByQuaternion (Vector3 v, Quaternion q);
  165. // Calculate linear interpolation between two vectors
  166. Vector3 Vector3Lerp (Vector3 v1, Vector3 v2, float amount);
  167. // Calculate reflected vector to normal
  168. // I is the original vector
  169. // N is the normal of the incident plane
  170. // R = I - (2*N*( DotProduct[ I,N] ))
  171. Vector3 Vector3Reflect (Vector3 v, Vector3 normal);
  172. // Return min value for each pair of components
  173. Vector3 Vector3Min (Vector3 v1, Vector3 v2);
  174. // Return max value for each pair of components
  175. Vector3 Vector3Max (Vector3 v1, Vector3 v2);
  176. // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
  177. // NOTE: Assumes P is on the plane of the triangle
  178. //Vector v0 = b - a, v1 = c - a, v2 = p - a;
  179. Vector3 Vector3Barycenter (Vector3 p, Vector3 a, Vector3 b, Vector3 c);
  180. // Returns Vector3 as float array
  181. float3 Vector3ToFloatV (Vector3 v);
  182. //----------------------------------------------------------------------------------
  183. // Module Functions Definition - Matrix math
  184. //----------------------------------------------------------------------------------
  185. // Compute matrix determinant
  186. // Cache the matrix values (speed optimization)
  187. float MatrixDeterminant (Matrix mat);
  188. // Returns the trace of the matrix (sum of the values along the diagonal)
  189. float MatrixTrace (Matrix mat);
  190. // Transposes provided matrix
  191. Matrix MatrixTranspose (Matrix mat);
  192. // Invert provided matrix
  193. // Cache the matrix values (speed optimization)
  194. // Calculate the invert determinant (inlined to avoid double-caching)
  195. Matrix MatrixInvert (Matrix mat);
  196. // Normalize provided matrix
  197. Matrix MatrixNormalize (Matrix mat);
  198. // Returns identity matrix
  199. Matrix MatrixIdentity ();
  200. // Add two matrices
  201. Matrix MatrixAdd (Matrix left, Matrix right);
  202. // Subtract two matrices (left - right)
  203. Matrix MatrixSubtract (Matrix left, Matrix right);
  204. // Returns translation matrix
  205. Matrix MatrixTranslate (float x, float y, float z);
  206. // Create rotation matrix from axis and angle
  207. // NOTE: Angle should be provided in radians
  208. Matrix MatrixRotate (Vector3 axis, float angle);
  209. // Returns xyz-rotation matrix (angles in radians)
  210. Matrix MatrixRotateXYZ (Vector3 ang);
  211. // Returns x-rotation matrix (angle in radians)
  212. Matrix MatrixRotateX (float angle);
  213. // Returns y-rotation matrix (angle in radians)
  214. Matrix MatrixRotateY (float angle);
  215. // Returns z-rotation matrix (angle in radians)
  216. Matrix MatrixRotateZ (float angle);
  217. // Returns scaling matrix
  218. Matrix MatrixScale (float x, float y, float z);
  219. // Returns two matrix multiplication
  220. // NOTE: When multiplying matrices... the order matters!
  221. Matrix MatrixMultiply (Matrix left, Matrix right);
  222. // Returns perspective projection matrix
  223. Matrix MatrixFrustum (
  224. double left,
  225. double right,
  226. double bottom,
  227. double top,
  228. double near,
  229. double far);
  230. // Returns perspective projection matrix
  231. // NOTE: Angle should be provided in radians
  232. Matrix MatrixPerspective (double fovy, double aspect, double near, double far);
  233. // Returns orthographic projection matrix
  234. Matrix MatrixOrtho (
  235. double left,
  236. double right,
  237. double bottom,
  238. double top,
  239. double near,
  240. double far);
  241. // Returns camera look-at matrix (view matrix)
  242. Matrix MatrixLookAt (Vector3 eye, Vector3 target, Vector3 up);
  243. // Returns float array of matrix data
  244. float16 MatrixToFloatV (Matrix mat);
  245. //----------------------------------------------------------------------------------
  246. // Module Functions Definition - Quaternion math
  247. //----------------------------------------------------------------------------------
  248. // Returns identity quaternion
  249. Quaternion QuaternionIdentity ();
  250. // Computes the length of a quaternion
  251. float QuaternionLength (Quaternion q);
  252. // Normalize provided quaternion
  253. Quaternion QuaternionNormalize (Quaternion q);
  254. // Invert provided quaternion
  255. Quaternion QuaternionInvert (Quaternion q);
  256. // Calculate two quaternion multiplication
  257. Quaternion QuaternionMultiply (Quaternion q1, Quaternion q2);
  258. // Calculate linear interpolation between two quaternions
  259. Quaternion QuaternionLerp (Quaternion q1, Quaternion q2, float amount);
  260. // Calculate slerp-optimized interpolation between two quaternions
  261. Quaternion QuaternionNlerp (Quaternion q1, Quaternion q2, float amount);
  262. // Calculates spherical linear interpolation between two quaternions
  263. Quaternion QuaternionSlerp (Quaternion q1, Quaternion q2, float amount);
  264. // Calculate quaternion based on the rotation from one vector to another
  265. // NOTE: Added QuaternioIdentity()
  266. // Normalize to essentially nlerp the original and identity to 0.5
  267. // Above lines are equivalent to:
  268. //Quaternion result = QuaternionNlerp(q, QuaternionIdentity(), 0.5f);
  269. Quaternion QuaternionFromVector3ToVector3 (Vector3 from, Vector3 to);
  270. // Returns a quaternion for a given rotation matrix
  271. Quaternion QuaternionFromMatrix (Matrix mat);
  272. // Returns a matrix for a given quaternion
  273. Matrix QuaternionToMatrix (Quaternion q);
  274. // Returns rotation quaternion for an angle and axis
  275. // NOTE: angle must be provided in radians
  276. Quaternion QuaternionFromAxisAngle (Vector3 axis, float angle);
  277. // Returns the rotation angle and axis for a given quaternion
  278. // This occurs when the angle is zero.
  279. // Not a problem: just set an arbitrary normalized axis.
  280. void QuaternionToAxisAngle (Quaternion q, Vector3* outAxis, float* outAngle);
  281. // Returns he quaternion equivalent to Euler angles
  282. Quaternion QuaternionFromEuler (float roll, float pitch, float yaw);
  283. // Return the Euler angles equivalent to quaternion (roll, pitch, yaw)
  284. // NOTE: Angles are returned in a Vector3 struct in degrees
  285. // roll (x-axis rotation)
  286. // pitch (y-axis rotation)
  287. // yaw (z-axis rotation)
  288. Vector3 QuaternionToEuler (Quaternion q);
  289. // Transform a quaternion given a transformation matrix
  290. Quaternion QuaternionTransform (Quaternion q, Matrix mat);
  291. // RAYMATH_H