bazar  1.3.1
matvec.h
Go to the documentation of this file.
1 #ifndef _MATVEC_H
2 #define _MATVEC_H
3 
4 #include <iostream>
5 #include <math.h>
6 #include <cv.h>
7 
8 struct Mat3x4;
9 struct Mat3x3;
10 
11 struct Vec3 {
12  double v[3];
13 
14  Vec3() {}
15  Vec3(const double p[3]) { v[0]=p[0]; v[1]=p[1]; v[2]=p[2]; }
16  Vec3(double x, double y, double z) { v[0]=x; v[1]=y; v[2]=z; }
17 
18  double &operator[](int i) { return v[i]; }
19  const double &operator[](int i) const { return v[i]; }
20 
21  const Vec3 &operator = (const Vec3 &a) {
22  v[0]=a[0]; v[1]=a[1]; v[2]=a[2]; return *this;
23  }
24 
25  void mul(double scalar) {
26  v[0] *= scalar;
27  v[1] *= scalar;
28  v[2] *= scalar;
29  }
30 
31  double norm() const { return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); }
32  void normalize() { mul(1.0/norm()); }
33 
34  void setMul(const Mat3x4 &mat, const Vec3 &v);
35 
36  void setSub(const Vec3 &a, const Vec3 &b) {
37  v[0] = a[0]-b[0]; v[1] = a[1]-b[1]; v[2] = a[2]-b[2];
38  }
39  void setAdd(const Vec3 &a, const Vec3 &b) {
40  v[0] = a[0]+b[0]; v[1] = a[1]+b[1]; v[2] = a[2]+b[2];
41  }
42  void sub(const Vec3 &a) { v[0] -= a[0]; v[1]-=a[1]; v[2]-=a[2]; }
43  void add(const Vec3 &a) { v[0] += a[0]; v[1]+=a[1]; v[2]+=a[2]; }
44 
45  void setCross(const Vec3 &a, const Vec3 &b) {
46  v[0] = a[1] * b[2] - a[2] * b[1];
47  v[1] = a[2] * b[0] - a[0] * b[2];
48  v[2] = a[0] * b[1] - a[1] * b[0];
49  }
50 
51  void set(double x, double y, double z) { v[0]=x; v[1]=y; v[2]=z; }
52  void set(double *a) { v[0]=a[0]; v[1]=a[1]; v[2]=a[2]; }
53 
54  double dot(const Vec3 &a) {
55  return a[0]*v[0] + a[1]*v[1] + a[2]*v[2];
56  }
57 };
58 
59 struct Mat3x4 {
60  double m[3][4];
61 
62  void set(CvMat *m);
63  void setIdentity();
64  void setMul(const Mat3x3 &a, const Mat3x4 &b);
65  void setMul(const Mat3x4 &a, const Mat3x4 &b);
66  void mul(const Mat3x4 &a);
67  void setInverseByTranspose(const Mat3x4 &m);
68  void setRotate(const Vec3 &axis, double angle);
69  void setTranslate(double x, double y, double z);
70  void transform(const float src[4], float dst[4]);
71  double det() { return m[0][0]*m[1][1]*m[2][2] -m[0][0]*m[1][2]*m[2][1] -m[1][0]*m[0][1]*m[2][2]
72  +m[1][0]*m[0][2]*m[2][1] +m[2][0]*m[0][1]*m[1][2]-m[2][0]*m[0][2]*m[1][1]; }
73 };
74 
75 std::ostream& operator << (std::ostream& os, const Mat3x4 &cam);
76 
77 struct Mat3x3 {
78  double m[3][3];
79 
80  void setIdentity();
81 };
82 
83 
84 
85 #endif
86