ICP.cc 6.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184




//=============================================================================
//
//  CLASS ICP - IMPLEMENTATION
//
//=============================================================================

#define ICP_C

//== INCLUDES =================================================================

#include "ICP.hh"
#include <float.h>


//== NAMESPACES ===============================================================

namespace ICP {

//== IMPLEMENTATION ==========================================================

template < typename VectorT >
inline
VectorT  
center_of_gravity(const std::vector< VectorT >& _points ) {
   VectorT cog(0.0,0.0,0.0);
   
   for (uint i = 0 ; i < _points.size() ; ++i ) {
     cog = cog + _points[i];
   }
   
   cog = cog / ( typename VectorT::value_type )_points.size();

   return cog;
}


template < typename VectorT , typename QuaternionT >
void 
icp(std::vector< VectorT >& _points1 , std::vector< VectorT >& _points2  , VectorT& _cog1 ,  VectorT& _cog2 , double& _scale1 , double& _scale2 , QuaternionT& _rotation )
{
   // compute Mean of Points
   _cog1 = center_of_gravity(_points1);
   _cog2 = center_of_gravity(_points2);
   
   
   VectorT diff;
   _scale1 = 0.0;
   _scale2 = 0.0;
   for ( uint i = 0 ; i < _points1.size() ; ++i ) {
       diff = _points1[i] - _cog1;
       _scale1 = std::max( _scale1 , sqrt( diff.norm() ) );
       diff = _points2[i] - _cog2;
       _scale2 = std::max( _scale2 , sqrt( diff.norm() ) );       
   }
   
   double Sxx = 0.0;
   double Sxy = 0.0;
   double Sxz = 0.0;
   
   double Syx = 0.0;
   double Syy = 0.0;
   double Syz = 0.0;
   
   double Szx = 0.0;
   double Szy = 0.0;
   double Szz = 0.0;
   
    for ( uint i = 0 ; i < _points1.size() ; ++i  ) {
      Sxx += ( _points1[i][0] - _cog1[0] ) * ( _points2[i][0] - _cog2[0] );
      Sxy += ( _points1[i][0] - _cog1[0] ) * ( _points2[i][1] - _cog2[1] );
      Sxz += ( _points1[i][0] - _cog1[0] ) * ( _points2[i][2] - _cog2[2] );
      
      Syx += ( _points1[i][1] - _cog1[1] ) * ( _points2[i][0] - _cog2[0] );
      Syy += ( _points1[i][1] - _cog1[1] ) * ( _points2[i][1] - _cog2[1] );
      Syz += ( _points1[i][1] - _cog1[1] ) * ( _points2[i][2] - _cog2[2] );
      
      Szx += ( _points1[i][2] - _cog1[2] ) * ( _points2[i][0] - _cog2[0] );
      Szy += ( _points1[i][2] - _cog1[2] ) * ( _points2[i][1] - _cog2[1] );
      Szz += ( _points1[i][2] - _cog1[2] ) * ( _points2[i][2] - _cog2[2] );
   }
   
   const double scale = _scale1 * _scale2;
   
   Sxx = Sxx * 1.0 / scale;
   Sxy = Sxy * 1.0 / scale;
   Sxz = Sxz * 1.0 / scale;
   Syx = Syx * 1.0 / scale;
   Syy = Syy * 1.0 / scale;
   Syz = Syz * 1.0 / scale;
   Szx = Szx * 1.0 / scale;
   Szy = Szy * 1.0 / scale;
   Szz = Szz * 1.0 / scale;
   
   gmm::dense_matrix<double> N; gmm::resize(N,4,4); gmm::clear(N);
   N(0,0) = Sxx + Syy + Szz;
   N(0,1) = Syz - Szy;
   N(0,2) = Szx - Sxz;
   N(0,3) = Sxy - Syx;
   
   N(1,0) = Syz - Szy;
   N(1,1) = Sxx - Syy - Szz;
   N(1,2) = Sxy + Syx;
   N(1,3) = Szx + Sxz;
   
   N(2,0) = Szx - Sxz;
   N(2,1) = Sxy + Syx;
   N(2,2) = -Sxx + Syy - Szz;
   N(2,3) = Syz + Szy;
   
   N(3,0) = Sxy - Syx;
   N(3,1) = Szx + Sxz;
   N(3,2) = Syz + Szy;
   N(3,3) = -Sxx - Syy + Szz;
   
   std::vector< double > eigval; eigval.resize(4);
   gmm::dense_matrix< double > eigvect; gmm::resize(eigvect, 4,4); gmm::clear(eigvect);
   gmm::symmetric_qr_algorithm(N, eigval, eigvect);
   
   int gr = 0;
   double max = -FLT_MAX;
   for (int i = 0; i < 4; i++)
   if (eigval[i] > max)
   {
      gr = i;
      max = eigval[i];
   }
   
   _rotation[0] = eigvect(0,gr);
   _rotation[1] = eigvect(1,gr);
   _rotation[2] = eigvect(2,gr);
   _rotation[3] = eigvect(3,gr);
}


//=============================================================================

} //namespace ICP

//=============================================================================