Structural deformable models
VVector.h
Go to the documentation of this file.
1 #ifndef _VVECTOR_H_
2 #define _VVECTOR_H_
3 
4 template<typename T>
5 struct Traits{
6  T static zero() { return 0; };
7  T static one() { return 1; };
8 };
9 template<> struct Traits<float>;
10 template<> struct Traits<double>;
11 
13 template<class T, unsigned int D>
14  class VVector
15 {
16 public:
17  typedef VVector<T, D> VT;
18  typedef T* TPtr;
19  typedef const T* CTPtr;
20  typedef Traits<T> TTrait;
21 
22  VVector() {};
23  VVector(const T &sval) {
24  operator=(sval);
25  }
26  VVector(CTPtr pval) {
27  operator=(pval);
28  }
29  VVector(const VT &rhs) {
30  operator=(rhs);
31  }
32  ~VVector() {};
33 
34  static VT zero() { return VT(TTrait::zero()); }
35  static VT one() { return VT(TTrait::one()); }
36 
37  TPtr begin() { return val; }
38  CTPtr begin() const { return val; }
39  TPtr end() { return &val[D]; }
40  CTPtr end() const { return &val[D]; }
41  static unsigned int size() { return D; }
42  const T& operator[](unsigned int i) const { return val[i]; }
43  T& operator[](unsigned int i) { return val[i]; }
44 
45  bool operator==(const VT& rhs) const {
46  CTPtr sv=rhs.begin();
47  for(CTPtr pv=begin(); pv!=end(); pv++, sv++)
48  if(*pv != *sv) return false;
49  return true;
50  }
51  bool operator<(const VT& rhs) const {
52  CTPtr sv=rhs.begin();
53  for(CTPtr pv=begin(); pv!=end(); pv++, sv++)
54  if(*pv >= *sv) return false;
55  return true;
56  }
57  bool operator>(const VT& rhs) const {
58  CTPtr sv=rhs.begin();
59  for(CTPtr pv=begin(); pv!=end(); pv++, sv++)
60  if(*pv <= *sv) return false;
61  return true;
62  }
63  bool operator<=(const VT& rhs) const {
64  CTPtr sv=rhs.begin();
65  for(CTPtr pv=begin(); pv!=end(); pv++, sv++)
66  if(*pv > *sv) return false;
67  return true;
68  }
69  bool operator>=(const VT& rhs) const {
70  CTPtr sv=rhs.begin();
71  for(CTPtr pv=begin(); pv!=end(); pv++, sv++)
72  if(*pv < *sv) return false;
73  return true;
74  }
75 
76  VT& operator=(const T &rhs) {
77  for(TPtr pv=begin(); pv!=end(); pv++)
78  *pv = rhs;
79  return *this;
80  }
81  VT& operator=(CTPtr rhs) {
82  for(TPtr pv=begin(); pv!=end(); pv++, rhs++)
83  *pv = *rhs;
84  return *this;
85  }
86  VT& operator=(const VT &rhs) {
87  CTPtr sv=rhs.begin();
88  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
89  *pv = *sv;
90  return *this;
91  }
92 
93  VT& operator+=(const VT &rhs) {
94  CTPtr sv=rhs.begin();
95  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
96  *pv += *sv;
97  return *this;
98  }
99  VT& operator-=(const VT &rhs) {
100  CTPtr sv=rhs.begin();
101  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
102  *pv -= *sv;
103  return *this;
104  }
105  VT& operator*=(const VT &rhs) {
106  CTPtr sv=rhs.begin();
107  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
108  *pv *= *sv;
109  return *this;
110  }
111  VT& operator/=(const VT &rhs) {
112  CTPtr sv=rhs.begin();
113  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
114  *pv /= *sv;
115  return *this;
116  }
117  VT& operator+=(const T &rhs) {
118  for(TPtr pv=begin(); pv!=end(); pv++)
119  *pv += rhs;
120  return *this;
121  }
122  VT& operator-=(const T &rhs) {
123  for(TPtr pv=begin(); pv!=end(); pv++)
124  *pv -= rhs;
125  return *this;
126  }
127  VT& operator*=(const T &rhs) {
128  for(TPtr pv=begin(); pv!=end(); pv++)
129  *pv *= rhs;
130  return *this;
131  }
132  VT& operator/=(const T &rhs) {
133  for(TPtr pv=begin(); pv!=end(); pv++)
134  *pv /= rhs;
135  return *this;
136  }
137 
138  VT operator+(const VT &rhs) const {
139  VT r(*this);
140  return r+=rhs;
141  }
142  VT operator-(const VT &rhs) const {
143  VT r(*this);
144  return r-=rhs;
145  }
146  VT operator*(const VT &rhs) const {
147  VT r(*this);
148  return r*=rhs;
149  }
150  VT operator/(const VT &rhs) const {
151  VT r(*this);
152  return r/=rhs;
153  }
154  VT operator+(const T &rhs) const {
155  return (VT(*this) += rhs);
156  }
157  VT operator-(const T &rhs) const {
158  return (VT(*this) -= rhs);
159  }
160  VT operator*(const T &rhs) const {
161  return (VT(*this) *= rhs);
162  }
163  VT operator/(const T &rhs) const {
164  return (VT(*this) /= rhs);
165  }
166 
167  VT& times(const VT& rhs) {
168  CTPtr sv=rhs.begin();
169  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
170  *pv *= *sv;
171  return *this;
172  }
173 
174  T dot(const VT &rhs) const {
175  T r= TTrait::zero();
176  for(CTPtr pv=begin(), *sv=rhs.begin(); pv!=end(); pv++, sv++)
177  r += *pv * *sv;
178  return r;
179  }
180 
181  VVector<T, 3> cross(const VVector<T, 3>& rhs) const {
182  VVector<T, 3> r;
183  r.val[0]= val[1]*rhs.val[2] - val[2]*rhs.val[1];
184  r.val[1]= val[2]*rhs.val[0] - val[0]*rhs.val[2];
185  r.val[2]= r.val[2] = val[0]*rhs.val[1] - val[1]*rhs.val[0];
186  return r;
187  }
188  T sum() const { //norm1
189  T n= TTrait::zero();
190  for(CTPtr pv=begin(); pv!=end(); pv++)
191  n += *pv;
192  return n;
193  }
194  T prod() const {
195  T n= TTrait::one();
196  for(CTPtr pv=begin(); pv!=end(); pv++)
197  n *= *pv;
198  return n;
199  }
200  T norm2() const {
201  T n= TTrait::zero();
202  for(CTPtr pv=begin(); pv!=end(); pv++)
203  n += *pv * *pv;
204  return n;
205  }
206  T norm() const { return sqrt(norm2()); }
207  T normalize() {
208  T n = norm();
209  operator*=(TTrait::one()/n);
210  return n;
211  }
212  T avg() const { return sum()/D; }
213  T var() const { return (*this-avg()).norm2(); }
214  T stdev() const { return sqrt(var()); }
215  VT& sqrtEach() {
216  for(TPtr pv=begin(); pv!=end(); pv++)
217  *pv = sqrt(*pv);
218  return *this;
219  }
220  T maxVal() const {
221  T mv=val[0];
222  for(CTPtr pv=begin(); pv!=end(); pv++)
223  if(mv < *pv) mv = *pv;
224  return mv;
225  }
226  T minVal() const {
227  T mv=val[0];
228  for(CTPtr pv=begin(); pv!=end(); pv++)
229  if(mv > *pv) mv = *pv;
230  return mv;
231  }
232  unsigned int majorComponent() const {
233  T mv=TTrait::zero();
234  unsigned int mc=0,cc=0;
235  for(CTPtr pv=begin(); pv!=end(); pv++, cc++) {
236  T apv = abs(*pv);
237  if(mv < apv) { mv = apv; mc = cc; }
238  }
239  return mc;
240  }
241  VT& clampLB(const VT &lb) {
242  CTPtr sv=lb.begin();
243  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
244  if(*pv < *sv) *pv = *sv;
245  return *this;
246  }
247  VT& clampUB(const VT &ub) {
248  CTPtr sv=ub.begin();
249  for(TPtr pv=begin(); pv!=end(); pv++, sv++)
250  if(*pv > *sv) *pv = *sv;
251  return *this;
252  }
253  VT& clamp(const VT &lb, const VT &ub) {
254  CTPtr sv=lb.begin();
255  CTPtr uv=ub.begin();
256  for(TPtr pv=begin(); pv!=end(); pv++, sv++, uv++) {
257  if(*pv > *uv) *pv = *uv;
258  else if(*pv < *sv) *pv = *sv;
259  }
260  return *this;
261  }
262  float angle(const VT& rhs) const {
263  float dv = cross(rhs).norm(); //sin(a)
264  float dh = dot(rhs); //cos(a)
265 #ifndef _THERE_IS_NO_ATAN2_
266  return atan2(dv,dh);
267 #else
268  float dvp,dvn;
269  if(dv > 0.0) {
270  dvp = 1.0;
271  dvn = 0.0;
272  } else {
273  dvp = 0.0;
274  dvn = 1.0;
275  }
276  float dhn = (dh < 0) ? 1.0 : 0.0;
277  float psi;
278  if (dh == 0.0)
279  psi = M_PI_2 * (dvp + 3.0 * dvn);
280  else if (dv == 0.0)
281  psi = M_PI * dhn;
282  else
283  psi = atan(dv/dh) + M_PI * dhn * dvp
284  + (M_PI + M_PI * (dh > 0.0)) * dvn;
285  return psi * (180.0*M_1_PI);
286 #endif
287  }
288 
289  friend std::ostream& operator<<(std::ostream& os, const VT& rhs) {
290  for(CTPtr pv= rhs.begin(); pv != rhs.end(); pv++) {
291  //if(pv != rhs.begin()) os << " ";
292  os << *pv << " ";
293  }
294  return os;
295  }
296  friend std::istream& operator>>(std::istream &is, VT &rhs) {
297  for(TPtr pv=rhs.begin(); pv!=rhs.end(); pv++) {
298  is >> *pv;
299  }
300  return is;
301  }
302 
303 public:
304  T val[D];
305 };
306 
307 template<> struct Traits<float> {
308  float static zero() { return 0.f; };
309  float static one() { return 1.f; };
310 };
311 
312 template<> struct Traits<double> {
313  double static zero() { return 0.; };
314  double static one() { return 1.; };
315 };
316 
317 #endif // _VVECTOR_H_
VT operator/(const T &rhs) const
Definition: VVector.h:163
VT & operator*=(const T &rhs)
Definition: VVector.h:127
static T zero()
Definition: VVector.h:6
VT & operator=(const T &rhs)
Definition: VVector.h:76
VT & operator=(const VT &rhs)
Definition: VVector.h:86
T * TPtr
Definition: VVector.h:18
bool operator>=(const VT &rhs) const
Definition: VVector.h:69
VT operator*(const VT &rhs) const
Definition: VVector.h:146
CTPtr begin() const
Definition: VVector.h:38
bool operator<=(const VT &rhs) const
Definition: VVector.h:63
VVector(const T &sval)
Definition: VVector.h:23
T normalize()
Definition: VVector.h:207
T prod() const
Definition: VVector.h:194
VT & operator+=(const VT &rhs)
Definition: VVector.h:93
DMatrix< T > var(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:97
VT & clamp(const VT &lb, const VT &ub)
Definition: VVector.h:253
float angle(const VT &rhs) const
Definition: VVector.h:262
VT & operator/=(const VT &rhs)
Definition: VVector.h:111
Definition: VVector.h:5
T norm() const
Definition: VVector.h:206
T val[D]
Definition: VVector.h:304
#define M_1_PI
Definition: mathutil.h:12
VT & times(const VT &rhs)
Definition: VVector.h:167
VT operator+(const VT &rhs) const
Definition: VVector.h:138
T & operator[](unsigned int i)
Definition: VVector.h:43
static float one()
Definition: VVector.h:309
static T one()
Definition: VVector.h:7
VT & clampUB(const VT &ub)
Definition: VVector.h:247
Traits< T > TTrait
Definition: VVector.h:20
const T * CTPtr
Definition: VVector.h:19
VVector(const VT &rhs)
Definition: VVector.h:29
CTPtr end() const
Definition: VVector.h:40
VT operator-(const VT &rhs) const
Definition: VVector.h:142
T minVal() const
Definition: VVector.h:226
T maxVal() const
Definition: VVector.h:220
bool operator==(const VT &rhs) const
Definition: VVector.h:45
VVector(CTPtr pval)
Definition: VVector.h:26
T sum() const
Definition: VVector.h:188
static VT one()
Definition: VVector.h:35
DMatrix< T > & abs(DMatrix< T > &mat)
Definition: DMatrixUtil.h:132
bool operator<(const VT &rhs) const
Definition: VVector.h:51
#define M_PI
Definition: mathutil.h:9
DMatrix< T > sum(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:59
bool operator>(const VT &rhs) const
Definition: VVector.h:57
T stdev() const
Definition: VVector.h:214
VT & operator+=(const T &rhs)
Definition: VVector.h:117
VT & sqrtEach()
Definition: VVector.h:215
static double one()
Definition: VVector.h:314
static VT zero()
Definition: VVector.h:34
TPtr begin()
Definition: VVector.h:37
VT & operator-=(const T &rhs)
Definition: VVector.h:122
const T & operator[](unsigned int i) const
Definition: VVector.h:42
VT & clampLB(const VT &lb)
Definition: VVector.h:241
T dot(const VT &rhs) const
Definition: VVector.h:174
TPtr end()
Definition: VVector.h:39
VVector< T, 3 > cross(const VVector< T, 3 > &rhs) const
Definition: VVector.h:181
VT operator-(const T &rhs) const
Definition: VVector.h:157
VT & operator*=(const VT &rhs)
Definition: VVector.h:105
unsigned int majorComponent() const
Definition: VVector.h:232
VT & operator-=(const VT &rhs)
Definition: VVector.h:99
T norm2() const
Definition: VVector.h:200
DMatrix< T > avg(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:90
VT operator+(const T &rhs) const
Definition: VVector.h:154
VVector< T, D > VT
Definition: VVector.h:17
T var() const
Definition: VVector.h:213
VT & operator=(CTPtr rhs)
Definition: VVector.h:81
VT operator*(const T &rhs) const
Definition: VVector.h:160
VT operator/(const VT &rhs) const
Definition: VVector.h:150
friend std::istream & operator>>(std::istream &is, VT &rhs)
Definition: VVector.h:296
friend std::ostream & operator<<(std::ostream &os, const VT &rhs)
Definition: VVector.h:289
static double zero()
Definition: VVector.h:313
static float zero()
Definition: VVector.h:308
~VVector()
Definition: VVector.h:32
T avg() const
Definition: VVector.h:212
static unsigned int size()
Definition: VVector.h:41
VVector()
Definition: VVector.h:22
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81
VT & operator/=(const T &rhs)
Definition: VVector.h:132