versor  3.0
C++11 library for Geometric algebra
vsr_twist.h
1 /*
2  * HELPFUL FOR TWISTING THINGS
3  *
4  *
5  * Created by x on 1/20/11.
6  * Copyright 2011 x. All rights reserved.
7  *
8  */
9 
10 #ifndef VSR_TWIST_H_INCLUDED
11 #define VSR_TWIST_H_INCLUDED
12 
13 
14 #include "space/vsr_cga3D_op.h"
15 #include "vsr_interp.h"
16 
17 namespace vsr { namespace cga {
18 
20  class Twist{
21 
22  //Biv mBiv; /// Direction and Spin (length)
23  //Drv mDrv; /// Moment (length) and Pitch
24 
25  Biv mBiv;
26  Drv mDrv;
27  Dll mDll;
28 
29  double mExt;
30 
31  public:
32 
33  Twist() : mExt(1){
34  //Default one unit up per half turn
35  //Mot m = Gen::trs3(0,1,0) * Gen::rot( Biv(0,PI/2.0,0) );
36  //m /= m.rnorm();
37  //dll( Gen::log_mot(m) );
38 
39  //dll( Dll(0,1,0,0,-1,0) );
40  mBiv = Biv(0,1,0);
41  mDrv = Drv(0,-1,0);
42  mDll = Dll(0,1,0,0,-1,0);
43  }
44 
45  //pass in 3d axis
46  template<class T>
47  Twist(const T& axis) : mExt(1) {
48  Biv b = Op::dl(axis);
49  Drv d = axis * -1 ;
50  mBiv = b;
51  mDrv = d;
52  dll( b, d );
53  }
54 
55  Twist(double x, double y, double z): mExt(1) {
56  Biv b = Op::dl(Vec(x,y,z));
57  Drv d = Vec(x,y,z) * -1;
58 
59  mBiv = b;
60  mDrv = d;
61 
62  dll( b, d );
63  }
64 
66  template <class T>
67  void moveBy( const T& vec ){
68  mDll = mDll.sp( Gen::trs(vec) );
69  }
70 
71  Vec vec() { return drv().copy<Vec>(); }
72 
74  void set( double distance, double period ){
75  Mot m = Gen::trs( Vec( drv() ).unit() * distance ) * Gen::rot( biv().unit() * period );
76  m /= m.rnorm();
77  mDll = Gen::log( m );
78  }
79 
81  double period() const { return biv().norm(); }
83  double pitch() const {
84  return Vec( drv().copy<Vec>() ).norm();
85  //return Op::sca( Vec( mBiv ) <= Vec ( mDrv ) );
86  //return Op::sca( Vec(mDrv).unit() <= Op::dle(mBiv).unit() ); //Op::pj( Vec(mDrv), Op::dle(mBiv) ).rnorm();
87  }
88 
90  double moment() const { return iphi().rnorm(); }
91 
93  Twist& along(const Dll& d, double period, double pitch){
94 
95  Dll td = d.runit() * period;
96 
97  Biv tb(td); //tb *= period; //Drv tv(td);
98  Drv tv(td);
99 
100  Vec dir = Op::dle(tb);
101 
102  dll(tb, Drv( tv - (dir * pitch).copy<Drv>() ) );
103 
104  return *this;
105 
106  }
107 
108  inline static Dll Along( const Dll& d, double period, double pitch){
109  Dll td = d.runit() * period;
110 
111  Biv tb(td); //tb *= period; //Drv tv(td);
112  Drv tv(td);
113 
114  Vec dir = Op::dle(tb);
115  Drv drv( tv + ( dir * pitch ).copy<Drv>() );
116 
117  return Dll(tb[0], tb[1], tb[2], drv[0], drv[1], drv[2] );
118  }
119 
120  Rot ratio() const { return Gen::ratio( Vec( biv() ), Vec( drv() ) ); }
121  Biv iphi() const { return Op::dle(biv()) ^ Vec(drv()); }
122  Biv pitchPlane() const { return iphi().unit(); }
123 
124  void dll( const Biv& b, const Drv & d) {
125  mDll[0] = b[0]; mDll[1] = b[1]; mDll[2] = b[2];
126  mDll[3] = d[0]; mDll[4] = d[1]; mDll[5] = d[2];
127  }
128  //void dll( const Dll& td ) { mDll = td; }
129  Dll dll( double t ) const { return mDll * t; }
130  Dll dll() const { return mDll; }
131  Dll& dll() { return mDll; }
132  Mot mot() const { return Gen::mot( mDll ); }
133 
134  Biv biv() const { return Biv(mDll); }
135  Drv drv() const { return Drv(mDll); }
136 
137  void period(double theta){
138  //Biv b = biv().unit() * theta;
139  dll( mBiv * theta, drv() );
140  //cout << dll() << endl;
141  }
142 
144  void pitch(double dt) {
145  //Drv d = Vec( drv() ) .unit() * dt;
146  dll(biv(), mDrv * dt);
147  }
148 
149  void ext( double e){
150  mExt = e;
151  }
152 
154  Mot mot( double t ) const { return Gen::mot( mDll * t * mExt ); }
155 
157  void set( const Mot& m){ mDll = Gen::log(m); }
158 
159  friend ostream& operator << (ostream&, const Twist&);
160 
161  };
162 
163  inline ostream& operator << (ostream& os, const Twist& t){
164  os << t.dll() << " pitch: " << t.pitch() << " period: " << t.period() << " moment: " << t.moment();
165  return os;
166  }
167 
169  class CoupledTwist{
170 
171  Twist mTwist[3];
172 
173  public:
174 
175  CoupledTwist() {
176  mTwist[0] = Twist(1,0,0);
177  mTwist[1] = Twist(0,1,0);
178  mTwist[2] = Twist(0,0,1);
179  }
180 
182  Dll& operator [] (int idx) { return mTwist[idx].dll(); }
183  Dll operator [] (int idx) const { return mTwist[idx].dll(); }
184 
185  Twist& x() { return mTwist[0]; }
186  Twist& y() { return mTwist[1]; }
187  Twist& z() { return mTwist[2]; }
188 
189  Twist x() const { return mTwist[0]; }
190  Twist y() const { return mTwist[1]; }
191  Twist z() const { return mTwist[2]; }
192 
194  void ext( double _x, double _y, double _z){
195  mTwist[0].ext(_x);
196  mTwist[1].ext(_y);
197  mTwist[2].ext(_z);
198  }
199 
201  double pitch( int i ) { return mTwist[i].pitch(); }
202  void pitch( double theta, int i ){ mTwist[i].pitch(theta); }
203  //rotate drv part towards biv part to set pitch
204  double period( int i ) { return mTwist[i].period(); }
205  void period( double theta, int i ) { mTwist[i].period(theta); }
206 
207  void pitch(double tx, double ty, double tz) { pitchX(tx); pitchY(ty); pitchZ(tz); }
208  void period(double tx, double ty, double tz) { periodX(tx); periodY(ty); periodZ(tz); }
209 
210  void pitchX( double theta ) { pitch(theta,0); }
211  void pitchY( double theta ) { pitch(theta,1); }
212  void pitchZ( double theta ) { pitch(theta,2); }
213  void periodX( double theta ) { period(theta,0); }
214  void periodY( double theta ) { period(theta,1); }
215  void periodZ( double theta ) { period(theta,2); }
216 
218  Mot mot(double t) { return mTwist[2].mot(t) * mTwist[1].mot(t) * mTwist[0].mot(t); }
220  Dll dll(double t) { Mot m = mot(t); m/=m.rnorm(); return Gen::log( m ); }
221 // Mot motor(double t) { return Gen::mot_dll( dll()*t ); }
222 
224  Dll dll() { return (mTwist[0].dll() + mTwist[1].dll() + mTwist[2].dll() ) /3.0; }
226  //void dll(double t) { mTwist[0].dll(t); mTwist[1].dll(t); mTwist[2].dll(t); }
227 
228  };
229 
231  class NTwist {
232  private:
233 
234  vector<Twist> mTwist;
235 
236  Dll * mDll;
237 
238  int mNum;
239 
240  public:
241 
242  NTwist(int n) : mNum(n) { mTwist.resize(mNum); }
243  ~NTwist() { if (mDll) delete[] mDll; }
244 
246  Dll& operator [] (int idx) { return mTwist[idx].dll(); }
247  Dll operator [] (int idx) const { return mTwist[idx].dll(); }
248 
250  Twist& twist(int idx) { return mTwist[idx]; }
251  Twist twist(int idx) const { return mTwist[idx]; }
252 
254  double pitch( int i ) { return mTwist[i].pitch(); }
255  void pitch( double theta, int i ){ mTwist[i].pitch(theta); }
256  //rotate drv part towards biv part to set pitch
257  double period( int i ) { return mTwist[i].period(); }
258  void period( double theta, int i ) { mTwist[i].period(theta); }
259 
261  Mot mot(double t) {
262  Mot rm;
263  rm[0] = 1;
264  for (int i =mNum-1; i >= 0; --i){
265  rm = rm * mTwist[i].mot(t);
266  }
267  return rm;
268  }
270  Dll dll() {
271  Dll tdll;
272  for (int i = 0; i < mNum; ++i){
273  tdll += mTwist[i].dll();
274  }
275  return tdll / mNum;
276  }
277 
278  void submit(){
279  if (mDll) delete[] mDll;
280  mDll = new Dll[mNum];
281  for (int i = 0; i < mNum; ++i){ mDll[i] = mTwist[i].dll(); }
282  }
283 
285  Dll at(double t) {
286  return Interp::quadric<Dll>(mDll, mNum, t);
287  }
288 
289 
290 
291  };
292 
293 } } //vsr::cga::
294 
295 #endif
Twist & along(const Dll &d, double period, double pitch)
given a (unit?) line, generate a dll with period and pitch along it
Definition: vsr_twist.h:93
Dll & operator[](int idx)
Gets raw data (dual line) of twist.
Definition: vsr_twist.h:182
Common Operations Specific to CGA3D.
double moment() const
Distance from origin.
Definition: vsr_twist.h:90
NDrv< 5 > Drv
DirectionVector
Definition: vsr_cga3D_types.h:84
NVec< 5 > Vec
Vector
Definition: vsr_cga3D_types.h:62
Generic Geometric Number Types (templated on an algebra and a basis )
Definition: vsr_algebra.h:69
Dll & operator[](int idx)
Gets raw data (dual line) of twist.
Definition: vsr_twist.h:246
static Rot rot(const Biv &b)
vsr::cga::Rotor from vsr::cga::Bivector
static Mot mot(const Dll &dll)
Generate a vsr::cga::Motor from a vsr::cga::DualLine Axis.
Dll at(double t)
Interpolated Generators (t from 0 to 1)
Definition: vsr_twist.h:285
Dll dll(double t)
Bivector Generator of motor at t.
Definition: vsr_twist.h:220
void moveBy(const T &vec)
Direction.
Definition: vsr_twist.h:67
NDll< 5 > Dll
DualLine
Definition: vsr_cga3D_types.h:79
double period() const
Period, i.e frequency, of twist (in PI units)
Definition: vsr_twist.h:81
void pitch(double dt)
Pass in 0 to 1 -> if 1 pitch is 1.
Definition: vsr_twist.h:144
Coupled Twist Shape Generation x, y, z (after Rosenhahn et al)
Definition: vsr_twist.h:169
double pitch(int i)
Get Pitch.
Definition: vsr_twist.h:201
GENERIC n-TWIST mechanism.
Definition: vsr_twist.h:231
Mot mot(double t)
Concatenated Motors (x first, then y, then z ) with weight of t.
Definition: vsr_twist.h:261
double pitch(int i)
Get Pitch.
Definition: vsr_twist.h:254
static Rot ratio(const Vec &v, const Vec &v2)
vsr::cga::Rotor that takes one vec to another
Twist & twist(int idx)
Gets Twist at idx.
Definition: vsr_twist.h:250
the versor library namespace
Definition: vsr_algebra.h:29
B copy() const
Copying to type B.
void set(const Mot &m)
Set Twist Axis From Destination Motor.
Definition: vsr_twist.h:157
Mot mot(double t)
Concatenated Motors (x first, then y, then z ) with weight of t.
Definition: vsr_twist.h:218
Mot mot(double t) const
Get Motor at t.
Definition: vsr_twist.h:154
void periodX(double theta)
Set Period.
Definition: vsr_twist.h:213
double pitch() const
Pitch from 0 (othogonal, pure rotation) to 1 ( identical, pure translation )
Definition: vsr_twist.h:83
static Biv log(const Rot &r)
vsr::cga::Bivector log of vsr::cga::Rotor
NBiv< 5 > Biv
Bivector
Definition: vsr_cga3D_types.h:63
void ext(double _x, double _y, double _z)
set extrapolation per twist
Definition: vsr_twist.h:194
static Trs trs(const A &a)
vsr::cga::Translator from any type
Definition: vsr_cga3D_op.h:136
Dll dll()
Averaged Generators.
Definition: vsr_twist.h:224
void set(double distance, double period)
Set Distance and Period.
Definition: vsr_twist.h:74
Dll dll()
Averaged Generators.
Definition: vsr_twist.h:270
Decomposed Dual Line (As a Bivector and Direction Vector to it )
Definition: vsr_twist.h:20
void pitchX(double theta)
Set Pitch.
Definition: vsr_twist.h:210
NMot< 5 > Mot
Motor
Definition: vsr_cga3D_types.h:92