-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphicsCellODE.cpp
More file actions
342 lines (276 loc) · 11 KB
/
GraphicsCellODE.cpp
File metadata and controls
342 lines (276 loc) · 11 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/***************************************************************************//**
* Project: Colony
*
* \file GraphicsCellODE.cpp
* \author Marc Weber\n
* The SiMBioSys group (CosmoLab)\n
* Parc Científic de Barcelona\n
* Barcelona, Spain.\n
* http://www.thesimbiosys.com
* \version 1.0
* \date 05/2011
*
* Copyright 2009 by Marc Weber
******************************************************************************/
#include "GraphicsCellODE.h"
#include "SpatialIntegratorODE.h"
//------------------------------------------------------------------------------
GraphicsCellODE::GraphicsCellODE()
: isAddedInSpatialIntegratorODECellsList_(false)
{}
//------------------------------------------------------------------------------
GraphicsCellODE::~GraphicsCellODE()
{
/// Delete reference in the list of cell in SpatialIntegratorODE class.
if (isAddedInSpatialIntegratorODECellsList_)
{
spatialIntegratorODE_->getCellsList()->removeAll(this);
isAddedInSpatialIntegratorODECellsList_ = false;
}
dJointDestroy(planeJointID_);
dBodyDestroy (body_);
dGeomDestroy (geom_);
}
//------------------------------------------------------------------------------
void GraphicsCellODE::initialize(dWorldID world, dSpaceID space, SpatialIntegratorODE* spatialIntegratorODE)
{
/// - Set the ODE world and space.
world_ = world;
space_ = space;
spatialIntegratorODE_ = spatialIntegratorODE;
planeJointID_ = dJointCreatePlane2D( world_, 0);
/// - Get the cell height from the GraphicsCell object.
float radius = cellHeight_/2.0;
if (radius <= 0) radius = 0.5;
float cylinderLength = cellLength_ - 2.0*radius;
if (cylinderLength <= 0) cylinderLength = 1.0;
geom_ = dCreateCCylinder(space_, radius, cylinderLength);
/// - Initiate body (mass, inertia).
body_ = dBodyCreate(world_);
dMass m;
dMassSetCappedCylinderTotal(&m, 1.0f, 3, radius, cylinderLength);
dBodySetMass(body_,&m);
/// - Associate body and geometry.
dGeomSetBody(geom_,body_);
dGeomSetPosition(geom_, 0.0, 0.0, 0.0);
dBodySetLinearVel(body_, 0.0, 0.0, 0.0);
dBodySetAngularVel(body_, 0.0, 0.0, 0.0);
/// - Restrict movement to the plane.
dJointAttach( planeJointID_, body_, 0 );
/// - Add the cell to the list in the spatialIntegratorODE
if (!isAddedInSpatialIntegratorODECellsList_)
{
spatialIntegratorODE_->getCellsList()->append(this);
isAddedInSpatialIntegratorODECellsList_ = true;
}
}
//------------------------------------------------------------------------------
GraphicsCellODE::GraphicsCellODE(const GraphicsCellODE& cell)
{
copy(cell);
}
//------------------------------------------------------------------------------
void GraphicsCellODE::copy(const GraphicsCellODE& cell)
{
isAddedInSpatialIntegratorODECellsList_ = false;
/// - Set the ODE world and space.
world_ = cell.world_;
space_ = cell.space_;
spatialIntegratorODE_ = cell.spatialIntegratorODE_;
planeJointID_ = dJointCreatePlane2D( world_, 0);
/// - Get body position and orientation.
const dReal *posConstPointer = dGeomGetPosition(cell.geom_);
dReal pos[3];
for(int i=0; i<3; ++i)
{
pos[i] = posConstPointer[i];
}
dQuaternion quat;
dGeomGetQuaternion(cell.geom_, quat);
/// - Get the body linear velocity and angular velocity.
const dReal *linearVelocity = dBodyGetLinearVel(cell.body_);
const dReal *angularVelocity = dBodyGetAngularVel(cell.body_);
/// - Get the cell height from the GraphicsCell object.
float radius = cell.cellHeight_/2.0;
if (radius <= 0) radius = 0.5;
float cylinderLength = cell.cellLength_ - 2.0*radius;
if (cylinderLength <= 0) cylinderLength = 1.0;
float angle = cell.angle_;
/// - Initiate geometry (object shape).
geom_ = dCreateCCylinder(space_, radius, cylinderLength);
/// - Initiate body (mass, inertia).
body_ = dBodyCreate(world_);
dMass m;
dMassSetCappedCylinderTotal(&m, 1.0f, 3, radius, cylinderLength);
dBodySetMass(body_,&m);
/// - Associate body and geometry.
dGeomSetBody(geom_,body_);
/// - Set geometry and body position to the same as before.
dGeomSetPosition(geom_, pos[0], pos[1], pos[2]);
/// - Set geometry and body rotation to the same as before.
dGeomSetQuaternion(geom_, quat);
/// - Set geometry and body linear velocity to the same as before.
dBodySetLinearVel(body_, linearVelocity[0], linearVelocity[1], linearVelocity[2]);
/// - Set geometry and body angular velocity to the same as before.
dBodySetAngularVel(body_, angularVelocity[0], angularVelocity[1], angularVelocity[2]);
/// - Restrict movement to the plane.
dJointAttach( planeJointID_, body_, 0 );
/// - Add the cell to the list in the spatialIntegratorODE
if (!isAddedInSpatialIntegratorODECellsList_)
{
spatialIntegratorODE_->getCellsList()->append(this);
isAddedInSpatialIntegratorODECellsList_ = true;
}
}
//------------------------------------------------------------------------------
void GraphicsCellODE::setPos(float x, float y, float z)
{
dBodySetPosition(body_, x, y, z);
}
//------------------------------------------------------------------------------
void GraphicsCellODE::setAngle(float angle)
{
/// - Set the rotation matrix R corresponding to the orientation defined by the angle parameter.
dMatrix3 R;
/// - The axis of rotation is in the xy plane and is perpendicular to the cell orientation.
/// The angle of rotation is 90 degrees.
dRFromAxisAndAngle(R, cos(PI*(angle_+90.0)/180.0), sin(PI*(angle_+90.0)/180.0), 0.0, PI/2.0);
dBodySetRotation(body_, R);
}
//------------------------------------------------------------------------------
void GraphicsCellODE::setCellLength(float length)
{
GraphicsCellBase::setCellLength(length);
updateGeometry();
}
//------------------------------------------------------------------------------
void GraphicsCellODE::setCellHeight(float height)
{
GraphicsCellBase::setCellHeight(height);
updateGeometry();
}
//------------------------------------------------------------------------------
void GraphicsCellODE::updateGeometry()
{
/// - Get body position and orientation.
const dReal *posConstPointer = dGeomGetPosition(geom_);
dReal pos[3];
for(int i=0; i<3; ++i)
{
pos[i] = posConstPointer[i];
}
dQuaternion quat;
dGeomGetQuaternion(geom_, quat);
/// - Get the body linear velocity and angular velocity.
const dReal *linearVelocity = dBodyGetLinearVel(body_);
const dReal *angularVelocity = dBodyGetAngularVel(body_);
/// - Get the cell height from the GraphicsCell object.
float radius = cellHeight_/2.0;
if (radius <= 0) radius = 0.5;
float cylinderLength = cellLength_ - 2.0*radius;
if (cylinderLength <= 0) cylinderLength = 1.0;
float angle = angle_;
/// - Initiate geometry (object shape).
dGeomDestroy(geom_);
geom_ = dCreateCCylinder(space_, radius, cylinderLength);
/// - Initiate body (mass, inertia).
dMass m;
dMassSetCappedCylinderTotal(&m, 1.0f, 3, radius, cylinderLength);
dBodySetMass(body_,&m);
/// - Associate body and geometry.
dGeomSetBody(geom_,body_);
/// - Set geometry and body position to the same as before.
dGeomSetPosition(geom_, pos[0], pos[1], pos[2]);
/// - Set geometry and body rotation to the same as before.
dGeomSetQuaternion(geom_, quat);
/// - Set geometry and body linear velocity to the same as before.
dBodySetLinearVel(body_, linearVelocity[0], linearVelocity[1], linearVelocity[2]);
/// - Set geometry and body angular velocity to the same as before.
dBodySetAngularVel(body_, angularVelocity[0], angularVelocity[1], angularVelocity[2]);
/// - Restrict movement to the plane.
dJointAttach( planeJointID_, body_, 0 );
}
//------------------------------------------------------------------------------
dWorldID GraphicsCellODE::getWorld() const
{
return world_;
}
//------------------------------------------------------------------------------
dSpaceID GraphicsCellODE::getSpace() const
{
return space_;
}
//------------------------------------------------------------------------------
dBodyID GraphicsCellODE::getBody()
{
return body_;
}
//------------------------------------------------------------------------------
TinyVector<double,3> GraphicsCellODE::getPositionODE() const
{
TinyVector<double,3> position;
const dReal *positionVector = dGeomGetPosition (geom_);
position(0) = positionVector[0];
position(1) = positionVector[1];
position(2) = positionVector[2];
return position;
}
//------------------------------------------------------------------------------
double GraphicsCellODE::getAngle() const
{
/// - Get the quaternion matrix of the ODE geometry.
const dReal *quat = dBodyGetQuaternion( body_ );
/// - Remark: a quaternion is equal to (cos(alpha/2),sin(alpha/2)*u), where u is a unit vector of the rotation axis.
/// - Remark: the rotation axis should already be in the xy plane and the rotation angle should be 90 degrees.
/// - Calculate the orientation of the rotation axis in the xy plane, Euler psi angle.
float psi;
psi = atan2(2*(quat[0]*quat[3]+quat[1]*quat[2]), 1-2*(quat[2]*quat[2]+quat[3]*quat[3]));
/// - The orientation of the cell is perpendicular to the rotation axis.
double angle;
angle = psi - (PI/2.0);
/// - Transform from radians to degrees.
angle = angle*180 / PI;
return angle;
}
//------------------------------------------------------------------------------
void GraphicsCellODE::alignToZAxis()
{
const dReal *rot = dBodyGetAngularVel( body_ );
const dReal *quat_ptr;
dReal quat[4], quat_len;
quat_ptr = dBodyGetQuaternion( body_ );
quat[0] = 1.0 / sqrt(2.0);
quat[1] = quat_ptr[1];
quat[2] = quat_ptr[2];
quat[3] = 0;
quat_len = quat[1] * quat[1] + quat[2] * quat[2] ;
quat[1] = 0.5 * quat[1] / quat_len;
quat[2] = 0.5 * quat[2] / quat_len;
dBodySetQuaternion( body_, quat );
dBodySetAngularVel( body_, 0, 0, rot[2] );
}
//------------------------------------------------------------------------------
void GraphicsCellODE::limitVelocity(float maxVelocitySquared, float maxRotVelocitySquared)
{
const dReal *vel = dBodyGetLinearVel( body_ );
float velNormSquared = vel[0]*vel[0] + vel[1]*vel[1] + vel[2]*vel[2];
if ( velNormSquared > maxVelocitySquared)
{
dReal newVel[3];
newVel[0] = sqrt(maxVelocitySquared/velNormSquared)*vel[0];
newVel[1] = sqrt(maxVelocitySquared/velNormSquared)*vel[1];
newVel[2] = sqrt(maxVelocitySquared/velNormSquared)*vel[2];
dBodySetLinearVel (body_, newVel[0], newVel[1], newVel[2]);
}
const dReal *rotVel = dBodyGetAngularVel( body_ );
float rotVelNormSquared = rotVel[0]*rotVel[0] + rotVel[1]*rotVel[1] + rotVel[2]*rotVel[2];
if ( rotVelNormSquared > maxRotVelocitySquared)
{
dReal newRotVel[3];
newRotVel[0] = sqrt(maxRotVelocitySquared/rotVelNormSquared)*rotVel[0];
newRotVel[1] = sqrt(maxRotVelocitySquared/rotVelNormSquared)*rotVel[1];
newRotVel[2] = sqrt(maxRotVelocitySquared/rotVelNormSquared)*rotVel[2];
dBodySetAngularVel (body_, newRotVel[0], newRotVel[1], newRotVel[2]);
}
}
//------------------------------------------------------------------------------