Salome HOME
a06e7505884497cadcd9e57d380275dec309ea15
[tools/medcoupling.git] / src / INTERP_KERNEL / DirectedBoundingBox.hxx
1 // Copyright (C) 2009-2016  OPEN CASCADE
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 #ifndef __DIRECTEDBOUNDINGBOX_HXX__
21 #define __DIRECTEDBOUNDINGBOX_HXX__
22
23 #include "INTERPKERNELDefines.hxx"
24
25 #include <vector>
26
27 namespace INTERP_KERNEL
28 {
29
30   /**
31    * \brief Class representing the bounding box of a number of points
32    *  with box axes parallel to principal axes of inertia of points
33    */
34   class DirectedBoundingBox
35   {
36   public:
37
38     INTERPKERNEL_EXPORT DirectedBoundingBox();
39
40     INTERPKERNEL_EXPORT DirectedBoundingBox(const double* pts, const unsigned numPts, const unsigned dim);
41
42     INTERPKERNEL_EXPORT DirectedBoundingBox(const double** pts, const unsigned numPts, const unsigned dim);
43
44     //~DirectedBoundingBox();
45
46     INTERPKERNEL_EXPORT void enlarge(const double tol);
47     
48     INTERPKERNEL_EXPORT bool isDisjointWith(const DirectedBoundingBox& box) const;
49
50     INTERPKERNEL_EXPORT bool isDisjointWith(const double* box) const;
51
52     INTERPKERNEL_EXPORT bool isOut(const double* point) const;
53
54
55     // return internal data
56     INTERPKERNEL_EXPORT std::vector<double> getData() const;
57
58     // initialize with data returned by getData()
59     INTERPKERNEL_EXPORT void setData(const double* data);
60
61     // return size of internal data
62     INTERPKERNEL_EXPORT static int dataSize(int dim);
63
64   private:
65
66     //void computeAxes3D(const std::vector<double>& tensor);
67
68     //void computeAxes2D(const std::vector<double>& tensor);
69
70     inline void addPointToBox(const double* coord);
71
72     void toLocalCS(const double* p, double* pLoc) const;
73
74     void fromLocalCS(const double* p, double* pGlob) const;
75
76     inline bool isLocalOut(const double* pLoc) const;
77
78     void getCorners(std::vector<double>& corners, const double* minmax) const;
79
80     unsigned _dim;
81
82     std::vector<double> _axes; //!< principal axes of inertia in full interlace
83     std::vector<double> _minmax; //!< pairs of min an max coordinates along the axes
84
85   };
86
87   //================================================================================
88   /*!
89    * \brief Test point in local CS against box extremities
90    * 
91    */
92   //================================================================================
93
94   inline bool DirectedBoundingBox::isLocalOut(const double* pLoc) const
95     {
96       for ( int i = 0; i < (int)_dim; ++i )
97         if ( pLoc[i] < _minmax[i*2] || pLoc[i] > _minmax[i*2+1] )
98           return true;
99       return false;
100     }
101
102   //================================================================================
103   /*!
104    * \brief Update box extremities
105    */
106   //================================================================================
107
108   inline void DirectedBoundingBox::addPointToBox(const double* coord)
109   {
110     for ( int i = 0; i < (int)_dim; ++i )
111       {
112         double c = 0;
113         for ( int j = 0; j < (int)_dim; ++j ) c += coord[j]*_axes[i*_dim+j];
114         if ( c < _minmax[i*2] )   _minmax[i*2] = c;
115         if ( c > _minmax[i*2+1] ) _minmax[i*2+1] = c;
116       }
117   }
118 }
119 #endif