0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[occt.git] / src / IntCurvesFace / IntCurvesFace_Intersector.cxx
CommitLineData
b311480e 1// Created on: 1996-06-03
2// Created by: Laurent BUCHARD
3// Copyright (c) 1996-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#define OPTIMISATION 1
18
19
20#include <IntCurvesFace_Intersector.ixx>
21
22#include <IntCurveSurface_ThePolyhedronToolOfHInter.hxx>
23#include <Bnd_BoundSortBox.hxx>
24
25#include <IntCurveSurface_IntersectionPoint.hxx>
26#include <gp_Lin.hxx>
27#include <TopoDS_Face.hxx>
28#include <TopAbs.hxx>
29
30
31#include <IntCurveSurface_HInter.hxx>
32#include <BRepAdaptor_HSurface.hxx>
33#include <Geom_Line.hxx>
34#include <gp_Pnt2d.hxx>
35#include <BRepClass_FaceClassifier.hxx>
36
37#include <GeomAdaptor_Curve.hxx>
38
39#include <GeomAdaptor_HCurve.hxx>
40#include <BRepAdaptor_HSurface.hxx>
41
42
43
44#include <Adaptor3d_HSurfaceTool.hxx>
45#include <IntCurveSurface_TheHCurveTool.hxx>
46#include <Adaptor3d_HCurve.hxx>
47#include <Bnd_Box.hxx>
48#include <Intf_Tool.hxx>
49#include <IntCurveSurface_ThePolyhedronOfHInter.hxx>
50#include <IntCurveSurface_ThePolygonOfHInter.hxx>
51#include <IntCurveSurface_SequenceOfPnt.hxx>
52
53
54
7d9b843c 55//=======================================================================
56//function : SurfaceType
57//purpose :
58//=======================================================================
59GeomAbs_SurfaceType IntCurvesFace_Intersector::SurfaceType() const
60{
7fd59977 61 return(Adaptor3d_HSurfaceTool::GetType(Hsurface));
62}
7d9b843c 63//=======================================================================
64//function : IntCurvesFace_Intersector
65//purpose :
66//=======================================================================
7fd59977 67IntCurvesFace_Intersector::IntCurvesFace_Intersector(const TopoDS_Face& Face,
68 const Standard_Real aTol)
69:
7d9b843c 70 Tol(aTol),
71 done(Standard_False),
72 nbpnt(0),
73 PtrOnPolyhedron(NULL),
74 PtrOnBndBounding(NULL)
7fd59977 75{
76 BRepAdaptor_Surface surface;
77 face = Face;
78 surface.Initialize(Face,Standard_True);
79 Hsurface = new BRepAdaptor_HSurface(surface);
80 myTopolTool = new BRepTopAdaptor_TopolTool(Hsurface);
81
82 GeomAbs_SurfaceType SurfaceType = Adaptor3d_HSurfaceTool::GetType(Hsurface);
83 if( (SurfaceType != GeomAbs_Plane)
84 && (SurfaceType != GeomAbs_Cylinder)
85 && (SurfaceType != GeomAbs_Cone)
86 && (SurfaceType != GeomAbs_Sphere)
87 && (SurfaceType != GeomAbs_Torus)) {
88 Standard_Integer nbsu,nbsv;
89 Standard_Real U0,V0,U1,V1;
90 U0 = Hsurface->FirstUParameter();
91 U1 = Hsurface->LastUParameter();
92 V0 = Hsurface->FirstVParameter();
93 V1 = Hsurface->LastVParameter();
7d9b843c 94 //modified by NIZNHY-PKV Fri Apr 06 07:30:47 2012f
95 Standard_Boolean bFlag;
96 //
97 {
7eb732b6 98 Standard_Real dU, dV, dA, dB, aTresh;
7d9b843c 99 bFlag=Standard_True;
100 //
101 aTresh=100.;
102 dU=U1-U0;
103 dV=V1-V0;
104 dA=dU;
105 dB=dV;
106 if (dV>dU) {
107 dA=dV;
108 dB=dU;
109 }
110 //
7eb732b6 111 if (dB < Precision::PConfusion() || dA > dB * aTresh) {
7d9b843c 112 bFlag=!bFlag;
113 }
7d9b843c 114 }
115 //
116 if (bFlag) {
117 nbsu = myTopolTool->NbSamplesU();
118 nbsv = myTopolTool->NbSamplesV();
119 if(nbsu>40) nbsu = 40;
120 if(nbsv>40) nbsv = 40;
121 PtrOnPolyhedron = (IntCurveSurface_ThePolyhedronOfHInter *)
122 new IntCurveSurface_ThePolyhedronOfHInter(Hsurface,nbsu,nbsv,U0,V0,U1,V1);
123 }
124 //
125 /*
7fd59977 126 nbsu = myTopolTool->NbSamplesU();
127 nbsv = myTopolTool->NbSamplesV();
128 if(nbsu>40) nbsu = 40;
129 if(nbsv>40) nbsv = 40;
7d9b843c 130 PtrOnPolyhedron = (IntCurveSurface_ThePolyhedronOfHInter *)
131 new IntCurveSurface_ThePolyhedronOfHInter(Hsurface,nbsu,nbsv,U0,V0,U1,V1);
132 */
133
134 //modified by NIZNHY-PKV Fri Apr 06 07:30:49 2012t
7fd59977 135 }
136}
7d9b843c 137//=======================================================================
138//function : InternalCall
139//purpose :
140//=======================================================================
7fd59977 141void IntCurvesFace_Intersector::InternalCall(const IntCurveSurface_HInter &HICS,
7d9b843c 142 const Standard_Real parinf,
143 const Standard_Real parsup)
144{
7fd59977 145 if(HICS.IsDone()) {
146 for(Standard_Integer index=HICS.NbPoints(); index>=1; index--) {
147 const IntCurveSurface_IntersectionPoint& HICSPointindex = HICS.Point(index);
148 gp_Pnt2d Puv(HICSPointindex.U(),HICSPointindex.V());
149
150 TopAbs_State currentstate = myTopolTool->Classify(Puv,Tol);
151 if(currentstate==TopAbs_IN || currentstate==TopAbs_ON) {
152 Standard_Real HICSW = HICSPointindex.W();
153 if(HICSW >= parinf && HICSW <= parsup ) {
154 Standard_Real U = HICSPointindex.U();
155 Standard_Real V = HICSPointindex.V();
156 Standard_Real W = HICSW;
157 IntCurveSurface_TransitionOnCurve transition = HICSPointindex.Transition();
158 gp_Pnt pnt = HICSPointindex.Pnt();
159 // state = currentstate;
160 // Modified by skv - Wed Sep 3 16:14:10 2003 OCC578 Begin
161 Standard_Integer anIntState = (currentstate == TopAbs_IN) ? 0 : 1;
162 // Modified by skv - Wed Sep 3 16:14:11 2003 OCC578 End
163
164 if(transition != IntCurveSurface_Tangent && face.Orientation()==TopAbs_REVERSED) {
165 if(transition == IntCurveSurface_In)
166 transition = IntCurveSurface_Out;
167 else
168 transition = IntCurveSurface_In;
169 }
170 //----- Insertion du point
171 if(nbpnt==0) {
172 IntCurveSurface_IntersectionPoint PPP(pnt,U,V,W,transition);
173 SeqPnt.Append(PPP);
174 // Modified by skv - Wed Sep 3 16:14:10 2003 OCC578 Begin
175 mySeqState.Append(anIntState);
176 // Modified by skv - Wed Sep 3 16:14:11 2003 OCC578 End
177 }
178 else {
179 Standard_Integer i = 1;
180 Standard_Integer b = nbpnt+1;
181 while(i<=nbpnt) {
182 const IntCurveSurface_IntersectionPoint& Pnti=SeqPnt.Value(i);
183 Standard_Real wi = Pnti.W();
184 if(wi >= W) { b=i; i=nbpnt; }
185 i++;
186 }
187 IntCurveSurface_IntersectionPoint PPP(pnt,U,V,W,transition);
188 // Modified by skv - Wed Sep 3 16:14:10 2003 OCC578 Begin
189// if(b>nbpnt) { SeqPnt.Append(PPP); }
190// else if(b>0) { SeqPnt.InsertBefore(b,PPP); }
191 if(b>nbpnt) {
192 SeqPnt.Append(PPP);
193 mySeqState.Append(anIntState);
194 } else if(b>0) {
195 SeqPnt.InsertBefore(b,PPP);
196 mySeqState.InsertBefore(b, anIntState);
197 }
198 // Modified by skv - Wed Sep 3 16:14:11 2003 OCC578 End
199 }
200
201
202 nbpnt++;
203 }
204 } //-- classifier state is IN or ON
205 } //-- Loop on Intersection points.
206 } //-- HICS.IsDone()
207}
7d9b843c 208//=======================================================================
209//function : Perform
210//purpose :
211//=======================================================================
212void IntCurvesFace_Intersector::Perform(const gp_Lin& L,
213 const Standard_Real ParMin,
214 const Standard_Real ParMax)
215{
7fd59977 216 done = Standard_True;
217 SeqPnt.Clear();
7fd59977 218 mySeqState.Clear();
7fd59977 219 nbpnt = 0;
7fd59977 220
7d9b843c 221 IntCurveSurface_HInter HICS;
7fd59977 222 Handle(Geom_Line) geomline = new Geom_Line(L);
223 GeomAdaptor_Curve LL(geomline);
7fd59977 224 Handle(GeomAdaptor_HCurve) HLL = new GeomAdaptor_HCurve(LL);
7fd59977 225 Standard_Real parinf=ParMin;
226 Standard_Real parsup=ParMax;
7d9b843c 227 //
7fd59977 228 if(PtrOnPolyhedron == NULL) {
229 HICS.Perform(HLL,Hsurface);
230 }
231 else {
7d9b843c 232 Intf_Tool bndTool;
233 Bnd_Box boxLine;
234 bndTool.LinBox
235 (L,
236 ((IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron)->Bounding(),
237 boxLine);
7fd59977 238 if(bndTool.NbSegments() == 0)
239 return;
240 for(Standard_Integer nbseg=1; nbseg<= bndTool.NbSegments(); nbseg++) {
241 Standard_Real pinf = bndTool.BeginParam(nbseg);
242 Standard_Real psup = bndTool.EndParam(nbseg);
243 Standard_Real pppp = 0.05*(psup-pinf);
244 pinf-=pppp;
245 psup+=pppp;
246 if((psup - pinf)<1e-10) { pinf-=1e-10; psup+=1e-10; }
247 if(nbseg==1) { parinf=pinf; parsup=psup; }
248 else {
249 if(parinf>pinf) parinf = pinf;
250 if(parsup<psup) parsup = psup;
251 }
252 }
253 if(parinf>ParMax) { return; }
254 if(parsup<ParMin) { return; }
255 if(parinf<ParMin) parinf=ParMin;
256 if(parsup>ParMax) parsup=ParMax;
257 if(parinf>(parsup-1e-9)) return;
258 IntCurveSurface_ThePolygonOfHInter polygon(HLL,
259 parinf,
260 parsup,
261 2);
262#if OPTIMISATION
263 if(PtrOnBndBounding==NULL) {
264 PtrOnBndBounding = (Bnd_BoundSortBox *) new Bnd_BoundSortBox();
7d9b843c 265 IntCurveSurface_ThePolyhedronOfHInter *thePolyh=
266 (IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron;
267 ((Bnd_BoundSortBox *)(PtrOnBndBounding))->
268 Initialize(IntCurveSurface_ThePolyhedronToolOfHInter::Bounding(*thePolyh),
269 IntCurveSurface_ThePolyhedronToolOfHInter::ComponentsBounding(*thePolyh));
7fd59977 270 }
271 HICS.Perform(HLL,
272 polygon,
273 Hsurface,
274 *((IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron),
275 *((Bnd_BoundSortBox *)PtrOnBndBounding));
276#else
277 HICS.Perform(HLL,
278 polygon,
279 Hsurface,
280 *((IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron));
281#endif
282 }
283
284 InternalCall(HICS,parinf,parsup);
7fd59977 285}
7d9b843c 286//=======================================================================
287//function : Perform
288//purpose :
289//=======================================================================
290void IntCurvesFace_Intersector::Perform(const Handle(Adaptor3d_HCurve)& HCu,
291 const Standard_Real ParMin,
292 const Standard_Real ParMax)
293{
7fd59977 294 done = Standard_True;
295 SeqPnt.Clear();
296 // Modified by skv - Wed Sep 3 16:14:10 2003 OCC578 Begin
297 mySeqState.Clear();
298 // Modified by skv - Wed Sep 3 16:14:11 2003 OCC578 End
299 nbpnt = 0;
300 IntCurveSurface_HInter HICS;
301
302 //--
303 Standard_Real parinf=ParMin;
304 Standard_Real parsup=ParMax;
305
306 if(PtrOnPolyhedron == NULL) {
307 HICS.Perform(HCu,Hsurface);
308 }
309 else {
310 parinf = IntCurveSurface_TheHCurveTool::FirstParameter(HCu);
311 parsup = IntCurveSurface_TheHCurveTool::LastParameter(HCu);
312 if(parinf<ParMin) parinf = ParMin;
313 if(parsup>ParMax) parsup = ParMax;
314 if(parinf>(parsup-1e-9)) return;
315 Standard_Integer nbs;
316 nbs = IntCurveSurface_TheHCurveTool::NbSamples(HCu,parinf,parsup);
317
318 IntCurveSurface_ThePolygonOfHInter polygon(HCu,
319 parinf,
320 parsup,
321 nbs);
322#if OPTIMISATION
323 if(PtrOnBndBounding==NULL) {
324 PtrOnBndBounding = (Bnd_BoundSortBox *) new Bnd_BoundSortBox();
325 IntCurveSurface_ThePolyhedronOfHInter *thePolyh=(IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron;
326 ((Bnd_BoundSortBox *)(PtrOnBndBounding))->Initialize(IntCurveSurface_ThePolyhedronToolOfHInter::Bounding(*thePolyh),
327 IntCurveSurface_ThePolyhedronToolOfHInter::ComponentsBounding(*thePolyh));
328 }
329 HICS.Perform(HCu,
330 polygon,
331 Hsurface,
332 *((IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron),
333 *((Bnd_BoundSortBox *)PtrOnBndBounding));
334#else
335 HICS.Perform(HCu,
336 polygon,
337 Hsurface,
338 *((IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron));
339#endif
340 }
341 InternalCall(HICS,parinf,parsup);
342}
343
344//============================================================================
345Bnd_Box IntCurvesFace_Intersector::Bounding() const {
346 if(PtrOnPolyhedron !=NULL) {
347 return(((IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron)->Bounding());
348 }
349 else {
350 Bnd_Box B;
351 return(B);
352 }
353}
354TopAbs_State IntCurvesFace_Intersector::ClassifyUVPoint(const gp_Pnt2d& Puv) const {
355 TopAbs_State state = myTopolTool->Classify(Puv,1e-7);
356 return(state);
357}
358//============================================================================
359void IntCurvesFace_Intersector::Destroy() {
360 if(PtrOnPolyhedron !=NULL) {
361 delete (IntCurveSurface_ThePolyhedronOfHInter *)PtrOnPolyhedron;
362 PtrOnPolyhedron = NULL;
363 }
364 if(PtrOnBndBounding !=NULL) {
365 delete (Bnd_BoundSortBox *)PtrOnBndBounding;
366 PtrOnBndBounding=NULL;
367 }
368}
369
370
371