Adding of testing cases from subgroups 937 940 and 941 of CHL group
[occt.git] / src / BOP / BOP_SolidClassifier.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 // NIZNHY-PKV Thu Apr 11 10:42:14 2002
19
20 #include <BOP_SolidClassifier.ixx>
21
22 #include <BRepClass3d_SolidClassifier.hxx>
23
24 //=======================================================================
25 //function : 
26 //purpose  : 
27 //=======================================================================
28 BOP_SolidClassifier::BOP_SolidClassifier()
29 {
30   Clear();
31 }
32
33 //=======================================================================
34 //function : Clear
35 //purpose  : 
36 //=======================================================================
37   void BOP_SolidClassifier::Clear() 
38 {
39   myPClassifier = NULL;
40   myClassifierMap.Clear();
41   myState = TopAbs_UNKNOWN;
42   myShell.Nullify();
43   mySolid.Nullify();
44 }
45
46 //=======================================================================
47 //function : LoadSolid
48 //purpose  : 
49 //=======================================================================
50   void BOP_SolidClassifier::LoadSolid(const TopoDS_Solid& SOL) 
51 {
52   Standard_Boolean found;
53
54   found = myClassifierMap.Contains(SOL);
55   if ( !found ) {
56     myPClassifier = new BRepClass3d_SolidClassifier(SOL);
57     myClassifierMap.Add(SOL, myPClassifier);
58   }
59   else {
60     myPClassifier = myClassifierMap.ChangeFromKey(SOL);
61   }
62 }
63
64 //=======================================================================
65 //function : Classify
66 //purpose  : 
67 //=======================================================================
68   TopAbs_State BOP_SolidClassifier::Classify (const TopoDS_Solid& SOL, 
69                                               const gp_Pnt& P, 
70                                               const Standard_Real Tol)
71 {
72   myPClassifier = NULL;
73   myState = TopAbs_UNKNOWN;
74
75   LoadSolid(SOL);
76
77   if (myPClassifier == NULL) {
78     return myState;
79   }  
80
81   myPClassifier->Perform(P,Tol);
82   myState = myPClassifier->State();
83   const TopoDS_Shape& fres = myPClassifier->Face();
84   if (fres.IsNull()) {
85     // NYI : in case of removal of EXTERNAL and INTERNAL faces by the
86     // classifier BRepClass3d_SolidClassifier, process these faces
87     // to generate state ON/Solid when the point is IN/face INTERNAL or EXTERNAL 
88     return myState;
89   }
90   
91   TopAbs_Orientation ofres;
92   
93   ofres = fres.Orientation();
94
95   if      ( ofres == TopAbs_EXTERNAL ) {
96     if      ( myState == TopAbs_IN ) {
97       myState = TopAbs_OUT;
98     }    
99     else if ( myState == TopAbs_OUT ){
100       myState = TopAbs_OUT;
101     }    
102     else if ( myState == TopAbs_ON ){
103       myState = TopAbs_ON;
104     }    
105     else if ( myState == TopAbs_UNKNOWN ){
106       myState = TopAbs_OUT;
107     }
108   }
109
110   else if ( ofres == TopAbs_INTERNAL ) {
111     if      ( myState == TopAbs_IN ) {
112       myState = TopAbs_IN;
113     }
114     else if ( myState == TopAbs_OUT) {
115       myState = TopAbs_IN;
116     }
117     else if ( myState == TopAbs_ON ) {
118       myState = TopAbs_ON;
119     }    
120     else if ( myState == TopAbs_UNKNOWN ) {
121       myState = TopAbs_IN;
122     }
123   }
124   return myState;
125 }
126
127
128 //=======================================================================
129 //function : LoadShell
130 //purpose  : 
131 //=======================================================================
132   void BOP_SolidClassifier::LoadShell(const TopoDS_Shell& SHE) 
133 {
134   Standard_Boolean found;
135
136   found = myClassifierMap.Contains (SHE);
137   
138   if ( !found ) {
139     myBuilder.MakeSolid(mySolid);
140     myBuilder.Add(mySolid,SHE);
141     TopoDS_Shell* pshe = (TopoDS_Shell*)&SHE; 
142     (*pshe).Free(Standard_True);  
143     
144     myPClassifier = new BRepClass3d_SolidClassifier(mySolid);
145     myClassifierMap.Add(SHE, myPClassifier);
146   }
147   else {
148     myPClassifier = myClassifierMap.ChangeFromKey(SHE);
149   }
150 }
151
152 //=======================================================================
153 //function : Classify
154 //purpose  : 
155 //=======================================================================
156   TopAbs_State BOP_SolidClassifier::Classify (const TopoDS_Shell& SHE, 
157                                               const gp_Pnt& P,
158                                               const Standard_Real Tol)
159 {
160   myPClassifier = NULL;
161   myState = TopAbs_UNKNOWN;
162
163   LoadShell(SHE);
164   //
165   if (myPClassifier == NULL) {
166     return myState;
167   }
168   
169   myPClassifier->Perform(P,Tol);
170   myState = myPClassifier->State();
171   return myState;
172 }
173
174 //=======================================================================
175 //function : State
176 //purpose  : 
177 //=======================================================================
178   TopAbs_State BOP_SolidClassifier::State() const
179 {
180   return myState;
181 }
182
183 //=======================================================================
184 //function : Destroy
185 //purpose  : 
186 //=======================================================================
187   void BOP_SolidClassifier::Destroy()
188 {
189   Standard_Integer i, aNb;
190   
191   aNb=myClassifierMap.Extent();
192   for (i=1; i<=aNb; ++i) {
193     BRepClass3d_SolidClassifier* pC=myClassifierMap(i);
194     delete pC;
195   }
196   myClassifierMap.Clear();
197 }