0025122: Expose internal static function ComputeDeviation from ShapeAnalysis_Edge.cxx
[occt.git] / src / ShapeAnalysis / ShapeAnalysis_BoxBndTree.cxx
1 // Created on: 2005-02-14
2 // Created by: Alexey MORENOV
3 // Copyright (c) 2005-2014 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 #include <ShapeAnalysis_BoxBndTree.hxx>
17 #include <Standard_NoSuchObject.hxx>
18 #include <ShapeAnalysis_Edge.hxx>
19 #include <TopoDS_Vertex.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <TopoDS_Wire.hxx>
22 #include <TopoDS.hxx>
23 #include <ShapeAnalysis.hxx>
24 #include <gp_Pnt.hxx>
25 #include <TColStd_ListIteratorOfListOfInteger.hxx>
26 #include <BRep_Tool.hxx>
27 #include <Precision.hxx>
28
29 //=======================================================================
30 //function : Reject
31 //purpose  : 
32 //=======================================================================
33
34 Standard_Boolean ShapeAnalysis_BoxBndTreeSelector::
35   Reject (const Bnd_Box& theBnd) const
36 {
37   Standard_Boolean fch = myFBox.IsOut(theBnd);
38   Standard_Boolean lch = myLBox.IsOut(theBnd);
39   if (fch == Standard_False || lch == Standard_False) return Standard_False;
40   return Standard_True;
41 }
42
43 //=======================================================================
44 //function : Accept
45 //purpose  : 
46 //=======================================================================
47
48 Standard_Boolean ShapeAnalysis_BoxBndTreeSelector::
49   Accept (const Standard_Integer& theObj)
50 {
51   if (theObj < 1 || theObj > mySeq->Length())
52     Standard_NoSuchObject::Raise
53       ("ShapeAnalysis_BoxBndTreeSelector::Accept : no such object for current index");
54   Standard_Boolean IsAccept = Standard_False;
55   if (myList.Contains(theObj))
56     return Standard_False;
57   enum
58   {
59     First = 1,
60     Last = 2
61   };
62    
63   TopoDS_Wire W = TopoDS::Wire (mySeq->Value (theObj));
64   TopoDS_Vertex V1,V2;                         
65   ShapeAnalysis::FindBounds (W,V1,V2);
66   if(myShared){
67     if (myLVertex.IsSame(V1)){      
68       myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
69       IsAccept = Standard_True;
70       myArrIndices(Last) = theObj;
71     }
72     else {
73       if (myLVertex.IsSame(V2)){
74         myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE2);
75         IsAccept = Standard_True;
76         myArrIndices(Last) = theObj;
77       }
78       else {
79         if (myFVertex.IsSame(V2)){
80           myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE3);
81           IsAccept = Standard_True;
82           myArrIndices(First) = theObj;
83         }
84         else {
85           if (myFVertex.IsSame(V1)){
86             myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE4);
87             IsAccept = Standard_True;
88             myArrIndices(First) = theObj;
89           }
90           else myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
91
92           
93         }
94       }
95     }
96     
97     if (IsAccept){
98       SetNb(theObj);
99       if(myArrIndices(Last))
100         myStop = Standard_True;
101       return Standard_True;
102     }
103     else myStop = Standard_False;
104   }
105   
106   else{
107     gp_Pnt p1 = BRep_Tool::Pnt(V1);
108     gp_Pnt p2 = BRep_Tool::Pnt(V2);
109     
110     Standard_Real tailhead, tailtail, headhead, headtail;
111     tailhead = p1.Distance(myLPnt);
112     tailtail = p2.Distance(myLPnt);
113     headhead = p1.Distance(myFPnt);
114     headtail = p2.Distance(myFPnt);
115     Standard_Real dm1 = tailhead, dm2 = headtail;
116     Standard_Integer res1 = 0, res2 = 0;
117     if (tailhead > tailtail) {res1 = 1; dm1 = tailtail;}
118     if (headtail > headhead) {res2 = 1; dm2 = headhead;}
119     Standard_Integer result = res1;
120     Standard_Real min3d;
121     min3d = Min (dm1, dm2);
122     if (min3d > myMin3d)
123       return Standard_False;
124
125     Standard_Integer minInd = (dm1 > dm2 ?  First : Last );
126     Standard_Integer maxInd = (dm1 > dm2 ? Last : First);
127     myArrIndices(minInd) = theObj;
128     if((min3d - myMin3d) > RealSmall())
129       myArrIndices(maxInd) = 0;
130       
131     myMin3d = min3d;
132     if (min3d > myTol)
133     {
134        myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
135        return Standard_False;
136     }
137     
138     Standard_Integer anObj = (myArrIndices(Last) ? myArrIndices(Last) : myArrIndices(First));
139     SetNb(anObj);
140     
141     if (min3d == 0 && minInd == Last)
142       myStop = Standard_True;
143    
144     if (dm1 > dm2) 
145     {
146       dm1 = dm2; 
147       result = res2 + 2;
148     }
149     if(anObj == theObj)
150     {
151       switch (result) {
152         case 0: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1); break; 
153         case 1: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE2);  break;
154         case 2: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE3);  break;
155         case 3: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE4);  break;
156       }
157     }
158       return Standard_True;
159     
160   }  
161    
162   return Standard_False;
163 }
164