-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTag.cpp
179 lines (135 loc) · 3.93 KB
/
Tag.cpp
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
178
179
#include "Tag.h"
#include <cmath> // For trig functions
#include "stdafx.h"
#include <boost/math/quaternion.hpp> // For quaternion
using namespace std;
using namespace boost::math;
// Extend ostream.
// Output stream operator. Allows easy writing of tag data to an output stream.
ostream& operator<<(ostream& output_stream, const Tag& tag) {
tuple<float, float, float> position = tag.getPosition();
tuple<float, float, float> orientation = tag.calcRollPitchYaw();
output_stream << "Tag ID:" << tag.getID()
<< ", Position (XYZ): <"
<< get<0>(position) << ", "
<< get<1>(position) << ", "
<< get<2>(position) << ", "
<< ">, "
<< " Orientation (RPY): <"
<< get<0>(orientation) << ", "
<< get<1>(orientation) << ", "
<< get<2>(orientation) << ", "
<< ">"
<< endl;
return output_stream;
}
// Constructors
Tag::Tag() {
// Initialize orientation and position
orientation = ::boost::math::quaternion<float>(0, 0, 0, 0);
position = make_tuple(0, 0, 0);
}
Tag::Tag(const Tag &that) {
this->setPosition(that.position);
this->setOrientation(that.orientation);
this->setID(that.id);
}
Tag::Tag(float x, float y, float z)
{
position = make_tuple(x, y, z);
}
int Tag::getID() const {
return id;
}
void Tag::setID(int id) {
this->id = id;
}
tuple<float, float, float> Tag::getPosition() const {
return position;
}
void Tag::setPosition(tuple<float, float, float> position) {
this->position = position;
}
quaternion<float> Tag::getOrientation() const {
return orientation;
}
void Tag::setOrientation(quaternion<float> orientation) {
this->orientation = orientation;
}
// convenience accessor functions
float Tag::getPositionX() const {
return std::get<0>(position);
}
float Tag::getPositionY() const {
return std::get<1>(position);
}
float Tag::getPositionZ() const {
return std::get<2>(position);
}
float Tag::getOrientationX() const {
return orientation.R_component_1();
}
float Tag::getOrientationY() const {
return orientation.R_component_2();
}
float Tag::getOrientationZ() const {
return orientation.R_component_3();
}
float Tag::getOrientationW() const {
return orientation.R_component_4();
}
void Tag::setPositionX(float x) {
std::get<0>(position) = x;
}
void Tag::setPositionY(float y) {
std::get<1>(position) = y;
}
void Tag::setPositionZ(float z) {
std::get<2>(position) = z;
}
// The following functions recreate the entire quaternion each time a value is changed
// becuase you cannot change quaternion components without rebuilding the whole quaternion.
// This is a limitation of the boost quaternion.
void Tag::setOrientationX(float x) {
orientation = quaternion<float>(x, getOrientationY(), getOrientationZ(), getOrientationW());
}
void Tag::setOrientationY(float y) {
orientation = quaternion<float>(getOrientationX(), y, getOrientationZ(), getOrientationW());
}
void Tag::setOrientationZ(float z) {
orientation = quaternion<float>(getOrientationX(), getOrientationY(), z, getOrientationW());
}
void Tag::setOrientationW(float w) {
orientation = quaternion<float>(getOrientationX(), getOrientationY(), getOrientationZ(), w);
}
// ***
// Consider whether to memoise the "calc" functions for performace
// ***
// Returns orientation as Roll-Pitch-Yaw
tuple<float, float, float> Tag::calcRollPitchYaw() const {
float yaw = calcYaw();
float pitch = calcPitch();
float roll = calcRoll();
return make_tuple(roll, pitch, yaw);
}
float Tag::calcYaw() const {
float x = getOrientationX();
float y = getOrientationY();
float z = getOrientationZ();
float w = getOrientationW();
return atan2(2.0f*(y*z + w*x), w*w - x*x - y*y + z*z);
}
float Tag::calcPitch() const {
float x = getOrientationX();
float y = getOrientationY();
float z = getOrientationZ();
float w = getOrientationW();
return asin(-2.0f*(x*z - w*y));
}
float Tag::calcRoll() const {
float x = getOrientationX();
float y = getOrientationY();
float z = getOrientationZ();
float w = getOrientationW();
return atan2(2.0f*(y + w*z), w*w + x*x - y*y - z*z);
}