Pose3D.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /// @cond
  15. /// Encapsulates a rotation and a translation. This is a convenience class that allows
  16. /// construction and value access either by Matrix4x4 or Quaternion + Vector3 types.
  17. using UnityEngine;
  18. namespace Nxr.Internal
  19. {
  20. public class Pose3D
  21. {
  22. /// Right-handed to left-handed matrix converter (and vice versa).
  23. protected static readonly Matrix4x4 flipZ = Matrix4x4.Scale(new Vector3(1, 1, -1));
  24. /// The translation component of the pose.
  25. public Vector3 Position { get; protected set; }
  26. /// The rotation component of the pose.
  27. public Quaternion Orientation { get; protected set; }
  28. /// The pose as a matrix in Unity gameobject convention (left-handed).
  29. public Matrix4x4 Matrix { get; protected set; }
  30. /// The pose as a matrix in right-handed coordinates.
  31. public Matrix4x4 RightHandedMatrix
  32. {
  33. get
  34. {
  35. return flipZ * Matrix * flipZ;
  36. }
  37. }
  38. /// Default constructor.
  39. /// Initializes position to the origin and orientation to the identity rotation.
  40. public Pose3D()
  41. {
  42. Position = Vector3.zero;
  43. Orientation = Quaternion.identity;
  44. Matrix = Matrix4x4.identity;
  45. }
  46. /// Constructor that takes a Vector3 and a Quaternion.
  47. public Pose3D(Vector3 position, Quaternion orientation)
  48. {
  49. Set(position, orientation, false);
  50. }
  51. public Pose3D(Vector3 position, Quaternion orientation, bool updateMatrix)
  52. {
  53. Set(position, orientation, updateMatrix);
  54. }
  55. /// Constructor that takes a Matrix4x4.
  56. public Pose3D(Matrix4x4 matrix)
  57. {
  58. Set(matrix);
  59. }
  60. protected void Set(Vector3 position, Quaternion orientation, bool updateMatrix)
  61. {
  62. Position = position;
  63. Orientation = orientation;
  64. if (updateMatrix)
  65. {
  66. Matrix = Matrix4x4.TRS(position, orientation, Vector3.one);
  67. }
  68. }
  69. protected void Set(Matrix4x4 matrix)
  70. {
  71. Matrix = matrix;
  72. Position = matrix.GetColumn(3);
  73. Orientation = Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1));
  74. }
  75. public void SetPosition(Vector3 position)
  76. {
  77. Position = position*NxrViewer.Instance.DisplacementCoefficient;
  78. }
  79. }
  80. /// @endcond
  81. /// @cond
  82. /// Mutable version of Pose3D.
  83. public class MutablePose3D : Pose3D
  84. {
  85. /// Sets the position and orientation from a Vector3 + Quaternion.
  86. public new void Set(Vector3 position, Quaternion orientation)
  87. {
  88. base.Set(position, orientation, false);
  89. }
  90. public new void Set(Vector3 position, Quaternion orientation, bool updateMatrix)
  91. {
  92. base.Set(position, orientation, updateMatrix);
  93. }
  94. /// Sets the position and orientation from a Matrix4x4.
  95. public new void Set(Matrix4x4 matrix)
  96. {
  97. base.Set(matrix);
  98. }
  99. /// Sets the position and orientation from a right-handed Matrix4x4.
  100. public void SetRightHanded(Matrix4x4 matrix)
  101. {
  102. Set(flipZ * matrix * flipZ);
  103. }
  104. public new void SetPosition(Vector3 position)
  105. {
  106. base.SetPosition(position);
  107. }
  108. }
  109. }
  110. /// @endcond