versor  3.0
C++11 library for Geometric algebra
vsr_cga3D_op.h
Go to the documentation of this file.
1 
10 #ifndef CGA3D_OPERATIONS_H_INCLUDED
11 #define CGA3D_OPERATIONS_H_INCLUDED
12 
13 #include <math.h>
14 #include <vector>
15 
16 #include "vsr_cga3D_types.h"
17 #include "vsr_cga3D_round.h"
18 
19 #include "detail/vsr_generic_op.h"
20 #include "util/vsr_constants.h"
21 #include "util/vsr_math.h"
22 
23 namespace vsr{ namespace cga{
24 
46  struct Op{
47 
48  static Rot AA( const Vec& s);
49  static Rot AA( const Dlp& s);
50  static Rot AA( const Cir& s);
51  static Rot AA( const Biv& s);
52  static Vec Pos( const Dlp& s);
53  static Pnt Pos( const Cir& s);
54 
55  template<class A>
56  static auto dual( const A& a ) RETURNS (
57  a.dual()
58  )
59 
60  template<class A>
61  static auto undual( const A& a ) RETURNS (
62  a.undual()
63  )
64 
65  template<class A>
66  static auto duale( const A& a ) RETURNS (
67  a.duale()
68  )
69 
70  template<class A>
71  static auto unduale( const A& a ) RETURNS (
72  a.unduale()
73  )
74 
75  template<class T> static auto dl( const T& t ) RETURNS ( dual(t) )
76  template<class T> static auto udl( const T& t) RETURNS ( udual(t) )
77  template<class T> static auto dle( const T& t) RETURNS (duale(t))
78  template<class T> static auto udle( const T& t) RETURNS (unduale(t))
79 
80 
81  template<class A>
82  static constexpr bool sign(const A& a, const A& b) {
83  return (a / b)[0] > 0 ? 1 : 0;
84  }
85 
87  template<class A>
88  static constexpr bool sn(const A& a, const A& b){
89  return sign(a,b);
90  }
91 
93  template< class A, class B>
94  static constexpr auto project(const A& a, const B& b) RETURNS (
95  (a <= b ) / b
96  )
97 
98 
99  template< class A, class B>
100  static constexpr auto reject(const A& a, const B& b) RETURNS (
101  (a ^ b ) / b
102  )
103 
105  template< class A, class B>
106  static constexpr auto pj(const A& a, const B& b) RETURNS ( project(a,b) )
107  template< class A, class B>
108  static constexpr auto rj(const A& a, const B& b) RETURNS ( reject(a,b) )
109 
110 
111  };
112 
113 
119  struct Gen{
120 
122  static Rot rot( const Biv& b);
124  static Rot rotor( const Biv& b);
126  static Bst bst(const Pair& p);
128  static Bst boost(const Pair& p);
130  static Tsd dil(const Pnt& p, VSR_PRECISION t);
132  static Tsd dilator(const Pnt& p, VSR_PRECISION t);
133 
135  template<class A>
136  static Trs trs(const A& a) { return nga::Gen::trs(a); }
138  template<class A>
139  static Trs translator(const A& a) { return nga::Gen::trs(a); }
141  template<class A>
142  static Trv trv(const A& a){ return nga::Gen::trv(a); }
144  template<class A>
145  static Trv transversor(const A& a){ return nga::Gen::trv(a); }
146 
147 
149  static Rot ratio(const Vec& v, const Vec& v2);
151  static Biv log(const Rot& r);
152 
153 
158  static Rot rot(double theta, double phi);
159 
165  static Rot rot( double yaw, double pitch, double roll);
166 
167  /*-----------------------------------------------------------------------------
168  * TWISTS (Motors, Pl├╝cker, etc)
169  *-----------------------------------------------------------------------------*/
173  static Mot mot( const Dll& dll);
174 
178  static Mot motor( const Dll& dll);
179 
183  static Dll log( const Mot& m);
184 
193  static Dll log(const Dll& a, const Dll& b, VSR_PRECISION t = 1.0);
194 
203  static Mot ratio( const vsr::cga::Dll& a, const vsr::cga::Dll& b, VSR_PRECISION t = 1.0);
204 
205  // Due to overloading, it is also possible to use Motors as Arguments
206 
215  static Mot ratio( const vsr::cga::Mot& a, const vsr::cga::Mot& b, VSR_PRECISION t);
216 
217  /*-----------------------------------------------------------------------------
218  * BOOSTS (Transversions, Conformal Rotors)
219  *-----------------------------------------------------------------------------*/
220 
226  template <class A, class T>
227  static Bst bst(const A& tnv, const Vec& vec, T t){
228  Par s = Par( tnv.template copy<Tnv>() ).sp( nga::Gen::trs(vec) );
229  return Gen::bst(s * t);
230  }
231 
232 
233 
237  static Bst ratio( const DualSphere& a, const DualSphere& b, bool bFlip = true);
238 
240  static Pair atanh2( const Pair& p, VSR_PRECISION cs, bool bCW);
241 
243  static Pair log( const Bst& b, bool bCW = false);
244 
248  static Con ratio( const Circle& a, const Circle& b, bool bFlip = false, float theta = 0);
249 
253  static Con ratio( const Pair& a, const Pair& b,bool bFlip = false, float theta = 0);//{ return ratio( a.dual(), b.dual() ); }
254 
259  static vector<Pair> split(const Pair& par);
260 
265  static vector<Pair> split(const Con& con);
266 
267 
269  static vector<Pair> log( const Con& rot);
270 
272  static vector<Pair> log( const Circle& ca, const Circle& cb, bool bFlip = false, VSR_PRECISION theta = 0);
273 
275  static vector<Pair> log( const Pair& ca, const Pair& cb, bool bFlip = false, VSR_PRECISION theta = 0);
276 
277 
279  static Con con( const vector<Pair>& log, VSR_PRECISION amt);
280 
282  static Con con( const vector<Pair>& log, VSR_PRECISION amtA, VSR_PRECISION amtB);
283 
284 
285  /* General Conformal Transformation from two circles */
286  static Con con( const Circle& ca, const Circle& cb, VSR_PRECISION amt);
287 
288  /* General Conformal Transformation from two circles and two weights */
289  static Con con( const Circle& ca, const Circle& cb, VSR_PRECISION amtA, VSR_PRECISION amtB);
290 
291 
295  static Rotor xf(const Biv& b);
299  static Motor xf(const DualLine& dll);
303  static Dilator xf(const FlatPoint& flp);
304 
308  static Bst xf(const Pair& p);
309 
310 };
311 
312 
313  /*-----------------------------------------------------------------------------
314  * ROTORS
315  *-----------------------------------------------------------------------------*/
316 
317 
330  struct Construct {
331 
332  /*-----------------------------------------------------------------------------
333  * PAIRS
334  *-----------------------------------------------------------------------------*/
335 
340  static Pair pair(const DualSphere& s, const Vec& v);
341 
345  static Pair pair(VSR_PRECISION x, VSR_PRECISION y, VSR_PRECISION z, Vec vec = Vec::y, VSR_PRECISION r=1.0);
346 
347 
348  /*-----------------------------------------------------------------------------
349  * POINTS
350  *-----------------------------------------------------------------------------*/
354  static Point pointA(const Pair& pp);
355 
359  static Point pointB(const Pair& pp);
360 
362  static Point point(const Circle& c, VSR_PRECISION t);
364  static Point point(const DualSphere& s, const Vec& v);
366  static Point point(VSR_PRECISION x, VSR_PRECISION y, VSR_PRECISION z);
368  static Point point(const Vec& v);
370  static Point point(const Line&, const Point& );
372  static Point point(const DualLine&, const Point& );
373 
374 
375  /*-----------------------------------------------------------------------------
376  * CIRCLES
377  *-----------------------------------------------------------------------------*/
378 
382  static Circle circle(const Point& a, const Point& b, const Point& c);
383 
387  static Circle circle(const Point& p, VSR_PRECISION r, const Biv& biv = Biv::xy);
391  static Circle circle(const Biv& B);
392 
393  //circle Facing v
394  static Circle circle(const Vec& v, VSR_PRECISION r=1.0);
395 
396  //Circle at x,y,z facing in biv
397  static Circle circle(VSR_PRECISION x, VSR_PRECISION y, VSR_PRECISION z, Biv biv = Biv::xy, VSR_PRECISION r=1.0);
398 
399 
400 
401  /*-----------------------------------------------------------------------------
402  * SPHERES
403  *-----------------------------------------------------------------------------*/
404  static Sphere sphere(const Pnt& a, const Pnt& b, const Pnt& c, const Pnt& d);
405  static DualSphere sphere(VSR_PRECISION x, VSR_PRECISION y, VSR_PRECISION z, VSR_PRECISION r=1.0);
406  static DualSphere sphere(const Point& p, VSR_PRECISION r=1.0);
407 
408 
409 
410  /*-----------------------------------------------------------------------------
411  * PLANES
412  *-----------------------------------------------------------------------------*/
413 
415  static DualPlane plane( VSR_PRECISION a, VSR_PRECISION b, VSR_PRECISION c, VSR_PRECISION d=0.0);
417  static DualPlane plane( const Vec& v, VSR_PRECISION d=0.0);
419  static Plane plane(const Pnt& a, const Pnt& b, const Pnt& c);
420 
421  /*-----------------------------------------------------------------------------
422  * LINES
423  *-----------------------------------------------------------------------------*/
424 
428  static DualLine axis(const Cir& c);
429 
431  static Line line( const Vec& a, const Vec& b);
432 
434  static Line line( VSR_PRECISION x, VSR_PRECISION y, VSR_PRECISION z );
435 
437  static Line dualLine( VSR_PRECISION x, VSR_PRECISION y, VSR_PRECISION z );
438 
440  static Line line( const Point& a, const Point& b);
442  static Line line( const Point& a, const Vec& b);
443 
444 
445 
447  static Circle hline( const Point& a, const Point& b);
449  static Circle sline( const Point& a, const Point& b);
450 
451 
452 
454  static VSR_PRECISION distance( const Lin& lin, const Pnt& pnt);
455 
456 
457  #pragma mark COINCIDENCE_FUNCTIONS
458 
460  static Circle meet( const Dls& s, const Dls& d);
462  static Circle meet( const Dls& s, const Dlp& d);
464  static Circle meet( const Dls& s, const Pln& d);
466  static Circle meet( const Sphere& s, const DualPlane& d);
468  static Circle meet( const Sphere& s, const Plane& d);
469 
470 
472  static Point meet( const Line& lin, const DualPlane&);
474  static Point meet( const DualLine&, const DualPlane&);
476  static Point meet( const Line& la, const Line& lb);
477 
479  static Pair meet( const Cir& cir, const Dlp& dlp);
481  static Pair meet( const Cir& cir, const Dls& s);
482 
483  #pragma mark HIT_TESTS
484 
488  static bool hit(const Point&, const Pair&);
489 
493  static bool hit(const Point&, const Circle&);
494 
495  static double squaredDistance(const Point& a, const Point& b);
496 
497 
498  #pragma mark HYPERBOLIC_FUNCTIONS
499  /*-----------------------------------------------------------------------------
500  * hyperbolic functions (see alan cortzen's document on this)
501  *-----------------------------------------------------------------------------*/
502 
506  static Point hnorm(const Pnt& p);
507 
508 
512  static double hdist(const Pnt& pa, const Pnt& pb);
513 
514 
518  static Pair hgen(const Pnt& pa, const Pnt& pb, double amt);
519 
523  static Point hspin(const Pnt& pa, const Pnt& pb, double amt);
524 
525 
526  #pragma mark AFFINE combinations
527 
528  template<class A, class B>
529  static Point affine( const A& a, const B& b, VSR_PRECISION t){
530  return (a + (b-a)*t).null();
531  }
532 };
533 
534  /*-----------------------------------------------------------------------------
535  * EVALUATION LAMBDAS
536  *-----------------------------------------------------------------------------*/
537 
541  auto pointOnLine = []( const Line& lin, const Point& v){
542  return Round::null( Flat::loc( lin, v, false) );
543  };
544 
546  auto pointOnCircle = [](const Circle& c, VSR_PRECISION t){
547  return Construct::point(c,t);
548  };
550  auto pointsOnCircle = [](const Circle& c, int num){
551  vector<Point> out;
552  for (int i=0;i<=num;++i){
553  out.push_back(pointOnCircle(c, TWOPI*(float)i/num) );
554  }
555  return out;
556  };
558  auto pairOnSphere = [](const DualSphere& s, VSR_PRECISION t, VSR_PRECISION p){
559  return Construct::pair(s, Vec::x.sp( Gen::rot(t,p) ) );
560  };
562  auto pointOnSphere = [](const DualSphere& s, VSR_PRECISION t, VSR_PRECISION p){
563  return Construct::pointA( pairOnSphere(s,t,p) ).null();
564  };
566  auto pointsOnSphere = [](const DualSphere& s, int u, int v){
567  vector<Point> out;
568  for (int i = 0; i < u; ++i){
569  for (int j = 0; j < v; ++j){
570 
571  float tu= TWOPI * i/u;//-1 + 2.0 * i/num;
572  float tv = -PIOVERTWO + PI * j/v;
573 
574  out.push_back( pointOnSphere(s,tu,tv) );
575 
576  }
577  }
578  return out;
579  };
580 
581 
582 } //cga::
583 
584 
585 template<class Algebra, class B> template <class A>
586 Multivector<Algebra,B> Multivector<Algebra,B>::mot( const Multivector<Algebra,A>& t) const{
587  return this -> sp ( cga::Gen::mot(t) );
588 }
589 template<class Algebra, class B> template <class A>
590 Multivector<Algebra,B> Multivector<Algebra,B>::motor( const Multivector<Algebra,A>& t) const{
591  return this -> sp ( cga::Gen::mot(t) );
592 }
593 template<class Algebra, class B> template <class A>
594 Multivector<Algebra,B> Multivector<Algebra,B>::twist( const Multivector<Algebra,A>& t) const{
595  return this -> sp ( cga::Gen::mot(t) );
596 }
597 
598 
605 #define E1 e1(1)
606 #define E2 e2(1)
607 #define E3 e3(1)
608 
610 #define PT(x,y,z) vsr::cga::Round::null(vsr::cga::Vec(x,y,z))
611 #define DLS(r) vsr::cga::Round::dls(0,0,0,r)
613 
614 #define PV(v) vsr::cga::Round::null(v)
615 #define PX(f) vsr::cga::Round::null(vsr::cga::Vec(f,0,0))
616 #define PY(f) vsr::cga::Round::null(vsr::cga::Vec(0,f,0))
617 #define PZ(f) vsr::cga::Round::null(vsr::cga::Vec(0,0,f))
618 
620 #define PAIR(x,y,z) (PT(x,y,z)^PT(-x,-y,-z))
621 #define CXY(f) (PX(f)^PY(f)^PX(-f)).unit()
623 #define CXZ(f) (PX(f)^PZ(f)^PX(-f)).unit()
625 #define CYZ(f) (PY(f)^PY(-f)^PZ(f)).unit()
627 #define F2S(f) f*1000.0
628 #define S2F(f) f/1000.0
629 
631 #define LN(x,y,z) ( vsr::cga::Point(0,0,0,1,.5)^PT(x,y,z)^vsr::cga::Inf(1) )
632 #define DLN(x,y,z) ( vsr::Op::dl(LN(x,y,z)) )
634 #define PAO vsr::cga::Point(0,0,0,1,0)
635 #define EP vsr::cga::Dls(0,0,0,1,-.5)
636 #define EM vsr::cga::Dls(0,0,0,1,.5)
637 #define INFTY vsr::cga::Inf(1)
638 #define HYPERBOLIC_INF EP
639 #define SPHERICAL_INF EM
640 #define EUCLIDEAN_INF INFTY
641 #define HLN(x,y,z) (vsr::cga::Ori(1)^PT(x,y,z)^EP) //hyperbolic line (circle)
642 #define HDLN(x,y,z) (vsr::Op::dl(HLN(x,y,z)))
643 
646 } //vsr::
647 
648 #endif
649 
650 
651 // template<bool _a, bool _b>
652 // struct meet_impl{
653 // template<class A, class B>
654 // auto operator()(const A& a, const B& b) RETURNS
655 // ( (a^b).dual() )
656 // };
657 
658 
659 // namespace topo{
660 //
661 //
662 //
663 // /*!
664 // * \brief spin product in range [0,RANGE) of some type t and some bivector generator p
665 // */
666 // template<class T, class G>
667 // vector<T> spin( const T& s, const G& p, int num, float range=PI){
668 // vector<T> res;
669 // for (int i=0;i<num;++i){
670 // float t=range*(float)i/num;
671 // res.push_back( s.spin( gen(p*t) ) ) ;
672 // }
673 // return res;
674 // }
675 //
676 //
677 //
678 // } //topo::
679 
680 
681 //} //cga::
682 
683 
static constexpr bool sn(const A &a, const A &b)
Sign of A with Respect to B (short hand)
Definition: vsr_cga3D_op.h:88
static Bst bst(const Pair &p)
vsr::cga::Boost from vsr::cga::Pair
constructive syntactic sugar for making geometric elements
Definition: vsr_cga3D_op.h:330
static constexpr auto pj(const A &a, const B &b) -> auto
Shorthand Proj and Rejection.
static constexpr auto project(const A &a, const B &b) -> auto
Projection of A onto B.
static Rot AA(const Vec &s)
axis angle from Vec stored in rotor
static Point point(const Circle &c, VSR_PRECISION t)
Point on Circle at theta t.
Generic Geometric Number Types (templated on an algebra and a basis )
Definition: vsr_algebra.h:69
static Rot rot(const Biv &b)
vsr::cga::Rotor from vsr::cga::Bivector
Multivector< algebra, typename algebra::vector_basis > null() const
Conformal Mapping \(\boldsymbol(x)\to n_o + \boldsymbol{x} + \boldsymbol{x}^2n_\infty \) ...
Definition: vsr_generic_op.h:1118
static Mot mot(const Dll &dll)
Generate a vsr::cga::Motor from a vsr::cga::DualLine Axis.
static Pair pair(const DualSphere &s, const Vec &v)
constructs a Pair on Sphere s in v direction
auto pairOnSphere
a pair on dual sphere
Definition: vsr_cga3D_op.h:558
static Point loc(const A &a, const Point &p, bool dual)
Location of flat (shorthand)three-letter version of cga::Flat::location.
Definition: vsr_cga3D_round.h:523
core namespaced operations that are metric-agnostic
static constexpr bool sign(const A &a, const A &b)
Sign of A with Respect to B.
Definition: vsr_cga3D_op.h:82
NPar< 5 > Par
Pair
Definition: vsr_cga3D_types.h:73
auto pointOnSphere
a single point on dual sphere s at theta t and phi p
Definition: vsr_cga3D_op.h:562
static GATrs< A > trs(const Multivector< A, B > &a)
Generate translation from any type as exponential of a direction vector.
Definition: vsr_generic_op.h:401
static Bst bst(const A &tnv, const Vec &vec, T t)
Generate a Translated Transversion.
Definition: vsr_cga3D_op.h:227
static Vec Pos(const Dlp &s)
Position of Dlp.
Generators and Logarithms Optimized for 3D Conformal Geometric Algebra.
Definition: vsr_cga3D_op.h:119
static constexpr auto reject(const A &a, const B &b) -> auto
Rejection of A from B.
the versor library namespace
Definition: vsr_algebra.h:29
static Trv trv(const A &a)
vsr::cga::Transversor from any type
Definition: vsr_cga3D_op.h:142
auto pointOnLine
Point on line closest to another point v.
Definition: vsr_cga3D_op.h:541
static Trs translator(const A &a)
vsr::cga::Translator from any type
Definition: vsr_cga3D_op.h:139
static auto trv(const Multivector< A, B > &a) -> auto
Generate Local Boost at origin as exponential of a TangentVector vsr::GATnv
static Trs trs(const A &a)
vsr::cga::Translator from any type
Definition: vsr_cga3D_op.h:136
static Point null(const Vec &v)
Null Point from a vec.
auto pointOnCircle
a single point on circle c at theta t
Definition: vsr_cga3D_op.h:546
static Point pointA(const Pair &pp)
First point of point pair pp.
auto pointsOnCircle
n points on circle c
Definition: vsr_cga3D_op.h:550
static Trv transversor(const A &a)
vsr::cga::Transversor from any type
Definition: vsr_cga3D_op.h:145
Extraction of axis-angle orientation and 3D position features from Multivectors.
Definition: vsr_cga3D_op.h:46
auto pointsOnSphere
many points on sphere (could use map func from gfx::data)
Definition: vsr_cga3D_op.h:566