forked from RobotWebTools/roslibjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath-examples.test.js
81 lines (73 loc) · 2.22 KB
/
math-examples.test.js
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
var expect = require('chai').expect;
var ROSLIB = require('..');
function clone(x) {
var y = {};
for (var prop in x) {
if (x.hasOwnProperty(prop)) {
y[prop] = typeof x[prop] === "object" ? clone(x[prop]) : x[prop];
}
}
return y;
}
describe('Math examples', function() {
var v1, q1, v2, q2;
var pos;
it('Vector3 example', function() {
// Let's start by adding some vectors.
v1 = new ROSLIB.Vector3({
x: 1,
y: 2,
z: 3
});
v2 = v1.clone();
expect(v1).not.equal(v2);
expect(v1).eql(v2);
v1.add(v2);
expect(clone(v1)).eql({
x: 2,
y: 4,
z: 6
});
});
it('Quaternion example', function() {
// Now let's play with some quaternions.
q1 = new ROSLIB.Quaternion({
x: 0.1,
y: 0.2,
z: 0.3,
w: 0.4
});
q2 = q1.clone();
expect(q1).not.equal(q2);
expect(q1).eql(q2);
q1.multiply(q2);
q1.invert();
expect(q1.x).to.be.within(-0.26667, -0.26666);
expect(q1.y).to.be.within(-0.53334, -0.53333);
expect(q1.z).to.be.within(-0.80000, -0.79999);
expect(q1.w).to.be.within(0.06666, 0.06667);
});
it('Pose example', function() {
// Let's copy the results into a pose.
pos = new ROSLIB.Pose({
position: v1,
orientation: q1
});
expect(clone(pos)).to.eql(clone({position: v1, orientation: q1}));
});
it('Transform example', function() {
// Finally, let's play with some transforms.
var tf = new ROSLIB.Transform({
translation: v2,
rotation: q2
});
pos.applyTransform(tf);
expect(pos.orientation.x).to.be.within(-0.1, -0.09999);
expect(pos.orientation.y).to.be.within(-0.20001, -0.20000);
expect(pos.orientation.z).to.be.within(-0.3, -0.3);
expect(pos.orientation.w).to.be.within(0.39999, 0.4);
expect(pos.position.x).to.be.within(1.6, 1.60001);
expect(pos.position.y).to.be.within(3.2, 3.20001);
expect(pos.position.z).to.be.within(4.8, 4.80001);
});
});