@openglobus/og 0.11.1 → 0.11.6

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 (255) hide show
  1. package/package.json +18 -20
  2. package/src/og/Clock.js +11 -11
  3. package/src/og/Deferred.js +1 -1
  4. package/src/og/Events.js +9 -7
  5. package/src/og/Extent.js +117 -72
  6. package/src/og/Globe.js +35 -22
  7. package/src/og/ImageCanvas.js +24 -18
  8. package/src/og/Lock.js +2 -2
  9. package/src/og/LonLat.js +40 -37
  10. package/src/og/Popup.js +21 -15
  11. package/src/og/QueueArray.js +7 -3
  12. package/src/og/Rectangle.js +11 -6
  13. package/src/og/Stack.js +8 -5
  14. package/src/og/ajax.js +21 -33
  15. package/src/og/astro/astro.js +5 -5
  16. package/src/og/astro/earth.js +23 -17
  17. package/src/og/astro/jd.js +44 -39
  18. package/src/og/astro/orbit.js +20 -18
  19. package/src/og/astro/rotation.js +17 -8
  20. package/src/og/bv/Box.js +7 -3
  21. package/src/og/bv/Sphere.js +17 -9
  22. package/src/og/camera/Camera.js +137 -86
  23. package/src/og/camera/Frustum.js +30 -19
  24. package/src/og/camera/PlanetCamera.js +30 -26
  25. package/src/og/control/CompassButton.js +8 -2
  26. package/src/og/control/Control.js +13 -11
  27. package/src/og/control/DebugInfo.js +4 -1
  28. package/src/og/control/EarthCoordinates.js +62 -41
  29. package/src/og/control/EarthNavigation.js +20 -22
  30. package/src/og/control/GeoImageDragControl.js +1 -1
  31. package/src/og/control/KeyboardNavigation.js +12 -12
  32. package/src/og/control/LayerSwitcher.js +24 -23
  33. package/src/og/control/Lighting.js +1 -1
  34. package/src/og/control/MouseNavigation.js +1 -1
  35. package/src/og/control/ScaleControl.js +1 -1
  36. package/src/og/control/SegmentBoundVisualization.js +8 -9
  37. package/src/og/control/ShowFps.js +15 -10
  38. package/src/og/control/SimpleNavigation.js +8 -10
  39. package/src/og/control/Sun.js +25 -15
  40. package/src/og/control/ToggleWireframe.js +5 -5
  41. package/src/og/control/TouchNavigation.js +58 -45
  42. package/src/og/control/ZoomControl.js +41 -25
  43. package/src/og/control/index.js +4 -0
  44. package/src/og/ellipsoid/Ellipsoid.js +181 -85
  45. package/src/og/ellipsoid/wgs84.js +4 -4
  46. package/src/og/entity/BaseBillboard.js +97 -44
  47. package/src/og/entity/Billboard.js +15 -11
  48. package/src/og/entity/BillboardHandler.js +314 -64
  49. package/src/og/entity/Entity.js +106 -74
  50. package/src/og/entity/EntityCollection.js +72 -49
  51. package/src/og/entity/GeoObject.js +228 -0
  52. package/src/og/entity/GeoObjectHandler.js +910 -0
  53. package/src/og/entity/Geometry.js +29 -26
  54. package/src/og/entity/GeometryHandler.js +598 -126
  55. package/src/og/entity/Label.js +74 -40
  56. package/src/og/entity/LabelHandler.js +47 -246
  57. package/src/og/entity/LabelWorker.js +213 -0
  58. package/src/og/entity/Object3d.js +504 -0
  59. package/src/og/entity/PointCloud.js +81 -33
  60. package/src/og/entity/PointCloudHandler.js +9 -9
  61. package/src/og/entity/Polyline.js +39 -39
  62. package/src/og/entity/Ray.js +61 -28
  63. package/src/og/entity/ShapeHandler.js +1 -1
  64. package/src/og/entity/Strip.js +77 -48
  65. package/src/og/entity/StripHandler.js +27 -27
  66. package/src/og/entity/index.js +8 -16
  67. package/src/og/input/KeyboardHandler.js +107 -104
  68. package/src/og/input/MouseHandler.js +81 -78
  69. package/src/og/input/TouchHandler.js +55 -51
  70. package/src/og/layer/BaseGeoImage.js +61 -30
  71. package/src/og/layer/CanvasTiles.js +26 -24
  72. package/src/og/layer/GeoImage.js +10 -10
  73. package/src/og/layer/GeoTexture2d.js +1 -1
  74. package/src/og/layer/GeoVideo.js +3 -3
  75. package/src/og/layer/KML.js +66 -40
  76. package/src/og/layer/Layer.js +27 -17
  77. package/src/og/layer/Material.js +93 -61
  78. package/src/og/layer/Vector.js +118 -72
  79. package/src/og/layer/WMS.js +36 -19
  80. package/src/og/layer/XYZ.js +55 -48
  81. package/src/og/light/LightSource.js +47 -40
  82. package/src/og/math/Line2.js +24 -24
  83. package/src/og/math/Line3.js +103 -102
  84. package/src/og/math/Mat3.js +134 -121
  85. package/src/og/math/Mat4.js +603 -459
  86. package/src/og/math/Plane.js +21 -20
  87. package/src/og/math/Quat.js +980 -927
  88. package/src/og/math/Ray.js +195 -187
  89. package/src/og/math/Vec2.js +127 -117
  90. package/src/og/math/Vec3.js +206 -176
  91. package/src/og/math/Vec4.js +49 -53
  92. package/src/og/math/coder.js +18 -18
  93. package/src/og/math.js +12 -12
  94. package/src/og/mercator.js +11 -11
  95. package/src/og/proj/EPSG3857.js +4 -4
  96. package/src/og/proj/EPSG4326.js +4 -4
  97. package/src/og/proj/Proj.js +31 -31
  98. package/src/og/quadTree/EntityCollectionNode.js +310 -300
  99. package/src/og/quadTree/Node.js +1008 -993
  100. package/src/og/renderer/Renderer.js +768 -747
  101. package/src/og/renderer/RendererEvents.js +9 -6
  102. package/src/og/scene/Axes.js +1 -1
  103. package/src/og/scene/BaseNode.js +8 -9
  104. package/src/og/scene/Planet.js +115 -87
  105. package/src/og/scene/RenderNode.js +29 -22
  106. package/src/og/scene/SkyBox.js +1 -1
  107. package/src/og/segment/Segment.js +1886 -1878
  108. package/src/og/segment/SegmentLonLat.js +298 -268
  109. package/src/og/segment/segmentHelper.js +11 -11
  110. package/src/og/shaders/bloom.js +1 -1
  111. package/src/og/shaders/blur.js +2 -2
  112. package/src/og/shaders/depth.js +1 -1
  113. package/src/og/shaders/geoObject.js +211 -0
  114. package/src/og/shaders/label.js +1 -1
  115. package/src/og/shaders/lumFilter.js +1 -1
  116. package/src/og/shaders/pointCloud.js +42 -42
  117. package/src/og/shaders/screenFrame.js +1 -1
  118. package/src/og/shaders/shape.js +15 -21
  119. package/src/og/shaders/skybox.js +37 -37
  120. package/src/og/shaders/toneMapping.js +1 -1
  121. package/src/og/shapes/BaseShape.js +129 -57
  122. package/src/og/shapes/Icosphere.js +30 -15
  123. package/src/og/shapes/Sphere.js +13 -16
  124. package/src/og/terrain/BilTerrain.js +2 -1
  125. package/src/og/terrain/EmptyTerrain.js +2 -2
  126. package/src/og/terrain/Geoid.js +2 -2
  127. package/src/og/terrain/GlobusTerrain.js +10 -10
  128. package/src/og/terrain/MapboxTerrain.js +1 -1
  129. package/src/og/utils/GeoImageCreator.js +9 -9
  130. package/src/og/utils/Loader.js +6 -6
  131. package/src/og/utils/NormalMapCreator.js +13 -13
  132. package/src/og/utils/TerrainWorker.js +1 -1
  133. package/src/og/utils/TextureAtlas.js +20 -22
  134. package/src/og/utils/VectorTileCreator.js +5 -5
  135. package/src/og/utils/earcut.js +1 -1
  136. package/src/og/utils/shared.js +15 -15
  137. package/src/og/webgl/Framebuffer.js +101 -53
  138. package/src/og/webgl/Handler.js +125 -85
  139. package/src/og/webgl/Multisample.js +55 -27
  140. package/src/og/webgl/Program.js +80 -35
  141. package/src/og/webgl/ProgramController.js +27 -23
  142. package/src/og/webgl/callbacks.js +8 -8
  143. package/types/Clock.d.ts +6 -2
  144. package/types/Deferred.d.ts +1 -1
  145. package/types/Events.d.ts +8 -4
  146. package/types/Extent.d.ts +37 -33
  147. package/types/Globe.d.ts +15 -13
  148. package/types/ImageCanvas.d.ts +5 -3
  149. package/types/LonLat.d.ts +24 -24
  150. package/types/Popup.d.ts +4 -0
  151. package/types/QueueArray.d.ts +6 -2
  152. package/types/Rectangle.d.ts +10 -4
  153. package/types/Stack.d.ts +4 -0
  154. package/types/ajax.d.ts +1 -1
  155. package/types/astro/earth.d.ts +3 -2
  156. package/types/bv/Box.d.ts +10 -5
  157. package/types/bv/Sphere.d.ts +13 -7
  158. package/types/camera/Camera.d.ts +90 -81
  159. package/types/camera/Frustum.d.ts +26 -22
  160. package/types/camera/PlanetCamera.d.ts +49 -53
  161. package/types/control/CompassButton.d.ts +4 -4
  162. package/types/control/Control.d.ts +7 -4
  163. package/types/control/DebugInfo.d.ts +3 -4
  164. package/types/control/EarthCoordinates.d.ts +6 -6
  165. package/types/control/EarthNavigation.d.ts +42 -0
  166. package/types/control/KeyboardNavigation.d.ts +3 -4
  167. package/types/control/LayerSwitcher.d.ts +3 -5
  168. package/types/control/Lighting.d.ts +3 -3
  169. package/types/control/MouseNavigation.d.ts +47 -0
  170. package/types/control/ScaleControl.d.ts +3 -3
  171. package/types/control/ShowFps.d.ts +3 -4
  172. package/types/control/SimpleNavigation.d.ts +3 -6
  173. package/types/control/Sun.d.ts +8 -10
  174. package/types/control/ToggleWireframe.d.ts +3 -3
  175. package/types/control/TouchNavigation.d.ts +48 -0
  176. package/types/control/ZoomControl.d.ts +3 -4
  177. package/types/control/index.d.ts +3 -1
  178. package/types/ellipsoid/Ellipsoid.d.ts +47 -43
  179. package/types/ellipsoid/wgs84.d.ts +3 -2
  180. package/types/entity/BaseBillboard.d.ts +41 -34
  181. package/types/entity/Billboard.d.ts +7 -7
  182. package/types/entity/BillboardHandler.d.ts +4 -0
  183. package/types/entity/Entity.d.ts +101 -83
  184. package/types/entity/EntityCollection.d.ts +57 -43
  185. package/types/entity/GeoObject.d.ts +111 -0
  186. package/types/entity/GeoObjectHandler.d.ts +77 -0
  187. package/types/entity/Geometry.d.ts +9 -7
  188. package/types/entity/GeometryHandler.d.ts +2 -2
  189. package/types/entity/Label.d.ts +24 -29
  190. package/types/entity/LabelHandler.d.ts +9 -2
  191. package/types/entity/LabelWorker.d.ts +9 -0
  192. package/types/entity/PointCloud.d.ts +8 -8
  193. package/types/entity/PointCloudHandler.d.ts +3 -3
  194. package/types/entity/Polyline.d.ts +58 -55
  195. package/types/entity/Ray.d.ts +19 -18
  196. package/types/entity/Strip.d.ts +8 -7
  197. package/types/entity/StripHandler.d.ts +3 -3
  198. package/types/entity/index.d.ts +7 -7
  199. package/types/input/KeyboardHandler.d.ts +10 -0
  200. package/types/input/MouseHandler.d.ts +5 -0
  201. package/types/input/TouchHandler.d.ts +5 -0
  202. package/types/layer/BaseGeoImage.d.ts +23 -35
  203. package/types/layer/CanvasTiles.d.ts +15 -22
  204. package/types/layer/GeoImage.d.ts +5 -18
  205. package/types/layer/GeoVideo.d.ts +5 -30
  206. package/types/layer/KML.d.ts +20 -15
  207. package/types/layer/Layer.d.ts +326 -0
  208. package/types/layer/Material.d.ts +45 -0
  209. package/types/layer/Vector.d.ts +208 -0
  210. package/types/layer/WMS.d.ts +4 -7
  211. package/types/layer/XYZ.d.ts +19 -25
  212. package/types/light/LightSource.d.ts +54 -53
  213. package/types/math/Line2.d.ts +11 -0
  214. package/types/math/Line3.d.ts +10 -0
  215. package/types/math/Mat3.d.ts +30 -18
  216. package/types/math/Mat4.d.ts +71 -52
  217. package/types/math/Plane.d.ts +5 -4
  218. package/types/math/Quat.d.ts +379 -0
  219. package/types/math/Ray.d.ts +82 -0
  220. package/types/math/Vec2.d.ts +102 -101
  221. package/types/math/Vec3.d.ts +143 -142
  222. package/types/math/Vec4.d.ts +40 -39
  223. package/types/math/coder.d.ts +12 -11
  224. package/types/math.d.ts +8 -8
  225. package/types/mercator.d.ts +3 -3
  226. package/types/proj/EPSG3857.d.ts +3 -2
  227. package/types/proj/EPSG4326.d.ts +3 -2
  228. package/types/proj/Proj.d.ts +42 -0
  229. package/types/quadTree/EntityCollectionNode.d.ts +34 -0
  230. package/types/quadTree/Node.d.ts +61 -0
  231. package/types/renderer/Renderer.d.ts +300 -0
  232. package/types/renderer/RendererEvents.d.ts +10 -7
  233. package/types/scene/BaseNode.d.ts +9 -9
  234. package/types/scene/Planet.d.ts +131 -126
  235. package/types/scene/RenderNode.d.ts +22 -23
  236. package/types/segment/Segment.d.ts +279 -0
  237. package/types/segment/SegmentLonLat.d.ts +16 -0
  238. package/types/shaders/geoObject.d.ts +3 -0
  239. package/types/shapes/BaseShape.d.ts +43 -40
  240. package/types/shapes/Sphere.d.ts +7 -7
  241. package/types/terrain/BilTerrain.d.ts +0 -1
  242. package/types/terrain/EmptyTerrain.d.ts +4 -4
  243. package/types/terrain/GlobusTerrain.d.ts +17 -54
  244. package/types/terrain/MapboxTerrain.d.ts +0 -1
  245. package/types/utils/GeoImageCreator.d.ts +3 -2
  246. package/types/utils/TextureAtlas.d.ts +8 -8
  247. package/types/utils/shared.d.ts +25 -24
  248. package/types/webgl/Framebuffer.d.ts +5 -5
  249. package/types/webgl/Handler.d.ts +68 -43
  250. package/types/webgl/Multisample.d.ts +5 -5
  251. package/types/webgl/Program.d.ts +8 -2
  252. package/types/webgl/ProgramController.d.ts +18 -13
  253. package/src/og/inherits.js +0 -8
  254. package/src/og/utils/polylabel.js +0 -176
  255. package/types/inherits.d.ts +0 -4
@@ -1,927 +1,980 @@
1
- 'use strict';
2
-
3
- import * as math from '../math.js';
4
- import { Mat4 } from './Mat4.js';
5
- import { Mat3 } from './Mat3.js';
6
- import { Vec3 } from './Vec3.js';
7
-
8
- /**
9
- * A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
10
- * @constructor
11
- * @param {Number} [x=0.0] The X component.
12
- * @param {Number} [y=0.0] The Y component.
13
- * @param {Number} [z=0.0] The Z component.
14
- * @param {Number} [w=0.0] The W component.
15
- */
16
- const Quat = function (x, y, z, w) {
17
-
18
- /**
19
- * The X component.
20
- * @public
21
- * @type {Number}
22
- * @default 0.0
23
- */
24
- this.x = x || 0.0;
25
-
26
- /**
27
- * The Y component.
28
- * @public
29
- * @type {Number}
30
- * @default 0.0
31
- */
32
- this.y = y || 0.0;
33
-
34
- /**
35
- * The Z component.
36
- * @public
37
- * @type {Number}
38
- * @default 0.0
39
- */
40
- this.z = z || 0.0;
41
-
42
- /**
43
- * The W component.
44
- * @public
45
- * @type {Number}
46
- * @default 0.0
47
- */
48
- this.w = w || 0.0;
49
- };
50
-
51
- /**
52
- * Creates Quat instance.
53
- * @function
54
- * @param {Number} [x=0.0] The X component.
55
- * @param {Number} [y=0.0] The Y component.
56
- * @param {Number} [z=0.0] The Z component.
57
- * @param {Number} [w=0.0] The W component.
58
- * @returns {og.Quat} -
59
- */
60
- export function quat(x, y, z, w) {
61
- return new Quat(x, y, z, w);
62
- };
63
-
64
- /**
65
- * Identity Quat.
66
- * @const
67
- * @type {og.Quat}
68
- */
69
- Quat.IDENTITY = new Quat(0.0, 0.0, 0.0, 1.0);
70
-
71
- /**
72
- * Returns a Quat represents rotation around X axis.
73
- * @static
74
- * @param {number} a - The angle in radians to rotate around the axis.
75
- * @returns {og.Quat} -
76
- */
77
- Quat.xRotation = function (a) {
78
- a *= 0.5;
79
- return new Quat(Math.sin(a), 0.0, 0.0, Math.cos(a));
80
- };
81
-
82
- /**
83
- * Returns a Quat represents rotation around Y axis.
84
- * @static
85
- * @param {number} a - The angle in radians to rotate around the axis.
86
- * @returns {og.Quat} -
87
- */
88
- Quat.yRotation = function (a) {
89
- a *= 0.5;
90
- return new Quat(0.0, Math.sin(a), 0.0, Math.cos(a));
91
- };
92
-
93
- /**
94
- * Returns a Quat represents rotation around Z axis.
95
- * @static
96
- * @param {number} a - The angle in radians to rotate around the axis.
97
- * @returns {og.Quat} -
98
- */
99
- Quat.zRotation = function (a) {
100
- a *= 0.5;
101
- return new Quat(0.0, 0.0, Math.sin(a), Math.cos(a));
102
- };
103
-
104
- /**
105
- * Computes a Quat representing a rotation around an axis.
106
- * @static
107
- * @param {og.Vec3} axis - The axis of rotation.
108
- * @param {number} [angle=0.0] The angle in radians to rotate around the axis.
109
- * @returns {og.Quat} -
110
- */
111
- Quat.axisAngleToQuat = function (axis, angle) {
112
- angle = angle || 0.0;
113
- var v = axis.normal();
114
- var half_angle = angle * 0.5;
115
- var sin_a = Math.sin(half_angle);
116
- return new Quat(
117
- v.x * sin_a,
118
- v.y * sin_a,
119
- v.z * sin_a,
120
- Math.cos(half_angle));
121
- };
122
-
123
- /**
124
- * Computes a rotation from the given heading and up vector.
125
- * @static
126
- * @param {og.Vec3} forward - Heading target coordinates.
127
- * @param {og.Vec3} up - Up vector.
128
- * @returns {og.Quat} -
129
- */
130
- Quat.getLookRotation = function (forward, up) {
131
-
132
- var f = forward.normal().negate();
133
- var s = (up.cross(f)).normalize();
134
- var u = f.cross(s);
135
-
136
- var z = 1.0 + s.x + u.y + f.z;
137
-
138
- if (z > 0.000001) {
139
- let fd = 1.0 / (2.0 * Math.sqrt(z));
140
- return new Quat(
141
- (f.y - u.z) * fd,
142
- (s.z - f.x) * fd,
143
- (u.x - s.y) * fd,
144
- 0.25 / fd
145
- );
146
- }
147
-
148
- if (s.x > u.y && s.x > f.z) {
149
- let fd = 1.0 / (2.0 * Math.sqrt(1.0 + s.x - u.y - f.z));
150
- return new Quat(
151
- 0.25 / fd,
152
- (u.x + s.y) * fd,
153
- (s.z + f.x) * fd,
154
- (f.y - u.z) * fd
155
- );
156
- }
157
-
158
- if (u.y > f.z) {
159
- let fd = 1.0 / (2.0 * Math.sqrt(1.0 + u.y - s.x - f.z));
160
- return new Quat(
161
- (u.x + s.y) * fd,
162
- 0.25 / fd,
163
- (f.y + u.z) * fd,
164
- (s.z - f.x) * fd
165
- );
166
- }
167
-
168
- let fd = 1.0 / (2.0 * Math.sqrt(1.0 + f.z - s.x - u.y));
169
- return new Quat(
170
- (s.z + f.x) * fd,
171
- (f.y + u.z) * fd,
172
- 0.25 / fd,
173
- (u.x - s.y) * fd
174
- );
175
- };
176
-
177
- /**
178
- * Computes a Quat from from source point heading to the destination point.
179
- * @static
180
- * @param {og.Vec3} sourcePoint - Source coordinate.
181
- * @param {og.Vec3} destPoint - Destination coordinate.
182
- * @returns {og.Quat} -
183
- */
184
- Quat.getLookAtSourceDest = function (sourcePoint, destPoint) {
185
- var forwardVector = destPoint.subA(sourcePoint).normalize();
186
- var dot = Vec3.FORWARD.dot(forwardVector);
187
- if (Math.abs(dot - (-1.0)) < 0.000001) {
188
- return Quat.axisAngleToQuat(Vec3.UP, Math.PI);
189
- }
190
- if (Math.abs(dot - (1.0)) < 0.000001) {
191
- return new Quat(0.0, 0.0, 0.0, 1.0);
192
- }
193
- var rotAngle = Math.acos(dot);
194
- var rotAxis = Vec3.FORWARD.cross(forwardVector).normalize();
195
- return Quat.axisAngleToQuat(rotAxis, rotAngle);
196
- };
197
-
198
- /**
199
- * Compute rotation between two vectors.
200
- * @static
201
- * @param {og.Vec3} u - First vector.
202
- * @param {og.Vec3} v - Second vector.
203
- * @returns {og.Quat} -
204
- */
205
- Quat.getRotationBetweenVectors = function (u, v) {
206
- var w = u.cross(v);
207
- var q = new Quat(w.x, w.y, w.z, 1.0 + u.dot(v));
208
- return q.normalize();
209
- };
210
-
211
- /**
212
- * Compute rotation between two vectors.
213
- * @static
214
- * @param {og.Vec3} u - First vector.
215
- * @param {og.Vec3} v - Second vector.
216
- * @returns {og.Quat} -
217
- */
218
- Quat.getRotationBetweenVectorsRes = function (u, v, res) {
219
- var w = u.cross(v);
220
- res.set(w.x, w.y, w.z, 1.0 + u.dot(v));
221
- return res.normalize();
222
- };
223
-
224
- /**
225
- * Compute rotation between two vectors with around vector up
226
- * for exactly opposite vectors. If vectors exaclty in the same
227
- * direction than returns identity Quat.
228
- * @static
229
- * @param {og.Vec3} source - First vector.
230
- * @param {og.Vec3} dest - Second vector.
231
- * @param {og.Vec3} up - Up vector.
232
- * @returns {og.Quat} -
233
- */
234
- Quat.getRotationBetweenVectorsUp = function (source, dest, up) {
235
- var dot = source.dot(dest);
236
- if (Math.abs(dot + 1.0) < 0.000001) {
237
- // vector source and dest point exactly in the opposite direction,
238
- // so it is a 180 degrees turn around the up-axis
239
- return Quat.axisAngleToQuat(up, Math.PI);
240
- }
241
- if (Math.abs(dot - 1.0) < 0.000001) {
242
- // vector source and dest point exactly in the same direction
243
- // so we return the identity Quat
244
- return new Quat(0, 0, 0, 1);
245
- }
246
- var rotAngle = Math.acos(dot);
247
- var rotAxis = source.cross(dest).normalize();
248
- return Quat.axisAngleToQuat(rotAxis, rotAngle);
249
- };
250
-
251
- /**
252
- * Returns true if the components are zero.
253
- * @public
254
- * @param {og.Quat} q - Quat to subtract.
255
- * @returns {og.Quat} -
256
- */
257
- Quat.prototype.isZero = function () {
258
- return this.x === 0.0 && this.y === 0.0 && this.z === 0.0 && this.w === 0.0;
259
- };
260
-
261
- /**
262
- * Clear Quat. Sets zeroes.
263
- * @public
264
- * @returns {og.Quat} -
265
- */
266
- Quat.prototype.clear = function () {
267
- this.x = this.y = this.z = this.w = 0;
268
- return this;
269
- };
270
-
271
- /**
272
- * Sets Quat values.
273
- * @public
274
- * @param {Number} [x=0.0] The X component.
275
- * @param {Number} [y=0.0] The Y component.
276
- * @param {Number} [z=0.0] The Z component.
277
- * @param {Number} [w=0.0] The W component.
278
- * @returns {og.Quat} -
279
- */
280
- Quat.prototype.set = function (x, y, z, w) {
281
- this.x = x;
282
- this.y = y;
283
- this.z = z;
284
- this.w = w;
285
- return this;
286
- };
287
-
288
- /**
289
- * Copy Quat values.
290
- * @public
291
- * @param {og.Quat} q - Copy Quat.
292
- * @returns {og.Quat} -
293
- */
294
- Quat.prototype.copy = function (q) {
295
- this.x = q.x;
296
- this.y = q.y;
297
- this.z = q.z;
298
- this.w = q.w;
299
- return this;
300
- };
301
-
302
- /**
303
- * Set current Quat instance to identity Quat.
304
- * @public
305
- * @returns {og.Quat} -
306
- */
307
- Quat.prototype.setIdentity = function () {
308
- this.x = 0.0;
309
- this.y = 0.0;
310
- this.z = 0.0;
311
- this.w = 1.0;
312
- return this;
313
- };
314
-
315
- /**
316
- * Duplicates a Quat instance.
317
- * @public
318
- * @returns {og.Quat} -
319
- */
320
- Quat.prototype.clone = function () {
321
- return new Quat(this.x, this.y, this.z, this.w);
322
- };
323
-
324
- /**
325
- * Computes the componentwise sum of two Quats.
326
- * @public
327
- * @param {og.Quat} q - Quat to add.
328
- * @returns {og.Quat} -
329
- */
330
- Quat.prototype.add = function (q) {
331
- return new Quat(this.x + q.x, this.y + q.y, this.z + q.z, this.w + q.w);
332
- };
333
-
334
- /**
335
- * Computes the componentwise difference of two Quats.
336
- * @public
337
- * @param {og.Quat} q - Quat to subtract.
338
- * @returns {og.Quat} -
339
- */
340
- Quat.prototype.sub = function (q) {
341
- return new Quat(this.x - q.x, this.y - q.y, this.z - q.z, this.w - q.w);
342
- };
343
-
344
- /**
345
- * Multiplies the provided Quat componentwise by the provided scalar.
346
- * @public
347
- * @param {Number} scale - The scalar to multiply with.
348
- * @returns {og.Quat} -
349
- */
350
- Quat.prototype.scaleTo = function (scale) {
351
- return new Quat(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
352
- };
353
-
354
- /**
355
- * Multiplies the provided Quat componentwise.
356
- * @public
357
- * @param {Number} scale - The scalar to multiply with.
358
- * @returns {og.Quat} -
359
- */
360
- Quat.prototype.scale = function (scale) {
361
- this.x *= scale; this.y *= scale; this.z *= scale; this.w *= scale;
362
- return this;
363
- };
364
-
365
- /**
366
- * Converts Quat values to array.
367
- * @public
368
- * @returns {Array.<number,number,number,number>} -
369
- */
370
- Quat.prototype.toVec = function () {
371
- return [this.x, this.y, this.z, this.w];
372
- };
373
-
374
- /**
375
- * Sets current quaternion by spherical coordinates.
376
- * @public
377
- * @param {number} lat - Latitude.
378
- * @param {number} lon - Longitude.
379
- * @param {number} angle - Angle in radians.
380
- * @returns {og.Quat} -
381
- */
382
- Quat.prototype.setFromSphericalCoords = function (lat, lon, angle) {
383
- var sin_a = Math.sin(angle / 2);
384
- var cos_a = Math.cos(angle / 2);
385
- var sin_lat = Math.sin(lat);
386
- var cos_lat = Math.cos(lat);
387
- var sin_long = Math.sin(lon);
388
- var cos_long = Math.cos(lon);
389
- this.x = sin_a * cos_lat * sin_long;
390
- this.y = sin_a * sin_lat;
391
- this.z = sin_a * sin_lat * cos_long;
392
- this.w = cos_a;
393
- return this;
394
- };
395
-
396
- /**
397
- * Sets rotation with the given heading and up vectors.
398
- * @static
399
- * @param {og.Vec3} forward - Heading target coordinates.
400
- * @param {og.Vec3} up - Up vector.
401
- * @returns {og.Quat} -
402
- */
403
- Quat.prototype.setLookRotation = function (forward, up) {
404
-
405
- var f = forward.normal().negate();
406
- var s = (up.cross(f)).normalize();
407
- var u = f.cross(s);
408
-
409
- var z = 1.0 + s.x + u.y + f.z;
410
-
411
- if (z > 0.000001) {
412
- let fd = 1.0 / (2.0 * Math.sqrt(z));
413
- this.x = (f.y - u.z) * fd;
414
- this.y = (s.z - f.x) * fd;
415
- this.z = (u.x - s.y) * fd;
416
- this.w = 0.25 / fd;
417
- } else if (s.x > u.y && s.x > f.z) {
418
- let fd = 1.0 / (2.0 * Math.sqrt(1.0 + s.x - u.y - f.z));
419
- this.x = 0.25 / fd;
420
- this.y = (u.x + s.y) * fd;
421
- this.z = (s.z + f.x) * fd;
422
- this.w = (f.y - u.z) * fd;
423
- } else if (u.y > f.z) {
424
- let fd = 1.0 / (2.0 * Math.sqrt(1.0 + u.y - s.x - f.z));
425
- this.x = (u.x + s.y) * fd;
426
- this.y = 0.25 / fd;
427
- this.z = (f.y + u.z) * fd;
428
- this.w = (s.z - f.x) * fd;
429
- } else {
430
- let fd = 1.0 / (2.0 * Math.sqrt(1.0 + f.z - s.x - u.y));
431
- this.x = (s.z + f.x) * fd;
432
- this.y = (f.y + u.z) * fd;
433
- this.z = 0.25 / fd;
434
- this.w = (u.x - s.y) * fd;
435
- }
436
-
437
- return this;
438
- };
439
-
440
- /**
441
- * Gets spherical coordinates.
442
- * @public
443
- * @returns {Object} Returns object with latitude, longitude and alpha.
444
- */
445
- Quat.prototype.toSphericalCoords = function () {
446
- var cos_a = this.w;
447
- var sin_a = Math.sqrt(1.0 - cos_a * cos_a);
448
- // var angle = Math.acos(cos_a) * 2;
449
- if (Math.abs(sin_a) < 0.0005) {
450
- sin_a = 1;
451
- }
452
- var tx = this.x / sin_a;
453
- var ty = this.y / sin_a;
454
- var tz = this.z / sin_a;
455
-
456
- var lon, lat = -Math.asin(ty);
457
- if (tx * tx + tz * tz < 0.0005) {
458
- lon = 0;
459
- } else {
460
- lon = Math.atan2(tx, tz);
461
- }
462
- if (lon < 0) {
463
- lon += 360.0;
464
- }
465
-
466
- return { lat: lat, lon: lon, alpha: Math.acos(cos_a) };
467
- };
468
-
469
- /**
470
- * Sets current Quat representing a rotation around an axis.
471
- * @public
472
- * @param {og.Vec3} axis - The axis of rotation.
473
- * @param {number} angle The angle in radians to rotate around the axis.
474
- * @returns {og.Quat} -
475
- */
476
- Quat.prototype.setFromAxisAngle = function (axis, angle) {
477
- var v = axis.normal();
478
- var half_angle = angle * 0.5;
479
- var sin_a = Math.sin(half_angle);
480
- this.set(v.x * sin_a, v.y * sin_a, v.z * sin_a, Math.cos(half_angle));
481
- return this;
482
- };
483
-
484
- /**
485
- * Returns axis and angle of the current Quat.
486
- * @public
487
- * @returns {Object} -
488
- */
489
- Quat.prototype.getAxisAngle = function () {
490
- var vl = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
491
- var axis, angle;
492
- if (vl > 0.0000001) {
493
- var ivl = 1.0 / vl;
494
- axis = new Vec3(x * ivl, y * ivl, z * ivl);
495
- if (this.w < 0) {
496
- angle = 2.0 * Math.atan2(-vl, -w); // -PI,0
497
- } else {
498
- angle = 2.0 * Math.atan2(vl, w); // 0,PI
499
- }
500
- } else {
501
- axis = new Vec3(0, 0, 0);
502
- angle = 0;
503
- }
504
- return { axis: axis, angle: angle };
505
- };
506
-
507
- /**
508
- * Sets current Quat by Euler's angles.
509
- * @public
510
- * @param {number} pitch - Pitch angle in degrees.
511
- * @param {number} yaw - Yaw angle in degrees.
512
- * @param {number} roll - Roll angle in degrees.
513
- * @returns {og.Quat} -
514
- */
515
- Quat.prototype.setFromEulerAngles = function (pitch, yaw, roll) {
516
- var ex = pitch * math.RADIANS_HALF,
517
- ey = yaw * math.RADIANS_HALF,
518
- ez = roll * math.RADIANS_HALF;
519
-
520
- var cr = Math.cos(ex),
521
- cp = Math.cos(ey),
522
- cy = Math.cos(ez);
523
-
524
- var sr = Math.sin(ex),
525
- sp = Math.sin(ey),
526
- sy = Math.sin(ez);
527
-
528
- var cpcy = cp * cy,
529
- spsy = sp * sy;
530
-
531
- this.w = cr * cpcy + sr * spsy;
532
- this.x = sr * cpcy - cr * spsy;
533
- this.y = cr * sp * cy + sr * cp * sy;
534
- this.z = cr * cp * sy - sr * sp * cy;
535
-
536
- return this.normalize();
537
- };
538
-
539
- /**
540
- * Returns Euler's angles of the current Quat.
541
- * @public
542
- * @returns {Object} -
543
- */
544
- Quat.prototype.getEulerAngles = function () {
545
-
546
- let x = this.x, y = this.y, z = this.z, w = this.w;
547
-
548
- let sqy = y * y;
549
-
550
- let roll = Math.atan2(2.0 * (w * x + y * z), 1.0 - 2.0 * (x * x + sqy));
551
-
552
- let a = w * y - z * x;
553
-
554
- if (a < -1.0) {
555
- a = -1.0;
556
- } else if (a > 1.0) {
557
- a = 1.0;
558
- }
559
- let pitch = Math.asin(2.0 * a);
560
-
561
- let yaw = Math.atan2(2.0 * (w * z + x * y), 1.0 - 2.0 * (sqy + z * z));
562
-
563
- return { roll, pitch, yaw };
564
- };
565
-
566
- /**
567
- * Computes a Quat from the provided 4x4 matrix instance.
568
- * @public
569
- * @param {og.Mat4} m - The rotation matrix.
570
- * @returns {og.Quat} -
571
- */
572
- Quat.prototype.setFromMatrix4 = function (m) {
573
- var tr, s, q = [];
574
- var i, j, k;
575
- m = m._m;
576
-
577
- var nxt = [1, 2, 0];
578
-
579
- tr = m[0] + m[5] + m[10];
580
-
581
- if (tr > 0.0) {
582
- s = Math.sqrt(tr + 1.0);
583
- this.w = s / 2.0;
584
- s = 0.5 / s;
585
- this.x = (m[6] - m[9]) * s;
586
- this.y = (m[8] - m[2]) * s;
587
- this.z = (m[1] - m[4]) * s;
588
- } else {
589
- i = 0;
590
- if (m[5] > m[0]) i = 1;
591
- if (m[10] > m[i * 5]) i = 2;
592
- j = nxt[i];
593
- k = nxt[j];
594
-
595
- s = Math.sqrt((m[i * 5] - (m[j * 5] + m[k * 5])) + 1.0);
596
-
597
- q[i] = s * 0.5;
598
-
599
- if (s !== 0.0) s = 0.5 / s;
600
-
601
- q[3] = (m[j * 4 + k] - m[k * 4 + j]) * s;
602
- q[j] = (m[i * 4 + j] + m[j * 4 + i]) * s;
603
- q[k] = (m[i * 4 + k] + m[k * 4 + i]) * s;
604
-
605
- this.x = q[0];
606
- this.y = q[1];
607
- this.z = q[2];
608
- this.w = q[3];
609
- }
610
- return this;
611
- };
612
-
613
- /**
614
- * Converts current Quat to the rotation 4x4 matrix.
615
- * @public
616
- * @returns {og.Mat4} -
617
- */
618
- Quat.prototype.getMat4 = function (out) {
619
- var xs = this.x + this.x;
620
- var ys = this.y + this.y;
621
- var zs = this.z + this.z;
622
- var wx = this.w * xs;
623
- var wy = this.w * ys;
624
- var wz = this.w * zs;
625
- var xx = this.x * xs;
626
- var xy = this.x * ys;
627
- var xz = this.x * zs;
628
- var yy = this.y * ys;
629
- var yz = this.y * zs;
630
- var zz = this.z * zs;
631
- var m = out || new Mat4();
632
- return m.set([1 - (yy + zz), xy - wz, xz + wy, 0, xy + wz, 1 - (xx + zz), yz - wx, 0, xz - wy, yz + wx, 1 - (xx + yy), 0, 0, 0, 0, 1]);
633
- };
634
-
635
- /**
636
- * Converts current Quat to the rotation 3x3 matrix.
637
- * @public
638
- * @returns {og.Mat3} -
639
- * @todo NOT TESTED
640
- */
641
- Quat.prototype.getMat3 = function () {
642
- var m = new Mat3();
643
- var mx = m._m;
644
- var c = this.x,
645
- d = this.y,
646
- e = this.z,
647
- g = this.w,
648
- f = c + c,
649
- h = d + d,
650
- i = e + e,
651
- j = c * f,
652
- k = c * h;
653
-
654
- c = c * i;
655
-
656
- var l = d * h;
657
-
658
- d = d * i;
659
- e = e * i;
660
- f = g * f;
661
- h = g * h;
662
- g = g * i;
663
-
664
- mx[0] = 1 - (l + e); mx[1] = k - g; mx[2] = c + h;
665
- mx[3] = k + g; mx[4] = 1 - (j + e); mx[5] = d - f;
666
- mx[6] = c - h; mx[7] = d + f; mx[8] = 1 - (j + l);
667
-
668
- return m;
669
- };
670
-
671
- /**
672
- * Returns quatrenion and vector production.
673
- * @public
674
- * @param {og.Vec3} v - 3d Vector.
675
- * @returns {og.Vec3} -
676
- */
677
- Quat.prototype.mulVec3 = function (v) {
678
-
679
- // t = 2 * cross(q.xyz, v)
680
- // v' = v + q.w * t + cross(q.xyz, t)
681
-
682
- var d = v.x,
683
- e = v.y,
684
- g = v.z;
685
-
686
- var b = this.x,
687
- f = this.y,
688
- h = this.z,
689
- a = this.w;
690
-
691
- var i = a * d + f * g - h * e,
692
- j = a * e + h * d - b * g,
693
- k = a * g + b * e - f * d;
694
-
695
- d = -b * d - f * e - h * g;
696
-
697
- return new Vec3(
698
- i * a + d * -b + j * -h - k * -f,
699
- j * a + d * -f + k * -b - i * -h,
700
- k * a + d * -h + i * -f - j * -b);
701
- };
702
-
703
- /**
704
- * Computes the product of two Quats.
705
- * @public
706
- * @param {og.Quat} q - Quat to multiply.
707
- * @returns {og.Quat} -
708
- */
709
- Quat.prototype.mul = function (q) {
710
- var d = this.x, e = this.y, g = this.z, a = this.w;
711
- var f = q.x, h = q.y, i = q.z, b = q.w;
712
- return new Quat(
713
- d * b + a * f + e * i - g * h,
714
- e * b + a * h + g * f - d * i,
715
- g * b + a * i + d * h - e * f,
716
- a * b - d * f - e * h - g * i);
717
- };
718
-
719
- /**
720
- * Computes the product of two Quats.
721
- * @public
722
- * @param {og.Quat} q - Quat to multiply.
723
- * @returns {og.Quat} -
724
- */
725
- Quat.prototype.mulA = function (q) {
726
- var d = this.x, e = this.y, g = this.z, a = this.w;
727
- var f = q.x, h = q.y, i = q.z, b = q.w;
728
- this.x = d * b + a * f + e * i - g * h;
729
- this.y = e * b + a * h + g * f - d * i;
730
- this.z = g * b + a * i + d * h - e * f;
731
- this.w = a * b - d * f - e * h - g * i;
732
- return this;
733
- };
734
-
735
- /**
736
- * Gets the conjugate of the Quat.
737
- * @public
738
- * @returns {og.Quat} -
739
- */
740
- Quat.prototype.conjugate = function () {
741
- return new Quat(-this.x, -this.y, -this.z, this.w);
742
- };
743
-
744
- /**
745
- * Computes the inverse of the Quat.
746
- * @public
747
- * @returns {og.Quat} -
748
- */
749
- Quat.prototype.inverse = function () {
750
- var n = 1 / this.magnitude2();
751
- return new Quat(-this.x * n, -this.y * n, -this.z * n, this.w * n);
752
- };
753
-
754
- /**
755
- * Computes a magnitude of the Quat.
756
- * @public
757
- * @returns {number} -
758
- */
759
- Quat.prototype.magnitude = function () {
760
- var b = this.x, c = this.y, d = this.z, a = this.w;
761
- return Math.sqrt(b * b + c * c + d * d + a * a);
762
- };
763
-
764
- /**
765
- * Computes a squared magnitude of the Quat.
766
- * @public
767
- * @returns {number} -
768
- */
769
- Quat.prototype.magnitude2 = function () {
770
- var b = this.x, c = this.y, d = this.z, a = this.w;
771
- return b * b + c * c + d * d + a * a;
772
- };
773
-
774
- /**
775
- * Computes the dot (scalar) product of two Quats.
776
- * @public
777
- * @param {og.Quat} q - Second quatrnion.
778
- * @returns {number} -
779
- */
780
- Quat.prototype.dot = function (q) {
781
- return this.x * q.x + this.y * q.y + this.z * q.z;
782
- };
783
-
784
- /**
785
- * Current Quat normalization.
786
- * @public
787
- * @returns {og.Quat} -
788
- */
789
- Quat.prototype.normalize = function () {
790
- var c = this.x, d = this.y, e = this.z, g = this.w,
791
- f = Math.sqrt(c * c + d * d + e * e + g * g);
792
- if (f === 0.0) {
793
- this.x = 0;
794
- this.y = 0;
795
- this.z = 0;
796
- this.w = 0;
797
- return this;
798
- }
799
- f = 1 / f;
800
- this.x = c * f;
801
- this.y = d * f;
802
- this.z = e * f;
803
- this.w = g * f;
804
- return this;
805
- };
806
-
807
- /**
808
- * Compares two Quats.
809
- * @public
810
- * @param {og.Quat} q - Second quatrnion.
811
- * @returns {Boolean} -
812
- */
813
- Quat.prototype.isEqual = function (q) {
814
- var matching = this.dot(q);
815
- if (Math.abs(matching - 1.0) < 0.001) {
816
- return true;
817
- }
818
- return false;
819
- };
820
-
821
- /**
822
- * Performs a spherical linear interpolation between two Quats.
823
- * @public
824
- * @param {og.Quat} b - The end rotation Quat.
825
- * @param {number} t - interpolation amount between the two Quats.
826
- * @returns {og.Quat} -
827
- */
828
- Quat.prototype.slerp = function (b, t) {
829
-
830
- var ax = this.x, ay = this.y, az = this.z, aw = this.w,
831
- bx = b.x, by = b.y, bz = b.z, bw = b.w;
832
-
833
- var omega, cosom, sinom, scale0, scale1;
834
-
835
- cosom = ax * bx + ay * by + az * bz + aw * bw;
836
-
837
- if (cosom < 0.0) {
838
- cosom = -cosom;
839
- bx = -bx;
840
- by = -by;
841
- bz = -bz;
842
- bw = -bw;
843
- }
844
-
845
- if ((1.0 - cosom) > 0.000001) {
846
- omega = Math.acos(cosom);
847
- sinom = Math.sin(omega);
848
- scale0 = Math.sin((1.0 - t) * omega) / sinom;
849
- scale1 = Math.sin(t * omega) / sinom;
850
- } else {
851
- scale0 = 1.0 - t;
852
- scale1 = t;
853
- }
854
-
855
- return new Quat(
856
- scale0 * ax + scale1 * bx,
857
- scale0 * ay + scale1 * by,
858
- scale0 * az + scale1 * bz,
859
- scale0 * aw + scale1 * bw
860
- );
861
- };
862
-
863
- /**
864
- * Returns a roll angle in radians.
865
- * @public
866
- * @param {Boolean} [reprojectAxis] -
867
- * @returns {Number} -
868
- */
869
- Quat.prototype.getRoll = function (reprojectAxis) {
870
- var x = this.x, y = this.y, z = this.z, w = this.w;
871
- if (reprojectAxis) {
872
- var fTy = 2.0 * y;
873
- var fTz = 2.0 * z;
874
- var fTwz = fTz * w;
875
- var fTxy = fTy * x;
876
- var fTyy = fTy * y;
877
- var fTzz = fTz * z;
878
- return Math.atan2(fTxy + fTwz, 1.0 - (fTyy + fTzz));
879
- } else {
880
- return Math.atan2(2 * (x * y + w * z), w * w + x * x - y * y - z * z);
881
- }
882
- };
883
-
884
- /**
885
- * Returns a pitch angle in radians.
886
- * @public
887
- * @param {Boolean} [reprojectAxis] -
888
- * @returns {number} -
889
- */
890
- Quat.prototype.getPitch = function (reprojectAxis) {
891
- var x = this.x, y = this.y, z = this.z, w = this.w;
892
- if (reprojectAxis) {
893
- var fTx = 2.0 * x;
894
- var fTz = 2.0 * z;
895
- var fTwx = fTx * w;
896
- var fTxx = fTx * x;
897
- var fTyz = fTz * y;
898
- var fTzz = fTz * z;
899
- return Math.atan2(fTyz + fTwx, 1.0 - (fTxx + fTzz));
900
- } else {
901
- return Math.atan2(2 * (y * z + w * x), w * w - x * x - y * y + z * z);
902
- }
903
- };
904
-
905
- /**
906
- * Returns a yaw angle in radians.
907
- * @public
908
- * @param {Boolean} [reprojectAxis] -
909
- * @returns {number} -
910
- */
911
- Quat.prototype.getYaw = function (reprojectAxis) {
912
- var x = this.x, y = this.y, z = this.z, w = this.w;
913
- if (reprojectAxis) {
914
- var fTx = 2.0 * x;
915
- var fTy = 2.0 * y;
916
- var fTz = 2.0 * z;
917
- var fTwy = fTy * w;
918
- var fTxx = fTx * x;
919
- var fTxz = fTz * x;
920
- var fTyy = fTy * y;
921
- return Math.atan2(fTxz + fTwy, 1.0 - (fTxx + fTyy));
922
- } else {
923
- return Math.asin(-2 * (x * z - w * y));
924
- }
925
- };
926
-
927
- export { Quat };
1
+ "use strict";
2
+
3
+ import * as math from "../math.js";
4
+ import { Mat3 } from "./Mat3.js";
5
+ import { Mat4 } from "./Mat4.js";
6
+ import { Vec3 } from "./Vec3.js";
7
+
8
+ /**
9
+ * A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
10
+ * @constructor
11
+ * @param {Number} [x=0.0] The X component.
12
+ * @param {Number} [y=0.0] The Y component.
13
+ * @param {Number} [z=0.0] The Z component.
14
+ * @param {Number} [w=0.0] The W component.
15
+ */
16
+ export class Quat {
17
+ /**
18
+ * @param {Number} [x=0.0] The X component.
19
+ * @param {Number} [y=0.0] The Y component.
20
+ * @param {Number} [z=0.0] The Z component.
21
+ * @param {Number} [w=0.0] The W component.
22
+ */
23
+ constructor(x, y, z, w) {
24
+ /**
25
+ * The X component.
26
+ * @public
27
+ * @type {Number}
28
+ * @default 0.0
29
+ */
30
+ this.x = x || 0.0;
31
+
32
+ /**
33
+ * The Y component.
34
+ * @public
35
+ * @type {Number}
36
+ * @default 0.0
37
+ */
38
+ this.y = y || 0.0;
39
+
40
+ /**
41
+ * The Z component.
42
+ * @public
43
+ * @type {Number}
44
+ * @default 0.0
45
+ */
46
+ this.z = z || 0.0;
47
+
48
+ /**
49
+ * The W component.
50
+ * @public
51
+ * @type {Number}
52
+ * @default 0.0
53
+ */
54
+ this.w = w || 0.0;
55
+ }
56
+
57
+ /**
58
+ * Identity Quat.
59
+ * @const
60
+ * @type {Quat}
61
+ */
62
+ static get IDENTITY() {
63
+ return new Quat(0.0, 0.0, 0.0, 1.0);
64
+ }
65
+
66
+ /**
67
+ * Returns a Quat represents rotation around X axis.
68
+ * @static
69
+ * @param {number} a - The angle in radians to rotate around the axis.
70
+ * @returns {Quat} -
71
+ */
72
+ static xRotation(a) {
73
+ a *= 0.5;
74
+ return new Quat(Math.sin(a), 0.0, 0.0, Math.cos(a));
75
+ }
76
+
77
+ /**
78
+ * Returns a Quat represents rotation around Y axis.
79
+ * @static
80
+ * @param {number} a - The angle in radians to rotate around the axis.
81
+ * @returns {Quat} -
82
+ */
83
+ static yRotation(a) {
84
+ a *= 0.5;
85
+ return new Quat(0.0, Math.sin(a), 0.0, Math.cos(a));
86
+ }
87
+
88
+ /**
89
+ * Returns a Quat represents rotation around Z axis.
90
+ * @static
91
+ * @param {number} a - The angle in radians to rotate around the axis.
92
+ * @returns {Quat} -
93
+ */
94
+ static zRotation(a) {
95
+ a *= 0.5;
96
+ return new Quat(0.0, 0.0, Math.sin(a), Math.cos(a));
97
+ }
98
+
99
+ /**
100
+ * Computes a Quat representing a rotation around an axis.
101
+ * @static
102
+ * @param {Vec3} axis - The axis of rotation.
103
+ * @param {number} [angle=0.0] The angle in radians to rotate around the axis.
104
+ * @returns {Quat} -
105
+ */
106
+ static axisAngleToQuat(axis, angle) {
107
+ angle = angle || 0.0;
108
+ var v = axis.normal();
109
+ var half_angle = angle * 0.5;
110
+ var sin_a = Math.sin(half_angle);
111
+ return new Quat(v.x * sin_a, v.y * sin_a, v.z * sin_a, Math.cos(half_angle));
112
+ }
113
+
114
+ /**
115
+ * Computes a rotation from the given heading and up vector.
116
+ * @static
117
+ * @param {Vec3} forward - Heading target coordinates.
118
+ * @param {Vec3} up - Up vector.
119
+ * @returns {Quat} -
120
+ */
121
+ static getLookRotation(forward, up) {
122
+ var f = forward.normal().negate();
123
+ var s = up.cross(f).normalize();
124
+ var u = f.cross(s);
125
+
126
+ var z = 1.0 + s.x + u.y + f.z;
127
+
128
+ if (z > 0.000001) {
129
+ let fd = 1.0 / (2.0 * Math.sqrt(z));
130
+ return new Quat((f.y - u.z) * fd, (s.z - f.x) * fd, (u.x - s.y) * fd, 0.25 / fd);
131
+ }
132
+
133
+ if (s.x > u.y && s.x > f.z) {
134
+ let fd = 1.0 / (2.0 * Math.sqrt(1.0 + s.x - u.y - f.z));
135
+ return new Quat(0.25 / fd, (u.x + s.y) * fd, (s.z + f.x) * fd, (f.y - u.z) * fd);
136
+ }
137
+
138
+ if (u.y > f.z) {
139
+ let fd = 1.0 / (2.0 * Math.sqrt(1.0 + u.y - s.x - f.z));
140
+ return new Quat((u.x + s.y) * fd, 0.25 / fd, (f.y + u.z) * fd, (s.z - f.x) * fd);
141
+ }
142
+
143
+ let fd = 1.0 / (2.0 * Math.sqrt(1.0 + f.z - s.x - u.y));
144
+ return new Quat((s.z + f.x) * fd, (f.y + u.z) * fd, 0.25 / fd, (u.x - s.y) * fd);
145
+ }
146
+
147
+ /**
148
+ * Computes a Quat from from source point heading to the destination point.
149
+ * @static
150
+ * @param {Vec3} sourcePoint - Source coordinate.
151
+ * @param {Vec3} destPoint - Destination coordinate.
152
+ * @returns {Quat} -
153
+ */
154
+ static getLookAtSourceDest(sourcePoint, destPoint) {
155
+ var forwardVector = destPoint.subA(sourcePoint).normalize();
156
+ var dot = Vec3.FORWARD.dot(forwardVector);
157
+ if (Math.abs(dot - -1.0) < 0.000001) {
158
+ return Quat.axisAngleToQuat(Vec3.UP, Math.PI);
159
+ }
160
+ if (Math.abs(dot - 1.0) < 0.000001) {
161
+ return new Quat(0.0, 0.0, 0.0, 1.0);
162
+ }
163
+ var rotAngle = Math.acos(dot);
164
+ var rotAxis = Vec3.FORWARD.cross(forwardVector).normalize();
165
+ return Quat.axisAngleToQuat(rotAxis, rotAngle);
166
+ }
167
+
168
+ /**
169
+ * Compute rotation between two vectors.
170
+ * @static
171
+ * @param {Vec3} u - First vector.
172
+ * @param {Vec3} v - Second vector.
173
+ * @returns {Quat} -
174
+ */
175
+ static getRotationBetweenVectors(u, v) {
176
+ var w = u.cross(v);
177
+ var q = new Quat(w.x, w.y, w.z, 1.0 + u.dot(v));
178
+ return q.normalize();
179
+ }
180
+
181
+ /**
182
+ * Compute rotation between two vectors.
183
+ * @static
184
+ * @param {Vec3} u - First vector.
185
+ * @param {Vec3} v - Second vector.
186
+ * @param {Quat} res
187
+ * @returns {Quat} -
188
+ */
189
+ static getRotationBetweenVectorsRes(u, v, res) {
190
+ var w = u.cross(v);
191
+ res.set(w.x, w.y, w.z, 1.0 + u.dot(v));
192
+ return res.normalize();
193
+ }
194
+
195
+ /**
196
+ * Compute rotation between two vectors with around vector up
197
+ * for exactly opposite vectors. If vectors exaclty in the same
198
+ * direction than returns identity Quat.
199
+ * @static
200
+ * @param {Vec3} source - First vector.
201
+ * @param {Vec3} dest - Second vector.
202
+ * @param {Vec3} up - Up vector.
203
+ * @returns {Quat} -
204
+ */
205
+ static getRotationBetweenVectorsUp(source, dest, up) {
206
+ var dot = source.dot(dest);
207
+ if (Math.abs(dot + 1.0) < 0.000001) {
208
+ // vector source and dest point exactly in the opposite direction,
209
+ // so it is a 180 degrees turn around the up-axis
210
+ return Quat.axisAngleToQuat(up, Math.PI);
211
+ }
212
+ if (Math.abs(dot - 1.0) < 0.000001) {
213
+ // vector source and dest point exactly in the same direction
214
+ // so we return the identity Quat
215
+ return new Quat(0, 0, 0, 1);
216
+ }
217
+ var rotAngle = Math.acos(dot);
218
+ var rotAxis = source.cross(dest).normalize();
219
+ return Quat.axisAngleToQuat(rotAxis, rotAngle);
220
+ }
221
+
222
+ /**
223
+ * Returns true if the components are zero.
224
+ * @public
225
+ * @param {Quat} q - Quat to subtract.
226
+ * @returns {Quat} -
227
+ */
228
+ isZero() {
229
+ return this.x === 0.0 && this.y === 0.0 && this.z === 0.0 && this.w === 0.0;
230
+ }
231
+
232
+ /**
233
+ * Clear Quat. Sets zeroes.
234
+ * @public
235
+ * @returns {Quat} -
236
+ */
237
+ clear() {
238
+ this.x = this.y = this.z = this.w = 0;
239
+ return this;
240
+ }
241
+
242
+ /**
243
+ * Sets Quat values.
244
+ * @public
245
+ * @param {Number} [x=0.0] The X component.
246
+ * @param {Number} [y=0.0] The Y component.
247
+ * @param {Number} [z=0.0] The Z component.
248
+ * @param {Number} [w=0.0] The W component.
249
+ * @returns {Quat} -
250
+ */
251
+ set(x, y, z, w) {
252
+ this.x = x;
253
+ this.y = y;
254
+ this.z = z;
255
+ this.w = w;
256
+ return this;
257
+ }
258
+
259
+ /**
260
+ * Copy Quat values.
261
+ * @public
262
+ * @param {Quat} q - Copy Quat.
263
+ * @returns {Quat} -
264
+ */
265
+ copy(q) {
266
+ this.x = q.x;
267
+ this.y = q.y;
268
+ this.z = q.z;
269
+ this.w = q.w;
270
+ return this;
271
+ }
272
+
273
+ /**
274
+ * Set current Quat instance to identity Quat.
275
+ * @public
276
+ * @returns {Quat} -
277
+ */
278
+ setIdentity() {
279
+ this.x = 0.0;
280
+ this.y = 0.0;
281
+ this.z = 0.0;
282
+ this.w = 1.0;
283
+ return this;
284
+ }
285
+
286
+ /**
287
+ * Duplicates a Quat instance.
288
+ * @public
289
+ * @returns {Quat} -
290
+ */
291
+ clone() {
292
+ return new Quat(this.x, this.y, this.z, this.w);
293
+ }
294
+
295
+ /**
296
+ * Computes the componentwise sum of two Quats.
297
+ * @public
298
+ * @param {Quat} q - Quat to add.
299
+ * @returns {Quat} -
300
+ */
301
+ add(q) {
302
+ return new Quat(this.x + q.x, this.y + q.y, this.z + q.z, this.w + q.w);
303
+ }
304
+
305
+ /**
306
+ * Computes the componentwise difference of two Quats.
307
+ * @public
308
+ * @param {Quat} q - Quat to subtract.
309
+ * @returns {Quat} -
310
+ */
311
+ sub(q) {
312
+ return new Quat(this.x - q.x, this.y - q.y, this.z - q.z, this.w - q.w);
313
+ }
314
+
315
+ /**
316
+ * Multiplies the provided Quat componentwise by the provided scalar.
317
+ * @public
318
+ * @param {Number} scale - The scalar to multiply with.
319
+ * @returns {Quat} -
320
+ */
321
+ scaleTo(scale) {
322
+ return new Quat(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
323
+ }
324
+
325
+ /**
326
+ * Multiplies the provided Quat componentwise.
327
+ * @public
328
+ * @param {Number} scale - The scalar to multiply with.
329
+ * @returns {Quat} -
330
+ */
331
+ scale(scale) {
332
+ this.x *= scale;
333
+ this.y *= scale;
334
+ this.z *= scale;
335
+ this.w *= scale;
336
+ return this;
337
+ }
338
+
339
+ /**
340
+ * Converts Quat values to array.
341
+ * @public
342
+ * @returns {Array.<number>} - (exactly 4 entries)
343
+ */
344
+ toVec() {
345
+ return [this.x, this.y, this.z, this.w];
346
+ }
347
+
348
+ /**
349
+ * Sets current quaternion by spherical coordinates.
350
+ * @public
351
+ * @param {number} lat - Latitude.
352
+ * @param {number} lon - Longitude.
353
+ * @param {number} angle - Angle in radians.
354
+ * @returns {Quat} -
355
+ */
356
+ setFromSphericalCoords(lat, lon, angle) {
357
+ var sin_a = Math.sin(angle / 2);
358
+ var cos_a = Math.cos(angle / 2);
359
+ var sin_lat = Math.sin(lat);
360
+ var cos_lat = Math.cos(lat);
361
+ var sin_long = Math.sin(lon);
362
+ var cos_long = Math.cos(lon);
363
+ this.x = sin_a * cos_lat * sin_long;
364
+ this.y = sin_a * sin_lat;
365
+ this.z = sin_a * sin_lat * cos_long;
366
+ this.w = cos_a;
367
+ return this;
368
+ }
369
+
370
+ /**
371
+ * Sets rotation with the given heading and up vectors.
372
+ * @static
373
+ * @param {Vec3} forward - Heading target coordinates.
374
+ * @param {Vec3} up - Up vector.
375
+ * @returns {Quat} -
376
+ */
377
+ setLookRotation(forward, up) {
378
+ var f = forward.normal().negate();
379
+ var s = up.cross(f).normalize();
380
+ var u = f.cross(s);
381
+
382
+ var z = 1.0 + s.x + u.y + f.z;
383
+
384
+ if (z > 0.000001) {
385
+ let fd = 1.0 / (2.0 * Math.sqrt(z));
386
+ this.x = (f.y - u.z) * fd;
387
+ this.y = (s.z - f.x) * fd;
388
+ this.z = (u.x - s.y) * fd;
389
+ this.w = 0.25 / fd;
390
+ } else if (s.x > u.y && s.x > f.z) {
391
+ let fd = 1.0 / (2.0 * Math.sqrt(1.0 + s.x - u.y - f.z));
392
+ this.x = 0.25 / fd;
393
+ this.y = (u.x + s.y) * fd;
394
+ this.z = (s.z + f.x) * fd;
395
+ this.w = (f.y - u.z) * fd;
396
+ } else if (u.y > f.z) {
397
+ let fd = 1.0 / (2.0 * Math.sqrt(1.0 + u.y - s.x - f.z));
398
+ this.x = (u.x + s.y) * fd;
399
+ this.y = 0.25 / fd;
400
+ this.z = (f.y + u.z) * fd;
401
+ this.w = (s.z - f.x) * fd;
402
+ } else {
403
+ let fd = 1.0 / (2.0 * Math.sqrt(1.0 + f.z - s.x - u.y));
404
+ this.x = (s.z + f.x) * fd;
405
+ this.y = (f.y + u.z) * fd;
406
+ this.z = 0.25 / fd;
407
+ this.w = (u.x - s.y) * fd;
408
+ }
409
+
410
+ return this;
411
+ }
412
+
413
+ /**
414
+ * Gets spherical coordinates.
415
+ * @public
416
+ * @returns {Object} Returns object with latitude, longitude and alpha.
417
+ */
418
+ toSphericalCoords() {
419
+ var cos_a = this.w;
420
+ var sin_a = Math.sqrt(1.0 - cos_a * cos_a);
421
+ // var angle = Math.acos(cos_a) * 2;
422
+ if (Math.abs(sin_a) < 0.0005) {
423
+ sin_a = 1;
424
+ }
425
+ var tx = this.x / sin_a;
426
+ var ty = this.y / sin_a;
427
+ var tz = this.z / sin_a;
428
+
429
+ var lon,
430
+ lat = -Math.asin(ty);
431
+ if (tx * tx + tz * tz < 0.0005) {
432
+ lon = 0;
433
+ } else {
434
+ lon = Math.atan2(tx, tz);
435
+ }
436
+ if (lon < 0) {
437
+ lon += 360.0;
438
+ }
439
+
440
+ return { lat: lat, lon: lon, alpha: Math.acos(cos_a) };
441
+ }
442
+
443
+ /**
444
+ * Sets current Quat representing a rotation around an axis.
445
+ * @public
446
+ * @param {Vec3} axis - The axis of rotation.
447
+ * @param {number} angle The angle in radians to rotate around the axis.
448
+ * @returns {Quat} -
449
+ */
450
+ setFromAxisAngle(axis, angle) {
451
+ var v = axis.normal();
452
+ var half_angle = angle * 0.5;
453
+ var sin_a = Math.sin(half_angle);
454
+ this.set(v.x * sin_a, v.y * sin_a, v.z * sin_a, Math.cos(half_angle));
455
+ return this;
456
+ }
457
+
458
+ /**
459
+ * Returns axis and angle of the current Quat.
460
+ * @public
461
+ * @returns {Object} -
462
+ */
463
+ getAxisAngle() {
464
+ let x = this.x,
465
+ y = this.y,
466
+ z = this.z,
467
+ w = this.w;
468
+ var vl = Math.sqrt(x * x + y * y + z * z);
469
+ var axis, angle;
470
+ if (vl > 0.0000001) {
471
+ var ivl = 1.0 / vl;
472
+ axis = new Vec3(x * ivl, y * ivl, z * ivl);
473
+ if (w < 0) {
474
+ angle = 2.0 * Math.atan2(-vl, -w); // -PI,0
475
+ } else {
476
+ angle = 2.0 * Math.atan2(vl, w); // 0,PI
477
+ }
478
+ } else {
479
+ axis = new Vec3(0, 0, 0);
480
+ angle = 0;
481
+ }
482
+ return { axis: axis, angle: angle };
483
+ }
484
+
485
+ /**
486
+ * Sets current Quat by Euler's angles.
487
+ * @public
488
+ * @param {number} pitch - Pitch angle in degrees.
489
+ * @param {number} yaw - Yaw angle in degrees.
490
+ * @param {number} roll - Roll angle in degrees.
491
+ * @returns {Quat} -
492
+ */
493
+ setFromEulerAngles(pitch, yaw, roll) {
494
+ var ex = pitch * math.RADIANS_HALF,
495
+ ey = yaw * math.RADIANS_HALF,
496
+ ez = roll * math.RADIANS_HALF;
497
+
498
+ var cr = Math.cos(ex),
499
+ cp = Math.cos(ey),
500
+ cy = Math.cos(ez);
501
+
502
+ var sr = Math.sin(ex),
503
+ sp = Math.sin(ey),
504
+ sy = Math.sin(ez);
505
+
506
+ var cpcy = cp * cy,
507
+ spsy = sp * sy;
508
+
509
+ this.w = cr * cpcy + sr * spsy;
510
+ this.x = sr * cpcy - cr * spsy;
511
+ this.y = cr * sp * cy + sr * cp * sy;
512
+ this.z = cr * cp * sy - sr * sp * cy;
513
+
514
+ return this.normalize();
515
+ }
516
+
517
+ /**
518
+ * Returns Euler's angles of the current Quat.
519
+ * @public
520
+ * @returns {Object} -
521
+ */
522
+ getEulerAngles() {
523
+ let x = this.x,
524
+ y = this.y,
525
+ z = this.z,
526
+ w = this.w;
527
+
528
+ let sqy = y * y;
529
+
530
+ let roll = Math.atan2(2.0 * (w * x + y * z), 1.0 - 2.0 * (x * x + sqy));
531
+
532
+ let a = w * y - z * x;
533
+
534
+ if (a < -1.0) {
535
+ a = -1.0;
536
+ } else if (a > 1.0) {
537
+ a = 1.0;
538
+ }
539
+ let pitch = Math.asin(2.0 * a);
540
+
541
+ let yaw = Math.atan2(2.0 * (w * z + x * y), 1.0 - 2.0 * (sqy + z * z));
542
+
543
+ return { roll, pitch, yaw };
544
+ }
545
+
546
+ /**
547
+ * Computes a Quat from the provided 4x4 matrix instance.
548
+ * @public
549
+ * @param {Mat4} m - The rotation matrix.
550
+ * @returns {Quat} -
551
+ */
552
+ setFromMatrix4(m) {
553
+ var tr,
554
+ s,
555
+ q = [];
556
+ var i, j, k;
557
+ m = m._m;
558
+
559
+ var nxt = [1, 2, 0];
560
+
561
+ tr = m[0] + m[5] + m[10];
562
+
563
+ if (tr > 0.0) {
564
+ s = Math.sqrt(tr + 1.0);
565
+ this.w = s / 2.0;
566
+ s = 0.5 / s;
567
+ this.x = (m[6] - m[9]) * s;
568
+ this.y = (m[8] - m[2]) * s;
569
+ this.z = (m[1] - m[4]) * s;
570
+ } else {
571
+ i = 0;
572
+ if (m[5] > m[0]) i = 1;
573
+ if (m[10] > m[i * 5]) i = 2;
574
+ j = nxt[i];
575
+ k = nxt[j];
576
+
577
+ s = Math.sqrt(m[i * 5] - (m[j * 5] + m[k * 5]) + 1.0);
578
+
579
+ q[i] = s * 0.5;
580
+
581
+ if (s !== 0.0) s = 0.5 / s;
582
+
583
+ q[3] = (m[j * 4 + k] - m[k * 4 + j]) * s;
584
+ q[j] = (m[i * 4 + j] + m[j * 4 + i]) * s;
585
+ q[k] = (m[i * 4 + k] + m[k * 4 + i]) * s;
586
+
587
+ this.x = q[0];
588
+ this.y = q[1];
589
+ this.z = q[2];
590
+ this.w = q[3];
591
+ }
592
+ return this;
593
+ }
594
+
595
+ /**
596
+ * Converts current Quat to the rotation 4x4 matrix.
597
+ * @public
598
+ * @returns {Mat4} -
599
+ */
600
+ getMat4(out) {
601
+ var xs = this.x + this.x;
602
+ var ys = this.y + this.y;
603
+ var zs = this.z + this.z;
604
+ var wx = this.w * xs;
605
+ var wy = this.w * ys;
606
+ var wz = this.w * zs;
607
+ var xx = this.x * xs;
608
+ var xy = this.x * ys;
609
+ var xz = this.x * zs;
610
+ var yy = this.y * ys;
611
+ var yz = this.y * zs;
612
+ var zz = this.z * zs;
613
+ var m = out || new Mat4();
614
+ return m.set([
615
+ 1 - (yy + zz),
616
+ xy - wz,
617
+ xz + wy,
618
+ 0,
619
+ xy + wz,
620
+ 1 - (xx + zz),
621
+ yz - wx,
622
+ 0,
623
+ xz - wy,
624
+ yz + wx,
625
+ 1 - (xx + yy),
626
+ 0,
627
+ 0,
628
+ 0,
629
+ 0,
630
+ 1
631
+ ]);
632
+ }
633
+
634
+ /**
635
+ * Converts current Quat to the rotation 3x3 matrix.
636
+ * @public
637
+ * @returns {Mat3} -
638
+ * @todo NOT TESTED
639
+ */
640
+ getMat3() {
641
+ var m = new Mat3();
642
+ var mx = m._m;
643
+ var c = this.x,
644
+ d = this.y,
645
+ e = this.z,
646
+ g = this.w,
647
+ f = c + c,
648
+ h = d + d,
649
+ i = e + e,
650
+ j = c * f,
651
+ k = c * h;
652
+
653
+ c = c * i;
654
+
655
+ var l = d * h;
656
+
657
+ d = d * i;
658
+ e = e * i;
659
+ f = g * f;
660
+ h = g * h;
661
+ g = g * i;
662
+
663
+ mx[0] = 1 - (l + e);
664
+ mx[1] = k - g;
665
+ mx[2] = c + h;
666
+ mx[3] = k + g;
667
+ mx[4] = 1 - (j + e);
668
+ mx[5] = d - f;
669
+ mx[6] = c - h;
670
+ mx[7] = d + f;
671
+ mx[8] = 1 - (j + l);
672
+
673
+ return m;
674
+ }
675
+
676
+ /**
677
+ * Returns quatrenion and vector production.
678
+ * @public
679
+ * @param {Vec3} v - 3d Vector.
680
+ * @returns {Vec3} -
681
+ */
682
+ mulVec3(v) {
683
+ // t = 2 * cross(q.xyz, v)
684
+ // v' = v + q.w * t + cross(q.xyz, t)
685
+
686
+ var d = v.x,
687
+ e = v.y,
688
+ g = v.z;
689
+
690
+ var b = this.x,
691
+ f = this.y,
692
+ h = this.z,
693
+ a = this.w;
694
+
695
+ var i = a * d + f * g - h * e,
696
+ j = a * e + h * d - b * g,
697
+ k = a * g + b * e - f * d;
698
+
699
+ d = -b * d - f * e - h * g;
700
+
701
+ return new Vec3(
702
+ i * a + d * -b + j * -h - k * -f,
703
+ j * a + d * -f + k * -b - i * -h,
704
+ k * a + d * -h + i * -f - j * -b
705
+ );
706
+ }
707
+
708
+ /**
709
+ * Computes the product of two Quats.
710
+ * @public
711
+ * @param {Quat} q - Quat to multiply.
712
+ * @returns {Quat} -
713
+ */
714
+ mul(q) {
715
+ var d = this.x,
716
+ e = this.y,
717
+ g = this.z,
718
+ a = this.w;
719
+ var f = q.x,
720
+ h = q.y,
721
+ i = q.z,
722
+ b = q.w;
723
+ return new Quat(
724
+ d * b + a * f + e * i - g * h,
725
+ e * b + a * h + g * f - d * i,
726
+ g * b + a * i + d * h - e * f,
727
+ a * b - d * f - e * h - g * i
728
+ );
729
+ }
730
+
731
+ /**
732
+ * Computes the product of two Quats.
733
+ * @public
734
+ * @param {Quat} q - Quat to multiply.
735
+ * @returns {Quat} -
736
+ */
737
+ mulA(q) {
738
+ var d = this.x,
739
+ e = this.y,
740
+ g = this.z,
741
+ a = this.w;
742
+ var f = q.x,
743
+ h = q.y,
744
+ i = q.z,
745
+ b = q.w;
746
+ this.x = d * b + a * f + e * i - g * h;
747
+ this.y = e * b + a * h + g * f - d * i;
748
+ this.z = g * b + a * i + d * h - e * f;
749
+ this.w = a * b - d * f - e * h - g * i;
750
+ return this;
751
+ }
752
+
753
+ /**
754
+ * Gets the conjugate of the Quat.
755
+ * @public
756
+ * @returns {Quat} -
757
+ */
758
+ conjugate() {
759
+ return new Quat(-this.x, -this.y, -this.z, this.w);
760
+ }
761
+
762
+ /**
763
+ * Computes the inverse of the Quat.
764
+ * @public
765
+ * @returns {Quat} -
766
+ */
767
+ inverse() {
768
+ var n = 1 / this.magnitude2();
769
+ return new Quat(-this.x * n, -this.y * n, -this.z * n, this.w * n);
770
+ }
771
+
772
+ /**
773
+ * Computes a magnitude of the Quat.
774
+ * @public
775
+ * @returns {number} -
776
+ */
777
+ magnitude() {
778
+ var b = this.x,
779
+ c = this.y,
780
+ d = this.z,
781
+ a = this.w;
782
+ return Math.sqrt(b * b + c * c + d * d + a * a);
783
+ }
784
+
785
+ /**
786
+ * Computes a squared magnitude of the Quat.
787
+ * @public
788
+ * @returns {number} -
789
+ */
790
+ magnitude2() {
791
+ var b = this.x,
792
+ c = this.y,
793
+ d = this.z,
794
+ a = this.w;
795
+ return b * b + c * c + d * d + a * a;
796
+ }
797
+
798
+ /**
799
+ * Computes the dot (scalar) product of two Quats.
800
+ * @public
801
+ * @param {Quat} q - Second quatrnion.
802
+ * @returns {number} -
803
+ */
804
+ dot(q) {
805
+ return this.x * q.x + this.y * q.y + this.z * q.z;
806
+ }
807
+
808
+ /**
809
+ * Current Quat normalization.
810
+ * @public
811
+ * @returns {Quat} -
812
+ */
813
+ normalize() {
814
+ var c = this.x,
815
+ d = this.y,
816
+ e = this.z,
817
+ g = this.w,
818
+ f = Math.sqrt(c * c + d * d + e * e + g * g);
819
+ if (f === 0.0) {
820
+ this.x = 0;
821
+ this.y = 0;
822
+ this.z = 0;
823
+ this.w = 0;
824
+ return this;
825
+ }
826
+ f = 1 / f;
827
+ this.x = c * f;
828
+ this.y = d * f;
829
+ this.z = e * f;
830
+ this.w = g * f;
831
+ return this;
832
+ }
833
+
834
+ /**
835
+ * Compares two Quats.
836
+ * @public
837
+ * @param {Quat} q - Second quatrnion.
838
+ * @returns {Boolean} -
839
+ */
840
+ isEqual(q) {
841
+ var matching = this.dot(q);
842
+ if (Math.abs(matching - 1.0) < 0.001) {
843
+ return true;
844
+ }
845
+ return false;
846
+ }
847
+
848
+ /**
849
+ * Performs a spherical linear interpolation between two Quats.
850
+ * @public
851
+ * @param {Quat} b - The end rotation Quat.
852
+ * @param {number} t - interpolation amount between the two Quats.
853
+ * @returns {Quat} -
854
+ */
855
+ slerp(b, t) {
856
+ var ax = this.x,
857
+ ay = this.y,
858
+ az = this.z,
859
+ aw = this.w,
860
+ bx = b.x,
861
+ by = b.y,
862
+ bz = b.z,
863
+ bw = b.w;
864
+
865
+ var omega, cosom, sinom, scale0, scale1;
866
+
867
+ cosom = ax * bx + ay * by + az * bz + aw * bw;
868
+
869
+ if (cosom < 0.0) {
870
+ cosom = -cosom;
871
+ bx = -bx;
872
+ by = -by;
873
+ bz = -bz;
874
+ bw = -bw;
875
+ }
876
+
877
+ if (1.0 - cosom > 0.000001) {
878
+ omega = Math.acos(cosom);
879
+ sinom = Math.sin(omega);
880
+ scale0 = Math.sin((1.0 - t) * omega) / sinom;
881
+ scale1 = Math.sin(t * omega) / sinom;
882
+ } else {
883
+ scale0 = 1.0 - t;
884
+ scale1 = t;
885
+ }
886
+
887
+ return new Quat(
888
+ scale0 * ax + scale1 * bx,
889
+ scale0 * ay + scale1 * by,
890
+ scale0 * az + scale1 * bz,
891
+ scale0 * aw + scale1 * bw
892
+ );
893
+ }
894
+
895
+ /**
896
+ * Returns a roll angle in radians.
897
+ * @public
898
+ * @param {Boolean} [reprojectAxis] -
899
+ * @returns {Number} -
900
+ */
901
+ getRoll(reprojectAxis) {
902
+ var x = this.x,
903
+ y = this.y,
904
+ z = this.z,
905
+ w = this.w;
906
+ if (reprojectAxis) {
907
+ var fTy = 2.0 * y;
908
+ var fTz = 2.0 * z;
909
+ var fTwz = fTz * w;
910
+ var fTxy = fTy * x;
911
+ var fTyy = fTy * y;
912
+ var fTzz = fTz * z;
913
+ return Math.atan2(fTxy + fTwz, 1.0 - (fTyy + fTzz));
914
+ } else {
915
+ return Math.atan2(2 * (x * y + w * z), w * w + x * x - y * y - z * z);
916
+ }
917
+ }
918
+
919
+ /**
920
+ * Returns a pitch angle in radians.
921
+ * @public
922
+ * @param {Boolean} [reprojectAxis] -
923
+ * @returns {number} -
924
+ */
925
+ getPitch(reprojectAxis) {
926
+ var x = this.x,
927
+ y = this.y,
928
+ z = this.z,
929
+ w = this.w;
930
+ if (reprojectAxis) {
931
+ var fTx = 2.0 * x;
932
+ var fTz = 2.0 * z;
933
+ var fTwx = fTx * w;
934
+ var fTxx = fTx * x;
935
+ var fTyz = fTz * y;
936
+ var fTzz = fTz * z;
937
+ return Math.atan2(fTyz + fTwx, 1.0 - (fTxx + fTzz));
938
+ } else {
939
+ return Math.atan2(2 * (y * z + w * x), w * w - x * x - y * y + z * z);
940
+ }
941
+ }
942
+
943
+ /**
944
+ * Returns a yaw angle in radians.
945
+ * @public
946
+ * @param {Boolean} [reprojectAxis] -
947
+ * @returns {number} -
948
+ */
949
+ getYaw(reprojectAxis) {
950
+ var x = this.x,
951
+ y = this.y,
952
+ z = this.z,
953
+ w = this.w;
954
+ if (reprojectAxis) {
955
+ var fTx = 2.0 * x;
956
+ var fTy = 2.0 * y;
957
+ var fTz = 2.0 * z;
958
+ var fTwy = fTy * w;
959
+ var fTxx = fTx * x;
960
+ var fTxz = fTz * x;
961
+ var fTyy = fTy * y;
962
+ return Math.atan2(fTxz + fTwy, 1.0 - (fTxx + fTyy));
963
+ } else {
964
+ return Math.asin(-2 * (x * z - w * y));
965
+ }
966
+ }
967
+ }
968
+
969
+ /**
970
+ * Creates Quat instance.
971
+ * @function
972
+ * @param {Number} [x=0.0] The X component.
973
+ * @param {Number} [y=0.0] The Y component.
974
+ * @param {Number} [z=0.0] The Z component.
975
+ * @param {Number} [w=0.0] The W component.
976
+ * @returns {Quat} -
977
+ */
978
+ export function quat(x, y, z, w) {
979
+ return new Quat(x, y, z, w);
980
+ }