phycylinder.cpp
1 /********************************************************************************
2  * WorldSim -- library for robot simulations *
3  * Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #include "phycylinder.h"
21 #include "private/phyobjectprivate.h"
22 #include "private/worldprivate.h"
23 #include <QtAlgorithms>
24 
25 namespace farsa {
26 
27 PhyCylinder::PhyCylinder( real radius, real height, World* w, QString name, const wMatrix& tm ) :
28  PhyObject( w, name, tm, false ),
29  radiusv(radius),
30  heightv(height),
31  m_upperBaseColor(),
32  m_lowerBaseColor(),
33  m_segmentsColor(),
34  m_graphicalRepresentationNeedsUpdate(true),
35  m_oldColorv()
36 {
37  w->pushObject( this );
39  changedMatrix();
40 }
41 
43 #ifdef WORLDSIM_USE_NEWTON
44  /* nothing to do */
45 #endif
46 }
47 
49 {
50  m_upperBaseColor = color;
51 
52  if (colorv.isValid()) {
53  m_lowerBaseColor = colorv;
54  m_segmentsColor.append(SegmentColor(0.0, colorv));
55  colorv = QColor();
56  }
57 
58  m_graphicalRepresentationNeedsUpdate = true;
59 }
60 
62 {
63  m_lowerBaseColor = color;
64 
65  if (colorv.isValid()) {
66  m_upperBaseColor = colorv;
67  m_segmentsColor.append(SegmentColor(0.0, colorv));
68  colorv = QColor();
69  }
70 
71  m_graphicalRepresentationNeedsUpdate = true;
72 }
73 
74 void PhyCylinder::setSegmentsColor(QList<SegmentColor> segmentsColor)
75 {
76  m_segmentsColor = segmentsColor;
77  qSort(m_segmentsColor.begin(), m_segmentsColor.end());
78 
79  if (colorv.isValid()) {
80  m_upperBaseColor = colorv;
81  m_lowerBaseColor = colorv;
82  colorv = QColor();
83  }
84 
85  m_graphicalRepresentationNeedsUpdate = true;
86 }
87 
89 {
90  const bool ret = (m_graphicalRepresentationNeedsUpdate || (m_oldColorv != colorv));
91 
92  m_graphicalRepresentationNeedsUpdate = false;
93  m_oldColorv = colorv;
94 
95  return ret;
96 }
97 
99 #ifdef WORLDSIM_USE_NEWTON
100  NewtonCollision* c = NewtonCreateCylinder( worldpriv->world, radiusv, heightv, 1, 0 );
101  wMatrix initialTransformationMatrix = wMatrix::identity(); // The transformation matrix is set in other places
102  priv->body = NewtonCreateBody( worldpriv->world, c, &initialTransformationMatrix[0][0] );
103  priv->collision = c;
104  NewtonBodySetAutoSleep( priv->body, 0 );
105  setMass( 1 );
106  NewtonBodySetUserData( priv->body, this );
107  NewtonBodySetLinearDamping( priv->body, 0.0 );
108  wVector zero = wVector(0,0,0,0);
109  NewtonBodySetAngularDamping( priv->body, &zero[0] );
110  NewtonBodySetAutoSleep( priv->body, 0 );
111  NewtonBodySetFreezeState( priv->body, 0 );
112  // Sets the signal-wrappers callback
113  NewtonBodySetTransformCallback( priv->body, (PhyObjectPrivate::setTransformHandler) );
114  NewtonBodySetForceAndTorqueCallback( priv->body, (PhyObjectPrivate::applyForceAndTorqueHandler) );
115 #endif
116 }
117 
118 } // end namespace farsa