Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
GeomDefs.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   : ICLGeom/src/ICLGeom/GeomDefs.h                         **
00010 ** Module : ICLGeom                                                **
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.GPL  **
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 <ICLCore/Types.h>
00034 #include <ICLMath/FixedMatrix.h>
00035 #include <ICLMath/FixedVector.h>
00036 #include <vector>
00037 #include <ICLCore/Color.h>
00038 #include <ICLUtils/Point32f.h>
00039 
00040 namespace icl{
00041   namespace geom{
00043     typedef core::Color4D32f GeomColor;
00044   
00046     inline GeomColor geom_white(float alpha=255) { return GeomColor(255,255,255,alpha); }
00047   
00049     inline GeomColor geom_red(float alpha=255) { return GeomColor(255,0,0,alpha); }
00050   
00052     inline GeomColor geom_blue(float alpha=255) { return GeomColor(0,100,255,alpha); }
00053   
00055     inline GeomColor geom_green(float alpha=255) { return GeomColor(0,255,0,alpha); }
00056   
00058     inline GeomColor geom_yellow(float alpha=255) { return GeomColor(255,255,0,alpha); }
00059   
00061     inline GeomColor geom_magenta(float alpha=255) { return GeomColor(255,0,255,alpha); }
00062   
00064     inline GeomColor geom_cyan(float alpha=255) { return GeomColor(0,255,255,alpha); }
00065   
00067     inline GeomColor geom_black(float alpha=255) { return GeomColor(0,0,0,alpha); }
00068   
00070     inline GeomColor geom_invisible() { return GeomColor(0,0,0,0); }
00071   
00073     typedef math::FixedMatrix<icl32f,4,4> Mat4D32f;
00074   
00076     typedef math::FixedMatrix<icl64f,4,4> Mat4D64f;
00077   
00079     typedef math::FixedColVector<icl32f,4> Vec4D32f;
00080   
00082     typedef math::FixedColVector<icl64f,4> Vec4D64f;
00083   
00085     typedef Vec4D32f Vec;
00086   
00088     typedef Mat4D32f Mat;
00089   
00091     typedef math::FixedColVector<icl32f,3> Vec3;
00092     
00093     
00095 
00096     template<class T>
00097     T linear_interpolate(const T &a, const T &b, float x){
00098       return a * (1-x) + b*x;
00099     }
00100 
00102 
00105     float dist_point_linesegment(const Vec &p,
00106                                  const Vec &lineStart,
00107                                  const Vec &lineEnd,
00108                                  Vec *nearestPoint=0);
00109     
00110     
00112 
00115     float dist_point_triangle(const Vec &p,
00116                               const Vec &a,
00117                               const Vec &b,
00118                               const Vec &c,
00119                               Vec *nearestPoint=0);
00120     
00121     
00123 
00130     inline Vec bilinear_interpolate(const Vec corners[4], float x, float y){
00131       const Vec a = linear_interpolate(corners[0],corners[1],x);
00132       const Vec b = linear_interpolate(corners[2],corners[3],x);
00133       Vec c = linear_interpolate(a,b,y);
00134       c[3] = 1;
00135       return c;
00136     }
00137 
00139     template<class T>
00140     inline math::FixedColVector<T,4> normalize(const math::FixedMatrix<T,1,4> &v) { 
00141       double l = v.length();
00142       ICLASSERT_RETURN_VAL(l,v);
00143       return v/l;
00144     }
00146     template<class T>
00147     inline math::FixedColVector<T,4> normalize3(const math::FixedMatrix<T,1,4> &v,const double& h=1) { 
00148       double l = ::sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
00149       ICLASSERT_RETURN_VAL(l,v);
00150       Vec n = v/l;
00151       // XXX 
00152       n[3]=h;
00153       return n;
00154     }
00155   
00157     inline float sprod3(const Vec &a, const Vec &b){
00158       return a[0]*b[0] + a[1]*b[1]+ a[2]*b[2];
00159     }
00160     
00162     inline float sqrnorm3(const Vec &a){
00163       return sprod3(a,a);
00164     }
00165       
00167     inline float norm3(const Vec &a){
00168       return ::sqrt(sqrnorm3(a));
00169     }
00170   
00171   
00173     template<class T>
00174     inline math::FixedColVector<T,4> homogenize(const math::FixedMatrix<T,1,4> &v){
00175       ICLASSERT_RETURN_VAL(v[3],v); return v/v[3];
00176     }
00177   
00179     template<class T>
00180     inline math::FixedColVector<T,4> project(math::FixedMatrix<T,1,4> v, T z){
00181       T zz = z*v[2];
00182       v[0]/=zz;
00183       v[1]/=zz;
00184       v[2]=0;
00185       v[3]=1;
00186       return v;
00187     }
00188     
00190     template<class T>
00191     inline math::FixedColVector<T,4> cross(const math::FixedMatrix<T,1,4> &v1, const math::FixedMatrix<T,1,4> &v2){
00192       return math::FixedColVector<T,4>(v1[1]*v2[2]-v1[2]*v2[1],
00193                                  v1[2]*v2[0]-v1[0]*v2[2],
00194                                  v1[0]*v2[1]-v1[1]*v2[0],
00195                                  1 );
00196     }
00197   
00199     typedef std::vector<Vec> VecArray;
00200   
00201     
00203     inline Vec rotate_vector(const Vec &axis, float angle, const Vec &vec){
00204       return math::create_rot_4x4(axis[0],axis[1],axis[2],angle)*vec;
00205       /*
00206           angle /= 2;
00207           float a = cos(angle);
00208           float sa = sin(angle);
00209           float b = axis[0] * sa;
00210           float c = axis[1] * sa;
00211           float d = axis[2] * sa;
00212           
00213           float a2=a*a, b2=b*b, c2=c*c, d2=d*d;
00214           float ab=a*b, ac=a*c, ad=a*d, bc=b*c, bd=b*d, cd=c*d;
00215           
00216           Mat X(a2+b2-c2-d2,  2*bc-2*ad,   2*ac+2*bd,   0,
00217           2*ad+2*bd,    a2-b2+c2-d2, 2*cd-2*ab,   0,
00218           2*bd-2*ac,    2*ab+2*cd,   a2-b2-c2+d2, 0,
00219           0,            0,           0,           1);
00220           
00221           return X * vec;
00222       */
00223     }
00224   
00225   } // namespace geom
00226 }
00227 
00228 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines