1 // Copyright (C) 2007-2024 CEA, EDF
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)
40 initializeWith(pts,numPts);
43 void BoundingBox::fillInXMinXmaxYminYmaxZminZmaxFormat(double data[6]) const
45 data[0] = this->getCoordinate(BoundingBox::XMIN);
46 data[1] = this->getCoordinate(BoundingBox::XMAX);
47 data[2] = this->getCoordinate(BoundingBox::YMIN);
48 data[3] = this->getCoordinate(BoundingBox::YMAX);
49 data[4] = this->getCoordinate(BoundingBox::ZMIN);
50 data[5] = this->getCoordinate(BoundingBox::ZMAX);
54 * Constructor creating box from an array of the points corresponding
55 * to the vertices of the element.
56 * Each point is represented by an array of three doubles.
58 * @param pts array of points
59 * @param numPts number of vertices
62 void BoundingBox::initializeWith(const double** pts, const unsigned numPts)
64 // initialize with first two points
65 const double *pt0(pts[0]);
67 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
70 _coords[c + 3] = pt0[c];
73 for(unsigned i = 1 ; i < numPts ; ++i)
75 updateWithPoint(pts[i]);
82 * Constructor creating box from union of two boxes, resulting in a box that encloses both of them
84 * @param box1 the first box
85 * @param box2 the second box
87 BoundingBox::BoundingBox(const BoundingBox& box1, const BoundingBox& box2)
89 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
91 _coords[c] = std::min(box1._coords[c], box2._coords[c]);
92 _coords[c + 3] = std::max(box1._coords[c + 3], box2._coords[c + 3]);
99 * Determines if the intersection with a given box is empty
101 * @param box BoundingBox with which intersection is tested
102 * @return true if intersection between boxes is empty, false if not
104 bool BoundingBox::isDisjointWith(const BoundingBox& box) const
106 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
108 const double otherMinCoord = box.getCoordinate(c);
109 const double otherMaxCoord = box.getCoordinate(BoxCoord(c + 3));
111 // boxes are disjoint if there exists a direction in which the
112 // minimum coordinate of one is greater than the maximum coordinate of the other
114 // more stable version ?
115 // const double tol = 1.0e-2*_coords[c];
116 // if(_coords[c] > otherMaxCoord + tol
117 // || _coords[c + 3] < otherMinCoord - tol)
120 if(_coords[c] > otherMaxCoord
121 || _coords[c + 3] < otherMinCoord)
134 * Updates the bounding box to include a given point
136 * @param pt point to be included
139 void BoundingBox::updateWithPoint(const double* pt)
141 for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
143 const double ptVal = pt[c];
145 // update min and max coordinates
146 _coords[c] = std::min(_coords[c], ptVal);
147 _coords[c + 3] = std::max(_coords[c + 3], ptVal);
153 * Checks if the box is valid, which it is if its minimum coordinates are
154 * smaller than its maximum coordinates in all directions.
156 * @return true if the box is valid, false if not
158 bool BoundingBox::isValid() const
161 for(BoxCoord c = XMIN ; c < ZMIN ; c = BoxCoord(c + 1))
163 if(_coords[c] > _coords[c + 3])
165 std::cout << "+++ Error in BoundingBox |: coordinate " << c << " is invalid : "
166 <<_coords[c] << " > " << _coords[c+3] << std::endl;
173 void BoundingBox::toCompactData(double data[6]) const
175 data[0]=_coords[XMIN];
176 data[1]=_coords[XMAX];
177 data[2]=_coords[YMIN];
178 data[3]=_coords[YMAX];
179 data[4]=_coords[ZMIN];
180 data[5]=_coords[ZMAX];