raymath.d 18 KB

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