transformcomposer.js
5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import ARCS from '../engine/arcs.js';
import * as THREE from '../deps/three.js/index.js';
let TransformComposer = ARCS.Component.create(
function(config) {
const self = this;
config ??= {};
config.poses ??= {};
config.operations ??= {};
let poses = {};
// operand list
// multiply, premultiply, invert, compose
// structure for the operation
/*
* {
* poses : {
* pose_name : { type : "", values: [opt] }
* },
* operations : {
* op_name : { func: f_name, params : [] }
* }
* }
*/
let fTypes = {
'mat' : function(m) {
return m.clone();
},
'arr' : function(arr) {
return (new THREE.Matrix4()).fromArray(arr);
},
'pos_quat' : function(pos,quat) {
let m = new THREE.Matrix4()
m.setPosition((new THREE.Vector3()).fromArray(pos));
m.makeRotationFromQuaternion((new THREE.Quaternion()).fromArray(quat));
},
'pos': function(pos) {
return (new THREE.Matrix4()).setPosition((new THREE.Vector3()).fromArray(pos));
},
'quat': function(pos) {
return (new THREE.Matrix4()).makeRotationFromQuaternion((new THREE.Quaternion()).fromArray(quat));
},
};
let fOperators = {
'-1' : function(m) {
return m.clone().invert();
},
'T': function(m) {
return m.clone().transpose();
},
'*>' : function() {
if( arguments.length === 0)
return new THREE.Matrix4();
let mat = arguments[0].clone();
if (arguments.length > 1) {
for (let i=1; i< arguments.length; i++) {
mat.multiply(arguments[i]);
}
}
return mat;
},
'<*' : function() {
if( arguments.length === 0)
return new THREE.Matrix4();
let mat = arguments[0].clone();
if (arguments.length > 1) {
for (let i=1; i< arguments.length; i++) {
mat.premultiply(arguments[i]);
}
}
return mat;
},
't': function(a,b) {
return a.clone().copyPosition(b);
},
'R': function(a, b) {
return a.clone().extractRotation(b);
},
'|': function(m) {
let mat = m.elements;
const mX = new THREE.Vector3(mat[0],0,mat[2]);
const mY = new THREE.Vector3(0,1,0);
const mZ = new THREE.Vector3(mat[8],0,mat[10]);
const lm = new THREE.Vector3();
lm.addVectors(mX,mZ);
const lX = lm.clone().normalize().applyAxisAngle(mY,Math.PI/4);
const lZ = lm.clone().normalize().applyAxisAngle(mY,-Math.PI/4);
// potentially, the acquired point is a little bit lower
// so, a correction should also be applied to this one.
const rY = new THREE.Vector3(mat[4],mat[5],mat[6]);
const rZ = mY.clone().cross(rY);
const aZ = rY.angleTo(mY);
const t = new THREE.Vector3(mat[12], mat[13], mat[14]);
t.applyAxisAngle(rZ,-aZ);
return (new THREE.Matrix4()).fromArray([
lX.x, lX.y, lX.z, 0,
mY.x, mY.y, mY.z, 0,
lZ.x, lZ.y, lZ.z, 0,
t.x, t.y, t.z, 1
]);
}
};
let computeMatrix = function(obj) {
if (typeof obj === "string") {
console.log(obj,poses[obj]);
return poses[obj];
} else {
for (let p in obj) {
let mat = fOperators[p].apply(
null, obj[p].map( (v) => { return computeMatrix(v);} )
);
console.log(JSON.stringify(obj), mat);
return mat;
}
}
};
let computePose = function(name) {
let obj = config.operations[name];
if (obj === undefined) return;
let m = computeMatrix(obj);
console.log(name, m);
self.emit(name, m);
};
for(let p in config.poses) {
if (config.poses.hasOwnProperty(p)) {
self.slot(p, function() { poses[p] = fTypes[config.poses[p].type].apply(null,arguments);});
if (config.poses[p].values !== undefined) {
/*poses[p] =*/ self[p].apply(null, config.poses[p].values);
} else {
poses[p] = new THREE.Matrix4();
}
}
}
for (let o in config.operations) {
// create operations that correspond to a slot and signals
self.signal(o);
self.slot(o, ((name) => { return function() { computePose(name);};})(o));
}
//console.log("transform composer", self, poses);
}
);
export default { TransformComposer: TransformComposer};
/*
ARCS.__lib__`
{
"components": {
"TransformComposer": {
"description": "A component that computes a transform matrix from a set of poses and operations",
"keywords": ["transform", "matrix", "pose"]
}
},
"dependencies": [ "three" ],
"recipes": {
"from": "node_modules/three/build/three.module.js",
"to": "deps/three.js/index.js"
}
}
`;
*/