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