[go: up one dir, main page]

Menu

[9202b6]: / body / Frame.cc  Maximize  Restore  History

Download this file

239 lines (205 with data), 6.4 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Frame.cc - a coordinate system.
//
// Copyright (C) 2001--2004 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "Frame.h"
#include "../geometry/Conversions.h"
#include <cassert>
#include <cmath>
using Vamos_Geometry::Three_Vector;
using Vamos_Geometry::Three_Matrix;
using namespace Vamos_Body;
//* Class Frame
//** Constructors
// Specify the position and orientation.
Frame::Frame (const Three_Vector& position,
const Three_Matrix& orientation,
const Frame* parent)
: mp_parent (parent),
m_orientation (orientation),
m_position (position)
{
}
// Take the parent's orientation.
Frame::Frame (const Three_Vector& position,
const Frame* parent)
: mp_parent (parent),
m_position (position)
{
}
// Make a frame that's coincident with the parent frame.
Frame::Frame (const Frame* parent)
: mp_parent (parent)
{
}
Three_Vector
Frame::transform_from_parent (const Three_Vector& r) const
{
return rotate_from_parent (r - m_position);
}
Three_Vector
Frame::transform_velocity_from_parent (const Three_Vector& v) const
{
return rotate_from_parent (v - m_velocity);
}
Three_Vector
Frame::transform_from_world (const Three_Vector& r) const
{
Three_Vector in = transform_from_parent (r);
if (is_world_frame ())
return in;
else
return mp_parent->transform_from_world (in);
}
Three_Vector
Frame::transform_velocity_from_world (const Three_Vector& v) const
{
Three_Vector in = transform_velocity_from_parent (v);
if (is_world_frame ())
return in;
else
return mp_parent->transform_velocity_from_world (in);
}
Three_Vector
Frame::transform_to_parent (const Three_Vector& r) const
{
return rotate_to_parent (r) + m_position;
}
Three_Vector
Frame::transform_velocity_to_parent (const Three_Vector& v) const
{
return rotate_to_parent (v) + m_velocity;
}
Three_Vector
Frame::transform_to_world (const Three_Vector& r) const
{
Three_Vector out = transform_to_parent (r);
if (is_world_frame ())
return out;
else
return mp_parent->transform_to_world (out);
}
Three_Vector
Frame::transform_velocity_to_world (const Three_Vector& v) const
{
Three_Vector out = transform_velocity_to_parent (v);
if (is_world_frame ())
return out;
else
return mp_parent->transform_velocity_to_world (out);
}
Three_Vector
Frame::rotate_to_parent (const Three_Vector& vector) const
{
return m_orientation * vector;
}
Three_Vector
Frame::rotate_to_world (const Three_Vector& vector) const
{
Three_Vector out = rotate_to_parent (vector);
if (is_world_frame ())
return out;
else
return mp_parent->rotate_to_world (out);
}
Three_Vector
Frame::rotate_from_parent (const Three_Vector& vector) const
{
return m_orientation.transpose() * vector;
}
Three_Vector
Frame::rotate_from_world (const Three_Vector& vector) const
{
Three_Vector in = rotate_from_parent (vector);
if (is_world_frame ())
return in;
else
return mp_parent->rotate_from_world (in);
}
// Express the orientation of this frame as a vector in the parent
// frame and a rotation about that vector. ANGLE holds the rotation
// angle when the function returns. The returned vector has a
// magnitude of sin (ANGLE). The values returned are suitable for use
// with the glRotate functions.
Three_Vector
Frame::axis_angle (double* angle) const
{
// To convert the rotation matrix representation of the body's orientation
// to an axis-angle orientation, we transform first to a quaternion
// representation. The matrix-to-quaternion and quaternion-to-axis-angle
// transformations are described in the Matrix and Quaternion FAQ
// (matrixfaq.htm) in the doc directory.
// Make a local reference to the tranformation matrix for brevity.
const Three_Matrix& omat = m_orientation;
// Convert from matrix to quaternion
double trace = omat [0][0] + omat [1][1] + omat [2][2] + 1.0;
double s, w, x, y, z;
s = w = x = y = z = 0.0;
if (trace > 0.0)
{
s = 0.5 / sqrt (trace);
w = 0.25 / s;
x = (omat [2][1] - omat [1][2]) * s;
y = (omat [0][2] - omat [2][0]) * s;
z = (omat [1][0] - omat [0][1]) * s;
}
else
{
// Find the largest diagonal element and do the appropriate
// transformation.
double largest = omat [0][0];
int index = 0;
if (omat [1][1] > largest)
{
largest = omat [1][1];
index = 1;
}
if (omat [2][2] > largest)
{
largest = omat [2][2];
s = sqrt (1.0 - omat [0][0] - omat [1][1] + omat [2][2]) * 2.0;
w = (omat [0][1] + omat [1][0]) / s;
x = (omat [0][2] + omat [2][0]) / s;
y = (omat [1][2] + omat [2][1]) / s;
z = 0.5 / s;
}
else if (index == 0)
{
s = sqrt (1.0 + omat [0][0] - omat [1][1] - omat [2][2]) * 2.0;
w = (omat [1][2] + omat [2][1]) / s;
x = 0.5 / s;
y = (omat [0][1] + omat [1][0]) / s;
z = (omat [0][2] + omat [2][0]) / s;
}
else
{
assert (index == 1);
s = sqrt (1.0 - omat [0][0] + omat [1][1] - omat [2][2]) * 2.0;
w = (omat [0][2] + omat [2][0]) / s;
x = (omat [0][1] + omat [1][0]) / s;
y = 0.5 / s;
z = (omat [1][2] + omat [2][1]) / s;
}
}
// Convert from quaternion to angle-axis.
*angle = Vamos_Geometry::rad_to_deg (acos (w) * 2.0);
// The return value would be divided by sin (angle) to give a unit
// vector, but glRotate* () does not care about the length so we'll
// leave it as is.
return Three_Vector (x, y, z);
}