9324aa2d |
1 | // Created by: Eugeny MALTCHIKOV |
2 | // Copyright (c) 2017 OPEN CASCADE SAS |
3 | // |
4 | // This file is part of Open CASCADE Technology software library. |
5 | // |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
11 | // |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
14 | |
15 | #ifndef BOPTools_PairSelector_HeaderFile |
16 | #define BOPTools_PairSelector_HeaderFile |
17 | |
18 | #include <BVH_Traverse.hxx> |
19 | #include <BVH_BoxSet.hxx> |
20 | |
21 | #include <Standard_Integer.hxx> |
22 | #include <TColStd_ListOfInteger.hxx> |
23 | #include <algorithm> |
24 | |
25 | //! Template Selector for selection of the elements from two BVH trees. |
26 | template <int Dimension> |
27 | class BOPTools_PairSelector : |
28 | public BVH_PairTraverse <Standard_Real, Dimension, BVH_BoxSet <Standard_Real, Dimension, Standard_Integer>> |
29 | { |
30 | public: //! @name public types |
31 | |
32 | //! Auxiliary structure to keep the pair of indices |
33 | struct PairIDs |
34 | { |
35 | PairIDs (const Standard_Integer theId1 = -1, |
36 | const Standard_Integer theId2 = -1) |
37 | : ID1 (theId1), ID2 (theId2) |
38 | {} |
39 | |
40 | Standard_Boolean operator< (const PairIDs& theOther) const |
41 | { |
42 | return ID1 < theOther.ID1 || |
43 | (ID1 == theOther.ID1 && ID2 < theOther.ID2); |
44 | } |
45 | |
46 | Standard_Integer ID1; |
47 | Standard_Integer ID2; |
48 | }; |
49 | |
50 | typedef typename BVH::VectorType<Standard_Real, Dimension>::Type BVH_VecNd; |
51 | |
52 | public: //! @name Constructor |
53 | |
54 | //! Empty constructor |
55 | BOPTools_PairSelector() |
56 | : mySameBVHs (Standard_False) |
57 | {} |
58 | |
59 | public: //! @name public interfaces |
60 | |
61 | //! Clears the indices |
62 | void Clear() |
63 | { |
64 | myPairs.Clear(); |
65 | } |
66 | |
67 | //! Sorts the indices |
68 | void Sort() |
69 | { |
70 | std::sort (myPairs.begin(), myPairs.end()); |
71 | } |
72 | |
73 | //! Tells to selector that BVH trees are the same. |
74 | //! If the flag is set to true the resulting vector will contain |
75 | //! only unique pairs (mirrored pairs will be rejected, |
76 | //! e.g. (1, 2) will be taken, (2, 1) will be rejected) and will |
77 | //! not contain pairs in which IDs are the same (pair (1, 1) will be rejected). |
78 | //! If it is required to have a full vector of pairs even |
79 | //! for the same BVH trees, just keep the false value of this flag. |
80 | void SetSame (const Standard_Boolean theIsSame) |
81 | { |
82 | mySameBVHs = theIsSame; |
83 | } |
84 | |
85 | //! Returns the list of accepted indices |
86 | const std::vector<PairIDs>& Pairs() const |
87 | { |
88 | return myPairs; |
89 | } |
90 | |
91 | public: //! @name Rejection/Acceptance rules |
92 | |
93 | //! Basing on the bounding boxes of the nodes checks if the pair of nodes should be rejected. |
94 | virtual Standard_Boolean RejectNode (const BVH_VecNd& theCMin1, |
95 | const BVH_VecNd& theCMax1, |
96 | const BVH_VecNd& theCMin2, |
97 | const BVH_VecNd& theCMax2, |
98 | Standard_Real&) const Standard_OVERRIDE |
99 | { |
100 | return BVH_Box<Standard_Real, 3> (theCMin1, theCMax1).IsOut (theCMin2, theCMax2); |
101 | } |
102 | |
103 | //! Checks if the pair of elements should be rejected. |
104 | Standard_Boolean RejectElement (const Standard_Integer theID1, |
105 | const Standard_Integer theID2) |
106 | { |
107 | return (mySameBVHs && theID1 >= theID2) || |
108 | this->myBVHSet1->Box (theID1).IsOut( |
109 | this->myBVHSet2->Box (theID2)); |
110 | } |
111 | |
112 | //! Checks and accepts the pair of elements. |
113 | virtual Standard_Boolean Accept (const Standard_Integer theID1, |
114 | const Standard_Integer theID2) Standard_OVERRIDE |
115 | { |
116 | if (!RejectElement (theID1, theID2)) |
117 | { |
118 | myPairs.push_back (PairIDs (this->myBVHSet1->Element (theID1), |
119 | this->myBVHSet2->Element (theID2))); |
120 | return Standard_True; |
121 | } |
122 | return Standard_False; |
123 | } |
124 | |
125 | protected: //! @name Fields |
126 | |
127 | std::vector<PairIDs> myPairs; //!< Selected pairs of indices |
128 | Standard_Boolean mySameBVHs; //!< Selection is performed from the same BVH trees |
129 | }; |
130 | |
131 | #endif |