Back to index

salome-med  6.5.0
BoundingBox.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "BoundingBox.hxx"
00021 
00022 #include <iostream>
00023 #include <algorithm>
00024 #include <cassert>
00025 
00026 namespace INTERP_KERNEL
00027 {
00028   
00038   BoundingBox::BoundingBox(const double** pts, const unsigned numPts)
00039     :_coords(new double[6])
00040   {
00041     assert(numPts > 1);     
00042 
00043     // initialize with first two points
00044     const double* pt1 = pts[0];
00045     const double* pt2 = pts[1];
00046 
00047     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
00048       {
00049         _coords[c] = std::min(pt1[c], pt2[c]);
00050         _coords[c + 3] = std::max(pt1[c], pt2[c]);
00051       }
00052 
00053     for(unsigned i = 2 ; i < numPts ; ++i)
00054       {
00055         updateWithPoint(pts[i]);
00056       }
00057   
00058     assert(isValid());
00059   }
00060 
00067   BoundingBox::BoundingBox(const BoundingBox& box1, const BoundingBox& box2) 
00068     : _coords(new double[6])
00069   {
00070     assert(_coords != 0);
00071 
00072     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
00073       {
00074         _coords[c] = std::min(box1._coords[c], box2._coords[c]);
00075         _coords[c + 3] = std::max(box1._coords[c + 3], box2._coords[c + 3]);
00076       }
00077     
00078     assert(isValid());
00079   }
00080 
00085   BoundingBox::~BoundingBox()
00086   {
00087     delete[] _coords;
00088   }
00089 
00096   bool BoundingBox::isDisjointWith(const BoundingBox& box) const
00097   {
00098     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
00099       {
00100         const double otherMinCoord = box.getCoordinate(c);
00101         const double otherMaxCoord = box.getCoordinate(BoxCoord(c + 3));
00102        
00103         // boxes are disjoint if there exists a direction in which the 
00104         // minimum coordinate of one is greater than the maximum coordinate of the other
00105 
00106         // more stable version ?
00107         // const double tol = 1.0e-2*_coords[c];
00108         // if(_coords[c] > otherMaxCoord + tol 
00109         //   || _coords[c + 3] < otherMinCoord - tol)
00110        
00111        
00112         if(_coords[c] > otherMaxCoord 
00113            || _coords[c + 3] < otherMinCoord)
00114        
00115           {
00116             return true;
00117           }
00118        
00119       }
00120     return false;
00121   }
00122     
00123   
00124 
00131   void BoundingBox::updateWithPoint(const double* pt)
00132   {
00133     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
00134       {
00135         const double ptVal = pt[c];
00136 
00137         // update min and max coordinates
00138         _coords[c] = std::min(_coords[c], ptVal);
00139         _coords[c + 3] = std::max(_coords[c + 3], ptVal);
00140 
00141       }
00142   }
00143   
00150   bool BoundingBox::isValid() const
00151   {
00152     bool valid = true;
00153     for(BoxCoord c = XMIN ; c < ZMIN ; c = BoxCoord(c + 1))
00154       {
00155         if(_coords[c] > _coords[c + 3])
00156           {
00157             std::cout << "+++ Error in  BoundingBox |: coordinate " << c << " is invalid : "
00158                       <<_coords[c] << " > " << _coords[c+3] << std::endl;
00159             valid = false;
00160           }
00161       }
00162     return valid;
00163   }
00164 
00165 }