Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/BVH_Traverse.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Created by: Eugeny MALTCHIKOV
0002 // Created on: 2019-04-17
0003 // Copyright (c) 2019 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _BVH_Traverse_Header
0017 #define _BVH_Traverse_Header
0018 
0019 #include <BVH_Box.hxx>
0020 #include <BVH_Tree.hxx>
0021 
0022 //! The classes implement the traverse of the BVH tree.
0023 //!
0024 //! There are two traverse methods implemented:
0025 //! - Traverse of the single tree
0026 //! - Parallel traverse of two trees
0027 //!
0028 //! To perform Selection of the elements from BVH_Tree using
0029 //! the traverse methods implemented here it is
0030 //! required to define Acceptance/Rejection rules in the
0031 //! following methods:
0032 //! - *RejectNode* - Node rejection by its bounding box.
0033 //!   It is applied to both inner and outer nodes of the BVH tree.
0034 //!   Optionally, the method should compute the metric for the node
0035 //!   which will allow performing traverse faster by descending by the
0036 //!   best branches.
0037 //! - *Accept* - Element acceptance. It takes the index of the element
0038 //!   of BVH tree. The access to the element itself should be performed
0039 //!   through the set on which BVH is built.
0040 //!   The *Accept* method implements the leaf node operation and usually
0041 //!   defines the logic of the whole operation.
0042 //! - *IsMetricBetter* - Compares the metrics of the nodes and returns
0043 //!   true if the left metric is better than the right one.
0044 //! - *RejectMetric* - Node rejection by the metric. It should compare
0045 //!   the metric of the node with the global one and return true
0046 //!   if the global metric is better than the given one.
0047 //! - *Stop* - implements conditions to stop the tree descend if the necessary
0048 //!   elements are already found.
0049 //!
0050 //! The selector of a single tree has an extra method which allows
0051 //! accepting the whole branches without any further checks
0052 //! (e.g. full inclusion test):
0053 //! - *AcceptMetric* - basing on the metric of the node decides if the
0054 //!   node may be accepted without any further checks.
0055 //!
0056 //! Two ways of selection are possible:
0057 //! 1. Set the BVH set containing the tree and use the method Select()
0058 //!    which allows using common interface for setting the BVH Set for accessing
0059 //!    the BVH tree and elements in the Accept method.
0060 //! 2. Keep the BVHSetType void, do not set the BVH set and use the
0061 //!    method Select (const BVH_Tree<>&) which allows performing selection
0062 //!    on the arbitrary BVH tree.
0063 //!
0064 //! Here is the example of usage of the traverse to find the point-triangulation
0065 //! minimal distance.
0066 //! ~~~~
0067 //! // Structure to contain points of the triangle
0068 //! struct Triangle
0069 //! {
0070 //!   Triangle() {}
0071 //!   Triangle(const BVH_Vec3d& theNode1, 
0072 //!            const BVH_Vec3d& theNode2,
0073 //!            const BVH_Vec3d& theNode3)
0074 //!     : Node1 (theNode1), Node2 (theNode2), Node3 (theNode3)
0075 //!   {}
0076 //!
0077 //!   BVH_Vec3d Node1;
0078 //!   BVH_Vec3d Node2;
0079 //!   BVH_Vec3d Node3;
0080 //! };
0081 //!
0082 //! // Selector for min point-triangulation distance
0083 //! class BVH_PointTriangulationSqDist :
0084 //!   public BVH_Distance<Standard_Real, 3, BVH_Vec3d, BVH_BoxSet<Standard_Real, 3, Triangle>>
0085 //! {
0086 //! public:
0087 //!
0088 //!   // Computes the distance from the point to bounding box 
0089 //!   virtual Standard_Boolean RejectNode (const BVH_Vec3d& theCMin,
0090 //!                                        const BVH_Vec3d& theCMax,
0091 //!                                        Standard_Real& theDistance) const Standard_OVERRIDE
0092 //!   {
0093 //!     theDistance = BVH_Tools<Standard_Real, 3>::PointBoxSquareDistance (myObject, theCMin, theCMax);
0094 //!     return RejectMetric (theDistance);
0095 //!   }
0096 //!
0097 //!   // Computes the distance from the point to triangle
0098 //!   virtual Standard_Boolean Accept (const Standard_Integer theIndex,
0099 //!                                    const Standard_Real&) Standard_OVERRIDE
0100 //!   {
0101 //!     const Triangle& aTri = myBVHSet->Element (theIndex);
0102 //!     Standard_Real aDist = BVH_Tools<Standard_Real, 3>::PointTriangleSquareDistance (myObject, aTri.Node1, aTri.Node2, aTri.Node3);
0103 //!     if (aDist < myDistance)
0104 //!     {
0105 //!       myDistance = aDist;
0106 //!       return Standard_True;
0107 //!     }
0108 //!     return Standard_False;
0109 //!   }
0110 //! };
0111 //!
0112 //! // Point to which the distance is required
0113 //! BVH_Vec3d aPoint = ...;
0114 //! // BVH Set containing triangulation
0115 //! opencascade::handle<BVH_BoxSet<Standard_Real, 3, Triangle>> aTriangulationSet = ...;
0116 //!
0117 //! BVH_PointTriangulationSqDist aDistTool;
0118 //! aDistTool.SetObject (aPoint);
0119 //! aDistTool.SetBVHSet (aTriangulationSet.get());
0120 //! aDistTool.ComputeDistance();
0121 //! if (aDistTool.IsDone())
0122 //! {
0123 //!   Standard_Real aPointTriSqDist = aDistTool.Distance();
0124 //! }
0125 //!
0126 //! ~~~~
0127 //!
0128 
0129 //! Abstract class implementing the base Traverse interface
0130 //! required for selection of the elements from BVH tree.
0131 //!
0132 //! \tparam MetricType Type of metric to perform more optimal tree descend
0133 template <class MetricType>
0134 class BVH_BaseTraverse
0135 {
0136 public: //! @name Metrics comparison for choosing the best branch
0137 
0138   //! Compares the two metrics and chooses the best one.
0139   //! Returns true if the first metric is better than the second,
0140   //! false otherwise.
0141   virtual Standard_Boolean IsMetricBetter (const MetricType&,
0142                                            const MetricType&) const
0143   {
0144     // Keep the left to right tree descend by default
0145     return Standard_True;
0146   }
0147 
0148 public: //! @name Rejection of the node by metric
0149 
0150   //! Rejects the node by the metric
0151   virtual Standard_Boolean RejectMetric (const MetricType&) const
0152   {
0153     // Do not reject any nodes by metric by default
0154     return Standard_False;
0155   }
0156 
0157 public: //! @name Condition to stop the descend
0158 
0159   //! Returns the flag controlling the tree descend.
0160   //! Returns true if the tree descend should be stopped.
0161   virtual Standard_Boolean Stop() const
0162   {
0163     // Do not stop tree descend by default
0164     return Standard_False;
0165   }
0166 
0167 protected: //! @name Constructors
0168 
0169   //! Constructor
0170   BVH_BaseTraverse() {}
0171 
0172   //! Destructor
0173   virtual ~BVH_BaseTraverse() {}
0174 };
0175 
0176 //! Abstract class implementing the traverse of the single binary tree.
0177 //! Selection of the data from the tree is performed by the
0178 //! rules defined in the Accept/Reject methods.
0179 //! See description of the required methods in the comments above.
0180 //!
0181 //! \tparam NumType Numeric data type
0182 //! \tparam Dimension Vector dimension
0183 //! \tparam BVHSetType Type of set containing the BVH tree (required to access the elements by the index)
0184 //! \tparam MetricType Type of metric to perform more optimal tree descend
0185 template <class NumType, int Dimension, class BVHSetType = void, class MetricType = NumType>
0186 class BVH_Traverse : public BVH_BaseTraverse<MetricType>
0187 {
0188 public: //! @name public types
0189 
0190   typedef typename BVH_Box<NumType, Dimension>::BVH_VecNt BVH_VecNt;
0191 
0192 public: //! @name Constructor
0193 
0194   //! Constructor
0195   BVH_Traverse()
0196     : BVH_BaseTraverse<MetricType>(),
0197       myBVHSet (NULL)
0198   {}
0199 
0200 public: //! @name Setting the set to access the elements and BVH tree
0201 
0202   //! Sets the BVH Set containing the BVH tree
0203   void SetBVHSet (BVHSetType* theBVHSet)
0204   {
0205     myBVHSet = theBVHSet;
0206   }
0207 
0208 public: //! @name Rules for Accept/Reject
0209 
0210   //! Basing on the given metric, checks if the whole branch may be
0211   //! accepted without any further checks.
0212   //! Returns true if the metric is accepted, false otherwise.
0213   virtual Standard_Boolean AcceptMetric (const MetricType&) const
0214   {
0215     // Do not accept the whole branch by default
0216     return Standard_False;
0217   }
0218 
0219   //! Rejection of the node by bounding box.
0220   //! Metric is computed to choose the best branch.
0221   //! Returns true if the node should be rejected, false otherwise.
0222   virtual Standard_Boolean RejectNode (const BVH_VecNt& theCornerMin,
0223                                        const BVH_VecNt& theCornerMax,
0224                                        MetricType& theMetric) const = 0;
0225 
0226   //! Leaf element acceptance.
0227   //! Metric of the parent leaf-node is passed to avoid the check on the
0228   //! element and accept it unconditionally.
0229   //! Returns true if the element has been accepted, false otherwise.
0230   virtual Standard_Boolean Accept (const Standard_Integer theIndex,
0231                                    const MetricType& theMetric) = 0;
0232 
0233 public: //! @name Selection
0234 
0235   //! Selection of the elements from the BVH tree by the
0236   //! rules defined in Accept/Reject methods.
0237   //! The method requires the BVHSet containing BVH tree to be set.
0238   //! Returns the number of accepted elements.
0239   Standard_Integer Select()
0240   {
0241     if (myBVHSet)
0242     {
0243       const opencascade::handle<BVH_Tree <NumType, Dimension>>& aBVH = myBVHSet->BVH();
0244       return Select (aBVH);
0245     }
0246     return 0;
0247   }
0248 
0249   //! Performs selection of the elements from the BVH tree by the
0250   //! rules defined in Accept/Reject methods.
0251   //! Returns the number of accepted elements.
0252   Standard_Integer Select (const opencascade::handle<BVH_Tree <NumType, Dimension>>& theBVH);
0253 
0254 protected: //! @name Fields
0255 
0256   BVHSetType* myBVHSet;
0257 };
0258 
0259 //! Abstract class implementing the parallel traverse of two binary trees.
0260 //! Selection of the data from the trees is performed by the
0261 //! rules defined in the Accept/Reject methods.
0262 //! See description of the required methods in the comments above.
0263 //!
0264 //! \tparam NumType Numeric data type
0265 //! \tparam Dimension Vector dimension
0266 //! \tparam BVHSetType Type of set containing the BVH tree (required to access the elements by the index)
0267 //! \tparam MetricType Type of metric to perform more optimal tree descend
0268 template <class NumType, int Dimension, class BVHSetType = void, class MetricType = NumType>
0269 class BVH_PairTraverse : public BVH_BaseTraverse<MetricType>
0270 {
0271 public: //! @name public types
0272 
0273   typedef typename BVH_Box<NumType, Dimension>::BVH_VecNt BVH_VecNt;
0274 
0275 public: //! @name Constructor
0276 
0277   //! Constructor
0278   BVH_PairTraverse()
0279     : BVH_BaseTraverse<MetricType>(),
0280       myBVHSet1 (NULL),
0281       myBVHSet2 (NULL)
0282   {
0283   }
0284 
0285 public: //! @name Setting the sets to access the elements and BVH trees
0286 
0287   //! Sets the BVH Sets containing the BVH trees
0288   void SetBVHSets (BVHSetType* theBVHSet1,
0289                    BVHSetType* theBVHSet2)
0290   {
0291     myBVHSet1 = theBVHSet1;
0292     myBVHSet2 = theBVHSet2;
0293   }
0294 
0295 public: //! @name Rules for Accept/Reject
0296 
0297   //! Rejection of the pair of nodes by bounding boxes.
0298   //! Metric is computed to choose the best branch.
0299   //! Returns true if the pair of nodes should be rejected, false otherwise.
0300   virtual Standard_Boolean RejectNode (const BVH_VecNt& theCornerMin1,
0301                                        const BVH_VecNt& theCornerMax1,
0302                                        const BVH_VecNt& theCornerMin2,
0303                                        const BVH_VecNt& theCornerMax2,
0304                                        MetricType& theMetric) const = 0;
0305 
0306   //! Leaf element acceptance.
0307   //! Returns true if the pair of elements is accepted, false otherwise.
0308   virtual Standard_Boolean Accept (const Standard_Integer theIndex1,
0309                                    const Standard_Integer theIndex2) = 0;
0310 
0311 public: //! @name Selection
0312 
0313   //! Selection of the pairs of elements of two BVH trees by the
0314   //! rules defined in Accept/Reject methods.
0315   //! The method requires the BVHSets containing BVH trees to be set.
0316   //! Returns the number of accepted pairs of elements.
0317   Standard_Integer Select()
0318   {
0319     if (myBVHSet1 && myBVHSet2)
0320     {
0321       const opencascade::handle <BVH_Tree <NumType, Dimension> >& aBVH1 = myBVHSet1->BVH();
0322       const opencascade::handle <BVH_Tree <NumType, Dimension> >& aBVH2 = myBVHSet2->BVH();
0323       return Select (aBVH1, aBVH2);
0324     }
0325     return 0;
0326   }
0327 
0328   //! Performs selection of the elements from two BVH trees by the
0329   //! rules defined in Accept/Reject methods.
0330   //! Returns the number of accepted pairs of elements.
0331   Standard_Integer Select (const opencascade::handle<BVH_Tree <NumType, Dimension>>& theBVH1,
0332                            const opencascade::handle<BVH_Tree <NumType, Dimension>>& theBVH2);
0333 
0334 protected: //! @name Fields
0335 
0336   BVHSetType* myBVHSet1;
0337   BVHSetType* myBVHSet2;
0338 
0339 };
0340 
0341 #include <BVH_Traverse.lxx>
0342 
0343 #endif // _BVH_Traverse_Header