versor  3.0
C++11 library for Geometric algebra
vsr_chain_draw.h
1 
2 #ifndef vsr_chain_draw_INC
3 #define vsr_chain_draw_INC
4 
5 #include "gfx/gfx_mesh.h"
6 #include "gfx/gfx_print.h"
7 
8 #include "vsr_cga3D_draw.h"
9 #include "form/vsr_chain.h"
10 
11 #include <sstream>
12 
13 
14 namespace gfx{
15 
16  //using namespace vsr;
17 
18  using vsr::cga::Chain;
19  using vsr::cga::Frame;
20  using vsr::cga::Prismatic;
21  using vsr::cga::Biv;
22  using vsr::cga::Rot;
23  using vsr::cga::Vec;
24  using vsr::cga::Gen;
25 
26  void Draw(const Chain& c, bool bLoop = false, bool bFrame=true, float r=1.0, float g=1.0, float b=1.0, float a=1.0){
27 
28  if(bFrame){
29  for (int i = 0; i<c.num(); ++i){
30  float t=(float)i/c.num();
31  DrawAt(c[i].z(), c[i].pos(), 1-t,0,t);
32  DrawAt(c[i].y(), c[i].pos(), 1,1,t);
33  DrawAt(c.joint(i).y() * .2, c[i].pos(), 0,1,0);
34  //Draw( c[i].cxy(),0,1,0 );
35  }
36  }
37 
38 
39  glColor4f(r,g,b,a);
40  glBegin( bLoop ? GL_LINE_LOOP : GL_LINE_STRIP);
41  for (int i = 0; i<c.num(); ++i){
42  float t=(float)i/c.num();
43  auto cvec = c[i].vec();
44  auto nvec = c.link(i).vec() <= Vec::y; //for chains with R offset
45  gfx::GL::vertex( cvec.begin() );
46  gfx::GL::vertex( (cvec+c[i].y()*nvec).begin() );
47  }
48  glEnd();
49  }
50 
51 
52  void DrawR(const Frame& f, float r= 1.0, float g = 1.0, float b = 1.0, float a = 1.0){
53 
54  static gfx::Mesh ma = gfx::Mesh::Cylinder( .5, 2, 30);
55 
56  //Frame f = r();
57  Rot rot = f.rot();
58  glPushMatrix();
59  gfx::GL::translate( f.pos()[0], f.pos()[1], f.pos()[2] );// - 2 * f.scale() );
60  gfx::GL::rotate( nga::Gen::aa( rot * Gen::rot( Biv::yz * PIOVERFOUR ) ).begin() );
61  gfx::GL::scale( f.scale() );
62  glColor4f(r,g,b,a);
63  ma.drawElements();
64  glPopMatrix();
65 
66  glPushMatrix();
67 
68  gfx::GL::translate( f.pos()[0], f.pos()[1], f.pos()[2] );
69  gfx::GL::rotate( nga::Gen::aa( rot ).begin() );
70  gfx::GL::translate( 0, f.scale() * .5, 0 );
71 
72  // glPushMatrix();
73  gfx::GL::scale( f.scale() * .5 );
74  glColor4f(g,r,b,a);
75  ma.drawElements();
76  // glPopMatrix();
77 
78  /* gfx::GL::scale( 1, 10, 1); */
79  /* ma.drawElements(g,r,b,a); */
80 
81  glPopMatrix();
82  }
83 
84 
85 
86  void DrawR(const Chain& c, float r= 1.0, float g = 1.0, float b = 1.0, float a = 1.0){
87  for (int i = 0; i<c.num(); ++i){
88  DrawR(c[i], r, g,b,a);
89  }
90  }
91 
92  void DrawR_(const Chain& c, float r= 1.0, float g = 1.0, float b = 1.0, float a = 1.0){
93  for (int i = 0; i<c.num(); ++i){
94  Draw(c[i].cxy(), r, g,b,a);
95  glColor4f(g,r,b,a);
96  gfx::Glyph::Line(c[i].vec(), c[i].vec()+c[i].z()*.5);
97  }
98 
99  /* glBegin(GL_TRIANGLES); */
100  /* for (int i =0;i<c.num();i+=2){ */
101  /* int next = i<(c.num()-1)? i+1 : 0; */
102  /* int next2 = next<(c.num()-1)? next+1 : 0; */
103 
104  /* gfx::GL::normal((c[i].y()^c[next].z()).dual() ); */
105  /* gfx::GL::vertex(c[i].vec()); */
106  /* gfx::GL::vertex(c[next].vec()); */
107  /* gfx::GL::vertex(c[next].vec() + c[next].z()); */
108  /* gfx::GL::normal((c[next].y()^c[next2].z()).dual() ); */
109  /* gfx::GL::vertex(c[next2].vec()); */
110  /* /1* gfx::GL::vertex(c[next].vec()); *1/ */
111  /* /1* gfx::GL::vertex(c[next].vec() - c[next].z()); *1/ */
112  /* } */
113  /* glEnd(); */
114  }
115 
116 
117  void Draw(const Prismatic& p){
118 
119  }
120 
121 
122 namespace tikz{
123 
124  string PrintPS(const Frame& f, int n, bool bx=true, bool by=true, bool bz=true){
125  string c = name("c",n);
126  stringstream os;
127  auto v = f.vec();
128  auto x = f.x(); auto y = f.y(); auto z = f.z();
129  auto sx = name("cx",n); auto sy = name("cy",n); auto sz = name("cz",n);
130  os << "\\coordinate"<<c<<" at "<<coord(v)<<";"<<endl;
131 
132  if (bx) os << "\\coordinate"<<sx<<" at "<<coord(x)<<";"<<endl;
133  if (by) os << "\\coordinate"<<sy<<" at "<<coord(y)<<";"<<endl;
134  if (bz) os << "\\coordinate"<<sz<<" at "<<coord(z)<<";"<<endl;
135 
136  if (bx) os << "\\draw [->]"<<c<<"--+"<<sx<<";"<<endl;
137  if (by) os << "\\draw [->]"<<c<<"--+"<<sy<<";"<<endl;
138  if (bz) os << "\\draw [->]"<<c<<"--+"<<sz<<";"<<endl;
139 
140  auto sxp = name("cxp",n); auto sxn = name("cxn",n);
141  auto syp = name("cyp",n); auto syn = name("cyn",n);
142  auto szp = name("czp",n); auto szn = name("czn",n);
143 
144  os << coord(sxp,x/4.0) << coord(sxn,-x/4.0);
145  os << coord(syp,y/4.0) << coord(syn,-y/4.0);
146  os << coord(szp,z/2.0) << coord(szn,-z/2.0);
147 
148  os << "\\draw[rounded corners=4pt]"<<c<<"+"<<sxp<<"--+"<<syp<<"--+"<<sxn<<"--+"<<syn<<"--cycle;"<<endl;
149  //os << "\\draw[rounded corners=4pt]"<<c<<"+"<<szn<<"--+"<<sxp<<"--+"<<syp<<"--+"<<sxn<<"--+"<<syn<<"--cycle;"<<endl;
150  //os << "\\draw[-]
151 
152  return os.str();
153  }
154 
155 
156 
157  string PrintPS(const Chain& c){
158  string s = begin();
159  for (int i = 0;i<c.num();++i){
160  int next = i<(c.num()-1)? i+1:0;
161  s+=PrintPS(c[i],i,false,false,true);
162  s+=draw(c[i].vec(), c[next].vec(),"-");
163  }
164  return s+ end();
165  }
166 }//tikz::
167 
168 }
169 
170 #endif /* ----- #ifndef vsr_chain_draw_INC ----- */
NVec< 5 > Vec
Vector
Definition: vsr_cga3D_types.h:62
Definition: vsr_cga2D_draw.h:13
static Rot rot(const Biv &b)
vsr::cga::Rotor from vsr::cga::Bivector
A sequence of spatial Frames.
Definition: vsr_chain.h:107
Generators and Logarithms Optimized for 3D Conformal Geometric Algebra.
Definition: vsr_cga3D_op.h:119
Orthonormal Frame composed from a Position and Orientation.
Definition: vsr_cga3D_frame.h:47
NBiv< 5 > Biv
Bivector
Definition: vsr_cga3D_types.h:63
Lin Line
Direct Line Trivector \(p_a \wedge p_b \wedge n_\infty\)
Definition: vsr_cga3D_types.h:140
Definition: vsr_chain.h:61
NRot< 5 > Rot
Rotor
Definition: vsr_cga3D_types.h:64