Cafu Engine
ClipModel.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_CLIPSYS_CLIPMODEL_HPP_INCLUDED
8 #define CAFU_CLIPSYS_CLIPMODEL_HPP_INCLUDED
9 
10 #include "Math3D/BoundingBox.hpp"
11 #include "Math3D/Matrix3x3.hpp"
12 #include "Math3D/Vector3.hpp"
13 
14 
15 namespace cf { namespace GameSys { class ComponentBaseT; } }
16 
17 
18 namespace cf
19 {
20  namespace ClipSys
21  {
22  class CollisionModelT;
23  class ClipLinkT;
24  class ClipWorldT;
25  struct TraceResultT;
26  class TraceSolidT;
27 
28 
29  /// A clip model represents an object in the world against which clipping queries can be performed.
30  /// Normally, clip models are inserted into a ClipWorldT instance and are then used via the clip world.
31  class ClipModelT
32  {
33  public:
34 
35  /// The constructor for creating a clip model.
36  ClipModelT(const ClipWorldT& ClipWorld_, const CollisionModelT* CollisionModel_ = NULL);
37 
38  /// The destructor.
39  ~ClipModelT();
40 
41  /// Registers this clip model with the clip world.
42  void Register();
43 
44  /// Unregisters this clip model from the clip world.
45  void Unregister();
46 
47  /// Returns the collision model of this clip model.
48  const CollisionModelT* GetCollisionModel() const { return CollisionModel; }
49 
50  /// Sets the given collision model for use with (as the basis of) this clip model.
51  /// Unregisters from the clip world first.
52  void SetCollisionModel(const CollisionModelT* CollisionModel_);
53 
54  /// Returns the origin of this clip model.
55  const Vector3dT& GetOrigin() const { return Origin; }
56 
57  /// Sets a new origin for this clip model.
58  /// Unregisters the model from the clip world before updating the origin, but does *not* re-register it!
59  /// It is up to the user to call Register() again to re-register to model in the clip world.
60  void SetOrigin(const Vector3dT& NewOrigin);
61 
62  /// Returns the orientation (axes, rotation matrix) for this clip model.
63  const math::Matrix3x3T<double>& GetOrientation() const { return Orientation; }
64 
65  /// Sets a new orientation (axes, rotation matrix) for this clip model.
66  /// Unregisters the model from the clip world before updating the orientation, but does *not* re-register it!
67  /// It is up to the user to call Register() again to re-register to model in the clip world.
68  void SetOrientation(const math::Matrix3x3T<double>& NewOrientation);
69 
70  /// Returns the user data associated with this clip model.
71  /// This is usually the pointer to the entity that "owns" this clip model, i.e. this clip model is a member of this entity.
72  cf::GameSys::ComponentBaseT* GetOwner() const { return m_Owner; }
73 
74  /// Sets the user data associated with this clip model.
75  /// @param Owner The component that created and is responsible for this clip model.
76  /// @see GetOwner()
77  void SetOwner(GameSys::ComponentBaseT* Owner) { m_Owner = Owner; }
78 
79  /// Returns the bounding box of this clip model in absolute world coordinates.
80  /// Note that the clip model must be registered (linked) with the clip world when this method is called.
81  const BoundingBox3dT& GetAbsoluteBB() const;
82 
83  /// Returns the contents of this clip model, which is the forwarded contents of the underlying collision model.
84  unsigned long GetContents() const;
85 
86  /// Traces the given convex solid from Start along Ray (up to the input value of Result.Fraction) through the clip model,
87  /// and reports the first collision, if any.
88  /// @param TraceSolid The convex solid to trace through the model.
89  /// @param Start The start point in world space where the trace begins.
90  /// @param Ray The ray along which the trace is performed. Note that with F being the input value of Result.Fraction, the endpoint is at Start+Ray*F.
91  /// @param ClipMask Only surfaces whose clip flags match this mask participate in the test. This is for optimization, because it allows the implementation to cull surfaces that are not of interest early.
92  /// @param Result The start value of Fraction is input via this reference, and the result of the trace returned.
93  /// Using an input/output parameter for returning the result, rather than a true return type, suggests itself because it makes
94  /// cascaded calls to this function natural (i.e. from (possibly many) super-objects and to (possibly many) sub-objects).
95  /// @see TraceResultT
96  void TraceConvexSolid(const TraceSolidT& TraceSolid, const Vector3dT& Start, const Vector3dT& Ray, unsigned long ClipMask, TraceResultT& Result) const;
97 
98  /// Determines the volume contents of the model at the given point / in the given box.
99  /// The function considers all brush volumes in the clip model that contain the given point or intersect the given box,
100  /// and returns their combined ("or'ed") contents.
101  /// @param Point The point in world space at which the volume contents of this model is to be determined.
102  /// @param BoxRadius When nonzero, this is the radius of an imaginary box around Point.
103  /// @param ContMask Only volumes whose contents matches this mask participate in the test. This is for optimization, because it allows the implementation to cull volumes that are not of interest early.
104  /// @returns the combined volume content flags of the intersected volumes in the clip model.
105  // If the Point (and thus the associated box) is outside of all volumes, 0 is returned.
106  unsigned long GetContents(const Vector3dT& Point, double BoxRadius, unsigned long ContMask) const;
107 
108 
109  private:
110 
111  friend class ClipWorldT;
112 
113  ClipModelT(const ClipModelT&); ///< Use of the Copy Constructor is not allowed.
114  void operator = (const ClipModelT&); ///< Use of the Assignment Operator is not allowed.
115 
116  const ClipWorldT& ClipWorld;
117  const CollisionModelT* CollisionModel;
118  Vector3T<double> Origin; ///< The translation of the collision model that positions it in world space. Origin and Orientation together form a matrix M that transforms a point from model to world space.
119  math::Matrix3x3T<double> Orientation; ///< The local axes of the collision model that positions it in world space. Origin and Orientation together form a matrix M that transforms a point from model to world space.
120  GameSys::ComponentBaseT* m_Owner; ///< The component that "owns" this clip model, i.e. this clip model belongs to and is a member of this component.
121  BoundingBox3dT AbsoluteBB; ///< The world-space bounding box of the collision model, i.e. with Transformation applied.
122  ClipLinkT* ListOfSectors; ///< The list of sectors that this model is in.
123  bool IsEnabled; ///< Whether this model is enabled for clipping or not. When disabled, the model is not taken into account even while being registered with the clip world.
124  bool AlreadyChecked;///< For sole use by the ClipWorldT::GetClipModelsFromBB() method only, should always be false outside of that method.
125  };
126  }
127 }
128 
129 #endif
cf::GameSys::ComponentBaseT * GetOwner() const
Returns the user data associated with this clip model.
Definition: ClipModel.hpp:72
A clip model represents an object in the world against which clipping queries can be performed...
Definition: ClipModel.hpp:31
void SetOrigin(const Vector3dT &NewOrigin)
Sets a new origin for this clip model.
Definition: ClipModel.cpp:239
This class represents a solid object that can be traced through collision worlds, models and shapes...
Definition: TraceSolid.hpp:30
void SetCollisionModel(const CollisionModelT *CollisionModel_)
Sets the given collision model for use with (as the basis of) this clip model.
Definition: ClipModel.cpp:232
ClipModelT(const ClipWorldT &ClipWorld_, const CollisionModelT *CollisionModel_=NULL)
The constructor for creating a clip model.
Definition: ClipModel.cpp:29
void SetOwner(GameSys::ComponentBaseT *Owner)
Sets the user data associated with this clip model.
Definition: ClipModel.hpp:77
const BoundingBox3dT & GetAbsoluteBB() const
Returns the bounding box of this clip model in absolute world coordinates.
Definition: ClipModel.cpp:48
void SetOrientation(const math::Matrix3x3T< double > &NewOrientation)
Sets a new orientation (axes, rotation matrix) for this clip model.
Definition: ClipModel.cpp:248
const math::Matrix3x3T< double > & GetOrientation() const
Returns the orientation (axes, rotation matrix) for this clip model.
Definition: ClipModel.hpp:63
void Unregister()
Unregisters this clip model from the clip world.
Definition: ClipModel.cpp:196
const Vector3dT & GetOrigin() const
Returns the origin of this clip model.
Definition: ClipModel.hpp:55
This class describes the result of tracing an object (a ray, a bounding-box, or a convex solid) throu...
Definition: TraceResult.hpp:36
This is the base class for collision models, defining their common interface.
Definition: CollisionModel_base.hpp:29
void Register()
Registers this clip model with the clip world.
Definition: ClipModel.cpp:143
~ClipModelT()
The destructor.
Definition: ClipModel.cpp:42
The clip world manages all the clip models that exist in a world (their "union"). ...
Definition: ClipWorld.hpp:27
void TraceConvexSolid(const TraceSolidT &TraceSolid, const Vector3dT &Start, const Vector3dT &Ray, unsigned long ClipMask, TraceResultT &Result) const
Traces the given convex solid from Start along Ray (up to the input value of Result.Fraction) through the clip model, and reports the first collision, if any.
Definition: ClipModel.cpp:64
unsigned long GetContents() const
Returns the contents of this clip model, which is the forwarded contents of the underlying collision ...
Definition: ClipModel.cpp:58
Definition: ClipWorld_private.hpp:19
const CollisionModelT * GetCollisionModel() const
Returns the collision model of this clip model.
Definition: ClipModel.hpp:48
This is the base class for the components that an entity is composed/aggregated of.
Definition: CompBase.hpp:54