raymath.d 16 KB

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