mat.hpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2007, Michael Kazhdan
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification,
00006 are permitted provided that the following conditions are met:
00007 
00008 Redistributions of source code must retain the above copyright notice, this list of
00009 conditions and the following disclaimer. Redistributions in binary form must reproduce
00010 the above copyright notice, this list of conditions and the following disclaimer
00011 in the documentation and/or other materials provided with the distribution. 
00012 
00013 Neither the name of the Johns Hopkins University nor the names of its contributors
00014 may be used to endorse or promote products derived from this software without specific
00015 prior written permission. 
00016 
00017 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00018 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00020 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00022 TO, PROCUREMENT OF SUBSTITUTE  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
00023 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00025 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00026 DAMAGE.
00027 */
00029 // MinimalAreaTriangulation //
00031 
00032 namespace pcl
00033 {
00034   namespace poisson
00035   {
00036 
00037     template <class Real>
00038     MinimalAreaTriangulation<Real>::MinimalAreaTriangulation(void)
00039     {
00040       bestTriangulation=NULL;
00041       midPoint=NULL;
00042     }
00043     template <class Real>
00044     MinimalAreaTriangulation<Real>::~MinimalAreaTriangulation(void)
00045     {
00046       if(bestTriangulation)
00047         delete[] bestTriangulation;
00048       bestTriangulation=NULL;
00049       if(midPoint)
00050         delete[] midPoint;
00051       midPoint=NULL;
00052     }
00053     template <class Real>
00054     void MinimalAreaTriangulation<Real>::GetTriangulation(const std::vector<Point3D<Real> >& vertices,std::vector<TriangleIndex>& triangles)
00055     {
00056       if(vertices.size()==3)
00057       {
00058         triangles.resize(1);
00059         triangles[0].idx[0]=0;
00060         triangles[0].idx[1]=1;
00061         triangles[0].idx[2]=2;
00062         return;
00063       }
00064       else if(vertices.size()==4)
00065       {
00066         TriangleIndex tIndex[2][2];
00067         Real area[2];
00068 
00069         area[0]=area[1]=0;
00070         triangles.resize(2);
00071 
00072         tIndex[0][0].idx[0]=0;
00073         tIndex[0][0].idx[1]=1;
00074         tIndex[0][0].idx[2]=2;
00075         tIndex[0][1].idx[0]=2;
00076         tIndex[0][1].idx[1]=3;
00077         tIndex[0][1].idx[2]=0;
00078 
00079         tIndex[1][0].idx[0]=0;
00080         tIndex[1][0].idx[1]=1;
00081         tIndex[1][0].idx[2]=3;
00082         tIndex[1][1].idx[0]=3;
00083         tIndex[1][1].idx[1]=1;
00084         tIndex[1][1].idx[2]=2;
00085 
00086         Point3D<Real> n,p1,p2;
00087         for(int i=0;i<2;i++)
00088           for(int j=0;j<2;j++)
00089           {
00090             p1=vertices[tIndex[i][j].idx[1]]-vertices[tIndex[i][j].idx[0]];
00091             p2=vertices[tIndex[i][j].idx[2]]-vertices[tIndex[i][j].idx[0]];
00092             CrossProduct(p1,p2,n);
00093             area[i] += Real( Length(n) );
00094           }
00095         if(area[0]>area[1])
00096         {
00097           triangles[0]=tIndex[1][0];
00098           triangles[1]=tIndex[1][1];
00099         }
00100         else
00101         {
00102           triangles[0]=tIndex[0][0];
00103           triangles[1]=tIndex[0][1];
00104         }
00105         return;
00106       }
00107       if(bestTriangulation)
00108         delete[] bestTriangulation;
00109       if(midPoint)
00110         delete[] midPoint;
00111       bestTriangulation=NULL;
00112       midPoint=NULL;
00113       size_t eCount=vertices.size();
00114       bestTriangulation=new Real[eCount*eCount];
00115       midPoint=new int[eCount*eCount];
00116       for(size_t i=0;i<eCount*eCount;i++)
00117         bestTriangulation[i]=-1;
00118       memset(midPoint,-1,sizeof(int)*eCount*eCount);
00119       GetArea(0,1,vertices);
00120       triangles.clear();
00121       GetTriangulation(0,1,vertices,triangles);
00122     }
00123     template <class Real>
00124     Real MinimalAreaTriangulation<Real>::GetArea(const std::vector<Point3D<Real> >& vertices)
00125     {
00126       if(bestTriangulation)
00127         delete[] bestTriangulation;
00128       if(midPoint)
00129         delete[] midPoint;
00130       bestTriangulation=NULL;
00131       midPoint=NULL;
00132       int eCount=vertices.size();
00133       bestTriangulation=new double[eCount*eCount];
00134       midPoint=new int[eCount*eCount];
00135       for(int i=0;i<eCount*eCount;i++)
00136         bestTriangulation[i]=-1;
00137       memset(midPoint,-1,sizeof(int)*eCount*eCount);
00138       return GetArea(0,1,vertices);
00139     }
00140     template<class Real>
00141     void MinimalAreaTriangulation<Real>::GetTriangulation(const size_t& i,const size_t& j,const std::vector<Point3D<Real> >& vertices,std::vector<TriangleIndex>& triangles)
00142     {
00143       TriangleIndex tIndex;
00144       size_t eCount=vertices.size();
00145       size_t ii=i;
00146       if(i<j)
00147         ii+=eCount;
00148       if(j+1>=ii)
00149         return;
00150       ii=midPoint[i*eCount+j];
00151       if(ii>=0)
00152       {
00153         tIndex.idx[0] = int( i );
00154         tIndex.idx[1] = int( j );
00155         tIndex.idx[2] = int( ii );
00156         triangles.push_back(tIndex);
00157         GetTriangulation(i,ii,vertices,triangles);
00158         GetTriangulation(ii,j,vertices,triangles);
00159       }
00160     }
00161 
00162     template<class Real>
00163     Real MinimalAreaTriangulation<Real>::GetArea(const size_t& i,const size_t& j,const std::vector<Point3D<Real> >& vertices)
00164     {
00165       Real a=FLT_MAX,temp;
00166       size_t eCount=vertices.size();
00167       size_t idx=i*eCount+j;
00168       size_t ii=i;
00169       if(i<j)
00170         ii+=eCount;
00171       if(j+1>=ii)
00172       {
00173         bestTriangulation[idx]=0;
00174         return 0;
00175       }
00176       if(midPoint[idx]!=-1)
00177         return bestTriangulation[idx];
00178       int mid=-1;
00179       for(size_t r=j+1;r<ii;r++)
00180       {
00181         size_t rr=r%eCount;
00182         size_t idx1=i*eCount+rr,idx2=rr*eCount+j;
00183         Point3D<Real> p,p1,p2;
00184         p1=vertices[i]-vertices[rr];
00185         p2=vertices[j]-vertices[rr];
00186         CrossProduct(p1,p2,p);
00187         temp = Real( Length(p) );
00188         if(bestTriangulation[idx1]>=0)
00189         {
00190           temp+=bestTriangulation[idx1];
00191           if(temp>a)
00192             continue;
00193           if(bestTriangulation[idx2]>0)
00194             temp+=bestTriangulation[idx2];
00195           else
00196             temp+=GetArea(rr,j,vertices);
00197         }
00198         else
00199         {
00200           if(bestTriangulation[idx2]>=0)
00201             temp+=bestTriangulation[idx2];
00202           else
00203             temp+=GetArea(rr,j,vertices);
00204           if(temp>a)
00205             continue;
00206           temp+=GetArea(i,rr,vertices);
00207         }
00208 
00209         if(temp<a)
00210         {
00211           a=temp;
00212           mid=int(rr);
00213         }
00214       }
00215       bestTriangulation[idx]=a;
00216       midPoint[idx]=mid;
00217 
00218       return a;
00219     }
00220 
00221 
00222   }
00223 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:25:28