Adding of testing cases from subgroups 937 940 and 941 of CHL group
[occt.git] / src / BOPTools / BOPTools_StateFiller.cxx
1 // Created on: 2002-02-04
2 // Created by: Peter KURNEV
3 // Copyright (c) 2002-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 //  Modified by skv - Tue Aug 24 12:31:16 2004 OCC6450
22
23 #include <BOPTools_StateFiller.ixx>
24
25 #include <Precision.hxx>
26
27 #include <Geom_Curve.hxx>
28 #include <gp_Pnt.hxx>
29
30 #include <BRep_Tool.hxx>
31 #include <BRepClass3d_SolidClassifier.hxx>
32
33 #include <TopoDS.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS_Solid.hxx>
36
37 #include <TopAbs_State.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TopExp.hxx>
40
41 #include <BooleanOperations_StateOfShape.hxx>
42
43 #include <IntTools_Tools.hxx>
44 #include <IntTools_Context.hxx>
45 #include <Geom_Surface.hxx>
46
47 //=======================================================================
48 // function:  BOPTools_StateFiller::BOPTools_StateFiller
49 // purpose: 
50 //=======================================================================
51 BOPTools_StateFiller::BOPTools_StateFiller(const BOPTools_PaveFiller& aFiller)
52 :
53   myIsDone(Standard_False)
54 {
55   myFiller=(BOPTools_PaveFiller*) &aFiller;
56   myDS=myFiller->DS();
57   myIntrPool=myFiller->InterfPool();
58 }
59 //=======================================================================
60 // function: Do 
61 // purpose: 
62 //=======================================================================
63   void BOPTools_StateFiller::Do()
64 {
65 }
66 //=======================================================================
67 // function:  IsDone
68 // purpose: 
69 //=======================================================================
70   Standard_Boolean BOPTools_StateFiller::IsDone() const
71 {
72   return myIsDone;
73 }
74
75 //=======================================================================
76 // function:  ConvertState
77 // purpose: 
78 //=======================================================================
79   BooleanOperations_StateOfShape BOPTools_StateFiller::ConvertState(const TopAbs_State aSt)
80 {
81   BooleanOperations_StateOfShape aState;
82   switch (aSt) {
83     case TopAbs_IN:
84       aState=BooleanOperations_IN;
85       break;
86     case TopAbs_OUT:
87       aState=BooleanOperations_OUT;
88       break;  
89     case TopAbs_ON:
90       aState=BooleanOperations_ON;
91       break;  
92     case TopAbs_UNKNOWN:
93       aState=BooleanOperations_UNKNOWN;
94       break;  
95     default:
96       aState=BooleanOperations_UNKNOWN;
97       break;  
98   }
99   return aState;
100 }
101
102 //=======================================================================
103 // function:  ConvertState
104 // purpose: 
105 //=======================================================================
106   TopAbs_State BOPTools_StateFiller::ConvertState(const BooleanOperations_StateOfShape aSt)
107 {
108   TopAbs_State aState;
109   
110   switch (aSt) {
111     case BooleanOperations_IN:
112       aState=TopAbs_IN;
113       break;
114     case BooleanOperations_OUT:
115       aState=TopAbs_OUT;
116       break;  
117     case BooleanOperations_ON:
118       aState=TopAbs_ON;
119       break;  
120     case BooleanOperations_UNKNOWN:
121       aState=TopAbs_UNKNOWN;
122       break;  
123     default:
124       aState=TopAbs_UNKNOWN;
125       break;  
126   }
127   return aState;
128 }
129                                             
130 //=======================================================================
131 // function:  ClassifyShapeByRef
132 // purpose: 
133 //=======================================================================
134   BooleanOperations_StateOfShape BOPTools_StateFiller::ClassifyShapeByRef (const TopoDS_Shape& aS,
135                                                                            const TopoDS_Shape& aRef)
136 {
137   TopAbs_ShapeEnum aType;
138   aType=aS.ShapeType();
139
140 //  Modified by skv - Tue Aug 24 12:31:16 2004 OCC6450 Begin
141   Standard_Boolean hasEdge = Standard_True;
142 //  Modified by skv - Tue Aug 24 12:31:17 2004 OCC6450 End
143   TopoDS_Edge aE;
144   if (aType!=TopAbs_EDGE) {
145     TopTools_IndexedMapOfShape aME;
146     TopExp::MapShapes(aS, TopAbs_EDGE, aME);
147
148 //  Modified by skv - Tue Aug 24 12:31:16 2004 OCC6450 Begin
149     if (aME.Extent() == 0)
150       hasEdge = Standard_False;
151     else
152 //  Modified by skv - Tue Aug 24 12:31:16 2004 OCC6450 End
153       aE=TopoDS::Edge(aME(1));
154   }
155   else {
156     aE=TopoDS::Edge(aS);
157   }
158   
159 //  Modified by skv - Tue Aug 24 12:31:16 2004 OCC6450 Begin
160 //   TopAbs_State aSt=ClassifyEdgeToSolidByOnePoint(aE, aRef);
161   TopAbs_State aSt;
162
163   if (hasEdge) {
164     aSt = ClassifyEdgeToSolidByOnePoint(aE, aRef);
165   } else {
166     TopTools_IndexedMapOfShape aMF;
167     Standard_Boolean           hasFace = Standard_True;
168     TopoDS_Face                aF;
169
170     TopExp::MapShapes(aS, TopAbs_FACE, aMF);
171
172 //  Modified by skv - Tue Aug 24 12:31:16 2004 OCC6450 Begin
173     if (aMF.Extent() == 0) {
174       hasFace = Standard_False;
175     } else {
176       aF = TopoDS::Face(aMF(1));
177     }
178
179     if (!hasFace) {
180       aSt = TopAbs_UNKNOWN;
181     } else {
182       Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aF);
183       Standard_Real        aUMin;
184       Standard_Real        aUMax;
185       Standard_Real        aVMin;
186       Standard_Real        aVMax;
187
188       aSurf->Bounds(aUMin, aUMax, aVMin, aVMax);
189
190       Standard_Boolean isMinInf = Precision::IsNegativeInfinite(aUMin);
191       Standard_Boolean isMaxInf = Precision::IsPositiveInfinite(aUMax);
192       Standard_Real    dT=10.;
193       Standard_Real    aParU;
194       Standard_Real    aParV;
195
196       if (isMinInf && !isMaxInf) {
197         aParU = aUMax - dT;
198       } else if (!isMinInf && isMaxInf) {
199         aParU = aUMin + dT;
200       } else if (isMinInf && isMaxInf) {
201         aParU = 0.;
202       } else {
203         aParU = IntTools_Tools::IntermediatePoint(aUMin, aUMax);
204       }
205
206       isMinInf = Precision::IsNegativeInfinite(aVMin);
207       isMaxInf = Precision::IsPositiveInfinite(aVMax);
208
209       if (isMinInf && !isMaxInf) {
210         aParV = aVMax - dT;
211       } else if (!isMinInf && isMaxInf) {
212         aParV = aVMin + dT;
213       } else if (isMinInf && isMaxInf) {
214         aParV = 0.;
215       } else {
216         aParV = IntTools_Tools::IntermediatePoint(aVMin, aVMax);
217       }
218
219       gp_Pnt aP3d = aSurf->Value(aParU, aParV);
220
221       const TopoDS_Solid          &aRefSolid = TopoDS::Solid(aRef);
222       const Handle(IntTools_Context)& aContext  = myFiller->Context();
223       BRepClass3d_SolidClassifier &aSC = 
224         aContext->SolidClassifier(aRefSolid);
225       //
226       aSC.Perform(aP3d, 1e-7);
227       //
228       aSt = aSC.State();
229     }
230   }
231 //  Modified by skv - Tue Aug 24 12:31:16 2004 OCC6450 Begin
232
233   BooleanOperations_StateOfShape aState=BOPTools_StateFiller::ConvertState(aSt) ;
234   
235   return aState;
236 }
237
238
239 //=======================================================================
240 // function:  ClassifyEdgeToSolidByOnePoint
241 // purpose: 
242 //=======================================================================
243   TopAbs_State  BOPTools_StateFiller::ClassifyEdgeToSolidByOnePoint(const TopoDS_Edge& E,
244                                                                     const TopoDS_Shape& Ref)
245 {
246   Standard_Real f2 = 0., l2 = 0., par = 0.;
247   
248   Handle(Geom_Curve) C3D = BRep_Tool::Curve(E, f2, l2);
249   gp_Pnt aP3d;
250         
251   if(C3D.IsNull()) {
252     //it means that we are in degenerated edge
253     const TopoDS_Vertex& fv = TopExp::FirstVertex(E);
254     if(fv.IsNull()){
255       return TopAbs_UNKNOWN;
256     }
257     aP3d = BRep_Tool::Pnt(fv);
258   }
259   else {//usual case
260     Standard_Boolean bF2Inf, bL2Inf;
261     Standard_Real dT=10.;
262     //
263     bF2Inf = Precision::IsNegativeInfinite(f2);
264     bL2Inf = Precision::IsPositiveInfinite(l2);
265     //
266     if (bF2Inf && !bL2Inf) {
267       par=l2-dT;
268     }
269     else if (!bF2Inf && bL2Inf) {
270       par=f2+dT;
271     }
272     else if (bF2Inf && bL2Inf) {
273       par=0.;
274     }
275     else {
276       par=IntTools_Tools::IntermediatePoint(f2, l2);
277     }
278     C3D -> D0(par, aP3d);
279   }
280   //
281   const TopoDS_Solid& aRefSolid=TopoDS::Solid(Ref);
282   const Handle(IntTools_Context)& aContext=myFiller->Context();
283   BRepClass3d_SolidClassifier& aSC=aContext->SolidClassifier(aRefSolid);
284   //
285   aSC.Perform(aP3d, 1e-7);
286   //
287   TopAbs_State aState=aSC.State();
288   
289   return aState;
290 }
291 //=======================================================================
292 // function:  SubType
293 // purpose: 
294 //=======================================================================
295   TopAbs_ShapeEnum BOPTools_StateFiller::SubType(const TopoDS_Shape& aS)
296 {
297   TopAbs_ShapeEnum aSourceType, aReturnType;
298   aSourceType=aS.ShapeType();
299
300   switch (aSourceType) {
301     case TopAbs_SOLID:
302       aReturnType=TopAbs_SHELL;
303       break;
304     case TopAbs_SHELL:
305       aReturnType=TopAbs_FACE;
306       break;
307     case TopAbs_FACE:
308       aReturnType=TopAbs_WIRE;
309       break;  
310     case TopAbs_WIRE:
311       aReturnType=TopAbs_EDGE;
312       break;   
313     case TopAbs_EDGE:
314       aReturnType=TopAbs_VERTEX;
315       break;     
316     default:
317       aReturnType=TopAbs_SHAPE;
318       break;
319   }
320   return aReturnType;
321 }