xyz_utils.h
Go to the documentation of this file.
00001 // Copyright (c) 2009-2010  INRIA Sophia-Antipolis (France).
00002 // All rights reserved.
00003 //
00004 //This file is part of ESBTL.
00005 //
00006 //ESBTL is free software: you can redistribute it and/or modify
00007 //it under the terms of the GNU General Public License as published by
00008 //the Free Software Foundation, either version 3 of the License, or
00009 //(at your option) any later version.
00010 //
00011 //ESBTL is distributed in the hope that it will be useful,
00012 //but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //GNU General Public License for more details.
00015 //
00016 //You should have received a copy of the GNU General Public License
00017 //along with ESBTL.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 //
00020 //Additional permission under GNU GPL version 3 section 7
00021 //
00022 //If you modify this Library, or any covered work, by linking or
00023 //combining it with CGAL (or a modified version of that library), the
00024 //licensors of this Library grant you additional permission to convey
00025 //the resulting work. Corresponding Source for a non-source form of
00026 //such a combination shall include the source code for the parts of CGAL
00027 //used as well as that of the covered work. 
00028 //
00029 //
00030 //
00031 // Author(s)     :  Sébastien Loriot
00032 
00033 
00034 
00035 #ifndef ESBTL_XYZ_UTILS_H
00036 #define ESBTL_XYZ_UTILS_H
00037 
00038 #include <cmath>
00039 #include <math.h>
00040 #include <limits>
00041 #include <ESBTL/constants.h>
00042 #include <boost/iterator/zip_iterator.hpp>
00043 #include <boost/tuple/tuple.hpp>
00044 
00045 namespace ESBTL{
00046   
00050   class Point_3{
00051   public:
00052     Point_3(double x,double y,double z):x_(x),y_(y),z_(z){}
00053     Point_3():x_(0),y_(0),z_(0){}
00054     
00055     DECLARE_AND_ACCESS(x,double)
00056     DECLARE_AND_ACCESS(y,double)
00057     DECLARE_AND_ACCESS(z,double)
00058   };
00059 
00066   template <class NT>
00067   inline NT square(const NT& n){return n*n;}
00068 
00069 
00078   template <class Point1,class Point2>
00079   inline double squared_distance(const Point1& p1,const Point2& p2){
00080     return square(p1.x()-p2.x())+square(p1.y()-p2.y())+square(p1.z()-p2.z());
00081   }
00082 
00083 
00094   template <class Iterator>
00095   double rms_no_align(Iterator begin1,Iterator end1,Iterator begin2,Iterator end2){
00096     typedef boost::tuple<Iterator,Iterator>   It_tuple;
00097     typedef boost::zip_iterator<It_tuple>     Zip_iterator;
00098     double nb=0;
00099     double sum_sqd=0;
00100     for (Zip_iterator it=boost::make_zip_iterator(boost::make_tuple(begin1,begin1));it!=boost::make_zip_iterator(boost::make_tuple(end1,end2));++it){
00101       sum_sqd+=squared_distance(boost::get<0>(*it),boost::get<1>(*it));
00102       nb+=1;
00103     }
00104     return sqrt(sum_sqd/nb);
00105   }
00106   
00115   template <class Point_3,class Iterator>
00116   std::pair<Point_3,Point_3> 
00117   bounding_box(Iterator begin,Iterator end){
00118     double xmin=std::numeric_limits<double>::max(),ymin=std::numeric_limits<double>::max(),zmin=std::numeric_limits<double>::max();
00119     double xmax=std::numeric_limits<double>::min(),ymax=std::numeric_limits<double>::min(),zmax=std::numeric_limits<double>::min();
00120     for (Iterator it=begin;it!=end;++it){
00121       if (it->x() < xmin) xmin=it->x();
00122       if (it->x() > xmax) xmax=it->x();
00123       if (it->y() < ymin) ymin=it->y();
00124       if (it->y() > ymax) ymax=it->y();
00125       if (it->z() < zmin) zmin=it->z();
00126       if (it->z() > zmax) zmax=it->z();     
00127     }
00128     return std::make_pair(Point_3(xmin,ymin,zmin),Point_3(xmax,ymax,zmax));
00129   }
00130 
00139   template <class Point_3,class Iterator>
00140   inline
00141   std::pair<Point_3,double> 
00142   bounding_cube(Iterator begin,Iterator end){
00143     std::pair<Point_3,Point_3> bbox=bounding_box<Point_3>(begin,end);
00144     double edge_length=bbox.second.x()-bbox.first.x();
00145     if (edge_length < bbox.second.y()-bbox.first.y()) edge_length=bbox.second.y()-bbox.first.y();
00146     if (edge_length < bbox.second.z()-bbox.first.z()) edge_length=bbox.second.z()-bbox.first.z();
00147     
00148     return std::make_pair( bbox.first,nextafter(edge_length,std::numeric_limits<double>::max()) );
00149   }  
00150     
00151 } //namespace ESBTL
00152 
00153 
00154 #endif //ESBTL_XYZ_UTILS_H