Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
HomogeneousMath.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLMath/src/ICLMath/HomogeneousMath.h                  **
00010 ** Module : ICLMath                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLMath/FixedMatrix.h>
00034 #include <ICLMath/FixedVector.h>
00035 
00036 namespace icl{
00037   namespace math{
00038 
00040     typedef FixedColVector<icl32f,3> Vec3;
00041 
00043     typedef FixedColVector<icl32f,4> Vec4;
00044     
00046     typedef FixedMatrix<icl32f, 3,3> Mat3;
00047 
00049     typedef FixedMatrix<icl32f, 4,4> Mat4;
00050     
00052 
00053     template<class T>
00054     T linear_interpolate(const T &a, const T &b, float x){
00055       return a * (1-x) + b*x;
00056     }
00057     
00059 
00062     ICLMath_API float dist_point_linesegment(const Vec4 &p,
00063                                              const Vec4 &lineStart,
00064                                              const Vec4 &lineEnd,
00065                                              Vec4 *nearestPoint=0);
00066     
00067     
00069 
00072     ICLMath_API float dist_point_triangle(const Vec4 &p,
00073                                           const Vec4 &a,
00074                                           const Vec4 &b,
00075                                           const Vec4 &c,
00076                                           Vec4 *nearestPoint=0);
00077     
00078     
00080 
00087     inline Vec4 bilinear_interpolate(const Vec4 corners[4], float x, float y){
00088       const Vec4 a = linear_interpolate(corners[0],corners[1],x);
00089       const Vec4 b = linear_interpolate(corners[2],corners[3],x);
00090       Vec4 c = linear_interpolate(a,b,y);
00091       c[3] = 1;
00092       return c;
00093     }
00094 
00096     template<class T>
00097     inline math::FixedColVector<T,4> normalize(const math::FixedMatrix<T,1,4> &v) { 
00098       double l = v.length();
00099       ICLASSERT_RETURN_VAL(l,v);
00100       return v/l;
00101     }
00103     template<class T>
00104     inline math::FixedColVector<T,4> normalize3(const math::FixedMatrix<T,1,4> &v,const double& h=1) { 
00105       double l = ::sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
00106       ICLASSERT_RETURN_VAL(l,v);
00107       Vec4 n = v/l;
00108       // XXX 
00109       n[3]=h;
00110       return n;
00111     }
00112   
00114     inline float sprod3(const Vec4 &a, const Vec4 &b){
00115       return a[0]*b[0] + a[1]*b[1]+ a[2]*b[2];
00116     }
00117 
00118     
00120     inline float sqrnorm3(const Vec4 &a){
00121       return sprod3(a,a);
00122     }
00123       
00125     inline float norm3(const Vec4 &a){
00126       return ::sqrt(sqrnorm3(a));
00127     }
00128   
00130     inline float dist3(const Vec4 &a, const Vec4 &b){
00131       return norm3(a-b);
00132     }
00133 
00134   
00136     template<class T>
00137     inline math::FixedColVector<T,4> homogenize(const math::FixedMatrix<T,1,4> &v){
00138       ICLASSERT_RETURN_VAL(v[3],v); return v/v[3];
00139     }
00140   
00142     template<class T>
00143     inline math::FixedColVector<T,4> project(math::FixedMatrix<T,1,4> v, T z){
00144       T zz = z*v[2];
00145       v[0]/=zz;
00146       v[1]/=zz;
00147       v[2]=0;
00148       v[3]=1;
00149       return v;
00150     }
00151     
00153     template<class T>
00154     inline math::FixedColVector<T,4> cross(const math::FixedMatrix<T,1,4> &v1, const math::FixedMatrix<T,1,4> &v2){
00155       return math::FixedColVector<T,4>(v1[1]*v2[2]-v1[2]*v2[1],
00156                                  v1[2]*v2[0]-v1[0]*v2[2],
00157                                  v1[0]*v2[1]-v1[1]*v2[0],
00158                                  1 );
00159     }
00160   
00161     
00163     inline Vec4 rotate_vector(const Vec4 &axis, float angle, const Vec4 &vec){
00164       return create_rot_4x4(axis[0],axis[1],axis[2],angle)*vec;
00165     }
00166 
00167   }
00168 }  
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines