Cesium for Unity 1.23.0
Loading...
Searching...
No Matches
Helpers.cs
Go to the documentation of this file.
1using System;
2using Unity.Mathematics;
3using UnityEditor;
4using UnityEngine;
5
6namespace CesiumForUnity
7{
8 internal class Helpers
9 {
10 public static string ToString<T>(T value)
11 {
12 return value.ToString();
13 }
14
15 public static Vector3 FromMathematics(double3 vector)
16 {
17 return new Vector3((float)vector.x, (float)vector.y, (float)vector.z);
18 }
19
20 public static Vector4 FromMathematics(double4 vector)
21 {
22 return new Vector4((float)vector.x, (float)vector.y, (float)vector.z, (float)vector.w);
23 }
24
25 public static double4x4 ToMathematics(Matrix4x4 matrix)
26 {
27 return new double4x4(
28 matrix.m00, matrix.m01, matrix.m02, matrix.m03,
29 matrix.m10, matrix.m11, matrix.m12, matrix.m13,
30 matrix.m20, matrix.m21, matrix.m22, matrix.m23,
31 matrix.m30, matrix.m31, matrix.m32, matrix.m33);
32 }
33
34 public static Matrix4x4 FromMathematics(double4x4 matrix)
35 {
36 return new Matrix4x4(FromMathematics(matrix.c0), FromMathematics(matrix.c1), FromMathematics(matrix.c2), FromMathematics(matrix.c3));
37 }
38
39 public static double3x3 ToMathematicsDouble3x3(Matrix4x4 matrix)
40 {
41 return new double3x3(
42 matrix.m00, matrix.m01, matrix.m02,
43 matrix.m10, matrix.m11, matrix.m12,
44 matrix.m20, matrix.m21, matrix.m22);
45 }
46
47 public static float3x3 ToMathematicsFloat3x3(Matrix4x4 matrix)
48 {
49 return new float3x3(
50 matrix.m00, matrix.m01, matrix.m02,
51 matrix.m10, matrix.m11, matrix.m12,
52 matrix.m20, matrix.m21, matrix.m22);
53 }
54
55 internal static void MatrixToRotationAndScale(double3x3 matrix, out quaternion rotation, out double3 scale)
56 {
57 double lengthColumn0 = math.length(matrix.c0);
58 double lengthColumn1 = math.length(matrix.c1);
59 double lengthColumn2 = math.length(matrix.c2);
60
61 float3x3 rotationMatrix = new float3x3(
62 (float3)(matrix.c0 / lengthColumn0),
63 (float3)(matrix.c1 / lengthColumn1),
64 (float3)(matrix.c2 / lengthColumn2));
65
66 scale = new double3(lengthColumn0, lengthColumn1, lengthColumn2);
67
68 double3 cross = math.cross(matrix.c0, matrix.c1);
69 if (math.dot(cross, matrix.c2) < 0.0)
70 {
71 rotationMatrix *= -1.0f;
72 scale *= -1.0f;
73 }
74
75 rotation = math.quaternion(rotationMatrix);
76 }
77
78 internal static void MatrixToTranslationRotationAndScale(double4x4 matrix, out double3 translation, out quaternion rotation, out double3 scale)
79 {
80 translation = matrix.c3.xyz;
81
82 Helpers.MatrixToRotationAndScale(
83 new double3x3(matrix.c0.xyz, matrix.c1.xyz, matrix.c2.xyz),
84 out rotation,
85 out scale);
86 }
87
88 internal static void MatrixToInaccurateRotationAndScale(double3x3 matrix, out Quaternion rotation, out Vector3 scale)
89 {
90 quaternion rotationTemp;
91 double3 scaleTemp;
92 MatrixToRotationAndScale(matrix, out rotationTemp, out scaleTemp);
93
94 rotation = rotationTemp;
95 scale = (float3)scaleTemp;
96 }
97
98 internal static void MatrixToInaccurateTranslationRotationAndScale(double4x4 matrix, out Vector3 translation, out Quaternion rotation, out Vector3 scale)
99 {
100 translation = Helpers.FromMathematics(matrix.c3.xyz);
101
102 Helpers.MatrixToInaccurateRotationAndScale(
103 new double3x3(matrix.c0.xyz, matrix.c1.xyz, matrix.c2.xyz),
104 out rotation,
105 out scale);
106 }
107
108 internal static double4x4 TranslationRotationAndScaleToMatrix(double3 translation, quaternion rotation, double3 scale)
109 {
110 double3x3 scaleMatrix = new double3x3(
111 new double3(scale.x, 0.0, 0.0),
112 new double3(0.0, scale.y, 0.0),
113 new double3(0.0, 0.0, scale.z));
114 double3x3 rotationMatrix = new float3x3(rotation);
115 double3x3 scaleAndRotate = math.mul(rotationMatrix, scaleMatrix);
116 return new double4x4(
117 new double4(scaleAndRotate.c0, 0.0),
118 new double4(scaleAndRotate.c1, 0.0),
119 new double4(scaleAndRotate.c2, 0.0),
120 new double4(translation, 1.0));
121 }
122
128 public static double NegativePiToPi(double angle)
129 {
130 if (angle >= -Math.PI && angle <= Math.PI)
131 {
132 // Early exit if the input is already inside the range. This avoids
133 // unnecessary math which could introduce floating point error.
134 return angle;
135 }
136 return Helpers.ZeroToTwoPi(angle + Math.PI) - Math.PI;
137 }
138
144 public static double Negative180To180(double angle)
145 {
146 if (angle >= -180.0 && angle <= 180.0)
147 {
148 // Early exit if the input is already inside the range. This avoids
149 // unnecessary math which could introduce floating point error.
150 return angle;
151 }
152 return Helpers.ZeroTo360(angle + 180.0) - 180.0;
153 }
154
160 public static double ZeroToTwoPi(double angle)
161 {
162 if (angle >= 0 && angle <= 2.0 * Math.PI)
163 {
164 // Early exit if the input is already inside the range. This avoids
165 // unnecessary math which could introduce floating point error.
166 return angle;
167 }
168 double mod = Helpers.Mod(angle, 2.0 * Math.PI);
169 if (Math.Abs(mod) < 1.0e-14 && Math.Abs(angle) > 1.0e-14)
170 {
171 return 2.0 * Math.PI;
172 }
173 return mod;
174 }
175
181 public static double ZeroTo360(double angle)
182 {
183 if (angle >= 0 && angle <= 360.0)
184 {
185 // Early exit if the input is already inside the range. This avoids
186 // unnecessary math which could introduce floating point error.
187 return angle;
188 }
189 double mod = Helpers.Mod(angle, 360.0);
190 if (Math.Abs(mod) < 1.0e-14 && Math.Abs(angle) > 1.0e-14)
191 {
192 return 360.0;
193 }
194 return mod;
195 }
196
203 public static double Mod(double m, double n)
204 {
205 if (Math.Sign(m) == Math.Sign(n) && Math.Abs(m) < Math.Abs(n)) {
206 // Early exit if the input does not need to be modded. This avoids
207 // unnecessary math which could introduce floating point error.
208 return m;
209 }
210 return ((m % n) + n) % n;
211 }
212 }
213}