0026621: Boolean Cut does not work on two solids
[occt.git] / src / IntPatch / IntPatch_PrmPrmIntersection.cxx
1 // Created on: 1993-02-02
2 // Created by: Laurent BUCHARD
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 // modified by Edward AGAPOV (eap) Tue Jan 22 12:29:55 2002
18 // modified by Oleg FEDYAED  (ofv) Fri Nov 29 16:08:02 2002
19
20 #include <Adaptor3d_HSurface.hxx>
21 #include <Adaptor3d_TopolTool.hxx>
22 #include <gp_Dir.hxx>
23 #include <gp_Pnt.hxx>
24 #include <Intf_PIType.hxx>
25 #include <Intf_SectionLine.hxx>
26 #include <Intf_SectionPoint.hxx>
27 #include <Intf_TangentZone.hxx>
28 #include <IntPatch_InterferencePolyhedron.hxx>
29 #include <IntPatch_Line.hxx>
30 #include <IntPatch_Polyhedron.hxx>
31 #include <IntPatch_PrmPrmIntersection.hxx>
32 #include <IntPatch_PrmPrmIntersection_T3Bits.hxx>
33 #include <IntPatch_RstInt.hxx>
34 #include <IntPatch_WLine.hxx>
35 #include <IntPolyh_Intersection.hxx>
36 #include <IntSurf_LineOn2S.hxx>
37 #include <IntSurf_ListIteratorOfListOfPntOn2S.hxx>
38 #include <IntSurf_PntOn2S.hxx>
39 #include <IntWalk_PWalking.hxx>
40 #include <Standard_OutOfRange.hxx>
41 #include <StdFail_NotDone.hxx>
42 #include <TColStd_Array1OfReal.hxx>
43 #include <TColStd_HArray1OfReal.hxx>
44 #include <TColStd_SequenceOfInteger.hxx>
45
46 static void SectionPointToParameters(const Intf_SectionPoint& Sp,
47                                      const IntPatch_Polyhedron& Surf1,
48                                      const IntPatch_Polyhedron& Surf2,
49                                      Standard_Real& u1,
50                                      Standard_Real& v1,
51                                      Standard_Real& u2,
52                                      Standard_Real& v2);
53
54
55 static 
56 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
57                       const Handle(Adaptor3d_HSurface)& Surf2,
58                       IntPatch_SequenceOfLine& aSLin);
59
60 static
61 IntSurf_PntOn2S MakeNewPoint(const IntSurf_PntOn2S& replacePnt,
62                              const IntSurf_PntOn2S& oldPnt,
63                              const Standard_Real* Periods);
64
65 static Standard_Boolean IsPointOnLine(const IntSurf_PntOn2S        &thePOn2S,
66                                       const Handle(IntPatch_WLine) &theWLine,
67                                       const Standard_Real           Deflection);
68
69 static void AddWLine(IntPatch_SequenceOfLine      &theLines,
70                      const Handle(IntPatch_WLine) &theWLine,
71                      const Standard_Real           Deflection);
72
73 static void SeveralWlinesProcessing(const Handle(Adaptor3d_HSurface)& theSurf1,
74                                     const Handle(Adaptor3d_HSurface)& theSurf2,
75                                     const IntPatch_SequenceOfLine& theSLin,
76                                     const Standard_Real* const thePeriodsArr,
77                                     const IntSurf_TypeTrans theTrans1,
78                                     const IntSurf_TypeTrans theTrans2,
79                                     const Standard_Real theTol,
80                                     Handle(IntPatch_WLine)& theWLline)
81 {
82   if(theSLin.Length() == 0)
83     return;
84
85   Standard_Real aU1 = 0.0, aV1 = 0.0, aU2 = 0.0, aV2 = 0.0;
86
87   const Standard_Real aTol2D = 1.e-4;
88   Standard_Integer cnbV = theWLline->NbVertex();
89   Standard_Integer ciV;
90   for( ciV = 1; ciV <= cnbV; ciV++ )
91   {
92     Standard_Real pntDMin = 1.e+100;
93     Standard_Integer VDMin = 0;
94     Standard_Integer WLDMin = 0;
95     gp_Pnt cPV = theWLline->Vertex(ciV).Value();
96     theWLline->Vertex(ciV).Parameters(aU1, aV1, aU2, aV2);
97     const gp_Pnt2d aPCS1(aU1, aV1), aPCS2(aU2, aV2);
98     Standard_Integer iL;
99     for( iL = 1; iL <= theSLin.Length(); iL++)
100     {
101       const Handle(IntPatch_Line)& aSLine = theSLin.Value(iL);
102       IntPatch_IType aType = aSLine->ArcType();
103       if( aType != IntPatch_Walking)
104         continue;
105       const Handle(IntPatch_WLine) aWLine = Handle(IntPatch_WLine)::DownCast(aSLine);
106       Standard_Integer tnbV = aWLine->NbVertex();
107       Standard_Integer tiV;
108       for( tiV = 1; tiV <= tnbV; tiV++ )
109       {
110         gp_Pnt tPV = aWLine->Vertex(tiV).Value();
111
112         aWLine->Vertex(tiV).Parameters(aU1, aV1, aU2, aV2);
113         const gp_Pnt2d aPTS1(aU1, aV1), aPTS2(aU2, aV2);
114
115         Standard_Real tDistance = cPV.Distance(tPV);
116         Standard_Real uRs1 = theSurf1->Surface().UResolution(tDistance);
117         Standard_Real vRs1 = theSurf1->Surface().VResolution(tDistance);
118         Standard_Real uRs2 = theSurf2->Surface().UResolution(tDistance);
119         Standard_Real vRs2 = theSurf2->Surface().VResolution(tDistance);
120         Standard_Real RmaxS1 = Max(uRs1,vRs1);
121         Standard_Real RmaxS2 = Max(uRs2,vRs2);
122         if((aPCS1.SquareDistance(aPTS1) < RmaxS1*RmaxS1) && (aPCS2.SquareDistance(aPTS2) < RmaxS2*RmaxS2))
123         {
124           if(RmaxS1 < aTol2D && RmaxS2 < aTol2D)
125           {
126             if( pntDMin > tDistance && tDistance > 1.e-9)
127             {
128               pntDMin = tDistance;
129               VDMin = tiV;
130               WLDMin = iL;
131             }
132           }
133         }
134       }
135     }
136
137     if( VDMin != 0 )
138     {
139       const Handle(IntPatch_Line)& aSLine = theSLin.Value(WLDMin);
140       const Handle(IntPatch_WLine) aWLine = Handle(IntPatch_WLine)::DownCast(aSLine);
141       Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
142       Standard_Integer ciVpar = (Standard_Integer)theWLline->Vertex(ciV).ParameterOnLine();
143       Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
144       Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
145       theWLline->Point(ciVpar).Parameters(u11,v11,u12,v12);
146       aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
147
148       Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
149       IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
150       Standard_Integer cNbP = theWLline->NbPnts();
151
152       TColStd_SequenceOfInteger VPold;
153       Standard_Integer iPo;
154       for( iPo = 1; iPo <= cnbV; iPo++ )
155       {
156         Standard_Real Po = theWLline->Vertex(iPo).ParameterOnLine();
157         Standard_Integer IPo = (Standard_Integer) Po;
158         VPold.Append(IPo);
159       }
160
161       Standard_Boolean removeNext = Standard_False;
162       Standard_Boolean removePrev = Standard_False;
163       if( ciV == 1)
164       {
165         Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
166         if(dPar > 10)
167         {
168           removeNext = Standard_True;
169           for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
170             VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
171         }
172       }
173       else if( ciV == cnbV)
174       {
175         Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
176         if(dPar > 10)
177         {
178           removePrev = Standard_True;
179           VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
180         }
181       }
182       else
183       {
184         Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
185         Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
186         if(dParMi > 10)
187         {
188           removePrev = Standard_True;
189           VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
190         }
191
192         if(dParMa > 10)
193         {
194           removeNext = Standard_True;
195           for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
196           {
197             if(dParMi > 10)
198               VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
199             else
200               VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
201           }
202         }
203         else
204         {
205           if(dParMi > 10)
206             for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
207               VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
208         } 
209       }
210
211       Standard_Integer pI = ciVpar;
212
213       Standard_Integer iP;
214       for( iP = 1; iP <= cNbP; iP++)
215       {
216         if( pI == iP )
217         {
218           IntSurf_PntOn2S newPnt = MakeNewPoint(replacePnt, theWLline->Point(iP), thePeriodsArr);
219           newL2s->Add(newPnt);
220         }
221         else if(removeNext && iP == (pI + 1))
222           continue;
223         else if(removePrev && iP == (pI - 1))
224           continue;
225         else
226           newL2s->Add(theWLline->Point(iP));
227       }
228
229       IntPatch_Point newVtx;
230       gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
231       newVtx.SetValue(Pnt3dV,theTol,Standard_False);
232       newVtx.SetParameters(u21,v21,u22,v22);
233       newVtx.SetParameter(VPold.Value(ciV));
234
235       Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,theTrans1,theTrans2);
236
237       Standard_Integer iV;
238       for( iV = 1; iV <= cnbV; iV++ )
239       {
240         if( iV == ciV )
241           NWLine->AddVertex(newVtx);
242         else
243         {
244           IntPatch_Point theVtx = theWLline->Vertex(iV);
245           theVtx.SetParameter(VPold.Value(iV));
246           NWLine->AddVertex(theVtx);
247         }
248       }
249
250       theWLline = NWLine;
251     }//if( VDMin != 0 )
252   }//for( ciV = 1; ciV <= cnbV; ciV++ )
253 }
254
255 //=======================================================================
256 //function : DublicateOfLinesProcessing
257 //purpose  : Decides, if rejecting current line is necessary
258 //=======================================================================
259 static void DublicateOfLinesProcessing( const IntWalk_PWalking& thePW,
260                                         const Standard_Integer theWLID,
261                                         IntPatch_SequenceOfLine& theLines,
262                                         Standard_Boolean& theIsRejectReq)
263 {
264   const Handle(IntPatch_WLine)& anExistWL =
265                       *((Handle(IntPatch_WLine)*)&theLines.Value(theWLID));
266   const Standard_Integer aNbPrevPoints = anExistWL->NbPnts();
267   const Standard_Integer aNbCurrPoints = thePW.NbPoints();
268
269   if(aNbPrevPoints < aNbCurrPoints)
270   {//Remove preview line
271     theLines.Remove(theWLID);
272     theIsRejectReq = Standard_False;
273   }
274   else if(aNbPrevPoints == aNbCurrPoints)
275   {
276     Standard_Real aLPrev = 0.0, aLCurr = 0.0;
277     for(Standard_Integer aNbPP = 1; aNbPP < aNbPrevPoints; aNbPP++)
278     {
279       const gp_Pnt  aP1prev(anExistWL->Point(aNbPP).Value()),
280         aP2prev(anExistWL->Point(aNbPP+1).Value());
281       const gp_Pnt  aP1curr(thePW.Value(aNbPP).Value()),
282         aP2curr(thePW.Value(aNbPP+1).Value());
283
284       aLPrev += aP1prev.Distance(aP2prev);
285       aLCurr += aP1curr.Distance(aP2curr);
286     }
287
288     if(aLPrev < aLCurr)
289     {//Remove preview line
290       theLines.Remove(theWLID);
291       theIsRejectReq = Standard_False;
292     }
293   }
294 }
295
296 //==================================================================================
297 // function : 
298 // purpose  : 
299 //==================================================================================
300 IntPatch_PrmPrmIntersection::IntPatch_PrmPrmIntersection(): done(Standard_False)
301 {
302 }
303
304 //==================================================================================
305 // function : Perform
306 // purpose  : 
307 //==================================================================================
308 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&   Surf1,
309                                            const Handle(Adaptor3d_TopolTool)& D1,
310                                            const Standard_Real  TolTangency,
311                                            const Standard_Real  Epsilon,
312                                            const Standard_Real  Deflection,
313                                            const Standard_Real  Increment)
314
315   IntPatch_Polyhedron Poly1( Surf1, D1->NbSamplesU(), D1->NbSamplesV() );
316   Perform( Surf1, Poly1, D1, TolTangency, Epsilon, Deflection, Increment );
317 }
318
319 //==================================================================================
320 // function : Perform
321 // purpose  : 
322 //==================================================================================
323 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&   Surf1,
324                                            const IntPatch_Polyhedron& Poly1,
325                                            const Handle(Adaptor3d_TopolTool)& D1,
326                                            const Handle(Adaptor3d_HSurface)&   Surf2,
327                                            const Handle(Adaptor3d_TopolTool)& D2,
328                                            const Standard_Real  TolTangency,
329                                            const Standard_Real  Epsilon,
330                                            const Standard_Real  Deflection,
331                                            const Standard_Real  Increment)
332
333   IntPatch_Polyhedron Poly2( Surf2 );
334   Perform( Surf1, Poly1, D1, Surf2, Poly2, D2, TolTangency, Epsilon, Deflection, Increment);
335 }
336
337 //==================================================================================
338 // function : Perform
339 // purpose  : 
340 //==================================================================================
341 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&   Surf1,
342                                            const Handle(Adaptor3d_TopolTool)& D1,
343                                            const Handle(Adaptor3d_HSurface)&   Surf2,
344                                            const IntPatch_Polyhedron& Poly2,
345                                            const Handle(Adaptor3d_TopolTool)& D2,
346                                            const Standard_Real  TolTangency,
347                                            const Standard_Real  Epsilon,
348                                            const Standard_Real  Deflection,
349                                            const Standard_Real  Increment)
350
351   IntPatch_Polyhedron Poly1( Surf1 );    
352   Perform( Surf1, Poly1, D1, Surf2, Poly2, D2, TolTangency, Epsilon, Deflection, Increment );
353 }
354
355 //==================================================================================
356 // function : Perform
357 // purpose  : 
358 //==================================================================================
359 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
360                                            const IntPatch_Polyhedron&  Poly1,
361                                            const Handle(Adaptor3d_TopolTool)& D1,
362                                            const Handle(Adaptor3d_HSurface)&    Surf2,
363                                            const IntPatch_Polyhedron&  Poly2,
364                                            const Handle(Adaptor3d_TopolTool)& D2,
365                                            const Standard_Real   TolTangency,
366                                            const Standard_Real   Epsilon,
367                                            const Standard_Real   Deflection,
368                                            const Standard_Real   Increment)
369
370   IntPatch_InterferencePolyhedron Interference(Poly1,Poly2);
371   empt = Standard_True;
372   done = Standard_True;
373   SLin.Clear();  
374
375   Standard_Integer nbLigSec = Interference.NbSectionLines();
376   Standard_Integer nbTanZon = Interference.NbTangentZones();
377
378   Standard_Integer NbLigCalculee = 0;
379
380   Standard_Real U1,U2,V1,V2;
381   Standard_Real pu1,pu2,pv1,pv2;
382
383   TColStd_Array1OfReal StartParams(1,4);
384
385   IntWalk_PWalking PW( Surf1, Surf2, TolTangency, Epsilon, Deflection, Increment );
386
387   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
388   Standard_Real    incidence;
389   Standard_Real    dminiPointLigne;
390
391   Standard_Boolean HasStartPoint,RejetLigne;
392
393   IntSurf_PntOn2S StartPOn2S;
394
395   Standard_Integer ver;
396
397   gp_Pnt Point3dDebut,Point3dFin;
398
399   if( nbLigSec >= 1 ) {
400     Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
401     Standard_Integer ls;
402     for( ls = 1; ls <= nbLigSec; ls++ )
403       TabL[ls]=ls;
404
405     Standard_Boolean triok;
406     do { 
407       triok=Standard_True;
408       for(Standard_Integer b=2; b<=nbLigSec; b++) {
409         Standard_Integer nb_B = Interference.LineValue(TabL[b]).NumberOfPoints();
410         Standard_Integer nb_A = Interference.LineValue(TabL[b-1]).NumberOfPoints();
411         if( nb_B > nb_A ) { 
412           Standard_Integer tyu=TabL[b]; 
413           TabL[b]=TabL[b-1];
414           TabL[b-1]=tyu;
415           triok=Standard_False;
416         }
417       }
418     } while( triok==Standard_False );
419
420     for( ls = 1; ls <= nbLigSec; ls++) {
421       const Intf_SectionLine& LineSec=Interference.LineValue(TabL[ls]);
422       Standard_Integer nbp = LineSec.NumberOfPoints();
423
424       Standard_Integer *TabPtDep = new Standard_Integer [nbp+1];
425       Standard_Integer ilig;
426       for( ilig = 1; ilig <= nbp; ilig++) 
427         TabPtDep[ilig]=0;
428
429       Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
430       Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
431
432       SectionPointToParameters(LineSec.GetPoint(1),Poly1,Poly2,UminLig1,VminLig1,UminLig2,VminLig2);
433
434       UmaxLig1=UminLig1;
435       VmaxLig1=VminLig1;
436       UmaxLig2=UminLig2;
437       VmaxLig2=VminLig2;
438
439       for( ilig = 2; ilig <= nbp; ilig++ ) { 
440         SectionPointToParameters(LineSec.GetPoint(ilig),Poly1,Poly2,U1,V1,U2,V2);
441
442         if(U1>UmaxLig1) UmaxLig1=U1;
443         if(V1>VmaxLig1) VmaxLig1=V1;
444         if(U2>UmaxLig2) UmaxLig2=U2;
445         if(V2>VmaxLig2) VmaxLig2=V2;
446
447         if(U1<UminLig1) UminLig1=U1;
448         if(V1<VminLig1) VminLig1=V1;
449         if(U2<UminLig2) UminLig2=U2;
450         if(V2<VminLig2) VminLig2=V2;
451       }
452
453       Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
454       Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
455       Standard_Integer IndicePointdeDepart1 = 0,IndicePointdeDepart2 = 0;
456       Standard_Boolean lignetrouvee=Standard_False;
457
458       do { 
459         NombreDePointsDeDepartDuCheminement++;
460         if(NombreDePointsDeDepartDuCheminement == 1) { 
461           incidence=3.0;
462           Standard_Integer nbp1_4=nbp/4;
463           Standard_Integer nbp3_4=nbp-nbp1_4;
464
465           Standard_Integer nsp;
466           for(nsp=nbp/2; nsp<nbp3_4; nsp++) { 
467             Standard_Real CurrentIncidence =  LineSec.GetPoint(nsp).Incidence();
468             if(CurrentIncidence < incidence) { 
469               nbps2 = nsp;
470               incidence = 0.9*CurrentIncidence;
471             }
472           }
473
474           for(nsp=nbp/2; nsp>nbp1_4; nsp--) { 
475             Standard_Real CurrentIncidence =  LineSec.GetPoint(nsp).Incidence();
476             if(CurrentIncidence < incidence) { 
477               nbps2 = nsp;
478               incidence = 0.9*CurrentIncidence;
479             }
480           }
481
482           if(nbp<3) 
483             NombreDePointsDeDepartDuCheminement=3;
484
485           IndicePointdeDepart1 = nbps2;
486         }
487         else if(NombreDePointsDeDepartDuCheminement == 2) { 
488           if(IndicePointdeDepart1 == 1) { 
489             nbps2 = nbp/2;
490             IndicePointdeDepart2 = nbps2;
491           }
492           else { 
493             nbps2 = 1;
494             IndicePointdeDepart2 = 1;
495           }
496         }
497         else if(NombreDePointsDeDepartDuCheminement == 3) {
498           if(IndicePointdeDepart1 == nbp)
499             nbps2 = (IndicePointdeDepart1+IndicePointdeDepart2)/2;
500           else
501             nbps2 = nbp;
502         }
503         else { 
504           nbps2 = NombreDePointsDeDepartDuCheminement-3;
505           NombreDePointsDeDepartDuCheminement++;
506         } 
507
508         if(TabPtDep[nbps2]==0) { 
509           TabPtDep[nbps2]=1;
510           SectionPointToParameters(LineSec.GetPoint(nbps2),Poly1,Poly2,U1,V1,U2,V2);
511
512           StartParams(1) = U1;
513           StartParams(2) = V1;
514           StartParams(3) = U2;
515           StartParams(4) = V2;
516
517           HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
518           dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
519
520           if(HasStartPoint) { 
521             StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
522             NbLigCalculee = SLin.Length();
523             Standard_Integer l;
524             for( l=1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
525               const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
526
527               if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
528                 dminiPointLigne = 0.0;
529               }
530             } // for ( l ...
531
532             if(dminiPointLigne > SeuildPointLigne) { 
533               PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
534               if(PW.IsDone()) {
535                 if(PW.NbPoints()>2) { 
536                   RejetLigne = Standard_False;
537                   Point3dDebut = PW.Value(1).Value();
538                   const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
539                   Point3dFin   = PointFin.Value();
540                   for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
541                     const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
542
543                     // Check end point if it is on existing line.
544                     // Start point is checked before.
545                     if (IsPointOnLine(PointFin, verwline, Deflection)) {
546                       RejetLigne = Standard_True; 
547                       break;
548                     }
549
550                     const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
551                     const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
552                     if( (Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) &&
553                         (Point3dFin.Distance(verPointFin.Value()) <= TolTangency))
554                     { 
555                       RejetLigne = Standard_True; 
556                       break;
557                     }
558                   }
559
560                   if(RejetLigne)
561                   {
562                     DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
563                   }
564
565                   if(!RejetLigne) { 
566                     // Calculation transition
567                     IntSurf_TypeTrans trans1,trans2;
568                     Standard_Real locu,locv;
569                     gp_Vec norm1,norm2,d1u,d1v;
570                     gp_Pnt ptbid;
571                     Standard_Integer indextg;
572                     gp_Vec tgline(PW.TangentAtLine(indextg));
573                     PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
574                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
575                     norm1 = d1u.Crossed(d1v);
576                     PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
577                     Surf2->D1(locu,locv,ptbid,d1u,d1v);
578                     norm2 = d1u.Crossed(d1v);
579                     if(tgline.DotCross(norm2,norm1)>0.) {
580                       trans1 = IntSurf_Out;
581                       trans2 = IntSurf_In;
582                     }
583                     else {
584                       trans1 = IntSurf_In;
585                       trans2 = IntSurf_Out;
586                     }
587
588                     Standard_Real TolTang = TolTangency;
589                     Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
590                     IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
591                     IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
592
593                     if(wline->NbVertex() == 0) {
594                       IntPatch_Point vtx;
595                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
596                       POn2S.Parameters(pu1,pv1,pu2,pv2);
597                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
598                       vtx.SetParameters(pu1,pv1,pu2,pv2);
599                       vtx.SetParameter(1);
600                       wline->AddVertex(vtx);
601
602                       POn2S = PW.Line()->Value(wline->NbPnts());
603                       POn2S.Parameters(pu1,pv1,pu2,pv2);
604                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
605                       vtx.SetParameters(pu1,pv1,pu2,pv2);
606                       vtx.SetParameter(wline->NbPnts());
607                       wline->AddVertex(vtx);
608                     }
609
610                     lignetrouvee = Standard_True;
611                     AddWLine(SLin, wline, Deflection);
612                     empt = Standard_False;
613                   }// !RejetLigne
614                 }// PW.NbPoints()>2
615               }// done is True
616             }// dminiPointLigne > SeuildPointLigne
617           }// HasStartPoint
618         }// TabPtDep[nbps2]==0
619       } while( nbp>5 && ( !( ( (NombreDePointsDeDepartDuCheminement >= 3) && lignetrouvee ) || 
620         ( (NombreDePointsDeDepartDuCheminement-3>=nbp) && !lignetrouvee ) ) ) );
621
622       delete [] TabPtDep;
623     }// for( ls ...
624
625     delete [] TabL;
626   }// nbLigSec >= 1
627
628   Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
629   Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
630
631   UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
632   UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
633
634   Standard_Integer z;
635   for(z=1; z <= nbTanZon; z++) { 
636     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
637     for(Standard_Integer pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
638       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly2,U1,V1,U2,V2);
639
640       if(U1>UmaxLig1) UmaxLig1=U1;
641       if(V1>VmaxLig1) VmaxLig1=V1;
642       if(U2>UmaxLig2) UmaxLig2=U2;
643       if(V2>VmaxLig2) VmaxLig2=V2;
644
645       if(U1<UminLig1) UminLig1=U1;
646       if(V1<VminLig1) VminLig1=V1;
647       if(U2<UminLig2) UminLig2=U2;
648       if(V2<VminLig2) VminLig2=V2;
649     }
650   }
651
652   for(z=1; z <= nbTanZon; z++) {
653     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
654     Standard_Integer pz;
655     for( pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
656       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly2,U1,V1,U2,V2);
657
658       StartParams(1) = U1;
659       StartParams(2) = V1;
660       StartParams(3) = U2;
661       StartParams(4) = V2;
662
663       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
664       if(HasStartPoint) { 
665         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
666         NbLigCalculee = SLin.Length();
667         dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
668         Standard_Integer l;
669         for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
670           const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
671
672           if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
673             dminiPointLigne = 0.0;
674           }
675         }// for( l ...
676
677         if(dminiPointLigne > SeuildPointLigne) { 
678           PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
679           if(PW.IsDone()) {
680             if(PW.NbPoints()>2) { 
681               RejetLigne = Standard_False;
682               Point3dDebut = PW.Value(1).Value();
683               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
684               Point3dFin   = PointFin.Value();
685               for(ver=1 ; ver<= NbLigCalculee ; ver++) { 
686                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
687
688                 // Check end point if it is on existing line.
689                 // Start point is checked before.
690                 if (IsPointOnLine(PointFin, verwline, Deflection)) {
691                   RejetLigne = Standard_True; 
692                   break;
693                 }
694
695                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
696                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
697                 if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
698                     (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
699                 {
700                   RejetLigne = Standard_True; 
701                   break;
702                 }
703               }
704
705               if(RejetLigne)
706               {
707                 DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
708               }
709
710               if(!RejetLigne) { 
711                 IntSurf_TypeTrans trans1,trans2;
712                 Standard_Real locu,locv;
713                 gp_Vec norm1,norm2,d1u,d1v;
714                 gp_Pnt ptbid;
715                 Standard_Integer indextg;
716                 gp_Vec tgline(PW.TangentAtLine(indextg));
717                 PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
718                 Surf1->D1(locu,locv,ptbid,d1u,d1v);
719                 norm1 = d1u.Crossed(d1v);
720                 PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
721                 Surf2->D1(locu,locv,ptbid,d1u,d1v);
722                 norm2 = d1u.Crossed(d1v);
723                 if(tgline.DotCross(norm2,norm1)>0.) {
724                   trans1 = IntSurf_Out;
725                   trans2 = IntSurf_In;
726                 }
727                 else {
728                   trans1 = IntSurf_In;
729                   trans2 = IntSurf_Out;
730                 }
731
732                 Standard_Real TolTang = TolTangency;
733                 Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
734                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
735                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
736
737                 if(wline->NbVertex() == 0) {
738                   IntPatch_Point vtx;
739                   IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
740                   POn2S.Parameters(pu1,pv1,pu2,pv2);
741                   vtx.SetValue(Point3dDebut,TolTang,Standard_False);
742                   vtx.SetParameters(pu1,pv1,pu2,pv2);
743                   vtx.SetParameter(1);
744                   wline->AddVertex(vtx);
745
746                   POn2S = PW.Line()->Value(wline->NbPnts());
747                   POn2S.Parameters(pu1,pv1,pu2,pv2);
748                   vtx.SetValue(Point3dFin,TolTang,Standard_False);
749                   vtx.SetParameters(pu1,pv1,pu2,pv2);
750                   vtx.SetParameter(wline->NbPnts());
751                   wline->AddVertex(vtx);
752                 }
753
754                 AddWLine(SLin, wline, Deflection);
755                 empt = Standard_False;
756               }// if !RejetLigne
757             }// PW.NbPoints()>2
758           }// done is True
759         }// dminiPointLigne > SeuildPointLigne
760       }// HasStartPoint
761     }// for( pz ...
762   }// for( z ...
763 }
764
765 //==================================================================================
766 // function : Perform
767 // purpose  : 
768 //==================================================================================
769 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
770                                            const IntPatch_Polyhedron&  Poly1,
771                                            const Handle(Adaptor3d_TopolTool)& D1,
772                                            const Standard_Real   TolTangency,
773                                            const Standard_Real   Epsilon,
774                                            const Standard_Real   Deflection,
775                                            const Standard_Real   Increment)
776
777   IntPatch_InterferencePolyhedron Interference(Poly1);
778   empt = Standard_True;
779   done = Standard_True;
780   SLin.Clear();  
781
782   Standard_Integer nbLigSec = Interference.NbSectionLines();
783   Standard_Integer nbTanZon = Interference.NbTangentZones();
784
785   Standard_Integer NbPntOn2SOnLine;
786   Standard_Integer NbLigCalculee = 0;
787
788   Standard_Real U1,U2,V1,V2;
789   Standard_Real pu1,pu2,pv1,pv2;
790
791   TColStd_Array1OfReal StartParams(1,4);
792   IntWalk_PWalking PW(Surf1,Surf1,TolTangency,Epsilon,Deflection,Increment);
793
794   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
795   Standard_Real    incidence;
796   Standard_Real    dminiPointLigne;
797
798   Standard_Boolean HasStartPoint,RejetLigne;
799
800   IntSurf_PntOn2S StartPOn2S;
801
802   Standard_Integer ver;
803
804   gp_Pnt Point3dDebut,Point3dFin;
805
806   if(nbLigSec>=1) {
807     Standard_Integer ls;
808     for( ls = 1; ls <= nbLigSec; ls++) {
809       const Intf_SectionLine& LineSec=Interference.LineValue(ls);
810       Standard_Integer nbp = LineSec.NumberOfPoints();
811       Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
812       Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
813       Standard_Integer IndicePointdeDepart1 = 0, IndicePointdeDepart2 = 0;
814       do { 
815         NombreDePointsDeDepartDuCheminement++;
816         if(NombreDePointsDeDepartDuCheminement == 1) { 
817           incidence = 0.0;
818           Standard_Integer nsp1;
819           for( nsp1= nbp/2; nsp1 >= 1; nsp1--) { 
820             SectionPointToParameters(LineSec.GetPoint(nsp1),Poly1,Poly1,U1,V1,U2,V2);
821             Standard_Real CurrentIncidence =  Abs(U1-U2)+Abs(V1-V2);
822             if(CurrentIncidence > incidence) { 
823               nbps2 = nsp1;
824               incidence = CurrentIncidence;
825             }
826           }
827           for( nsp1 = nbp/2; nsp1 <= nbp; nsp1++) { 
828             SectionPointToParameters(LineSec.GetPoint(nsp1),Poly1,Poly1,U1,V1,U2,V2);
829             Standard_Real CurrentIncidence =  Abs(U1-U2)+Abs(V1-V2);
830             if(CurrentIncidence > incidence) { 
831               nbps2 = nsp1;
832               incidence = CurrentIncidence;
833             }
834           }
835
836           if(nbp<3) 
837             NombreDePointsDeDepartDuCheminement=3;
838
839           IndicePointdeDepart1 = nbps2;
840         }
841         else if(NombreDePointsDeDepartDuCheminement == 2) { 
842           if(IndicePointdeDepart1 == 1) { 
843             nbps2 = nbp/2;
844             IndicePointdeDepart2 = nbps2;
845           }
846           else { 
847             nbps2 = 1;
848             IndicePointdeDepart2 = 1;
849           }
850         }
851         else {
852           if(IndicePointdeDepart1 == nbp)
853             nbps2 = (IndicePointdeDepart1+IndicePointdeDepart2)/2;
854           else
855             nbps2 = nbp;
856         }
857
858         SectionPointToParameters(LineSec.GetPoint(nbps2),Poly1,Poly1,U1,V1,U2,V2);
859
860         StartParams(1) = U1;
861         StartParams(2) = V1;
862         StartParams(3) = U2;
863         StartParams(4) = V2;
864
865         HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
866         dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
867         if(HasStartPoint) { 
868           StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
869           if(Abs(pu1-pu2)>1e-7 || Abs(pv1-pv2)>1e-7) { 
870             NbLigCalculee = SLin.Length();
871             Standard_Integer l;
872             for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
873               const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
874               if( (testwline->IsOutSurf1Box(gp_Pnt2d(pu1,pv1))==Standard_False) &&
875                 (testwline->IsOutSurf2Box(gp_Pnt2d(pu2,pv2))==Standard_False) &&
876                 (testwline->IsOutBox(StartPOn2S.Value())==Standard_False) ) { 
877                   NbPntOn2SOnLine = testwline->NbPnts();
878                   Standard_Integer ll;
879                   for( ll=1; (ll < NbPntOn2SOnLine) && (dminiPointLigne >= SeuildPointLigne); ll++) { 
880                     Standard_Real t,Au1,Av1,Au2,Av2,Bu1,Bv1,Bu2,Bv2;
881                     testwline->Point(ll).Parameters(Au1,Av1,Au2,Av2);
882                     testwline->Point(ll+1).Parameters(Bu1,Bv1,Bu2,Bv2);
883                     if(Au1>Bu1) {
884                       t=Au1;
885                       Au1=Bu1;
886                       Bu1=t;
887                     } 
888                     if(Av1>Bv1) {
889                       t=Av1;
890                       Av1=Bv1;
891                       Bv1=t;
892                     } 
893                     Au1-=1.0e-7;
894                     Av1-=1.0e-7;
895                     Bu1+=1.0e-7;
896                     Bv1+=1.0e-7;
897
898                     if((pu1>=Au1) && (pu1<=Bu1) && (pv1>=Av1) && (pv1<=Bv1))
899                       dminiPointLigne = 0.0; 
900                     else { 
901                       if((pu2>=Au1) && (pu2<=Bu1) && (pv2>=Av1) && (pv2<=Bv1))
902                         dminiPointLigne = 0.0;
903                     }
904                   }// for( ll ...
905               }// if ...
906             }// for( l ...
907
908             if(dminiPointLigne > SeuildPointLigne) { 
909               PW.Perform(StartParams);
910               if(PW.IsDone()) {
911                 if(PW.NbPoints()>2) { 
912                   RejetLigne = Standard_False;
913                   Point3dDebut = PW.Value(1).Value();
914                   Point3dFin   = PW.Value(PW.NbPoints()).Value();
915                   for(ver=1 ; ver<= NbLigCalculee ; ver++) { 
916                     const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
917                     const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
918                     const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
919                     if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
920                         (Point3dFin.Distance(verPointFin.Value()) < TolTangency)) 
921                     {
922                       RejetLigne = Standard_True; 
923                       break;
924                     }
925                   }
926
927                   if(RejetLigne)
928                   {
929                     DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
930                   }
931
932                   if(!RejetLigne) { 
933                     IntSurf_TypeTrans trans1,trans2;
934                     Standard_Real locu,locv;
935                     gp_Vec norm1,norm2,d1u,d1v;
936                     gp_Pnt ptbid;
937                     Standard_Integer indextg;
938                     gp_Vec tgline(PW.TangentAtLine(indextg));
939                     PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
940                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
941                     norm1 = d1u.Crossed(d1v);
942                     PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
943                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
944                     norm2 = d1u.Crossed(d1v);
945                     if (tgline.DotCross(norm2,norm1)>0.) {
946                       trans1 = IntSurf_Out;
947                       trans2 = IntSurf_In;
948                     }
949                     else {
950                       trans1 = IntSurf_In;
951                       trans2 = IntSurf_Out;
952                     }
953
954                     IntSurf_LineOn2S LineOn2S;
955                     Standard_Integer nbpw,imin,imax,i;
956                     nbpw = PW.Line()->NbPoints();
957                     Standard_Real u1,v1,u2,v2;
958                     i=0;
959                     do { 
960                       i++;
961                       imin=i;
962                       const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
963                       Pi.Parameters(u1,v1,u2,v2);
964                     } while((i<nbpw)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
965
966                     if(imin>2)
967                       imin--;
968
969                     i=nbpw+1;
970                     do { 
971                       i--;
972                       imax=i;
973                       const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
974                       Pi.Parameters(u1,v1,u2,v2);
975                     } while((i>2)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
976
977                     if(imax<nbpw)
978                       imax++;
979
980                     if(imin<imax) { 
981                       Handle(IntSurf_LineOn2S) PWLine = new IntSurf_LineOn2S();
982                       for(i=imin;i<=imax;i++) 
983                         PWLine->Add(PW.Line()->Value(i));
984
985                       Standard_Real TolTang = TolTangency;
986                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PWLine,Standard_False,trans1,trans2);
987                       const IntSurf_PntOn2S& POn2SDeb = wline->Point(1);
988                       const IntSurf_PntOn2S& POn2SFin = wline->Point(wline->NbPnts());
989                       if((POn2SDeb.Value()).Distance(POn2SFin.Value()) <= TolTangency) { 
990                         Standard_Real u1t,v1t,u2t,v2t; 
991                         POn2SDeb.Parameters(u1t,v1t,u2t,v2t);
992                         IntPatch_Point vtx;
993                         vtx.SetValue(POn2SDeb.Value(),TolTang,Standard_False);
994                         vtx.SetParameters(u2t,v2t,u1t,v1t);
995                         vtx.SetParameter(wline->NbPnts());
996                         wline->SetPoint(wline->NbPnts(),vtx);
997                       }
998                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf1,Standard_True,TolTang);
999                       if(wline->NbVertex() == 0) {
1000                         IntPatch_Point vtx;
1001                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1002                         POn2S.Parameters(pu1,pv1,pu2,pv2);
1003                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1004                         vtx.SetParameters(pu1,pv1,pu2,pv2);
1005                         vtx.SetParameter(1);
1006                         wline->AddVertex(vtx);
1007
1008                         POn2S = PW.Line()->Value(wline->NbPnts());
1009                         POn2S.Parameters(pu1,pv1,pu2,pv2);
1010                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
1011                         vtx.SetParameters(pu1,pv1,pu2,pv2);
1012                         vtx.SetParameter(wline->NbPnts());
1013                         wline->AddVertex(vtx);
1014                       }
1015                       SLin.Append(wline);
1016                       empt = Standard_False;
1017                     }// imin<imax
1018                   }// !RejetLigne
1019                 }// PW.NbPoints()>2
1020               }// done is True
1021             }// dminiPointLigne > SeuildPointLigne
1022           }// Abs || Abs
1023         }// HasStartPoint
1024       } while(nbp>5 && NombreDePointsDeDepartDuCheminement<3);
1025     }// for( ls ...
1026   }// nbLigSec>=1
1027
1028   Standard_Integer z;
1029   for( z = 1; z <= nbTanZon; z++) { 
1030     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
1031     for(Standard_Integer pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
1032       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly1,U1,V1,U2,V2);
1033
1034       StartParams(1) = U1;
1035       StartParams(2) = V1;
1036       StartParams(3) = U2;
1037       StartParams(4) = V2;
1038
1039       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);     
1040       if(HasStartPoint) { 
1041         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
1042         if(Abs(pu1-pu2)>1e-7 || Abs(pv1-pv2)>1e-7) { 
1043           NbLigCalculee = SLin.Length();
1044           dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
1045           Standard_Integer l;
1046           for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
1047             const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
1048             if( (testwline->IsOutSurf1Box(gp_Pnt2d(pu1,pv1))==Standard_False) &&
1049               (testwline->IsOutSurf2Box(gp_Pnt2d(pu2,pv2))==Standard_False) &&
1050               (testwline->IsOutBox(StartPOn2S.Value())==Standard_False) ) { 
1051                 NbPntOn2SOnLine = testwline->NbPnts();
1052                 Standard_Integer ll;
1053                 for( ll = 1; (ll < NbPntOn2SOnLine) && (dminiPointLigne >= SeuildPointLigne); ll++) { 
1054                   Standard_Real t,Au1,Av1,Au2,Av2,Bu1,Bv1,Bu2,Bv2;
1055                   testwline->Point(ll).Parameters(Au1,Av1,Au2,Av2);
1056                   testwline->Point(ll+1).Parameters(Bu1,Bv1,Bu2,Bv2);
1057                   if(Au1>Bu1) {
1058                     t=Au1;
1059                     Au1=Bu1;
1060                     Bu1=t;
1061                   } 
1062                   if(Av1>Bv1) {
1063                     t=Av1;
1064                     Av1=Bv1;
1065                     Bv1=t;
1066                   } 
1067                   Au1-=1.0e-7;
1068                   Av1-=1.0e-7;
1069                   Bu1+=1.0e-7;
1070                   Bv1+=1.0e-7;
1071                   if((pu1>=Au1) && (pu1<=Bu1) && (pv1>=Av1) && (pv1<=Bv1))
1072                     dminiPointLigne = 0.0; 
1073                   else { 
1074                     if((pu2>=Au1) && (pu2<=Bu1) && (pv2>=Av1) && (pv2<=Bv1))
1075                       dminiPointLigne = 0.0;
1076                   }
1077                 }// for( ll ...
1078             }// if ...
1079           }// for( l ...
1080
1081           if(dminiPointLigne > SeuildPointLigne) { 
1082             PW.Perform(StartParams);
1083             if(PW.IsDone()) {
1084               if(PW.NbPoints()>2) { 
1085                 RejetLigne = Standard_False;
1086                 Point3dDebut = PW.Value(1).Value();
1087                 Point3dFin   = PW.Value(PW.NbPoints()).Value();
1088                 for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
1089                   const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
1090                   const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
1091                   const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
1092                   if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
1093                       (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
1094                   {
1095                     RejetLigne = Standard_True; 
1096                     break;
1097                   }
1098                 }
1099
1100                 if(RejetLigne)
1101                 {
1102                   DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
1103                 }
1104
1105                 if(!RejetLigne) { 
1106                   IntSurf_TypeTrans trans1,trans2;
1107                   Standard_Real locu,locv;
1108                   gp_Vec norm1,norm2,d1u,d1v;
1109                   gp_Pnt ptbid;
1110                   Standard_Integer indextg;
1111                   gp_Vec tgline(PW.TangentAtLine(indextg));
1112                   PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1113                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
1114                   norm1 = d1u.Crossed(d1v);
1115                   PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1116                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
1117                   norm2 = d1u.Crossed(d1v);
1118                   if(tgline.DotCross(norm2,norm1)>0.) {
1119                     trans1 = IntSurf_Out;
1120                     trans2 = IntSurf_In;
1121                   }
1122                   else {
1123                     trans1 = IntSurf_In;
1124                     trans2 = IntSurf_Out;
1125                   }
1126
1127                   IntSurf_LineOn2S LineOn2S;
1128                   Standard_Integer nbp,imin,imax,i;
1129                   nbp = PW.Line()->NbPoints();
1130                   Standard_Real u1,v1,u2,v2;
1131                   i=0;
1132                   do { 
1133                     i++;
1134                     imin=i;
1135                     const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
1136                     Pi.Parameters(u1,v1,u2,v2);
1137                   } while((i<nbp)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
1138
1139                   if(imin>2)
1140                     imin--;
1141
1142                   i=nbp+1;
1143                   do { 
1144                     i--;
1145                     imax=i;
1146                     const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
1147                     Pi.Parameters(u1,v1,u2,v2);
1148                   } while((i>2)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
1149
1150                   if(imax<nbp)
1151                     imax++;
1152
1153                   if(imin<imax) { 
1154                     Handle(IntSurf_LineOn2S) PWLine = new IntSurf_LineOn2S();
1155                     for(i=imin;i<=imax;i++)
1156                       PWLine->Add(PW.Line()->Value(i));
1157
1158                     Standard_Real TolTang = TolTangency;
1159                     Handle(IntPatch_WLine) wline = new IntPatch_WLine(PWLine,Standard_False,trans1,trans2);
1160                     const IntSurf_PntOn2S& POn2SDeb = wline->Point(1);
1161                     const IntSurf_PntOn2S& POn2SFin = wline->Point(wline->NbPnts());
1162                     if((POn2SDeb.Value()).Distance(POn2SFin.Value()) <= TolTangency) { 
1163                       Standard_Real u1t,v1t,u2t,v2t; 
1164                       POn2SDeb.Parameters(u1t,v1t,u2t,v2t);
1165                       IntPatch_Point vtx;
1166                       vtx.SetValue(POn2SDeb.Value(),TolTang,Standard_False);
1167                       vtx.SetParameters(u2t,v2t,u1t,v1t);
1168                       vtx.SetParameter(wline->NbPnts());
1169                       wline->SetPoint(wline->NbPnts(),vtx);
1170                     }
1171
1172                     IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf1,Standard_True,TolTang);
1173
1174                     if(wline->NbVertex() == 0) {
1175                       IntPatch_Point vtx;
1176                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1177                       POn2S.Parameters(pu1,pv1,pu2,pv2);
1178                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1179                       vtx.SetParameters(pu1,pv1,pu2,pv2);
1180                       vtx.SetParameter(1);
1181                       wline->AddVertex(vtx);
1182
1183                       POn2S = PW.Line()->Value(wline->NbPnts());
1184                       POn2S.Parameters(pu1,pv1,pu2,pv2);
1185                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
1186                       vtx.SetParameters(pu1,pv1,pu2,pv2);
1187                       vtx.SetParameter(wline->NbPnts());
1188                       wline->AddVertex(vtx);
1189                     }
1190
1191                     SLin.Append(wline);
1192                     empt = Standard_False;
1193                   }// imin<imax
1194                 }// !RejetLigne
1195               }// PW.NbPoints()>2
1196             }// done a True
1197           }// dminiPointLigne > SeuildPointLigne
1198         }// Abs || Abs
1199       }// HasStartPoint
1200     }// for ( pz ...
1201   }// for( z ...
1202 }
1203
1204 //==================================================================================
1205 // function : NewLine
1206 // purpose  : 
1207 //==================================================================================
1208 Handle(IntPatch_Line) IntPatch_PrmPrmIntersection::NewLine (const Handle(Adaptor3d_HSurface)&    Surf1,
1209                                                            const Handle(Adaptor3d_HSurface)&    Surf2,
1210                                                            const Standard_Integer NumLine,
1211                                                            const Standard_Integer Low,
1212                                                            const Standard_Integer High,
1213                                                            const Standard_Integer NbPntsToInsert) const 
1214
1215   Standard_Integer NbPnts = NbPntsToInsert + High - Low;
1216   if(NumLine>NbLines() || NumLine<1  || Low>=High ) 
1217     Standard_OutOfRange::Raise(" IntPatch_PrmPrmIntersection NewLine "); 
1218   //------------------------------------------------------------------
1219   //--  Indice     :   Low       Low+1     I    I+1         High    --
1220   //--                                                              --
1221   //--  Abs.Curv.  :  S(Low)              S(I)  S(I+1)      S(High) --
1222   //--                                                              --
1223   //--                On echantillonne a abcisse curviligne         --
1224   //--                constante.                                    --
1225   //--                L abcisse est calculee sur les params U1,V1   --
1226   //------------------------------------------------------------------
1227   TColStd_Array1OfReal U1(Low,High);
1228   TColStd_Array1OfReal V1(Low,High);
1229   TColStd_Array1OfReal U2(Low,High);
1230   TColStd_Array1OfReal V2(Low,High);
1231   TColStd_Array1OfReal AC(Low,High);
1232
1233   Standard_Real s,ds;
1234   Handle(IntPatch_WLine) TheLine = Handle(IntPatch_WLine)::DownCast(Line(NumLine));
1235   const IntSurf_PntOn2S& Point=TheLine->Point(Low);
1236   Standard_Real u1,v1,u2,v2;
1237   Point.Parameters(u1,v1,u2,v2);
1238   U1(Low) = u1;
1239   V1(Low) = v1;
1240   U2(Low) = u2;
1241   V2(Low) = v2;
1242   AC(Low) =0.0;
1243
1244   IntWalk_PWalking PW(Surf1,Surf2,0.000001,0.000001,0.001,0.001);
1245
1246   Standard_Integer i;
1247   for(i=Low+1; i<=High; i++)
1248   {
1249     const IntSurf_PntOn2S& Pointi=TheLine->Point(i);
1250     Pointi.Parameters(u1,v1,u2,v2);
1251     U1(i) = u1;
1252     V1(i) = v1;
1253     U2(i) = u2;
1254     V2(i) = v2;
1255
1256     Standard_Real du1=u1-U1(i-1);
1257     Standard_Real dv1=v1-V1(i-1);
1258
1259     AC(i) = AC(i-1) + Sqrt((du1*du1)+(dv1*dv1));
1260   }
1261
1262   Handle(IntSurf_LineOn2S) ResultPntOn2SLine = new IntSurf_LineOn2S();
1263
1264   IntSurf_PntOn2S StartPOn2S;  
1265   TColStd_Array1OfReal StartParams(1,4);
1266
1267   ResultPntOn2SLine->Add(TheLine->Point(Low));
1268
1269   ds = AC(High) / (NbPnts-1);
1270   Standard_Integer Indice = Low;
1271
1272   Standard_Real dsmin = ds*0.3;
1273   Standard_Real smax  = AC(High);
1274
1275   for(i=2,s=ds; (i<NbPnts)&&(s<smax); i++,s+=ds)
1276   { 
1277     while(AC(Indice+1) <= s)
1278     { 
1279       ResultPntOn2SLine->Add(TheLine->Point(Indice));
1280       Indice++;
1281     }
1282     Standard_Real a = s - AC(Indice);
1283     Standard_Real b = AC(Indice+1) - s;
1284     Standard_Real nab = 1.0/(a+b);
1285     //----------------------------------------------------------
1286     //-- Verification :  Si Dist au prochain  point < dsmin   --
1287     //--                 Si Dist au precedent point < dsmin   --
1288     //--                                                      --
1289     //----------------------------------------------------------
1290     if((nab > ds)&&(a>dsmin)&&(b>dsmin))
1291     {
1292       StartParams(1) = (U1(Indice) * b   +  U1(Indice+1) * a) * nab;
1293       StartParams(2) = (V1(Indice) * b   +  V1(Indice+1) * a) * nab;
1294       StartParams(3) = (U2(Indice) * b   +  U2(Indice+1) * a) * nab;
1295       StartParams(4) = (V2(Indice) * b   +  V2(Indice+1) * a) * nab;
1296
1297       Standard_Boolean HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
1298       if(HasStartPoint)
1299         ResultPntOn2SLine->Add(StartPOn2S);
1300     }
1301     else
1302       s+=dsmin; 
1303   }
1304
1305   ResultPntOn2SLine->Add(TheLine->Point(High));
1306
1307   return(new IntPatch_WLine(ResultPntOn2SLine,Standard_False));
1308 }
1309
1310 //==================================================================================
1311 // function : SectionPointToParameters
1312 // purpose  : 
1313 //==================================================================================
1314 void SectionPointToParameters(const Intf_SectionPoint& Sp,
1315                               const IntPatch_Polyhedron& Poly1,
1316                               const IntPatch_Polyhedron& Poly2,
1317                               Standard_Real& u1,
1318                               Standard_Real& v1,
1319                               Standard_Real& u2,
1320                               Standard_Real& v2)
1321 {
1322   Intf_PIType       typ;
1323   Standard_Integer  Adr1,Adr2;
1324   Standard_Real     Param,u,v;
1325   gp_Pnt P(Sp.Pnt());
1326
1327   Standard_Integer Pt1,Pt2,Pt3;
1328
1329   Sp.InfoFirst(typ,Adr1,Adr2,Param);
1330   switch(typ) { 
1331   case Intf_VERTEX:   //-- Adr1 est le numero du vertex
1332     {
1333       Poly1.Parameters(Adr1,u1,v1);
1334       break;
1335     }
1336   case Intf_EDGE:
1337     {
1338       Poly1.Parameters(Adr1,u1,v1);    
1339       Poly1.Parameters(Adr2,u,v);
1340       u1+= Param * (u-u1);
1341       v1+= Param * (v-v1);
1342       break;
1343     }
1344   case Intf_FACE:
1345     {
1346       Standard_Real ua,va,ub,vb,uc,vc,ca,cb,cc,cabc;
1347       Poly1.Triangle(Adr1,Pt1,Pt2,Pt3);
1348       gp_Pnt PA(Poly1.Point(Pt1));
1349       gp_Pnt PB(Poly1.Point(Pt2));
1350       gp_Pnt PC(Poly1.Point(Pt3));
1351       Poly1.Parameters(Pt1,ua,va);
1352       Poly1.Parameters(Pt2,ub,vb);
1353       Poly1.Parameters(Pt3,uc,vc);
1354       gp_Vec Normale(gp_Vec(PA,PB).Crossed(gp_Vec(PA,PC)));
1355       cc = (gp_Vec(PA,PB).Crossed(gp_Vec(PA,P))).Dot(Normale);
1356       ca = (gp_Vec(PB,PC).Crossed(gp_Vec(PB,P))).Dot(Normale);
1357       cb = (gp_Vec(PC,PA).Crossed(gp_Vec(PC,P))).Dot(Normale);
1358       cabc = ca + cb + cc;
1359
1360       ca/=cabc;     cb/=cabc;       cc/=cabc;
1361
1362       u1 = ca * ua + cb * ub + cc * uc;
1363       v1 = ca * va + cb * vb + cc * vc;
1364       break;
1365     }
1366   default: 
1367     {
1368       //-- cout<<" Default dans SectionPointToParameters "<<endl;
1369       break;
1370     }
1371   }
1372
1373
1374   Sp.InfoSecond(typ,Adr1,Adr2,Param);
1375   switch(typ) { 
1376   case Intf_VERTEX:   //-- Adr1 est le numero du vertex
1377     {
1378       Poly2.Parameters(Adr1,u2,v2);
1379       break;
1380     }
1381   case Intf_EDGE:
1382     {
1383       Poly2.Parameters(Adr1,u2,v2);    
1384       Poly2.Parameters(Adr2,u,v);
1385       u2+= Param * (u-u2);
1386       v2+= Param * (v-v2);
1387       break;
1388     }
1389   case Intf_FACE:
1390     {
1391       Standard_Real ua,va,ub,vb,uc,vc,ca,cb,cc,cabc;
1392       Poly2.Triangle(Adr1,Pt1,Pt2,Pt3);
1393       gp_Pnt PA(Poly2.Point(Pt1));
1394       gp_Pnt PB(Poly2.Point(Pt2));
1395       gp_Pnt PC(Poly2.Point(Pt3));
1396       Poly2.Parameters(Pt1,ua,va);
1397       Poly2.Parameters(Pt2,ub,vb);
1398       Poly2.Parameters(Pt3,uc,vc);
1399       gp_Vec Normale(gp_Vec(PA,PB).Crossed(gp_Vec(PA,PC)));
1400       cc = (gp_Vec(PA,PB).Crossed(gp_Vec(PA,P))).Dot(Normale);
1401       ca = (gp_Vec(PB,PC).Crossed(gp_Vec(PB,P))).Dot(Normale);
1402       cb = (gp_Vec(PC,PA).Crossed(gp_Vec(PC,P))).Dot(Normale);
1403       cabc = ca + cb + cc;
1404
1405       ca/=cabc;     cb/=cabc;       cc/=cabc;
1406
1407       u2 = ca * ua + cb * ub + cc * uc;
1408       v2 = ca * va + cb * vb + cc * vc;
1409       break;
1410     }
1411   default: 
1412     {
1413       //-- cout<<" Default dans SectionPointToParameters "<<endl;
1414       break;
1415     }
1416   }
1417
1418
1419 //==================================================================================
1420 // function : RemplitLin
1421 // purpose  : 
1422 //==================================================================================
1423 void IntPatch_PrmPrmIntersection::RemplitLin(const Standard_Integer x1,
1424                                              const Standard_Integer y1,
1425                                              const Standard_Integer z1,
1426                                              const Standard_Integer x2,
1427                                              const Standard_Integer y2,
1428                                              const Standard_Integer z2,
1429                                              IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1430 {
1431   int xg,yg,zg;
1432   xg=x1-x2; if(xg<0) xg=-xg; 
1433   yg=y1-y2; if(yg<0) yg=-yg; 
1434   zg=z1-z2; if(zg<0) zg=-zg; 
1435   if(DansGrille(x1) && DansGrille(y1) && DansGrille(z1)) { 
1436     Standard_Integer t = GrilleInteger(x1,y1,z1); 
1437     Map.Add(t);
1438   }
1439   if(xg<=1 && yg<=1 && zg<=1) return;
1440   xg = (x1+x2)>>1;
1441   yg = (y1+y2)>>1;
1442   zg = (z1+z2)>>1;
1443   RemplitLin(x1,y1,z1,xg,yg,zg,Map);
1444   RemplitLin(x2,y2,z2,xg,yg,zg,Map);
1445 }
1446
1447 //==================================================================================
1448 // function : RemplitTri
1449 // purpose  : 
1450 //==================================================================================
1451 void IntPatch_PrmPrmIntersection::RemplitTri(const Standard_Integer x1,
1452                                              const Standard_Integer y1,
1453                                              const Standard_Integer z1,
1454                                              const Standard_Integer x2,
1455                                              const Standard_Integer y2,
1456                                              const Standard_Integer z2,
1457                                              const Standard_Integer x3,
1458                                              const Standard_Integer y3,
1459                                              const Standard_Integer z3,
1460                                              IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1461
1462   if(x1==x2 && x1==x3 && y1==y2 && y1==y3 && z1==z2 && z1==z3) {
1463     if(DansGrille(x1) && DansGrille(y1) && DansGrille(z1)) { 
1464       Standard_Integer t = GrilleInteger(x1,y1,z1); 
1465       Map.Add(t);
1466     }
1467     return;
1468   }
1469   else { 
1470     Standard_Integer xg=(x1+x2+x3)/3;
1471     Standard_Integer yg=(y1+y2+y3)/3;
1472     Standard_Integer zg=(z1+z2+z3)/3;
1473     if(xg==x1 && yg==y1 && zg==z1) { 
1474       RemplitLin(x1,y1,z1,x2,y2,z2,Map);
1475       RemplitLin(x1,y1,z1,x3,y3,z3,Map);
1476       return;
1477     }
1478     if(xg==x2 && yg==y2 && zg==z2) { 
1479       RemplitLin(x2,y2,z2,x1,y1,z1,Map);
1480       RemplitLin(x2,y2,z2,x3,y3,z3,Map);
1481       return;
1482     }
1483     if(xg==x3 && yg==y3 && zg==z3) { 
1484       RemplitLin(x3,y3,z3,x2,y2,z2,Map);
1485       RemplitLin(x3,y3,z3,x1,y1,z1,Map);
1486       return;
1487     }
1488     if(DansGrille(xg) && DansGrille(yg) && DansGrille(zg)) {
1489       Standard_Integer t = GrilleInteger(xg,yg,zg); 
1490       Map.Add(t);
1491     }
1492     if(xg!=x3 || yg!=y3 || zg!=z3) RemplitTri(x1,y1,z1, x2,y2,z2, xg,yg,zg, Map);
1493     if(xg!=x1 || yg!=y1 || zg!=z1) RemplitTri(xg,yg,zg, x2,y2,z2, x3,y3,z3, Map);
1494     if(xg!=x2 || yg!=y2 || zg!=z2) RemplitTri(x1,y1,z1, xg,yg,zg, x3,y3,z3, Map);
1495   }
1496 }
1497
1498 //==================================================================================
1499 // function : Remplit
1500 // purpose  : 
1501 //==================================================================================
1502 void IntPatch_PrmPrmIntersection::Remplit(const Standard_Integer a,
1503                                           const Standard_Integer b,
1504                                           const Standard_Integer c,
1505                                           IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1506
1507   int iax,iay,iaz,ibx,iby,ibz,icx,icy,icz;
1508   if(a!=-1) Map.Add(a);
1509   if(b!=-1) Map.Add(b);
1510   if(c!=-1) Map.Add(c);
1511
1512   if(a!=-1 && b!=-1 && c!=-1 ) { 
1513     IntegerGrille(a,iax,iay,iaz);
1514     IntegerGrille(b,ibx,iby,ibz);
1515     IntegerGrille(c,icx,icy,icz);
1516     RemplitTri(iax,iay,iaz,ibx,iby,ibz,icx,icy,icz,Map);
1517   }
1518 }
1519
1520
1521
1522 //=======================================================================
1523 //function : Perform
1524 //purpose  : 
1525 //=======================================================================
1526 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
1527                                            const Handle(Adaptor3d_TopolTool)& D1,
1528                                            const Handle(Adaptor3d_HSurface)&    Surf2,
1529                                            const Handle(Adaptor3d_TopolTool)& D2,
1530                                            const Standard_Real   TolTangency,
1531                                            const Standard_Real   Epsilon,
1532                                            const Standard_Real   Deflection,
1533                                            const Standard_Real   Increment,
1534                                            IntSurf_ListOfPntOn2S& LOfPnts,
1535                                            const Standard_Boolean RestrictLine)
1536 {
1537   if (LOfPnts.IsEmpty()){
1538     done = Standard_True;
1539     return;
1540   }
1541
1542   empt = Standard_True;
1543   SLin.Clear();  
1544
1545   Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
1546   Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
1547   Standard_Real U1,U2,V1,V2;
1548
1549   UminLig1 = Surf1->FirstUParameter();
1550   VminLig1 = Surf1->FirstVParameter();
1551   UmaxLig1 = Surf1->LastUParameter();
1552   VmaxLig1 = Surf1->LastVParameter();
1553   UminLig2 = Surf2->FirstUParameter();
1554   VminLig2 = Surf2->FirstVParameter();
1555   UmaxLig2 = Surf2->LastUParameter();
1556   VmaxLig2 = Surf2->LastVParameter();
1557
1558   Standard_Real Periods [4];
1559   Periods[0] = (Surf1->IsUPeriodic())? Surf1->UPeriod() : 0.;
1560   Periods[1] = (Surf1->IsVPeriodic())? Surf1->VPeriod() : 0.;
1561   Periods[2] = (Surf2->IsUPeriodic())? Surf2->UPeriod() : 0.;
1562   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
1563
1564   IntSurf_ListIteratorOfListOfPntOn2S IterLOP1(LOfPnts);
1565   if (Surf1->IsUClosed() || Surf1->IsVClosed() ||
1566       Surf2->IsUClosed() || Surf2->IsVClosed())
1567   {
1568     Standard_Real TolPar = Precision::PConfusion();
1569     IntSurf_ListOfPntOn2S AdditionalPnts;
1570     Standard_Real NewU1, NewV1, NewU2, NewV2;
1571     for(; IterLOP1.More(); IterLOP1.Next())
1572     {
1573       IntSurf_PntOn2S Pnt = IterLOP1.Value();
1574       Pnt.Parameters(U1, V1, U2, V2);
1575       IntSurf_PntOn2S NewPnt;
1576       if (Surf1->IsUClosed())
1577       {
1578         if (Abs(U1 - Surf1->FirstUParameter()) <= TolPar)
1579         {
1580           NewU1 = Surf1->LastUParameter();
1581           NewPnt.SetValue( NewU1, V1, U2, V2 );
1582           AdditionalPnts.Append(NewPnt);
1583         }
1584         else if (Abs(U1 - Surf1->LastUParameter()) <= TolPar)
1585         {
1586           NewU1 = Surf1->FirstUParameter();
1587           NewPnt.SetValue( NewU1, V1, U2, V2 );
1588           AdditionalPnts.Append(NewPnt);
1589         }
1590       }
1591       if (Surf1->IsVClosed())
1592       {
1593         if (Abs(V1 - Surf1->FirstVParameter()) <= TolPar)
1594         {
1595           NewV1 = Surf1->LastVParameter();
1596           NewPnt.SetValue( U1, NewV1, U2, V2 );
1597           AdditionalPnts.Append(NewPnt);
1598         }
1599         else if (Abs(V1 - Surf1->LastVParameter()) <= TolPar)
1600         {
1601           NewV1 = Surf1->FirstVParameter();
1602           NewPnt.SetValue( U1, NewV1, U2, V2 );
1603           AdditionalPnts.Append(NewPnt);
1604         }
1605       }
1606       if (Surf2->IsUClosed())
1607       {
1608         if (Abs(U2 - Surf2->FirstUParameter()) <= TolPar)
1609         {
1610           NewU2 = Surf2->LastUParameter();
1611           NewPnt.SetValue( U1, V1, NewU2, V2);
1612           AdditionalPnts.Append(NewPnt);
1613         }
1614         else if (Abs(U2 - Surf2->LastUParameter()) <= TolPar)
1615         {
1616           NewU2 = Surf2->FirstUParameter();
1617           NewPnt.SetValue( U1, V1, NewU2, V2);
1618           AdditionalPnts.Append(NewPnt);
1619         }
1620       }
1621       if (Surf2->IsVClosed())
1622       {
1623         if (Abs(V2 - Surf2->FirstVParameter()) <= TolPar)
1624         {
1625           NewV2 = Surf2->LastVParameter();
1626           NewPnt.SetValue( U1, V1, U2, NewV2 );
1627           AdditionalPnts.Append(NewPnt);
1628         }
1629         else if (Abs(V2 - Surf2->LastVParameter()) <= TolPar)
1630         {
1631           NewV2 = Surf2->FirstVParameter();
1632           NewPnt.SetValue( U1, V1, U2, NewV2 );
1633           AdditionalPnts.Append(NewPnt);
1634         }
1635       }
1636     }
1637     //Cut repeated points
1638     for (IterLOP1.Initialize(LOfPnts); IterLOP1.More(); IterLOP1.Next())
1639     {
1640       IntSurf_PntOn2S aPnt = IterLOP1.Value();
1641       aPnt.Parameters(U1, V1, U2, V2);
1642       IntSurf_ListIteratorOfListOfPntOn2S iter2(AdditionalPnts);
1643       while (iter2.More())
1644       {
1645         IntSurf_PntOn2S aNewPnt = iter2.Value();
1646         aNewPnt.Parameters(NewU1, NewV1, NewU2, NewV2);
1647         if (Abs(U1 - NewU1) <= TolPar &&
1648             Abs(V1 - NewV1) <= TolPar &&
1649             Abs(U2 - NewU2) <= TolPar &&
1650             Abs(V2 - NewV2) <= TolPar)
1651           AdditionalPnts.Remove(iter2);
1652         else
1653           iter2.Next();
1654       }
1655     }
1656
1657     LOfPnts.Append(AdditionalPnts);
1658   }
1659
1660   for(IterLOP1.Initialize(LOfPnts); IterLOP1.More(); IterLOP1.Next()){
1661     IntSurf_PntOn2S Pnt = IterLOP1.Value();
1662     Pnt.Parameters(U1, V1, U2, V2);
1663     if(U1>UmaxLig1) UmaxLig1=U1;
1664     if(V1>VmaxLig1) VmaxLig1=V1;
1665     if(U2>UmaxLig2) UmaxLig2=U2;
1666     if(V2>VmaxLig2) VmaxLig2=V2;
1667
1668     if(U1<UminLig1) UminLig1=U1;
1669     if(V1<VminLig1) VminLig1=V1;
1670     if(U2<UminLig2) UminLig2=U2;
1671     if(V2<VminLig2) VminLig2=V2; 
1672   }
1673
1674   Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1675
1676   Standard_Integer NbLigCalculee = 0, ver;
1677   Standard_Real pu1,pu2,pv1,pv2, dminiPointLigne;
1678   Standard_Boolean HasStartPoint,RejetLigne;
1679   IntSurf_PntOn2S StartPOn2S;
1680   gp_Pnt Point3dDebut,Point3dFin;
1681
1682   TColStd_Array1OfReal StartParams(1,4);
1683   IntWalk_PWalking PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);  
1684
1685   IntSurf_ListIteratorOfListOfPntOn2S IterLOP2(LOfPnts);
1686   for(; IterLOP2.More(); IterLOP2.Next() ){
1687
1688     IntSurf_PntOn2S cPnt = IterLOP2.Value();
1689     cPnt.Parameters(U1, V1, U2, V2);
1690
1691     StartParams(1) = U1;
1692     StartParams(2) = V1;
1693     StartParams(3) = U2;
1694     StartParams(4) = V2;
1695
1696     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
1697     dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
1698     if(HasStartPoint) {
1699       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
1700       NbLigCalculee = SLin.Length();
1701       Standard_Integer l;
1702       for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
1703         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
1704
1705         if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
1706           dminiPointLigne = 0.0;
1707         }
1708       }// for( l ...
1709
1710       if(dminiPointLigne > SeuildPointLigne) {
1711         PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
1712         if(PW.IsDone()) {
1713           if(PW.NbPoints()>2)
1714           {
1715             //Try to extend the intersection line to boundary, if it is possibly
1716             Standard_Boolean hasBeenAdded = PW.PutToBoundary(Surf1, Surf2);
1717
1718             RejetLigne = Standard_False;
1719             Point3dDebut = PW.Value(1).Value();
1720             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
1721             Point3dFin   = PointFin.Value();
1722             for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
1723               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
1724
1725               // Check end point if it is on existing line.
1726               // Start point is checked before.
1727               if (IsPointOnLine(PointFin, verwline, Deflection)) {
1728                 RejetLigne = Standard_True; 
1729                 break;
1730               }
1731
1732               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
1733               const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
1734               if( (Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) &&
1735                   (Point3dFin.Distance(verPointFin.Value()) <= TolTangency)) 
1736               {
1737                 RejetLigne = Standard_True; 
1738                 break;
1739               }
1740             }
1741
1742             if(RejetLigne)
1743             {
1744               DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
1745             }
1746
1747             if(!RejetLigne) {
1748               IntSurf_TypeTrans trans1,trans2;
1749               Standard_Real locu,locv;
1750               gp_Vec norm1,norm2,d1u,d1v;
1751               gp_Pnt ptbid;
1752               Standard_Integer indextg;
1753               gp_Vec tgline(PW.TangentAtLine(indextg));
1754               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1755               Surf1->D1(locu,locv,ptbid,d1u,d1v);
1756               norm1 = d1u.Crossed(d1v);
1757               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1758               Surf2->D1(locu,locv,ptbid,d1u,d1v);
1759               norm2 = d1u.Crossed(d1v);
1760               if( tgline.DotCross(norm2,norm1) >= 0. ) {
1761                 trans1 = IntSurf_Out;
1762                 trans2 = IntSurf_In;
1763               }
1764               else {
1765                 trans1 = IntSurf_In;
1766                 trans2 = IntSurf_Out;
1767               }
1768
1769               Standard_Real TolTang = TolTangency;
1770               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1771               if (RestrictLine){
1772                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang,hasBeenAdded);
1773                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang,hasBeenAdded);
1774               }
1775
1776               if(wline->NbVertex() == 0) {
1777                 IntPatch_Point vtx;
1778                 IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1779                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1780                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1781                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1782                 vtx.SetParameter(1);
1783                 wline->AddVertex(vtx);
1784
1785                 POn2S = PW.Line()->Value(wline->NbPnts());
1786                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1787                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
1788                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1789                 vtx.SetParameter(wline->NbPnts());
1790                 wline->AddVertex(vtx);
1791               }              
1792
1793               SeveralWlinesProcessing(Surf1, Surf2, SLin, Periods, trans1, trans2, TolTang, wline);
1794
1795               AddWLine(SLin, wline, Deflection);
1796               empt = Standard_False;
1797             }// !RejetLigne
1798           }// PW points > 2
1799         }// done is True
1800       }// dminiPointLigne > SeuildPointLigne
1801     }// HasStartPoint  
1802   }// for( IterLOP ...
1803   done = Standard_True;
1804   return;      
1805 }
1806 //=======================================================================
1807 //function : Perform
1808 //purpose  : 
1809 //=======================================================================
1810 void IntPatch_PrmPrmIntersection::Perform(const Handle(Adaptor3d_HSurface)&    Surf1,
1811                                           const Handle(Adaptor3d_TopolTool)& D1,
1812                                           const Handle(Adaptor3d_HSurface)&    Surf2,
1813                                           const Handle(Adaptor3d_TopolTool)& D2,
1814                                           const Standard_Real   U1Depart,
1815                                           const Standard_Real   V1Depart,
1816                                           const Standard_Real   U2Depart,
1817                                           const Standard_Real   V2Depart,
1818                                           const Standard_Real   TolTangency,
1819                                           const Standard_Real   Epsilon,
1820                                           const Standard_Real   Deflection,
1821                                           const Standard_Real   Increment)
1822 {
1823   //    Standard_Integer NbU1 = D1->NbSamplesU();
1824   //    Standard_Integer NbV1 = D1->NbSamplesV();
1825   //    Standard_Integer NbU2 = D2->NbSamplesU();
1826   //    Standard_Integer NbV2 = D2->NbSamplesV();
1827
1828   //-- Traitement des Lignes de sections
1829   empt = Standard_True;
1830   done = Standard_True;
1831   SLin.Clear();  
1832
1833   //------------------------------------------------------------
1834
1835   Standard_Real pu1,pu2,pv1,pv2;
1836
1837   TColStd_Array1OfReal StartParams(1,4);
1838
1839   //    Standard_Integer MaxOscill = NbU1;
1840   //    if(MaxOscill < NbU2) MaxOscill=NbU2;
1841   //    if(MaxOscill < NbV1) MaxOscill=NbV1;
1842   //    if(MaxOscill < NbV2) MaxOscill=NbV2;
1843
1844   //    Standard_Real nIncrement=Increment;
1845   //    if(MaxOscill>10) { 
1846   //  #ifdef OCCT_DEBUG
1847   //      cout<<"\n IntPatch_PrmPrmIntersection.gxx : Increment:"<<Increment<<" -> "<<Increment/(0.5*MaxOscill)<<endl;
1848   //  #endif
1849   //      nIncrement/=0.5*MaxOscill;
1850   //    }
1851
1852   IntWalk_PWalking PW(Surf1,Surf2,
1853     TolTangency,
1854     Epsilon,
1855     Deflection,
1856     Increment); //nIncrement);
1857
1858
1859   //Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
1860   //Standard_Real    incidence;
1861   //Standard_Real    dminiPointLigne;
1862
1863   Standard_Boolean HasStartPoint;//,RejetLigne;
1864
1865   IntSurf_PntOn2S StartPOn2S;
1866
1867   //Standard_Integer ver;
1868
1869   gp_Pnt Point3dDebut,Point3dFin;
1870
1871   //------------------------------------------------------------
1872
1873   StartParams(1) = U1Depart;
1874   StartParams(2) = V1Depart;
1875   StartParams(3) = U2Depart;
1876   StartParams(4) = V2Depart;
1877
1878   //-----------------------------------------------------------------------
1879   //-- Calcul du premier point de cheminement a partir du point approche --
1880   //-----------------------------------------------------------------------
1881   HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S); 
1882   if(HasStartPoint) { 
1883     //-------------------------------------------------
1884     //-- Un point a ete trouve                       --
1885     //-- On verifie qu il n appartient pas           --
1886     //--  a une ligne de cheminement deja calculee.  --
1887     //-------------------------------------------------
1888
1889     PW.Perform(StartParams);
1890     if(PW.IsDone()) {
1891
1892       Point3dDebut = PW.Value(1).Value();
1893       Point3dFin   = PW.Value(PW.NbPoints()).Value();
1894
1895       IntSurf_TypeTrans trans1,trans2;
1896       Standard_Real locu,locv;
1897       gp_Vec norm1,norm2,d1u,d1v;
1898       gp_Pnt ptbid;
1899       Standard_Integer indextg;
1900       gp_Vec tgline(PW.TangentAtLine(indextg));
1901       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1902       Surf1->D1(locu,locv,ptbid,d1u,d1v);
1903       norm1 = d1u.Crossed(d1v);
1904       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1905       Surf2->D1(locu,locv,ptbid,d1u,d1v);
1906       norm2 = d1u.Crossed(d1v);
1907       if (tgline.DotCross(norm2,norm1)>0.) {
1908         trans1 = IntSurf_Out;
1909         trans2 = IntSurf_In;
1910       }
1911       else {
1912         trans1 = IntSurf_In;
1913         trans2 = IntSurf_Out;
1914       }
1915
1916
1917
1918       Standard_Real TolTang = TolTangency;
1919       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1920       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1921       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1922
1923       //---------------
1924       if(wline->NbVertex() == 0) {
1925         IntPatch_Point vtx;
1926         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1927         POn2S.Parameters(pu1,pv1,pu2,pv2);
1928         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1929         vtx.SetParameters(pu1,pv1,pu2,pv2);
1930         vtx.SetParameter(1);
1931         wline->AddVertex(vtx);
1932
1933         POn2S = PW.Line()->Value(wline->NbPnts());
1934         POn2S.Parameters(pu1,pv1,pu2,pv2);
1935         vtx.SetValue(Point3dFin,TolTang,Standard_False);
1936         vtx.SetParameters(pu1,pv1,pu2,pv2);
1937         vtx.SetParameter(wline->NbPnts());
1938         wline->AddVertex(vtx);
1939       }
1940
1941       //---------------
1942       SLin.Append(wline);
1943       empt = Standard_False;
1944
1945     }
1946   }
1947 }
1948 //==================================================================================
1949 // function : AdjustOnPeriodic
1950 // purpose  : 
1951 //==================================================================================
1952 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
1953                       const Handle(Adaptor3d_HSurface)& Surf2,
1954                       IntPatch_SequenceOfLine& aSLin)
1955 {
1956   Standard_Boolean bIsPeriodic[4], bModified, bIsNull, bIsPeriod;
1957   Standard_Integer i, j, k, aNbLines, aNbPx, aIndx, aIndq;
1958   Standard_Real aPeriod[4], dPeriod[4], ux[4], uq[4], aEps, du;
1959   //
1960   aEps=Precision::Confusion();
1961   //
1962   for (k=0; k<4; ++k) {
1963     aPeriod[k]=0.;
1964   }
1965   //
1966   bIsPeriodic[0]=Surf1->IsUPeriodic();
1967   bIsPeriodic[1]=Surf1->IsVPeriodic();
1968   bIsPeriodic[2]=Surf2->IsUPeriodic();
1969   bIsPeriodic[3]=Surf2->IsVPeriodic();
1970   //
1971   if (bIsPeriodic[0]){
1972     aPeriod[0]=Surf1->UPeriod();
1973   }
1974   if (bIsPeriodic[1]){
1975     aPeriod[1]=Surf1->VPeriod();
1976   }
1977   if (bIsPeriodic[2]){
1978     aPeriod[2]=Surf2->UPeriod();
1979   }
1980   if (bIsPeriodic[3]){
1981     aPeriod[3]=Surf2->VPeriod();
1982   }
1983   //
1984   for (k=0; k<4; ++k) {
1985     dPeriod[k]=0.25*aPeriod[k];
1986   }
1987   //
1988   aNbLines=aSLin.Length();
1989   for (i=1; i<=aNbLines; ++i) {
1990     Handle(IntPatch_WLine) aIL=Handle(IntPatch_WLine)::DownCast(aSLin.Value(i));
1991     Handle(IntSurf_LineOn2S) aL=aIL->Curve();
1992
1993     aNbPx=aL->NbPoints();
1994     if (aNbPx<10) {
1995       continue;
1996     }
1997     //
1998     for (j=0; j<2; ++j) {
1999       bModified=Standard_False;
2000       aIndx=1;
2001       aIndq=2;
2002       if (j) {
2003         aIndx=aNbPx;
2004         aIndq=aNbPx-1;
2005       }
2006       //
2007       const IntSurf_PntOn2S& aPSx=aL->Value(aIndx);
2008       const IntSurf_PntOn2S& aPSq=aL->Value(aIndq);
2009       //
2010       aPSx.Parameters(ux[0], ux[1], ux[2], ux[3]);
2011       aPSq.Parameters(uq[0], uq[1], uq[2], uq[3]);
2012       //
2013       for (k=0; k<4; ++k) {
2014         bIsNull=Standard_False;
2015         bIsPeriod=Standard_False;
2016         //
2017         if (!bIsPeriodic[k]) {
2018           continue;
2019         }
2020         //
2021         if (fabs(ux[k])<aEps) {
2022           bModified=Standard_True;
2023           bIsNull=Standard_True;
2024         }
2025         //
2026         else if (fabs(ux[k]-aPeriod[k])<aEps) {
2027           bModified=Standard_True;
2028           bIsPeriod=Standard_True;
2029         }
2030         //
2031         if (bModified) {
2032           du=fabs(ux[k]-uq[k]);
2033           if (du > dPeriod[k]) {
2034             if(bIsNull){
2035               ux[k]=aPeriod[k];
2036             }
2037             if(bIsPeriod) {
2038               ux[k]=0.;
2039             }
2040           }
2041         }
2042       }//for (k=0; k<4; ++k) 
2043       if (bModified) {
2044         IntSurf_PntOn2S aPntOn2S;
2045         //
2046         aPntOn2S=aPSx;  
2047         aPntOn2S.SetValue(ux[0], ux[1], ux[2], ux[3]);
2048         aL->Value(aIndx, aPntOn2S);
2049       }
2050     }//for (j=0; j<1; ++j) {
2051   }//for (i=1; i<=aNbLines; ++i)
2052 }
2053
2054 //==================================================================================
2055 // function : MakeNewPoint
2056 // purpose  : 
2057 //==================================================================================
2058 IntSurf_PntOn2S MakeNewPoint(const IntSurf_PntOn2S& replacePnt,
2059                              const IntSurf_PntOn2S& oldPnt,
2060                              const Standard_Real* Periods)
2061 {
2062   IntSurf_PntOn2S NewPoint;
2063   NewPoint.SetValue(replacePnt.Value());
2064
2065   Standard_Real OldParams[4], NewParams[4];
2066   oldPnt.Parameters(OldParams[0], OldParams[1], OldParams[2], OldParams[3]);
2067   replacePnt.Parameters(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
2068
2069   Standard_Integer i;
2070   for (i = 0; i < 4; i++)
2071     if (Periods[i] != 0.)
2072     {
2073       if (Abs(NewParams[i] - OldParams[i]) >= 0.5*Periods[i])
2074       {
2075         if (NewParams[i] < OldParams[i])
2076           NewParams[i] += Periods[i];
2077         else
2078           NewParams[i] -= Periods[i];
2079       }
2080     }
2081
2082     NewPoint.SetValue(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
2083     return NewPoint;
2084 }
2085
2086 //==================================================================================
2087 // function : Perform
2088 // purpose  : base SS Int. function
2089 //==================================================================================
2090 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
2091                                            const Handle(Adaptor3d_TopolTool)& D1,
2092                                            const Handle(Adaptor3d_HSurface)& Surf2,
2093                                            const Handle(Adaptor3d_TopolTool)& D2,
2094                                            const Standard_Real   TolTangency,
2095                                            const Standard_Real   Epsilon,
2096                                            const Standard_Real   Deflection,
2097                                            const Standard_Real   Increment,
2098                                            const Standard_Boolean ClearFlag) 
2099 {
2100   Standard_Integer Limit = 2500;
2101   Standard_Integer NbU1 = 10, NbV1 = 10, NbU2 = 10, NbV2 = 10;
2102   //
2103   D1->SamplePnts(Deflection, NbU1, NbV1);
2104   D2->SamplePnts(Deflection, NbU2, NbV2);
2105   //
2106   NbU1 = D1->NbSamplesU();
2107   NbV1 = D1->NbSamplesV();
2108   NbU2 = D2->NbSamplesU();
2109   NbV2 = D2->NbSamplesV();
2110
2111   TColStd_Array1OfReal anUpars1(1, NbU1), aVpars1(1, NbV1);
2112   TColStd_Array1OfReal anUpars2(1, NbU2), aVpars2(1, NbV2);
2113   //
2114   D1->UParameters(anUpars1); 
2115   D1->VParameters(aVpars1);
2116   D2->UParameters(anUpars2); 
2117   D2->VParameters(aVpars2);
2118
2119   Standard_Real Periods [4];
2120   Periods[0] = (Surf1->IsUPeriodic())? Surf1->UPeriod() : 0.;
2121   Periods[1] = (Surf1->IsVPeriodic())? Surf1->VPeriod() : 0.;
2122   Periods[2] = (Surf2->IsUPeriodic())? Surf2->UPeriod() : 0.;
2123   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
2124
2125   //---------------------------------------------
2126   if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit))
2127   {
2128     empt = Standard_True;
2129     if (ClearFlag)
2130     {
2131       SLin.Clear();
2132     }
2133     //
2134     IntPolyh_Intersection* pInterference = NULL;
2135
2136     if ( D1->IsUniformSampling() || D2->IsUniformSampling() )
2137     {
2138       pInterference = new IntPolyh_Intersection(Surf1,NbU1,NbV1,Surf2,NbU2,NbV2);
2139     }
2140     else
2141     {
2142       pInterference = new IntPolyh_Intersection(Surf1, anUpars1, aVpars1, 
2143         Surf2, anUpars2, aVpars2 );
2144     }
2145
2146     if ( !pInterference )
2147     {
2148       done = Standard_False;
2149       return;
2150     }
2151     //
2152     IntPolyh_Intersection& Interference = *pInterference;
2153     //
2154     done = Interference.IsDone();
2155     if( !done )
2156     {
2157       if (pInterference)
2158       {
2159         delete pInterference;
2160         pInterference = NULL;
2161       }
2162
2163       return;
2164     }
2165
2166     Standard_Integer nbLigSec = Interference.NbSectionLines();
2167     Standard_Integer nbTanZon = Interference.NbTangentZones();
2168     Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
2169
2170     Standard_Integer NbLigCalculee = 0, ver;
2171     Standard_Real pu1,pu2,pv1,pv2, incidence, dminiPointLigne;
2172     Standard_Boolean HasStartPoint = Standard_False, RejectLine = Standard_False;
2173     IntSurf_PntOn2S StartPOn2S;
2174     gp_Pnt Point3dDebut,Point3dFin;
2175
2176     TColStd_Array1OfReal StartParams(1,4);
2177     IntWalk_PWalking PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);
2178
2179     if(nbLigSec>=1)
2180     {
2181       Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
2182       for(Standard_Integer ls=1; ls<=nbLigSec; ++ls)
2183       {
2184         TabL[ls]=ls;
2185       }
2186       //----------------------------------------1.1
2187       {
2188         Standard_Boolean triok;
2189         Standard_Integer nb_A, nb_B, tyu;
2190         do
2191         {
2192           triok=Standard_True;
2193           for(Standard_Integer b = 2; b <= nbLigSec; ++b )
2194           {
2195             nb_B = Interference.NbPointsInLine(TabL[b]);
2196             nb_A = Interference.NbPointsInLine(TabL[b-1]);
2197
2198             if( nb_B > nb_A )
2199             {
2200               tyu=TabL[b];
2201               TabL[b]=TabL[b-1];
2202               TabL[b-1]=tyu;
2203               triok=Standard_False;
2204             }
2205           }
2206         }
2207         while(triok==Standard_False);
2208       }
2209
2210       //----------------------------------------
2211       // 1.2 For the line "ls" get 2D-bounds U,V for surfaces 1,2
2212       //
2213       for(Standard_Integer ls = 1; ls <= nbLigSec; ++ls)
2214       {
2215         Standard_Integer nbp = Interference.NbPointsInLine(TabL[ls]);
2216         if (!nbp)
2217         {
2218           continue;
2219         }
2220         //
2221         Standard_Integer *TabPtDep = new Standard_Integer [nbp+1];
2222         for(Standard_Integer ilig = 1; ilig <= nbp; ++ilig )
2223         {
2224           TabPtDep[ilig]=0;
2225         }
2226         //
2227         Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2228         Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2229         Standard_Real _x,_y,_z;
2230         //
2231         Interference.GetLinePoint(TabL[ls], 1, _x, _y, _z,
2232           UminLig1, VminLig1, UminLig2, VminLig2, 
2233           incidence);
2234
2235         UmaxLig1=UminLig1;
2236         VmaxLig1=VminLig1;
2237         UmaxLig2=UminLig2;
2238         VmaxLig2=VminLig2;
2239         //
2240         for(Standard_Integer ilig = 2; ilig <= nbp; ilig++ )
2241         {
2242           Standard_Real U1, U2, V1, V2;
2243           Interference.GetLinePoint(TabL[ls],ilig,_x,_y,_z,U1,V1,U2,V2,incidence);
2244           //
2245           if(U1>UmaxLig1) UmaxLig1=U1;
2246           if(V1>VmaxLig1) VmaxLig1=V1;
2247           if(U2>UmaxLig2) UmaxLig2=U2;
2248           if(V2>VmaxLig2) VmaxLig2=V2;
2249           //
2250           if(U1<UminLig1) UminLig1=U1;
2251           if(V1<VminLig1) VminLig1=V1;
2252           if(U2<UminLig2) UminLig2=U2;
2253           if(V2<VminLig2) VminLig2=V2;
2254         }//for( ilig = 2; ilig <= nbp; ilig++ ) { 
2255         //
2256         //----------------------------------------
2257         // 1.3
2258         Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
2259         Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
2260         Standard_Boolean lignetrouvee=Standard_False;
2261         const Standard_Integer NbDePointsDeDepartDuChmLimit = 5;
2262         //
2263         do
2264         {
2265           NombreDePointsDeDepartDuCheminement++;
2266           switch (NombreDePointsDeDepartDuCheminement)
2267           {
2268           case 1:
2269             nbps2 = (nbp > 1) ? nbp/2 : 1;
2270             if(nbp<3) 
2271               NombreDePointsDeDepartDuCheminement = NbDePointsDeDepartDuChmLimit;
2272
2273             break;
2274           case 2:
2275             nbps2 = 1;
2276             break;
2277           case 3:
2278             nbps2 = nbp-1;
2279             break;
2280
2281           case 4:
2282             nbps2 = 3 * nbp / 4;
2283             break;
2284
2285           case 5:
2286             nbps2 = nbp / 4;
2287             break;
2288           default:
2289             nbps2 = NombreDePointsDeDepartDuCheminement-3;
2290             NombreDePointsDeDepartDuCheminement++;
2291           }
2292
2293           //
2294           if(TabPtDep[nbps2] == 0)
2295           {
2296             Standard_Real U1, U2, V1, V2;
2297
2298             TabPtDep[nbps2] = 1;
2299             Interference.GetLinePoint(TabL[ls],nbps2,_x,_y,_z,U1,V1,U2,V2,incidence);
2300
2301             StartParams(1) = U1;
2302             StartParams(2) = V1;
2303             StartParams(3) = U2;
2304             StartParams(4) = V2;
2305
2306             HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
2307             dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
2308             if(HasStartPoint)
2309             {
2310               StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2311               NbLigCalculee = SLin.Length();
2312               Standard_Integer l;
2313               for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++)
2314               {
2315                 const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2316
2317                 if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2318                 {
2319                   dminiPointLigne = 0.0;
2320                 }
2321               }// for( l ...
2322
2323               if(dminiPointLigne > SeuildPointLigne)
2324               {
2325                 PW.Perform(StartParams, UminLig1, VminLig1, UminLig2, VminLig2,
2326                   UmaxLig1, VmaxLig1, UmaxLig2, VmaxLig2);
2327
2328                 //
2329                 Standard_Boolean bPWIsDone;
2330                 Standard_Integer iPWNbPoints, aNbPointsVer;
2331                 Standard_Real aD11, aD12, aD21, aD22, aDx;
2332                 //
2333                 bPWIsDone=PW.IsDone();
2334
2335                 if(bPWIsDone)
2336                 {
2337                   iPWNbPoints=PW.NbPoints();
2338                   //
2339                   if( iPWNbPoints > 2 )
2340                   {
2341                     //Try to extend the intersection line to boundary, if it is possibly
2342                     Standard_Boolean hasBeenAdded = PW.PutToBoundary(Surf1, Surf2);
2343
2344                     const Standard_Integer aMinNbPoints = 40;
2345                     if(iPWNbPoints < aMinNbPoints)
2346                     {
2347                       hasBeenAdded = 
2348                         PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints) || hasBeenAdded;
2349                       iPWNbPoints = PW.NbPoints();
2350                     }
2351                     
2352                     RejectLine = Standard_False;
2353                     Point3dDebut = PW.Value(1).Value();
2354                     Point3dFin   = PW.Value(iPWNbPoints).Value();
2355                     for( ver = 1; (!RejectLine) && (ver<= NbLigCalculee); ++ver)
2356                     {
2357                       const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2358                       //
2359                       aNbPointsVer=verwline->NbPnts();
2360                       if (aNbPointsVer<3)
2361                       {
2362                         continue;
2363                       }
2364                       //
2365                       const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2366                       const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
2367                       //xf
2368                       const gp_Pnt& aP21=verPointDebut.Value();
2369                       const gp_Pnt& aP22=verPointFin.Value();
2370                       //
2371                       aD11=Point3dDebut.Distance(aP21);
2372                       aD12=Point3dDebut.Distance(aP22);
2373                       aD21=Point3dFin.Distance(aP21);
2374                       aD22=Point3dFin.Distance(aP22);
2375                       //
2376                       if((aD11<=TolTangency && aD22<=TolTangency) ||
2377                         (aD12<=TolTangency && aD21<=TolTangency))
2378                       {
2379                         Standard_Integer m, mx;
2380                         //
2381                         mx=aNbPointsVer/2;
2382                         if (aNbPointsVer%2)
2383                         {
2384                           ++mx; 
2385                         }
2386                         //
2387                         const gp_Pnt& aPx=verwline->Point(mx).Value();
2388                         for(m=1; m<iPWNbPoints; ++m)
2389                         {
2390                           const gp_Pnt& aP1=PW.Value(m).Value();
2391                           const gp_Pnt& aP2=PW.Value(m+1).Value();
2392                           gp_Vec aVec12(aP1, aP2);
2393                           if (aVec12.SquareMagnitude()<1.e-20)
2394                           {
2395                             continue;
2396                           }
2397
2398                           //
2399                           gp_Dir aDir12(aVec12);
2400                           gp_Lin aLin12(aP1, aDir12);
2401                           aDx=aLin12.Distance(aPx);
2402
2403                           //modified by NIZNHY-PKV Tue May 10 11:08:07 2011f
2404                           if (aDx<=2.*Epsilon)
2405                           {
2406                             //if (aDx<=TolTangency) {
2407                             //modified by NIZNHY-PKV Tue May 10 11:08:13 2011t
2408
2409                             RejectLine = Standard_True;
2410                             ver--;
2411                             break;
2412                           }
2413                         }//for(m=1; m<iPWNbPoints; ++m){
2414                       }//if((aD11<=TolTangency && aD22<=TolTangency) ||...
2415                     }// for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2416                     //
2417
2418                     if(RejectLine)
2419                     {
2420                       DublicateOfLinesProcessing(PW, ver, SLin, RejectLine);
2421                     }
2422
2423                     if(!RejectLine)
2424                     {
2425                       IntSurf_TypeTrans trans1,trans2;
2426                       Standard_Real locu,locv;
2427                       gp_Vec norm1,norm2,d1u,d1v;
2428                       gp_Pnt ptbid;
2429                       Standard_Integer indextg;
2430                       gp_Vec tgline(PW.TangentAtLine(indextg));
2431                       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2432                       Surf1->D1(locu,locv,ptbid,d1u,d1v);
2433                       norm1 = d1u.Crossed(d1v);
2434                       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2435                       Surf2->D1(locu,locv,ptbid,d1u,d1v);
2436                       norm2 = d1u.Crossed(d1v);
2437                       if( tgline.DotCross(norm2,norm1) >= 0. )
2438                       {
2439                         trans1 = IntSurf_Out;
2440                         trans2 = IntSurf_In;
2441                       }
2442                       else
2443                       {
2444                         trans1 = IntSurf_In;
2445                         trans2 = IntSurf_Out;
2446                       }
2447
2448                       Standard_Real TolTang = TolTangency;
2449                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2450                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang,hasBeenAdded);
2451                       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang,hasBeenAdded);
2452
2453                       if(wline->NbVertex() == 0)
2454                       {
2455                         IntPatch_Point vtx;
2456                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2457                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2458                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2459                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2460                         vtx.SetParameter(1);
2461                         wline->AddVertex(vtx);
2462
2463                         POn2S = PW.Line()->Value(wline->NbPnts());
2464                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2465                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
2466                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2467                         vtx.SetParameter(wline->NbPnts());
2468                         wline->AddVertex(vtx);
2469                       }
2470
2471                       lignetrouvee = Standard_True;
2472
2473                       SeveralWlinesProcessing(Surf1, Surf2, SLin, Periods, trans1, trans2, TolTang, wline);
2474
2475                       AddWLine(SLin, wline, Deflection);
2476                       empt = Standard_False;
2477                     }// !RejetLigne
2478                   }// PW points > 2
2479                 }// done is True
2480               }// dminiPointLigne > SeuildPointLigne
2481             }// HasStartPoint
2482           }// if TabPtDep[nbps2] == 0
2483         } while(nbp>5 && !( (NombreDePointsDeDepartDuCheminement >= NbDePointsDeDepartDuChmLimit && lignetrouvee) || 
2484           (NombreDePointsDeDepartDuCheminement-3 >= nbp && (!lignetrouvee))));
2485         delete [] TabPtDep;
2486       }// for( ls ...
2487
2488       delete [] TabL;
2489
2490     }// if nbLigSec >= 1
2491     //
2492     AdjustOnPeriodic(Surf1, Surf2, SLin);
2493     //
2494
2495     //--------------------------------------------------------------------
2496     //-- Calcul des parametres approches a partir des Zones De Tangence --
2497     //--------------------------------------------------------------------
2498     Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2499     Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2500
2501     UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
2502     UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
2503
2504     // NbPointsInTangentZone always == 1 (eap)
2505
2506     Standard_Integer z;
2507     for( z=1; z <= nbTanZon; z++)
2508     { 
2509       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2510       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) {
2511       Standard_Integer pz=1;
2512       Standard_Real _x,_y,_z;
2513       Standard_Real U1, U2, V1, V2;
2514       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2515
2516       if(U1>UmaxLig1) UmaxLig1=U1;
2517       if(V1>VmaxLig1) VmaxLig1=V1;
2518       if(U2>UmaxLig2) UmaxLig2=U2;
2519       if(V2>VmaxLig2) VmaxLig2=V2;
2520
2521       if(U1<UminLig1) UminLig1=U1;
2522       if(V1<VminLig1) VminLig1=V1;
2523       if(U2<UminLig2) UminLig2=U2;
2524       if(V2<VminLig2) VminLig2=V2;
2525       //}
2526     }
2527
2528     for(z=1; z <= nbTanZon; z++)
2529     {
2530       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2531       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) { 
2532       Standard_Integer pz=1;
2533       Standard_Real _x,_y,_z;
2534       Standard_Real U1, U2, V1, V2;
2535       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2536
2537       StartParams(1) = U1;
2538       StartParams(2) = V1;
2539       StartParams(3) = U2;
2540       StartParams(4) = V2;
2541
2542       //-----------------------------------------------------------------------
2543       //-- Calcul du premier point de cheminement a partir du point approche --
2544       //-----------------------------------------------------------------------
2545       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);     
2546       if(HasStartPoint)
2547       {
2548         //-------------------------------------------------
2549         //-- Un point a ete trouve                       --
2550         //-- On verifie qu il n appartient pas           --
2551         //--  a une ligne de cheminement deja calculee.  --
2552         //-------------------------------------------------
2553         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2554
2555         NbLigCalculee = SLin.Length();
2556         dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2557
2558         for(Standard_Integer l=1; 
2559           (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2560           l++)
2561         {
2562           const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2563
2564           if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2565           {
2566             dminiPointLigne = 0.0;
2567           }
2568         }
2569
2570         //-- Fin d exploration des lignes
2571         if(dminiPointLigne > SeuildPointLigne)
2572         {
2573           //---------------------------------------------------
2574           //-- Le point de depart du nouveau cheminement     --
2575           //-- n est present dans aucune ligne deja calculee.--
2576           //---------------------------------------------------
2577           PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,
2578             UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2579
2580           if(PW.IsDone())
2581           {
2582             if(PW.NbPoints()>2)
2583             { 
2584               const Standard_Integer aMinNbPoints = 40;
2585               if(PW.NbPoints() < aMinNbPoints)
2586               {
2587                 PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints);
2588               }
2589
2590               //-----------------------------------------------
2591               //-- Verification a posteriori : 
2592               //-- On teste si le point de depart et de fin de 
2593               //-- la ligne de cheminement est present dans une 
2594               //-- autre ligne . 
2595               //-----------------------------------------------
2596               RejectLine = Standard_False;
2597               Point3dDebut = PW.Value(1).Value();
2598               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2599               Point3dFin   = PointFin.Value();
2600
2601               for(ver=1 ; (!RejectLine) && (ver<= NbLigCalculee) ; ver++)
2602               {
2603                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2604                 //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2605
2606                 // Check end point if it is on existing line.
2607                 // Start point is checked before.
2608                 if (IsPointOnLine(PointFin, verwline, Deflection))
2609                 {
2610                   RejectLine = Standard_True; 
2611                   break;
2612                 }
2613
2614                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2615                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2616                 if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
2617                 {
2618                   RejectLine = Standard_True; 
2619                 }
2620                 else
2621                 {
2622                   if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
2623                   {
2624                     RejectLine = Standard_True; 
2625                   }
2626                 }
2627               }
2628
2629               if(!RejectLine)
2630               { 
2631                 IntSurf_TypeTrans trans1,trans2;
2632                 Standard_Real locu,locv;
2633                 gp_Vec norm1,norm2,d1u,d1v;
2634                 gp_Pnt ptbid;
2635                 Standard_Integer indextg;
2636                 gp_Vec tgline(PW.TangentAtLine(indextg));
2637                 PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2638                 Surf1->D1(locu,locv,ptbid,d1u,d1v);
2639                 norm1 = d1u.Crossed(d1v);
2640                 PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2641                 Surf2->D1(locu,locv,ptbid,d1u,d1v);
2642                 norm2 = d1u.Crossed(d1v);
2643                 if (tgline.DotCross(norm2,norm1)>0.)
2644                 {
2645                   trans1 = IntSurf_Out;
2646                   trans2 = IntSurf_In;
2647                 }
2648                 else
2649                 {
2650                   trans1 = IntSurf_In;
2651                   trans2 = IntSurf_Out;
2652                 }
2653
2654                 Standard_Real TolTang = TolTangency;
2655                 Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2656                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2657                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2658
2659                 //---------------
2660                 if(wline->NbVertex() == 0)
2661                 {
2662                   IntPatch_Point vtx;
2663                   IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2664                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2665                   vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2666                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2667                   vtx.SetParameter(1);
2668                   wline->AddVertex(vtx);
2669
2670                   POn2S = PW.Line()->Value(wline->NbPnts());
2671                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2672                   vtx.SetValue(Point3dFin,TolTang,Standard_False);
2673                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2674                   vtx.SetParameter(wline->NbPnts());
2675                   wline->AddVertex(vtx);
2676                 }
2677
2678                 //---------------
2679                 AddWLine(SLin, wline, Deflection);
2680                 empt = Standard_False;
2681               }
2682               else
2683               {
2684                 //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2685               }
2686               //------------------------------------------------------------            
2687             }
2688           }  //--  le cheminement a reussi (done a True)
2689         }  //--  le point approche ne renvoie pas sur une ligne existante
2690       } //-- Si HasStartPoint
2691       //} //-- Boucle Sur les Points de la Tangent Zone
2692     } //-- Boucle sur Les Tangent Zones
2693
2694     if ( pInterference )
2695     {
2696       delete pInterference;
2697       pInterference = NULL;
2698     }
2699
2700     return;
2701   }// if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
2702
2703   Handle(IntSurf_LineOn2S) LOn2S = new IntSurf_LineOn2S();
2704   PointDepart( LOn2S, Surf1, NbU1, NbV1, Surf2, NbU2, NbV2 );
2705   empt = Standard_True;
2706   done = Standard_True;
2707   SLin.Clear();  
2708
2709   Standard_Integer NbLigCalculee = 0;
2710   Standard_Real U1,U2,V1,V2;
2711   Standard_Real pu1,pu2,pv1,pv2;
2712
2713   TColStd_Array1OfReal StartParams(1,4);
2714   Standard_Integer MaxOscill = NbU1;
2715   if(MaxOscill < NbU2) MaxOscill=NbU2;
2716   if(MaxOscill < NbV1) MaxOscill=NbV1;
2717   if(MaxOscill < NbV2) MaxOscill=NbV2;
2718
2719   Standard_Real nIncrement=Increment;
2720   //if(MaxOscill>10)
2721   //nIncrement/=0.5*MaxOscill;
2722
2723   IntWalk_PWalking PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,nIncrement);
2724   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
2725   Standard_Real    dminiPointLigne;
2726   Standard_Boolean HasStartPoint,RejetLigne;
2727   IntSurf_PntOn2S StartPOn2S;
2728   Standard_Integer ver;
2729   gp_Pnt Point3dDebut,Point3dFin;
2730
2731   //------------------------------------------------------------
2732   //-- Calcul des parametres approches a partir des Zones De Tangence --
2733   //--------------------------------------------------------------------
2734   Standard_Integer nbTanZon = LOn2S->NbPoints();
2735   for(Standard_Integer z=1; z <= nbTanZon; z++) { 
2736     const IntSurf_PntOn2S& POn2S = LOn2S->Value(z);
2737     POn2S.Parameters(U1,V1,U2,V2);
2738     StartParams(1) = U1;
2739     StartParams(2) = V1;
2740     StartParams(3) = U2;
2741     StartParams(4) = V2;
2742
2743     //-----------------------------------------------------------------------
2744     //-- Calcul du premier point de cheminement a partir du point approche --
2745     //-----------------------------------------------------------------------
2746     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);       
2747     if(HasStartPoint)
2748     { 
2749       //-------------------------------------------------
2750       //-- Un point a ete trouve                       --
2751       //-- On verifie qu il n appartient pas           --
2752       //--  a une ligne de cheminement deja calculee.  --
2753       //-------------------------------------------------
2754       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2755
2756       NbLigCalculee = SLin.Length();
2757       dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2758
2759       for(Standard_Integer l=1; 
2760         (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2761         l++)
2762       {
2763         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2764
2765         if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2766         {
2767           dminiPointLigne = 0.0;
2768         }
2769       }
2770
2771       //-- Fin d exploration des lignes
2772       if(dminiPointLigne > SeuildPointLigne)
2773       { 
2774         //---------------------------------------------------
2775         //-- Le point de depart du nouveau cheminement     --
2776         //-- n est present dans aucune ligne deja calculee.--
2777         //---------------------------------------------------
2778         PW.Perform(StartParams);
2779         if(PW.IsDone())
2780         {
2781           if(PW.NbPoints()>2)
2782           {
2783             //-----------------------------------------------
2784             //-- Verification a posteriori : 
2785             //-- On teste si le point de depart et de fin de 
2786             //-- la ligne de cheminement est present dans une 
2787             //-- autre ligne . 
2788             //-----------------------------------------------
2789             RejetLigne = Standard_False;
2790             Point3dDebut = PW.Value(1).Value();
2791             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2792             Point3dFin   = PointFin.Value();
2793
2794             for(ver=1 ; ver<= NbLigCalculee ; ver++)
2795             {
2796               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2797               //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2798
2799               // Check end point if it is on existing line.
2800               // Start point is checked before.
2801               if (IsPointOnLine(PointFin, verwline, Deflection))
2802               {
2803                 RejetLigne = Standard_True; 
2804                 break;
2805               }
2806
2807               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2808               const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2809               if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
2810                   (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
2811               {
2812                 RejetLigne = Standard_True; 
2813                 break;
2814               }
2815             }
2816
2817             if(RejetLigne)
2818             {
2819               DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
2820             }
2821
2822             if(!RejetLigne)
2823             {
2824               IntSurf_TypeTrans trans1,trans2;
2825               Standard_Real locu,locv;
2826               gp_Vec norm1,norm2,d1u,d1v;
2827               gp_Pnt ptbid;
2828               Standard_Integer indextg;
2829               gp_Vec tgline(PW.TangentAtLine(indextg));
2830               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2831               Surf1->D1(locu,locv,ptbid,d1u,d1v);
2832               norm1 = d1u.Crossed(d1v);
2833               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2834               Surf2->D1(locu,locv,ptbid,d1u,d1v);
2835               norm2 = d1u.Crossed(d1v);
2836               if (tgline.DotCross(norm2,norm1)>0.)
2837               {
2838                 trans1 = IntSurf_Out;
2839                 trans2 = IntSurf_In;
2840               }
2841               else {
2842                 trans1 = IntSurf_In;
2843                 trans2 = IntSurf_Out;
2844               }
2845
2846               Standard_Real TolTang = TolTangency;
2847               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2848               IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2849               IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2850
2851               //---------------
2852               if(wline->NbVertex() == 0)
2853               {
2854                 IntPatch_Point vtx;
2855                 const IntSurf_PntOn2S& POn2Sf = PW.Line()->Value(1);
2856                 POn2Sf.Parameters(pu1,pv1,pu2,pv2);
2857                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2858                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2859                 vtx.SetParameter(1);
2860                 wline->AddVertex(vtx);
2861
2862                 const IntSurf_PntOn2S& POn2Sl = PW.Line()->Value(wline->NbPnts());
2863                 POn2Sl.Parameters(pu1,pv1,pu2,pv2);
2864                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
2865                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2866                 vtx.SetParameter(wline->NbPnts());
2867                 wline->AddVertex(vtx);
2868               }
2869
2870               //---------------
2871               AddWLine(SLin, wline, Deflection);
2872               empt = Standard_False;
2873             }
2874             else
2875             { 
2876               //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2877             }
2878             //------------------------------------------------------------              
2879           }
2880         }  //--  le cheminement a reussi (done a True)
2881       }  //--  le point approche ne renvoie pas sur une ligne existante
2882     } //-- Si HasStartPoint
2883   } //-- Boucle sur Les Tangent Zones
2884 }
2885 //modified by NIZNHY-PKV Wed May 25 09:39:07 2011f
2886 //=======================================================================
2887 //class : IntPatch_InfoPD
2888 //purpose  : 
2889 //=======================================================================
2890 class IntPatch_InfoPD {
2891 public:
2892   //----------------------------------------C-tor
2893   IntPatch_InfoPD(const Standard_Integer aNBI) {
2894     Standard_Integer aNBI2, i, j;
2895     myNBI=aNBI;
2896     //
2897     aNBI2=aNBI*aNBI;
2898     myP1DS2=new char[aNBI2];
2899     myP2DS1=new char[aNBI2];
2900     myIP1=new Standard_Integer[aNBI2]; 
2901     myIP2=new Standard_Integer[aNBI2];
2902     myP1=new gp_Pnt[aNBI2];
2903     myP2=new gp_Pnt[aNBI2];
2904     //
2905     for (i=0; i<myNBI; ++i) {
2906       for (j=0; j<myNBI; ++j) {
2907         xP1DS2(i, j)=0;
2908         xP2DS1(i, j)=0;
2909         xIP1(i, j)=0;
2910         xIP2(i, j)=0;
2911         xP1(i, j).SetCoord(0., 0., 0.);
2912         xP2(i, j).SetCoord(0., 0., 0.);
2913       }
2914     }
2915   };
2916   //---------------------------------------- D-tor
2917   ~IntPatch_InfoPD() {
2918     delete [] (char*) myP1DS2;
2919     delete [] (char*) myP2DS1;
2920     delete [] (Standard_Integer*) myIP1;
2921     delete [] (Standard_Integer*) myIP2; 
2922     delete [] (gp_Pnt*)myP1;
2923     delete [] (gp_Pnt*)myP2;
2924   };
2925   //---------------------------------------- Index
2926   Standard_Integer Index(const Standard_Integer i,
2927     const Standard_Integer j) const { 
2928       return i*myNBI+j;
2929   };
2930   //---------------------------------------- NBI
2931   Standard_Integer NBI() const { 
2932     return myNBI;
2933   };
2934   //----------------------------------------xP1DS2
2935   char& xP1DS2(const Standard_Integer i,
2936     const Standard_Integer j) { 
2937       return myP1DS2[Index(i,j)];
2938   };
2939   //----------------------------------------xP2DS1
2940   char& xP2DS1(const Standard_Integer i,
2941     const Standard_Integer j) { 
2942       return myP2DS1[Index(i,j)];
2943   };
2944   //----------------------------------------xIP1
2945   Standard_Integer& xIP1(const Standard_Integer i,
2946     const Standard_Integer j) { 
2947       return myIP1[Index(i,j)];
2948   };
2949   //----------------------------------------xIP2
2950   Standard_Integer& xIP2(const Standard_Integer i,
2951     const Standard_Integer j) { 
2952       return myIP2[Index(i,j)];
2953   };
2954   //----------------------------------------xP1
2955   gp_Pnt& xP1(const Standard_Integer i,
2956     const Standard_Integer j) { 
2957       return myP1[Index(i,j)];
2958   };
2959   //----------------------------------------xP1
2960   gp_Pnt& xP2(const Standard_Integer i,
2961     const Standard_Integer j) { 
2962       return myP2[Index(i,j)];
2963   };
2964
2965 private:
2966
2967   IntPatch_InfoPD (const IntPatch_InfoPD&);
2968   IntPatch_InfoPD& operator=(const IntPatch_InfoPD&);
2969
2970 private:
2971
2972   Standard_Integer myNBI;
2973   char *myP1DS2;
2974   char *myP2DS1;
2975   Standard_Integer *myIP1;
2976   Standard_Integer *myIP2;
2977   gp_Pnt *myP1;
2978   gp_Pnt *myP2;
2979 }; 
2980 //modified by NIZNHY-PKV Tue May 24 11:38:55 2011t
2981 //==================================================================================
2982 // function : PointDepart
2983 // purpose  : 
2984 //==================================================================================
2985 void IntPatch_PrmPrmIntersection::PointDepart(Handle(IntSurf_LineOn2S)& LineOn2S,
2986                                               const Handle(Adaptor3d_HSurface)& S1,
2987                                               const Standard_Integer SU_1,
2988                                               const Standard_Integer SV_1,
2989                                               const Handle(Adaptor3d_HSurface)& S2,
2990                                               const Standard_Integer SU_2,
2991                                               const Standard_Integer SV_2) const 
2992
2993   Standard_Integer i, j, xNBI;
2994   //modified by NIZNHY-PKV Tue May 24 11:37:38 2011f
2995   xNBI=200;
2996   IntPatch_InfoPD aIPD(xNBI);
2997   //modified by NIZNHY-PKV Wed May 25 06:47:12 2011t
2998   Standard_Integer iC15, SU1, SV1, SU2, SV2;
2999   Standard_Real U0, U1, V0, V1, U, V;
3000   Standard_Real resu0,resv0;
3001   Standard_Real  du1,du2,dv1,dv2, dmaxOn1, dmaxOn2;
3002   Standard_Real x0,y0,z0, x1,y1,z1,d;
3003   Bnd_Box Box1, Box2;
3004   //
3005   iC15=15;
3006   SU1 =iC15*SU_1 ;
3007   SV1 =iC15*SV_1 ;
3008   SU2 =iC15*SU_2 ;
3009   SV2 =iC15*SV_2 ;
3010   //
3011   if(xNBI<SU1) {
3012     SU1 = xNBI;
3013   }
3014   if(xNBI<SV1){
3015     SV1 = xNBI;
3016   }
3017   if(xNBI<SU2){
3018     SU2 = xNBI;
3019   }
3020   if(xNBI<SV2){
3021     SV2 = xNBI;
3022   }
3023   //
3024   U0 = S1->FirstUParameter();
3025   U1 = S1->LastUParameter();
3026   V0 = S1->FirstVParameter();
3027   V1 = S1->LastVParameter();
3028   //
3029   resu0=U0;
3030   resv0=V0;
3031   //
3032   dmaxOn1 = 0.0;
3033   dmaxOn2 = 0.0;
3034   //-----
3035   du1 = (U1-U0)/(SU1-1);
3036   dv1 = (V1-V0)/(SV1-1);
3037   for(U=U0,i=0; i<SU1; i++,U+=du1) { 
3038     for(V=V0,j=0; j<SV1; V+=dv1,j++) { 
3039       aIPD.xP1(i, j)= S1->Value(U,V);
3040       Box1.Add(aIPD.xP1(i, j));
3041       if(i>0 && j>0) { 
3042         aIPD.xP1(i, j)    .Coord(x0,y0,z0);
3043         aIPD.xP1(i-1, j-1).Coord(x1,y1,z1);
3044         //
3045         d=Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
3046         if(d>dmaxOn1) {
3047           dmaxOn1 = d;
3048         }
3049       }
3050     }
3051   }
3052   Box1.Enlarge(1.e-8);
3053   //
3054   U0 = S2->FirstUParameter();
3055   U1 = S2->LastUParameter();
3056   V0 = S2->FirstVParameter();
3057   V1 = S2->LastVParameter();
3058   //
3059   du2 = (U1-U0)/(SU2-1);  
3060   dv2 = (V1-V0)/(SV2-1);
3061   for(U=U0,i=0; i<SU2; i++,U+=du2) { 
3062     for(V=V0,j=0; j<SV2; V+=dv2,j++) { 
3063       aIPD.xP2(i, j) = S2->Value(U,V);
3064       Box2.Add(aIPD.xP2(i, j));
3065       if(i>0 && j>0) { 
3066         aIPD.xP2(i, j)    .Coord(x0,y0,z0);
3067         aIPD.xP2(i-1, j-1).Coord(x1,y1,z1);
3068         d =  Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
3069         if(d>dmaxOn2) {
3070           dmaxOn2 = d;
3071         }
3072       }
3073     }
3074   }
3075   Box2.Enlarge(1.e-8);
3076   //--------
3077   //
3078   if(Box1.IsOut(Box2)) {
3079
3080     return;
3081   }
3082   //
3083   Standard_Integer aNbPG;
3084   Standard_Real x10,y10,z10,x11,y11,z11;
3085   Standard_Real x20,y20,z20,x21,y21,z21;
3086   Standard_Real dx, dy, dz, dmax;
3087   Standard_Real dx2, dy2, dz2;
3088   //
3089   Box1.Get(x10,y10,z10,x11,y11,z11);
3090   Box2.Get(x20,y20,z20,x21,y21,z21);
3091   //
3092   x0 = (x10>x20)? x10 : x20;
3093   y0 = (y10>y20)? y10 : y20;
3094   z0 = (z10>z20)? z10 : z20;
3095   //
3096   x1 = (x11<x21)? x11 : x21;
3097   y1 = (y11<y21)? y11 : y21;
3098   z1 = (z11<z21)? z11 : z21;
3099   //
3100   if(dmaxOn2 > dmaxOn1) {
3101     dmaxOn1 = dmaxOn2;
3102   }
3103   //
3104   dmaxOn1+=dmaxOn1;
3105   x0-=dmaxOn1;
3106   y0-=dmaxOn1;
3107   z0-=dmaxOn1;
3108   x1+=dmaxOn1;
3109   y1+=dmaxOn1;
3110   z1+=dmaxOn1;
3111   //
3112   x10-=dmaxOn1;   y10-=dmaxOn1;   z10-=dmaxOn1; 
3113   x11+=dmaxOn1;   y11+=dmaxOn1;   z11+=dmaxOn1; 
3114
3115   x20-=dmaxOn1;   y20-=dmaxOn1;   z20-=dmaxOn1; 
3116   x21+=dmaxOn1;   y21+=dmaxOn1;   z21+=dmaxOn1; 
3117
3118   aNbPG=NbPointsGrille();
3119   dx = (x1-x0)/aNbPG;
3120   dy = (y1-y0)/aNbPG;
3121   dz = (z1-z0)/aNbPG;
3122   //
3123   dmax = dx;
3124   if(dy>dmax) {
3125     dmax = dy;
3126   }
3127   if(dz>dmax){
3128     dmax = dz;
3129   }
3130   //
3131   if(dx<dmax*0.01) {
3132     dx = dmax*0.01;
3133   }
3134   if(dy<dmax*0.01) {
3135     dy = dmax*0.01;
3136   }
3137   if(dz<dmax*0.01) {
3138     dz = dmax*0.01;
3139   }
3140   //
3141   dx2 = dx*0.5;
3142   dy2 = dy*0.5;
3143   dz2 = dz*0.5 ;
3144   //
3145   IntPatch_PrmPrmIntersection_T3Bits M1(_BASE);
3146   IntPatch_PrmPrmIntersection_T3Bits M2(_BASE);
3147   //
3148   for(i=0;i<SU1;i++) { 
3149     for(j=0;j<SV1;j++) { 
3150       aIPD.xIP1(i, j)=-1;
3151       const gp_Pnt& P=aIPD.xP1(i, j);
3152       aIPD.xP1DS2(i, j) = (char)CodeReject(x20,y20,z20,x21,y21,z21,P.X(),P.Y(),P.Z());
3153       int ix = (int)((P.X()-x0  + dx2 )/dx);
3154       if(DansGrille(ix)) { 
3155         int iy = (int)((P.Y()-y0 + dy2)/dy);
3156         if(DansGrille(iy)) {
3157           int iz = (int)((P.Z()-z0 + dz2)/dz);
3158           if(DansGrille(iz)) {
3159             aIPD.xIP1(i, j) = GrilleInteger(ix,iy,iz);
3160           }
3161         }
3162       }
3163     }
3164   }