common.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00041 
00042 #include <pcl/apps/point_cloud_editor/common.h>
00043 #include <pcl/apps/point_cloud_editor/localTypes.h>
00044 
00045 void
00046 setIdentity(float* matrix)
00047 {
00048   std::fill_n(matrix, MATRIX_SIZE, 0.0f);
00049   for (unsigned int i = 0; i < MATRIX_SIZE; i+=MATRIX_SIZE_DIM+1)
00050     matrix[i] = 1.0f;
00051 }
00052 
00053 void
00054 multMatrix(const float* left, const float* right, float* result)
00055 {
00056   float r[MATRIX_SIZE];
00057   for(unsigned int i = 0; i < MATRIX_SIZE_DIM; ++i)
00058   {
00059     for(unsigned int j = 0; j < MATRIX_SIZE_DIM; ++j)
00060     {
00061       float sum = 0.0;
00062       for(unsigned int k = 0; k < MATRIX_SIZE_DIM; ++k)
00063         sum += left[i * MATRIX_SIZE_DIM + k] * right[k * MATRIX_SIZE_DIM + j];
00064       r[i * MATRIX_SIZE_DIM + j] = sum;
00065     }
00066   }
00067   std::copy(r, r+MATRIX_SIZE, result);
00068 }
00069 
00070 
00071 // This code was found on:
00072 // http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix
00073 // and is listed as being part of an open soure project (the MESA project)
00074 //
00075 // The original code in MESA comes from __gluInvertMatrixd() in project.c
00076 //
00077 // The mesa license (can be found on http://www.mesa3d.org) is as follows:
00078 //
00079 // The Mesa distribution consists of several components. Different copyrights
00080 // and licenses apply to different components. For example, some demo programs
00081 // are copyrighted by SGI, some of the Mesa device drivers are copyrighted by
00082 // their authors. See below for a list of Mesa's main components and the license
00083 // for each.
00084 //
00085 //The core Mesa library is licensed according to the terms of the MIT license.
00086 // This allows integration with the XFree86, Xorg and DRI projects.
00087 //
00088 //The default Mesa license is as follows:
00089 //
00090 //Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
00091 //
00092 //Permission is hereby granted, free of charge, to any person obtaining a
00093 //copy of this software and associated documentation files (the "Software"),
00094 //to deal in the Software without restriction, including without limitation
00095 //the rights to use, copy, modify, merge, publish, distribute, sublicense,
00096 //and/or sell copies of the Software, and to permit persons to whom the
00097 //Software is furnished to do so, subject to the following conditions:
00098 //
00099 //The above copyright notice and this permission notice shall be included
00100 //in all copies or substantial portions of the Software.
00101 //
00102 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00103 //OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00104 //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00105 //BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00106 //AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00107 //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00108 //
00109 bool invertMatrix(const float* matrix, float* inverse)
00110 {
00111   double inv[16], det;
00112 
00113   inv[0] = matrix[5]  * matrix[10] * matrix[15] -
00114       matrix[5]  * matrix[11] * matrix[14] -
00115       matrix[9]  * matrix[6]  * matrix[15] +
00116       matrix[9]  * matrix[7]  * matrix[14] +
00117       matrix[13] * matrix[6]  * matrix[11] -
00118       matrix[13] * matrix[7]  * matrix[10];
00119 
00120   inv[4] = -matrix[4]  * matrix[10] * matrix[15] +
00121       matrix[4]  * matrix[11] * matrix[14] +
00122       matrix[8]  * matrix[6]  * matrix[15] -
00123       matrix[8]  * matrix[7]  * matrix[14] -
00124       matrix[12] * matrix[6]  * matrix[11] +
00125       matrix[12] * matrix[7]  * matrix[10];
00126 
00127   inv[8] = matrix[4]  * matrix[9] * matrix[15] -
00128       matrix[4]  * matrix[11] * matrix[13] -
00129       matrix[8]  * matrix[5] * matrix[15] +
00130       matrix[8]  * matrix[7] * matrix[13] +
00131       matrix[12] * matrix[5] * matrix[11] -
00132       matrix[12] * matrix[7] * matrix[9];
00133 
00134   inv[12] = -matrix[4]  * matrix[9] * matrix[14] +
00135       matrix[4]  * matrix[10] * matrix[13] +
00136       matrix[8]  * matrix[5] * matrix[14] -
00137       matrix[8]  * matrix[6] * matrix[13] -
00138       matrix[12] * matrix[5] * matrix[10] +
00139       matrix[12] * matrix[6] * matrix[9];
00140 
00141   inv[1] = -matrix[1]  * matrix[10] * matrix[15] +
00142       matrix[1]  * matrix[11] * matrix[14] +
00143       matrix[9]  * matrix[2] * matrix[15] -
00144       matrix[9]  * matrix[3] * matrix[14] -
00145       matrix[13] * matrix[2] * matrix[11] +
00146       matrix[13] * matrix[3] * matrix[10];
00147 
00148   inv[5] = matrix[0]  * matrix[10] * matrix[15] -
00149       matrix[0]  * matrix[11] * matrix[14] -
00150       matrix[8]  * matrix[2] * matrix[15] +
00151       matrix[8]  * matrix[3] * matrix[14] +
00152       matrix[12] * matrix[2] * matrix[11] -
00153       matrix[12] * matrix[3] * matrix[10];
00154 
00155   inv[9] = -matrix[0]  * matrix[9] * matrix[15] +
00156       matrix[0]  * matrix[11] * matrix[13] +
00157       matrix[8]  * matrix[1] * matrix[15] -
00158       matrix[8]  * matrix[3] * matrix[13] -
00159       matrix[12] * matrix[1] * matrix[11] +
00160       matrix[12] * matrix[3] * matrix[9];
00161 
00162   inv[13] = matrix[0]  * matrix[9] * matrix[14] -
00163       matrix[0]  * matrix[10] * matrix[13] -
00164       matrix[8]  * matrix[1] * matrix[14] +
00165       matrix[8]  * matrix[2] * matrix[13] +
00166       matrix[12] * matrix[1] * matrix[10] -
00167       matrix[12] * matrix[2] * matrix[9];
00168 
00169   inv[2] = matrix[1]  * matrix[6] * matrix[15] -
00170       matrix[1]  * matrix[7] * matrix[14] -
00171       matrix[5]  * matrix[2] * matrix[15] +
00172       matrix[5]  * matrix[3] * matrix[14] +
00173       matrix[13] * matrix[2] * matrix[7] -
00174       matrix[13] * matrix[3] * matrix[6];
00175 
00176   inv[6] = -matrix[0]  * matrix[6] * matrix[15] +
00177       matrix[0]  * matrix[7] * matrix[14] +
00178       matrix[4]  * matrix[2] * matrix[15] -
00179       matrix[4]  * matrix[3] * matrix[14] -
00180       matrix[12] * matrix[2] * matrix[7] +
00181       matrix[12] * matrix[3] * matrix[6];
00182 
00183   inv[10] = matrix[0]  * matrix[5] * matrix[15] -
00184       matrix[0]  * matrix[7] * matrix[13] -
00185       matrix[4]  * matrix[1] * matrix[15] +
00186       matrix[4]  * matrix[3] * matrix[13] +
00187       matrix[12] * matrix[1] * matrix[7] -
00188       matrix[12] * matrix[3] * matrix[5];
00189 
00190   inv[14] = -matrix[0]  * matrix[5] * matrix[14] +
00191       matrix[0]  * matrix[6] * matrix[13] +
00192       matrix[4]  * matrix[1] * matrix[14] -
00193       matrix[4]  * matrix[2] * matrix[13] -
00194       matrix[12] * matrix[1] * matrix[6] +
00195       matrix[12] * matrix[2] * matrix[5];
00196 
00197   inv[3] = -matrix[1] * matrix[6] * matrix[11] +
00198       matrix[1] * matrix[7] * matrix[10] +
00199       matrix[5] * matrix[2] * matrix[11] -
00200       matrix[5] * matrix[3] * matrix[10] -
00201       matrix[9] * matrix[2] * matrix[7] +
00202       matrix[9] * matrix[3] * matrix[6];
00203 
00204   inv[7] = matrix[0] * matrix[6] * matrix[11] -
00205       matrix[0] * matrix[7] * matrix[10] -
00206       matrix[4] * matrix[2] * matrix[11] +
00207       matrix[4] * matrix[3] * matrix[10] +
00208       matrix[8] * matrix[2] * matrix[7] -
00209       matrix[8] * matrix[3] * matrix[6];
00210 
00211   inv[11] = -matrix[0] * matrix[5] * matrix[11] +
00212       matrix[0] * matrix[7] * matrix[9] +
00213       matrix[4] * matrix[1] * matrix[11] -
00214       matrix[4] * matrix[3] * matrix[9] -
00215       matrix[8] * matrix[1] * matrix[7] +
00216       matrix[8] * matrix[3] * matrix[5];
00217 
00218   inv[15] = matrix[0] * matrix[5] * matrix[10] -
00219       matrix[0] * matrix[6] * matrix[9] -
00220       matrix[4] * matrix[1] * matrix[10] +
00221       matrix[4] * matrix[2] * matrix[9] +
00222       matrix[8] * matrix[1] * matrix[6] -
00223       matrix[8] * matrix[2] * matrix[5];
00224 
00225   det = matrix[0] * inv[0] + matrix[1] * inv[4] +
00226         matrix[2] * inv[8] + matrix[3] * inv[12];
00227 
00228   if (det == 0)
00229     return (false);
00230 
00231   det = 1.0 / det;
00232 
00233   for (unsigned int i = 0; i < MATRIX_SIZE; ++i)
00234     inverse[i] = inv[i] * det;
00235 
00236   return (true);
00237 }
00238 
00239 void
00240 stringToLower(std::string &s)
00241 {
00242   std::transform(s.begin(), s.end(), s.begin(), tolower);
00243 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:22:49