Salome HOME
Doc: indicating how to pass MPI_Comm from mpi4py
[tools/medcoupling.git] / src / INTERP_KERNEL / BoundingBox.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "BoundingBox.hxx"
21
22 #include <iostream>
23 #include <algorithm>
24 #include <cassert>
25
26 namespace INTERP_KERNEL
27 {
28   
29   /**
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.
33    *
34    * @param pts     array of points 
35    * @param numPts  number of vertices
36    *
37    */
38   BoundingBox::BoundingBox(const double** pts, const unsigned numPts)
39   {
40     initializeWith(pts,numPts);
41   }
42
43   void BoundingBox::fillInXMinXmaxYminYmaxZminZmaxFormat(double data[6]) const
44   {
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);
51   }
52
53   /**
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.
57    *
58    * @param pts     array of points 
59    * @param numPts  number of vertices
60    *
61    */
62   void BoundingBox::initializeWith(const double** pts, const unsigned numPts)
63   {
64     // initialize with first two points
65     const double *pt0(pts[0]);
66
67     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
68       {
69         _coords[c] = pt0[c];
70         _coords[c + 3] = pt0[c];
71       }
72
73     for(unsigned i = 1 ; i < numPts ; ++i)
74       {
75         updateWithPoint(pts[i]);
76       }
77   
78     assert(isValid());
79   }
80
81   /**
82    * Constructor creating box from union of two boxes, resulting in a box that encloses both of them
83    *
84    * @param  box1  the first box
85    * @param  box2  the second box
86    */
87   BoundingBox::BoundingBox(const BoundingBox& box1, const BoundingBox& box2)
88   {
89     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
90       {
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]);
93       }
94     
95     assert(isValid());
96   }
97
98   /**
99    * Determines if the intersection with a given box is empty
100    * 
101    * @param    box   BoundingBox with which intersection is tested
102    * @return  true if intersection between boxes is empty, false if not
103    */
104   bool BoundingBox::isDisjointWith(const BoundingBox& box) const
105   {
106     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
107       {
108         const double otherMinCoord = box.getCoordinate(c);
109         const double otherMaxCoord = box.getCoordinate(BoxCoord(c + 3));
110        
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
113
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)
118        
119        
120         if(_coords[c] > otherMaxCoord 
121            || _coords[c + 3] < otherMinCoord)
122        
123           {
124             return true;
125           }
126        
127       }
128     return false;
129   }
130     
131   
132
133   /**
134    * Updates the bounding box to include a given point
135    * 
136    * @param pt    point to be included
137    *
138    */
139   void BoundingBox::updateWithPoint(const double* pt)
140   {
141     for(BoxCoord c = XMIN ; c <= ZMIN ; c = BoxCoord(c + 1))
142       {
143         const double ptVal = pt[c];
144
145         // update min and max coordinates
146         _coords[c] = std::min(_coords[c], ptVal);
147         _coords[c + 3] = std::max(_coords[c + 3], ptVal);
148
149       }
150   }
151   
152   /**
153    * Checks if the box is valid, which it is if its minimum coordinates are
154    * smaller than its maximum coordinates in all directions.
155    *
156    * @return  true if the box is valid, false if not
157    */
158   bool BoundingBox::isValid() const
159   {
160     bool valid = true;
161     for(BoxCoord c = XMIN ; c < ZMIN ; c = BoxCoord(c + 1))
162       {
163         if(_coords[c] > _coords[c + 3])
164           {
165             std::cout << "+++ Error in  BoundingBox |: coordinate " << c << " is invalid : "
166                       <<_coords[c] << " > " << _coords[c+3] << std::endl;
167             valid = false;
168           }
169       }
170     return valid;
171   }
172
173   void BoundingBox::toCompactData(double data[6]) const
174   {
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];
181   }
182
183 }