Fade2.5D Documentation pages v1.85
Delaunay Features
FadeExport.h
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 
18 #pragma once
19 
20 #include <vector>
21 #include <algorithm>
22 #include "common.h"
23 
24 #if GEOM_PSEUDO3D==GEOM_TRUE
25  namespace GEOM_FADE25D {
26 #elif GEOM_PSEUDO3D==GEOM_FALSE
27  namespace GEOM_FADE2D {
28 #else
29  #error GEOM_PSEUDO3D is not defined
30 #endif
31 
41 struct CLASS_DECLSPEC FadeExport
42 {
43  FadeExport():
44  numCustomIndices(0),numTriangles(0),numPoints(0),
45  aCoords(NULL),aCustomIndices(NULL),aTriangles(NULL)
46  {
47 #if GEOM_PSEUDO3D==GEOM_TRUE
48  dim=3;
49 #else
50  dim=2;
51 #endif
52  }
53  ~FadeExport();
54 
55 
56 
58  void print() const;
59 
61  bool writeObj(const char* filename) const;
62 
64  void extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const;
65 
71  void getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const;
72 
73 #if GEOM_PSEUDO3D==GEOM_TRUE
74 
79  void getCoordinates(int vtxIdx,double& x,double& y,double& z) const;
80 #else
81 
86  void getCoordinates(int vtxIdx,double& x,double& y) const;
87 #endif
88 
89  // DATA
92  int numPoints;
93  double* aCoords;
95  int* aTriangles;
96  int dim;
97 };
98 
99 inline FadeExport::~FadeExport()
100 {
101  if(aCoords!=NULL) delete [] aCoords;
102  if(aCustomIndices!=NULL) delete [] aCustomIndices;
103  if(aTriangles!=NULL) delete [] aTriangles;
104  numCustomIndices=0;
105  numTriangles=0;
106  numPoints=0;
107 }
108 
109 
110 
111 // For a triangle return the vertex indices
112 inline void FadeExport::getCornerIndices(int triIdx,int& vtxIdx0,int& vtxIdx1,int& vtxIdx2) const
113 {
114  int base(3*triIdx);
115  vtxIdx0=aTriangles[base];
116  vtxIdx1=aTriangles[base+1];
117  vtxIdx2=aTriangles[base+2];
118 }
119 
120 // Print, just for demo purposes
121 inline void FadeExport::print() const
122 {
123  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
124  {
125  int customIndex(-1); // Optional custom index
126  if(numCustomIndices>0) customIndex=aCustomIndices[vtxIdx];
127  std::cout<<"\nVertex "<<vtxIdx<<" (customIndex="<<customIndex<<"):";
128  for(int component=0;component<dim;++component) std::cout<<" "<<aCoords[dim*vtxIdx+component];
129  }
130 
131  for(int triIdx=0;triIdx<numTriangles;++triIdx)
132  {
133  int v0,v1,v2;
134  getCornerIndices(int(triIdx),v0,v1,v2);
135  std::cout<<"\nTriangle "<<triIdx<<": "<<v0<<" "<<v1<<" "<<v2;
136  }
137 
138  std::vector<std::pair<int,int> > vNeighbors;
139  this->extractTriangleNeighborships(vNeighbors);
140  for(size_t i=0;i<vNeighbors.size();++i)
141  {
142  std::cout<<"\nTriangle "<<vNeighbors[i].first<<" <-> Triangle "<<vNeighbors[i].second;
143  }
144  std::cout<<std::endl;
145 }
146 
147 // Write an *.obj file
148 inline bool FadeExport::writeObj(const char* filename) const
149 {
150  std::ofstream outFile(filename);
151  if(!outFile.is_open())
152  {
153  std::cout<<"Can't write "<<filename<<std::endl;
154  return false;
155  }
156  std::cout<<"writing "<<filename<<std::endl;
157 
158  outFile<<"# Written by Fade2D";
159  for(int vtxIdx=0;vtxIdx<numPoints;++vtxIdx)
160  {
161  outFile<<"\nv";
162  for(int component=0;component<dim;++component) outFile<<" "<<aCoords[dim*vtxIdx+component];
163  if(dim==2) outFile<<" 0"; // *.obj needs always 3 components, so add z=0
164  }
165  for(int triIdx=0;triIdx<numTriangles;++triIdx)
166  {
167  outFile<<"\nf";
168  for(int corner=0;corner<3;++corner)
169  {
170  outFile<<" "<<aTriangles[3*triIdx+corner]+1; // +1 because in *.obj format indices start at 1, not 0.
171  }
172  }
173  outFile<<std::endl;
174  outFile.close();
175  return true;
176 }
177 
178 
179 inline void FadeExport::extractTriangleNeighborships(std::vector<std::pair<int,int> >& vNeigs) const
180 {
181  vNeigs.reserve(numTriangles*3/2);
182  std::vector<std::pair<std::pair<int,int>,int> > vVtxPair2Tri;
183  vVtxPair2Tri.reserve(numTriangles*3);
184 
185  for(int tri=0;tri<numTriangles;++tri)
186  {
187  size_t vtxIdx(3*tri);
188  int vtx0(aTriangles[vtxIdx]);
189  int vtx1(aTriangles[vtxIdx+1]);
190  int vtx2(aTriangles[vtxIdx+2]);
191  if(vtx0>vtx1) std::swap(vtx0,vtx1);
192  if(vtx1>vtx2)
193  {
194  std::swap(vtx1,vtx2);
195  if(vtx0>vtx1) std::swap(vtx0,vtx1);
196  }
197  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx1),tri));
198  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx1,vtx2),tri));
199  vVtxPair2Tri.push_back(std::make_pair(std::make_pair(vtx0,vtx2),tri));
200  }
201  std::sort(vVtxPair2Tri.begin(),vVtxPair2Tri.end());
202  for(size_t i=0;i<vVtxPair2Tri.size();++i)
203  {
204  int vtx0(vVtxPair2Tri[i].first.first);
205  int vtx1(vVtxPair2Tri[i].first.second);
206  int tri(vVtxPair2Tri[i].second);
207  if( ++i<vVtxPair2Tri.size() &&
208  vVtxPair2Tri[i].first.first==vtx0 &&
209  vVtxPair2Tri[i].first.second==vtx1)
210  {
211  vNeigs.push_back(std::pair<int,int>(tri,vVtxPair2Tri[i].second));
212  }
213  --i;
214  }
215 }
216 
217 
218 #if GEOM_PSEUDO3D==GEOM_TRUE
219 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y,double& z) const
220 {
221  int base(dim*vtxIdx);
222  x=aCoords[base];
223  y=aCoords[base+1];
224  z=aCoords[base+2];
225 }
226 #else
227 inline void FadeExport::getCoordinates(int vtxIdx,double& x,double& y) const
228 {
229  int base(dim*vtxIdx);
230  x=aCoords[base];
231  y=aCoords[base+1];
232 }
233 #endif
234 
235 
236 
237 
238 } // (namespace)
239 
240 
241 
242 
243 #ifndef FADE2D_EXPORT
244  #include "License.h"
245 #endif
246 
FadeExport::dim
int dim
Dimension.
Definition: FadeExport.h:96
FadeExport::numTriangles
int numTriangles
number of triangles
Definition: FadeExport.h:91
FadeExport::writeObj
bool writeObj(const char *filename) const
Write an *.obj file (supported by virtually any 3D viewer)
Definition: FadeExport.h:148
FadeExport::numCustomIndices
int numCustomIndices
number of custom indices (same as numPoints when exported, otherwise 0)
Definition: FadeExport.h:90
FadeExport::aTriangles
int * aTriangles
3 counterclockwise oriented vertex-indices per triangle (3*numTriangles)
Definition: FadeExport.h:95
FadeExport::print
void print() const
Print data for demonstration purposes.
Definition: FadeExport.h:121
FadeExport::extractTriangleNeighborships
void extractTriangleNeighborships(std::vector< std::pair< int, int > > &vNeigs) const
Determine index-pairs of adjacent triangles.
Definition: FadeExport.h:179
FadeExport::aCustomIndices
int * aCustomIndices
Custom indices of the points (only when exported)
Definition: FadeExport.h:94
FadeExport::getCoordinates
void getCoordinates(int vtxIdx, double &x, double &y) const
Get the coorinates for a certain vertex index.
Definition: FadeExport.h:227
FadeExport::aCoords
double * aCoords
Cartesian coordinates (dim*numPoints)
Definition: FadeExport.h:93
FadeExport::getCornerIndices
void getCornerIndices(int triIdx, int &vtxIdx0, int &vtxIdx1, int &vtxIdx2) const
Get the corner indices of a certain triangle.
Definition: FadeExport.h:112
FadeExport::numPoints
int numPoints
number of points
Definition: FadeExport.h:92
FadeExport
FadeExport is a simple struct to export triangulation data.
Definition: FadeExport.h:41