Sensor Scol plugin
Multi platform sensors for handled devices
Vector3d.cpp
1
2#include "tools/Vector3d.h"
3#include <cmath>
4
5
6Vector3d::Vector3d()
7{
8 setZero();
9}
10
11Vector3d::Vector3d(const Vector3d &v)
12{
13 set(v.x, v.y, v.z);
14}
15
16Vector3d::Vector3d(double x, double y, double z)
17{
18 set(x, y, z);
19}
20
21void Vector3d::set(double nx, double ny, double nz)
22{
23 x = nx;
24 y = ny;
25 z = nz;
26}
27
28void Vector3d::setComponent(int i, double val)
29{
30 if (i == 0)
31 {
32 x = val;
33 }
34 else if (i == 1)
35 {
36 y = val;
37 }
38 else
39 {
40 z = val;
41 }
42}
43
44void Vector3d::setZero()
45{
46 set(0, 0, 0);
47}
48
49void Vector3d::set(Vector3d other)
50{
51 set(other.x, other.y, other.z);
52}
53
54void Vector3d::scale(double s)
55{
56 x *= s;
57 y *= s;
58 z *= s;
59}
60
61void Vector3d::normalize()
62{
63 double d = length();
64 if (d != 0.0)
65 {
66 scale(1.0 / d);
67 }
68}
69
70double Vector3d::dot(Vector3d a, Vector3d b)
71{
72 return a.x * b.x + a.y * b.y + a.z * b.z;
73}
74
75double Vector3d::length()
76{
77 return sqrt(x * x + y * y + z * z);
78}
79
80bool Vector3d::sameValues(Vector3d other)
81{
82 return (x == other.x) && (y == other.y) && (z == other.z);
83}
84
85void Vector3d::add(Vector3d a, Vector3d b, Vector3d &result)
86{
87 result.set(a.x + b.x,
88 a.y + b.y,
89 a.z + b.z);
90}
91
92void Vector3d::sub(Vector3d a, Vector3d b, Vector3d &result)
93{
94 result.set(a.x - b.x,
95 a.y - b.y,
96 a.z - b.z);
97}
98
99void Vector3d::cross(Vector3d a, Vector3d b, Vector3d &result)
100{
101 result.set(a.y * b.z - a.z * b.y,
102 a.z * b.x - a.x * b.z,
103 a.x * b.y - a.y * b.x);
104}
105
106void Vector3d::ortho(Vector3d v, Vector3d &result)
107{
108 int k = largestAbsComponent(v) - 1;
109 if (k < 0)
110 {
111 k = 2;
112 }
113 result.setZero();
114 result.setComponent(k, 1.0);
115 cross(v, result, result);
116 result.normalize();
117}
118
119int Vector3d::largestAbsComponent(Vector3d v)
120{
121 double xAbs = fabs(v.x);
122 double yAbs = fabs(v.y);
123 double zAbs = fabs(v.z);
124 if (xAbs > yAbs)
125 {
126 if (xAbs > zAbs)
127 {
128 return 0;
129 }
130 return 2;
131 }
132 if (yAbs > zAbs)
133 {
134 return 1;
135 }
136 return 2;
137}