raymath.d 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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-2021 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) @nogc nothrow:
  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. // Vector4 type
  71. // Quaternion type
  72. // Matrix type (OpenGL style 4x4 - right handed, column major)
  73. // NOTE: Helper types to be used instead of array return types for *ToFloat functions
  74. struct float3
  75. {
  76. float[3] v;
  77. }
  78. struct float16
  79. {
  80. float[16] v;
  81. }
  82. // Required for: sinf(), cosf(), sqrtf(), tan(), fabs()
  83. //----------------------------------------------------------------------------------
  84. // Module Functions Definition - Utils math
  85. //----------------------------------------------------------------------------------
  86. // Clamp float value
  87. float Clamp(float value, float min, float max);
  88. // Calculate linear interpolation between two floats
  89. float Lerp(float start, float end, float amount);
  90. // Normalize input value within input range
  91. float Normalize(float value, float start, float end);
  92. // Remap input value within input range to output range
  93. float Remap(
  94. float value,
  95. float inputStart,
  96. float inputEnd,
  97. float outputStart,
  98. float outputEnd);
  99. //----------------------------------------------------------------------------------
  100. // Module Functions Definition - Vector2 math
  101. //----------------------------------------------------------------------------------
  102. // Vector with components value 0.0f
  103. Vector2 Vector2Zero();
  104. // Vector with components value 1.0f
  105. Vector2 Vector2One();
  106. // Add two vectors (v1 + v2)
  107. Vector2 Vector2Add(Vector2 v1, Vector2 v2);
  108. // Add vector and float value
  109. Vector2 Vector2AddValue(Vector2 v, float add);
  110. // Subtract two vectors (v1 - v2)
  111. Vector2 Vector2Subtract(Vector2 v1, Vector2 v2);
  112. // Subtract vector by float value
  113. Vector2 Vector2SubtractValue(Vector2 v, float sub);
  114. // Calculate vector length
  115. float Vector2Length(Vector2 v);
  116. // Calculate vector square length
  117. float Vector2LengthSqr(Vector2 v);
  118. // Calculate two vectors dot product
  119. float Vector2DotProduct(Vector2 v1, Vector2 v2);
  120. // Calculate distance between two vectors
  121. float Vector2Distance(Vector2 v1, Vector2 v2);
  122. // Calculate angle from two vectors in X-axis
  123. float Vector2Angle(Vector2 v1, Vector2 v2);
  124. // Scale vector (multiply by value)
  125. Vector2 Vector2Scale(Vector2 v, float scale);
  126. // Multiply vector by vector
  127. Vector2 Vector2Multiply(Vector2 v1, Vector2 v2);
  128. // Negate vector
  129. Vector2 Vector2Negate(Vector2 v);
  130. // Divide vector by vector
  131. Vector2 Vector2Divide(Vector2 v1, Vector2 v2);
  132. // Normalize provided vector
  133. Vector2 Vector2Normalize(Vector2 v);
  134. // Calculate linear interpolation between two vectors
  135. Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount);
  136. // Calculate reflected vector to normal
  137. Vector2 Vector2Reflect(Vector2 v, Vector2 normal);
  138. // Rotate Vector by float in Degrees.
  139. Vector2 Vector2Rotate(Vector2 v, float degs);
  140. // Move Vector towards target
  141. Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance);
  142. //----------------------------------------------------------------------------------
  143. // Module Functions Definition - Vector3 math
  144. //----------------------------------------------------------------------------------
  145. // Vector with components value 0.0f
  146. Vector3 Vector3Zero();
  147. // Vector with components value 1.0f
  148. Vector3 Vector3One();
  149. // Add two vectors
  150. Vector3 Vector3Add(Vector3 v1, Vector3 v2);
  151. // Add vector and float value
  152. Vector3 Vector3AddValue(Vector3 v, float add);
  153. // Subtract two vectors
  154. Vector3 Vector3Subtract(Vector3 v1, Vector3 v2);
  155. // Subtract vector by float value
  156. Vector3 Vector3SubtractValue(Vector3 v, float sub);
  157. // Multiply vector by scalar
  158. Vector3 Vector3Scale(Vector3 v, float scalar);
  159. // Multiply vector by vector
  160. Vector3 Vector3Multiply(Vector3 v1, Vector3 v2);
  161. // Calculate two vectors cross product
  162. Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2);
  163. // Calculate one vector perpendicular vector
  164. Vector3 Vector3Perpendicular(Vector3 v);
  165. // Calculate vector length
  166. float Vector3Length(const Vector3 v);
  167. // Calculate vector square length
  168. float Vector3LengthSqr(const Vector3 v);
  169. // Calculate two vectors dot product
  170. float Vector3DotProduct(Vector3 v1, Vector3 v2);
  171. // Calculate distance between two vectors
  172. float Vector3Distance(Vector3 v1, Vector3 v2);
  173. // Negate provided vector (invert direction)
  174. Vector3 Vector3Negate(Vector3 v);
  175. // Divide vector by vector
  176. Vector3 Vector3Divide(Vector3 v1, Vector3 v2);
  177. // Normalize provided vector
  178. Vector3 Vector3Normalize(Vector3 v);
  179. // Orthonormalize provided vectors
  180. // Makes vectors normalized and orthogonal to each other
  181. // Gram-Schmidt function implementation
  182. void Vector3OrthoNormalize(Vector3* v1, Vector3* v2);
  183. // Transforms a Vector3 by a given Matrix
  184. Vector3 Vector3Transform(Vector3 v, Matrix mat);
  185. // Transform a vector by quaternion rotation
  186. Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q);
  187. // Calculate linear interpolation between two vectors
  188. Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount);
  189. // Calculate reflected vector to normal
  190. // I is the original vector
  191. // N is the normal of the incident plane
  192. // R = I - (2*N*( DotProduct[ I,N] ))
  193. Vector3 Vector3Reflect(Vector3 v, Vector3 normal);
  194. // Return min value for each pair of components
  195. Vector3 Vector3Min(Vector3 v1, Vector3 v2);
  196. // Return max value for each pair of components
  197. Vector3 Vector3Max(Vector3 v1, Vector3 v2);
  198. // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
  199. // NOTE: Assumes P is on the plane of the triangle
  200. //Vector v0 = b - a, v1 = c - a, v2 = p - a;
  201. Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c);
  202. // Returns Vector3 as float array
  203. float3 Vector3ToFloatV(Vector3 v);
  204. //----------------------------------------------------------------------------------
  205. // Module Functions Definition - Matrix math
  206. //----------------------------------------------------------------------------------
  207. // Compute matrix determinant
  208. // Cache the matrix values (speed optimization)
  209. float MatrixDeterminant(Matrix mat);
  210. // Returns the trace of the matrix (sum of the values along the diagonal)
  211. float MatrixTrace(Matrix mat);
  212. // Transposes provided matrix
  213. Matrix MatrixTranspose(Matrix mat);
  214. // Invert provided matrix
  215. // Cache the matrix values (speed optimization)
  216. // Calculate the invert determinant (inlined to avoid double-caching)
  217. Matrix MatrixInvert(Matrix mat);
  218. // Normalize provided matrix
  219. Matrix MatrixNormalize(Matrix mat);
  220. // Returns identity matrix
  221. Matrix MatrixIdentity();
  222. // Add two matrices
  223. Matrix MatrixAdd(Matrix left, Matrix right);
  224. // Subtract two matrices (left - right)
  225. Matrix MatrixSubtract(Matrix left, Matrix right);
  226. // Returns two matrix multiplication
  227. // NOTE: When multiplying matrices... the order matters!
  228. Matrix MatrixMultiply(Matrix left, Matrix right);
  229. // Returns translation matrix
  230. Matrix MatrixTranslate(float x, float y, float z);
  231. // Create rotation matrix from axis and angle
  232. // NOTE: Angle should be provided in radians
  233. Matrix MatrixRotate(Vector3 axis, float angle);
  234. // Returns x-rotation matrix (angle in radians)
  235. Matrix MatrixRotateX(float angle);
  236. // Returns y-rotation matrix (angle in radians)
  237. Matrix MatrixRotateY(float angle);
  238. // Returns z-rotation matrix (angle in radians)
  239. Matrix MatrixRotateZ(float angle);
  240. // Returns xyz-rotation matrix (angles in radians)
  241. Matrix MatrixRotateXYZ(Vector3 ang);
  242. // Returns zyx-rotation matrix (angles in radians)
  243. Matrix MatrixRotateZYX(Vector3 ang);
  244. // Returns scaling matrix
  245. Matrix MatrixScale(float x, float y, float z);
  246. // Returns perspective projection matrix
  247. Matrix MatrixFrustum(
  248. double left,
  249. double right,
  250. double bottom,
  251. double top,
  252. double near,
  253. double far);
  254. // Returns perspective projection matrix
  255. // NOTE: Angle should be provided in radians
  256. Matrix MatrixPerspective(double fovy, double aspect, double near, double far);
  257. // Returns orthographic projection matrix
  258. Matrix MatrixOrtho(
  259. double left,
  260. double right,
  261. double bottom,
  262. double top,
  263. double near,
  264. double far);
  265. // Returns camera look-at matrix (view matrix)
  266. Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up);
  267. // Returns float array of matrix data
  268. float16 MatrixToFloatV(Matrix mat);
  269. //----------------------------------------------------------------------------------
  270. // Module Functions Definition - Quaternion math
  271. //----------------------------------------------------------------------------------
  272. // Add two quaternions
  273. Quaternion QuaternionAdd(Quaternion q1, Quaternion q2);
  274. // Add quaternion and float value
  275. Quaternion QuaternionAddValue(Quaternion q, float add);
  276. // Subtract two quaternions
  277. Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2);
  278. // Subtract quaternion and float value
  279. Quaternion QuaternionSubtractValue(Quaternion q, float sub);
  280. // Returns identity quaternion
  281. Quaternion QuaternionIdentity();
  282. // Computes the length of a quaternion
  283. float QuaternionLength(Quaternion q);
  284. // Normalize provided quaternion
  285. Quaternion QuaternionNormalize(Quaternion q);
  286. // Invert provided quaternion
  287. Quaternion QuaternionInvert(Quaternion q);
  288. // Calculate two quaternion multiplication
  289. Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2);
  290. // Scale quaternion by float value
  291. Quaternion QuaternionScale(Quaternion q, float mul);
  292. // Divide two quaternions
  293. Quaternion QuaternionDivide(Quaternion q1, Quaternion q2);
  294. // Calculate linear interpolation between two quaternions
  295. Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount);
  296. // Calculate slerp-optimized interpolation between two quaternions
  297. Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount);
  298. // Calculates spherical linear interpolation between two quaternions
  299. Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount);
  300. // Calculate quaternion based on the rotation from one vector to another
  301. // NOTE: Added QuaternioIdentity()
  302. // Normalize to essentially nlerp the original and identity to 0.5
  303. // Above lines are equivalent to:
  304. //Quaternion result = QuaternionNlerp(q, QuaternionIdentity(), 0.5f);
  305. Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to);
  306. // Returns a quaternion for a given rotation matrix
  307. Quaternion QuaternionFromMatrix(Matrix mat);
  308. // Returns a matrix for a given quaternion
  309. //, d2=2*(q.w*q.w);
  310. Matrix QuaternionToMatrix(Quaternion q);
  311. // Returns rotation quaternion for an angle and axis
  312. // NOTE: angle must be provided in radians
  313. Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle);
  314. // Returns the rotation angle and axis for a given quaternion
  315. // This occurs when the angle is zero.
  316. // Not a problem: just set an arbitrary normalized axis.
  317. void QuaternionToAxisAngle(Quaternion q, Vector3* outAxis, float* outAngle);
  318. // Returns the quaternion equivalent to Euler angles
  319. // NOTE: Rotation order is ZYX
  320. Quaternion QuaternionFromEuler(float pitch, float yaw, float roll);
  321. // Return the Euler angles equivalent to quaternion (roll, pitch, yaw)
  322. // NOTE: Angles are returned in a Vector3 struct in degrees
  323. // roll (x-axis rotation)
  324. // pitch (y-axis rotation)
  325. // yaw (z-axis rotation)
  326. Vector3 QuaternionToEuler(Quaternion q);
  327. // Transform a quaternion given a transformation matrix
  328. Quaternion QuaternionTransform(Quaternion q, Matrix mat);
  329. // Projects a Vector3 from screen space into object space
  330. // Calculate unproject matrix (multiply view patrix by projection matrix) and invert it
  331. // Create quaternion from source point
  332. // Multiply quat point by unproject matrix
  333. // Normalized world points in vectors
  334. Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view);
  335. // RAYMATH_H