versor  3.0
C++11 library for Geometric algebra
vsr_constraint.h
2 #include "vsr_set.h"
3 
4 //assumes conformal
5 
6 namespace vsr{
7 
8  //generic vec
9  template<typename T>
10  T Diff( const T& a, const T& b ){
11  return (a-b);
12  }
13 
15  template<typename T>
16  VT Dist( const T& a, const T& b ){
17  // cout << "generic vec" << endl;
18  return (a-b).wt(); //vec, etc must have a wt() method ( dot() )
19  }
20 
21  /* //generic vec */
22  /* template<typename T> */
23  /* VT SqDist( const T& a, const T& b ){ */
24  /* return (a-b).dot(); */
25  /* } */
26 
27  //specific
28  /* template<TT DIM> */
29  /* NVec<DIM> Diff( const NVec<DIM>& a, const NVec<DIM>& b ){ */
30  /* return (a-b); */
31  /* } */
32 
33  /* template<TT DIM> */
34  /* VT Dist( const NVec<DIM>& a, const NVec<DIM>& b ){ */
35  /* return (a-b).wt(); */
36  /* } */
37  template<TT DIM>
38  VT Dist( const NPnt<DIM>& a, const NPnt<DIM>& b ){
39  return Ro::sqd(a,b);
40  }
41 
42  //template<class T>
43  struct Constraint{
44 
45  bool bActive;
46  Constraint() : bActive(true) {}
47  //satisfy
48  virtual void operator ()(){
49  if (bActive){
50  exec();
51  }
52  }
53 
54  virtual void exec(){
55 
56  }
57  };
58 
59  //rigid distance
60  template<class T>
61  class Distance : public Constraint {
62 
63  static int S_ID;
64 
65  public:
66 
67  bool operator < (const Distance& d) const { return id < d.id; }
68 
69  Distance(T& a, T& b) : a(&a), b(&b), sqLength( Dist(a,b) ) {
70  // cout << sqLength << endl;
71  if (sqLength==0) { a.vprint(); b.vprint(); }
72  id = S_ID;
73  S_ID++;
74  }
75 
76  T * a; T * b;
77 
78  VT sqLength;
79  int id;
80 
81  void verlet() {
82  auto diff = Diff( *a, *b);
83  float sq = sqLength;
84 
85  float d = diff.wt() + sq;
86  float r = d == 0 ? -.5 : ( sq ) / ( d ) - .5;
87  diff *= r;
88 
89  *a += diff;
90  *b -= diff;
91 
92  }
93  };
94 
95  template<class T>
96  int Distance<T> :: S_ID = 0;
97 
98  // template<typename T>
99  // struct Solver {
100  // T * mData; // Data to Solve
101  // T * data() { return mData; }
102  // };
103 
104  //A Set of Distances between Type Ts (i.e. bonds)
105  template<typename T>
106  class Verlet : public DoubleData<T> {
107 
108 
109  int mIter;
110  double mTimeStep;
111 
112  public:
113 
114  Set< Distance<T> * > constraint;
115 
116  Verlet() : mIter(20), mTimeStep(.2){}
117 
118 
119  //set constraints from current positions, could make a convex hull here for instance . .
120  void set(){
121 
122  }
123 
124  double timeSq() { return mTimeStep * mTimeStep; }
125 
126 
127  void step(){
128  force();
129  move();
130  constrain();
131  }
132 
133  void force(){
134 
135  }
136 
137  void move(){
138  double s = timeSq();
139  for (int i = 0; i < this->num(); ++i ){
140  T tmp = this->data(i);
141  T v = this->data(i) - this->prev(i);
142  this->data(i) += v;// + mForces[i] * s;
143  this->prev(i) = tmp;
144  }
145  }
146  void constrain(){
147  for (int i = 0; i < mIter; ++i){
148  for (int j = 0; j < constraint.size(); ++j){
149  constraint[j] -> verlet();
150  }
151  }
152  }
153 
154  };
155 }
Definition: vsr_set.h:13
Definition: vsr_constraint.h:43
core namespaced operations that are metric-agnostic
Definition: vsr_set.h:104
Definition: vsr_constraint.h:61
VT Dist(const T &a, const T &b)
Squared Distances.
Definition: vsr_constraint.h:16
the versor library namespace
Definition: vsr_algebra.h:29
Definition: vsr_constraint.h:106