versor  3.0
C++11 library for Geometric algebra
vsr_shapes.h
1 /*
2  * =====================================================================================
3  *
4  * Filename: vsr_shapes.h
5  *
6  * Description: meshes made by leveraging cga3d
7  *
8  * Version: 1.0
9  * Created: 12/05/2013 17:25:51
10  * Revision: none
11  * Compiler: gcc
12  *
13  * Author: Pablo Colapinto (), wolftype (gmail)
14  * Organization: Media Arts and Technology Program, UCSB
15  *
16  * =====================================================================================
17  */
18 
19 #include "space/vsr_cga3D_op.h"
20 //#include "vsr_cga3D_funcs.h"
21 
22 #include "gfx/gfx_mesh.h"
23 
24 namespace vsr{ namespace cga {
25 
26  using gfx::Mesh;
27  using gfx::Vec3f;
28 
29  struct Shape {
30 
32  static inline Mesh Torus( double rad = 1.0, int slices =20, int stacks=20){
33  Mesh m;
34 
35  Cir cira = CXZ(.5);
36  Cir cirb = CXZ(1);
37 
38  for (int j = 0; j < slices; ++j){
39  VSR_PRECISION tu = PI * (VSR_PRECISION)j/slices;
40  Pnt pa = Round::pnt_cir(cirb, tu);
41  for (int i = 0; i < stacks; ++i){
42  VSR_PRECISION tv = PI * (VSR_PRECISION)i/stacks;
43  Pnt pb = Round::pnt_cir( (pa <= cira).dual(), tv );
44  m.add(pb);
45  }
46  }
47 
48  //c --- d
49  //|
50  //a --- b
51  for (int j = 0; j < slices; ++j){
52  static bool color = false;
53  color = !color;
54  for (int i = 0; i < stacks; ++i){
55  int a = j * stacks + i;
56  int b = (j < slices - 1) ? a + stacks : i;
57  int c = (i < stacks - 1 ) ? a + 1 : j * stacks;
58  int d = (j < slices - 1 ) ? c + stacks : ( i < stacks -1 ) ? i + 1 : 0 ;
59 
60  m[a].Col.set(color,!color,1,1);
61  m[b].Col.set(color,!color,1,1);
62  m[c].Col.set(color,!color,1,1);
63  m[d].Col.set(color,!color,1,1);
64  m.add(a).add(b).add(c).add(d);
65  }
66  }
67 
68 
69 
70  m.mode(GL::TS);
71  return m;
72  }
73 
75  template<typename T>
76  static inline Mesh Skin( T cir, int num, int res = 10){
77 
78  Mesh m;
79 
80  for (int i = 0; i < res; ++i){
81  double t= 1.0 * i/res;
82 
83  for (int j = 0; j < num; ++j){
84  Vec v = Round::pnt_cir( cir[j], TWOPI * t );
85  m.add( v[0], v[1], v[2] );
86  }
87  }
88 
89  //Calc Indices (FOR TRIANGLE STRIP)
90  int a,b,c,d;
91  for (int i = 0; i < num-1; ++i){
92  for (int j = 0; j < res; ++j){
93 
94  a = j * num + i; //<-- current idx
95  b = a + 1; //<-- next circle
96  if (j>=res-1) {
97  c = i + 1;
98  d = i;
99  }
100  else {
101  c = a + 1 + num ; //<-- next on next circle
102  d = a + num ; //<-- next on same circle
103  }
104  int idx[2] = {a,c};
105  m.add(idx,2);
106  }
107  m.add(i).add(i+1+num);
108  }
109 
110  //calc normals
111  for (int i = 0; i < res; ++i){
112  for (int j = 0; j < num-1; ++j){
113  a = i * num + j; //<-- current idx
114  b = a + 1; //<-- same on next circle
115 
116  if (i>=res-1) {
117  c = j + 1;
118  d = j;
119  }
120  else {
121  c = a + 1 + num ; //<-- next on next circle
122  d = a + num ; //<-- next on same circle
123  }
124  Vec3f ta = m[b].Pos - m[a].Pos;
125  Vec3f tb = m[d].Pos - m[a].Pos;
126  Vec3f tc = -ta.cross(tb);
127  m[a].Norm = tc.unit();
128  }
129  }
130 
131  m.mode( GL::TS );
132  return m;
133  }
134 
135  };
136 } } //vsr::cga::
Common Operations Specific to CGA3D.
Generic Geometric Number Types (templated on an algebra and a basis )
Definition: vsr_algebra.h:69
#define CXZ(f)
A vsr::cga::Circle in xz plane with radius f.
Definition: vsr_cga3D_op.h:624
static Mesh Skin(T cir, int num, int res=10)
Skin Circles – pass in an array or vector of circles.
Definition: vsr_shapes.h:76
Definition: vsr_shapes.h:29
the versor library namespace
Definition: vsr_algebra.h:29
static Mesh Torus(double rad=1.0, int slices=20, int stacks=20)
A Torus Mesh.
Definition: vsr_shapes.h:32