V-ART
joint.cpp
Go to the documentation of this file.
1 
5 #include <algorithm>
6 #include <cassert>
7 #include "vart/joint.h"
8 #include "vart/dof.h"
9 
10 #ifdef WIN32
11 #include <windows.h>
12 #endif
13 #ifdef VART_OGL
14 #include <GL/gl.h>
15 #endif
16 
17 using namespace std;
18 
20 {
21 }
22 
24 {
25  this->operator=(j);
26 }
27 
29 {
30  VART::Joint * copy;
31 
32  copy = new VART::Joint(*this);
33  copy->CopyDofListFrom(*this);
34  return copy;
35 }
36 
38 {
39  std::list<Dof*>::iterator iter;
40  VART::Dof* dof;
41 
42  dofList.clear();
43  for( iter = joint.dofList.begin(); iter != joint.dofList.end(); iter++ )
44  {
45  dof = new VART::Dof(**iter);
46  dof->SetOwnerJoint( this );
47  AddDof( dof );
48  }
49 }
50 
52 {
53  // Deallocate all Dofs marked as autoDelete
54  list<VART::Dof*>::iterator iter;
55  for (iter = dofList.begin(); iter != dofList.end(); ++iter)
56  {
57  if ((*iter)->autoDelete)
58  delete *iter;
59  }
60 }
61 
63 {
64  this->Transform::operator=(j);
65  dofList = j.dofList;
66  return *this;
67 }
68 
69 VART::Dof* VART::Joint::AddDof(const Point4D& vec, const Point4D& pos, float min, float max)
70 {
71  Dof* dofPtr = new Dof(vec, pos, min, max);
72  dofPtr->SetOwnerJoint(this);
73  dofList.push_back(dofPtr);
74  return dofPtr;
75 }
76 
78 {
79  dofList.push_back(dof);
80  dof->SetOwnerJoint(this);
81 }
82 
84 // In visual mode, the lim is not really used
85 {
86 // LIM = ...DOF3 * DOF2 * DOF1
87  list<VART::Dof*>::reverse_iterator iter = dofList.rbegin();
88 
89  // Copy DOF1's matrix to this object
90  (*iter)->GetLim(this);
91  ++iter;
92  while (iter != dofList.rend())
93  {
94  // this = dof * this
95  (*iter)->ApplyTransformTo(this);
96  ++iter;
97  }
98 }
99 
101 {
102  assert(static_cast<int>(dofList.size()) > dof);
103  list<VART::Dof*>::const_iterator iter = dofList.begin();
104  int position = 0;
105  while (position < dof)
106  {
107  ++position;
108  ++iter;
109  }
110  return **iter;
111 }
112 
113 void VART::Joint::GetDofs(std::list<Dof*>* dofListPtr)
114 // Warning: this method exposes the dofs of a joint. It has been created for IKChain's constructor.
115 // An IK chain is a sequence of DOFs that need to need in order to make an articulated object reach
116 // a certain state, therefore, DOFs must not be constant. I'm not sure if there is a better way to
117 // deal with encapsulation of the joint class in this case (perhaps making it a friend of IKChain?).
118 {
119  list<Dof*>::iterator iter = dofList.begin();
120  while (iter != dofList.end())
121  {
122  dofListPtr->push_back(*iter);
123  ++iter;
124  }
125 }
126 
128 {
129  list<Dof*>::const_iterator iter = dofList.begin();
130  unsigned int dofIndex = 0;
131 
132  while ((iter != dofList.end()) && (*iter != dofPtr))
133  {
134  ++iter;
135  ++dofIndex;
136  }
137  assert(iter != dofList.end());
138  return static_cast<DofID>(dofIndex);
139 }
140 
142 {
143  list<VART::Dof*>::iterator iter;
144  for(iter = dofList.begin(); iter != dofList.end(); ++iter)
145  (*iter)->Rest();
146 }
147 
148 bool VART::Joint::MoveDof(DofID dof, float variance)
149 {
150  unsigned int dofNum = static_cast<unsigned int>(dof);
151 
152  if (dofNum < dofList.size())
153  {
154  list<VART::Dof*>::iterator iter = dofList.begin();
155  while (dofNum > 0)
156  {
157  ++iter;
158  --dofNum;
159  }
160  (*iter)->Move(variance);
161  return true;
162  }
163  else
164  return false;
165 }
166 
168 {
169  return static_cast<unsigned int>(dof) < dofList.size();
170 }
171 
172 #ifdef VISUAL_JOINTS
173 bool VART::Joint::DrawOGL() const
174 {
175 #ifdef VART_OGL
176  bool result = true;
177  list<VART::SceneNode*>::const_iterator iter;
178  list<VART::Dof*>::const_iterator dofIter;
179  int i = 0;
180 
181  glPushMatrix();
182 
183  for (dofIter = dofList.begin(); dofIter != dofList.end(); ++dofIter)
184  {
185  glMultMatrixd((*dofIter)->GetLim().GetData());
186  GetMaterial(i).DrawOGL();
187  (*dofIter)->DrawInstanceOGL();
188  ++i;
189  }
190  for (iter = childList.begin(); iter != childList.end(); ++iter)
191  result &= (*iter)->DrawOGL();
192  glPopMatrix();
193  return result;
194 #else
195  return false;
196 #endif // VART_OGL
197 }
198 
199 const VART::Material& VART::Joint::GetMaterial(int num)
200 {
201  static VART::Material red(VART::Color::RED());
202  static VART::Material green(VART::Color::GREEN());
203  static VART::Material blue(VART::Color::BLUE());
204  switch (num)
205  {
206  case 0:
207  return red;
208  break;
209  case 1:
210  return green;
211  break;
212  default:
213  return blue;
214  }
215 }
216 #endif // VISUAL_JOINTS
217 
218 void VART::Joint::XmlPrintOn(ostream& os, unsigned int indent) const
219 // virtual method
220 {
221  list<Dof*>::const_iterator dofIter = dofList.begin();
222  list<SceneNode*>::const_iterator iter = childList.begin();
223  string indentStr(indent,' ');
224 
225  os << indentStr << "<joint description=\"" << description << "\" type=\"";
226  switch (GetNumDofs())
227  {
228  case 1:
229  os << "uniaxial";
230  break;
231  case 2:
232  os << "biaxial";
233  break;
234  default:
235  os << "poliaxial";
236  }
237  os << "\">\n";
238  while (dofIter != dofList.end())
239  {
240  (*dofIter)->XmlPrintOn(os, indent+2);
241  ++dofIter;
242  }
243  if (recursivePrinting)
244  while (iter != childList.end())
245  {
246  (*iter)->XmlPrintOn(os, indent+2);
247  ++iter;
248  }
249  os << indentStr << "</joint>\n";
250  os << flush;
251 }
252 
253 ostream& VART::operator<<(ostream& output, const VART::Joint::DofID& dofId)
254 {
255  switch (dofId)
256  {
258  output << "FLEXION";
259  break;
261  output << "ADDUCTION";
262  break;
263  default:
264  output << "TWIST";
265  break;
266  }
267  return output;
268 }
269 
270 istream& VART::operator>>(istream& input, VART::Joint::DofID& dofId)
271 {
272  string buffer;
273  input >> buffer;
274  if (buffer == "FLEXION")
275  dofId = VART::Joint::FLEXION;
276  else
277  {
278  if (buffer == "ADDUCTION")
279  dofId = VART::Joint::ADDUCTION;
280  else
281  {
282  if (buffer == "TWIST")
283  dofId = VART::Joint::TWIST;
284  else
285  input.setstate(ios_base::failbit);
286  }
287  }
288  return input;
289 }
const Joint & operator=(const Joint &j)
Definition: joint.cpp:62
std::istream & operator>>(std::istream &input, Joint::DofID &dofId)
Base class for objects that compose a scene graph.
Definition: scenenode.h:25
static const Color & RED()
Red opaque color.
Definition: color.cpp:75
Header file for V-ART class "Dof".
Representation of joints.
Definition: joint.h:34
static const Color & BLUE()
Blue opaque color.
Definition: color.cpp:85
Points and vectors using homogeneous coordinates.
Definition: point4d.h:22
std::list< Dof * > dofList
Definition: joint.h:118
static const Color & GREEN()
Green opaque color.
Definition: color.cpp:80
std::ostream & operator<<(std::ostream &output, const Joint::DofID &dofId)
bool HasDof(DofID dof)
Checks whether a DOF exists in the joint.
Definition: joint.cpp:167
void SetOwnerJoint(Joint *ow)
Definition: dof.cpp:316
void MakeLim()
Updates the LIM, based on DOFs' situation.
Definition: joint.cpp:83
bool MoveDof(DofID dof, float variance)
Moves a joint's DOF.
Definition: joint.cpp:148
Header file for V-ART class "Joint".
virtual Dof * AddDof(const Point4D &vec, const Point4D &pos, float min, float max)
Adds a Dof.
Definition: joint.cpp:69
virtual void CopyDofListFrom(VART::Joint &joint)
Copy the dofList atribute from an joint, setting the ownerJoint atribute of copied dof to 'this' join...
Definition: joint.cpp:37
Material properties for graphical objects.
Definition: material.h:16
virtual bool DrawOGL() const
Apply transform to rendering engine.
Definition: transform.cpp:260
void SetAtRest()
Put the joint in a rest position.
Definition: joint.cpp:141
virtual VART::SceneNode * Copy()
Returns a copy of a Joint. Every derived class must reimplement this method, to avoid errors with VAR...
Definition: joint.cpp:28
void GetDofs(std::list< Dof * > *dofListPtr)
Returns all DOFs.
Definition: joint.cpp:113
Joint()
Creates an uninitialized joint.
Definition: joint.cpp:19
virtual ~Joint()
Definition: joint.cpp:51
DofID GetDofID(const Dof *dofPtr) const
Returns the DofID of some member Dof.
Definition: joint.cpp:127
const Dof & GetDof(DofID dof) const
Returns a joint's DOF.
Definition: joint.cpp:100
Degree Of Freedom - basic component of a Joint.
Definition: dof.h:28
virtual void XmlPrintOn(std::ostream &os, unsigned int indent) const
Outputs XML representation of the scene.
Definition: joint.cpp:218