UNPKG

2.33 kBJavaScriptView Raw
1const Matrix4x4 = require('./math/Matrix4')
2const Vector3D = require('./math/Vector3')
3const Plane = require('./math/Plane')
4
5// Add several convenience methods to the classes that support a transform() method:
6const addTransformationMethodsToPrototype = function (prot) {
7 prot.mirrored = function (plane) {
8 return this.transform(Matrix4x4.mirroring(plane))
9 }
10
11 prot.mirroredX = function () {
12 let plane = new Plane(Vector3D.Create(1, 0, 0), 0)
13 return this.mirrored(plane)
14 }
15
16 prot.mirroredY = function () {
17 let plane = new Plane(Vector3D.Create(0, 1, 0), 0)
18 return this.mirrored(plane)
19 }
20
21 prot.mirroredZ = function () {
22 let plane = new Plane(Vector3D.Create(0, 0, 1), 0)
23 return this.mirrored(plane)
24 }
25
26 prot.translate = function (v) {
27 return this.transform(Matrix4x4.translation(v))
28 }
29
30 prot.scale = function (f) {
31 return this.transform(Matrix4x4.scaling(f))
32 }
33
34 prot.rotateX = function (deg) {
35 return this.transform(Matrix4x4.rotationX(deg))
36 }
37
38 prot.rotateY = function (deg) {
39 return this.transform(Matrix4x4.rotationY(deg))
40 }
41
42 prot.rotateZ = function (deg) {
43 return this.transform(Matrix4x4.rotationZ(deg))
44 }
45
46 prot.rotate = function (rotationCenter, rotationAxis, degrees) {
47 return this.transform(Matrix4x4.rotation(rotationCenter, rotationAxis, degrees))
48 }
49
50 prot.rotateEulerAngles = function (alpha, beta, gamma, position) {
51 position = position || [0, 0, 0]
52
53 let Rz1 = Matrix4x4.rotationZ(alpha)
54 let Rx = Matrix4x4.rotationX(beta)
55 let Rz2 = Matrix4x4.rotationZ(gamma)
56 let T = Matrix4x4.translation(new Vector3D(position))
57
58 return this.transform(Rz2.multiply(Rx).multiply(Rz1).multiply(T))
59 }
60}
61
62// TODO: consider generalization and adding to addTransformationMethodsToPrototype
63const addCenteringToPrototype = function (prot, axes) {
64 prot.center = function (cAxes) {
65 cAxes = Array.prototype.map.call(arguments, function (a) {
66 return a // .toLowerCase();
67 })
68 // no args: center on all axes
69 if (!cAxes.length) {
70 cAxes = axes.slice()
71 }
72 let b = this.getBounds()
73 return this.translate(axes.map(function (a) {
74 return cAxes.indexOf(a) > -1 ? -(b[0][a] + b[1][a]) / 2 : 0
75 }))
76 }
77}
78module.exports = {
79 addTransformationMethodsToPrototype,
80 addCenteringToPrototype
81}