Fade2.5D Documentation pages v2.16.7
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 
33 #ifdef _WIN32
34 #pragma warning(push)
35 #pragma warning(disable : 4996)
36 #endif
37 
48 struct CLASS_DECLSPEC FadeExport
49 {
52  numCustomIndices(0),numTriangles(0),numPoints(0),
53  aCoords(NULL),aCustomIndices(NULL),aTriangles(NULL)
54  {
55 #if GEOM_PSEUDO3D==GEOM_TRUE
56  dim=3;
57 #else
58  dim=2;
59 #endif
60  }
62  ~FadeExport();
64  FadeExport(const FadeExport& other)
65  {
66  copy(other);
67  }
70  {
71  copy(other);
72  return *this;
73  }
80  void copy(const FadeExport& other)
81  {
82  numCustomIndices=other.numCustomIndices;
83  numTriangles=other.numTriangles;
84  numPoints=other.numPoints;
85  aCoords=NULL;
86  aCustomIndices=NULL;
87  aTriangles=NULL;
88  dim=other.dim;
89  if(other.aTriangles!=NULL)
90  {
91  size_t siz(3*numTriangles);
92  aTriangles=new int[siz];
93  std::copy(other.aTriangles,other.aTriangles+siz,aTriangles);
94  }
95  if(other.aCoords!=NULL)
96  {
97  size_t siz(dim*numPoints);
98  aCoords=new double[siz];
99  std::copy(other.aCoords,other.aCoords+siz,aCoords);
100  }
101  if(other.aCustomIndices!=NULL)
102  {
103  aCustomIndices=new int[numCustomIndices];
104  std::copy(other.aCustomIndices,other.aCustomIndices+numCustomIndices,aCustomIndices);
105  }
106  }
107 
114  void reset()
115  {
116  if(aCoords!=NULL) delete [] aCoords;
117  if(aCustomIndices!=NULL) delete [] aCustomIndices;
118  if(aTriangles!=NULL) delete [] aTriangles;
119 
120  aCoords=NULL;
121  aCustomIndices=NULL;
122  aTriangles=NULL;
123 
124  numCustomIndices=0;
125  numTriangles=0;
126  numPoints=0;
127  }
128 
136  void lexiSort();
137 
138 
144  void print() const;
145 
146 
156  bool writeObj(const char* filename) const;
157 
158 
168  void extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const;
169 
175  void getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const;
176 
183  int getCustomIndex(int vtxIdx) const;
184 
185 #if GEOM_PSEUDO3D==GEOM_TRUE
194  void getNormal(int triIdx,double& x,double& y,double& z) const;
195 #endif
196 
197 #if GEOM_PSEUDO3D==GEOM_TRUE
203  void getCoordinates(int vtxIdx,double& x,double& y,double& z) const;
204 #else
210  void getCoordinates(int vtxIdx,double& x,double& y) const;
211 #endif
212  bool operator==(const FadeExport& other) const;
213  // DATA
216  int numPoints;
217  double* aCoords;
219  int* aTriangles;
220  int dim;
221 };
222 
224 {
225  if(aCoords!=NULL) delete [] aCoords;
226  if(aCustomIndices!=NULL) delete [] aCustomIndices;
227  if(aTriangles!=NULL) delete [] aTriangles;
228  numCustomIndices=0;
229  numTriangles=0;
230  numPoints=0;
231 }
232 
233 inline bool FadeExport::operator==(const FadeExport& other) const
234 {
235  if(numTriangles != other.numTriangles) return false;
236  if(numPoints != other.numPoints) return false;
237 
238  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
239  {
240  for(int component=0;component<dim;++component)
241  {
242  size_t addr(dim*vtxIdx+component);
243  if(aCoords[addr]!=other.aCoords[addr]) return false;
244  }
245  }
246 
247  for(int triIdx=0;triIdx<numTriangles;++triIdx)
248  {
249  int v0,v1,v2;
250  getCornerIndices(int(triIdx),v0,v1,v2);
251  int va,vb,vc;
252  other.getCornerIndices(int(triIdx),va,vb,vc);
253  if(v0!=va || v1!=vb || v2!=vc) return false;
254  }
255  return true;
256 }
257 
258 
259 // For a triangle return the vertex indices
260 inline void FadeExport::getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const
261 {
262  int base(3*triIdx);
263  vtxIdx0=aTriangles[base];
264  vtxIdx1=aTriangles[base+1];
265  vtxIdx2=aTriangles[base+2];
266 }
267 
268 // Print, just for demo purposes
269 inline void FadeExport::print() const
270 {
271  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
272  {
273  int customIndex(-1); // Optional custom index
274  if(numCustomIndices>0) customIndex=aCustomIndices[vtxIdx];
275  GCOUT<<"\nVertex "<<vtxIdx<<" (customIndex="<<customIndex<<"):";
276  for(int component=0;component<dim;++component) GCOUT<<" "<<aCoords[dim*vtxIdx+component];
277  }
278 
279  for(int triIdx=0;triIdx<numTriangles;++triIdx)
280  {
281  int v0,v1,v2;
282  getCornerIndices(int(triIdx),v0,v1,v2);
283  GCOUT<<"\nTriangle "<<triIdx<<": "<<v0<<" "<<v1<<" "<<v2;
284  }
285 
286  std::vector<std::pair<int,int> > vNeighbors;
287  this->extractTriangleNeighborships(vNeighbors);
288  for(size_t i=0;i<vNeighbors.size();++i)
289  {
290  GCOUT<<"\nTriangle "<<vNeighbors[i].first<<" <-> Triangle "<<vNeighbors[i].second;
291  }
292  GCOUT<<std::endl;
293 }
294 
295 // Write an *.obj file
296 inline bool FadeExport::writeObj(const char* filename) const
297 {
298  std::ofstream outFile(filename);
299  if(!outFile.is_open())
300  {
301  GCOUT<<"Can't write "<<filename<<std::endl;
302  return false;
303  }
304  GCOUT<<"writing "<<filename<<std::endl;
305 
306  outFile<<"# Written by Fade2D";
307  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
308  {
309  outFile<<"\nv";
310  for(int component=0;component<dim;++component) outFile<<" "<<aCoords[dim*vtxIdx+component];
311  if(dim==2) outFile<<" 0"; // *.obj needs always 3 components, so add z=0
312  }
313  for(int triIdx=0;triIdx<numTriangles;++triIdx)
314  {
315  outFile<<"\nf";
316  for(int corner=0;corner<3;++corner)
317  {
318  outFile<<" "<<aTriangles[3*triIdx+corner]+1; // +1 because in *.obj format indices start at 1, not 0.
319  }
320  }
321  outFile<<std::endl;
322  outFile.close();
323  return true;
324 }
325 
326 
327 inline void FadeExport::extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const
328 {
329  vNeigs.reserve(numTriangles*3/2);
330  std::vector<std::pair<std::pair<int,int>,int> > vVtxPair2Tri;
331  vVtxPair2Tri.reserve(numTriangles*3);
332 
333  for(int tri=0;tri<numTriangles;++tri)
334  {
335  size_t vtxIdx(3*tri);
336  int vtx0(aTriangles[vtxIdx]);
337  int vtx1(aTriangles[vtxIdx+1]);
338  int vtx2(aTriangles[vtxIdx+2]);
339  if(vtx0>vtx1) std::swap(vtx0,vtx1);
340  if(vtx1>vtx2)
341  {
342  std::swap(vtx1,vtx2);
343  if(vtx0>vtx1) std::swap(vtx0,vtx1);
344  }
345  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx1),tri));
346  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx1,vtx2),tri));
347  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx2),tri));
348  }
349  std::sort(vVtxPair2Tri.begin(),vVtxPair2Tri.end());
350  for(size_t i=0;i<vVtxPair2Tri.size();++i)
351  {
352  int vtx0(vVtxPair2Tri[i].first.first);
353  int vtx1(vVtxPair2Tri[i].first.second);
354  int tri0(vVtxPair2Tri[i].second);
355 
356  if( ++i<vVtxPair2Tri.size() &&
357  vVtxPair2Tri[i].first.first==vtx0 &&
358  vVtxPair2Tri[i].first.second==vtx1)
359  {
360  int tri1(vVtxPair2Tri[i].second);
361  vNeigs.push_back(std::pair<int,int>(tri0,tri1));
362  }
363  --i;
364  }
365 }
366 
367 
368 #if GEOM_PSEUDO3D==GEOM_TRUE
369 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y,double& z) const
370 {
371  int base(dim*vtxIdx);
372  x=aCoords[base];
373  y=aCoords[base+1];
374  z=aCoords[base+2];
375 }
376 #else
377 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y) const
378 {
379  int base(dim*vtxIdx);
380  x=aCoords[base];
381  y=aCoords[base+1];
382 }
383 #endif
384 
385 inline int FadeExport::getCustomIndex(int vtxIdx) const
386 {
387  if(vtxIdx<numCustomIndices)
388  {
389  return aCustomIndices[vtxIdx];
390  }
391  return -1;
392 }
393 #ifdef _WIN32
394 #pragma warning(pop)
395 #endif
396 } // (namespace)
397 
398 
399 
400 
401 
FadeExport is a simple struct to export triangulation data.
Definition: FadeExport.h:49
void reset()
Resets the object.
Definition: FadeExport.h:114
void getNormal(int triIdx, double &x, double &y, double &z) const
Gets the normal vector of a triangle.
int numTriangles
number of triangles
Definition: FadeExport.h:215
void print() const
Prints the data for demonstration purposes.
Definition: FadeExport.h:269
void lexiSort()
Lexicographically sorts the points.
void copy(const FadeExport &other)
A copy function.
Definition: FadeExport.h:80
FadeExport(const FadeExport &other)
Copy constructor.
Definition: FadeExport.h:64
FadeExport & operator=(const FadeExport &other)
Assignment operator.
Definition: FadeExport.h:69
int * aCustomIndices
Custom indices of the points (only when exported)
Definition: FadeExport.h:218
~FadeExport()
Destructor.
Definition: FadeExport.h:223
void getCornerIndices(int triIdx, int &vtxIdx0, int &vtxIdx1, int &vtxIdx2) const
Get the corner indices of a certain triangle.
Definition: FadeExport.h:260
int getCustomIndex(int vtxIdx) const
Get the custom vertex index.
Definition: FadeExport.h:385
FadeExport()
Default constructor.
Definition: FadeExport.h:51
int dim
Dimension.
Definition: FadeExport.h:220
void extractTriangleNeighborships(std::vector< std::pair< int, int > > &vNeigs) const
Extracts triangle adjacencies.
Definition: FadeExport.h:327
int numPoints
number of points
Definition: FadeExport.h:216
int numCustomIndices
number of custom indices (same as numPoints when exported, otherwise 0)
Definition: FadeExport.h:214
bool writeObj(const char *filename) const
Writes the triangulation data to a .obj file.
Definition: FadeExport.h:296
double * aCoords
Cartesian coordinates (dim*numPoints)
Definition: FadeExport.h:217
void getCoordinates(int vtxIdx, double &x, double &y, double &z) const
Get the coorinates for a certain vertex index.
Definition: FadeExport.h:369
int * aTriangles
3 counterclockwise oriented vertex-indices per triangle (3*numTriangles)
Definition: FadeExport.h:219