Fade2.5D Documentation pages v2.03
Delaunay Features
FadeExport.h
Go to the documentation of this file.
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the Fade2D library. The student license is free
4 // of charge and covers personal non-commercial research. Licensees
5 // holding a commercial license may use this file in accordance with
6 // the Commercial License Agreement.
7 //
8 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
9 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
10 // FOR A PARTICULAR PURPOSE.
11 //
12 // Please contact the author if any conditions of this licensing are
13 // not clear to you.
14 //
15 // Author: Bernhard Kornberger, bkorn (at) geom.at
16 // http://www.geom.at
17 
19 #pragma once
20 
21 #include <vector>
22 #include <algorithm>
23 #include "common.h"
24 
25 #if GEOM_PSEUDO3D==GEOM_TRUE
26  namespace GEOM_FADE25D {
27 #elif GEOM_PSEUDO3D==GEOM_FALSE
28  namespace GEOM_FADE2D {
29 #else
30  #error GEOM_PSEUDO3D is not defined
31 #endif
32 
42 struct CLASS_DECLSPEC FadeExport
43 {
44  FadeExport():
45  numCustomIndices(0),numTriangles(0),numPoints(0),
46  aCoords(NULL),aCustomIndices(NULL),aTriangles(NULL)
47  {
48 #if GEOM_PSEUDO3D==GEOM_TRUE
49  dim=3;
50 #else
51  dim=2;
52 #endif
53  }
54  ~FadeExport();
55 
56 
57  FadeExport(const FadeExport& other)
58  {
59  copy(other);
60  }
61 
62  FadeExport& operator=(const FadeExport& other)
63  {
64  copy(other);
65  return *this;
66  }
67 
68  void copy(const FadeExport& other)
69  {
70  numCustomIndices=other.numCustomIndices;
71  numTriangles=other.numTriangles;
72  numPoints=other.numPoints;
73  aCoords=NULL;
74  aCustomIndices=NULL;
75  aTriangles=NULL;
76  dim=other.dim;
77  if(other.aTriangles!=NULL)
78  {
79  size_t siz(3*numTriangles);
80  aTriangles=new int[siz];
81  std::copy(other.aTriangles,other.aTriangles+siz,aTriangles);
82  }
83  if(other.aCoords!=NULL)
84  {
85  size_t siz(dim*numPoints);
86  aCoords=new double[siz];
87  std::copy(other.aCoords,other.aCoords+siz,aCoords);
88  }
89  if(other.aCustomIndices!=NULL)
90  {
91  aCustomIndices=new int[numCustomIndices];
92  std::copy(other.aCustomIndices,other.aCustomIndices+numCustomIndices,aCustomIndices);
93  }
94  }
95 
100  void reset()
101  {
102  if(aCoords!=NULL) delete [] aCoords;
103  if(aCustomIndices!=NULL) delete [] aCustomIndices;
104  if(aTriangles!=NULL) delete [] aTriangles;
105 
106  aCoords=NULL;
107  aCustomIndices=NULL;
108  aTriangles=NULL;
109 
110  numCustomIndices=0;
111  numTriangles=0;
112  numPoints=0;
113  }
114 
120  void lexiSort();
121 
123  void print() const;
124 
126  bool writeObj(const char* filename) const;
127 
129  void extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const;
130 
136  void getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const;
137 
144  int getCustomIndex(int vtxIdx) const;
145 
146 #if GEOM_PSEUDO3D==GEOM_TRUE
148  void getNormal(int triIdx,double& x,double& y,double& z) const;
149 #endif
150 
151 #if GEOM_PSEUDO3D==GEOM_TRUE
157  void getCoordinates(int vtxIdx,double& x,double& y,double& z) const;
158 #else
164  void getCoordinates(int vtxIdx,double& x,double& y) const;
165 #endif
166  bool operator==(const FadeExport& other) const;
167  // DATA
170  int numPoints;
171  double* aCoords;
173  int* aTriangles;
174  int dim;
175 };
176 
177 inline FadeExport::~FadeExport()
178 {
179  if(aCoords!=NULL) delete [] aCoords;
180  if(aCustomIndices!=NULL) delete [] aCustomIndices;
181  if(aTriangles!=NULL) delete [] aTriangles;
182  numCustomIndices=0;
183  numTriangles=0;
184  numPoints=0;
185 }
186 
187 inline bool FadeExport::operator==(const FadeExport& other) const
188 {
189  if(numTriangles != other.numTriangles) return false;
190  if(numPoints != other.numPoints) return false;
191 
192  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
193  {
194  for(int component=0;component<dim;++component)
195  {
196  size_t addr(dim*vtxIdx+component);
197  if(aCoords[addr]!=other.aCoords[addr]) return false;
198  }
199  }
200 
201  for(int triIdx=0;triIdx<numTriangles;++triIdx)
202  {
203  int v0,v1,v2;
204  getCornerIndices(int(triIdx),v0,v1,v2);
205  int va,vb,vc;
206  other.getCornerIndices(int(triIdx),va,vb,vc);
207  if(v0!=va || v1!=vb || v2!=vc) return false;
208  }
209  return true;
210 }
211 
212 
213 // For a triangle return the vertex indices
214 inline void FadeExport::getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const
215 {
216  int base(3*triIdx);
217  vtxIdx0=aTriangles[base];
218  vtxIdx1=aTriangles[base+1];
219  vtxIdx2=aTriangles[base+2];
220 }
221 
222 // Print, just for demo purposes
223 inline void FadeExport::print() const
224 {
225  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
226  {
227  int customIndex(-1); // Optional custom index
228  if(numCustomIndices>0) customIndex=aCustomIndices[vtxIdx];
229  std::cout<<"\nVertex "<<vtxIdx<<" (customIndex="<<customIndex<<"):";
230  for(int component=0;component<dim;++component) std::cout<<" "<<aCoords[dim*vtxIdx+component];
231  }
232 
233  for(int triIdx=0;triIdx<numTriangles;++triIdx)
234  {
235  int v0,v1,v2;
236  getCornerIndices(int(triIdx),v0,v1,v2);
237  std::cout<<"\nTriangle "<<triIdx<<": "<<v0<<" "<<v1<<" "<<v2;
238  }
239 
240  std::vector<std::pair<int,int> > vNeighbors;
241  this->extractTriangleNeighborships(vNeighbors);
242  for(size_t i=0;i<vNeighbors.size();++i)
243  {
244  std::cout<<"\nTriangle "<<vNeighbors[i].first<<" <-> Triangle "<<vNeighbors[i].second;
245  }
246  std::cout<<std::endl;
247 }
248 
249 // Write an *.obj file
250 inline bool FadeExport::writeObj(const char* filename) const
251 {
252  std::ofstream outFile(filename);
253  if(!outFile.is_open())
254  {
255  std::cout<<"Can't write "<<filename<<std::endl;
256  return false;
257  }
258  std::cout<<"writing "<<filename<<std::endl;
259 
260  outFile<<"# Written by Fade2D";
261  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
262  {
263  outFile<<"\nv";
264  for(int component=0;component<dim;++component) outFile<<" "<<aCoords[dim*vtxIdx+component];
265  if(dim==2) outFile<<" 0"; // *.obj needs always 3 components, so add z=0
266  }
267  for(int triIdx=0;triIdx<numTriangles;++triIdx)
268  {
269  outFile<<"\nf";
270  for(int corner=0;corner<3;++corner)
271  {
272  outFile<<" "<<aTriangles[3*triIdx+corner]+1; // +1 because in *.obj format indices start at 1, not 0.
273  }
274  }
275  outFile<<std::endl;
276  outFile.close();
277  return true;
278 }
279 
280 
281 inline void FadeExport::extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const
282 {
283  vNeigs.reserve(numTriangles*3/2);
284  std::vector<std::pair<std::pair<int,int>,int> > vVtxPair2Tri;
285  vVtxPair2Tri.reserve(numTriangles*3);
286 
287  for(int tri=0;tri<numTriangles;++tri)
288  {
289  size_t vtxIdx(3*tri);
290  int vtx0(aTriangles[vtxIdx]);
291  int vtx1(aTriangles[vtxIdx+1]);
292  int vtx2(aTriangles[vtxIdx+2]);
293  if(vtx0>vtx1) std::swap(vtx0,vtx1);
294  if(vtx1>vtx2)
295  {
296  std::swap(vtx1,vtx2);
297  if(vtx0>vtx1) std::swap(vtx0,vtx1);
298  }
299  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx1),tri));
300  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx1,vtx2),tri));
301  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx2),tri));
302  }
303  std::sort(vVtxPair2Tri.begin(),vVtxPair2Tri.end());
304  for(size_t i=0;i<vVtxPair2Tri.size();++i)
305  {
306  int vtx0(vVtxPair2Tri[i].first.first);
307  int vtx1(vVtxPair2Tri[i].first.second);
308 
309  if( ++i<vVtxPair2Tri.size() &&
310  vVtxPair2Tri[i].first.first==vtx0 &&
311  vVtxPair2Tri[i].first.second==vtx1)
312  {
313  int tri(vVtxPair2Tri[i].second);
314  vNeigs.push_back(std::pair<int,int>(tri,vVtxPair2Tri[i].second));
315  }
316  --i;
317  }
318 }
319 
320 
321 #if GEOM_PSEUDO3D==GEOM_TRUE
322 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y,double& z) const
323 {
324  int base(dim*vtxIdx);
325  x=aCoords[base];
326  y=aCoords[base+1];
327  z=aCoords[base+2];
328 }
329 #else
330 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y) const
331 {
332  int base(dim*vtxIdx);
333  x=aCoords[base];
334  y=aCoords[base+1];
335 }
336 #endif
337 
338 inline int FadeExport::getCustomIndex(int vtxIdx) const
339 {
340  if(vtxIdx<numCustomIndices)
341  {
342  return aCustomIndices[vtxIdx];
343  }
344  return -1;
345 }
346 
347 } // (namespace)
348 
349 
350 
351 
352 
FadeExport is a simple struct to export triangulation data.
Definition: FadeExport.h:43
void reset()
Reset the object.
Definition: FadeExport.h:100
void getNormal(int triIdx, double &x, double &y, double &z) const
Get the normal vector of a triangle.
int numTriangles
number of triangles
Definition: FadeExport.h:169
void print() const
Print data for demonstration purposes.
Definition: FadeExport.h:223
void lexiSort()
Sort the points lexicographically.
int * aCustomIndices
Custom indices of the points (only when exported)
Definition: FadeExport.h:172
void getCornerIndices(int triIdx, int &vtxIdx0, int &vtxIdx1, int &vtxIdx2) const
Get the corner indices of a certain triangle.
Definition: FadeExport.h:214
int getCustomIndex(int vtxIdx) const
Get the custom vertex index.
Definition: FadeExport.h:338
int dim
Dimension.
Definition: FadeExport.h:174
void extractTriangleNeighborships(std::vector< std::pair< int, int > > &vNeigs) const
Determine index-pairs of adjacent triangles.
Definition: FadeExport.h:281
int numPoints
number of points
Definition: FadeExport.h:170
int numCustomIndices
number of custom indices (same as numPoints when exported, otherwise 0)
Definition: FadeExport.h:168
bool writeObj(const char *filename) const
Write an *.obj file (supported by virtually any 3D viewer)
Definition: FadeExport.h:250
double * aCoords
Cartesian coordinates (dim*numPoints)
Definition: FadeExport.h:171
void getCoordinates(int vtxIdx, double &x, double &y, double &z) const
Get the coorinates for a certain vertex index.
Definition: FadeExport.h:322
int * aTriangles
3 counterclockwise oriented vertex-indices per triangle (3*numTriangles)
Definition: FadeExport.h:173