raymath.d 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. module raylib.raymath;
  2. import raylib;
  3. /**********************************************************************************************
  4. *
  5. * raymath v1.5 - Math functions to work with Vector2, Vector3, Matrix and Quaternions
  6. *
  7. * CONVENTIONS:
  8. * - Matrix structure is defined as row-major (memory layout) but parameters naming AND all
  9. * math operations performed by the library consider the structure as it was column-major
  10. * It is like transposed versions of the matrices are used for all the maths
  11. * It benefits some functions making them cache-friendly and also avoids matrix
  12. * transpositions sometimes required by OpenGL
  13. * Example: In memory order, row0 is [m0 m4 m8 m12] but in semantic math row0 is [m0 m1 m2 m3]
  14. * - Functions are always self-contained, no function use another raymath function inside,
  15. * required code is directly re-implemented inside
  16. * - Functions input parameters are always received by value (2 unavoidable exceptions)
  17. * - Functions use always a "result" variable for return
  18. * - Functions are always defined inline
  19. * - Angles are always in radians (DEG2RAD/RAD2DEG macros provided for convenience)
  20. * - No compound literals used to make sure libray is compatible with C++
  21. *
  22. * CONFIGURATION:
  23. * #define RAYMATH_IMPLEMENTATION
  24. * Generates the implementation of the library into the included file.
  25. * If not defined, the library is in header only mode and can be included in other headers
  26. * or source files without problems. But only ONE file should hold the implementation.
  27. *
  28. * #define RAYMATH_STATIC_INLINE
  29. * Define static inline functions code, so #include header suffices for use.
  30. * This may use up lots of memory.
  31. *
  32. *
  33. * LICENSE: zlib/libpng
  34. *
  35. * Copyright (c) 2015-2023 Ramon Santamaria (@raysan5)
  36. *
  37. * This software is provided "as-is", without any express or implied warranty. In no event
  38. * will the authors be held liable for any damages arising from the use of this software.
  39. *
  40. * Permission is granted to anyone to use this software for any purpose, including commercial
  41. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  42. *
  43. * 1. The origin of this software must not be misrepresented; you must not claim that you
  44. * wrote the original software. If you use this software in a product, an acknowledgment
  45. * in the product documentation would be appreciated but is not required.
  46. *
  47. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  48. * as being the original software.
  49. *
  50. * 3. This notice may not be removed or altered from any source distribution.
  51. *
  52. **********************************************************************************************/
  53. extern (C) @nogc nothrow:
  54. // Function specifiers definition
  55. // We are building raylib as a Win32 shared library (.dll).
  56. // We are using raylib as a Win32 shared library (.dll)
  57. // Provide external definition
  58. // Functions may be inlined, no external out-of-line definition
  59. // plain inline not supported by tinycc (See issue #435) // Functions may be inlined or external definition used
  60. //----------------------------------------------------------------------------------
  61. // Defines and Macros
  62. //----------------------------------------------------------------------------------
  63. enum PI = 3.14159265358979323846f;
  64. enum EPSILON = 0.000001f;
  65. enum DEG2RAD = PI / 180.0f;
  66. enum RAD2DEG = 180.0f / PI;
  67. // Get float vector for Matrix
  68. extern (D) auto MatrixToFloat(T)(auto ref T mat)
  69. {
  70. return MatrixToFloatV(mat).v;
  71. }
  72. // Get float vector for Vector3
  73. extern (D) auto Vector3ToFloat(T)(auto ref T vec)
  74. {
  75. return Vector3ToFloatV(vec).v;
  76. }
  77. //----------------------------------------------------------------------------------
  78. // Types and Structures Definition
  79. //----------------------------------------------------------------------------------
  80. // Vector2 type
  81. // Vector3 type
  82. // Vector4 type
  83. // Quaternion type
  84. // Matrix type (OpenGL style 4x4 - right handed, column major)
  85. // Matrix first row (4 components)
  86. // Matrix second row (4 components)
  87. // Matrix third row (4 components)
  88. // Matrix fourth row (4 components)
  89. // NOTE: Helper types to be used instead of array return types for *ToFloat functions
  90. struct float3
  91. {
  92. float[3] v;
  93. }
  94. struct float16
  95. {
  96. float[16] v;
  97. }
  98. // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), floor(), fminf(), fmaxf(), fabs()
  99. //----------------------------------------------------------------------------------
  100. // Module Functions Definition - Utils math
  101. //----------------------------------------------------------------------------------
  102. // Clamp float value
  103. float Clamp(float value, float min, float max);
  104. // Calculate linear interpolation between two floats
  105. float Lerp(float start, float end, float amount);
  106. // Normalize input value within input range
  107. float Normalize(float value, float start, float end);
  108. // Remap input value within input range to output range
  109. float Remap(
  110. float value,
  111. float inputStart,
  112. float inputEnd,
  113. float outputStart,
  114. float outputEnd);
  115. // Wrap input value from min to max
  116. float Wrap(float value, float min, float max);
  117. // Check whether two given floats are almost equal
  118. int FloatEquals(float x, float y);
  119. //----------------------------------------------------------------------------------
  120. // Module Functions Definition - Vector2 math
  121. //----------------------------------------------------------------------------------
  122. // Vector with components value 0.0f
  123. Vector2 Vector2Zero();
  124. // Vector with components value 1.0f
  125. Vector2 Vector2One();
  126. // Add two vectors (v1 + v2)
  127. Vector2 Vector2Add(Vector2 v1, Vector2 v2);
  128. // Add vector and float value
  129. Vector2 Vector2AddValue(Vector2 v, float add);
  130. // Subtract two vectors (v1 - v2)
  131. Vector2 Vector2Subtract(Vector2 v1, Vector2 v2);
  132. // Subtract vector by float value
  133. Vector2 Vector2SubtractValue(Vector2 v, float sub);
  134. // Calculate vector length
  135. float Vector2Length(Vector2 v);
  136. // Calculate vector square length
  137. float Vector2LengthSqr(Vector2 v);
  138. // Calculate two vectors dot product
  139. float Vector2DotProduct(Vector2 v1, Vector2 v2);
  140. // Calculate distance between two vectors
  141. float Vector2Distance(Vector2 v1, Vector2 v2);
  142. // Calculate square distance between two vectors
  143. float Vector2DistanceSqr(Vector2 v1, Vector2 v2);
  144. // Calculate angle between two vectors
  145. // NOTE: Angle is calculated from origin point (0, 0)
  146. float Vector2Angle(Vector2 v1, Vector2 v2);
  147. // Calculate angle defined by a two vectors line
  148. // NOTE: Parameters need to be normalized
  149. // Current implementation should be aligned with glm::angle
  150. // TODO(10/9/2023): Currently angles move clockwise, determine if this is wanted behavior
  151. float Vector2LineAngle(Vector2 start, Vector2 end);
  152. // Scale vector (multiply by value)
  153. Vector2 Vector2Scale(Vector2 v, float scale);
  154. // Multiply vector by vector
  155. Vector2 Vector2Multiply(Vector2 v1, Vector2 v2);
  156. // Negate vector
  157. Vector2 Vector2Negate(Vector2 v);
  158. // Divide vector by vector
  159. Vector2 Vector2Divide(Vector2 v1, Vector2 v2);
  160. // Normalize provided vector
  161. Vector2 Vector2Normalize(Vector2 v);
  162. // Transforms a Vector2 by a given Matrix
  163. Vector2 Vector2Transform(Vector2 v, Matrix mat);
  164. // Calculate linear interpolation between two vectors
  165. Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount);
  166. // Calculate reflected vector to normal
  167. // Dot product
  168. Vector2 Vector2Reflect(Vector2 v, Vector2 normal);
  169. // Rotate vector by angle
  170. Vector2 Vector2Rotate(Vector2 v, float angle);
  171. // Move Vector towards target
  172. Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance);
  173. // Invert the given vector
  174. Vector2 Vector2Invert(Vector2 v);
  175. // Clamp the components of the vector between
  176. // min and max values specified by the given vectors
  177. Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max);
  178. // Clamp the magnitude of the vector between two min and max values
  179. Vector2 Vector2ClampValue(Vector2 v, float min, float max);
  180. // Check whether two given vectors are almost equal
  181. int Vector2Equals(Vector2 p, Vector2 q);
  182. //----------------------------------------------------------------------------------
  183. // Module Functions Definition - Vector3 math
  184. //----------------------------------------------------------------------------------
  185. // Vector with components value 0.0f
  186. Vector3 Vector3Zero();
  187. // Vector with components value 1.0f
  188. Vector3 Vector3One();
  189. // Add two vectors
  190. Vector3 Vector3Add(Vector3 v1, Vector3 v2);
  191. // Add vector and float value
  192. Vector3 Vector3AddValue(Vector3 v, float add);
  193. // Subtract two vectors
  194. Vector3 Vector3Subtract(Vector3 v1, Vector3 v2);
  195. // Subtract vector by float value
  196. Vector3 Vector3SubtractValue(Vector3 v, float sub);
  197. // Multiply vector by scalar
  198. Vector3 Vector3Scale(Vector3 v, float scalar);
  199. // Multiply vector by vector
  200. Vector3 Vector3Multiply(Vector3 v1, Vector3 v2);
  201. // Calculate two vectors cross product
  202. Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2);
  203. // Calculate one vector perpendicular vector
  204. // Cross product between vectors
  205. Vector3 Vector3Perpendicular(Vector3 v);
  206. // Calculate vector length
  207. float Vector3Length(const Vector3 v);
  208. // Calculate vector square length
  209. float Vector3LengthSqr(const Vector3 v);
  210. // Calculate two vectors dot product
  211. float Vector3DotProduct(Vector3 v1, Vector3 v2);
  212. // Calculate distance between two vectors
  213. float Vector3Distance(Vector3 v1, Vector3 v2);
  214. // Calculate square distance between two vectors
  215. float Vector3DistanceSqr(Vector3 v1, Vector3 v2);
  216. // Calculate angle between two vectors
  217. float Vector3Angle(Vector3 v1, Vector3 v2);
  218. // Negate provided vector (invert direction)
  219. Vector3 Vector3Negate(Vector3 v);
  220. // Divide vector by vector
  221. Vector3 Vector3Divide(Vector3 v1, Vector3 v2);
  222. // Normalize provided vector
  223. Vector3 Vector3Normalize(Vector3 v);
  224. //Calculate the projection of the vector v1 on to v2
  225. Vector3 Vector3Project(Vector3 v1, Vector3 v2);
  226. //Calculate the rejection of the vector v1 on to v2
  227. Vector3 Vector3Reject(Vector3 v1, Vector3 v2);
  228. // Orthonormalize provided vectors
  229. // Makes vectors normalized and orthogonal to each other
  230. // Gram-Schmidt function implementation
  231. // Vector3Normalize(*v1);
  232. // Vector3CrossProduct(*v1, *v2)
  233. // Vector3Normalize(vn1);
  234. // Vector3CrossProduct(vn1, *v1)
  235. void Vector3OrthoNormalize(Vector3* v1, Vector3* v2);
  236. // Transforms a Vector3 by a given Matrix
  237. Vector3 Vector3Transform(Vector3 v, Matrix mat);
  238. // Transform a vector by quaternion rotation
  239. Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q);
  240. // Rotates a vector around an axis
  241. // Using Euler-Rodrigues Formula
  242. // Ref.: https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula
  243. // Vector3Normalize(axis);
  244. // Vector3CrossProduct(w, v)
  245. // Vector3CrossProduct(w, wv)
  246. // Vector3Scale(wv, 2*a)
  247. // Vector3Scale(wwv, 2)
  248. Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle);
  249. // Calculate linear interpolation between two vectors
  250. Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount);
  251. // Calculate reflected vector to normal
  252. // I is the original vector
  253. // N is the normal of the incident plane
  254. // R = I - (2*N*(DotProduct[I, N]))
  255. Vector3 Vector3Reflect(Vector3 v, Vector3 normal);
  256. // Get min value for each pair of components
  257. Vector3 Vector3Min(Vector3 v1, Vector3 v2);
  258. // Get max value for each pair of components
  259. Vector3 Vector3Max(Vector3 v1, Vector3 v2);
  260. // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
  261. // NOTE: Assumes P is on the plane of the triangle
  262. // Vector3Subtract(b, a)
  263. // Vector3Subtract(c, a)
  264. // Vector3Subtract(p, a)
  265. // Vector3DotProduct(v0, v0)
  266. // Vector3DotProduct(v0, v1)
  267. // Vector3DotProduct(v1, v1)
  268. // Vector3DotProduct(v2, v0)
  269. // Vector3DotProduct(v2, v1)
  270. Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c);
  271. // Projects a Vector3 from screen space into object space
  272. // NOTE: We are avoiding calling other raymath functions despite available
  273. // Calculate unprojected matrix (multiply view matrix by projection matrix) and invert it
  274. // MatrixMultiply(view, projection);
  275. // Calculate inverted matrix -> MatrixInvert(matViewProj);
  276. // Cache the matrix values (speed optimization)
  277. // Calculate the invert determinant (inlined to avoid double-caching)
  278. // Create quaternion from source point
  279. // Multiply quat point by unprojecte matrix
  280. // QuaternionTransform(quat, matViewProjInv)
  281. // Normalized world points in vectors
  282. Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view);
  283. // Get Vector3 as float array
  284. float3 Vector3ToFloatV(Vector3 v);
  285. // Invert the given vector
  286. Vector3 Vector3Invert(Vector3 v);
  287. // Clamp the components of the vector between
  288. // min and max values specified by the given vectors
  289. Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max);
  290. // Clamp the magnitude of the vector between two values
  291. Vector3 Vector3ClampValue(Vector3 v, float min, float max);
  292. // Check whether two given vectors are almost equal
  293. int Vector3Equals(Vector3 p, Vector3 q);
  294. // Compute the direction of a refracted ray
  295. // v: normalized direction of the incoming ray
  296. // n: normalized normal vector of the interface of two optical media
  297. // r: ratio of the refractive index of the medium from where the ray comes
  298. // to the refractive index of the medium on the other side of the surface
  299. Vector3 Vector3Refract(Vector3 v, Vector3 n, float r);
  300. //----------------------------------------------------------------------------------
  301. // Module Functions Definition - Matrix math
  302. //----------------------------------------------------------------------------------
  303. // Compute matrix determinant
  304. // Cache the matrix values (speed optimization)
  305. float MatrixDeterminant(Matrix mat);
  306. // Get the trace of the matrix (sum of the values along the diagonal)
  307. float MatrixTrace(Matrix mat);
  308. // Transposes provided matrix
  309. Matrix MatrixTranspose(Matrix mat);
  310. // Invert provided matrix
  311. // Cache the matrix values (speed optimization)
  312. // Calculate the invert determinant (inlined to avoid double-caching)
  313. Matrix MatrixInvert(Matrix mat);
  314. // Get identity matrix
  315. Matrix MatrixIdentity();
  316. // Add two matrices
  317. Matrix MatrixAdd(Matrix left, Matrix right);
  318. // Subtract two matrices (left - right)
  319. Matrix MatrixSubtract(Matrix left, Matrix right);
  320. // Get two matrix multiplication
  321. // NOTE: When multiplying matrices... the order matters!
  322. Matrix MatrixMultiply(Matrix left, Matrix right);
  323. // Get translation matrix
  324. Matrix MatrixTranslate(float x, float y, float z);
  325. // Create rotation matrix from axis and angle
  326. // NOTE: Angle should be provided in radians
  327. Matrix MatrixRotate(Vector3 axis, float angle);
  328. // Get x-rotation matrix
  329. // NOTE: Angle must be provided in radians
  330. // MatrixIdentity()
  331. Matrix MatrixRotateX(float angle);
  332. // Get y-rotation matrix
  333. // NOTE: Angle must be provided in radians
  334. // MatrixIdentity()
  335. Matrix MatrixRotateY(float angle);
  336. // Get z-rotation matrix
  337. // NOTE: Angle must be provided in radians
  338. // MatrixIdentity()
  339. Matrix MatrixRotateZ(float angle);
  340. // Get xyz-rotation matrix
  341. // NOTE: Angle must be provided in radians
  342. // MatrixIdentity()
  343. Matrix MatrixRotateXYZ(Vector3 angle);
  344. // Get zyx-rotation matrix
  345. // NOTE: Angle must be provided in radians
  346. Matrix MatrixRotateZYX(Vector3 angle);
  347. // Get scaling matrix
  348. Matrix MatrixScale(float x, float y, float z);
  349. // Get perspective projection matrix
  350. Matrix MatrixFrustum(
  351. double left,
  352. double right,
  353. double bottom,
  354. double top,
  355. double near,
  356. double far);
  357. // Get perspective projection matrix
  358. // NOTE: Fovy angle must be provided in radians
  359. // MatrixFrustum(-right, right, -top, top, near, far);
  360. Matrix MatrixPerspective(
  361. double fovY,
  362. double aspect,
  363. double nearPlane,
  364. double farPlane);
  365. // Get orthographic projection matrix
  366. Matrix MatrixOrtho(
  367. double left,
  368. double right,
  369. double bottom,
  370. double top,
  371. double nearPlane,
  372. double farPlane);
  373. // Get camera look-at matrix (view matrix)
  374. // Vector3Subtract(eye, target)
  375. // Vector3Normalize(vz)
  376. // Vector3CrossProduct(up, vz)
  377. // Vector3Normalize(x)
  378. // Vector3CrossProduct(vz, vx)
  379. // Vector3DotProduct(vx, eye)
  380. // Vector3DotProduct(vy, eye)
  381. // Vector3DotProduct(vz, eye)
  382. Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up);
  383. // Get float array of matrix data
  384. float16 MatrixToFloatV(Matrix mat);
  385. //----------------------------------------------------------------------------------
  386. // Module Functions Definition - Quaternion math
  387. //----------------------------------------------------------------------------------
  388. // Add two quaternions
  389. Quaternion QuaternionAdd(Quaternion q1, Quaternion q2);
  390. // Add quaternion and float value
  391. Quaternion QuaternionAddValue(Quaternion q, float add);
  392. // Subtract two quaternions
  393. Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2);
  394. // Subtract quaternion and float value
  395. Quaternion QuaternionSubtractValue(Quaternion q, float sub);
  396. // Get identity quaternion
  397. Quaternion QuaternionIdentity();
  398. // Computes the length of a quaternion
  399. float QuaternionLength(Quaternion q);
  400. // Normalize provided quaternion
  401. Quaternion QuaternionNormalize(Quaternion q);
  402. // Invert provided quaternion
  403. Quaternion QuaternionInvert(Quaternion q);
  404. // Calculate two quaternion multiplication
  405. Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2);
  406. // Scale quaternion by float value
  407. Quaternion QuaternionScale(Quaternion q, float mul);
  408. // Divide two quaternions
  409. Quaternion QuaternionDivide(Quaternion q1, Quaternion q2);
  410. // Calculate linear interpolation between two quaternions
  411. Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount);
  412. // Calculate slerp-optimized interpolation between two quaternions
  413. // QuaternionLerp(q1, q2, amount)
  414. // QuaternionNormalize(q);
  415. Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount);
  416. // Calculates spherical linear interpolation between two quaternions
  417. Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount);
  418. // Calculate quaternion based on the rotation from one vector to another
  419. // Vector3DotProduct(from, to)
  420. // Vector3CrossProduct(from, to)
  421. // QuaternionNormalize(q);
  422. // NOTE: Normalize to essentially nlerp the original and identity to 0.5
  423. Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to);
  424. // Get a quaternion for a given rotation matrix
  425. Quaternion QuaternionFromMatrix(Matrix mat);
  426. // Get a matrix for a given quaternion
  427. // MatrixIdentity()
  428. Matrix QuaternionToMatrix(Quaternion q);
  429. // Get rotation quaternion for an angle and axis
  430. // NOTE: Angle must be provided in radians
  431. // Vector3Normalize(axis)
  432. // QuaternionNormalize(q);
  433. Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle);
  434. // Get the rotation angle and axis for a given quaternion
  435. // QuaternionNormalize(q);
  436. // This occurs when the angle is zero.
  437. // Not a problem: just set an arbitrary normalized axis.
  438. void QuaternionToAxisAngle(Quaternion q, Vector3* outAxis, float* outAngle);
  439. // Get the quaternion equivalent to Euler angles
  440. // NOTE: Rotation order is ZYX
  441. Quaternion QuaternionFromEuler(float pitch, float yaw, float roll);
  442. // Get the Euler angles equivalent to quaternion (roll, pitch, yaw)
  443. // NOTE: Angles are returned in a Vector3 struct in radians
  444. // Roll (x-axis rotation)
  445. // Pitch (y-axis rotation)
  446. // Yaw (z-axis rotation)
  447. Vector3 QuaternionToEuler(Quaternion q);
  448. // Transform a quaternion given a transformation matrix
  449. Quaternion QuaternionTransform(Quaternion q, Matrix mat);
  450. // Check whether two given quaternions are almost equal
  451. int QuaternionEquals(Quaternion p, Quaternion q);
  452. // RAYMATH_H