Fade2.5D Documentation pages v1.99
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 
61  void lexiSort();
62 
64  void print() const;
65 
67  bool writeObj(const char* filename) const;
68 
70  void extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const;
71 
77  void getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const;
78 
85  int getCustomIndex(int vtxIdx) const;
86 
87 #if GEOM_PSEUDO3D==GEOM_TRUE
89  void getNormal(int triIdx,double& x,double& y,double& z) const;
90 #endif
91 
92 #if GEOM_PSEUDO3D==GEOM_TRUE
98  void getCoordinates(int vtxIdx,double& x,double& y,double& z) const;
99 #else
105  void getCoordinates(int vtxIdx,double& x,double& y) const;
106 #endif
107 
108  // DATA
111  int numPoints;
112  double* aCoords;
114  int* aTriangles;
115  int dim;
116 };
117 
118 inline FadeExport::~FadeExport()
119 {
120  if(aCoords!=NULL) delete [] aCoords;
121  if(aCustomIndices!=NULL) delete [] aCustomIndices;
122  if(aTriangles!=NULL) delete [] aTriangles;
123  numCustomIndices=0;
124  numTriangles=0;
125  numPoints=0;
126 }
127 
128 
129 
130 // For a triangle return the vertex indices
131 inline void FadeExport::getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const
132 {
133  int base(3*triIdx);
134  vtxIdx0=aTriangles[base];
135  vtxIdx1=aTriangles[base+1];
136  vtxIdx2=aTriangles[base+2];
137 }
138 
139 // Print, just for demo purposes
140 inline void FadeExport::print() const
141 {
142  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
143  {
144  int customIndex(-1); // Optional custom index
145  if(numCustomIndices>0) customIndex=aCustomIndices[vtxIdx];
146  std::cout<<"\nVertex "<<vtxIdx<<" (customIndex="<<customIndex<<"):";
147  for(int component=0;component<dim;++component) std::cout<<" "<<aCoords[dim*vtxIdx+component];
148  }
149 
150  for(int triIdx=0;triIdx<numTriangles;++triIdx)
151  {
152  int v0,v1,v2;
153  getCornerIndices(int(triIdx),v0,v1,v2);
154  std::cout<<"\nTriangle "<<triIdx<<": "<<v0<<" "<<v1<<" "<<v2;
155  }
156 
157  std::vector<std::pair<int,int> > vNeighbors;
158  this->extractTriangleNeighborships(vNeighbors);
159  for(size_t i=0;i<vNeighbors.size();++i)
160  {
161  std::cout<<"\nTriangle "<<vNeighbors[i].first<<" <-> Triangle "<<vNeighbors[i].second;
162  }
163  std::cout<<std::endl;
164 }
165 
166 // Write an *.obj file
167 inline bool FadeExport::writeObj(const char* filename) const
168 {
169  std::ofstream outFile(filename);
170  if(!outFile.is_open())
171  {
172  std::cout<<"Can't write "<<filename<<std::endl;
173  return false;
174  }
175  std::cout<<"writing "<<filename<<std::endl;
176 
177  outFile<<"# Written by Fade2D";
178  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
179  {
180  outFile<<"\nv";
181  for(int component=0;component<dim;++component) outFile<<" "<<aCoords[dim*vtxIdx+component];
182  if(dim==2) outFile<<" 0"; // *.obj needs always 3 components, so add z=0
183  }
184  for(int triIdx=0;triIdx<numTriangles;++triIdx)
185  {
186  outFile<<"\nf";
187  for(int corner=0;corner<3;++corner)
188  {
189  outFile<<" "<<aTriangles[3*triIdx+corner]+1; // +1 because in *.obj format indices start at 1, not 0.
190  }
191  }
192  outFile<<std::endl;
193  outFile.close();
194  return true;
195 }
196 
197 
198 inline void FadeExport::extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const
199 {
200  vNeigs.reserve(numTriangles*3/2);
201  std::vector<std::pair<std::pair<int,int>,int> > vVtxPair2Tri;
202  vVtxPair2Tri.reserve(numTriangles*3);
203 
204  for(int tri=0;tri<numTriangles;++tri)
205  {
206  size_t vtxIdx(3*tri);
207  int vtx0(aTriangles[vtxIdx]);
208  int vtx1(aTriangles[vtxIdx+1]);
209  int vtx2(aTriangles[vtxIdx+2]);
210  if(vtx0>vtx1) std::swap(vtx0,vtx1);
211  if(vtx1>vtx2)
212  {
213  std::swap(vtx1,vtx2);
214  if(vtx0>vtx1) std::swap(vtx0,vtx1);
215  }
216  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx1),tri));
217  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx1,vtx2),tri));
218  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx2),tri));
219  }
220  std::sort(vVtxPair2Tri.begin(),vVtxPair2Tri.end());
221  for(size_t i=0;i<vVtxPair2Tri.size();++i)
222  {
223  int vtx0(vVtxPair2Tri[i].first.first);
224  int vtx1(vVtxPair2Tri[i].first.second);
225  int tri(vVtxPair2Tri[i].second);
226  if( ++i<vVtxPair2Tri.size() &&
227  vVtxPair2Tri[i].first.first==vtx0 &&
228  vVtxPair2Tri[i].first.second==vtx1)
229  {
230  vNeigs.push_back(std::pair<int,int>(tri,vVtxPair2Tri[i].second));
231  }
232  --i;
233  }
234 }
235 
236 
237 #if GEOM_PSEUDO3D==GEOM_TRUE
238 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y,double& z) const
239 {
240  int base(dim*vtxIdx);
241  x=aCoords[base];
242  y=aCoords[base+1];
243  z=aCoords[base+2];
244 }
245 #else
246 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y) const
247 {
248  int base(dim*vtxIdx);
249  x=aCoords[base];
250  y=aCoords[base+1];
251 }
252 #endif
253 
254 inline int FadeExport::getCustomIndex(int vtxIdx) const
255 {
256  if(vtxIdx<numCustomIndices)
257  {
258  return aCustomIndices[vtxIdx];
259  }
260  return -1;
261 }
262 
263 } // (namespace)
264 
265 
266 
267 
268 
FadeExport is a simple struct to export triangulation data.
Definition: FadeExport.h:43
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:110
void print() const
Print data for demonstration purposes.
Definition: FadeExport.h:140
void lexiSort()
Sort the points lexicographically.
int * aCustomIndices
Custom indices of the points (only when exported)
Definition: FadeExport.h:113
void getCornerIndices(int triIdx, int &vtxIdx0, int &vtxIdx1, int &vtxIdx2) const
Get the corner indices of a certain triangle.
Definition: FadeExport.h:131
int getCustomIndex(int vtxIdx) const
Get the custom vertex index.
Definition: FadeExport.h:254
int dim
Dimension.
Definition: FadeExport.h:115
void extractTriangleNeighborships(std::vector< std::pair< int, int > > &vNeigs) const
Determine index-pairs of adjacent triangles.
Definition: FadeExport.h:198
int numPoints
number of points
Definition: FadeExport.h:111
int numCustomIndices
number of custom indices (same as numPoints when exported, otherwise 0)
Definition: FadeExport.h:109
bool writeObj(const char *filename) const
Write an *.obj file (supported by virtually any 3D viewer)
Definition: FadeExport.h:167
double * aCoords
Cartesian coordinates (dim*numPoints)
Definition: FadeExport.h:112
void getCoordinates(int vtxIdx, double &x, double &y, double &z) const
Get the coorinates for a certain vertex index.
Definition: FadeExport.h:238
int * aTriangles
3 counterclockwise oriented vertex-indices per triangle (3*numTriangles)
Definition: FadeExport.h:114