Cafu Engine
MorphPrim.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_MORPH_PRIMITIVE_HPP_INCLUDED
8 #define CAFU_MORPH_PRIMITIVE_HPP_INCLUDED
9 
10 #include "Math3D/Vector3.hpp"
11 #include "Templates/Array.hpp"
12 
13 
14 class MapBezierPatchT;
15 class MapBrushT;
16 class MapPrimitiveT;
17 class Renderer2DT;
18 class Renderer3DT;
19 class wxPoint;
20 class MP_FaceT;
21 
22 
23 struct MP_PartT
24 {
25  enum TypeT
26  {
27  TYPE_VERTEX,
28  TYPE_EDGE
29  };
30 
31 
32  MP_PartT() : m_Selected(false)
33  {
34  }
35 
36  virtual ~MP_PartT() { }
37 
38  virtual TypeT GetType() const=0;
39  virtual Vector3fT GetPos() const=0;
40 
41  bool m_Selected; ///< Is this part/handle selected by the user?
42 };
43 
44 
45 class MP_VertexT : public MP_PartT
46 {
47  public:
48 
49  TypeT GetType() const { return TYPE_VERTEX; }
50  Vector3fT GetPos() const { return pos; }
51 
52  Vector3fT pos;
53 };
54 
55 
56 class MP_EdgeT : public MP_PartT
57 {
58  public:
59 
60  MP_EdgeT();
61 
62  TypeT GetType() const { return TYPE_EDGE; }
63  Vector3fT GetPos() const { return (Vertices[0]->pos + Vertices[1]->pos)*0.5f; }
64 
65  MP_VertexT* Vertices[2]; ///< The vertices of this edge (pointing into the MorphPrimT::m_Vertices array).
66  MP_FaceT* Faces[2]; ///< The faces this edge belongs to (pointing into the MorphPrimT::m_Faces array).
67 };
68 
69 
70 class MP_FaceT
71 {
72  public:
73 
74  ArrayT<MP_EdgeT*> Edges; ///< The edges of this face (pointing into the MorphPrimT::m_Edges array).
75 };
76 
77 
78 /// This is a helper class for the ToolMorphT ("edit vertices") tool.
79 /// It represents a map primitive (a brush or a bezier patch) that is currently being morphed by the tool.
80 ///
81 /// This class is considered a \emph{helper} class, because the user code (i.e. the morph tool)
82 /// (currently) needs knowledge about the implementation of this class whenever it keeps pointers to
83 /// parts (vertices, edges) of the object that this class represents. Such pointers into the internal
84 /// structures may become invalidated by certain methods, and thus great care is required.
86 {
87  public:
88 
89  /// The constructor.
90  /// @param MapPrim The original brush or bezier patch that this MorphPrimT is associated with / attached to.
91  /// Note that this MorphPrimT does not become the "owner" of the MapPrim pointer, e.g. it does not attempt to delete it in its dtor.
92  /// That also means that this MorphPrimT should not live longer than the MapPrim object.
93  MorphPrimT(const MapPrimitiveT* MapPrim);
94 
95  ~MorphPrimT();
96 
97  const MapPrimitiveT* GetMapPrim() const { return m_MapPrim; }
98  bool IsModified() const { return m_Modified; }
99 
100  /// Returns a newly created instance matching the morphed map primitive, or NULL if reconstruction was not possible.
101  /// It does not reset the modified-flag.
103 
104  /// Moves the selected handles by Delta.
105  void MoveSelectedHandles(const Vector3fT& Delta);
106 
107  void Render(Renderer2DT& Renderer, bool RenderVertexHandles, bool RenderEdgeHandles) const;
108  void Render(Renderer3DT& Renderer, bool RenderVertexHandles, bool RenderEdgeHandles) const;
109 
110 
111  // Must store MP_VertexT*, not MP_VertexT, or else array growing renders all external pointers obsolete...
112  ArrayT<MP_VertexT*> m_Vertices;
113  ArrayT<MP_EdgeT* > m_Edges;
114  ArrayT<MP_FaceT* > m_Faces;
115 
116 
117  private:
118 
119  MorphPrimT(const MorphPrimT&); ///< Use of the Copy Constructor is not allowed.
120  void operator = (const MorphPrimT&); ///< Use of the Assignment Operator is not allowed.
121 
122  /// After a change of (or in) the m_Vertices array, this method computes the convex hull over them
123  /// and updates (or rather, recomputes) all other member variables (the m_Edges and m_Faces).
124  /// This method should only be called if the m_MapPrim member is of type MapBrushT,
125  /// because it also modifies the m_Vertices array, which is not desired for the MapBezierPatchT type.
126  void UpdateBrushFromVertices();
127  void UpdatePatch();
128 
129  MP_VertexT* FindClosestVertex(const Vector3fT& Point) const;
130  MP_EdgeT* FindEdge(const MP_VertexT* v1, const MP_VertexT* v2) const;
131 
132  MP_VertexT* GetConnectionVertex(MP_EdgeT* Edge1, MP_EdgeT* Edge2) const;
133  void RenderHandle(Renderer3DT& Renderer, const wxPoint& ClientPos, const float* color) const;
134 
135 
136  const MapPrimitiveT* m_MapPrim; ///< The "attached" source/reference map brush / bezier patch.
137  bool m_Modified; ///< Whether the MorphPrimT contains any modifications to the "attached" map brush/bezier patch.
138  MapBezierPatchT* m_RenderBP; ///< If m_MapPrim is a Bezier patch, this is the current morphed clone that we use for rendering.
139 };
140 
141 #endif
This class provides auxiliary means for rendering a 3D view.
Definition: Renderer3D.hpp:30
This class implements the rendering into a 2D view.
Definition: Renderer2D.hpp:22
ArrayT< MP_EdgeT * > Edges
The edges of this face (pointing into the MorphPrimT::m_Edges array).
Definition: MorphPrim.hpp:74
This class adds no functionality of its own, but only exists for proper type separation.
Definition: MapPrimitive.hpp:21
This is a helper class for the ToolMorphT ("edit vertices") tool.
Definition: MorphPrim.hpp:85
bool m_Selected
Is this part/handle selected by the user?
Definition: MorphPrim.hpp:41
Definition: MapBrush.hpp:15
This class represents a bezier patch.
Definition: MapBezierPatch.hpp:44
MapPrimitiveT * GetMorphedMapPrim() const
Returns a newly created instance matching the morphed map primitive, or NULL if reconstruction was no...
Definition: MorphPrim.cpp:92
void MoveSelectedHandles(const Vector3fT &Delta)
Moves the selected handles by Delta.
Definition: MorphPrim.cpp:345
MP_VertexT * Vertices[2]
The vertices of this edge (pointing into the MorphPrimT::m_Vertices array).
Definition: MorphPrim.hpp:65
Definition: MorphPrim.hpp:70
MP_FaceT * Faces[2]
The faces this edge belongs to (pointing into the MorphPrimT::m_Faces array).
Definition: MorphPrim.hpp:66
Definition: MorphPrim.hpp:56
Definition: MorphPrim.hpp:45
Definition: MorphPrim.hpp:23
MorphPrimT(const MapPrimitiveT *MapPrim)
The constructor.
Definition: MorphPrim.cpp:31