CODEGenericBody.h
Go to the documentation of this file.
1 //===========================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti
40  \version 3.2.0 $Rev: 1917 $
41 */
42 //===========================================================================
43 
44 //---------------------------------------------------------------------------
45 #ifndef CODEGenericBodyH
46 #define CODEGenericBodyH
47 //---------------------------------------------------------------------------
48 #ifndef dDOUBLE
49 #define dDOUBLE
50 #endif
51 #include "ode/ode.h"
52 #include "chai3d.h"
53 //---------------------------------------------------------------------------
54 
55 //===========================================================================
63 //===========================================================================
64 
65 //---------------------------------------------------------------------------
66 class cODEWorld;
67 //---------------------------------------------------------------------------
70 {
76 };
77 //---------------------------------------------------------------------------
78 
79 //===========================================================================
90 //===========================================================================
91 class cODEGenericBody : public chai3d::cGenericObject, public dBody
92 {
93  //-----------------------------------------------------------------------
94  // CONSTRUCTOR & DESTRUCTOR:
95  //-----------------------------------------------------------------------
96 
97 public:
98 
100  cODEGenericBody(cODEWorld* a_world) { initialize(a_world); }
101 
103  virtual ~cODEGenericBody() {};
104 
105 
106  //-----------------------------------------------------------------------
107  // PUBLIC METHODS - TRANSLATION AND ORIENTATION:
108  //-----------------------------------------------------------------------
109 
110 public:
111 
113  virtual void setLocalPos(const chai3d::cVector3d &a_position);
114 
115 #ifdef C_USE_EIGEN
116  void setLocalPos(const Eigen::Vector3d& a_localPos)
118  {
119  setLocalPos(chai3d::cVector3d(a_localPos[0], a_localPos[1], a_localPos[2]));
120  }
121 #endif
122 
124  void setLocalPos(const double a_x = 0.0,
125  const double a_y = 0.0,
126  const double a_z = 0.0)
127  {
128  setLocalPos(chai3d::cVector3d(a_x, a_y, a_z));
129  }
130 
131 #ifdef C_USE_EIGEN
132  inline void setLocalRot(const Eigen::Matrix3d a_localRot)
134  {
135  chai3d::cMatrix3d localRot;
136  localRot.copyfrom(a_localRot);
137  setLocalRot(localRot);
138  }
139 #endif
140 
142  virtual void setLocalRot(const chai3d::cMatrix3d &a_rotation);
143 
145  void updateBodyPosition(void);
146 
147 
148  //-----------------------------------------------------------------------
149  // PUBLIC METHODS - EXTERNAL FORCES:
150  //-----------------------------------------------------------------------
151 
152 public:
153 
155  void addExternalForce(const chai3d::cVector3d& a_force);
156 
158  void addExternalTorque(const chai3d::cVector3d& a_torque);
159 
161  void addExternalForceAtPoint(const chai3d::cVector3d& a_force,
162  const chai3d::cVector3d& a_pos);
163 
164  //-----------------------------------------------------------------------
165  // PUBLIC METHODS - DYNAMIC PROPERTIES:
166  //-----------------------------------------------------------------------
167 
168 public:
169 
171  void setMass(const double a_mass);
172 
174  double getMass() const { return (m_mass); }
175 
177  void enableDynamics();
178 
180  void disableDynamics();
181 
183  bool isStatic() const { return (m_static); }
184 
186  virtual void buildDynamicModel() {};
187 
189  void createDynamicBoundingBox(const bool a_staticObject = false);
190 
192  void createDynamicSphere(const double a_radius,
193  const bool a_staticObject = false,
194  const chai3d::cVector3d& a_offsetPos = chai3d::cVector3d(0.0, 0.0, 0.0),
195  const chai3d::cMatrix3d& a_offsetRot = chai3d::cIdentity3d());
196 
198  void createDynamicBox(const double a_lengthX,
199  const double a_lengthY,
200  const double a_lengthZ,
201  const bool a_staticObject = false,
202  const chai3d::cVector3d& a_offsetPos = chai3d::cVector3d(0.0, 0.0, 0.0),
203  const chai3d::cMatrix3d& a_offsetRot = chai3d::cIdentity3d());
204 
206  void createDynamicCapsule(const double a_radius,
207  const double a_length,
208  const bool a_staticObject = false,
209  const chai3d::cVector3d& a_offsetPos = chai3d::cVector3d(0.0, 0.0, 0.0),
210  const chai3d::cMatrix3d& a_offsetRot = chai3d::cIdentity3d());
211 
213  void createDynamicCylinder(const double a_radius,
214  const double a_length,
215  const bool a_staticObject = false,
216  const chai3d::cVector3d& a_offsetPos = chai3d::cVector3d(0.0, 0.0, 0.0),
217  const chai3d::cMatrix3d& a_offsetRot = chai3d::cIdentity3d());
218 
220  void createStaticPlane(const chai3d::cVector3d& a_position,
221  const chai3d::cVector3d& a_normal);
222 
224  void createDynamicMesh(const bool a_staticObject = false,
225  const chai3d::cVector3d& a_offsetPos = chai3d::cVector3d(0.0, 0.0, 0.0),
226  const chai3d::cMatrix3d& a_offsetRot = chai3d::cIdentity3d());
227 
228 
229  //-----------------------------------------------------------------------
230  // PUBLIC METHODS - COLLISION DETETCTION:
231  //-----------------------------------------------------------------------
232 
233 public:
234 
236  virtual bool computeCollisionDetection(const chai3d::cVector3d& a_segmentPointA,
237  const chai3d::cVector3d& a_segmentPointB,
238  chai3d::cCollisionRecorder& a_recorder,
239  chai3d::cCollisionSettings& a_settings);
240 
241 
242  //-----------------------------------------------------------------------
243  // PUBLIC METHODS - BODY IMAGE AND DISPLAY PROPERTIES:
244  //-----------------------------------------------------------------------
245 
246 public:
247 
249  void setImageModel(chai3d::cGenericObject* a_imageModel);
250 
253 
255  void setShowDynamicCollisionModel(const bool a_show) { m_showDynamicCollisionModel = a_show; }
256 
259 
260 
261  //-----------------------------------------------------------------------
262  // PUBLIC MEMBERS:
263  //-----------------------------------------------------------------------
264 
265 public:
266 
269 
272 
274  dBodyID m_ode_body;
275 
277  dGeomID m_ode_geom;
278 
281 
282 
283  //-----------------------------------------------------------------------
284  // PROTECTED METHODS:
285  //-----------------------------------------------------------------------
286 
287 protected:
288 
290  virtual void updateGlobalPositions(const bool a_frameOnly);
291 
293  virtual void render(chai3d::cRenderOptions& a_options);
294 
296  int buildMeshTable(chai3d::cMultiMesh* a_multiMesh);
297 
299  int buildMeshTable(chai3d::cMesh* a_mesh);
300 
302  void initialize(cODEWorld* a_world);
303 
304 
305  //-----------------------------------------------------------------------
306  // PROTECTED MEMBERS:
307  //-----------------------------------------------------------------------
308 
309 protected:
310 
312  bool m_static;
313 
315  dMass m_ode_mass;
316 
318  double m_mass;
319 
321  float* m_vertices;
322 
325 
327  double m_prevTransform[16];
328 
330  dTriMeshDataID m_ode_triMeshDataID;
331 
334 
337 
348 };
349 
350 
351 //---------------------------------------------------------------------------
352 #endif
353 //---------------------------------------------------------------------------
void createDynamicBoundingBox(const bool a_staticObject=false)
This method creates a dynamic box representation for modeling collisions between objects.
Definition: CODEGenericBody.cpp:425
void addExternalForceAtPoint(const chai3d::cVector3d &a_force, const chai3d::cVector3d &a_pos)
This method applies an external force at a given position described in global coordinates to this obj...
Definition: CODEGenericBody.cpp:211
This class implements an ODE dynamic object.
Definition: CODEGenericBody.h:91
virtual ~cODEGenericBody()
Destructor of cODEGenericBody.
Definition: CODEGenericBody.h:103
double m_paramDynColModel2
Definition: CODEGenericBody.h:345
double m_prevTransform[16]
ODE previous tri mesh position and orientation.
Definition: CODEGenericBody.h:327
chai3d::cColorf m_colorDynamicCollisionModel
Color used to render collision model.
Definition: CODEGenericBody.h:280
Definition: CODEGenericBody.h:71
chai3d::cMatrix3d m_rotOffsetDynColModel
Definition: CODEGenericBody.h:347
int buildMeshTable(chai3d::cMultiMesh *a_multiMesh)
This method builds table of triangles and vertices for ODE multiMesh representation.
Definition: CODEGenericBody.cpp:953
virtual void setLocalPos(const chai3d::cVector3d &a_position)
This method sets the local position of object.
Definition: CODEGenericBody.cpp:142
virtual void buildDynamicModel()
This method creates a dynamic model of the object for modeling collisions between objects...
Definition: CODEGenericBody.h:186
chai3d::cVector3d m_posOffsetDynColModel
Definition: CODEGenericBody.h:346
void disableDynamics()
This method disables dynamics for this object.
Definition: CODEGenericBody.cpp:1072
dMass m_ode_mass
ODE body mass matrix.
Definition: CODEGenericBody.h:315
void setLocalPos(const double a_x=0.0, const double a_y=0.0, const double a_z=0.0)
This method sets the local position of this object.
Definition: CODEGenericBody.h:124
void setImageModel(chai3d::cGenericObject *a_imageModel)
This method sets a CHAI3D body image for this object.
Definition: CODEGenericBody.cpp:399
void createDynamicBox(const double a_lengthX, const double a_lengthY, const double a_lengthZ, const bool a_staticObject=false, const chai3d::cVector3d &a_offsetPos=chai3d::cVector3d(0.0, 0.0, 0.0), const chai3d::cMatrix3d &a_offsetRot=chai3d::cIdentity3d())
This method creates a dynamic box representation for modeling collisions between objects.
Definition: CODEGenericBody.cpp:561
double m_mass
Mass of object units: [kg].
Definition: CODEGenericBody.h:318
double m_paramDynColModel0
Definition: CODEGenericBody.h:343
void initialize(cODEWorld *a_world)
This method initializes the ODE object.
Definition: CODEGenericBody.cpp:64
double m_paramDynColModel1
Definition: CODEGenericBody.h:344
void createDynamicSphere(const double a_radius, const bool a_staticObject=false, const chai3d::cVector3d &a_offsetPos=chai3d::cVector3d(0.0, 0.0, 0.0), const chai3d::cMatrix3d &a_offsetRot=chai3d::cIdentity3d())
This method creates a dynamic sphere representation for modeling collisions between objects...
Definition: CODEGenericBody.cpp:490
Definition: CODEGenericBody.h:72
bool getShowDynamicCollisionModel()
This method returns the display status of the collision model.
Definition: CODEGenericBody.h:258
void setMass(const double a_mass)
This method assigns a value for the mass of this object.
Definition: CODEGenericBody.cpp:121
int * m_vertexIndices
ODE indices (for triangle mesh models).
Definition: CODEGenericBody.h:324
void addExternalForce(const chai3d::cVector3d &a_force)
This method applies an external force at the center of mass of this object.
Definition: CODEGenericBody.cpp:230
virtual void updateGlobalPositions(const bool a_frameOnly)
This method updates the global position frame of this object.
Definition: CODEGenericBody.cpp:1176
virtual bool computeCollisionDetection(const chai3d::cVector3d &a_segmentPointA, const chai3d::cVector3d &a_segmentPointB, chai3d::cCollisionRecorder &a_recorder, chai3d::cCollisionSettings &a_settings)
This method computes all collisions between a segment passed as argument and the body image of this o...
Definition: CODEGenericBody.cpp:320
float * m_vertices
ODE vertices (for triangle mesh models) - Do not use doubles since not supported under ODE! ...
Definition: CODEGenericBody.h:321
dBodyID m_ode_body
ODE body.
Definition: CODEGenericBody.h:274
void createStaticPlane(const chai3d::cVector3d &a_position, const chai3d::cVector3d &a_normal)
This method creates a static plane representation for modeling collisions between objects...
Definition: CODEGenericBody.cpp:772
void enableDynamics()
This method enables dynamics for this object.
Definition: CODEGenericBody.cpp:1058
bool m_static
if true then ODE object is static and does not move.
Definition: CODEGenericBody.h:312
Definition: CODEGenericBody.h:75
cODEDynamicModelType m_typeDynamicCollisionModel
Dynamic model type.
Definition: CODEGenericBody.h:336
virtual void render(chai3d::cRenderOptions &a_options)
This method renders the object graphically using OpenGL.
Definition: CODEGenericBody.cpp:1087
cMatrix3d cIdentity3d(void)
void setShowDynamicCollisionModel(const bool a_show)
This method displays or hides the collision model.
Definition: CODEGenericBody.h:255
chai3d::cGenericObject * getImageModel()
This method returns a pointer to the CHAI3D body image.
Definition: CODEGenericBody.h:252
virtual void setLocalRot(const chai3d::cMatrix3d &a_rotation)
This method sets the orientation of this object.
Definition: CODEGenericBody.cpp:171
chai3d::cGenericObject * m_imageModel
Object used to represent the geometry and graphical properties of the object.
Definition: CODEGenericBody.h:271
void createDynamicMesh(const bool a_staticObject=false, const chai3d::cVector3d &a_offsetPos=chai3d::cVector3d(0.0, 0.0, 0.0), const chai3d::cMatrix3d &a_offsetRot=chai3d::cIdentity3d())
This method creates a triangle mesh representation for modeling collisions between objects...
Definition: CODEGenericBody.cpp:827
Definition: CODEGenericBody.h:74
double getMass() const
This method returns the mass value for this object.
Definition: CODEGenericBody.h:174
Definition: CODEGenericBody.h:73
cODEDynamicModelType
ODE geometry used for dynamic collision computation.
Definition: CODEGenericBody.h:69
void updateBodyPosition(void)
This method updates the position and orientation from the ODE model to CHAI3D model.
Definition: CODEGenericBody.cpp:255
dGeomID m_ode_geom
ODE body geometry.
Definition: CODEGenericBody.h:277
void addExternalTorque(const chai3d::cVector3d &a_torque)
This method applies an external torque at the center of mass of of this object.
Definition: CODEGenericBody.cpp:243
void createDynamicCylinder(const double a_radius, const double a_length, const bool a_staticObject=false, const chai3d::cVector3d &a_offsetPos=chai3d::cVector3d(0.0, 0.0, 0.0), const chai3d::cMatrix3d &a_offsetRot=chai3d::cIdentity3d())
This method creates a dynamic caped cylinder representation for modeling collisions between objects...
Definition: CODEGenericBody.cpp:704
dTriMeshDataID m_ode_triMeshDataID
ODE triangle mesh ID.
Definition: CODEGenericBody.h:330
cODEGenericBody(cODEWorld *a_world)
Constructor of cODEGenericBody.
Definition: CODEGenericBody.h:100
cODEWorld * m_ODEWorld
Parent world.
Definition: CODEGenericBody.h:268
bool isStatic() const
This method returns true if the object is dynamically static, false otherwise.
Definition: CODEGenericBody.h:183
void copyfrom(const cMatrix3d &a_source)
This class implements an ODE dynamic world.
Definition: CODEWorld.h:75
bool m_showDynamicCollisionModel
Enable/Disable graphical representation of collision model.
Definition: CODEGenericBody.h:333
void createDynamicCapsule(const double a_radius, const double a_length, const bool a_staticObject=false, const chai3d::cVector3d &a_offsetPos=chai3d::cVector3d(0.0, 0.0, 0.0), const chai3d::cMatrix3d &a_offsetRot=chai3d::cIdentity3d())
This method creates a dynamic capsule representation for modeling collisions between objects...
Definition: CODEGenericBody.cpp:633