world.h
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 #ifndef WORLD_H
21 #define WORLD_H
22 
23 #include "worldsimconfig.h"
24 #include "wobject.h"
25 #include "wquaternion.h"
26 #include <QString>
27 #include <QHash>
28 #include <QSet>
29 #include <QList>
30 #include <QLinkedList>
31 #include <QVector>
32 #include <QPair>
33 #include <QEvent>
34 #include <QTimerEvent>
35 #include <QColor>
36 #include "simpletimer.h"
37 
38 namespace farsa {
39 
40 class World;
41 class WorldPrivate;
42 class PhyObject;
43 class PhyJoint;
44 class MotorController;
45 
46 //--- this function is required by QSet<NObj> attribute of World class
47 inline uint qHash( const QPair<PhyObject*, PhyObject*>& pair ) {
48 #ifdef _LP64
49  return ( ::qHash( (long int)(pair.first) ) );
50 #else
51  return ( ::qHash( (int)(pair.first) ) );
52 #endif
53 }
54 
63 class FARSA_WSIM_API MaterialDB {
64 public:
66  World* world() {
67  return worldv;
68  };
69 
73  bool createMaterial( QString name );
74 
80  void setFrictions( QString mat1, QString mat2, real st, real kn );
81 
85  void setElasticity( QString mat1, QString mat2, real );
86 
90  void setSoftness( QString mat1, QString mat2, real );
94  void setGravityForce( QString mat, real force );
97  real gravityForce( QString mat );
98 
103  void enableCollision( QString mat1, QString mat2, bool enable = true );
104 
112  void setProperties( QString mat1, QString mat2, real fs, real fk, real el, real sf, bool en = true );
113 
114 private:
116  MaterialDB( World* );
118  class materialPairInfo {
119  public:
120  real staticFriction;
121  real dynamicFriction;
122  real elasticity;
123  real softness;
124  bool collisions;
125  };
127  QSet<QString> mats;
129  QMap<QString, real> gravities;
130 
141  QHash<QString, materialPairInfo> pmap;
142 
144  QString createKey( QString mat1, QString mat2 );
145 
147  World* worldv;
148  friend class World;
149 };
150 
151 
162 class FARSA_WSIM_TEMPLATE Contact {
163 public:
174 };
175 
177 typedef QVector<Contact> contactVec;
179 typedef QHash<PhyObject*, contactVec> contactMap;
181 typedef QHashIterator<PhyObject*, contactVec> contactMapIterator;
182 
183 
195 class FARSA_WSIM_TEMPLATE RayCastHit {
196 public:
208 };
209 
211 typedef QVector<RayCastHit> rayCastHitVector;
212 
223 class FARSA_WSIM_API World : public QObject {
224  Q_OBJECT
225 public:
229  World( QString worldname );
231  virtual ~World();
233  QString name() const;
235  real elapsedTime() const;
237  void resetElapsedTime();
239  void setTimeStep( real );
242  void setMinimumFrameRate( unsigned int frames );
244  void setIsRealTime( bool b );
246  bool isRealTime() const;
248  real timeStep() const;
250  void setGravityForce( real g );
252  real gravityForce() const;
254  const QLinkedList<WObject*> objects();
256  WObject* getObject( QString name );
258  const QLinkedList<PhyJoint*> joints();
260  const QHash<WObject*, QList<PhyJoint*> > mapObjectsToJoints();
261 
266  void advanceUntil( real time );
267 
273  void disableCollision( PhyObject* obj1, PhyObject* obj2 );
275  void enableCollision( PhyObject* obj1, PhyObject* obj2 );
277  const contactMap& contacts();
286  bool closestPoints( PhyObject* objA, PhyObject* objB, wVector& pointA, wVector& pointB );
296  bool checkContacts( PhyObject* obj1, PhyObject* obj2, int maxContacts = 4, QVector<wVector>* contacts = NULL, QVector<wVector>* normals = NULL, QVector<real>* penetra = NULL );
307  bool smartCheckContacts( PhyObject* obj1, PhyObject* obj2, int maxContacts = 4, QVector<wVector>* contacts = NULL );
318  real collisionRayCast( PhyObject* obj, wVector start, wVector end, wVector* normal = NULL );
331  rayCastHitVector worldRayCast( wVector start, wVector end, bool onlyClosest, const QSet<PhyObject*>& ignoredObjs = QSet<PhyObject*>() );
332 
335  return (*mats);
336  };
337 
339  void setSolverModel( QString model );
341  void setFrictionModel( QString model );
343  void setMultiThread( int numThreads );
345  void setSize( const wVector& minPoint, const wVector& maxPoint );
347  void size( wVector &minPoint, wVector &maxPoint );
348 
353  void pushObject( WObject* );
358  void popObject( WObject* );
359 
364  void pushJoint( PhyJoint* );
369  void popJoint( PhyJoint* );
371  enum w_state { playingS, pausedS, stoppedS };
374  return state;
375  };
377  void cleanUpMemory();
378 public slots:
380  void initialize();
387  void advance();
389  void finalize();
391  int play();
393  void pause();
395  void stop();
396 signals:
398  void initialized();
400  void finished();
402  void advanced();
404  void paused();
406  void stopped();
408  void addedObject( WObject* );
410  void addedJoint( PhyJoint* );
412  void removedObject( WObject* );
414  void removedJoint( PhyJoint* );
416  void resized();
417 protected:
425  wVector minP, maxP;
435  QLinkedList<WObject*> objs;
436 
439  QLinkedList<PhyJoint*> jointsv;
441  QHash<WObject*, QList<PhyJoint*> > mapObjJoints;
442 
444  void timerEvent( QTimerEvent* e );
446  void customEvent( QEvent* e );
447 public:
449  enum { E_Advance = 1200, E_Play = 1201, E_Stop = 1202, E_Pause = 1203 };
450 private:
452  QString namev;
453 
455  contactMap cmap;
456 
458  typedef QPair<PhyObject*, PhyObject*> NObj;
460  QSet<NObj> nobjs;
461 
463  MaterialDB* mats;
464  friend class MaterialDB;
465 
467  SimpleTimer timer;
469  bool isrealtimev;
471  bool isInit;
473  bool isFinish;
475  w_state state;
477  unsigned int timerId;
478 
480  WorldPrivate* priv;
481  friend class PhyObject;
482  friend class PhyJoint;
483  friend class WorldPrivate;
484 };
485 
486 } // end namespace farsa
487 
488 #endif
Raycast intersection class.
Definition: world.h:195
the MaterialDB class managea the material properties
Definition: world.h:63
World's Object class.
Definition: wobject.h:39
PhyObject * collide
Collide Object.
Definition: world.h:167
wVector worldPos
position in the global coordinate
Definition: world.h:171
QHash< WObject *, QList< PhyJoint * > > mapObjJoints
Map object -> joints.
Definition: world.h:441
real timestepv
The time step.
Definition: world.h:421
real distance
The distance from start at which the ray intersects the object.
Definition: world.h:202
real time
The elapsed simulation-time from last calls of initialize()
Definition: world.h:419
wVector normal
The normal at the hit point in the global frame of reference.
Definition: world.h:207
real gforce
Gravity Force.
Definition: world.h:423
QLinkedList< WObject * > objs
Vector of WObject inside the World.
Definition: world.h:435
World class.
Definition: world.h:223
Contact class.
Definition: world.h:162
World * world()
return the world associate with these materials
Definition: world.h:66
w_state status()
return the actual state
Definition: world.h:373
PhyJoint class.
Definition: phyjoint.h:359
wVector minP
Size of the Newton world.
Definition: world.h:425
wVector pos
position of the contact in the local object coordinates
Definition: world.h:169
w_state
define the state of the World
Definition: world.h:371
float real
MaterialDB & materials()
return the MaterialDB object for managing World's materials
Definition: world.h:334
PhyObject * object
Object intersected by the ray.
Definition: world.h:198
wVector force
the normal restitution force at the contact point
Definition: world.h:173
wVector position
The actual point at which the ray intersects the object in the global frame of reference.
Definition: world.h:205
QLinkedList< PhyJoint * > jointsv
Vector of Joints inside the World.
Definition: world.h:439
PhyObject class.
Definition: phyobject.h:46
PhyObject * object
Reference Object.
Definition: world.h:165