VCG Library
platonic.h
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 
24 #ifndef __VCGLIB_PLATONIC
25 #define __VCGLIB_PLATONIC
26 
27 #include<vcg/math/base.h>
28 #include<vcg/complex/algorithms/refine.h>
29 #include<vcg/complex/algorithms/update/position.h>
30 #include<vcg/complex/algorithms/update/bounding.h>
31 #include<vcg/complex/algorithms/clean.h>
32 #include<vcg/complex/algorithms/polygon_support.h>
33 #include<vcg/complex/algorithms/smooth.h>
34 
35 
36 namespace vcg {
37 namespace tri {
48 template <class TetraMeshType>
49 void Tetrahedron(TetraMeshType &in)
50 {
51  typedef typename TetraMeshType::CoordType CoordType;
52  typedef typename TetraMeshType::VertexPointer VertexPointer;
53  typedef typename TetraMeshType::VertexIterator VertexIterator;
54  typedef typename TetraMeshType::FaceIterator FaceIterator;
55 
56  in.Clear();
59 
60  VertexPointer ivp[4];
61  VertexIterator vi=in.vert.begin();
62  ivp[0]=&*vi;(*vi).P()=CoordType ( 1.0, 1.0, 1.0); ++vi;
63  ivp[1]=&*vi;(*vi).P()=CoordType (-1.0, 1.0,-1.0); ++vi;
64  ivp[2]=&*vi;(*vi).P()=CoordType (-1.0,-1.0, 1.0); ++vi;
65  ivp[3]=&*vi;(*vi).P()=CoordType ( 1.0,-1.0,-1.0);
66 
67  FaceIterator fi=in.face.begin();
68  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[1]; (*fi).V(2)=ivp[2]; ++fi;
69  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[2]; (*fi).V(2)=ivp[3]; ++fi;
70  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[3]; (*fi).V(2)=ivp[1]; ++fi;
71  (*fi).V(0)=ivp[3]; (*fi).V(1)=ivp[2]; (*fi).V(2)=ivp[1];
72 }
73 
74 
77 template <class DodMeshType>
78 void Dodecahedron(DodMeshType & in)
79 {
80  typedef DodMeshType MeshType;
81  typedef typename MeshType::CoordType CoordType;
82  typedef typename MeshType::VertexPointer VertexPointer;
83  typedef typename MeshType::VertexIterator VertexIterator;
84  typedef typename MeshType::FaceIterator FaceIterator;
85  typedef typename MeshType::ScalarType ScalarType;
86  const int N_penta=12;
87  const int N_points=62;
88 
89  int penta[N_penta*3*3]=
90  {20,11, 18, 18, 11, 8, 8, 11, 4,
91  13,23, 4, 4, 23, 8, 8, 23, 16,
92  13, 4, 30, 30, 4, 28, 28, 4, 11,
93  16,34, 8, 8, 34, 18, 18, 34, 36,
94  11,20, 28, 28, 20, 45, 45, 20, 38,
95  13,30, 23, 23, 30, 41, 41, 30, 47,
96  16,23, 34, 34, 23, 50, 50, 23, 41,
97  20,18, 38, 38, 18, 52, 52, 18, 36,
98  30,28, 47, 47, 28, 56, 56, 28, 45,
99  50,60, 34, 34, 60, 36, 36, 60, 52,
100  45,38, 56, 56, 38, 60, 60, 38, 52,
101  50,41, 60, 60, 41, 56, 56, 41, 47 };
102  //A B E D C
103  const ScalarType p=(1.0 + math::Sqrt(5.0)) / 2.0;
104  const ScalarType p2=p*p;
105  const ScalarType p3=p*p*p;
106  ScalarType vv[N_points*3]=
107  {
108  0, 0, 2*p2, p2, 0, p3, p, p2, p3,
109  0, p, p3, -p, p2, p3, -p2, 0, p3,
110  -p, -p2, p3, 0, -p, p3, p, -p2, p3,
111  p3, p, p2, p2, p2, p2, 0, p3, p2,
112  -p2, p2, p2, -p3, p, p2, -p3, -p, p2,
113  -p2, -p2, p2, 0, -p3, p2, p2, -p2, p2,
114  p3, -p, p2, p3, 0, p, p2, p3, p,
115  -p2, p3, p, -p3, 0, p, -p2, -p3, p,
116  p2, -p3, p, 2*p2, 0, 0, p3, p2, 0,
117  p, p3, 0, 0, 2*p2, 0, -p, p3, 0,
118  -p3, p2, 0, -2*p2, 0, 0, -p3, -p2, 0,
119  -p, -p3, 0, 0, -2*p2, 0, p, -p3, 0,
120  p3, -p2, 0, p3, 0, -p, p2, p3, -p,
121  -p2, p3, -p, -p3, 0, -p, -p2, -p3, -p,
122  p2, -p3, -p, p3, p, -p2, p2, p2, -p2,
123  0, p3, -p2, -p2, p2, -p2, -p3, p, -p2,
124  -p3, -p, -p2, -p2, -p2, -p2, 0, -p3, -p2,
125  p2, -p2, -p2, p3, -p, -p2, p2, 0, -p3,
126  p, p2, -p3, 0, p, -p3, -p, p2, -p3,
127  -p2, 0, -p3, -p, -p2, -p3, 0, -p, -p3,
128  p, -p2, -p3, 0, 0, -2*p2
129  };
130  in.Clear();
131  //in.face.clear();
133  Allocator<DodMeshType>::AddFaces(in, 5*12); // five pentagons, each made by 5 tri
134 
135  int h,i,j,m=0;
136 
137  bool used[N_points];
138  for (i=0; i<N_points; i++) used[i]=false;
139 
140  int reindex[20+12 *10];
141  ScalarType xx,yy,zz, sx,sy,sz;
142 
143  int order[5]={0,1,8,6,2};
144  int added[12];
145 
146  VertexIterator vi=in.vert.begin();
147 
148  for (i=0; i<12; i++) {
149  sx=sy=sz=0;
150  for (int j=0; j<5; j++) {
151  h= penta[ i*9 + order[j] ]-1;
152  xx=vv[h*3];yy=vv[h*3+1];zz=vv[h*3+2]; sx+=xx; sy+=yy; sz+=zz;
153  if (!used[h]) {
154  (*vi).P()=CoordType( xx, yy, zz ); vi++;
155  used[h]=true;
156  reindex[ h ] = m++;
157  }
158  }
159  (*vi).P()=CoordType( sx/5.0, sy/5.0, sz/5.0 ); vi++;
160  added[ i ] = m++;
161  }
162 
163  std::vector<VertexPointer> index(in.vn);
164 
165  for(j=0,vi=in.vert.begin();j<in.vn;++j,++vi) index[j] = &(*vi);
166 
167  FaceIterator fi=in.face.begin();
168 
169  for (i=0; i<12; i++) {
170  for (j=0; j<5; j++){
171  (*fi).V(0)=index[added[i] ];
172  (*fi).V(1)=index[reindex[penta[i*9 + order[j ] ] -1 ] ];
173  (*fi).V(2)=index[reindex[penta[i*9 + order[(j+1)%5] ] -1 ] ];
174  if (HasPerFaceFlags(in)) {
175  // tag faux edges
176  (*fi).SetF(0);
177  (*fi).SetF(2);
178  }
179  fi++;
180  }
181  }
182 }
183 
184 template <class OctMeshType>
185 void Octahedron(OctMeshType &in)
186 {
187  typedef OctMeshType MeshType;
188  typedef typename MeshType::CoordType CoordType;
189  typedef typename MeshType::VertexPointer VertexPointer;
190  typedef typename MeshType::VertexIterator VertexIterator;
191  typedef typename MeshType::FaceIterator FaceIterator;
192 
193  in.Clear();
196 
197  VertexPointer ivp[6];
198 
199  VertexIterator vi=in.vert.begin();
200  ivp[0]=&*vi;(*vi).P()=CoordType ( 1, 0, 0); ++vi;
201  ivp[1]=&*vi;(*vi).P()=CoordType ( 0, 1, 0); ++vi;
202  ivp[2]=&*vi;(*vi).P()=CoordType ( 0, 0, 1); ++vi;
203  ivp[3]=&*vi;(*vi).P()=CoordType (-1, 0, 0); ++vi;
204  ivp[4]=&*vi;(*vi).P()=CoordType ( 0,-1, 0); ++vi;
205  ivp[5]=&*vi;(*vi).P()=CoordType ( 0, 0,-1);
206 
207  FaceIterator fi=in.face.begin();
208  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[1]; (*fi).V(2)=ivp[2]; ++fi;
209  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[2]; (*fi).V(2)=ivp[4]; ++fi;
210  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[4]; (*fi).V(2)=ivp[5]; ++fi;
211  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[5]; (*fi).V(2)=ivp[1]; ++fi;
212  (*fi).V(0)=ivp[3]; (*fi).V(1)=ivp[1]; (*fi).V(2)=ivp[5]; ++fi;
213  (*fi).V(0)=ivp[3]; (*fi).V(1)=ivp[5]; (*fi).V(2)=ivp[4]; ++fi;
214  (*fi).V(0)=ivp[3]; (*fi).V(1)=ivp[4]; (*fi).V(2)=ivp[2]; ++fi;
215  (*fi).V(0)=ivp[3]; (*fi).V(1)=ivp[2]; (*fi).V(2)=ivp[1];
216 }
217 
218 template <class IcoMeshType>
219 void Icosahedron(IcoMeshType &in)
220 {
221  typedef IcoMeshType MeshType;
222  typedef typename MeshType::ScalarType ScalarType;
223  typedef typename MeshType::CoordType CoordType;
224  typedef typename MeshType::VertexPointer VertexPointer;
225  typedef typename MeshType::VertexIterator VertexIterator;
226  typedef typename MeshType::FaceIterator FaceIterator;
227 
228  ScalarType L=ScalarType((math::Sqrt(5.0)+1.0)/2.0);
229  CoordType vv[12]={
230  CoordType ( 0, L, 1),
231  CoordType ( 0, L,-1),
232  CoordType ( 0,-L, 1),
233  CoordType ( 0,-L,-1),
234 
235  CoordType ( L, 1, 0),
236  CoordType ( L,-1, 0),
237  CoordType (-L, 1, 0),
238  CoordType (-L,-1, 0),
239 
240  CoordType ( 1, 0, L),
241  CoordType (-1, 0, L),
242  CoordType ( 1, 0,-L),
243  CoordType (-1, 0,-L)
244  };
245 
246  int ff[20][3]={
247  {1,0,4},{0,1,6},{2,3,5},{3,2,7},
248  {4,5,10},{5,4,8},{6,7,9},{7,6,11},
249  {8,9,2},{9,8,0},{10,11,1},{11,10,3},
250  {0,8,4},{0,6,9},{1,4,10},{1,11,6},
251  {2,5,8},{2,9,7},{3,10,5},{3,7,11}
252  };
253 
254 
255  in.Clear();
258  VertexPointer ivp[12];
259 
260  VertexIterator vi;
261  int i;
262  for(i=0,vi=in.vert.begin();vi!=in.vert.end();++i,++vi){
263  (*vi).P()=vv[i];
264  ivp[i]=&*vi;
265  }
266 
267  FaceIterator fi;
268  for(i=0,fi=in.face.begin();fi!=in.face.end();++i,++fi){
269  (*fi).V(0)=ivp[ff[i][0]];
270  (*fi).V(1)=ivp[ff[i][1]];
271  (*fi).V(2)=ivp[ff[i][2]];
272  }
273 }
274 
275 template <class MeshType>
276 void Hexahedron(MeshType &in)
277 {
278  typedef typename MeshType::CoordType CoordType;
279  typedef typename MeshType::VertexPointer VertexPointer;
280  typedef typename MeshType::VertexIterator VertexIterator;
281  typedef typename MeshType::FaceIterator FaceIterator;
282 
283  in.Clear();
286 
287  VertexPointer ivp[8];
288 
289  VertexIterator vi=in.vert.begin();
290 
291  ivp[7]=&*vi;(*vi).P()=CoordType (-1,-1,-1); ++vi;
292  ivp[6]=&*vi;(*vi).P()=CoordType ( 1,-1,-1); ++vi;
293  ivp[5]=&*vi;(*vi).P()=CoordType (-1, 1,-1); ++vi;
294  ivp[4]=&*vi;(*vi).P()=CoordType ( 1, 1,-1); ++vi;
295  ivp[3]=&*vi;(*vi).P()=CoordType (-1,-1, 1); ++vi;
296  ivp[2]=&*vi;(*vi).P()=CoordType ( 1,-1, 1); ++vi;
297  ivp[1]=&*vi;(*vi).P()=CoordType (-1, 1, 1); ++vi;
298  ivp[0]=&*vi;(*vi).P()=CoordType ( 1, 1, 1);
299 
300  FaceIterator fi=in.face.begin();
301  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[1]; (*fi).V(2)=ivp[2]; ++fi;
302  (*fi).V(0)=ivp[3]; (*fi).V(1)=ivp[2]; (*fi).V(2)=ivp[1]; ++fi;
303  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[2]; (*fi).V(2)=ivp[4]; ++fi;
304  (*fi).V(0)=ivp[6]; (*fi).V(1)=ivp[4]; (*fi).V(2)=ivp[2]; ++fi;
305  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[4]; (*fi).V(2)=ivp[1]; ++fi;
306  (*fi).V(0)=ivp[5]; (*fi).V(1)=ivp[1]; (*fi).V(2)=ivp[4]; ++fi;
307  (*fi).V(0)=ivp[7]; (*fi).V(1)=ivp[5]; (*fi).V(2)=ivp[6]; ++fi;
308  (*fi).V(0)=ivp[4]; (*fi).V(1)=ivp[6]; (*fi).V(2)=ivp[5]; ++fi;
309  (*fi).V(0)=ivp[7]; (*fi).V(1)=ivp[6]; (*fi).V(2)=ivp[3]; ++fi;
310  (*fi).V(0)=ivp[2]; (*fi).V(1)=ivp[3]; (*fi).V(2)=ivp[6]; ++fi;
311  (*fi).V(0)=ivp[7]; (*fi).V(1)=ivp[3]; (*fi).V(2)=ivp[5]; ++fi;
312  (*fi).V(0)=ivp[1]; (*fi).V(1)=ivp[5]; (*fi).V(2)=ivp[3];
313 
314  if (HasPerFaceFlags(in)) {
315  FaceIterator fi=in.face.begin();
316  for (int k=0; k<12; k++) {
317  (*fi).SetF(1); fi++;
318  }
319  }
320 
321 }
322 
323 template <class MeshType>
324 void Square(MeshType &in)
325 {
326  typedef typename MeshType::CoordType CoordType;
327  typedef typename MeshType::VertexPointer VertexPointer;
328  typedef typename MeshType::VertexIterator VertexIterator;
329  typedef typename MeshType::FaceIterator FaceIterator;
330 
331  in.Clear();
334 
335  VertexPointer ivp[4];
336 
337  VertexIterator vi=in.vert.begin();
338  ivp[0]=&*vi;(*vi).P()=CoordType ( 1, 0, 0); ++vi;
339  ivp[1]=&*vi;(*vi).P()=CoordType ( 0, 1, 0); ++vi;
340  ivp[2]=&*vi;(*vi).P()=CoordType (-1, 0, 0); ++vi;
341  ivp[3]=&*vi;(*vi).P()=CoordType ( 0,-1, 0);
342 
343  FaceIterator fi=in.face.begin();
344  (*fi).V(0)=ivp[0]; (*fi).V(1)=ivp[1]; (*fi).V(2)=ivp[2]; ++fi;
345  (*fi).V(0)=ivp[2]; (*fi).V(1)=ivp[3]; (*fi).V(2)=ivp[0];
346 
347  if (HasPerFaceFlags(in)) {
348  FaceIterator fi=in.face.begin();
349  for (int k=0; k<2; k++) {
350  (*fi).SetF(2); fi++;
351  }
352  }
353 }
354 
355 template <class MeshType>
356 void SphericalCap(MeshType &in, float angleRad, const int subdiv = 3 )
357 {
358  typedef typename MeshType::CoordType CoordType;
359  typedef typename MeshType::VertexIterator VertexIterator;
360  in.Clear();
361  tri::Allocator<MeshType>::AddVertex(in,CoordType(0,0,0));
362  for(int i=0;i<6;++i)
363  tri::Allocator<MeshType>::AddVertex(in,CoordType(cos(math::ToRad(i*60.0)),sin(math::ToRad(i*60.0)),0));
364 
365  for(int i=0;i<6;++i)
366  tri::Allocator<MeshType>::AddFace(in,&(in.vert[0]),&(in.vert[1+i]),&(in.vert[1+(i+1)%6]));
367 
369  for(int i=0;i<subdiv;++i)
370  {
371  tri::Refine(in, MidPoint<MeshType>(&in));
372 
375 
376  for(int i=0;i<in.vn;++i)
377  if(in.vert[i].IsB())
378  in.vert[i].P().Normalize();
379 
382  tri::Smooth<MeshType>::VertexCoordLaplacian(in,10,true);
383  }
384 
385  float angleHalfRad = angleRad /2.0f;
386  float width = sin(angleHalfRad);
387  tri::UpdatePosition<MeshType>::Scale(in,width);
388  tri::Allocator<MeshType>::CompactEveryVector(in);
389  for(VertexIterator vi=in.vert.begin(); vi!=in.vert.end();++vi)
390  {
391  float cosVi = vi->P().Norm();
392  float angVi = asin (cosVi);
393  vi->P()[2] = cos(angVi) - cos(angleHalfRad);
394  }
395 }
396 
397 // this function build a sphere starting from a eventually not empty mesh.
398 // If the mesh is not empty it is 'spherified' and used as base for the subdivision process.
399 // otherwise an icosahedron is used.
400 template <class MeshType>
401 void Sphere(MeshType &in, const int subdiv = 3 )
402 {
403  typedef typename MeshType::CoordType CoordType;
404  typedef typename MeshType::VertexIterator VertexIterator;
405  typedef typename MeshType::FaceIterator FaceIterator;
406  if(in.vn==0 && in.fn==0) Icosahedron(in);
407 
408  for(VertexIterator vi = in.vert.begin(); vi!=in.vert.end();++vi)
409  vi->P().Normalize();
410 
411  for(int i = 0 ; i < subdiv; ++i)
412  {
413  MeshType newM;
414  for(FaceIterator fi=in.face.begin();fi!=in.face.end();++fi)
415  {
416  CoordType me01 = (fi->P(0)+fi->P(1))/2.0;
417  CoordType me12 = (fi->P(1)+fi->P(2))/2.0;
418  CoordType me20 = (fi->P(2)+fi->P(0))/2.0;
419  tri::Allocator<MeshType>::AddFace(newM,me01,me12,me20);
420  tri::Allocator<MeshType>::AddFace(newM,fi->P(0),me01,me20);
421  tri::Allocator<MeshType>::AddFace(newM,fi->P(1),me12,me01);
422  tri::Allocator<MeshType>::AddFace(newM,fi->P(2),me20,me12);
423  }
426 
427  for(VertexIterator vi = in.vert.begin(); vi != in.vert.end(); ++vi)
428  vi->P().Normalize();
429  }
430 }
431 
432 
434 template <class MeshType>
435 void Cone( MeshType& in,
436  const typename MeshType::ScalarType r1,
437  const typename MeshType::ScalarType r2,
438  const typename MeshType::ScalarType h,
439  const int SubDiv = 36 )
440 {
441  typedef typename MeshType::CoordType CoordType;
442  typedef typename MeshType::VertexPointer VertexPointer;
443  typedef typename MeshType::VertexIterator VertexIterator;
444  typedef typename MeshType::FaceIterator FaceIterator;
445 
446  int i,b1,b2;
447  in.Clear();
448  int VN,FN;
449  if(r1==0 || r2==0) {
450  VN=SubDiv+2;
451  FN=SubDiv*2;
452  } else {
453  VN=SubDiv*2+2;
454  FN=SubDiv*4;
455  }
456 
459  VertexPointer *ivp = new VertexPointer[VN];
460 
461  VertexIterator vi=in.vert.begin();
462  ivp[0]=&*vi;(*vi).P()=CoordType ( 0,-h/2.0,0 ); ++vi;
463  ivp[1]=&*vi;(*vi).P()=CoordType ( 0, h/2.0,0 ); ++vi;
464 
465  b1 = b2 = 2;
466  int cnt=2;
467  if(r1!=0)
468  {
469  for(i=0;i<SubDiv;++i)
470  {
471  double a = math::ToRad(i*360.0/SubDiv);
472  ivp[cnt]=&*vi; (*vi).P()= CoordType(r1*cos(a), -h/2.0, r1*sin(a)); ++vi;++cnt;
473  }
474  b2 += SubDiv;
475  }
476 
477  if(r2!=0)
478  {
479  for(i=0;i<SubDiv;++i)
480  {
481  double a = math::ToRad(i*360.0/SubDiv);
482  ivp[cnt]=&*vi; (*vi).P()= CoordType( r2*cos(a), h/2.0, r2*sin(a)); ++vi;++cnt;
483  }
484  }
485 
486  FaceIterator fi=in.face.begin();
487 
488  if(r1!=0) for(i=0;i<SubDiv;++i,++fi) {
489  (*fi).V(0)=ivp[0];
490  (*fi).V(1)=ivp[b1+i];
491  (*fi).V(2)=ivp[b1+(i+1)%SubDiv];
492  }
493 
494  if(r2!=0) for(i=0;i<SubDiv;++i,++fi) {
495  (*fi).V(0)=ivp[1];
496  (*fi).V(2)=ivp[b2+i];
497  (*fi).V(1)=ivp[b2+(i+1)%SubDiv];
498  }
499 
500  if(r1==0) for(i=0;i<SubDiv;++i,++fi)
501  {
502  (*fi).V(0)=ivp[0];
503  (*fi).V(1)=ivp[b2+i];
504  (*fi).V(2)=ivp[b2+(i+1)%SubDiv];
505  }
506  if(r2==0) for(i=0;i<SubDiv;++i,++fi){
507  (*fi).V(0)=ivp[1];
508  (*fi).V(2)=ivp[b1+i];
509  (*fi).V(1)=ivp[b1+(i+1)%SubDiv];
510  }
511 
512  if(r1!=0 && r2!=0)for(i=0;i<SubDiv;++i)
513  {
514  (*fi).V(0)=ivp[b1+i];
515  (*fi).V(1)=ivp[b2+i];
516  (*fi).V(2)=ivp[b2+(i+1)%SubDiv];
517  ++fi;
518  (*fi).V(0)=ivp[b1+i];
519  (*fi).V(1)=ivp[b2+(i+1)%SubDiv];
520  (*fi).V(2)=ivp[b1+(i+1)%SubDiv];
521  ++fi;
522  }
523 }
524 
525 template <class MeshType>
526 void OrientedCone(MeshType & m,
527  const typename MeshType::CoordType origin,
528  const typename MeshType::CoordType end,
529  const typename MeshType::ScalarType r1,
530  const typename MeshType::ScalarType r2,
531  const int SubDiv = 36 )
532 {
533  typedef typename MeshType::ScalarType ScalarType;
534  typedef typename MeshType::CoordType CoordType;
535  typedef Matrix44<typename MeshType::ScalarType> Matrix44x;
536  Cone(m,r1,r2,Distance(origin,end),SubDiv);
537 
538 // tri::UpdatePosition<MeshType>::Translate(m,CoordType(0,1,0));
539 // tri::UpdatePosition<MeshType>::Scale(m,CoordType(1,0.5f,1));
540 // tri::UpdatePosition<MeshType>::Scale(m,CoordType(xScale,1.0f,yScale));
541 
542 // float height = Distance(origin,end);
543 // tri::UpdatePosition<MeshType>::Scale(m,CoordType(radius,height,radius));
544  CoordType norm = end-origin;
545  ScalarType angleRad = Angle(CoordType(0,1,0),norm);
546  const ScalarType Delta= 0.000000001;
547  Matrix44x rotM;
548  if (fabs(angleRad)<Delta)
549  rotM.SetIdentity();
550  else
551  if (fabs(angleRad-M_PI)<Delta)
552  {
553  CoordType axis = CoordType(0,0,1)^norm;
554  rotM.SetRotateRad(angleRad,axis);
555  }
556  else
557  {
558  CoordType axis = CoordType(0,1,0)^norm;
559  rotM.SetRotateRad(angleRad,axis);
560  }
562  tri::UpdatePosition<MeshType>::Translate(m,origin);
563 
564 }
565 
566 
567 template <class MeshType >
568 void Box(MeshType &in, const typename MeshType::BoxType & bb )
569 {
570  typedef typename MeshType::CoordType CoordType;
571  typedef typename MeshType::VertexPointer VertexPointer;
572  typedef typename MeshType::VertexIterator VertexIterator;
573  typedef typename MeshType::FaceIterator FaceIterator;
574 
575  in.Clear();
577  VertexPointer ivp[8];
578 
579  VertexIterator vi=in.vert.begin();
580  ivp[0]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.min[1],bb.min[2]); ++vi;
581  ivp[1]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.min[1],bb.min[2]); ++vi;
582  ivp[2]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.max[1],bb.min[2]); ++vi;
583  ivp[3]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.max[1],bb.min[2]); ++vi;
584  ivp[4]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.min[1],bb.max[2]); ++vi;
585  ivp[5]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.min[1],bb.max[2]); ++vi;
586  ivp[6]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.max[1],bb.max[2]); ++vi;
587  ivp[7]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.max[1],bb.max[2]);
588 
589  Allocator<MeshType>::AddFace(in,ivp[2],ivp[1],ivp[0]);
590  Allocator<MeshType>::AddFace(in,ivp[1],ivp[2],ivp[3]);
591  Allocator<MeshType>::AddFace(in,ivp[4],ivp[2],ivp[0]);
592  Allocator<MeshType>::AddFace(in,ivp[2],ivp[4],ivp[6]);
593  Allocator<MeshType>::AddFace(in,ivp[1],ivp[4],ivp[0]);
594  Allocator<MeshType>::AddFace(in,ivp[4],ivp[1],ivp[5]);
595  Allocator<MeshType>::AddFace(in,ivp[6],ivp[5],ivp[7]);
596  Allocator<MeshType>::AddFace(in,ivp[5],ivp[6],ivp[4]);
597  Allocator<MeshType>::AddFace(in,ivp[3],ivp[6],ivp[7]);
598  Allocator<MeshType>::AddFace(in,ivp[6],ivp[3],ivp[2]);
599  Allocator<MeshType>::AddFace(in,ivp[5],ivp[3],ivp[7]);
600  Allocator<MeshType>::AddFace(in,ivp[3],ivp[5],ivp[1]);
601 
602  if (HasPerFaceFlags(in)) {
603  FaceIterator fi=in.face.begin();
604  for (int k=0; k<12; k++) {
605  (*fi).SetF(0); fi++;
606  }
607  }
608 
609 }
610 
611 // Torus
612 template <class MeshType>
613 void Torus(MeshType &m, float hRingRadius, float vRingRadius, int hRingDiv=24, int vRingDiv=12 )
614 {
615  typedef typename MeshType::CoordType CoordType;
616  typedef typename MeshType::ScalarType ScalarType;
617  typedef Matrix44<ScalarType> Matrix44x;
618  m.Clear();
619  ScalarType angleStepV = (2.0f*M_PI)/vRingDiv;
620  ScalarType angleStepH = (2.0f*M_PI)/hRingDiv;
621 
622  Allocator<MeshType>::AddVertices(m,(vRingDiv+1)*(hRingDiv+1));
623  for(int i=0;i<hRingDiv+1;++i)
624  {
625  Matrix44x RotM; RotM.SetRotateRad(float(i%hRingDiv)*angleStepH,CoordType(0,0,1));
626  for(int j=0;j<vRingDiv+1;++j)
627  {
628  CoordType p;
629  p[0]= vRingRadius*cos(float(j%vRingDiv)*angleStepV) + hRingRadius;
630  p[1] = 0;
631  p[2]= vRingRadius*sin(float(j%vRingDiv)*angleStepV);
632 
633  m.vert[i*(vRingDiv+1)+j].P() = RotM*p;
634  }
635  }
636  FaceGrid(m,vRingDiv+1,hRingDiv+1);
638  tri::Allocator<MeshType>::CompactEveryVector(m);
639 
640 }
641 
644 template <class ScalarType>
645 static ScalarType _SQfnC(ScalarType a, ScalarType b){
646  return math::Sgn(cos(a))*pow(fabs(cos(a)),b);
647 };
648 template <class ScalarType>
649 static ScalarType _SQfnS(ScalarType a, ScalarType b){
650  return math::Sgn(sin(a))*pow(fabs(sin(a)),b);
651 };
652 
653 
661 template <class MeshType>
662 void SuperToroid(MeshType &m, float hRingRadius, float vRingRadius, float vSquareness, float hSquareness, int hRingDiv=24, int vRingDiv=12 )
663 {
664  typedef typename MeshType::CoordType CoordType;
665  typedef typename MeshType::ScalarType ScalarType;
666  m.Clear();
667  ScalarType angleStepV = (2.0f*M_PI)/vRingDiv;
668  ScalarType angleStepH = (2.0f*M_PI)/hRingDiv;
669 
670  ScalarType u,v;
671  int count;
672  Allocator<MeshType>::AddVertices(m,(vRingDiv+1)*(hRingDiv+1));
673  for(int i=0;i<hRingDiv+1;++i)
674  {
675  u=float(i%hRingDiv)*angleStepH;
676  count=0;
677  for(int j=vRingDiv;j>=0;--j)
678  {
679  CoordType p;
680  v=float(j%vRingDiv)*angleStepV;
681  p[0]= (hRingRadius+vRingRadius*_SQfnC(u,vSquareness))*_SQfnC(v,hSquareness);;
682  p[1]= (hRingRadius+vRingRadius*_SQfnC(u,vSquareness))*_SQfnS(v,hSquareness);
683  p[2] = vRingRadius*_SQfnS(u,vSquareness);
684  m.vert[i*(vRingDiv+1)+count].P() = p;
685  count++;
686  }
687  }
688  FaceGrid(m,vRingDiv+1,hRingDiv+1);
691 
692 }
698 template <class MeshType>
699 void SuperEllipsoid(MeshType &m, float rFeature, float sFeature, float tFeature, int hRingDiv=24, int vRingDiv=12 )
700 {
701  typedef typename MeshType::CoordType CoordType;
702  typedef typename MeshType::ScalarType ScalarType;
703  m.Clear();
704  ScalarType angleStepV = (2.0f*M_PI)/vRingDiv;
705  ScalarType angleStepH = (1.0f*M_PI)/hRingDiv;
706  float u;
707  float v;
708  Allocator<MeshType>::AddVertices(m,(vRingDiv+1)*(hRingDiv+1));
709  for(int i=0;i<hRingDiv+1;++i)
710  {
711  //u=ScalarType(i%hRingDiv)*angleStepH + angleStepH/2.0;
712  u=i*angleStepH;
713  for(int j=0;j<vRingDiv+1;++j)
714  {
715  CoordType p;
716  v=ScalarType(j%vRingDiv)*angleStepV;
717  p[0] = _SQfnC(v,2/rFeature)*_SQfnC(u,2/rFeature);
718  p[1] = _SQfnC(v,2/sFeature)*_SQfnS(u,2/sFeature);
719  p[2] = _SQfnS(v,2/tFeature);
720  m.vert[i*(vRingDiv+1)+j].P() = p;
721  }
722  }
723  FaceGrid(m,vRingDiv+1,hRingDiv+1);
724  tri::Clean<MeshType>::MergeCloseVertex(m,ScalarType(angleStepV*angleStepV*0.001));
726  bool oriented, orientable;
728  tri::Clean<MeshType>::OrientCoherentlyMesh(m,oriented,orientable);
730 }
731 
737 template <class MeshType, class InCoordType, class InFaceIndexType >
738 void BuildMeshFromCoordVectorIndexVector(MeshType & in, const std::vector<InCoordType> & v, const std::vector<InFaceIndexType> & f)
739 {
740  typedef typename MeshType::CoordType CoordType;
741 
742  in.Clear();
744  Allocator<MeshType>::AddFaces(in,f.size());
745 
746  for(size_t i=0;i<v.size();++i)
747  {
748  const InCoordType &vv = v[i];
749  in.vert[i].P() = CoordType( vv[0],vv[1],vv[2]);
750  }
751 
752  for(size_t i=0;i<f.size();++i)
753  {
754  const InFaceIndexType &ff= f[i];
755  assert( ff[0]>=0 && ff[0]<in.vn);
756  assert( ff[1]>=0 && ff[1]<in.vn);
757  assert( ff[2]>=0 && ff[2]<in.vn);
758  in.face[i].V(0) = &in.vert[ ff[0] ];
759  in.face[i].V(1) = &in.vert[ ff[1] ];
760  in.face[i].V(2) = &in.vert[ ff[2] ];
761  }
762 
764 }
765 
766 
767 template <class MeshType,class V>
768 void BuildMeshFromCoordVector( MeshType & in, const V & v)
769 {
770  std::vector<Point3i> dummyfaceVec;
771  BuildMeshFromCoordVectorIndexVector(in,v,dummyfaceVec);
772 }
773 
774 
775 template <class TriMeshType,class EdgeMeshType >
776 void BuildFromNonFaux(TriMeshType &in, EdgeMeshType &out)
777 {
778  tri::RequireCompactness(in);
779  std::vector<typename tri::UpdateTopology<TriMeshType>::PEdge> edgevec;
780  tri::UpdateTopology<TriMeshType>::FillUniqueEdgeVector(in, edgevec, false);
781  out.Clear();
782  for(size_t i=0;i<in.vert.size();++i)
783  tri::Allocator<EdgeMeshType>::AddVertex(out, in.vert[i].P());
784  tri::UpdateFlags<EdgeMeshType>::VertexClearV(out);
785 
786  for(size_t i=0;i<edgevec.size();++i)
787  {
788  int i0 = tri::Index(in,edgevec[i].v[0]);
789  int i1 = tri::Index(in,edgevec[i].v[1]);
790  out.vert[i0].SetV();
791  out.vert[i1].SetV();
792  tri::Allocator<EdgeMeshType>::AddEdge(out,&out.vert[i0],&out.vert[i1]);
793  if(in.vert[i0].IsS()) out.vert[i0].SetS();
794  if(in.vert[i1].IsS()) out.vert[i1].SetS();
795  }
796 
797  for(size_t i=0;i<out.vert.size();++i)
798  if(!out.vert[i].IsV()) tri::Allocator<EdgeMeshType>::DeleteVertex(out,out.vert[i]);
799 
800  tri::Allocator<EdgeMeshType>::CompactEveryVector(out);
801 }
802 
803 // Build a regular grid mesh as a typical height field mesh
804 // x y are the position on the grid scaled by wl and hl (at the end x is in the range 0..wl and y is in 0..hl)
805 // z is taken from the <data> array
806 // Once generated the vertex positions it uses the FaceGrid function to generate the faces;
807 
808 template <class MeshType>
809 void Grid(MeshType & in, int w, int h, float wl, float hl, float *data=0)
810 {
811  typedef typename MeshType::CoordType CoordType;
812 
813  in.Clear();
815 
816  float wld=wl/float(w-1);
817  float hld=hl/float(h-1);
818  float zVal=0;
819  for(int i=0;i<h;++i)
820  for(int j=0;j<w;++j)
821  {
822  if(data) zVal=data[i*w+j];
823  in.vert[i*w+j].P()=CoordType ( j*wld, i*hld, zVal) ;
824  }
825  FaceGrid(in,w,h);
826 }
827 
828 
829 // Build a regular grid mesh of faces as a typical height field mesh
830 // Vertexes are assumed to be already be allocated.
831 
832 template <class MeshType>
833 void FaceGrid(MeshType & in, int w, int h)
834 {
835  assert(in.vn == (int)in.vert.size()); // require a compact vertex vector
836  assert(in.vn >= w*h); // the number of vertices should match the number of expected grid vertices
837 
838  Allocator<MeshType>::AddFaces(in,(w-1)*(h-1)*2);
839 
840 // i+0,j+0 -- i+0,j+1
841 // | \ |
842 // | \ |
843 // | \ |
844 // | \ |
845 // i+1,j+0 -- i+1,j+1
846 //
847  for(int i=0;i<h-1;++i)
848  for(int j=0;j<w-1;++j)
849  {
850  in.face[2*(i*(w-1)+j)+0].V(0) = &(in.vert[(i+1)*w+j+1]);
851  in.face[2*(i*(w-1)+j)+0].V(1) = &(in.vert[(i+0)*w+j+1]);
852  in.face[2*(i*(w-1)+j)+0].V(2) = &(in.vert[(i+0)*w+j+0]);
853 
854  in.face[2*(i*(w-1)+j)+1].V(0) = &(in.vert[(i+0)*w+j+0]);
855  in.face[2*(i*(w-1)+j)+1].V(1) = &(in.vert[(i+1)*w+j+0]);
856  in.face[2*(i*(w-1)+j)+1].V(2) = &(in.vert[(i+1)*w+j+1]);
857  }
858 
859  if (HasPerFaceFlags(in)) {
860  for (int k=0; k<(h-1)*(w-1)*2; k++) {
861  in.face[k].SetF(2);
862  }
863  }
864 
865 }
866 
867 
868 // Build a regular grid mesh of faces as the resulto of a sparsely regularly sampled height field.
869 // Vertexes are assumed to be already be allocated, but not all the grid vertexes are present.
870 // For this purpose vector with a grid of indexes is also passed.
871 // Negative indexes in this vector means that there is no vertex.
872 
873 template <class MeshType>
874 void SparseFaceGrid(MeshType & in, const std::vector<int> &grid, int w, int h)
875 {
876  tri::RequireCompactness(in);
877  assert(in.vn <= w*h); // the number of vertices should match the number of expected grid vertices
878 
879 // V0 V1
880 // i+0,j+0 -- i+0,j+1
881 // | \ |
882 // | \ |
883 // | \ |
884 // | \ |
885 // i+1,j+0 -- i+1,j+1
886 // V2 V3
887 
888 
889  for(int i=0;i<h-1;++i)
890  for(int j=0;j<w-1;++j)
891  {
892  int V0i= grid[(i+0)*w+j+0];
893  int V1i= grid[(i+0)*w+j+1];
894  int V2i= grid[(i+1)*w+j+0];
895  int V3i= grid[(i+1)*w+j+1];
896 
897  int ndone=0;
898  bool quad = (V0i>=0 && V1i>=0 && V2i>=0 && V3i>=0 ) && tri::HasPerFaceFlags(in);
899 
900  if(V0i>=0 && V2i>=0 && V3i>=0 )
901  {
902  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
903  f->V(0)=&(in.vert[V3i]);
904  f->V(1)=&(in.vert[V2i]);
905  f->V(2)=&(in.vert[V0i]);
906  if (quad) f->SetF(2);
907  ndone++;
908  }
909  if(V0i>=0 && V1i>=0 && V3i>=0 )
910  {
911  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
912  f->V(0)=&(in.vert[V0i]);
913  f->V(1)=&(in.vert[V1i]);
914  f->V(2)=&(in.vert[V3i]);
915  if (quad) f->SetF(2);
916  ndone++;
917  }
918 
919  if (ndone==0) { // try diag the other way
920  if(V2i>=0 && V0i>=0 && V1i>=0 )
921  {
922  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
923  f->V(0)=&(in.vert[V2i]);
924  f->V(1)=&(in.vert[V0i]);
925  f->V(2)=&(in.vert[V1i]);
926  ndone++;
927  }
928  if(V1i>=0 && V3i>=0 && V2i>=0 )
929  {
930  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
931  f->V(0)=&(in.vert[V1i]);
932  f->V(1)=&(in.vert[V3i]);
933  f->V(2)=&(in.vert[V2i]);
934  ndone++;
935  }
936  }
937  }
938 }
939 template <class MeshType>
940 void Annulus(MeshType & m, float externalRadius, float internalRadius, int slices)
941 {
942  m.Clear();
943  typename MeshType::VertexIterator vi = vcg::tri::Allocator<MeshType>::AddVertices(m,slices*2);
944 
945  for ( int j = 0; j < slices; ++j)
946  {
947  float x = cos( 2.0 * M_PI / slices * j);
948  float y = sin( 2.0 * M_PI / slices * j);
949 
950  (*vi).P() = typename MeshType::CoordType(x,y,0)*internalRadius;
951  ++vi;
952  (*vi).P() = typename MeshType::CoordType(x,y,0)*externalRadius;
953  ++vi;
954  }
955  typename MeshType::FaceIterator fi ;
956  for ( int j = 0; j < slices; ++j)
957  {
959  (*fi).V(0) = &m.vert[ ((j+0)*2+0)%(slices*2) ];
960  (*fi).V(1) = &m.vert[ ((j+1)*2+1)%(slices*2) ];
961  (*fi).V(2) = &m.vert[ ((j+0)*2+1)%(slices*2) ];
962 
964  (*fi).V(0) = &m.vert[ ((j+1)*2+0)%(slices*2) ];
965  (*fi).V(1) = &m.vert[ ((j+1)*2+1)%(slices*2) ];
966  (*fi).V(2) = &m.vert[ ((j+0)*2+0)%(slices*2) ];
967  }
968 }
969 
970 template <class MeshType>
971 void OrientedAnnulus(MeshType & m, Point3f center, Point3f norm, float externalRadius, float internalRadius, int slices)
972 {
973  Annulus(m,externalRadius,internalRadius, slices);
974  float angleRad = Angle(Point3f(0,0,1),norm);
975  Point3f axis = Point3f(0,0,1)^norm;
976 
977  Matrix44f rotM;
978  rotM.SetRotateRad(angleRad,axis);
980  tri::UpdatePosition<MeshType>::Translate(m,center);
981 }
982 
983 
984 template <class MeshType>
985 void Disk(MeshType & m, int slices)
986 {
987  m.Clear();
988  typename MeshType::VertexIterator vi = vcg::tri::Allocator<MeshType>::AddVertices(m,slices+1);
989  (*vi).P() = typename MeshType::CoordType(0,0,0);
990  ++vi;
991 
992  for ( int j = 0; j < slices; ++j)
993  {
994  float x = cos( 2.0 * M_PI / slices * j);
995  float y = sin( 2.0 * M_PI / slices * j);
996 
997  (*vi).P() = typename MeshType::CoordType(x,y,0);
998  ++vi;
999  }
1000  typename MeshType::FaceIterator fi ;
1001  for ( int j = 0; j < slices; ++j)
1002  {
1003  int a = 1+(j+0)%slices;
1004  int b = 1+(j+1)%slices;
1006  (*fi).V(0) = &m.vert[ 0 ];
1007  (*fi).V(1) = &m.vert[ a ];
1008  (*fi).V(2) = &m.vert[ b ];
1009  }
1010 }
1011 
1012 template <class MeshType>
1013 void OrientedDisk(MeshType &m, int slices, typename MeshType::CoordType center, typename MeshType::CoordType norm, float radius)
1014 {
1015  typedef typename MeshType::ScalarType ScalarType;
1016  typedef typename MeshType::CoordType CoordType;
1017 
1018  Disk(m,slices);
1019  tri::UpdatePosition<MeshType>::Scale(m,radius);
1020  ScalarType angleRad = Angle(CoordType(0,0,1),norm);
1021  CoordType axis = CoordType(0,0,1)^norm;
1022 
1023  Matrix44<ScalarType> rotM;
1024  rotM.SetRotateRad(angleRad,axis);
1026  tri::UpdatePosition<MeshType>::Translate(m,center);
1027 }
1028 
1029 template <class MeshType>
1030 void OrientedEllipticPrism(MeshType & m, const typename MeshType::CoordType origin, const typename MeshType::CoordType end, float radius, float xScale, float yScale,bool capped, int slices=32, int stacks=4 )
1031 {
1032  typedef typename MeshType::ScalarType ScalarType;
1033  typedef typename MeshType::CoordType CoordType;
1034  typedef Matrix44<typename MeshType::ScalarType> Matrix44x;
1035  Cylinder(slices,stacks,m,capped);
1036  tri::UpdatePosition<MeshType>::Translate(m,CoordType(0,1,0));
1037  tri::UpdatePosition<MeshType>::Scale(m,CoordType(1,0.5f,1));
1038  tri::UpdatePosition<MeshType>::Scale(m,CoordType(xScale,1.0f,yScale));
1039 
1040  float height = Distance(origin,end);
1041  tri::UpdatePosition<MeshType>::Scale(m,CoordType(radius,height,radius));
1042  CoordType norm = end-origin;
1043  ScalarType angleRad = Angle(CoordType(0,1,0),norm);
1044  const ScalarType Delta= 0.000000001;
1045  Matrix44x rotM;
1046  if (fabs(angleRad)<Delta)
1047  rotM.SetIdentity();
1048  else
1049  if (fabs(angleRad-M_PI)<Delta)
1050  {
1051  CoordType axis = CoordType(0,0,1)^norm;
1052  rotM.SetRotateRad(angleRad,axis);
1053  }
1054  else
1055  {
1056  CoordType axis = CoordType(0,1,0)^norm;
1057  rotM.SetRotateRad(angleRad,axis);
1058  }
1060  tri::UpdatePosition<MeshType>::Translate(m,origin);
1061 
1062 }
1063 
1064 template <class MeshType>
1065 void OrientedCylinder(MeshType & m, const typename MeshType::CoordType origin, const typename MeshType::CoordType end, float radius, bool capped, int slices=32, int stacks=4 )
1066 {
1067  OrientedEllipticPrism(m,origin,end,radius,1.0f,1.0f,capped,slices,stacks);
1068 }
1069 
1070 
1071 template <class MeshType>
1072 void Cylinder(int slices, int stacks, MeshType & m, bool capped=false)
1073 {
1074  m.Clear();
1075  typename MeshType::VertexIterator vi = vcg::tri::Allocator<MeshType>::AddVertices(m,slices*(stacks+1));
1076  for ( int i = 0; i < stacks+1; ++i)
1077  for ( int j = 0; j < slices; ++j)
1078  {
1079  float x,y,h;
1080  x = cos( 2.0 * M_PI / slices * j);
1081  y = sin( 2.0 * M_PI / slices * j);
1082  h = 2 * i / (float)(stacks) - 1;
1083 
1084  (*vi).P() = typename MeshType::CoordType(x,h,y);
1085  ++vi;
1086  }
1087 
1088  for ( int j = 0; j < stacks; ++j)
1089  for ( int i = 0; i < slices; ++i)
1090  {
1091  int a,b,c,d;
1092  a = (j+0)*slices + i;
1093  b = (j+1)*slices + i;
1094  c = (j+1)*slices + (i+1)%slices;
1095  d = (j+0)*slices + (i+1)%slices;
1096  if(((i+j)%2) == 0){
1097  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ a ], &m.vert[ b ], &m.vert[ c ]);
1098  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ c ], &m.vert[ d ], &m.vert[ a ]);
1099  }
1100  else{
1101  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ b ], &m.vert[ c ], &m.vert[ d ]);
1102  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ d ], &m.vert[ a ], &m.vert[ b ]);
1103  }
1104  }
1105 
1106  if(capped)
1107  {
1108  tri::Allocator<MeshType>::AddVertex(m,typename MeshType::CoordType(0,-1,0));
1109  tri::Allocator<MeshType>::AddVertex(m,typename MeshType::CoordType(0, 1,0));
1110  int base = 0;
1111  for ( int i = 0; i < slices; ++i)
1112  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ m.vn-2 ], &m.vert[ base+i ], &m.vert[ base+(i+1)%slices ]);
1113  base = (stacks)*slices;
1114  for ( int i = 0; i < slices; ++i)
1115  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ m.vn-1 ], &m.vert[ base+(i+1)%slices ], &m.vert[ base+i ]);
1116  }
1117  if (HasPerFaceFlags(m)) {
1118  for (typename MeshType::FaceIterator fi=m.face.begin(); fi!=m.face.end(); fi++) {
1119  (*fi).SetF(2);
1120  }
1121  }
1122 }
1123 
1124 
1125 
1126 class _SphFace;
1127 class _SphVertex;
1128 struct _SphUsedTypes : public UsedTypes< Use<_SphVertex> ::AsVertexType,
1129  Use<_SphFace> ::AsFaceType>{};
1130 
1131 class _SphVertex : public Vertex<_SphUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags >{};
1132 class _SphFace : public Face< _SphUsedTypes, face::VertexRef, face::Normal3f, face::BitFlags, face::FFAdj > {};
1133 class _SphMesh : public tri::TriMesh< vector<_SphVertex>, vector<_SphFace> > {};
1134 
1135 
1136 template <class MeshType>
1137 void BuildPrismFaceShell(MeshType &mIn, MeshType &mOut, float height=0, float inset=0, bool smoothFlag=false )
1138 {
1139  typedef typename MeshType::VertexPointer VertexPointer;
1140  typedef typename MeshType::FacePointer FacePointer;
1141  typedef typename MeshType::CoordType CoordType;
1142  if(height==0) height = mIn.bbox.Diag()/100.0f;
1143  if(inset==0) inset = mIn.bbox.Diag()/200.0f;
1145  tri::UpdateFlags<MeshType>::FaceClearV(mIn);
1147 
1148  for(size_t i=0;i<mIn.face.size();++i) if(!mIn.face[i].IsV())
1149  {
1150  MeshType faceM;
1151  std::vector<VertexPointer> vertVec;
1152  std::vector<FacePointer> faceVec;
1153  tri::PolygonSupport<MeshType,MeshType>::ExtractPolygon(&(mIn.face[i]),vertVec,faceVec);
1154  size_t vn = vertVec.size();
1155 
1156  CoordType nf(0,0,0);
1157  for(size_t j=0;j<faceVec.size();++j)
1158  nf+=vcg::NormalizedTriangleNormal(*faceVec[j]) * DoubleArea(*faceVec[j]);
1159  nf.Normalize();
1160  nf = nf*height/2.0f;
1161 
1162  CoordType bary(0,0,0);
1163  for(size_t j=0;j<faceVec.size();++j)
1164  bary+= Barycenter(*faceVec[j]);
1165  bary/=float(faceVec.size());
1166 
1167  // Add vertices (alternated top and bottom)
1168  tri::Allocator<MeshType>::AddVertex(faceM, bary+nf);
1169  tri::Allocator<MeshType>::AddVertex(faceM, bary-nf);
1170  for(size_t j=0;j<vn;++j){
1171  CoordType delta = (vertVec[j]->P() - bary);
1172  delta.Normalize();
1173  delta = delta*inset;
1174  tri::Allocator<MeshType>::AddVertex(faceM, vertVec[j]->P()-delta+nf);
1175  tri::Allocator<MeshType>::AddVertex(faceM, vertVec[j]->P()-delta-nf);
1176  }
1177 
1178  // Build top and bottom faces
1179  for(size_t j=0;j<vn;++j)
1180  tri::Allocator<MeshType>::AddFace(faceM, 0, 2+(j+0)*2, 2+((j+1)%vn)*2 );
1181  for(size_t j=0;j<vn;++j)
1182  tri::Allocator<MeshType>::AddFace(faceM, 1, 3+((j+1)%vn)*2, 3+(j+0)*2 );
1183 
1184  // Build side strip
1185  for(size_t j=0;j<vn;++j){
1186  size_t j0=j;
1187  size_t j1=(j+1)%vn;
1188  tri::Allocator<MeshType>::AddFace(faceM, 2+ j0*2 + 0 , 2+ j0*2+1, 2+j1*2+0);
1189  tri::Allocator<MeshType>::AddFace(faceM, 2+ j0*2 + 1 , 2+ j1*2+1, 2+j1*2+0);
1190  }
1191 
1192  for(size_t j=0;j<2*vn;++j)
1193  faceM.face[j].SetS();
1194 
1195  if(smoothFlag)
1196  {
1199  tri::Refine(faceM, MidPoint<MeshType>(&faceM),0,true);
1200  tri::Refine(faceM, MidPoint<MeshType>(&faceM),0,true);
1202  tri::Smooth<MeshType>::VertexCoordLaplacian(faceM,2,true,true);
1203  }
1204 
1206 
1207  } // end main loop for each face;
1208 }
1209 
1210 
1211 template <class MeshType>
1212 void BuildCylinderEdgeShell(MeshType &mIn, MeshType &mOut, float radius=0, int slices=16, int stacks=1 )
1213 {
1214  if(radius==0) radius = mIn.bbox.Diag()/100.0f;
1215  typedef typename tri::UpdateTopology<MeshType>::PEdge PEdge;
1216  std::vector<PEdge> edgeVec;
1217  tri::UpdateTopology<MeshType>::FillUniqueEdgeVector(mIn,edgeVec,false);
1218  for(size_t i=0;i<edgeVec.size();++i)
1219  {
1220  MeshType mCyl;
1221  tri::OrientedCylinder(mCyl,edgeVec[i].v[0]->P(),edgeVec[i].v[1]->P(),radius,true,slices,stacks);
1223  }
1224 }
1225 
1226 template <class MeshType>
1227 void BuildSphereVertexShell(MeshType &mIn, MeshType &mOut, float radius=0, int recDiv=2 )
1228 {
1229  if(radius==0) radius = mIn.bbox.Diag()/100.0f;
1230  for(size_t i=0;i<mIn.vert.size();++i)
1231  {
1232  MeshType mSph;
1233  tri::Sphere(mSph,recDiv);
1234  tri::UpdatePosition<MeshType>::Scale(mSph,radius);
1235  tri::UpdatePosition<MeshType>::Translate(mSph,mIn.vert[i].P());
1237  }
1238 }
1239 
1240 template <class MeshType>
1241 void BuildCylinderVertexShell(MeshType &mIn, MeshType &mOut, float radius=0, float height=0, int slices=16, int stacks=1 )
1242 {
1243  typedef typename MeshType::CoordType CoordType;
1244  if(radius==0) radius = mIn.bbox.Diag()/100.0f;
1245  if(height==0) height = mIn.bbox.Diag()/200.0f;
1246  for(size_t i=0;i<mIn.vert.size();++i)
1247  {
1248  CoordType p = mIn.vert[i].P();
1249  CoordType n = mIn.vert[i].N().Normalize();
1250 
1251  MeshType mCyl;
1252  tri::OrientedCylinder(mCyl,p-n*height,p+n*height,radius,true,slices,stacks);
1254  }
1255 }
1256 
1257 
1258 template <class MeshType>
1259 void GenerateCameraMesh(MeshType &in){
1260  typedef typename MeshType::CoordType MV;
1261  MV vv[52]={
1262  MV(-0.000122145 , -0.2 ,0.35),
1263  MV(0.000122145 , -0.2 ,-0.35),MV(-0.000122145 , 0.2 ,0.35),MV(0.000122145 , 0.2 ,-0.35),MV(0.999878 , -0.2 ,0.350349),MV(1.00012 , -0.2 ,-0.349651),MV(0.999878 , 0.2 ,0.350349),MV(1.00012 , 0.2 ,-0.349651),MV(1.28255 , 0.1 ,0.754205),MV(1.16539 , 0.1 ,1.03705),MV(0.88255 , 0.1 ,1.15421),
1264  MV(0.599707 , 0.1 ,1.03705),MV(0.48255 , 0.1 ,0.754205),MV(0.599707 , 0.1 ,0.471362),MV(0.88255 , 0.1 ,0.354205),MV(1.16539 , 0.1 ,0.471362),MV(1.28255 , -0.1 ,0.754205),MV(1.16539 , -0.1 ,1.03705),MV(0.88255 , -0.1 ,1.15421),MV(0.599707 , -0.1 ,1.03705),MV(0.48255 , -0.1 ,0.754205),
1265  MV(0.599707 , -0.1 ,0.471362),MV(1.16539 , -0.1 ,0.471362),MV(0.88255 , -0.1 ,0.354205),MV(3.49164e-005 , 0 ,-0.1),MV(1.74582e-005 , -0.0866025 ,-0.05),MV(-1.74582e-005 , -0.0866025 ,0.05),MV(-3.49164e-005 , 8.74228e-009 ,0.1),MV(-1.74582e-005 , 0.0866025 ,0.05),MV(1.74582e-005 , 0.0866025 ,-0.05),MV(-0.399913 , 1.99408e-022 ,-0.25014),
1266  MV(-0.399956 , -0.216506 ,-0.12514),MV(-0.400044 , -0.216506 ,0.12486),MV(-0.400087 , 2.18557e-008 ,0.24986),MV(-0.400044 , 0.216506 ,0.12486),MV(-0.399956 , 0.216506 ,-0.12514),MV(0.479764 , 0.1 ,0.754205),MV(0.362606 , 0.1 ,1.03705),MV(0.0797637 , 0.1 ,1.15421),MV(-0.203079 , 0.1 ,1.03705),MV(-0.320236 , 0.1 ,0.754205),
1267  MV(-0.203079 , 0.1 ,0.471362),MV(0.0797637 , 0.1 ,0.354205),MV(0.362606 , 0.1 ,0.471362),MV(0.479764 , -0.1 ,0.754205),MV(0.362606 , -0.1 ,1.03705),MV(0.0797637 , -0.1 ,1.15421),MV(-0.203079 , -0.1 ,1.03705),MV(-0.320236 , -0.1 ,0.754205),MV(0.0797637 , -0.1 ,0.354205),MV(0.362606 , -0.1 ,0.471362),
1268  MV(-0.203079 , -0.1 ,0.471362), };
1269  int ff[88][3]={
1270  {0,2,3},
1271  {3,1,0},{4,5,7},{7,6,4},{0,1,5},{5,4,0},{1,3,7},{7,5,1},{3,2,6},{6,7,3},{2,0,4},
1272  {4,6,2},{10,9,8},{10,12,11},{10,13,12},{10,14,13},{10,15,14},{10,8,15},{8,17,16},{8,9,17},{9,18,17},
1273  {9,10,18},{10,19,18},{10,11,19},{11,20,19},{11,12,20},{12,21,20},{12,13,21},{13,23,21},{13,14,23},{14,22,23},
1274  {14,15,22},{15,16,22},{15,8,16},{23,16,17},{23,17,18},{23,18,19},{23,19,20},{23,20,21},{23,22,16},{25,27,26},
1275  {25,28,27},{25,29,28},{25,24,29},{24,31,30},{24,25,31},{25,32,31},{25,26,32},{26,33,32},{26,27,33},{27,34,33},
1276  {27,28,34},{28,35,34},{28,29,35},{29,30,35},{29,24,30},{35,30,31},{35,31,32},{35,32,33},{35,33,34},{42,37,36},
1277  {42,38,37},{42,39,38},{42,40,39},{42,41,40},{42,36,43},{36,45,44},{36,37,45},{37,46,45},{37,38,46},{38,47,46},
1278  {38,39,47},{39,48,47},{39,40,48},{40,51,48},{40,41,51},{41,49,51},{41,42,49},{42,50,49},{42,43,50},{43,44,50},
1279  {43,36,44},{51,44,45},{51,45,46},{51,46,47},{51,47,48},{51,49,50},{51,50,44},
1280  };
1281 
1282  in.Clear();
1285 
1286  in.vn=52;in.fn=88;
1287  int i,j;
1288  for(i=0;i<in.vn;i++)
1289  in.vert[i].P()=vv[i];;
1290 
1291  std::vector<typename MeshType::VertexPointer> index(in.vn);
1292 
1293  typename MeshType::VertexIterator vi;
1294  for(j=0,vi=in.vert.begin();j<in.vn;++j,++vi) index[j] = &*vi;
1295  for(j=0;j<in.fn;++j)
1296  {
1297  in.face[j].V(0)=index[ff[j][0]];
1298  in.face[j].V(1)=index[ff[j][1]];
1299  in.face[j].V(2)=index[ff[j][2]];
1300  }
1301 }
1302 
1303 template <class MeshType>
1304 void OrientedRect(MeshType &square, float width, float height, Point3f c, Point3f dir=Point3f(0,0,0), float angleDeg=0,Point3f preRotTra = Point3f(0,0,0))
1305 {
1306  float zeros[4]={0,0,0,0};
1307  square.Clear();
1308  Matrix44f rotM;
1309  tri::Grid(square,2,2,width,height,zeros);
1310  tri::UpdatePosition<MeshType>::Translate(square,Point3f(-width/2.0f,-height/2.0f,0.0f));
1311  if(angleDeg!=0){
1312  tri::UpdatePosition<MeshType>::Translate(square,preRotTra);
1313  rotM.SetRotateDeg(angleDeg,dir);
1315  }
1316  tri::UpdatePosition<MeshType>::Translate(square,c);
1318 }
1319 
1320 template <class MeshType>
1321 void OrientedSquare(MeshType &square, float width, Point3f c, Point3f dir=Point3f(0,0,0), float angleDeg=0,Point3f preRotTra = Point3f(0,0,0))
1322 {
1323  OrientedRect(square,width,width,c,dir,angleDeg,preRotTra);
1324 }
1325 
1326 
1327 
1329 
1330 } // End Namespace TriMesh
1331 } // End Namespace vcg
1332 #endif