Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
TriangularMesh.cpp
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_TriangularMesh_cpp_
9#define model_TriangularMesh_cpp_
10
11#include <vector>
12#include <deque>
13#include <Eigen/Dense>
14#include <TriangularMesh.h>
15
16namespace model
17{
18
19
20const Eigen::Vector2d& TriangularMesh::vertex(const size_t& vID)
21{
22 return vertices()[vID];
23}
24
25std::deque<Eigen::Vector2d>& TriangularMesh::vertices()
26{
27 return *this;
28}
29
30const std::deque<Eigen::Vector2d>& TriangularMesh::vertices() const
31{
32 return *this;
33}
34
35std::deque<Eigen::Vector3i>& TriangularMesh::triangles()
36{
37 return *this;
38}
39
40const std::deque<Eigen::Vector3i>& TriangularMesh::triangles() const
41{
42 return *this;
43}
44
45void TriangularMesh::reMesh(const std::deque<Eigen::Matrix<double,2,1>>& boundaryPts,
46 const std::deque<Eigen::Matrix<double,2,1>>& internalPts,
47 const double& meshSize,
48 const std::string& flags)
49{// https://userpages.umbc.edu/~rostamia/cbook/mesh.c
50 // https://userpages.umbc.edu/~rostamia/cbook/triangle.h
51 // https://people.sc.fsu.edu/~jburkardt/data/poly/poly.html
52 std::cout<<"Creating triangular mesh"<<std::endl;
53 vertices().clear();
54 triangles().clear();
55 if(boundaryPts.size()>=3)
56 {
57
58 Eigen::Matrix<double,2,1> barycenter(Eigen::Matrix<double,2,1>::Zero());
59 for(const auto& bndPoint : boundaryPts)
60 {
61 barycenter+=bndPoint;
62 }
63 barycenter/=boundaryPts.size();
64
65 struct triangulateio in,mid;// out, vorout;
66
67 // numberofReservedpoints=100;
68
69 in.numberofpoints = boundaryPts.size()+internalPts.size();
70 in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
71 for(size_t i=0;i<boundaryPts.size();++i)
72 {
73 // std::cout<<points()[p]<<std::endl;
74 in.pointlist[2*i ] = boundaryPts[i](0);
75 in.pointlist[2*i+1] = boundaryPts[i](1);
76 }
77 for(size_t i=0;i<internalPts.size();++i)
78 {
79 const int i1(i+boundaryPts.size());
80 // std::cout<<points()[p]<<std::endl;
81 in.pointlist[2*i1 ] = internalPts[i](0);
82 in.pointlist[2*i1+1] = internalPts[i](1);
83 }
84 // in.pointlist[2*points().size() ] = 0.5;
85 // in.pointlist[2*points().size()+1] = 1.2;
86
87 // in.pointlist[0] = 0.0;
88 // in.pointlist[1] = 0.0;
89 // in.pointlist[2] = 1.0;
90 // in.pointlist[3] = 0.0;
91 // in.pointlist[4] = 1.0;
92 // in.pointlist[5] = 10.0;
93 // in.pointlist[6] = 0.0;
94 // in.pointlist[7] = 10.0;
95
96 in.numberofpointattributes = 1;
97 in.pointattributelist = (REAL *) malloc(in.numberofpoints *
98 in.numberofpointattributes *
99 sizeof(REAL));
100 for(int i=0;i<in.numberofpoints;++i)
101 {
102 for(int j=0;j<in.numberofpointattributes;++j)
103 {
104 in.pointattributelist[i*in.numberofpointattributes+j] = 1.0;
105 }
106 }
107 // in.pointattributelist[0] = 0.0;
108 // in.pointattributelist[1] = 1.0;
109 // in.pointattributelist[2] = 11.0;
110 // in.pointattributelist[3] = 10.0;
111 in.pointmarkerlist = (int *) malloc(in.numberofpoints * sizeof(int));
112 for(int i=0;i<in.numberofpoints;++i)
113 {
114 in.pointmarkerlist[i] = i;
115 }
116 // in.pointmarkerlist[0] = 0;
117 // in.pointmarkerlist[1] = 2;
118 // in.pointmarkerlist[2] = 0;
119 // in.pointmarkerlist[3] = 0;
120 // in.pointmarkerlist[4] = 2;
121 // in.pointmarkerlist[5] = 2;
122 // in.pointmarkerlist[6] = 0;
123
124
125 // in.numberofsegments = 0;
126
127 in.numberofsegments = boundaryPts.size();
128 in.segmentlist = (int *) malloc(in.numberofsegments * 2 * sizeof(int));
129 // in.segmentmarkerlist = (int *) malloc(in.numberofsegments * sizeof(int));
130 in.segmentmarkerlist = (int *) NULL;
131
132 for(int p=0;p<in.numberofsegments;++p)
133 {
134 // std::cout<<p<<std::endl;
135 in.segmentlist[2*p ] = p;
136 in.segmentlist[2*p+1] = (p<in.numberofsegments-1? p+1 : 0);
137 // in.segmentmarkerlist[p]=p;
138 }
139
140 in.numberofholes = 0;
141
142
143 in.numberofregions = 1;
144 in.regionlist = (REAL *) malloc(in.numberofregions * 4 * sizeof(REAL));
145 // in.regionlist = (REAL *) malloc(in.numberofregions * 4 * sizeof(REAL));
146 in.regionlist[0] = barycenter(0); // x center of region
147 in.regionlist[1] = barycenter(1); // y center of region
148 in.regionlist[2] = 100.0; /* Regional attribute (for whole mesh). */
149 in.regionlist[3] = meshSize; /* Area constraint */ // THIS CONTROLS TH MESH SIZE IN OUT???
150
151
152 // printf("Input point set:\n\n");
153 // report(&in, 1, 1, 1, 1, 1, 1);
154
155 /* Make necessary initializations so that Triangle can return a */
156 /* triangulation in `mid' and a voronoi diagram in `vorout'. */
157
158 mid.pointlist = (REAL *) NULL; /* Not needed if -N switch used. */
159 /* Not needed if -N switch used or number of point attributes is zero: */
160 mid.pointattributelist = (REAL *) NULL;
161 mid.pointmarkerlist = (int *) NULL; /* Not needed if -N or -B switch used. */
162 mid.trianglelist = (int *) NULL; /* Not needed if -E switch used. */
163 /* Not needed if -E switch used or number of triangle attributes is zero: */
164 mid.triangleattributelist = (REAL *) NULL;
165 mid.neighborlist = (int *) NULL; /* Needed only if -n switch used. */
166 /* Needed only if segments are output (-p or -c) and -P not used: */
167 mid.segmentlist = (int *) NULL;
168 /* Needed only if segments are output (-p or -c) and -P and -B not used: */
169 mid.segmentmarkerlist = (int *) NULL;
170 mid.edgelist = (int *) NULL; /* Needed only if -e switch used. */
171 mid.edgemarkerlist = (int *) NULL; /* Needed if -e used and -B not used. */
172 // mid.regionlist = (REAL *) NULL;
173 mid.numberofregions = 0;
174 // mid.regionlist = (REAL *) malloc(mid.numberofregions * 4 * sizeof(REAL));
175 // mid.regionlist[0] = 0.5;
176 // mid.regionlist[1] = 0.5;
177 // mid.regionlist[2] = 10.0; /* Regional attribute (for whole mesh). */
178 // mid.regionlist[3] = 10.0; /* Area constraint that will not be used. */
179
180
181 // vorout.pointlist = (REAL *) NULL; /* Needed only if -v switch used. */
182 // /* Needed only if -v switch used and number of attributes is not zero: */
183 // vorout.pointattributelist = (REAL *) NULL;
184 // vorout.edgelist = (int *) NULL; /* Needed only if -v switch used. */
185 // vorout.normlist = (REAL *) NULL; /* Needed only if -v switch used. */
186
187 /* Triangulate the points. Switches are chosen to read and write a */
188 /* PSLG (p), preserve the convex hull (c), number everything from */
189 /* zero (z), assign a regional attribute to each element (A), and */
190 /* produce an edge list (e), a Voronoi diagram (v), and a triangle */
191 /* neighbor list (n). */
192
193 // triangulate("pzAevn", &in, &mid, &vorout); // G.P. use D for Delaunay triangulation, q to enforce angle quality
194 // std::cout<<"Meshing plane..."<<std::flush;
195 // const auto t0= std::chrono::system_clock::now();
196 triangulate(flags.c_str(), &in, &mid, (struct triangulateio *) NULL);
197// triangulate("pazq", &in, &mid, (struct triangulateio *) NULL);
198
200
201 // std::cout<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<std::endl;
202
203
204 for (int i = 0; i < mid.numberofpoints; i++)
205 {
206 vertices().emplace_back(Eigen::Map<Eigen::Vector2d>(&mid.pointlist[i * 2 ]));
207 }
208
209 for (int i = 0; i < mid.numberoftriangles; i++)
210 {
211 triangles().emplace_back(Eigen::Map<Eigen::Vector3i>(&mid.trianglelist[i * mid.numberofcorners]));
212 }
213
214
215
216
217 /* Free all allocated arrays, including those allocated by Triangle. */
218
219 // printf("Freeing in :\n\n");
220 free(in.pointlist);
221 free(in.pointattributelist);
222 free(in.pointmarkerlist);
223 // printf("Freeing mid :\n\n");
224 free(in.regionlist);
225 free(mid.pointlist);
226 free(mid.pointattributelist);
227 free(mid.pointmarkerlist);
228 free(mid.trianglelist);
229 free(mid.triangleattributelist);
230 // free(mid.trianglearealist);
231 free(mid.neighborlist);
232 free(mid.segmentlist);
233 free(mid.segmentmarkerlist);
234 free(mid.edgelist);
235 free(mid.edgemarkerlist);
236 }
237 // printf("Freeing varout :\n\n");
238
239 // free(vorout.pointlist);
240 // free(vorout.pointattributelist);
241 // free(vorout.edgelist);
242 // free(vorout.normlist);
243
244 // printf("Freeing out :\n\n");
245
246 // free(out.pointlist);
247 // free(out.pointattributelist);
248 // free(out.trianglelist);
249 // free(out.triangleattributelist);
250
251}
252
253void TriangularMesh::reMesh(const std::vector<Eigen::Matrix<double,2,1>>& points,
254 const std::vector<Eigen::Matrix<int,2,1>>& segments,
255 const double& meshSize,
256 const std::string& flags)
257{// https://userpages.umbc.edu/~rostamia/cbook/mesh.c
258 // https://userpages.umbc.edu/~rostamia/cbook/triangle.h
259 // https://people.sc.fsu.edu/~jburkardt/data/poly/poly.html
260 std::cout<<"Creating triangular mesh"<<std::endl;
261 vertices().clear();
262 triangles().clear();
263 if(points.size()>=3)
264 {
265
266 Eigen::Matrix<double,2,1> barycenter(Eigen::Matrix<double,2,1>::Zero());
267 for(const auto& bndPoint : points)
268 {
269 barycenter+=bndPoint;
270 }
271 barycenter/=points.size();
272
273 struct triangulateio in,mid;// out, vorout;
274
275 // numberofReservedpoints=100;
276
277 in.numberofpoints = points.size();
278 in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
279 for(size_t i=0;i<points.size();++i)
280 {
281 // std::cout<<points()[p]<<std::endl;
282 in.pointlist[2*i ] = points[i](0);
283 in.pointlist[2*i+1] = points[i](1);
284 }
285
286 in.numberofpointattributes = 1;
287 in.pointattributelist = (REAL *) malloc(in.numberofpoints *
288 in.numberofpointattributes *
289 sizeof(REAL));
290 for(int i=0;i<in.numberofpoints;++i)
291 {
292 for(int j=0;j<in.numberofpointattributes;++j)
293 {
294 in.pointattributelist[i*in.numberofpointattributes+j] = 1.0;
295 }
296 }
297
298 in.pointmarkerlist = (int *) malloc(in.numberofpoints * sizeof(int));
299 for(int i=0;i<in.numberofpoints;++i)
300 {
301 in.pointmarkerlist[i] = i;
302 }
303
304 in.numberofsegments = segments.size();
305 in.segmentlist = (int *) malloc(in.numberofsegments * 2 * sizeof(int));
306 // in.segmentmarkerlist = (int *) malloc(in.numberofsegments * sizeof(int));
307 in.segmentmarkerlist = (int *) NULL;
308
309 for(int p=0;p<in.numberofsegments;++p)
310 {
311 // std::cout<<p<<std::endl;
312 in.segmentlist[2*p ] = segments[p](0);
313 in.segmentlist[2*p+1] = segments[p](1);
314 // in.segmentmarkerlist[p]=p;
315 }
316
317 in.numberofholes = 0;
318
319
320 in.numberofregions = 1;
321 in.regionlist = (REAL *) malloc(in.numberofregions * 4 * sizeof(REAL));
322 // in.regionlist = (REAL *) malloc(in.numberofregions * 4 * sizeof(REAL));
323 in.regionlist[0] = barycenter(0); // x center of region
324 in.regionlist[1] = barycenter(1); // y center of region
325 in.regionlist[2] = 100.0; /* Regional attribute (for whole mesh). */
326 in.regionlist[3] = meshSize; /* Area constraint */ // THIS CONTROLS TH MESH SIZE IN OUT???
327
328
329 // printf("Input point set:\n\n");
330 // report(&in, 1, 1, 1, 1, 1, 1);
331
332 /* Make necessary initializations so that Triangle can return a */
333 /* triangulation in `mid' and a voronoi diagram in `vorout'. */
334
335 mid.pointlist = (REAL *) NULL; /* Not needed if -N switch used. */
336 /* Not needed if -N switch used or number of point attributes is zero: */
337 mid.pointattributelist = (REAL *) NULL;
338 mid.pointmarkerlist = (int *) NULL; /* Not needed if -N or -B switch used. */
339 mid.trianglelist = (int *) NULL; /* Not needed if -E switch used. */
340 /* Not needed if -E switch used or number of triangle attributes is zero: */
341 mid.triangleattributelist = (REAL *) NULL;
342 mid.neighborlist = (int *) NULL; /* Needed only if -n switch used. */
343 /* Needed only if segments are output (-p or -c) and -P not used: */
344 mid.segmentlist = (int *) NULL;
345 /* Needed only if segments are output (-p or -c) and -P and -B not used: */
346 mid.segmentmarkerlist = (int *) NULL;
347 mid.edgelist = (int *) NULL; /* Needed only if -e switch used. */
348 mid.edgemarkerlist = (int *) NULL; /* Needed if -e used and -B not used. */
349 // mid.regionlist = (REAL *) NULL;
350 mid.numberofregions = 0;
351 // mid.regionlist = (REAL *) malloc(mid.numberofregions * 4 * sizeof(REAL));
352 // mid.regionlist[0] = 0.5;
353 // mid.regionlist[1] = 0.5;
354 // mid.regionlist[2] = 10.0; /* Regional attribute (for whole mesh). */
355 // mid.regionlist[3] = 10.0; /* Area constraint that will not be used. */
356
357
358 // vorout.pointlist = (REAL *) NULL; /* Needed only if -v switch used. */
359 // /* Needed only if -v switch used and number of attributes is not zero: */
360 // vorout.pointattributelist = (REAL *) NULL;
361 // vorout.edgelist = (int *) NULL; /* Needed only if -v switch used. */
362 // vorout.normlist = (REAL *) NULL; /* Needed only if -v switch used. */
363
364 /* Triangulate the points. Switches are chosen to read and write a */
365 /* PSLG (p), preserve the convex hull (c), number everything from */
366 /* zero (z), assign a regional attribute to each element (A), and */
367 /* produce an edge list (e), a Voronoi diagram (v), and a triangle */
368 /* neighbor list (n). */
369
370 // triangulate("pzAevn", &in, &mid, &vorout); // G.P. use D for Delaunay triangulation, q to enforce angle quality
371 // std::cout<<"Meshing plane..."<<std::flush;
372 // const auto t0= std::chrono::system_clock::now();
373 triangulate(flags.c_str(), &in, &mid, (struct triangulateio *) NULL);
374// triangulate("pz", &in, &mid, (struct triangulateio *) NULL);
375
376 // std::cout<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<std::endl;
377
378
379 for (int i = 0; i < mid.numberofpoints; i++)
380 {
381 vertices().emplace_back(Eigen::Map<Eigen::Vector2d>(&mid.pointlist[i * 2 ]));
382 }
383
384 for (int i = 0; i < mid.numberoftriangles; i++)
385 {
386 triangles().emplace_back(Eigen::Map<Eigen::Vector3i>(&mid.trianglelist[i * mid.numberofcorners]));
387 }
388
389
390
391
392 /* Free all allocated arrays, including those allocated by Triangle. */
393
394 // printf("Freeing in :\n\n");
395 free(in.pointlist);
396 free(in.pointattributelist);
397 free(in.pointmarkerlist);
398 // printf("Freeing mid :\n\n");
399 free(in.regionlist);
400 free(mid.pointlist);
401 free(mid.pointattributelist);
402 free(mid.pointmarkerlist);
403 free(mid.trianglelist);
404 free(mid.triangleattributelist);
405 // free(mid.trianglearealist);
406 free(mid.neighborlist);
407 free(mid.segmentlist);
408 free(mid.segmentmarkerlist);
409 free(mid.edgelist);
410 free(mid.edgemarkerlist);
411 }
412 // printf("Freeing varout :\n\n");
413
414 // free(vorout.pointlist);
415 // free(vorout.pointattributelist);
416 // free(vorout.edgelist);
417 // free(vorout.normlist);
418
419 // printf("Freeing out :\n\n");
420
421 // free(out.pointlist);
422 // free(out.pointattributelist);
423 // free(out.trianglelist);
424 // free(out.triangleattributelist);
425
426}
427
428
429
430} // namespace model
431#endif
void reMesh(const std::deque< Eigen::Matrix< double, 2, 1 > > &boundaryPts, const std::deque< Eigen::Matrix< double, 2, 1 > > &internalPts, const double &meshSize, const std::string &flags)
std::deque< Eigen::Vector2d > & vertices()
const Eigen::Vector2d & vertex(const size_t &vID)
std::deque< Eigen::Vector3i > & triangles()
REAL * pointattributelist
Definition triangle.h:260
int * neighborlist
Definition triangle.h:268
int numberofpoints
Definition triangle.h:262
int * edgemarkerlist
Definition triangle.h:284
int numberoftriangles
Definition triangle.h:269
int * trianglelist
Definition triangle.h:265
REAL * pointlist
Definition triangle.h:259
int * segmentmarkerlist
Definition triangle.h:274
int numberofcorners
Definition triangle.h:270
int numberofregions
Definition triangle.h:281
int * pointmarkerlist
Definition triangle.h:261
int * segmentlist
Definition triangle.h:273
REAL * triangleattributelist
Definition triangle.h:266
int * edgelist
Definition triangle.h:283
int j
void in
vertex p[3]
int i
void triangulate()