1 | #ifndef __GEO_MESH_SIMPLIFIER__
|
---|
2 | #define __GEO_MESH_SIMPLIFIER__
|
---|
3 |
|
---|
4 | #include <map>
|
---|
5 | #include <math.h>
|
---|
6 | #include "GeoMesh.h"
|
---|
7 | #include "GeoMeshSimpSequence.h"
|
---|
8 | #include "vmi_simplifier.h"
|
---|
9 | #include "change.h"
|
---|
10 |
|
---|
11 | //#include "SimplificationMethod.h"
|
---|
12 |
|
---|
13 | using namespace Geometry;
|
---|
14 |
|
---|
15 | namespace Geometry
|
---|
16 | {
|
---|
17 |
|
---|
18 | //-----------------------------------------------------------------------
|
---|
19 | // Class for join triangle holes.
|
---|
20 | //-----------------------------------------------------------------------
|
---|
21 | class _coord_
|
---|
22 | {
|
---|
23 | public:
|
---|
24 | float x,y,z;
|
---|
25 | inline _coord_( float x = 0.0f,
|
---|
26 | float y = 0.0f,
|
---|
27 | float z = 0.0f)
|
---|
28 | {
|
---|
29 | this->x = x;
|
---|
30 | this->y = y;
|
---|
31 | this->z = z;
|
---|
32 | }
|
---|
33 |
|
---|
34 | inline _coord_(const _coord_ &f)
|
---|
35 | {
|
---|
36 | x = f.x;
|
---|
37 | y = f.y;
|
---|
38 | z = f.z;
|
---|
39 | }
|
---|
40 |
|
---|
41 | inline _coord_ & operator=(const _coord_ &f)
|
---|
42 | {
|
---|
43 | x = f.x;
|
---|
44 | y = f.y;
|
---|
45 | z = f.z;
|
---|
46 |
|
---|
47 | return *this;
|
---|
48 | }
|
---|
49 |
|
---|
50 | inline bool operator<(const _coord_ &f) const
|
---|
51 | {
|
---|
52 | if (x<f.x-0.0001) return true;
|
---|
53 | if (x>f.x+0.0001) return false;
|
---|
54 | if (y<f.y-0.0001) return true;
|
---|
55 | if (y>f.y+0.0001) return false;
|
---|
56 | if (z<f.z-0.0001) return true;
|
---|
57 | if (z>f.z+0.0001) return false;
|
---|
58 |
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 | };
|
---|
62 |
|
---|
63 | //-----------------------------------------------------------------------
|
---|
64 | // Class tha implements a double-linked map.
|
---|
65 | //-----------------------------------------------------------------------
|
---|
66 | class EdgesMultimap
|
---|
67 | {
|
---|
68 | private:
|
---|
69 | multimap<int,int> edges;
|
---|
70 | public:
|
---|
71 | EdgesMultimap();
|
---|
72 | ~EdgesMultimap();
|
---|
73 | void insert(int v1,int v2);
|
---|
74 | void remove(int v1,int v2);
|
---|
75 | void contract(int v1,int v2);
|
---|
76 | bool exists(int v1,int v2);
|
---|
77 | };
|
---|
78 |
|
---|
79 | //-----------------------------------------------------------------------
|
---|
80 | // Class that conserves textures adding new vertices to mesh.
|
---|
81 | //-----------------------------------------------------------------------
|
---|
82 | class TextureConserver
|
---|
83 | {
|
---|
84 | private:
|
---|
85 | EdgesMultimap *mEdges;
|
---|
86 | Mesh *mGeoMesh;
|
---|
87 |
|
---|
88 | public:
|
---|
89 | multimap<int,int> mVertices;
|
---|
90 |
|
---|
91 | TextureConserver();
|
---|
92 | ~TextureConserver();
|
---|
93 |
|
---|
94 | void JoinVertices(Mesh *mesh);
|
---|
95 |
|
---|
96 | Mesh *GetMesh();
|
---|
97 | };
|
---|
98 |
|
---|
99 | /// Mesh simplificator interface.
|
---|
100 | /** This module is used by both models, general mesh models and the plant and tree models for the trunk and the branches. It contains functions that generate simplified versions of 3D objects made out of triangles. Given a 3D object, this module computes a sequence of geometric transformations that reduce the objects geometric detail while preserving its appearance. For each simplification step, returns a simplification sequence containing the edge to be collapse, the two triangles being removed and the new triangles remapped to the model.\n\n
|
---|
101 |
|
---|
102 | Inputs:\n
|
---|
103 | - A pointer to the Geometry::Mesh object containing the 3D model to be simplified.
|
---|
104 | .
|
---|
105 |
|
---|
106 | Outputs:\n
|
---|
107 | -# The simplified mesh, contained in a Geometry::Mesh object.
|
---|
108 | -# Simplification sequence, represented by a Geometry::MeshSimplificationSequence object.
|
---|
109 | */
|
---|
110 |
|
---|
111 | class MeshSimplifier
|
---|
112 | {
|
---|
113 | public:
|
---|
114 | /// Class constructor. Retrieves a pointer to a valid Geometry::Mesh object to simplify.
|
---|
115 | MeshSimplifier( const Geometry::Mesh *,
|
---|
116 | Geometry::TIPOFUNC upb=0);
|
---|
117 |
|
---|
118 | /// Virtual class destructor.
|
---|
119 | virtual ~MeshSimplifier (void);
|
---|
120 |
|
---|
121 | /// Copy constructor
|
---|
122 | //MeshSimplifier(const MeshSimplifier&);
|
---|
123 |
|
---|
124 | /// Assignment operator
|
---|
125 | //MeshSimplifier& operator =(const MeshSimplifier&);
|
---|
126 |
|
---|
127 | /// Starts the simplification process. Receives as a parameter the LOD factor in a range of [0,1]. This is a pure virtual method and must be overloaded in a derived class that implements a simplification algorithm.
|
---|
128 | virtual int Simplify(Geometry::Real)=0;
|
---|
129 |
|
---|
130 | /// Starts the simplification process. Receives as a parameter the number of vertices of the resulting mesh. This is a pure virtual method and must be overloaded in a derived class that implements a simplification algorithm.
|
---|
131 | virtual int Simplify(Geometry::uint32)=0;
|
---|
132 |
|
---|
133 | /// Returns the simplified mesh.
|
---|
134 | Mesh *GetMesh();
|
---|
135 |
|
---|
136 | /// Returns the simplification sequence for general meshes.
|
---|
137 | MeshSimplificationSequence *GetSimplificationSequence();
|
---|
138 |
|
---|
139 | // Sets what is the mesh that stores the leaves
|
---|
140 | void setMeshLeaves(Geometry::Index);
|
---|
141 |
|
---|
142 | protected:
|
---|
143 |
|
---|
144 | //private: // fer protected
|
---|
145 | Mesh *mGeoMesh;
|
---|
146 | Mesh *mInitialMesh;
|
---|
147 | MeshSimplificationSequence *msimpsequence;
|
---|
148 |
|
---|
149 | Geometry::Index indexMeshLeaves;
|
---|
150 |
|
---|
151 | // Progress bar function.
|
---|
152 | Geometry::TIPOFUNC mUPB;
|
---|
153 |
|
---|
154 | // Sort mesh bones.
|
---|
155 | void sortBones();
|
---|
156 |
|
---|
157 | private:
|
---|
158 |
|
---|
159 | void eraseVoidSubMeshes(Mesh *geoMesh);
|
---|
160 | };
|
---|
161 |
|
---|
162 | /// Implementation of a simplification algorithm based on viewpoint.
|
---|
163 | /** This class implements a simplification algorithm based on a viewpoint evaluation technique. */
|
---|
164 | class ViewPointDrivenSimplifier : public MeshSimplifier
|
---|
165 | {
|
---|
166 | private:
|
---|
167 |
|
---|
168 | // Vectors of positions, normals and texture coordinates.
|
---|
169 | vector<Vector3> vPositions;
|
---|
170 | vector<Vector3> vNormals;
|
---|
171 | vector<Vector2> vTexCoords;
|
---|
172 |
|
---|
173 | // Auxiliar vertex buffer.
|
---|
174 | Geometry::VertexBuffer *mVB;
|
---|
175 |
|
---|
176 | // Set of indices.
|
---|
177 | map<int,int> mIndexMap;
|
---|
178 |
|
---|
179 | // Join and split vertices to matain texture aspect.
|
---|
180 | TextureConserver *mTexConserver;
|
---|
181 |
|
---|
182 | // Loads a vmi mesh with a given geometry mesh.
|
---|
183 | VMI::Mesh * initMeshStructure(Geometry::Mesh *geoMesh);
|
---|
184 |
|
---|
185 | // Loads a geometry mesh with a given vmi mesh.
|
---|
186 | void loadMesh();
|
---|
187 |
|
---|
188 | // Find vertex in auxiliar vertex buffer.
|
---|
189 | void findVertex(size_t submesh, size_t index);
|
---|
190 |
|
---|
191 | // Gets the VMI mesh simplification sequence.
|
---|
192 | void GetMeshSimpSequence();
|
---|
193 |
|
---|
194 | // Fill up vectors of positions, normals and texture coordinates.
|
---|
195 | void fillUpPosNorTC(Mesh *geoMesh);
|
---|
196 |
|
---|
197 | // Reassigns bones.
|
---|
198 | void bonesReassignament();
|
---|
199 |
|
---|
200 | // Init VMI options.
|
---|
201 | int init(void);
|
---|
202 |
|
---|
203 | public:
|
---|
204 |
|
---|
205 | /// Class constructor. Will call Simplifier class constructor.
|
---|
206 | ViewPointDrivenSimplifier( const Geometry::Mesh *geoMesh,
|
---|
207 | Geometry::TIPOFUNC upb=0);
|
---|
208 |
|
---|
209 | /// Class destructor.
|
---|
210 | ~ViewPointDrivenSimplifier(void);
|
---|
211 |
|
---|
212 | /// Copy constructor
|
---|
213 | //ViewPointDrivenSimplifier(const ViewPointDrivenSimplifier&);
|
---|
214 |
|
---|
215 | /// Assignment operator
|
---|
216 | //ViewPointDrivenSimplifier& operator =(const ViewPointDrivenSimplifier&);
|
---|
217 |
|
---|
218 | /// Starts the simplification process. Receives as a parameter the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify method to perform an image based simplification.
|
---|
219 | int Simplify(Geometry::Real);
|
---|
220 |
|
---|
221 | /// Starts the simplification process. Receives as a parameter the number of vertices of the resulting mesh. Implements the Simplifier::Simplify method to perform an image based simplification.
|
---|
222 | int Simplify(Geometry::uint32);
|
---|
223 |
|
---|
224 | // Returns the simplified mesh.
|
---|
225 | //Mesh *GetMesh();
|
---|
226 | };
|
---|
227 |
|
---|
228 | /// Implementation of a simplification algorithm based on geometry information.
|
---|
229 | /** This class implements a simplification algorithm based on a classic geometry evaluation technique. */
|
---|
230 | class GeometryBasedSimplifier : public MeshSimplifier
|
---|
231 | {
|
---|
232 | public:
|
---|
233 | /// Class constructor. Will call Simplifier class constructor.
|
---|
234 | GeometryBasedSimplifier( const Geometry::Mesh *geoMesh,
|
---|
235 | Geometry::TIPOFUNC upb=0);
|
---|
236 |
|
---|
237 | /// Class destructor.
|
---|
238 | ~GeometryBasedSimplifier(void);
|
---|
239 |
|
---|
240 | /// Copy constructor
|
---|
241 | //GeometryBasedSimplifier(const GeometryBasedSimplifier&);
|
---|
242 |
|
---|
243 | /// Assignment operator
|
---|
244 | //GeometryBasedSimplifier& operator =(const GeometryBasedSimplifier&);
|
---|
245 |
|
---|
246 | /// Starts the simplification process. Receives as a parameter the LOD factor in a range of [0,1]. Implements the Simplifier::Simplify method to perform an image based simplification.
|
---|
247 | int Simplify(Geometry::Real);
|
---|
248 |
|
---|
249 | /// Starts the simplification process. Receives as a parameter the number of vertices of the resulting mesh. Implements the Simplifier::Simplify method to perform an image based simplification.
|
---|
250 | int Simplify(Geometry::uint32);
|
---|
251 | };
|
---|
252 | } // end of Geometry namespace;
|
---|
253 |
|
---|
254 | #endif
|
---|
255 |
|
---|