@mlightcad/geometry-engine 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/dist/geometry-engine.js +10186 -0
  4. package/dist/geometry-engine.umd.cjs +4 -0
  5. package/lib/geometry/AcGeArea2d.d.ts +61 -0
  6. package/lib/geometry/AcGeArea2d.d.ts.map +1 -0
  7. package/lib/geometry/AcGeArea2d.js +174 -0
  8. package/lib/geometry/AcGeArea2d.js.map +1 -0
  9. package/lib/geometry/AcGeCircArc2d.d.ts +108 -0
  10. package/lib/geometry/AcGeCircArc2d.d.ts.map +1 -0
  11. package/lib/geometry/AcGeCircArc2d.js +412 -0
  12. package/lib/geometry/AcGeCircArc2d.js.map +1 -0
  13. package/lib/geometry/AcGeCircArc3d.d.ts +138 -0
  14. package/lib/geometry/AcGeCircArc3d.d.ts.map +1 -0
  15. package/lib/geometry/AcGeCircArc3d.js +447 -0
  16. package/lib/geometry/AcGeCircArc3d.js.map +1 -0
  17. package/lib/geometry/AcGeCurve2d.d.ts +85 -0
  18. package/lib/geometry/AcGeCurve2d.d.ts.map +1 -0
  19. package/lib/geometry/AcGeCurve2d.js +213 -0
  20. package/lib/geometry/AcGeCurve2d.js.map +1 -0
  21. package/lib/geometry/AcGeCurve3d.d.ts +27 -0
  22. package/lib/geometry/AcGeCurve3d.d.ts.map +1 -0
  23. package/lib/geometry/AcGeCurve3d.js +29 -0
  24. package/lib/geometry/AcGeCurve3d.js.map +1 -0
  25. package/lib/geometry/AcGeEllipseArc2d.d.ts +105 -0
  26. package/lib/geometry/AcGeEllipseArc2d.d.ts.map +1 -0
  27. package/lib/geometry/AcGeEllipseArc2d.js +292 -0
  28. package/lib/geometry/AcGeEllipseArc2d.js.map +1 -0
  29. package/lib/geometry/AcGeEllipseArc3d.d.ts +141 -0
  30. package/lib/geometry/AcGeEllipseArc3d.d.ts.map +1 -0
  31. package/lib/geometry/AcGeEllipseArc3d.js +442 -0
  32. package/lib/geometry/AcGeEllipseArc3d.js.map +1 -0
  33. package/lib/geometry/AcGeLine2d.d.ts +56 -0
  34. package/lib/geometry/AcGeLine2d.d.ts.map +1 -0
  35. package/lib/geometry/AcGeLine2d.js +125 -0
  36. package/lib/geometry/AcGeLine2d.js.map +1 -0
  37. package/lib/geometry/AcGeLine3d.d.ts +134 -0
  38. package/lib/geometry/AcGeLine3d.d.ts.map +1 -0
  39. package/lib/geometry/AcGeLine3d.js +291 -0
  40. package/lib/geometry/AcGeLine3d.js.map +1 -0
  41. package/lib/geometry/AcGeLoop2d.d.ts +59 -0
  42. package/lib/geometry/AcGeLoop2d.d.ts.map +1 -0
  43. package/lib/geometry/AcGeLoop2d.js +141 -0
  44. package/lib/geometry/AcGeLoop2d.js.map +1 -0
  45. package/lib/geometry/AcGePolyline2d.d.ts +90 -0
  46. package/lib/geometry/AcGePolyline2d.d.ts.map +1 -0
  47. package/lib/geometry/AcGePolyline2d.js +224 -0
  48. package/lib/geometry/AcGePolyline2d.js.map +1 -0
  49. package/lib/geometry/AcGeShape.d.ts +12 -0
  50. package/lib/geometry/AcGeShape.d.ts.map +1 -0
  51. package/lib/geometry/AcGeShape.js +22 -0
  52. package/lib/geometry/AcGeShape.js.map +1 -0
  53. package/lib/geometry/AcGeShape2d.d.ts +31 -0
  54. package/lib/geometry/AcGeShape2d.d.ts.map +1 -0
  55. package/lib/geometry/AcGeShape2d.js +51 -0
  56. package/lib/geometry/AcGeShape2d.js.map +1 -0
  57. package/lib/geometry/AcGeShape3d.d.ts +33 -0
  58. package/lib/geometry/AcGeShape3d.d.ts.map +1 -0
  59. package/lib/geometry/AcGeShape3d.js +52 -0
  60. package/lib/geometry/AcGeShape3d.js.map +1 -0
  61. package/lib/geometry/AcGeSpline3d.d.ts +77 -0
  62. package/lib/geometry/AcGeSpline3d.d.ts.map +1 -0
  63. package/lib/geometry/AcGeSpline3d.js +221 -0
  64. package/lib/geometry/AcGeSpline3d.js.map +1 -0
  65. package/lib/geometry/index.d.ts +13 -0
  66. package/lib/geometry/index.d.ts.map +1 -0
  67. package/lib/geometry/index.js +13 -0
  68. package/lib/geometry/index.js.map +1 -0
  69. package/lib/index.d.ts +4 -0
  70. package/lib/index.d.ts.map +1 -0
  71. package/lib/index.js +4 -0
  72. package/lib/index.js.map +1 -0
  73. package/lib/math/AcGeBox2d.d.ts +173 -0
  74. package/lib/math/AcGeBox2d.d.ts.map +1 -0
  75. package/lib/math/AcGeBox2d.js +289 -0
  76. package/lib/math/AcGeBox2d.js.map +1 -0
  77. package/lib/math/AcGeBox3d.d.ts +195 -0
  78. package/lib/math/AcGeBox3d.d.ts.map +1 -0
  79. package/lib/math/AcGeBox3d.js +378 -0
  80. package/lib/math/AcGeBox3d.js.map +1 -0
  81. package/lib/math/AcGeEuler.d.ts +133 -0
  82. package/lib/math/AcGeEuler.d.ts.map +1 -0
  83. package/lib/math/AcGeEuler.js +358 -0
  84. package/lib/math/AcGeEuler.js.map +1 -0
  85. package/lib/math/AcGeMatrix2d.d.ts +203 -0
  86. package/lib/math/AcGeMatrix2d.d.ts.map +1 -0
  87. package/lib/math/AcGeMatrix2d.js +393 -0
  88. package/lib/math/AcGeMatrix2d.js.map +1 -0
  89. package/lib/math/AcGeMatrix3d.d.ts +279 -0
  90. package/lib/math/AcGeMatrix3d.d.ts.map +1 -0
  91. package/lib/math/AcGeMatrix3d.js +1037 -0
  92. package/lib/math/AcGeMatrix3d.js.map +1 -0
  93. package/lib/math/AcGePlane.d.ts +131 -0
  94. package/lib/math/AcGePlane.d.ts.map +1 -0
  95. package/lib/math/AcGePlane.js +218 -0
  96. package/lib/math/AcGePlane.js.map +1 -0
  97. package/lib/math/AcGePoint.d.ts +4 -0
  98. package/lib/math/AcGePoint.d.ts.map +1 -0
  99. package/lib/math/AcGePoint.js +2 -0
  100. package/lib/math/AcGePoint.js.map +1 -0
  101. package/lib/math/AcGePoint2d.d.ts +17 -0
  102. package/lib/math/AcGePoint2d.d.ts.map +1 -0
  103. package/lib/math/AcGePoint2d.js +40 -0
  104. package/lib/math/AcGePoint2d.js.map +1 -0
  105. package/lib/math/AcGePoint3d.d.ts +18 -0
  106. package/lib/math/AcGePoint3d.d.ts.map +1 -0
  107. package/lib/math/AcGePoint3d.js +43 -0
  108. package/lib/math/AcGePoint3d.js.map +1 -0
  109. package/lib/math/AcGeQuaternion.d.ts +230 -0
  110. package/lib/math/AcGeQuaternion.d.ts.map +1 -0
  111. package/lib/math/AcGeQuaternion.js +666 -0
  112. package/lib/math/AcGeQuaternion.js.map +1 -0
  113. package/lib/math/AcGeVector.d.ts +9 -0
  114. package/lib/math/AcGeVector.d.ts.map +1 -0
  115. package/lib/math/AcGeVector.js +2 -0
  116. package/lib/math/AcGeVector.js.map +1 -0
  117. package/lib/math/AcGeVector2d.d.ts +361 -0
  118. package/lib/math/AcGeVector2d.d.ts.map +1 -0
  119. package/lib/math/AcGeVector2d.js +622 -0
  120. package/lib/math/AcGeVector2d.js.map +1 -0
  121. package/lib/math/AcGeVector3d.d.ts +493 -0
  122. package/lib/math/AcGeVector3d.d.ts.map +1 -0
  123. package/lib/math/AcGeVector3d.js +868 -0
  124. package/lib/math/AcGeVector3d.js.map +1 -0
  125. package/lib/math/index.d.ts +14 -0
  126. package/lib/math/index.d.ts.map +1 -0
  127. package/lib/math/index.js +14 -0
  128. package/lib/math/index.js.map +1 -0
  129. package/lib/util/AcGeConstants.d.ts +19 -0
  130. package/lib/util/AcGeConstants.d.ts.map +1 -0
  131. package/lib/util/AcGeConstants.js +25 -0
  132. package/lib/util/AcGeConstants.js.map +1 -0
  133. package/lib/util/AcGeGeometryUtil.d.ts +16 -0
  134. package/lib/util/AcGeGeometryUtil.d.ts.map +1 -0
  135. package/lib/util/AcGeGeometryUtil.js +56 -0
  136. package/lib/util/AcGeGeometryUtil.js.map +1 -0
  137. package/lib/util/AcGeMathUtil.d.ts +203 -0
  138. package/lib/util/AcGeMathUtil.d.ts.map +1 -0
  139. package/lib/util/AcGeMathUtil.js +585 -0
  140. package/lib/util/AcGeMathUtil.js.map +1 -0
  141. package/lib/util/AcGeTol.d.ts +91 -0
  142. package/lib/util/AcGeTol.d.ts.map +1 -0
  143. package/lib/util/AcGeTol.js +83 -0
  144. package/lib/util/AcGeTol.js.map +1 -0
  145. package/lib/util/index.d.ts +5 -0
  146. package/lib/util/index.d.ts.map +1 -0
  147. package/lib/util/index.js +5 -0
  148. package/lib/util/index.js.map +1 -0
  149. package/package.json +42 -0
@@ -0,0 +1,666 @@
1
+ var __generator = (this && this.__generator) || function (thisArg, body) {
2
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
3
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
4
+ function verb(n) { return function (v) { return step([n, v]); }; }
5
+ function step(op) {
6
+ if (f) throw new TypeError("Generator is already executing.");
7
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
8
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
9
+ if (y = 0, t) op = [op[0] & 2, t.value];
10
+ switch (op[0]) {
11
+ case 0: case 1: t = op; break;
12
+ case 4: _.label++; return { value: op[1], done: false };
13
+ case 5: _.label++; y = op[1]; op = [0]; continue;
14
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
15
+ default:
16
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
17
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
18
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
19
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
20
+ if (t[2]) _.ops.pop();
21
+ _.trys.pop(); continue;
22
+ }
23
+ op = body.call(thisArg, _);
24
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
25
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
26
+ }
27
+ };
28
+ import * as MathUtils from '../util';
29
+ var AcGeQuaternion = /** @class */ (function () {
30
+ /**
31
+ * Create one instance of this class
32
+ * @param x Input x coordinate
33
+ * @param y Input y coordinate
34
+ * @param z Input z coordinate
35
+ * @param w Input w coordinate
36
+ */
37
+ function AcGeQuaternion(x, y, z, w) {
38
+ if (x === void 0) { x = 0; }
39
+ if (y === void 0) { y = 0; }
40
+ if (z === void 0) { z = 0; }
41
+ if (w === void 0) { w = 1; }
42
+ this._x = x;
43
+ this._y = y;
44
+ this._z = z;
45
+ this._w = w;
46
+ }
47
+ /**
48
+ * This SLERP implementation assumes the quaternion data are managed in flat arrays.
49
+ * @param dst Input the output array
50
+ * @param dstOffset Input an offset into the output array
51
+ * @param src0 Input the source array of the starting quaternion.
52
+ * @param srcOffset0 Input an offset into the array src0.
53
+ * @param src1 Input the source array of the target quaternion.
54
+ * @param srcOffset1 Input an offset into the array src1.
55
+ * @param t Input normalized interpolation factor (between 0 and 1).
56
+ */
57
+ AcGeQuaternion.slerpFlat = function (dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t) {
58
+ // fuzz-free, array-based Quaternion SLERP operation
59
+ var x0 = src0[srcOffset0 + 0], y0 = src0[srcOffset0 + 1], z0 = src0[srcOffset0 + 2], w0 = src0[srcOffset0 + 3];
60
+ var x1 = src1[srcOffset1 + 0], y1 = src1[srcOffset1 + 1], z1 = src1[srcOffset1 + 2], w1 = src1[srcOffset1 + 3];
61
+ if (t === 0) {
62
+ dst[dstOffset + 0] = x0;
63
+ dst[dstOffset + 1] = y0;
64
+ dst[dstOffset + 2] = z0;
65
+ dst[dstOffset + 3] = w0;
66
+ return;
67
+ }
68
+ if (t === 1) {
69
+ dst[dstOffset + 0] = x1;
70
+ dst[dstOffset + 1] = y1;
71
+ dst[dstOffset + 2] = z1;
72
+ dst[dstOffset + 3] = w1;
73
+ return;
74
+ }
75
+ if (w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1) {
76
+ var s = 1 - t;
77
+ var cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1, dir = cos >= 0 ? 1 : -1, sqrSin = 1 - cos * cos;
78
+ // Skip the Slerp for tiny steps to avoid numeric problems:
79
+ if (sqrSin > Number.EPSILON) {
80
+ var sin = Math.sqrt(sqrSin), len = Math.atan2(sin, cos * dir);
81
+ s = Math.sin(s * len) / sin;
82
+ t = Math.sin(t * len) / sin;
83
+ }
84
+ var tDir = t * dir;
85
+ x0 = x0 * s + x1 * tDir;
86
+ y0 = y0 * s + y1 * tDir;
87
+ z0 = z0 * s + z1 * tDir;
88
+ w0 = w0 * s + w1 * tDir;
89
+ // Normalize in case we just did a lerp:
90
+ if (s === 1 - t) {
91
+ var f = 1 / Math.sqrt(x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0);
92
+ x0 *= f;
93
+ y0 *= f;
94
+ z0 *= f;
95
+ w0 *= f;
96
+ }
97
+ }
98
+ dst[dstOffset] = x0;
99
+ dst[dstOffset + 1] = y0;
100
+ dst[dstOffset + 2] = z0;
101
+ dst[dstOffset + 3] = w0;
102
+ };
103
+ /**
104
+ * This multiplication implementation assumes the quaternion data are managed in flat arrays.
105
+ * @param dst Input the output array.
106
+ * @param dstOffset Input an offset into the output array.
107
+ * @param src0 Input the source array of the starting quaternion.
108
+ * @param srcOffset0 Input an offset into the array src0.
109
+ * @param src1 Input the source array of the target quaternion.
110
+ * @param srcOffset1 Input an offset into the array src1.
111
+ * @returns Return an array
112
+ */
113
+ AcGeQuaternion.multiplyQuaternionsFlat = function (dst, dstOffset, src0, srcOffset0, src1, srcOffset1) {
114
+ var x0 = src0[srcOffset0];
115
+ var y0 = src0[srcOffset0 + 1];
116
+ var z0 = src0[srcOffset0 + 2];
117
+ var w0 = src0[srcOffset0 + 3];
118
+ var x1 = src1[srcOffset1];
119
+ var y1 = src1[srcOffset1 + 1];
120
+ var z1 = src1[srcOffset1 + 2];
121
+ var w1 = src1[srcOffset1 + 3];
122
+ dst[dstOffset] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
123
+ dst[dstOffset + 1] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
124
+ dst[dstOffset + 2] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
125
+ dst[dstOffset + 3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
126
+ return dst;
127
+ };
128
+ Object.defineProperty(AcGeQuaternion.prototype, "x", {
129
+ /**
130
+ * X cooridinate
131
+ */
132
+ get: function () {
133
+ return this._x;
134
+ },
135
+ set: function (value) {
136
+ this._x = value;
137
+ this._onChangeCallback();
138
+ },
139
+ enumerable: false,
140
+ configurable: true
141
+ });
142
+ Object.defineProperty(AcGeQuaternion.prototype, "y", {
143
+ /**
144
+ * Y cooridinate
145
+ */
146
+ get: function () {
147
+ return this._y;
148
+ },
149
+ set: function (value) {
150
+ this._y = value;
151
+ this._onChangeCallback();
152
+ },
153
+ enumerable: false,
154
+ configurable: true
155
+ });
156
+ Object.defineProperty(AcGeQuaternion.prototype, "z", {
157
+ /**
158
+ * Z cooridinate
159
+ */
160
+ get: function () {
161
+ return this._z;
162
+ },
163
+ set: function (value) {
164
+ this._z = value;
165
+ this._onChangeCallback();
166
+ },
167
+ enumerable: false,
168
+ configurable: true
169
+ });
170
+ Object.defineProperty(AcGeQuaternion.prototype, "w", {
171
+ /**
172
+ * W cooridinate
173
+ */
174
+ get: function () {
175
+ return this._w;
176
+ },
177
+ set: function (value) {
178
+ this._w = value;
179
+ this._onChangeCallback();
180
+ },
181
+ enumerable: false,
182
+ configurable: true
183
+ });
184
+ /**
185
+ * Set x, y, z, w properties of this quaternion.
186
+ * @param x Input x coordinate
187
+ * @param y Input y coordinate
188
+ * @param z Input z coordinate
189
+ * @param w Input w coordinate
190
+ * @returns Return this quaternion
191
+ */
192
+ AcGeQuaternion.prototype.set = function (x, y, z, w) {
193
+ this._x = x;
194
+ this._y = y;
195
+ this._z = z;
196
+ this._w = w;
197
+ this._onChangeCallback();
198
+ return this;
199
+ };
200
+ /**
201
+ * Create a new quaternion with identical x, y, z and w properties to this one.
202
+ * @returns Return cloned instance
203
+ */
204
+ AcGeQuaternion.prototype.clone = function () {
205
+ return new AcGeQuaternion(this._x, this._y, this._z, this._w);
206
+ };
207
+ /**
208
+ * Copy the x, y, z and w properties of q into this quaternion.
209
+ * @param quaternion Input the quaternion copied from
210
+ * @returns Return this quaternion
211
+ */
212
+ AcGeQuaternion.prototype.copy = function (quaternion) {
213
+ this._x = quaternion.x;
214
+ this._y = quaternion.y;
215
+ this._z = quaternion.z;
216
+ this._w = quaternion.w;
217
+ this._onChangeCallback();
218
+ return this;
219
+ };
220
+ /**
221
+ * Sets this quaternion from the rotation specified by euler angle.
222
+ * @param euler Input one euler angle
223
+ * @param update Input one flag whether to trigger change callback function
224
+ * @returns Return this quaternion
225
+ */
226
+ AcGeQuaternion.prototype.setFromEuler = function (euler, update) {
227
+ if (update === void 0) { update = true; }
228
+ var x = euler.x, y = euler.y, z = euler.z, order = euler.order;
229
+ // http://www.mathworks.com/matlabcentral/fileexchange/
230
+ // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
231
+ // content/SpinCalc.m
232
+ var cos = Math.cos;
233
+ var sin = Math.sin;
234
+ var c1 = cos(x / 2);
235
+ var c2 = cos(y / 2);
236
+ var c3 = cos(z / 2);
237
+ var s1 = sin(x / 2);
238
+ var s2 = sin(y / 2);
239
+ var s3 = sin(z / 2);
240
+ switch (order) {
241
+ case 'XYZ':
242
+ this._x = s1 * c2 * c3 + c1 * s2 * s3;
243
+ this._y = c1 * s2 * c3 - s1 * c2 * s3;
244
+ this._z = c1 * c2 * s3 + s1 * s2 * c3;
245
+ this._w = c1 * c2 * c3 - s1 * s2 * s3;
246
+ break;
247
+ case 'YXZ':
248
+ this._x = s1 * c2 * c3 + c1 * s2 * s3;
249
+ this._y = c1 * s2 * c3 - s1 * c2 * s3;
250
+ this._z = c1 * c2 * s3 - s1 * s2 * c3;
251
+ this._w = c1 * c2 * c3 + s1 * s2 * s3;
252
+ break;
253
+ case 'ZXY':
254
+ this._x = s1 * c2 * c3 - c1 * s2 * s3;
255
+ this._y = c1 * s2 * c3 + s1 * c2 * s3;
256
+ this._z = c1 * c2 * s3 + s1 * s2 * c3;
257
+ this._w = c1 * c2 * c3 - s1 * s2 * s3;
258
+ break;
259
+ case 'ZYX':
260
+ this._x = s1 * c2 * c3 - c1 * s2 * s3;
261
+ this._y = c1 * s2 * c3 + s1 * c2 * s3;
262
+ this._z = c1 * c2 * s3 - s1 * s2 * c3;
263
+ this._w = c1 * c2 * c3 + s1 * s2 * s3;
264
+ break;
265
+ case 'YZX':
266
+ this._x = s1 * c2 * c3 + c1 * s2 * s3;
267
+ this._y = c1 * s2 * c3 + s1 * c2 * s3;
268
+ this._z = c1 * c2 * s3 - s1 * s2 * c3;
269
+ this._w = c1 * c2 * c3 - s1 * s2 * s3;
270
+ break;
271
+ case 'XZY':
272
+ this._x = s1 * c2 * c3 - c1 * s2 * s3;
273
+ this._y = c1 * s2 * c3 - s1 * c2 * s3;
274
+ this._z = c1 * c2 * s3 + s1 * s2 * c3;
275
+ this._w = c1 * c2 * c3 + s1 * s2 * s3;
276
+ break;
277
+ default:
278
+ console.warn('THREE.Quaternion: .setFromEuler() encountered an unknown order: ' +
279
+ order);
280
+ }
281
+ if (update === true)
282
+ this._onChangeCallback();
283
+ return this;
284
+ };
285
+ /**
286
+ * Set this quaternion from rotation specified by axis and angle. Axis is assumed to be normalized,
287
+ * angle is in radians.
288
+ * @param axis Input one normalized axis
289
+ * @param angle Input one angle in radians
290
+ * @returns Return this quaternion
291
+ */
292
+ AcGeQuaternion.prototype.setFromAxisAngle = function (axis, angle) {
293
+ // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
294
+ // assumes axis is normalized
295
+ var halfAngle = angle / 2, s = Math.sin(halfAngle);
296
+ this._x = axis.x * s;
297
+ this._y = axis.y * s;
298
+ this._z = axis.z * s;
299
+ this._w = Math.cos(halfAngle);
300
+ this._onChangeCallback();
301
+ return this;
302
+ };
303
+ /**
304
+ * Set this quaternion from rotation component of the specified matrix.
305
+ * @param m Input a Matrix4 of which the upper 3x3 of matrix is a pure rotation matrix (i.e. unscaled).
306
+ * @returns Return this quaternion
307
+ */
308
+ AcGeQuaternion.prototype.setFromRotationMatrix = function (m) {
309
+ // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
310
+ // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
311
+ var te = m.elements, m11 = te[0], m12 = te[4], m13 = te[8], m21 = te[1], m22 = te[5], m23 = te[9], m31 = te[2], m32 = te[6], m33 = te[10], trace = m11 + m22 + m33;
312
+ if (trace > 0) {
313
+ var s = 0.5 / Math.sqrt(trace + 1.0);
314
+ this._w = 0.25 / s;
315
+ this._x = (m32 - m23) * s;
316
+ this._y = (m13 - m31) * s;
317
+ this._z = (m21 - m12) * s;
318
+ }
319
+ else if (m11 > m22 && m11 > m33) {
320
+ var s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
321
+ this._w = (m32 - m23) / s;
322
+ this._x = 0.25 * s;
323
+ this._y = (m12 + m21) / s;
324
+ this._z = (m13 + m31) / s;
325
+ }
326
+ else if (m22 > m33) {
327
+ var s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
328
+ this._w = (m13 - m31) / s;
329
+ this._x = (m12 + m21) / s;
330
+ this._y = 0.25 * s;
331
+ this._z = (m23 + m32) / s;
332
+ }
333
+ else {
334
+ var s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
335
+ this._w = (m21 - m12) / s;
336
+ this._x = (m13 + m31) / s;
337
+ this._y = (m23 + m32) / s;
338
+ this._z = 0.25 * s;
339
+ }
340
+ this._onChangeCallback();
341
+ return this;
342
+ };
343
+ /**
344
+ * Set this quaternion to the rotation required to rotate direction vector vFrom to direction vector vTo.
345
+ * @param vFrom Input one normalized direction vector
346
+ * @param vTo Input one normalized direction vector
347
+ * @returns Return this quaternion
348
+ */
349
+ AcGeQuaternion.prototype.setFromUnitVectors = function (vFrom, vTo) {
350
+ // assumes direction vectors vFrom and vTo are normalized
351
+ var r = vFrom.dot(vTo) + 1;
352
+ if (r < Number.EPSILON) {
353
+ // vFrom and vTo point in opposite directions
354
+ r = 0;
355
+ if (Math.abs(vFrom.x) > Math.abs(vFrom.z)) {
356
+ this._x = -vFrom.y;
357
+ this._y = vFrom.x;
358
+ this._z = 0;
359
+ this._w = r;
360
+ }
361
+ else {
362
+ this._x = 0;
363
+ this._y = -vFrom.z;
364
+ this._z = vFrom.y;
365
+ this._w = r;
366
+ }
367
+ }
368
+ else {
369
+ // crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3
370
+ this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
371
+ this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
372
+ this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
373
+ this._w = r;
374
+ }
375
+ return this.normalize();
376
+ };
377
+ /**
378
+ * Return the angle between this quaternion and quaternion q in radians.
379
+ * @param q Input one quaternion
380
+ * @returns Return the angle between this quaternion and quaternion q in radians.
381
+ */
382
+ AcGeQuaternion.prototype.angleTo = function (q) {
383
+ return 2 * Math.acos(Math.abs(MathUtils.clamp(this.dot(q), -1, 1)));
384
+ };
385
+ /**
386
+ * Rotate this quaternion by a given angular step to the defined quaternion q. The method ensures
387
+ * that the final quaternion will not overshoot q.
388
+ * @param q Input the target quaternion.
389
+ * @param step Input the angular step in radians.
390
+ * @returns Return this quaternion
391
+ */
392
+ AcGeQuaternion.prototype.rotateTowards = function (q, step) {
393
+ var angle = this.angleTo(q);
394
+ if (angle === 0)
395
+ return this;
396
+ var t = Math.min(1, step / angle);
397
+ this.slerp(q, t);
398
+ return this;
399
+ };
400
+ /**
401
+ * Set this quaternion to the identity quaternion; that is, to the quaternion that represents
402
+ * "no rotation".
403
+ * @returns Return this quaternion.
404
+ */
405
+ AcGeQuaternion.prototype.identity = function () {
406
+ return this.set(0, 0, 0, 1);
407
+ };
408
+ /**
409
+ * Invert this quaternion - calculates the conjugate. The quaternion is assumed to have unit length.
410
+ * @returns Return this quaternion.
411
+ */
412
+ AcGeQuaternion.prototype.invert = function () {
413
+ // quaternion is assumed to have unit length
414
+ return this.conjugate();
415
+ };
416
+ /**
417
+ * Return the rotational conjugate of this quaternion. The conjugate of a quaternion represents the
418
+ * same rotation in the opposite direction about the rotational axis.
419
+ * @returns Return this quaternion
420
+ */
421
+ AcGeQuaternion.prototype.conjugate = function () {
422
+ this._x *= -1;
423
+ this._y *= -1;
424
+ this._z *= -1;
425
+ this._onChangeCallback();
426
+ return this;
427
+ };
428
+ /**
429
+ * Calculate the dot product of quaternions v and this one.
430
+ * @param v Input one quaternion
431
+ * @returns Return the dot product of quaternions v and this one
432
+ */
433
+ AcGeQuaternion.prototype.dot = function (v) {
434
+ return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
435
+ };
436
+ /**
437
+ * Compute the squared Euclidean length (straight-line length) of this quaternion, considered as a
438
+ * 4 dimensional vector. This can be useful if you are comparing the lengths of two quaternions, as
439
+ * this is a slightly more efficient calculation than length().
440
+ * @returns Return the squared Euclidean length (straight-line length) of this quaternion
441
+ */
442
+ AcGeQuaternion.prototype.lengthSq = function () {
443
+ return (this._x * this._x +
444
+ this._y * this._y +
445
+ this._z * this._z +
446
+ this._w * this._w);
447
+ };
448
+ /**
449
+ * Compute the Euclidean length (straight-line length) of this quaternion, considered as a 4 dimensional
450
+ * vector.
451
+ * @returns Return the Euclidean length (straight-line length) of this quaternion.
452
+ */
453
+ AcGeQuaternion.prototype.length = function () {
454
+ return Math.sqrt(this._x * this._x +
455
+ this._y * this._y +
456
+ this._z * this._z +
457
+ this._w * this._w);
458
+ };
459
+ /**
460
+ * Normalize this quaternion - that is, calculated the quaternion that performs the same rotation as
461
+ * this one, but has length equal to 1.
462
+ * @returns Return this quaternion
463
+ */
464
+ AcGeQuaternion.prototype.normalize = function () {
465
+ var l = this.length();
466
+ if (l === 0) {
467
+ this._x = 0;
468
+ this._y = 0;
469
+ this._z = 0;
470
+ this._w = 1;
471
+ }
472
+ else {
473
+ l = 1 / l;
474
+ this._x = this._x * l;
475
+ this._y = this._y * l;
476
+ this._z = this._z * l;
477
+ this._w = this._w * l;
478
+ }
479
+ this._onChangeCallback();
480
+ return this;
481
+ };
482
+ /**
483
+ * Multiply this quaternion by q.
484
+ * @param q Input one quaternion to multiply
485
+ * @returns Return this quaternion
486
+ */
487
+ AcGeQuaternion.prototype.multiply = function (q) {
488
+ return this.multiplyQuaternions(this, q);
489
+ };
490
+ /**
491
+ * Pre-multiply this quaternion by q.
492
+ * @param q Input one quaternion
493
+ * @returns Return this quaternion
494
+ */
495
+ AcGeQuaternion.prototype.premultiply = function (q) {
496
+ return this.multiplyQuaternions(q, this);
497
+ };
498
+ /**
499
+ * Sets this quaternion to a x b.
500
+ * @param a Input one quaternion
501
+ * @param b Input one quaternion
502
+ * @returns Return this quaternion
503
+ */
504
+ AcGeQuaternion.prototype.multiplyQuaternions = function (a, b) {
505
+ // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
506
+ var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
507
+ var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
508
+ this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
509
+ this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
510
+ this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
511
+ this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
512
+ this._onChangeCallback();
513
+ return this;
514
+ };
515
+ /**
516
+ * Handles the spherical linear interpolation between quaternions. t represents the amount of rotation
517
+ * between this quaternion (where t is 0) and qb (where t is 1).
518
+ * @param qb Input the other quaternion rotation
519
+ * @param t Input interpolation factor in the closed interval [0, 1].
520
+ * @returns Return this quaternion
521
+ */
522
+ AcGeQuaternion.prototype.slerp = function (qb, t) {
523
+ if (t === 0)
524
+ return this;
525
+ if (t === 1)
526
+ return this.copy(qb);
527
+ var x = this._x, y = this._y, z = this._z, w = this._w;
528
+ // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
529
+ var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
530
+ if (cosHalfTheta < 0) {
531
+ this._w = -qb._w;
532
+ this._x = -qb._x;
533
+ this._y = -qb._y;
534
+ this._z = -qb._z;
535
+ cosHalfTheta = -cosHalfTheta;
536
+ }
537
+ else {
538
+ this.copy(qb);
539
+ }
540
+ if (cosHalfTheta >= 1.0) {
541
+ this._w = w;
542
+ this._x = x;
543
+ this._y = y;
544
+ this._z = z;
545
+ return this;
546
+ }
547
+ var sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta;
548
+ if (sqrSinHalfTheta <= Number.EPSILON) {
549
+ var s = 1 - t;
550
+ this._w = s * w + t * this._w;
551
+ this._x = s * x + t * this._x;
552
+ this._y = s * y + t * this._y;
553
+ this._z = s * z + t * this._z;
554
+ this.normalize(); // normalize calls _onChangeCallback()
555
+ return this;
556
+ }
557
+ var sinHalfTheta = Math.sqrt(sqrSinHalfTheta);
558
+ var halfTheta = Math.atan2(sinHalfTheta, cosHalfTheta);
559
+ var ratioA = Math.sin((1 - t) * halfTheta) / sinHalfTheta, ratioB = Math.sin(t * halfTheta) / sinHalfTheta;
560
+ this._w = w * ratioA + this._w * ratioB;
561
+ this._x = x * ratioA + this._x * ratioB;
562
+ this._y = y * ratioA + this._y * ratioB;
563
+ this._z = z * ratioA + this._z * ratioB;
564
+ this._onChangeCallback();
565
+ return this;
566
+ };
567
+ /**
568
+ * Perform a spherical linear interpolation between the given quaternions and stores the result in
569
+ * this quaternion.
570
+ * @param qa Input one quaternion rotation
571
+ * @param qb Input the other quaternion rotation
572
+ * @param t Input interpolation factor in the closed interval [0, 1].
573
+ * @returns Return this quaternion
574
+ */
575
+ AcGeQuaternion.prototype.slerpQuaternions = function (qa, qb, t) {
576
+ return this.copy(qa).slerp(qb, t);
577
+ };
578
+ /**
579
+ * Set this quaternion to a uniformly random, normalized quaternion.
580
+ * @returns Return this quaternion
581
+ */
582
+ AcGeQuaternion.prototype.random = function () {
583
+ // sets this quaternion to a uniform random unit quaternnion
584
+ // Ken Shoemake
585
+ // Uniform random rotations
586
+ // D. Kirk, editor, Graphics Gems III, pages 124-132. Academic Press, New York, 1992.
587
+ var theta1 = 2 * Math.PI * Math.random();
588
+ var theta2 = 2 * Math.PI * Math.random();
589
+ var x0 = Math.random();
590
+ var r1 = Math.sqrt(1 - x0);
591
+ var r2 = Math.sqrt(x0);
592
+ return this.set(r1 * Math.sin(theta1), r1 * Math.cos(theta1), r2 * Math.sin(theta2), r2 * Math.cos(theta2));
593
+ };
594
+ /**
595
+ * Compare the x, y, z and w properties of v to the equivalent properties of this quaternion to
596
+ * determine if they represent the same rotation.
597
+ * @param quaternion Input quaternion that this quaternion will be compared to.
598
+ * @returns Return true if the specified quaternion and this quaternion represent the same rotation.
599
+ */
600
+ AcGeQuaternion.prototype.equals = function (quaternion) {
601
+ return (quaternion._x === this._x &&
602
+ quaternion._y === this._y &&
603
+ quaternion._z === this._z &&
604
+ quaternion._w === this._w);
605
+ };
606
+ /**
607
+ * Set this quaternion's x, y, z and w properties from an array.
608
+ * @param array Input an array of format (x, y, z, w) used to construct the quaternion.
609
+ * @param offset Input an optional offset into the array
610
+ * @returns Return this quaternion
611
+ */
612
+ AcGeQuaternion.prototype.fromArray = function (array, offset) {
613
+ if (offset === void 0) { offset = 0; }
614
+ this._x = array[offset];
615
+ this._y = array[offset + 1];
616
+ this._z = array[offset + 2];
617
+ this._w = array[offset + 3];
618
+ this._onChangeCallback();
619
+ return this;
620
+ };
621
+ /**
622
+ * Return the numerical elements of this quaternion in an array of format [x, y, z, w].
623
+ * @param array Input an optional array to store the quaternion. If not specified, a new array will be created.
624
+ * @param offset (optional) if specified, the result will be copied into this Array.
625
+ * @returns Return an array
626
+ */
627
+ AcGeQuaternion.prototype.toArray = function (array, offset) {
628
+ if (array === void 0) { array = []; }
629
+ if (offset === void 0) { offset = 0; }
630
+ array[offset] = this._x;
631
+ array[offset + 1] = this._y;
632
+ array[offset + 2] = this._z;
633
+ array[offset + 3] = this._w;
634
+ return array;
635
+ };
636
+ AcGeQuaternion.prototype.toJSON = function () {
637
+ return this.toArray();
638
+ };
639
+ AcGeQuaternion.prototype._onChange = function (callback) {
640
+ this._onChangeCallback = callback;
641
+ return this;
642
+ };
643
+ AcGeQuaternion.prototype._onChangeCallback = function () { };
644
+ AcGeQuaternion.prototype[Symbol.iterator] = function () {
645
+ return __generator(this, function (_a) {
646
+ switch (_a.label) {
647
+ case 0: return [4 /*yield*/, this._x];
648
+ case 1:
649
+ _a.sent();
650
+ return [4 /*yield*/, this._y];
651
+ case 2:
652
+ _a.sent();
653
+ return [4 /*yield*/, this._z];
654
+ case 3:
655
+ _a.sent();
656
+ return [4 /*yield*/, this._w];
657
+ case 4:
658
+ _a.sent();
659
+ return [2 /*return*/];
660
+ }
661
+ });
662
+ };
663
+ return AcGeQuaternion;
664
+ }());
665
+ export { AcGeQuaternion };
666
+ //# sourceMappingURL=AcGeQuaternion.js.map