3988d4123346c94c90e33b9f4e5306445bf1f577
[occt.git] / src / BVH / BVH_Distance.hxx
1 // Created by: Eugeny MALTCHIKOV
2 // Created on: 2019-04-17
3 // Copyright (c) 2019 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _BVH_Distance_Header
17 #define _BVH_Distance_Header
18
19 #include <BVH_Traverse.hxx>
20
21 //! Abstract class for computation of the min distance between some
22 //! Object and elements of BVH tree.
23 //! To use this class it is required to define two methods:
24 //! - *RejectNode* to compute distance from the object to bounding box
25 //! - *Accept* to compute distance from the object to the element of tree
26 //!
27 //! \tparam NumType Numeric data type
28 //! \tparam Dimension Vector dimension
29 //! \tparam ObjectType Type of the object to which the distance is required
30 //! \tparam BVHSetType Type of the set on which BVH is built
31 template <class NumType, int Dimension, class ObjectType, class BVHSetType>
32 class BVH_Distance : public BVH_Traverse<NumType, Dimension, BVHSetType, NumType>
33 {
34 public: //! @name Constructor
35
36   //! Constructor
37   BVH_Distance()
38     : BVH_Traverse <NumType, Dimension, BVHSetType, NumType>(),
39       myDistance (std::numeric_limits<NumType>::max()),
40       myIsDone(Standard_False)
41   {
42   }
43
44 public: //! @name Setting object for distance computation
45
46   //! Sets the object to which the distance is required
47   void SetObject (const ObjectType& theObject)
48   {
49     myObject = theObject;
50   }
51
52 public: //! @name Compute the distance
53
54   //! Computes the distance between object and BVH tree
55   NumType ComputeDistance()
56   {
57     myIsDone = this->Select() > 0;
58     return myDistance;
59   }
60
61 public: //! @name Accessing the results
62
63   //! Returns IsDone flag
64   Standard_Boolean IsDone () const { return myIsDone; }
65
66   //! Returns the computed distance
67   NumType Distance() const { return myDistance; }
68
69 public: //! @name Definition of the rules for tree descend
70
71   //! Compares the two metrics and chooses the best one
72   virtual Standard_Boolean IsMetricBetter (const NumType& theLeft,
73                                            const NumType& theRight) const Standard_OVERRIDE
74   {
75     return theLeft < theRight;
76   }
77
78   //! Rejects the branch by the metric
79   virtual Standard_Boolean RejectMetric (const NumType& theMetric) const Standard_OVERRIDE
80   {
81     return theMetric > myDistance;
82   }
83
84   //! Returns the flag controlling the tree descend
85   virtual Standard_Boolean Stop() const Standard_OVERRIDE
86   {
87     return myDistance == static_cast<NumType>(0);
88   }
89
90 protected: //! @name Fields
91
92   NumType myDistance;        //!< Distance
93   Standard_Boolean myIsDone; //!< State of the algorithm
94   ObjectType myObject;       //!< Object to compute the distance to
95
96 };
97
98 #endif // _BVH_Distance_Header