Image Component Library (ICL)
HomogeneousMath.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLMath/src/ICLMath/HomogeneousMath.h **
10 ** Module : ICLMath **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLMath/FixedMatrix.h>
34 #include <ICLMath/FixedVector.h>
35 
36 namespace icl{
37  namespace math{
38 
41 
44 
47 
50 
52 
53  template<class T>
54  T linear_interpolate(const T &a, const T &b, float x){
55  return a * (1-x) + b*x;
56  }
57 
59 
63  const Vec4 &lineStart,
64  const Vec4 &lineEnd,
65  Vec4 *nearestPoint=0);
66 
67 
69 
72  ICLMath_API float dist_point_triangle(const Vec4 &p,
73  const Vec4 &a,
74  const Vec4 &b,
75  const Vec4 &c,
76  Vec4 *nearestPoint=0);
77 
78 
80 
87  inline Vec4 bilinear_interpolate(const Vec4 corners[4], float x, float y){
88  const Vec4 a = linear_interpolate(corners[0],corners[1],x);
89  const Vec4 b = linear_interpolate(corners[2],corners[3],x);
90  Vec4 c = linear_interpolate(a,b,y);
91  c[3] = 1;
92  return c;
93  }
94 
96  template<class T>
98  double l = v.length();
100  return v/l;
101  }
103  template<class T>
104  inline math::FixedColVector<T,4> normalize3(const math::FixedMatrix<T,1,4> &v,const double& h=1) {
105  double l = ::sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
107  Vec4 n = v/l;
108  // XXX
109  n[3]=h;
110  return n;
111  }
112 
114  inline float sprod3(const Vec3 &a, const Vec3 &b){
115  return a[0]*b[0] + a[1]*b[1]+ a[2]*b[2];
116  }
117 
118  template<class T>
120  FixedMatrix<T,3,3> result(T(0));
121  Vec3 w1 = mat.col(0);
122  Vec3 w2 = mat.col(1);
123  Vec3 w3 = mat.col(2);
124  Vec3 v1 = w1.normalized();
125  Vec3 v2 = (w2 - (v1*sprod3(v1,w2))).normalized();
126  Vec3 v3 = (w3 - v1*sprod3(v1,w3) - v2*sprod3(v2,w3)).normalized();
127  result.col(0) = v1;
128  result.col(1) = v2;
129  result.col(2) = v3;
130  return result;
131  }
132 
133  /*template<class T, unsigned int DIM>
134  inline float sprod(FixedColVector<T,DIM> const &a, FixedColVector<T,DIM> const &b) {
135  float result = 0;
136  for(int i = 0; i < DIM; ++i) {
137  result += a[i]*b[i];
138  }
139  return result;
140  }
141 
142  template<class T, unsigned int COLS, unsigned int ROWS>
143  inline FixedMatrix<T,COLS,ROWS> gramSchmidtOrtho(FixedMatrix<T,COLS,ROWS> const &mat) {
144  FixedMatrix<T,COLS,ROWS> r(T(0.f));
145 
146  FixedColVector<T,ROWS> w = mat.col(0);
147  r.col(0) = w.normalized();
148 
149  for(unsigned int i = 1; i < COLS; ++i) {
150  FixedColVector<T,ROWS> w_i = mat.col(i);
151  FixedColVector<T,ROWS> tmp(0.f);
152  for(unsigned int k = 0; k < i; ++k) {
153  FixedColVector<T,ROWS> v_k = mat.col(k);
154  tmp += v_k*sprod(v_k,w_i);
155  }
156  r.col(i) = (w_i-tmp).normalized();
157  }
158 
159  return r;
160  }*/
161 
163  inline float sprod3(const Vec4 &a, const Vec4 &b){
164  return a[0]*b[0] + a[1]*b[1]+ a[2]*b[2];
165  }
166 
167 
169  inline float sqrnorm3(const Vec4 &a){
170  return sprod3(a,a);
171  }
172 
174  inline float norm3(const Vec4 &a){
176  }
177 
179  inline float dist3(const Vec4 &a, const Vec4 &b){
180  return norm3(a-b);
181  }
182 
183 
185  template<class T>
187  ICLASSERT_RETURN_VAL(v[3],v); return v/v[3];
188  }
189 
191  template<class T>
193  T zz = z*v[2];
194  v[0]/=zz;
195  v[1]/=zz;
196  v[2]=0;
197  v[3]=1;
198  return v;
199  }
200 
202  template<class T>
204  return math::FixedColVector<T,4>(v1[1]*v2[2]-v1[2]*v2[1],
205  v1[2]*v2[0]-v1[0]*v2[2],
206  v1[0]*v2[1]-v1[1]*v2[0],
207  1 );
208  }
209 
210  template<class T>
212  return math::FixedColVector<T,3>(v1[1]*v2[2]-v1[2]*v2[1],
213  v1[2]*v2[0]-v1[0]*v2[2],
214  v1[0]*v2[1]-v1[1]*v2[0]);
215  }
216 
218  inline Vec4 rotate_vector(const Vec4 &axis, float angle, const Vec4 &vec){
219  return create_rot_4x4(axis[0],axis[1],axis[2],angle)*vec;
220  }
221 
222  }
223 }
ICLQt_API ImgQ sqrt(const ImgQ &image)
calls sqrt( each pixel)
ICLMath_API float dist_point_triangle(const Vec4 &p, const Vec4 &a, const Vec4 &b, const Vec4 &c, Vec4 *nearestPoint=0)
distance from point p to triangle (a - b - c)
ICLMath_API float dist_point_linesegment(const Vec4 &p, const Vec4 &lineStart, const Vec4 &lineEnd, Vec4 *nearestPoint=0)
distance from point p to line segment (lineStart -> lineEnd)
math::FixedColVector< T, 4 > normalize3(const math::FixedMatrix< T, 1, 4 > &v, const double &h=1)
normalize a vector to length 1
Definition: HomogeneousMath.h:104
FixedMatrixPart< T, ROWS, col_iterator > col(unsigned int idx)
returns a matrix col-reference iterator pair
Definition: FixedMatrix.h:781
float sqrnorm3(const Vec4 &a)
sqared norm for 4D homogeneous vectors (ignoring the homegeneous component)
Definition: HomogeneousMath.h:169
FixedMatrix< icl32f, 4, 4 > Mat4
typedef for 4x4 fixed matrices
Definition: HomogeneousMath.h:49
ICLMath_IMP FixedMatrix< T, 4, 4 > create_rot_4x4(T axisX, T axisY, T axisZ, T angle)
create 4D homogeneous matrix that rotates about given axis by given angle (defined for float and doub...
undocument this line if you encounter any issues!
Definition: Any.h:37
FixedMatrix< icl32f, 3, 3 > Mat3
typedef for 3x3 fixed matrices
Definition: HomogeneousMath.h:46
math::FixedColVector< T, 4 > cross(const math::FixedMatrix< T, 1, 4 > &v1, const math::FixedMatrix< T, 1, 4 > &v2)
homogeneous 3D cross-product
Definition: HomogeneousMath.h:203
#define ICLMath_API
Definition: CompatMacros.h:173
Vec4 rotate_vector(const Vec4 &axis, float angle, const Vec4 &vec)
rotates a vector around a given axis
Definition: HomogeneousMath.h:218
FixedColVector< icl32f, 4 > Vec4
another shortcut for 3D vectors
Definition: HomogeneousMath.h:43
float norm3(const Vec4 &a)
3D- euclidian norm for 4D homogeneous vectors (ignoring the homegeneous component)
Definition: HomogeneousMath.h:174
FixedMatrix< T, COLS, ROWS > normalized(T norm=2) const
create a normalized version of this matrix
Definition: FixedMatrix.h:850
Vec4 bilinear_interpolate(const Vec4 corners[4], float x, float y)
bilinear vector interpolation
Definition: HomogeneousMath.h:87
math::FixedColVector< T, 4 > homogenize(const math::FixedMatrix< T, 1, 4 > &v)
homogenize a vector by normalizing 4th component to 1
Definition: HomogeneousMath.h:186
float sprod3(const Vec3 &a, const Vec3 &b)
3D scalar (aka dot-) product for 4D homogeneous vectors (ignoring the homegeneous component)
Definition: HomogeneousMath.h:114
#define ICLASSERT_RETURN_VAL(X, VALUE)
Definition: Macros.h:148
double length(T norm=2) const
Calculates the length of the matrix data vector.
Definition: FixedMatrix.h:835
T linear_interpolate(const T &a, const T &b, float x)
linearly interpolates between a, b (x must be in range [0,1])
Definition: HomogeneousMath.h:54
math::FixedColVector< T, 4 > normalize(const math::FixedMatrix< T, 1, 4 > &v)
normalize a vector to length 1
Definition: HomogeneousMath.h:97
math::FixedColVector< T, 4 > project(math::FixedMatrix< T, 1, 4 > v, T z)
perform perspective projection
Definition: HomogeneousMath.h:192
axis
for flipping of images
Definition: Types.h:99
FixedMatrix< T, 3, 3 > gramSchmidtOrtho(FixedMatrix< T, 3, 3 > const &mat)
Definition: HomogeneousMath.h:119
FixedColVector< icl32f, 3 > Vec3
another shortcut for 3D vectors
Definition: HomogeneousMath.h:40
float dist3(const Vec4 &a, const Vec4 &b)
3d euclidian distance
Definition: HomogeneousMath.h:179