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