rigid_transform.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using static Unity.Mathematics.math;
  4. namespace Unity.Mathematics
  5. {
  6. public struct RigidTransform
  7. {
  8. public quaternion rot;
  9. public float3 pos;
  10. /// <summary>A RigidTransform representing the identity transform.</summary>
  11. public static readonly RigidTransform identity = new RigidTransform(new quaternion(0.0f, 0.0f, 0.0f, 1.0f), new float3(0.0f, 0.0f, 0.0f));
  12. /// <summary>Constructs a RigidTransform from a rotation represented by a unit quaternion and a translation represented by a float3 vector.</summary>
  13. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  14. public RigidTransform(quaternion rotation, float3 translation)
  15. {
  16. this.rot = rotation;
  17. this.pos = translation;
  18. }
  19. /// <summary>Constructs a RigidTransform from a rotation represented by a float3x3 matrix and a translation represented by a float3 vector.</summary>
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. public RigidTransform(float3x3 rotation, float3 translation)
  22. {
  23. this.rot = new quaternion(rotation);
  24. this.pos = translation;
  25. }
  26. /// <summary>Constructs a RigidTransform from a float4x4. Assumes the matrix is orthonormal.</summary>
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public RigidTransform(float4x4 transform)
  29. {
  30. this.rot = new quaternion(transform);
  31. this.pos = transform.c3.xyz;
  32. }
  33. /// <summary>
  34. /// Returns a RigidTransform representing a rotation around a unit axis by an angle in radians.
  35. /// The rotation direction is clockwise when looking along the rotation axis towards the origin.
  36. /// </summary>
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. public static RigidTransform AxisAngle(float3 axis, float angle) { return new RigidTransform(quaternion.AxisAngle(axis, angle), float3.zero); }
  39. /// <summary>
  40. /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
  41. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  42. /// </summary>
  43. /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public static RigidTransform EulerXYZ(float3 xyz) { return new RigidTransform(quaternion.EulerXYZ(xyz), float3.zero); }
  46. /// <summary>
  47. /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
  48. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  49. /// </summary>
  50. /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. public static RigidTransform EulerXZY(float3 xyz) { return new RigidTransform(quaternion.EulerXZY(xyz), float3.zero); }
  53. /// <summary>
  54. /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
  55. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  56. /// </summary>
  57. /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
  58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. public static RigidTransform EulerYXZ(float3 xyz) { return new RigidTransform(quaternion.EulerYXZ(xyz), float3.zero); }
  60. /// <summary>
  61. /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
  62. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  63. /// </summary>
  64. /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. public static RigidTransform EulerYZX(float3 xyz) { return new RigidTransform(quaternion.EulerYZX(xyz), float3.zero); }
  67. /// <summary>
  68. /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
  69. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  70. /// This is the default order rotation order in Unity.
  71. /// </summary>
  72. /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
  73. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  74. public static RigidTransform EulerZXY(float3 xyz) { return new RigidTransform(quaternion.EulerZXY(xyz), float3.zero); }
  75. /// <summary>
  76. /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
  77. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  78. /// </summary>
  79. /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public static RigidTransform EulerZYX(float3 xyz) { return new RigidTransform(quaternion.EulerZYX(xyz), float3.zero); }
  82. /// <summary>
  83. /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the y-axis and finally the z-axis.
  84. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  85. /// </summary>
  86. /// <param name="x">The rotation angle around the x-axis in radians.</param>
  87. /// <param name="y">The rotation angle around the y-axis in radians.</param>
  88. /// <param name="z">The rotation angle around the z-axis in radians.</param>
  89. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  90. public static RigidTransform EulerXYZ(float x, float y, float z) { return EulerXYZ(float3(x, y, z)); }
  91. /// <summary>
  92. /// Returns a RigidTransform constructed by first performing a rotation around the x-axis, then the z-axis and finally the y-axis.
  93. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  94. /// </summary>
  95. /// <param name="x">The rotation angle around the x-axis in radians.</param>
  96. /// <param name="y">The rotation angle around the y-axis in radians.</param>
  97. /// <param name="z">The rotation angle around the z-axis in radians.</param>
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. public static RigidTransform EulerXZY(float x, float y, float z) { return EulerXZY(float3(x, y, z)); }
  100. /// <summary>
  101. /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the x-axis and finally the z-axis.
  102. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  103. /// </summary>
  104. /// <param name="x">The rotation angle around the x-axis in radians.</param>
  105. /// <param name="y">The rotation angle around the y-axis in radians.</param>
  106. /// <param name="z">The rotation angle around the z-axis in radians.</param>
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. public static RigidTransform EulerYXZ(float x, float y, float z) { return EulerYXZ(float3(x, y, z)); }
  109. /// <summary>
  110. /// Returns a RigidTransform constructed by first performing a rotation around the y-axis, then the z-axis and finally the x-axis.
  111. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  112. /// </summary>
  113. /// <param name="x">The rotation angle around the x-axis in radians.</param>
  114. /// <param name="y">The rotation angle around the y-axis in radians.</param>
  115. /// <param name="z">The rotation angle around the z-axis in radians.</param>
  116. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  117. public static RigidTransform EulerYZX(float x, float y, float z) { return EulerYZX(float3(x, y, z)); }
  118. /// <summary>
  119. /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the x-axis and finally the y-axis.
  120. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  121. /// This is the default order rotation order in Unity.
  122. /// </summary>
  123. /// <param name="x">The rotation angle around the x-axis in radians.</param>
  124. /// <param name="y">The rotation angle around the y-axis in radians.</param>
  125. /// <param name="z">The rotation angle around the z-axis in radians.</param>
  126. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127. public static RigidTransform EulerZXY(float x, float y, float z) { return EulerZXY(float3(x, y, z)); }
  128. /// <summary>
  129. /// Returns a RigidTransform constructed by first performing a rotation around the z-axis, then the y-axis and finally the x-axis.
  130. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  131. /// </summary>
  132. /// <param name="x">The rotation angle around the x-axis in radians.</param>
  133. /// <param name="y">The rotation angle around the y-axis in radians.</param>
  134. /// <param name="z">The rotation angle around the z-axis in radians.</param>
  135. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  136. public static RigidTransform EulerZYX(float x, float y, float z) { return EulerZYX(float3(x, y, z)); }
  137. /// <summary>
  138. /// Returns a RigidTransform constructed by first performing 3 rotations around the principal axes in a given order.
  139. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  140. /// When the rotation order is known at compile time, it is recommended for performance reasons to use specific
  141. /// Euler rotation constructors such as EulerZXY(...).
  142. /// </summary>
  143. /// <param name="xyz">A float3 vector containing the rotation angles around the x-, y- and z-axis measures in radians.</param>
  144. /// <param name="order">The order in which the rotations are applied.</param>
  145. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  146. public static RigidTransform Euler(float3 xyz, RotationOrder order = RotationOrder.ZXY)
  147. {
  148. switch (order)
  149. {
  150. case RotationOrder.XYZ:
  151. return EulerXYZ(xyz);
  152. case RotationOrder.XZY:
  153. return EulerXZY(xyz);
  154. case RotationOrder.YXZ:
  155. return EulerYXZ(xyz);
  156. case RotationOrder.YZX:
  157. return EulerYZX(xyz);
  158. case RotationOrder.ZXY:
  159. return EulerZXY(xyz);
  160. case RotationOrder.ZYX:
  161. return EulerZYX(xyz);
  162. default:
  163. return RigidTransform.identity;
  164. }
  165. }
  166. /// <summary>
  167. /// Returns a RigidTransform constructed by first performing 3 rotations around the principal axes in a given order.
  168. /// All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
  169. /// When the rotation order is known at compile time, it is recommended for performance reasons to use specific
  170. /// Euler rotation constructors such as EulerZXY(...).
  171. /// </summary>
  172. /// <param name="x">The rotation angle around the x-axis in radians.</param>
  173. /// <param name="y">The rotation angle around the y-axis in radians.</param>
  174. /// <param name="z">The rotation angle around the z-axis in radians.</param>
  175. /// <param name="order">The order in which the rotations are applied.</param>
  176. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  177. public static RigidTransform Euler(float x, float y, float z, RotationOrder order = RotationOrder.Default)
  178. {
  179. return Euler(float3(x, y, z), order);
  180. }
  181. /// <summary>Returns a float4x4 matrix that rotates around the x-axis by a given number of radians.</summary>
  182. /// <param name="angle">The clockwise rotation angle when looking along the x-axis towards the origin in radians.</param>
  183. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  184. public static RigidTransform RotateX(float angle)
  185. {
  186. return new RigidTransform(quaternion.RotateX(angle), float3.zero);
  187. }
  188. /// <summary>Returns a float4x4 matrix that rotates around the y-axis by a given number of radians.</summary>
  189. /// <param name="angle">The clockwise rotation angle when looking along the y-axis towards the origin in radians.</param>
  190. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  191. public static RigidTransform RotateY(float angle)
  192. {
  193. return new RigidTransform(quaternion.RotateY(angle), float3.zero);
  194. }
  195. /// <summary>Returns a float4x4 matrix that rotates around the z-axis by a given number of radians.</summary>
  196. /// <param name="angle">The clockwise rotation angle when looking along the z-axis towards the origin in radians.</param>
  197. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  198. public static RigidTransform RotateZ(float angle)
  199. {
  200. return new RigidTransform(quaternion.RotateZ(angle), float3.zero);
  201. }
  202. /// <summary>Returns a RigidTransform that translates by an amount specified by a float3 vector.</summary>
  203. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  204. public static RigidTransform Translate(float3 vector)
  205. {
  206. return new RigidTransform(quaternion.identity, vector);
  207. }
  208. /// <summary>Returns true if the RigidTransform is equal to a given RigidTransform, false otherwise.</summary>
  209. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  210. public bool Equals(RigidTransform x) { return rot.Equals(x.rot) && pos.Equals(x.pos); }
  211. /// <summary>Returns true if the RigidTransform is equal to a given RigidTransform, false otherwise.</summary>
  212. public override bool Equals(object x) { return Equals((RigidTransform)x); }
  213. /// <summary>Returns a hash code for the RigidTransform.</summary>
  214. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  215. public override int GetHashCode() { return (int)math.hash(this); }
  216. /// <summary>Returns a string representation of the RigidTransform.</summary>
  217. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  218. public override string ToString()
  219. {
  220. return string.Format("RigidTransform(({0}f, {1}f, {2}f, {3}f), ({4}f, {5}f, {6}f))",
  221. rot.value.x, rot.value.y, rot.value.z, rot.value.w, pos.x, pos.y, pos.z);
  222. }
  223. /// <summary>Returns a string representation of the quaternion using a specified format and culture-specific format information.</summary>
  224. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  225. public string ToString(string format, IFormatProvider formatProvider)
  226. {
  227. return string.Format("float4x4(({0}f, {1}f, {2}f, {3}f), ({4}f, {5}f, {6}f))",
  228. rot.value.x.ToString(format, formatProvider),
  229. rot.value.y.ToString(format, formatProvider),
  230. rot.value.z.ToString(format, formatProvider),
  231. rot.value.w.ToString(format, formatProvider),
  232. pos.x.ToString(format, formatProvider),
  233. pos.y.ToString(format, formatProvider),
  234. pos.z.ToString(format, formatProvider));
  235. }
  236. }
  237. public static partial class math
  238. {
  239. /// <summary>Returns a RigidTransform constructed from a rotation represented by a unit quaternion and a translation represented by a float3 vector.</summary>
  240. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  241. public static RigidTransform RigidTransform(quaternion rot, float3 pos) { return new RigidTransform(rot, pos); }
  242. /// <summary>Returns a RigidTransform constructed from a rotation represented by a unit quaternion and a translation represented by a float3 vector.</summary>
  243. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  244. public static RigidTransform RigidTransform(float3x3 rotation, float3 translation) { return new RigidTransform(rotation, translation); }
  245. /// <summary>Returns a RigidTransform constructed from a rotation represented by a float3x3 matrix and a translation represented by a float3 vector.</summary>
  246. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  247. public static RigidTransform RigidTransform(float4x4 transform) { return new RigidTransform(transform); }
  248. /// <summary>Returns the inverse of a RigidTransform.</summary>
  249. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  250. public static RigidTransform inverse(RigidTransform t)
  251. {
  252. quaternion invRotation = inverse(t.rot);
  253. float3 invTranslation = mul(invRotation, -t.pos);
  254. return new RigidTransform(invRotation, invTranslation);
  255. }
  256. /// <summary>Returns the result of transforming the RigidTransform b by the RigidTransform a.</summary>
  257. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  258. public static RigidTransform mul(RigidTransform a, RigidTransform b)
  259. {
  260. return new RigidTransform(mul(a.rot, b.rot), mul(a.rot, b.pos) + a.pos);
  261. }
  262. /// <summary>Returns the result of transforming a float4 homogeneous coordinate by a RigidTransform.</summary>
  263. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  264. public static float4 mul(RigidTransform a, float4 pos)
  265. {
  266. return float4(mul(a.rot, pos.xyz) + a.pos * pos.w, pos.w);
  267. }
  268. /// <summary>Returns the result of rotating a float3 vector by a RigidTransform.</summary>
  269. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  270. public static float3 rotate(RigidTransform a, float3 dir)
  271. {
  272. return mul(a.rot, dir);
  273. }
  274. /// <summary>Returns the result of transforming a float3 point by a RigidTransform.</summary>
  275. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  276. public static float3 transform(RigidTransform a, float3 pos)
  277. {
  278. return mul(a.rot, pos) + a.pos;
  279. }
  280. /// <summary>Returns a uint hash code of a RigidTransform.</summary>
  281. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  282. public static uint hash(RigidTransform t)
  283. {
  284. return hash(t.rot) + 0xC5C5394Bu * hash(t.pos);
  285. }
  286. /// <summary>
  287. /// Returns a uint4 vector hash code of a RigidTransform.
  288. /// When multiple elements are to be hashes together, it can more efficient to calculate and combine wide hash
  289. /// that are only reduced to a narrow uint hash at the very end instead of at every step.
  290. /// </summary>
  291. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  292. public static uint4 hashwide(RigidTransform t)
  293. {
  294. return hashwide(t.rot) + 0xC5C5394Bu * hashwide(t.pos).xyzz;
  295. }
  296. }
  297. }