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