CGELSkeletonNode.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: 1869 $
41 */
42 //===========================================================================
43 
44 //---------------------------------------------------------------------------
45 #ifndef CGELSkeletonNodeH
46 #define CGELSkeletonNodeH
47 //---------------------------------------------------------------------------
48 #include "chai3d.h"
49 //---------------------------------------------------------------------------
50 
51 //===========================================================================
58 //===========================================================================
59 
60 //===========================================================================
68 //===========================================================================
70 {
71  //-----------------------------------------------------------------------
72  // CONSTRUCTOR & DESTRUCTOR:
73  //-----------------------------------------------------------------------
74 
75 public:
76 
79 
82 
83 
84  //-----------------------------------------------------------------------
85  // PUBLIC METHODS:
86  //-----------------------------------------------------------------------
87 
88 public:
89 
91  void setMass(double a_mass);
92 
94  inline void addForce(chai3d::cVector3d &a_force)
95  {
96  m_force.add(a_force);
97  }
98 
100  inline void addTorque(chai3d::cVector3d &a_torque)
101  {
102  m_torque.add(a_torque);
103  }
104 
106  inline void setExternalForce(chai3d::cVector3d &a_force)
107  {
108  m_externalForce = a_force;
109  }
110 
112  inline void setExternalTorque(chai3d::cVector3d &a_torque)
113  {
114  m_externalTorque = a_torque;
115  }
116 
118  inline void computeNextPose(double a_timeInterval)
119  {
120  if (!m_fixed)
121  {
122  // Euler double integration for position
123  chai3d::cVector3d damping;
124  m_vel.mulr(-m_kDampingPos * m_mass, damping);
125  m_force.add(damping);
126  m_acc = cDiv(m_mass, cAdd(m_force, m_externalForce));
127  m_vel = cAdd(m_vel, cMul(a_timeInterval, m_acc));
128  m_nextPos = cAdd(m_pos, cMul(a_timeInterval, m_vel));
129  }
130  else
131  {
132  m_nextPos = m_pos;
133  }
134 
135  // Euler double integration for rotation
136  chai3d::cVector3d dampingAng;
137  m_angVel.mulr(-m_kDampingRot * m_mass, dampingAng);
138  m_torque.add(dampingAng);
140  m_angVel = cAdd(m_angVel, cMul(a_timeInterval, m_angAcc));
141 
142  double normAngVel = m_angVel.length();
143  if (normAngVel < 0.000001)
144  {
145  m_nextRot = m_rot;
146  }
147  else
148  {
149  m_nextRot = m_rot;
150  m_nextRot.rotateAboutGlobalAxisRad(m_angVel, a_timeInterval * normAngVel);
151  }
152  }
153 
155  inline void applyNextPose()
156  {
157  m_pos = m_nextPos;
158  m_rot = m_nextRot;
159  }
160 
162  inline void clearForces()
163  {
164  if (m_useGravity)
165  {
166  m_force = m_gravity;
167  m_force.mul(m_mass);
168  }
169  else
170  {
171  m_force.zero();
172  }
173  m_torque.zero();
174  }
175 
177  inline void clearExternalForces()
178  {
179  m_externalForce.zero();
180  m_externalTorque.zero();
181  }
182 
184  inline void render()
185  {
186  // set pose
187  chai3d::cTransform mat;
188  mat.set(m_pos, m_rot);
189  glPushMatrix();
190  glMultMatrixd( (const double *)mat.getData() );
191 
192  // draw node
193  m_color.render();
194  chai3d::cDrawSphere(m_radius, 12, 12);
195 
196  // draw frame
197  if (s_default_showFrame == true)
198  {
199  double frameScale = 3.0 * m_radius;
200  chai3d::cDrawFrame(frameScale);
201  }
202 
203  // pop OpenGL matrix
204  glPopMatrix();
205 
206  // render external forces
207  glColor4fv( (const float *)&m_color);
208  chai3d::cVector3d v = cAdd(m_pos, cMul(1.0/50.0, m_externalForce));
209  glBegin(GL_LINES);
210  glVertex3dv( (const double *)&m_pos);
211  glVertex3dv( (const double *)&v);
212  glEnd();
213  }
214 
215 
216  //-----------------------------------------------------------------------
217  // PUBLIC MEMBERS - GRAPHICAL PROPERTIES:
218  //-----------------------------------------------------------------------
219 
220 public:
221 
224 
225 
226  //-----------------------------------------------------------------------
227  // PUBLIC MEMBERS - PHYSICAL PROPERTIES:
228  //-----------------------------------------------------------------------
229 
230 public:
231 
233  double m_radius;
234 
236  double m_mass;
237 
240 
243 
246 
249 
252 
255 
258 
261 
263  double m_inertia;
264 
266  bool m_fixed;
267 
270 
273 
274 
275  //-----------------------------------------------------------------------
276  // PUBLIC MEMBERS - POSITION & ORIENTATION:
277  //-----------------------------------------------------------------------
278 
279 public:
280 
283 
286 
289 
292 
293 
294  //-----------------------------------------------------------------------
295  // PRIVATE MEMBERS - EXTERNAL FORCES:
296  //-----------------------------------------------------------------------
297 
298 private:
299 
301  chai3d::cVector3d m_externalForce;
302 
304  chai3d::cVector3d m_externalTorque;
305 
306 
307  //-----------------------------------------------------------------------
308  // STATIC MEMBERS - DEFAULT SETTINGS:
309  //-----------------------------------------------------------------------
310 
311 public:
312 
314  static double s_default_radius;
315 
317  static double s_default_kDampingPos;
318 
320  static double s_default_kDampingRot;
321 
323  static double s_default_mass;
324 
326  static bool s_default_useGravity;
327 
330 
332  static bool s_default_showFrame;
333 
336 };
337 
338 //---------------------------------------------------------------------------
339 #endif
340 //---------------------------------------------------------------------------
341 
342 
chai3d::cVector3d m_angAcc
Instant angular acceleration at node.
Definition: CGELSkeletonNode.h:248
void setMass(double a_mass)
This method sets the mass of the node.
Definition: CGELSkeletonNode.cpp:114
void set(const cVector3d &a_pos, const cMatrix3d &a_rot)
void computeNextPose(double a_timeInterval)
This method update the position of the node over a time interval.
Definition: CGELSkeletonNode.h:118
double m_mass
Mass property.
Definition: CGELSkeletonNode.h:236
chai3d::cVector3d m_nextPos
Next position computed.
Definition: CGELSkeletonNode.h:288
double m_kDampingRot
Angular damping.
Definition: CGELSkeletonNode.h:260
void clearForces()
This method clears the current forces and torques.
Definition: CGELSkeletonNode.h:162
void setExternalTorque(chai3d::cVector3d &a_torque)
This method sets an external torque to the node.
Definition: CGELSkeletonNode.h:112
chai3d::cVector3d m_gravity
Gravity field.
Definition: CGELSkeletonNode.h:272
static chai3d::cColorf s_default_color
Default color used to render the node.
Definition: CGELSkeletonNode.h:335
double * getData()
cVector3d cAdd(const cVector3d &a_vector1, const cVector3d &a_vector2)
static bool s_default_showFrame
Default value - Is the node reference frame displayed?
Definition: CGELSkeletonNode.h:332
void applyNextPose()
This method updates the position and orientation of this node with the latest computed values...
Definition: CGELSkeletonNode.h:155
void cDrawSphere(const double &a_radius, const unsigned int a_numSlices=10, const unsigned int a_numStacks=10)
double length() const
chai3d::cVector3d m_force
Current force applied on node.
Definition: CGELSkeletonNode.h:239
double m_inertia
Inertia (to be completed).
Definition: CGELSkeletonNode.h:263
static bool s_default_useGravity
Default value - Is gravity field enabled?
Definition: CGELSkeletonNode.h:326
void mulr(const double &a_scalar, cVector3d &a_result) const
void setExternalForce(chai3d::cVector3d &a_force)
This method sets an external force to the node.
Definition: CGELSkeletonNode.h:106
This class implements a mass node with mass and inertia properties.
Definition: CGELSkeletonNode.h:69
bool m_fixed
If true, then mass is fixed in space and can not move.
Definition: CGELSkeletonNode.h:266
~cGELSkeletonNode()
Destructor of cGELSkeletonNode.
Definition: CGELSkeletonNode.cpp:101
void addForce(chai3d::cVector3d &a_force)
This method adds a force to the node.
Definition: CGELSkeletonNode.h:94
chai3d::cMatrix3d m_nextRot
Next rotation computed.
Definition: CGELSkeletonNode.h:291
cGELSkeletonNode()
Constructor of cGELSkeletonNode.
Definition: CGELSkeletonNode.cpp:74
cVector3d cDiv(const double &a_value, const cVector3d &a_vector)
double m_radius
Radius of mass node.
Definition: CGELSkeletonNode.h:233
chai3d::cVector3d m_vel
Instant velocity at node.
Definition: CGELSkeletonNode.h:251
chai3d::cVector3d m_torque
Current torque applies on node.
Definition: CGELSkeletonNode.h:242
cVector3d cMul(const double &a_value, const cVector3d &a_vector)
chai3d::cColorf m_color
Color used to display nodes.
Definition: CGELSkeletonNode.h:223
void cDrawFrame(const double &a_scale=1.0)
double m_kDampingPos
Linear damping.
Definition: CGELSkeletonNode.h:257
void clearExternalForces()
This method clears the external forces and torques.
Definition: CGELSkeletonNode.h:177
chai3d::cVector3d m_angVel
Instant angular velocity at node.
Definition: CGELSkeletonNode.h:254
chai3d::cVector3d m_pos
Position computed.
Definition: CGELSkeletonNode.h:282
bool rotateAboutGlobalAxisRad(const cVector3d &a_axis, const double &a_angleRad)
bool m_useGravity
If true, then gravity is enabled.
Definition: CGELSkeletonNode.h:269
static double s_default_mass
Default value for node mass.
Definition: CGELSkeletonNode.h:323
chai3d::cMatrix3d m_rot
Rotation computed.
Definition: CGELSkeletonNode.h:285
static double s_default_kDampingRot
Default value for revolute damping.
Definition: CGELSkeletonNode.h:320
void mul(const double &a_scalar)
void add(const cVector3d &a_vector)
void render() const
static double s_default_radius
Default value for node radius.
Definition: CGELSkeletonNode.h:314
void addTorque(chai3d::cVector3d &a_torque)
This method adds a torque to the node.
Definition: CGELSkeletonNode.h:100
void render()
This method renders this node in OpenGL.
Definition: CGELSkeletonNode.h:184
chai3d::cVector3d m_acc
Instant acceleration at node.
Definition: CGELSkeletonNode.h:245
static double s_default_kDampingPos
Default value for linear damping.
Definition: CGELSkeletonNode.h:317
static chai3d::cVector3d s_default_gravity
Default value - Gravity field magnitude and direction.
Definition: CGELSkeletonNode.h:329