# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

primitive_world_image.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_PRIMITIVE_WORLD_IMAGE_H
00027 #define NL_PRIMITIVE_WORLD_IMAGE_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 
00031 #include "pacs/move_container.h"
00032 
00033 
00034 namespace NLPACS {
00035 
00036 
00043 class CPrimitiveWorldImage
00044 {
00045 public:
00046 
00047         // Constructor
00048         CPrimitiveWorldImage ();
00049 
00050         // To call before destruction
00051         void deleteIt (CMoveContainer &container, uint8 worldImage);
00052 
00053         // Copy
00054         void copy (const CPrimitiveWorldImage& source);
00055 
00061         void    setGlobalPosition (const UGlobalPosition& pos, CMoveContainer& container, CMovePrimitive &primitive, uint8 worldImage);
00062 
00070         void    setGlobalPosition (const NLMISC::CVectorD& pos, CMoveContainer& container, CMovePrimitive &primitive, 
00071                                                                 uint8 worldImage, bool keepZ = false);
00072 
00078         NLMISC::CVectorD        getFinalPosition () const
00079         {
00080                 // Get the position
00081                 return _Position.getPos();
00082         }
00083 
00089         void    setOrientation (double rot, CMoveContainer *container, CMovePrimitive *primitive, uint8 worldImage)
00090         {
00091                 // New position
00092                 _OBData.Orientation=rot;
00093 
00094                 // Position has changed
00095                 dirtPos (container, primitive, worldImage);
00096         }
00097 
00103         void    move (const NLMISC::CVectorD& speed, CMoveContainer& container, CMovePrimitive &primitive, 
00104                                         uint8 worldImage);
00105 
00111         void    setSpeed (const NLMISC::CVectorD& speed, CMoveContainer *container, CMovePrimitive *primitive, uint8 worldImage)
00112         {
00113                 // New time
00114                 _Speed=speed;
00115 
00116                 // Speed has changed
00117                 dirtPos (container, primitive, worldImage);
00118         }
00119 
00125         const NLMISC::CVectorD& getSpeed () const
00126         {
00127                 // New time
00128                 return _Speed;
00129         }
00130 
00132         bool    isInModifiedListFlag ()
00133         {
00134                 return (_DynamicFlags&InModifiedListFlag) != 0;
00135         }
00136 
00138         void    setInModifiedListFlag (bool itis)
00139         {
00140                 if (itis)
00141                         _DynamicFlags|=InModifiedListFlag;
00142                 else
00143                         _DynamicFlags&=~InModifiedListFlag;
00144         }
00145 
00146         // Link into modified list
00147         void    linkInModifiedList (CMovePrimitive* next)
00148         {
00149                 _NextModified=next;
00150         }
00151 
00153         bool    isInWorldImageFlag ()
00154         {
00155                 return (_DynamicFlags&InWorldImageFlag) != 0;
00156         }
00157 
00159         void    setInWorldImageFlag (bool itis)
00160         {
00161                 if (itis)
00162                         _DynamicFlags|=InWorldImageFlag;
00163                 else
00164                         _DynamicFlags&=~InWorldImageFlag;
00165         }
00166 
00168         CMovePrimitive  *getNextModified () const
00169         {
00170                 return _NextModified;
00171         }
00172 
00176         double  getBBXMin () const
00177         {
00178                 return _BBXMin;
00179         }
00180 
00184         double  getBBYMin () const
00185         {
00186                 return _BBYMin;
00187         }
00188 
00192         double  getBBXMax () const
00193         {
00194                 return _BBXMax;
00195         }
00196 
00200         double  getBBYMax () const
00201         {
00202                 return _BBYMax;
00203         }
00204 
00208         const NLMISC::CVectorD& getDeltaPosition () const
00209         {
00210                 return _DeltaPosition;
00211         }
00212 
00214         CMoveElement    *getMoveElement (uint i)
00215         {
00216                 return _MoveElement[i];
00217         }
00218 
00220         void removeMoveElement (uint i, CMoveContainer &container, uint8 worldImage);
00221 
00223         void addMoveElement (CMoveCell& cell, uint16 x, uint16 y, double centerX, double centerY, CMovePrimitive *primitive,
00224                                                 CMoveContainer &container, uint8 worldImage);
00225 
00227         void CPrimitiveWorldImage::addMoveElementendOfList (CMoveCell& cell, uint16 x, uint16 y, CMovePrimitive *primitive,
00228                                                                                                                 CMoveContainer &container);
00229 
00231         void update (double beginTime, double endTime, CMovePrimitive &primitive)
00232         {
00233                 // Pos dirt ?
00234                 if (_DynamicFlags&DirtPosFlag)
00235                 {
00236                         // Compute precalculated data
00237                         precalcPos (primitive);
00238 
00239                         // Clean
00240                         _DynamicFlags&=~DirtPosFlag;
00241                 }
00242 
00243                 // Bounding box dirt ?
00244                 if (_DynamicFlags&DirtBBFlag)
00245                 {
00246                         // Compute precalculated data
00247                         precalcBB (beginTime, endTime, primitive);
00248 
00249                         // Clean
00250                         _DynamicFlags&=~DirtBBFlag;
00251                 }
00252         }
00253 
00255         void checkSortedList (uint8 worldImage);
00256 
00257         // Return the global position of the primitive
00258         const UGlobalPosition& getGlobalPosition()
00259         {
00260                 return _Position.getGlobalPos();
00261         }
00262 
00263         // Return the orientation
00264         double getOrientation () const
00265         {
00266                 return _OBData.Orientation;
00267         }
00268 
00269         // Dirt the position flag. Position has changed.
00270         void    dirtPos (CMoveContainer *container, CMovePrimitive *primitive, uint8 worldImage)
00271         {
00272                 _DynamicFlags|=DirtPosFlag;
00273                 dirtBB (container, primitive, worldImage);
00274         }
00275 
00276         // Dirt the bounding box flag.
00277         void    dirtBB (CMoveContainer *container, CMovePrimitive *primitive, uint8 worldImage)
00278         {
00279                 // Warn container that BB has changed
00280                 container->changed (primitive, worldImage);
00281 
00282                 _DynamicFlags|=DirtBBFlag;
00283         }
00284 
00285         // Return the init time
00286         double  getInitTime() const
00287         {
00288                 return _InitTime;
00289         }
00290                 
00291         // Compute precalculated data for the position
00292         void precalcPos (CMovePrimitive &primitive);
00293 
00294         // Compute precalculated data for the speed
00295         void precalcSpeed ();
00296 
00297         // Compute precalculated bounding box
00298         void precalcBB (double beginTime, double endTime, CMovePrimitive &primitive);
00299 
00300         // *** Some methods to eval collisions
00301         /* * NOTES:             BB for bounding box
00302                                         OB for oriented Box
00303                                         OC for oriented cylinder
00304                                         P for box point
00305                                         S for box segment
00306         */
00307 
00319         bool    evalCollision (CPrimitiveWorldImage& other, class CCollisionDesc& desc, double timeMin, 
00320                                                                         double timeMax, uint32 testTime, uint32 maxTestIteration, 
00321                                                                         double &firstContactTime, double &lastContactTime, 
00322                                                                         CMovePrimitive& primitive, CMovePrimitive& otherPrimitive);
00323 
00324         // Box over box
00325         bool    evalCollisionOBoverOB (CPrimitiveWorldImage& other, CCollisionDesc& desc, double timeMin, 
00326                                                                         double timeMax, double &firstContactTime, double &lastContactTime, 
00327                                                                         CMovePrimitive& primitive, CMovePrimitive& otherPrimitive);
00328 
00329         // Box over cylinder
00330         bool    evalCollisionOBoverOC (CPrimitiveWorldImage& other, CCollisionDesc& desc, double timeMin, 
00331                                                                         double timeMax, double &firstContactTime, double &lastContactTime, 
00332                                                                         CMovePrimitive& primitive, CMovePrimitive& otherPrimitive);
00333 
00334         // Cylinder over cylinder
00335         bool    evalCollisionOCoverOC (CPrimitiveWorldImage& other, CCollisionDesc& desc, double timeMin, 
00336                                                                         double timeMax, double &firstContactTime, double &lastContactTime, 
00337                                                                         CMovePrimitive& primitive, CMovePrimitive& otherPrimitive);
00338 
00339         // *** Subprimitive over subprimitive
00340 
00341         // Point over segment in OB/OB test
00342         bool    evalCollisionPoverS (CPrimitiveWorldImage& other, CCollisionDesc& desc, uint numPoint, uint numSeg, 
00343                                                                         CMovePrimitive& primitive, CMovePrimitive& otherPrimitive);
00344 
00345         // Point over cylinder
00346         bool    evalCollisionPoverOC (CPrimitiveWorldImage& other, CCollisionDesc& desc, uint numPoint, 
00347                                                                         double &firstContactTime, double &lastContactTime, CMovePrimitive& primitive,
00348                                                                         CMovePrimitive& otherPrimitive);
00349 
00350         // Segment over cylinder
00351         bool    evalCollisionSoverOC (CPrimitiveWorldImage& other, CCollisionDesc& desc, uint numPoint, 
00352                                                                         CMovePrimitive& primitive, CMovePrimitive& otherPrimitive);
00353         
00363         const TCollisionSurfaceDescVector *evalCollision (CGlobalRetriever &retriever, CCollisionSurfaceTemp& surfaceTemp, 
00364                                                                                                         uint32 testTime, uint32 maxTestIteration, CMovePrimitive& primitive);
00365 
00366         // Make a move with globalRetriever. Must be call after a free collision evalCollision call.
00367         void    doMove (CGlobalRetriever &retriever, CCollisionSurfaceTemp& surfaceTemp, double originalMax, double finalMax, bool keepZ = false);
00368 
00369         // Make a move wihtout globalRetriever.
00370         void    doMove (double timeMax);
00371 
00372         // Reaction between two primitives. Return true if one object has been modified.
00373         void reaction (CPrimitiveWorldImage& second, const CCollisionDesc& desc, CGlobalRetriever* retriver,
00374                                         CCollisionSurfaceTemp& surfaceTemp, bool collision, CMovePrimitive &primitive, 
00375                                         CMovePrimitive &otherPrimitive, CMoveContainer *container, uint8 worldImage, 
00376                                         uint8 secondWorldImage, bool secondConst);
00377 
00378         // Reaction with a static collision. Return true if one object has been modified.
00379         void reaction ( const CCollisionSurfaceDesc&    surfaceDesc, const UGlobalPosition& globalPosition,
00380                                         CGlobalRetriever& retriever, double ratio, double dt, CMovePrimitive &primitive, CMoveContainer &container,
00381                                         uint8 worldImage);
00382 
00383 private:
00384         // Some flags
00385         enum TDynamicFlags
00386         {
00387                 // The dirt flag. Precalculated data for the position must be recomputed.
00388                 DirtPosFlag                     =0x0001,
00389 
00390                 // The dirt bounding box. Precalculated data for the bounding box must be recomputed.
00391                 DirtBBFlag                      =0x0002,
00392 
00393                 // In modified list.
00394                 InModifiedListFlag      =0x0004,
00395 
00396                 // Inserted in the world image.
00397                 InWorldImageFlag        =0x0008,
00398 
00399                 // Force the size to uint16.
00400                 ForceSize                       =0xffff
00401         };
00402 
00403         // Flags
00404         uint16                          _DynamicFlags;
00405 
00406         // This position is the central bottom position for the box or for the cylinder
00407         class CPosition
00408         {
00409         private:
00410                 // 3d position
00411                 NLMISC::CVectorD                                        _3dPosition;
00412 
00413                 // Global position
00414                 UGlobalPosition                                         _GlobalPosition;
00415         public:
00416                 // Return the 3d position
00417                 const NLMISC::CVectorD&                                         getPos () const
00418                 {
00419                         return _3dPosition;
00420                 }
00421 
00422                 // Set the 3d position
00423                 void                                                                            setPos (const NLMISC::CVectorD& newPos)
00424                 {
00425                         _3dPosition=newPos;
00426                 }
00427 
00428                 // Return the global position
00429                 const UGlobalPosition&                                          getGlobalPos () const
00430                 {
00431                         return _GlobalPosition;
00432                 }
00433 
00434                 // Set the global position
00435                 void                                                                            setGlobalPos (const UGlobalPosition& globalPosition,
00436                                                                                                                                   CGlobalRetriever& globalRetriver)
00437                 {                       
00438                         // Get position with global position
00439                         _GlobalPosition=globalPosition;
00440                         _GlobalPosition.LocalPosition.Estimation.z = globalRetriver.getMeanHeight(globalPosition);
00441                         _3dPosition=globalRetriver.getDoubleGlobalPosition (_GlobalPosition);
00442 //                      _3dPosition.z=(double)globalRetriver.getMeanHeight(globalPosition);                     
00443                 }
00444 
00445                 void                                                                            setGlobalPosKeepZ(const UGlobalPosition& globalPosition,
00446                                                                                                                                           CGlobalRetriever& globalRetriver)
00447                 {
00448                         _GlobalPosition.setKeepZ(globalPosition);
00449                         NLMISC::CVectorD dPos = globalRetriver.getDoubleGlobalPosition (globalPosition);
00450                         _3dPosition.x = dPos.x;
00451                         _3dPosition.y = dPos.y;
00452                 }
00453 
00454         };
00455 
00456         // Global position of the primitive in the world image.
00457         CPosition                       _Position;
00458 
00459         // Time for valid getPos () position.  _Position.getPos () == _3dInitPosition + _Speed * _InitTime;
00460         double                          _InitTime;
00461 
00462         // 3d position at t=0
00463         NLMISC::CVectorD        _3dInitPosition;
00464 
00465         // Delta position is : _Speed*Deltatime
00466         NLMISC::CVectorD        _DeltaPosition;
00467 
00468         // Current speed
00469         NLMISC::CVectorD        _Speed;
00470 
00471         // Movable bounding box
00472         double                          _BBXMin;
00473         double                          _BBYMin;
00474         double                          _BBXMax;
00475         double                          _BBYMax;
00476 
00477         // Union for data space shared by primitive type (cylinder or box)
00478         union 
00479         {
00480                 // Data for Boxes
00481                 struct
00482                 {
00483                         // 2d position of the 4 points of the box at initial time.
00484                         double PointPosX[4];
00485                         double PointPosY[4];
00486 
00487                         // The normalized direction vector of the 4 edges of the box.
00488                         double EdgeDirectionX[4];
00489                         double EdgeDirectionY[4];
00490 
00491                         // The box orientation
00492                         double Orientation;
00493                 } _OBData;
00494         };
00495 
00496         // Sorted list of modified primitives. This pointer is managed by CMoveContainer.
00497         CMovePrimitive          *_NextModified;
00498 
00499         // Pointer into the 4 possibles sorted lists of movable primitives. Can be NULL if not in the list
00500         CMoveElement            *_MoveElement[4];
00501 };
00502 
00503 
00504 } // NLPACS
00505 
00506 
00507 #endif // NL_PRIMITIVE_WORLD_IMAGE_H
00508 
00509 /* End of primitive_world_image.h */