0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / TopOpeBRep / TopOpeBRep_VPointInter.cxx
1 // Created on: 1993-11-10
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Adaptor2d_HCurve2d.hxx>
19 #include <Adaptor3d_HVertex.hxx>
20 #include <BRepAdaptor_Curve2d.hxx>
21 #include <BRepAdaptor_HCurve2d.hxx>
22 #include <BRepTopAdaptor_HVertex.hxx>
23 #include <gp_Pnt.hxx>
24 #include <gp_Pnt2d.hxx>
25 #include <IntPatch_Point.hxx>
26 #include <Precision.hxx>
27 #include <Standard_DomainError.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopOpeBRep_define.hxx>
34 #include <TopOpeBRep_FFTransitionTool.hxx>
35 #include <TopOpeBRep_VPointInter.hxx>
36 #include <TopOpeBRepDS_Transition.hxx>
37 #include <TopOpeBRepTool_ShapeTool.hxx>
38
39 #ifdef OCCT_DEBUG
40 static TCollection_AsciiString PRODINP("dinp ");
41 #endif
42
43 //=======================================================================
44 //function : SetPoint
45 //purpose  : 
46 //=======================================================================
47 void TopOpeBRep_VPointInter::SetPoint(const IntPatch_Point& P)
48 {
49   myPPOI = (IntPatch_Point*)&P;
50
51   Standard_Boolean isOn1 = P.IsOnDomS1(); 
52   Standard_Boolean isOn2 = P.IsOnDomS2();
53   if (isOn1 && isOn2) myShapeIndex = 3;
54   else if (isOn2)     myShapeIndex = 2;
55   else if (isOn1)     myShapeIndex = 1;
56   else                myShapeIndex = 0;
57 }
58
59 //=======================================================================
60 //function : ArcOnS1
61 //purpose  : 
62 //=======================================================================
63 const TopoDS_Shape&  TopOpeBRep_VPointInter::ArcOnS1() const
64 {
65   const Handle(Adaptor2d_HCurve2d)& HAHC2 = myPPOI->ArcOnS1();
66   const BRepAdaptor_Curve2d& BRAC2P = *((BRepAdaptor_Curve2d*)&(HAHC2->Curve2d()));
67   return BRAC2P.Edge();
68 }
69
70 //=======================================================================
71 //function : ArcOnS2
72 //purpose  : 
73 //=======================================================================
74 const TopoDS_Shape&  TopOpeBRep_VPointInter::ArcOnS2() const
75 {
76   const Handle(Adaptor2d_HCurve2d)& HAHC2 = myPPOI->ArcOnS2();
77   const BRepAdaptor_Curve2d& BRAC2P = *((BRepAdaptor_Curve2d*)&(HAHC2->Curve2d()));
78   return BRAC2P.Edge();
79 }
80
81 //=======================================================================
82 //function : VertexOnS1
83 //purpose  : 
84 //=======================================================================
85 const TopoDS_Shape& TopOpeBRep_VPointInter::VertexOnS1() const
86 {
87   if ( !myPPOI->IsVertexOnS1() )
88     throw Standard_DomainError("TopOpeBRep_VPointInter::VertexOnS1");
89
90   const Handle(BRepTopAdaptor_HVertex)* HBRTAHV = (Handle(BRepTopAdaptor_HVertex)*)&(myPPOI->VertexOnS1());
91   return (*HBRTAHV)->Vertex();
92 }
93
94 //=======================================================================
95 //function : VertexOnS2
96 //purpose  : 
97 //=======================================================================
98 const TopoDS_Shape& TopOpeBRep_VPointInter::VertexOnS2() const 
99 {
100   if ( !myPPOI->IsVertexOnS2() )
101     throw Standard_DomainError("TopOpeBRep_VPointInter::VertexOnS2");
102
103   const Handle(BRepTopAdaptor_HVertex)* HBRTAHV = (Handle(BRepTopAdaptor_HVertex)*)&(myPPOI->VertexOnS2());
104   return (*HBRTAHV)->Vertex();
105 }
106
107 //=======================================================================
108 //function : State
109 //purpose  : 
110 //=======================================================================
111 void TopOpeBRep_VPointInter::State(const TopAbs_State S,const Standard_Integer I)
112 {
113   if      (I == 1) myState1 = S;
114   else if (I == 2) myState2 = S;
115   else throw Standard_DomainError("TopOpeBRep_VPointInter::State");
116   UpdateKeep();
117 }
118
119 //=======================================================================
120 //function : State
121 //purpose  : 
122 //=======================================================================
123 TopAbs_State TopOpeBRep_VPointInter::State(const Standard_Integer I) const
124 {
125   if      (I == 1) return myState1;
126   else if (I == 2) return myState2;
127   else { throw Standard_DomainError("TopOpeBRep_VPointInter::State");}
128 }
129
130 //=======================================================================
131 //function : EdgeON
132 //purpose  : 
133 //=======================================================================
134 void TopOpeBRep_VPointInter::EdgeON(const TopoDS_Shape& Eon,const Standard_Real Par,const Standard_Integer I)
135 {
136   if      (I == 1) {
137     myEdgeON1 = Eon;
138     myEdgeONPar1 = Par;
139   }
140   else if (I == 2) {
141     myEdgeON2 = Eon;
142     myEdgeONPar2 = Par;
143   }
144 }
145
146 //=======================================================================
147 //function : EdgeON
148 //purpose  : 
149 //=======================================================================
150 const TopoDS_Shape& TopOpeBRep_VPointInter::EdgeON(const Standard_Integer I) const
151 {
152   if      (I == 1) return myEdgeON1;
153   else if (I == 2) return myEdgeON2;
154   else throw Standard_DomainError("TopOpeBRep_VPointInter::EdgeON");
155 }
156
157 //=======================================================================
158 //function : EdgeONParameter
159 //purpose  : 
160 //=======================================================================
161 Standard_Real TopOpeBRep_VPointInter::EdgeONParameter(const Standard_Integer I) const
162 {
163   if      (I == 1) return myEdgeONPar1;
164   else if (I == 2) return myEdgeONPar2;
165   else throw Standard_DomainError("TopOpeBRep_VPointInter::EdgeONParameter");
166 }
167
168 //=======================================================================
169 //function : Edge
170 //purpose  : 
171 //=======================================================================
172 const TopoDS_Shape& TopOpeBRep_VPointInter::Edge(const Standard_Integer I) const
173 {
174   if      (I == 1 && IsOnDomS1() ) return ArcOnS1(); 
175   else if (I == 2 && IsOnDomS2() ) return ArcOnS2(); 
176
177   return myNullShape;
178 }
179
180 //=======================================================================
181 //function : EdgeParameter
182 //purpose  : 
183 //=======================================================================
184 Standard_Real TopOpeBRep_VPointInter::EdgeParameter(const Standard_Integer I) const
185 {
186   if      (I == 1 && IsOnDomS1() ) return ParameterOnArc1();
187   else if (I == 2 && IsOnDomS2() ) return ParameterOnArc2();
188   return 0.;
189 }
190
191 //=======================================================================
192 //function : EdgeParameter
193 //purpose  : 
194 //=======================================================================
195 gp_Pnt2d TopOpeBRep_VPointInter::SurfaceParameters(const Standard_Integer I) const
196
197   Standard_Real u = 0., v = 0.;
198   //if      (I == 1 && IsOnDomS1() ) ParametersOnS1(u,v);
199   //else if (I == 2 && IsOnDomS1() ) ParametersOnS2(u,v);
200   if      (I == 1 ) ParametersOnS1(u,v);
201   else if (I == 2 ) ParametersOnS2(u,v);
202   gp_Pnt2d p2d(u,v);
203   return p2d;
204 }
205
206 //=======================================================================
207 //function : IsVertex
208 //purpose  : 
209 //=======================================================================
210 Standard_Boolean TopOpeBRep_VPointInter::IsVertex(const Standard_Integer I) const
211 {
212   if      ( I == 0 )                   return Standard_False;
213   if      ( I == 1 && IsVertexOnS1() ) return Standard_True;
214   else if ( I == 2 && IsVertexOnS2() ) return Standard_True;
215   return Standard_False;
216 }
217
218 //=======================================================================
219 //function : Vertex
220 //purpose  : 
221 //=======================================================================
222 const TopoDS_Shape& TopOpeBRep_VPointInter::Vertex(const Standard_Integer I) const
223 {
224   if      ( I == 1 && IsVertexOnS1() ) return VertexOnS1();
225   else if ( I == 2 && IsVertexOnS2() ) return VertexOnS2();
226   return myNullShape;
227 }
228
229 //=======================================================================
230 //function : UpdateKeep
231 //purpose  :
232 //=======================================================================
233 void TopOpeBRep_VPointInter::UpdateKeep() 
234 {
235 #define M_SINON(s) (((s) == TopAbs_IN) || ((s) == TopAbs_ON))
236   
237   TopAbs_State pos1 = State(1);
238   TopAbs_State pos2 = State(2);
239
240   Standard_Integer SI = ShapeIndex();
241
242   Standard_Boolean condition=Standard_False; 
243
244   if      (SI == 1) condition = M_SINON(pos2);
245   else if (SI == 2) condition = M_SINON(pos1);
246   else if (SI == 0) condition = M_SINON(pos1) && M_SINON(pos2);
247   else if (SI == 3) condition = M_SINON(pos1) && M_SINON(pos2);
248   // NYI : SI == 3 --> le VP devrait toujours etre Keep() (par definition)
249   
250   myKeep = condition;
251 }
252
253 //=======================================================================
254 //function : EqualpP
255 //purpose  : returns <True> if the 3d points and the parameters of the
256 //           VPoints are same.
257 //=======================================================================
258 Standard_Boolean TopOpeBRep_VPointInter::EqualpP(const TopOpeBRep_VPointInter& VP) const
259 {
260   Standard_Real p1 = ParameterOnLine();
261   Standard_Real p2 = VP.ParameterOnLine();      
262   Standard_Boolean pequal = fabs(p1-p2) < Precision::PConfusion();
263   gp_Pnt P1 = Value(); gp_Pnt P2 = VP.Value();
264   Standard_Real Ptol1 = Tolerance(), Ptol2 = VP.Tolerance();
265   Standard_Real Ptol = (Ptol1 > Ptol2) ? Ptol1 : Ptol2;
266   Standard_Boolean Pequal = P1.IsEqual(P2,Ptol);
267   Standard_Boolean pPequal = ( pequal && Pequal );
268   return pPequal;  
269 }
270
271 //=======================================================================
272 //function : 
273 //purpose  : 
274 //=======================================================================
275 Standard_Boolean TopOpeBRep_VPointInter::ParonE(const TopoDS_Edge& E,Standard_Real& par) const
276 {
277   Standard_Boolean found = Standard_False;
278   if (IsOnDomS1()) { 
279     if(E.IsSame(ArcOnS1())) found = Standard_True;
280     if (found) {par = ParameterOnArc1(); return found;}
281   }
282   if (IsOnDomS2()) { 
283     if(E.IsSame(ArcOnS2())) found = Standard_True;
284     if (found) {par = ParameterOnArc2(); return found;}
285   }
286
287   for (Standard_Integer i = 1; i <= 2; i++) {
288     if (State(i) != TopAbs_ON) continue;
289     if (EdgeON(i).IsSame(E)) {
290       par = EdgeONParameter(i); 
291       return Standard_True;
292     }
293   }
294   return found;
295 }
296
297 //=======================================================================
298 //function : DumpEdge
299 //purpose  : 
300 //=======================================================================
301 Standard_OStream& TopOpeBRep_VPointInter::Dump(const Standard_Integer I,const TopoDS_Face& F,Standard_OStream& OS) const
302 {
303   const TopoDS_Edge& E = TopoDS::Edge(Edge(I)); 
304 #ifdef OCCT_DEBUG
305   Standard_Real Epar =
306 #endif
307              EdgeParameter(I); 
308 #ifdef OCCT_DEBUG
309   TopAbs_Orientation O =
310 #endif
311            E.Orientation();
312 #ifdef OCCT_DEBUG
313   Standard_Boolean closingedge = 
314 #endif
315                     TopOpeBRepTool_ShapeTool::Closed(E,F);
316
317 #ifdef OCCT_DEBUG
318   if (closingedge) OS<<"on closing edge "; else OS<<"on edge "; TopAbs::Print(O,std::cout);
319   std::cout<<" of "<<I<<" : par : "<<Epar<<std::endl;
320   TopOpeBRep_FFTransitionTool::ProcessLineTransition(*this,I,O);
321   OS<<"line transition ";
322   if (closingedge) OS<<"on closing edge "; else OS<<"on edge "; TopAbs::Print(O,std::cout);
323   OS<<" of "<<I<<std::endl;
324 #endif
325   
326   return OS;
327 }
328
329 //=======================================================================
330 //function : Dump
331 //purpose  : 
332 //=======================================================================
333 #ifdef OCCT_DEBUG
334 Standard_OStream& TopOpeBRep_VPointInter::Dump(const TopoDS_Face& FF1,const TopoDS_Face& FF2,Standard_OStream& OS) const
335 {
336   const TopoDS_Face& F1 = TopoDS::Face(FF1);
337   const TopoDS_Face& F2 = TopoDS::Face(FF2);
338   OS<<"VP "<<myIndex<<" on "<<myShapeIndex<<" :";
339   Standard_Real Cpar = ParameterOnLine(); OS<<" on curve : "<<Cpar; 
340   if (!myKeep) OS<<" NOT kept";
341   OS<<std::endl;
342   const gp_Pnt& P = Value(); 
343   OS<<PRODINP<<"P"<<myIndex<<" "; OS<<P.X()<<" "<<P.Y()<<" "<<P.Z();
344   OS<<"; #draw"<<std::endl;
345    
346   if (IsVertexOnS1()) { OS<<"is vertex of 1"<<std::endl; }
347   if (IsVertexOnS2()) { OS<<"is vertex of 2"<<std::endl; }
348   if (IsMultiple())   { OS<<"is multiple"<<std::endl; }
349   if (IsInternal())   { OS<<"is internal"<<std::endl; }
350
351   if      (myShapeIndex == 1) { 
352     Dump(1,F1,OS); 
353   }
354   else if (myShapeIndex == 2) { 
355     Dump(2,F2,OS); 
356   }
357   else if (myShapeIndex == 3) { 
358     Dump(1,F1,OS);
359     Dump(2,F2,OS); 
360   }
361 #else
362 Standard_OStream& TopOpeBRep_VPointInter::Dump(const TopoDS_Face&,const TopoDS_Face&,Standard_OStream& OS) const
363 {
364 #endif
365
366   return OS;
367 }
368
369 //=======================================================================
370 //function : PThePointOfIntersectionDummy
371 //purpose  : 
372 //=======================================================================
373 TopOpeBRep_PThePointOfIntersection TopOpeBRep_VPointInter::PThePointOfIntersectionDummy() const
374 {
375   return myPPOI;
376 }