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,Distance(origin,end)/2,0));
539 
540  CoordType norm = end-origin;
541  ScalarType angleRad = Angle(CoordType(0,1,0),norm);
542  const ScalarType Delta= 0.000000001;
543  Matrix44x rotM;
544  if (fabs(angleRad)<Delta)
545  rotM.SetIdentity();
546  else
547  if (fabs(angleRad-M_PI)<Delta)
548  {
549  CoordType axis = CoordType(0,0,1)^norm;
550  rotM.SetRotateRad(angleRad,axis);
551  }
552  else
553  {
554  CoordType axis = CoordType(0,1,0)^norm;
555  rotM.SetRotateRad(angleRad,axis);
556  }
558  tri::UpdatePosition<MeshType>::Translate(m,origin);
559 }
560 
561 
562 template <class MeshType >
563 void Box(MeshType &in, const typename MeshType::BoxType & bb )
564 {
565  typedef typename MeshType::CoordType CoordType;
566  typedef typename MeshType::VertexPointer VertexPointer;
567  typedef typename MeshType::VertexIterator VertexIterator;
568  typedef typename MeshType::FaceIterator FaceIterator;
569 
570  in.Clear();
572  VertexPointer ivp[8];
573 
574  VertexIterator vi=in.vert.begin();
575  ivp[0]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.min[1],bb.min[2]); ++vi;
576  ivp[1]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.min[1],bb.min[2]); ++vi;
577  ivp[2]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.max[1],bb.min[2]); ++vi;
578  ivp[3]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.max[1],bb.min[2]); ++vi;
579  ivp[4]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.min[1],bb.max[2]); ++vi;
580  ivp[5]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.min[1],bb.max[2]); ++vi;
581  ivp[6]=&*vi;(*vi).P()=CoordType (bb.min[0],bb.max[1],bb.max[2]); ++vi;
582  ivp[7]=&*vi;(*vi).P()=CoordType (bb.max[0],bb.max[1],bb.max[2]);
583 
584  Allocator<MeshType>::AddFace(in,ivp[2],ivp[1],ivp[0]);
585  Allocator<MeshType>::AddFace(in,ivp[1],ivp[2],ivp[3]);
586  Allocator<MeshType>::AddFace(in,ivp[4],ivp[2],ivp[0]);
587  Allocator<MeshType>::AddFace(in,ivp[2],ivp[4],ivp[6]);
588  Allocator<MeshType>::AddFace(in,ivp[1],ivp[4],ivp[0]);
589  Allocator<MeshType>::AddFace(in,ivp[4],ivp[1],ivp[5]);
590  Allocator<MeshType>::AddFace(in,ivp[6],ivp[5],ivp[7]);
591  Allocator<MeshType>::AddFace(in,ivp[5],ivp[6],ivp[4]);
592  Allocator<MeshType>::AddFace(in,ivp[3],ivp[6],ivp[7]);
593  Allocator<MeshType>::AddFace(in,ivp[6],ivp[3],ivp[2]);
594  Allocator<MeshType>::AddFace(in,ivp[5],ivp[3],ivp[7]);
595  Allocator<MeshType>::AddFace(in,ivp[3],ivp[5],ivp[1]);
596 
597  if (HasPerFaceFlags(in)) {
598  FaceIterator fi=in.face.begin();
599  for (int k=0; k<12; k++) {
600  (*fi).SetF(0); fi++;
601  }
602  }
603 
604 }
605 
606 // Torus
607 template <class MeshType>
608 void Torus(MeshType &m, float hRingRadius, float vRingRadius, int hRingDiv=24, int vRingDiv=12 )
609 {
610  typedef typename MeshType::CoordType CoordType;
611  typedef typename MeshType::ScalarType ScalarType;
612  typedef Matrix44<ScalarType> Matrix44x;
613  m.Clear();
614  ScalarType angleStepV = (2.0f*M_PI)/vRingDiv;
615  ScalarType angleStepH = (2.0f*M_PI)/hRingDiv;
616 
617  Allocator<MeshType>::AddVertices(m,(vRingDiv+1)*(hRingDiv+1));
618  for(int i=0;i<hRingDiv+1;++i)
619  {
620  Matrix44x RotM; RotM.SetRotateRad(float(i%hRingDiv)*angleStepH,CoordType(0,0,1));
621  for(int j=0;j<vRingDiv+1;++j)
622  {
623  CoordType p;
624  p[0]= vRingRadius*cos(float(j%vRingDiv)*angleStepV) + hRingRadius;
625  p[1] = 0;
626  p[2]= vRingRadius*sin(float(j%vRingDiv)*angleStepV);
627 
628  m.vert[i*(vRingDiv+1)+j].P() = RotM*p;
629  }
630  }
631  FaceGrid(m,vRingDiv+1,hRingDiv+1);
633  tri::Allocator<MeshType>::CompactEveryVector(m);
634 
635 }
636 
639 template <class ScalarType>
640 static ScalarType _SQfnC(ScalarType a, ScalarType b){
641  return math::Sgn(cos(a))*pow(fabs(cos(a)),b);
642 };
643 template <class ScalarType>
644 static ScalarType _SQfnS(ScalarType a, ScalarType b){
645  return math::Sgn(sin(a))*pow(fabs(sin(a)),b);
646 };
647 
648 
656 template <class MeshType>
657 void SuperToroid(MeshType &m, float hRingRadius, float vRingRadius, float vSquareness, float hSquareness, int hRingDiv=24, int vRingDiv=12 )
658 {
659  typedef typename MeshType::CoordType CoordType;
660  typedef typename MeshType::ScalarType ScalarType;
661  m.Clear();
662  ScalarType angleStepV = (2.0f*M_PI)/vRingDiv;
663  ScalarType angleStepH = (2.0f*M_PI)/hRingDiv;
664 
665  ScalarType u,v;
666  int count;
667  Allocator<MeshType>::AddVertices(m,(vRingDiv+1)*(hRingDiv+1));
668  for(int i=0;i<hRingDiv+1;++i)
669  {
670  u=float(i%hRingDiv)*angleStepH;
671  count=0;
672  for(int j=vRingDiv;j>=0;--j)
673  {
674  CoordType p;
675  v=float(j%vRingDiv)*angleStepV;
676  p[0]= (hRingRadius+vRingRadius*_SQfnC(u,vSquareness))*_SQfnC(v,hSquareness);;
677  p[1]= (hRingRadius+vRingRadius*_SQfnC(u,vSquareness))*_SQfnS(v,hSquareness);
678  p[2] = vRingRadius*_SQfnS(u,vSquareness);
679  m.vert[i*(vRingDiv+1)+count].P() = p;
680  count++;
681  }
682  }
683  FaceGrid(m,vRingDiv+1,hRingDiv+1);
686 
687 }
693 template <class MeshType>
694 void SuperEllipsoid(MeshType &m, float rFeature, float sFeature, float tFeature, int hRingDiv=24, int vRingDiv=12 )
695 {
696  typedef typename MeshType::CoordType CoordType;
697  typedef typename MeshType::ScalarType ScalarType;
698  m.Clear();
699  ScalarType angleStepV = (2.0f*M_PI)/vRingDiv;
700  ScalarType angleStepH = (1.0f*M_PI)/hRingDiv;
701  float u;
702  float v;
703  Allocator<MeshType>::AddVertices(m,(vRingDiv+1)*(hRingDiv+1));
704  for(int i=0;i<hRingDiv+1;++i)
705  {
706  //u=ScalarType(i%hRingDiv)*angleStepH + angleStepH/2.0;
707  u=i*angleStepH;
708  for(int j=0;j<vRingDiv+1;++j)
709  {
710  CoordType p;
711  v=ScalarType(j%vRingDiv)*angleStepV;
712  p[0] = _SQfnC(v,2/rFeature)*_SQfnC(u,2/rFeature);
713  p[1] = _SQfnC(v,2/sFeature)*_SQfnS(u,2/sFeature);
714  p[2] = _SQfnS(v,2/tFeature);
715  m.vert[i*(vRingDiv+1)+j].P() = p;
716  }
717  }
718  FaceGrid(m,vRingDiv+1,hRingDiv+1);
719  tri::Clean<MeshType>::MergeCloseVertex(m,ScalarType(angleStepV*angleStepV*0.001));
721  bool oriented, orientable;
723  tri::Clean<MeshType>::OrientCoherentlyMesh(m,oriented,orientable);
725 }
726 
732 template <class MeshType, class InCoordType, class InFaceIndexType >
733 void BuildMeshFromCoordVectorIndexVector(MeshType & in, const std::vector<InCoordType> & v, const std::vector<InFaceIndexType> & f)
734 {
735  typedef typename MeshType::CoordType CoordType;
736 
737  in.Clear();
739  Allocator<MeshType>::AddFaces(in,f.size());
740 
741  for(size_t i=0;i<v.size();++i)
742  {
743  const InCoordType &vv = v[i];
744  in.vert[i].P() = CoordType( vv[0],vv[1],vv[2]);
745  }
746 
747  for(size_t i=0;i<f.size();++i)
748  {
749  const InFaceIndexType &ff= f[i];
750  assert( ff[0]>=0 && ff[0]<in.vn);
751  assert( ff[1]>=0 && ff[1]<in.vn);
752  assert( ff[2]>=0 && ff[2]<in.vn);
753  in.face[i].V(0) = &in.vert[ ff[0] ];
754  in.face[i].V(1) = &in.vert[ ff[1] ];
755  in.face[i].V(2) = &in.vert[ ff[2] ];
756  }
757 
759 }
760 
761 
762 template <class MeshType,class V>
763 void BuildMeshFromCoordVector( MeshType & in, const V & v)
764 {
765  std::vector<Point3i> dummyfaceVec;
766  BuildMeshFromCoordVectorIndexVector(in,v,dummyfaceVec);
767 }
768 
769 
770 template <class TriMeshType,class EdgeMeshType >
771 void BuildFromFaceEdgeSel(TriMeshType &in, EdgeMeshType &out)
772 {
773  tri::RequireCompactness(in);
774  std::vector<typename tri::UpdateTopology<TriMeshType>::PEdge> edgevec;
775  tri::UpdateTopology<TriMeshType>::FillSelectedFaceEdgeVector(in, edgevec);
776  out.Clear();
777  for(size_t i=0;i<in.vert.size();++i)
778  tri::Allocator<EdgeMeshType>::AddVertex(out, in.vert[i].P());
779  tri::UpdateFlags<EdgeMeshType>::VertexClearV(out);
780 
781  for(size_t i=0;i<edgevec.size();++i)
782  {
783  int i0 = tri::Index(in,edgevec[i].v[0]);
784  int i1 = tri::Index(in,edgevec[i].v[1]);
785  out.vert[i0].SetV();
786  out.vert[i1].SetV();
787  tri::Allocator<EdgeMeshType>::AddEdge(out,&out.vert[i0],&out.vert[i1]);
788  if(in.vert[i0].IsS()) out.vert[i0].SetS();
789  if(in.vert[i1].IsS()) out.vert[i1].SetS();
790  }
791 
792  for(size_t i=0;i<out.vert.size();++i)
793  if(!out.vert[i].IsV()) tri::Allocator<EdgeMeshType>::DeleteVertex(out,out.vert[i]);
794 
795  tri::Allocator<EdgeMeshType>::CompactEveryVector(out);
796 }
797 
798 // Build a regular grid mesh as a typical height field mesh
799 // 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)
800 // z is taken from the <data> array
801 // Once generated the vertex positions it uses the FaceGrid function to generate the faces;
802 
803 template <class MeshType>
804 void Grid(MeshType & in, int w, int h, float wl, float hl, float *data=0)
805 {
806  typedef typename MeshType::CoordType CoordType;
807 
808  in.Clear();
810 
811  float wld=wl/float(w-1);
812  float hld=hl/float(h-1);
813  float zVal=0;
814  for(int i=0;i<h;++i)
815  for(int j=0;j<w;++j)
816  {
817  if(data) zVal=data[i*w+j];
818  in.vert[i*w+j].P()=CoordType ( j*wld, i*hld, zVal) ;
819  }
820  FaceGrid(in,w,h);
821 }
822 
823 
824 // Build a regular grid mesh of faces as a typical height field mesh
825 // Vertexes are assumed to be already be allocated.
826 
827 template <class MeshType>
828 void FaceGrid(MeshType & in, int w, int h)
829 {
830  assert(in.vn == (int)in.vert.size()); // require a compact vertex vector
831  assert(in.vn >= w*h); // the number of vertices should match the number of expected grid vertices
832 
833  Allocator<MeshType>::AddFaces(in,(w-1)*(h-1)*2);
834 
835 // i+0,j+0 -- i+0,j+1
836 // | \ |
837 // | \ |
838 // | \ |
839 // | \ |
840 // i+1,j+0 -- i+1,j+1
841 //
842  for(int i=0;i<h-1;++i)
843  for(int j=0;j<w-1;++j)
844  {
845  in.face[2*(i*(w-1)+j)+0].V(0) = &(in.vert[(i+1)*w+j+1]);
846  in.face[2*(i*(w-1)+j)+0].V(1) = &(in.vert[(i+0)*w+j+1]);
847  in.face[2*(i*(w-1)+j)+0].V(2) = &(in.vert[(i+0)*w+j+0]);
848 
849  in.face[2*(i*(w-1)+j)+1].V(0) = &(in.vert[(i+0)*w+j+0]);
850  in.face[2*(i*(w-1)+j)+1].V(1) = &(in.vert[(i+1)*w+j+0]);
851  in.face[2*(i*(w-1)+j)+1].V(2) = &(in.vert[(i+1)*w+j+1]);
852  }
853 
854  if (HasPerFaceFlags(in)) {
855  for (int k=0; k<(h-1)*(w-1)*2; k++) {
856  in.face[k].SetF(2);
857  }
858  }
859 
860 }
861 
862 
863 // Build a regular grid mesh of faces as the resulto of a sparsely regularly sampled height field.
864 // Vertexes are assumed to be already be allocated, but not all the grid vertexes are present.
865 // For this purpose vector with a grid of indexes is also passed.
866 // Negative indexes in this vector means that there is no vertex.
867 
868 template <class MeshType>
869 void SparseFaceGrid(MeshType & in, const std::vector<int> &grid, int w, int h)
870 {
871  tri::RequireCompactness(in);
872  assert(in.vn <= w*h); // the number of vertices should match the number of expected grid vertices
873 
874 // V0 V1
875 // i+0,j+0 -- i+0,j+1
876 // | \ |
877 // | \ |
878 // | \ |
879 // | \ |
880 // i+1,j+0 -- i+1,j+1
881 // V2 V3
882 
883 
884  for(int i=0;i<h-1;++i)
885  for(int j=0;j<w-1;++j)
886  {
887  int V0i= grid[(i+0)*w+j+0];
888  int V1i= grid[(i+0)*w+j+1];
889  int V2i= grid[(i+1)*w+j+0];
890  int V3i= grid[(i+1)*w+j+1];
891 
892  int ndone=0;
893  bool quad = (V0i>=0 && V1i>=0 && V2i>=0 && V3i>=0 ) && tri::HasPerFaceFlags(in);
894 
895  if(V0i>=0 && V2i>=0 && V3i>=0 )
896  {
897  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
898  f->V(0)=&(in.vert[V3i]);
899  f->V(1)=&(in.vert[V2i]);
900  f->V(2)=&(in.vert[V0i]);
901  if (quad) f->SetF(2);
902  ndone++;
903  }
904  if(V0i>=0 && V1i>=0 && V3i>=0 )
905  {
906  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
907  f->V(0)=&(in.vert[V0i]);
908  f->V(1)=&(in.vert[V1i]);
909  f->V(2)=&(in.vert[V3i]);
910  if (quad) f->SetF(2);
911  ndone++;
912  }
913 
914  if (ndone==0) { // try diag the other way
915  if(V2i>=0 && V0i>=0 && V1i>=0 )
916  {
917  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
918  f->V(0)=&(in.vert[V2i]);
919  f->V(1)=&(in.vert[V0i]);
920  f->V(2)=&(in.vert[V1i]);
921  ndone++;
922  }
923  if(V1i>=0 && V3i>=0 && V2i>=0 )
924  {
925  typename MeshType::FaceIterator f= Allocator<MeshType>::AddFaces(in,1);
926  f->V(0)=&(in.vert[V1i]);
927  f->V(1)=&(in.vert[V3i]);
928  f->V(2)=&(in.vert[V2i]);
929  ndone++;
930  }
931  }
932  }
933 }
934 template <class MeshType>
935 void Annulus(MeshType & m, float externalRadius, float internalRadius, int slices)
936 {
937  m.Clear();
938  typename MeshType::VertexIterator vi = vcg::tri::Allocator<MeshType>::AddVertices(m,slices*2);
939 
940  for ( int j = 0; j < slices; ++j)
941  {
942  float x = cos( 2.0 * M_PI / slices * j);
943  float y = sin( 2.0 * M_PI / slices * j);
944 
945  (*vi).P() = typename MeshType::CoordType(x,y,0)*internalRadius;
946  ++vi;
947  (*vi).P() = typename MeshType::CoordType(x,y,0)*externalRadius;
948  ++vi;
949  }
950  typename MeshType::FaceIterator fi ;
951  for ( int j = 0; j < slices; ++j)
952  {
954  (*fi).V(0) = &m.vert[ ((j+0)*2+0)%(slices*2) ];
955  (*fi).V(1) = &m.vert[ ((j+1)*2+1)%(slices*2) ];
956  (*fi).V(2) = &m.vert[ ((j+0)*2+1)%(slices*2) ];
957 
959  (*fi).V(0) = &m.vert[ ((j+1)*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+0)%(slices*2) ];
962  }
963 }
964 
965 template <class MeshType>
966 void OrientedAnnulus(MeshType & m, Point3f center, Point3f norm, float externalRadius, float internalRadius, int slices)
967 {
968  Annulus(m,externalRadius,internalRadius, slices);
969  float angleRad = Angle(Point3f(0,0,1),norm);
970  Point3f axis = Point3f(0,0,1)^norm;
971 
972  Matrix44f rotM;
973  rotM.SetRotateRad(angleRad,axis);
975  tri::UpdatePosition<MeshType>::Translate(m,center);
976 }
977 
978 
979 template <class MeshType>
980 void Disk(MeshType & m, int slices)
981 {
982  m.Clear();
983  typename MeshType::VertexIterator vi = vcg::tri::Allocator<MeshType>::AddVertices(m,slices+1);
984  (*vi).P() = typename MeshType::CoordType(0,0,0);
985  ++vi;
986 
987  for ( int j = 0; j < slices; ++j)
988  {
989  float x = cos( 2.0 * M_PI / slices * j);
990  float y = sin( 2.0 * M_PI / slices * j);
991 
992  (*vi).P() = typename MeshType::CoordType(x,y,0);
993  ++vi;
994  }
995  typename MeshType::FaceIterator fi ;
996  for ( int j = 0; j < slices; ++j)
997  {
998  int a = 1+(j+0)%slices;
999  int b = 1+(j+1)%slices;
1001  (*fi).V(0) = &m.vert[ 0 ];
1002  (*fi).V(1) = &m.vert[ a ];
1003  (*fi).V(2) = &m.vert[ b ];
1004  }
1005 }
1006 
1007 template <class MeshType>
1008 void OrientedDisk(MeshType &m, int slices, typename MeshType::CoordType center, typename MeshType::CoordType norm, float radius)
1009 {
1010  typedef typename MeshType::ScalarType ScalarType;
1011  typedef typename MeshType::CoordType CoordType;
1012 
1013  Disk(m,slices);
1014  tri::UpdatePosition<MeshType>::Scale(m,radius);
1015  ScalarType angleRad = Angle(CoordType(0,0,1),norm);
1016  CoordType axis = CoordType(0,0,1)^norm;
1017 
1018  Matrix44<ScalarType> rotM;
1019  rotM.SetRotateRad(angleRad,axis);
1021  tri::UpdatePosition<MeshType>::Translate(m,center);
1022 }
1023 
1024 template <class MeshType>
1025 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 )
1026 {
1027  typedef typename MeshType::ScalarType ScalarType;
1028  typedef typename MeshType::CoordType CoordType;
1029  typedef Matrix44<typename MeshType::ScalarType> Matrix44x;
1030  Cylinder(slices,stacks,m,capped);
1031  tri::UpdatePosition<MeshType>::Translate(m,CoordType(0,1,0));
1032  tri::UpdatePosition<MeshType>::Scale(m,CoordType(1,0.5f,1));
1033  tri::UpdatePosition<MeshType>::Scale(m,CoordType(xScale,1.0f,yScale));
1034 
1035  float height = Distance(origin,end);
1036  tri::UpdatePosition<MeshType>::Scale(m,CoordType(radius,height,radius));
1037  CoordType norm = end-origin;
1038  ScalarType angleRad = Angle(CoordType(0,1,0),norm);
1039  const ScalarType Delta= 0.000000001;
1040  Matrix44x rotM;
1041  if (fabs(angleRad)<Delta)
1042  rotM.SetIdentity();
1043  else
1044  if (fabs(angleRad-M_PI)<Delta)
1045  {
1046  CoordType axis = CoordType(0,0,1)^norm;
1047  rotM.SetRotateRad(angleRad,axis);
1048  }
1049  else
1050  {
1051  CoordType axis = CoordType(0,1,0)^norm;
1052  rotM.SetRotateRad(angleRad,axis);
1053  }
1055  tri::UpdatePosition<MeshType>::Translate(m,origin);
1056 
1057 }
1058 
1059 template <class MeshType>
1060 void OrientedCylinder(MeshType & m, const typename MeshType::CoordType origin, const typename MeshType::CoordType end, float radius, bool capped, int slices=32, int stacks=4 )
1061 {
1062  OrientedEllipticPrism(m,origin,end,radius,1.0f,1.0f,capped,slices,stacks);
1063 }
1064 
1065 
1066 template <class MeshType>
1067 void Cylinder(int slices, int stacks, MeshType & m, bool capped=false)
1068 {
1069  m.Clear();
1070  typename MeshType::VertexIterator vi = vcg::tri::Allocator<MeshType>::AddVertices(m,slices*(stacks+1));
1071  for ( int i = 0; i < stacks+1; ++i)
1072  for ( int j = 0; j < slices; ++j)
1073  {
1074  float x,y,h;
1075  x = cos( 2.0 * M_PI / slices * j);
1076  y = sin( 2.0 * M_PI / slices * j);
1077  h = 2 * i / (float)(stacks) - 1;
1078 
1079  (*vi).P() = typename MeshType::CoordType(x,h,y);
1080  ++vi;
1081  }
1082 
1083  for ( int j = 0; j < stacks; ++j)
1084  for ( int i = 0; i < slices; ++i)
1085  {
1086  int a,b,c,d;
1087  a = (j+0)*slices + i;
1088  b = (j+1)*slices + i;
1089  c = (j+1)*slices + (i+1)%slices;
1090  d = (j+0)*slices + (i+1)%slices;
1091  if(((i+j)%2) == 0){
1092  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ a ], &m.vert[ b ], &m.vert[ c ]);
1093  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ c ], &m.vert[ d ], &m.vert[ a ]);
1094  }
1095  else{
1096  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ b ], &m.vert[ c ], &m.vert[ d ]);
1097  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ d ], &m.vert[ a ], &m.vert[ b ]);
1098  }
1099  }
1100 
1101  if(capped)
1102  {
1103  tri::Allocator<MeshType>::AddVertex(m,typename MeshType::CoordType(0,-1,0));
1104  tri::Allocator<MeshType>::AddVertex(m,typename MeshType::CoordType(0, 1,0));
1105  int base = 0;
1106  for ( int i = 0; i < slices; ++i)
1107  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ m.vn-2 ], &m.vert[ base+i ], &m.vert[ base+(i+1)%slices ]);
1108  base = (stacks)*slices;
1109  for ( int i = 0; i < slices; ++i)
1110  vcg::tri::Allocator<MeshType>::AddFace(m, &m.vert[ m.vn-1 ], &m.vert[ base+(i+1)%slices ], &m.vert[ base+i ]);
1111  }
1112  if (HasPerFaceFlags(m)) {
1113  for (typename MeshType::FaceIterator fi=m.face.begin(); fi!=m.face.end(); fi++) {
1114  (*fi).SetF(2);
1115  }
1116  }
1117 }
1118 
1119 
1120 
1121 class _SphFace;
1122 class _SphVertex;
1123 struct _SphUsedTypes : public UsedTypes< Use<_SphVertex> ::AsVertexType,
1124  Use<_SphFace> ::AsFaceType>{};
1125 
1126 class _SphVertex : public Vertex<_SphUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags >{};
1127 class _SphFace : public Face< _SphUsedTypes, face::VertexRef, face::Normal3f, face::BitFlags, face::FFAdj > {};
1128 class _SphMesh : public tri::TriMesh< vector<_SphVertex>, vector<_SphFace> > {};
1129 
1130 
1131 template <class MeshType>
1132 void BuildPrismFaceShell(MeshType &mIn, MeshType &mOut, float height=0, float inset=0, bool smoothFlag=false )
1133 {
1134  typedef typename MeshType::VertexPointer VertexPointer;
1135  typedef typename MeshType::FacePointer FacePointer;
1136  typedef typename MeshType::CoordType CoordType;
1137  if(height==0) height = mIn.bbox.Diag()/100.0f;
1138  if(inset==0) inset = mIn.bbox.Diag()/200.0f;
1140  tri::UpdateFlags<MeshType>::FaceClearV(mIn);
1142 
1143  for(size_t i=0;i<mIn.face.size();++i) if(!mIn.face[i].IsV())
1144  {
1145  MeshType faceM;
1146  std::vector<VertexPointer> vertVec;
1147  std::vector<FacePointer> faceVec;
1148  tri::PolygonSupport<MeshType,MeshType>::ExtractPolygon(&(mIn.face[i]),vertVec,faceVec);
1149  size_t vn = vertVec.size();
1150 
1151  CoordType nf(0,0,0);
1152  for(size_t j=0;j<faceVec.size();++j)
1153  nf+=vcg::NormalizedTriangleNormal(*faceVec[j]) * DoubleArea(*faceVec[j]);
1154  nf.Normalize();
1155  nf = nf*height/2.0f;
1156 
1157  CoordType bary(0,0,0);
1158  for(size_t j=0;j<faceVec.size();++j)
1159  bary+= Barycenter(*faceVec[j]);
1160  bary/=float(faceVec.size());
1161 
1162  // Add vertices (alternated top and bottom)
1163  tri::Allocator<MeshType>::AddVertex(faceM, bary+nf);
1164  tri::Allocator<MeshType>::AddVertex(faceM, bary-nf);
1165  for(size_t j=0;j<vn;++j){
1166  CoordType delta = (vertVec[j]->P() - bary);
1167  delta.Normalize();
1168  delta = delta*inset;
1169  tri::Allocator<MeshType>::AddVertex(faceM, vertVec[j]->P()-delta+nf);
1170  tri::Allocator<MeshType>::AddVertex(faceM, vertVec[j]->P()-delta-nf);
1171  }
1172 
1173  // Build top and bottom faces
1174  for(size_t j=0;j<vn;++j)
1175  tri::Allocator<MeshType>::AddFace(faceM, 0, 2+(j+0)*2, 2+((j+1)%vn)*2 );
1176  for(size_t j=0;j<vn;++j)
1177  tri::Allocator<MeshType>::AddFace(faceM, 1, 3+((j+1)%vn)*2, 3+(j+0)*2 );
1178 
1179  // Build side strip
1180  for(size_t j=0;j<vn;++j){
1181  size_t j0=j;
1182  size_t j1=(j+1)%vn;
1183  tri::Allocator<MeshType>::AddFace(faceM, 2+ j0*2 + 0 , 2+ j0*2+1, 2+j1*2+0);
1184  tri::Allocator<MeshType>::AddFace(faceM, 2+ j0*2 + 1 , 2+ j1*2+1, 2+j1*2+0);
1185  }
1186 
1187  for(size_t j=0;j<2*vn;++j)
1188  faceM.face[j].SetS();
1189 
1190  if(smoothFlag)
1191  {
1194  tri::Refine(faceM, MidPoint<MeshType>(&faceM),0,true);
1195  tri::Refine(faceM, MidPoint<MeshType>(&faceM),0,true);
1197  tri::Smooth<MeshType>::VertexCoordLaplacian(faceM,2,true,true);
1198  }
1199 
1201 
1202  } // end main loop for each face;
1203 }
1204 
1205 
1206 template <class MeshType>
1207 void BuildCylinderEdgeShell(MeshType &mIn, MeshType &mOut, float radius=0, int slices=16, int stacks=1 )
1208 {
1209  if(radius==0) radius = mIn.bbox.Diag()/100.0f;
1210  typedef typename tri::UpdateTopology<MeshType>::PEdge PEdge;
1211  std::vector<PEdge> edgeVec;
1212  tri::UpdateTopology<MeshType>::FillUniqueEdgeVector(mIn,edgeVec,false);
1213  for(size_t i=0;i<edgeVec.size();++i)
1214  {
1215  MeshType mCyl;
1216  tri::OrientedCylinder(mCyl,edgeVec[i].v[0]->P(),edgeVec[i].v[1]->P(),radius,true,slices,stacks);
1218  }
1219 }
1220 
1221 template <class MeshType>
1222 void BuildSphereVertexShell(MeshType &mIn, MeshType &mOut, float radius=0, int recDiv=2 )
1223 {
1224  if(radius==0) radius = mIn.bbox.Diag()/100.0f;
1225  for(size_t i=0;i<mIn.vert.size();++i)
1226  {
1227  MeshType mSph;
1228  tri::Sphere(mSph,recDiv);
1229  tri::UpdatePosition<MeshType>::Scale(mSph,radius);
1230  tri::UpdatePosition<MeshType>::Translate(mSph,mIn.vert[i].P());
1232  }
1233 }
1234 
1235 template <class MeshType>
1236 void BuildCylinderVertexShell(MeshType &mIn, MeshType &mOut, float radius=0, float height=0, int slices=16, int stacks=1 )
1237 {
1238  typedef typename MeshType::CoordType CoordType;
1239  if(radius==0) radius = mIn.bbox.Diag()/100.0f;
1240  if(height==0) height = mIn.bbox.Diag()/200.0f;
1241  for(size_t i=0;i<mIn.vert.size();++i)
1242  {
1243  CoordType p = mIn.vert[i].P();
1244  CoordType n = mIn.vert[i].N().Normalize();
1245 
1246  MeshType mCyl;
1247  tri::OrientedCylinder(mCyl,p-n*height,p+n*height,radius,true,slices,stacks);
1249  }
1250 }
1251 
1252 
1253 template <class MeshType>
1254 void GenerateCameraMesh(MeshType &in){
1255  typedef typename MeshType::CoordType MV;
1256  MV vv[52]={
1257  MV(-0.000122145 , -0.2 ,0.35),
1258  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),
1259  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),
1260  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),
1261  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),
1262  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),
1263  MV(-0.203079 , -0.1 ,0.471362), };
1264  int ff[88][3]={
1265  {0,2,3},
1266  {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},
1267  {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},
1268  {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},
1269  {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},
1270  {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},
1271  {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},
1272  {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},
1273  {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},
1274  {43,36,44},{51,44,45},{51,45,46},{51,46,47},{51,47,48},{51,49,50},{51,50,44},
1275  };
1276 
1277  in.Clear();
1280 
1281  in.vn=52;in.fn=88;
1282  int i,j;
1283  for(i=0;i<in.vn;i++)
1284  in.vert[i].P()=vv[i];;
1285 
1286  std::vector<typename MeshType::VertexPointer> index(in.vn);
1287 
1288  typename MeshType::VertexIterator vi;
1289  for(j=0,vi=in.vert.begin();j<in.vn;++j,++vi) index[j] = &*vi;
1290  for(j=0;j<in.fn;++j)
1291  {
1292  in.face[j].V(0)=index[ff[j][0]];
1293  in.face[j].V(1)=index[ff[j][1]];
1294  in.face[j].V(2)=index[ff[j][2]];
1295  }
1296 }
1297 
1298 template <class MeshType>
1299 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))
1300 {
1301  float zeros[4]={0,0,0,0};
1302  square.Clear();
1303  Matrix44f rotM;
1304  tri::Grid(square,2,2,width,height,zeros);
1305  tri::UpdatePosition<MeshType>::Translate(square,Point3f(-width/2.0f,-height/2.0f,0.0f));
1306  if(angleDeg!=0){
1307  tri::UpdatePosition<MeshType>::Translate(square,preRotTra);
1308  rotM.SetRotateDeg(angleDeg,dir);
1310  }
1311  tri::UpdatePosition<MeshType>::Translate(square,c);
1313 }
1314 
1315 template <class MeshType>
1316 void OrientedSquare(MeshType &square, float width, Point3f c, Point3f dir=Point3f(0,0,0), float angleDeg=0,Point3f preRotTra = Point3f(0,0,0))
1317 {
1318  OrientedRect(square,width,width,c,dir,angleDeg,preRotTra);
1319 }
1320 
1321 
1322 
1324 
1325 } // End Namespace TriMesh
1326 } // End Namespace vcg
1327 #endif