bazar  1.3.1
matvec.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include "matvec.h"
3 
4 void Vec3::setMul(const Mat3x4 &mat, const Vec3 &p)
5 {
6  for (int i=0; i<3; ++i) {
7  v[i] = mat.m[i][0]*p[0] +
8  mat.m[i][1]*p[1] +
9  mat.m[i][2]*p[2] +
10  mat.m[i][3];
11  }
12 }
13 
15 {
16  memset(m,0,sizeof(m));
17  m[0][0]=m[1][1]=m[2][2]=1;
18 }
19 
20 void Mat3x4::set(CvMat *mat)
21 {
22  setIdentity();
23 
24  int nr = mat->rows < 3 ? mat->rows : 3;
25  int nc = mat->cols < 4 ? mat->cols : 4;
26 
27  for (int r=0; r< nr; ++r) {
28  for (int c=0; c< nc; ++c) {
29  m[r][c] = cvGetReal2D(mat, r, c);
30  }
31  }
32 }
33 
35 {
36  memset(m,0,sizeof(m));
37  m[0][0]=m[1][1]=m[2][2]=1;
38 }
39 
40 void Mat3x4::setMul(const Mat3x3 &a, const Mat3x4 &b)
41 {
42  for (int i=0; i<3; i++) {
43  for (int j=0; j<4; j++) {
44  m[i][j] =
45  a.m[i][0]*b.m[0][j] +
46  a.m[i][1]*b.m[1][j] +
47  a.m[i][2]*b.m[2][j];
48  }
49  }
50 }
51 
52 void Mat3x4::transform(const float src[4], float dst[4])
53 {
54  float tmp[4];
55  const float *s=src;
56  if (src==dst) {
57  memcpy(tmp, src, 4*sizeof(float));
58  s=tmp;
59  }
60 
61  for (int i=0; i<3; ++i) {
62  dst[i]=0;
63  for(int j=0;j<4; j++)
64  dst[i] += s[j]*m[i][j];
65  }
66 }
67 
68 std::ostream& operator << (std::ostream& os, const Mat3x4 &cam)
69 {
70  for (int i=0; i<3; ++i) {
71  os << "[ " << cam.m[i][0];
72  for (int j=1;j<4; ++j) {
73  os << ", " << cam.m[i][j];
74  }
75  os << " ]\n";
76  }
77  return os;
78 }
79 
82 void Mat3x4::setMul(const Mat3x4 &a, const Mat3x4 &b)
83 {
84  static const double fake[4] = {0,0,0,1};
85 
86  for (int i=0; i<3; i++) {
87  for (int j=0; j<4; j++) {
88  m[i][j] =
89  a.m[i][0]*b.m[0][j] +
90  a.m[i][1]*b.m[1][j] +
91  a.m[i][2]*b.m[2][j] +
92  a.m[i][3]*fake[j];
93  }
94  }
95 }
96 
97 void Mat3x4::mul(const Mat3x4 &a)
98 {
99  Mat3x4 nm;
100  nm.setMul(*this, a);
101  *this = nm;
102 }
103 
104 void Mat3x4::setTranslate(double x, double y, double z)
105 {
106  setIdentity();
107  m[0][3] = x;
108  m[1][3] = y;
109  m[2][3] = z;
110 }
111 
113 {
114  // unrolling by maple...
115  m[0][0] = mat.m[0][0];
116  m[0][1] = mat.m[1][0];
117  m[0][2] = mat.m[2][0];
118  m[0][3] = -(mat.m[0][0]*mat.m[0][3]+mat.m[1][0]*mat.m[1]
119  [3]+mat.m[2][0]*mat.m[2][3]);
120  m[1][0] = mat.m[0][1];
121  m[1][1] = mat.m[1][1];
122  m[1][2] = mat.m[2][1];
123  m[1][3] = -(mat.m[0][1]*mat.m[0][3]+mat.m[1][1]*mat.m[1]
124  [3]+mat.m[2][1]*mat.m[2][3]);
125  m[2][0] = mat.m[0][2];
126  m[2][1] = mat.m[1][2];
127  m[2][2] = mat.m[2][2];
128  m[2][3] = -(mat.m[0][2]*mat.m[0][3]+mat.m[1][2]*mat.m[1]
129  [3]+mat.m[2][2]*mat.m[2][3]);
130 }
131 
132 void Mat3x4::setRotate(const Vec3 &axis, double angle)
133 {
134  // produce a quaternion
135  double theta = angle/2;
136  double sinTheta = sin(theta);
137  double qx = sinTheta * axis[0];
138  double qy = sinTheta * axis[1];
139  double qz = sinTheta * axis[2];
140  double qw = cos( theta );
141 
142  double xx = qx*qx; double yy = qy*qy; double zz = qz*qz;
143  double xy = qx*qy; double xz = qx*qz; double yz = qy*qz;
144  double wx = qw*qx; double wy = qw*qy; double wz = qw*qz;
145 
146  m[0][0] = 1 - 2 * ( yy + zz );
147  m[0][1] = 2 * ( xy - wz );
148  m[0][2] = 2 * ( xz + wy );
149  m[0][3] = 0;
150 
151  m[1][0] = 2 * ( xy + wz );
152  m[1][1] = 1 - 2 * ( xx + zz );
153  m[1][2] = 2 * ( yz - wx );
154  m[1][3] = 0;
155 
156  m[2][0] = 2 * ( xz - wy );
157  m[2][1] = 2 * ( yz + wx );
158  m[2][2] = 1 - 2 * ( xx + yy );
159  m[2][3] = 0;
160 }