1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "BoundingBox.hxx"
26 namespace INTERP_KERNEL
30 * Constructor creating box from an array of the points corresponding
31 * to the vertices of the element.
32 * Each point is represented by an array of three doubles.
34 * @param pts array of points
35 * @param numPts number of vertices
38 BoundingBox::BoundingBox(const double** pts, const unsigned numPts)
39 :_coords(new double[6])
43 // initialize with first two points
44 const double *pt0(pts[0]);
46 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
49 _coords[c + 3] = pt0[c];
52 for(unsigned i = 1 ; i < numPts ; ++i)
54 updateWithPoint(pts[i]);
61 * Constructor creating box from union of two boxes, resulting in a box that encloses both of them
63 * @param box1 the first box
64 * @param box2 the second box
66 BoundingBox::BoundingBox(const BoundingBox& box1, const BoundingBox& box2)
67 : _coords(new double[6])
71 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
73 _coords[c] = std::min(box1._coords[c], box2._coords[c]);
74 _coords[c + 3] = std::max(box1._coords[c + 3], box2._coords[c + 3]);
84 BoundingBox::~BoundingBox()
90 * Determines if the intersection with a given box is empty
92 * @param box BoundingBox with which intersection is tested
93 * @return true if intersection between boxes is empty, false if not
95 bool BoundingBox::isDisjointWith(const BoundingBox& box) const
97 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
99 const double otherMinCoord = box.getCoordinate(c);
100 const double otherMaxCoord = box.getCoordinate(BoxCoord(c + 3));
102 // boxes are disjoint if there exists a direction in which the
103 // minimum coordinate of one is greater than the maximum coordinate of the other
105 // more stable version ?
106 // const double tol = 1.0e-2*_coords[c];
107 // if(_coords[c] > otherMaxCoord + tol
108 // || _coords[c + 3] < otherMinCoord - tol)
111 if(_coords[c] > otherMaxCoord
112 || _coords[c + 3] < otherMinCoord)
125 * Updates the bounding box to include a given point
127 * @param pt point to be included
130 void BoundingBox::updateWithPoint(const double* pt)
132 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
134 const double ptVal = pt[c];
136 // update min and max coordinates
137 _coords[c] = std::min(_coords[c], ptVal);
138 _coords[c + 3] = std::max(_coords[c + 3], ptVal);
144 * Checks if the box is valid, which it is if its minimum coordinates are
145 * smaller than its maximum coordinates in all directions.
147 * @return true if the box is valid, false if not
149 bool BoundingBox::isValid() const
152 for(BoxCoord c = XMIN ; c < ZMIN ; c = BoxCoord(c + 1))
154 if(_coords[c] > _coords[c + 3])
156 std::cout << "+++ Error in BoundingBox |: coordinate " << c << " is invalid : "
157 <<_coords[c] << " > " << _coords[c+3] << std::endl;
164 void BoundingBox::toCompactData(double data[6]) const
166 data[0]=_coords[XMIN];
167 data[1]=_coords[XMAX];
168 data[2]=_coords[YMIN];
169 data[3]=_coords[YMAX];
170 data[4]=_coords[ZMIN];
171 data[5]=_coords[ZMAX];