0024510: Remove unused local variables
[occt.git] / src / IntPatch / IntPatch_ImpPrmIntersection.cxx
1 // Created on: 1992-05-07
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1992-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
9 // under the terms of the GNU Lesser General Public 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 #include <IntPatch_ImpPrmIntersection.ixx>
18
19 #include <Standard_ConstructionError.hxx>
20 #include <IntPatch_SequenceOfLine.hxx>
21 #include <TColStd_Array1OfInteger.hxx>
22 #include <IntSurf_PntOn2S.hxx>
23 #include <IntSurf_LineOn2S.hxx>
24 #include <IntSurf.hxx>
25
26 #include <Adaptor2d_HCurve2d.hxx>
27 #include <IntSurf_PathPoint.hxx>
28 #include <IntSurf_SequenceOfPathPoint.hxx>
29 #include <IntPatch_TheIWalking.hxx>
30 #include <IntPatch_TheIWLineOfTheIWalking.hxx>
31 #include <IntPatch_ThePathPointOfTheSOnBounds.hxx>
32 #include <IntPatch_TheSegmentOfTheSOnBounds.hxx>
33 #include <IntPatch_TheSurfFunction.hxx>
34 #include <IntPatch_RLine.hxx>
35 #include <IntPatch_WLine.hxx>
36 #include <IntPatch_ArcFunction.hxx>
37 #include <IntPatch_RstInt.hxx>
38
39 //=======================================================================
40 //function : IntPatch_ImpPrmIntersection
41 //purpose  : 
42 //=======================================================================
43
44 IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection ()
45  : done(Standard_False),
46    empt(Standard_False),
47    myIsStartPnt(Standard_False),
48    myUStart(0.0),
49    myVStart(0.0)
50 { }
51
52
53 //=======================================================================
54 //function : IntPatch_ImpPrmIntersection
55 //purpose  : 
56 //=======================================================================
57
58 IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection
59        (const Handle(Adaptor3d_HSurface)&    Surf1,
60         const Handle(Adaptor3d_TopolTool)&   D1,
61         const Handle(Adaptor3d_HSurface)&    Surf2,
62         const Handle(Adaptor3d_TopolTool)&   D2,
63         const Standard_Real    TolArc,
64         const Standard_Real    TolTang,
65         const Standard_Real    Fleche,
66         const Standard_Real    Pas)
67  : done(Standard_False),
68    empt(Standard_False),
69    myIsStartPnt(Standard_False),
70    myUStart(0.0),
71    myVStart(0.0)
72 {
73   Perform(Surf1,D1,Surf2,D2,TolArc,TolTang,Fleche,Pas);
74 }
75
76
77 //=======================================================================
78 //function : SetStartPoint
79 //purpose  : 
80 //=======================================================================
81
82 void IntPatch_ImpPrmIntersection::SetStartPoint(const Standard_Real U,
83                                                 const Standard_Real V)
84 {
85   myIsStartPnt = Standard_True;
86   myUStart = U; myVStart = V;
87 }
88
89 #ifndef DEB
90 #define No_Standard_RangeError
91 #define No_Standard_OutOfRange
92 #endif
93
94
95 #include <math_Vector.hxx>
96 #include <math_Matrix.hxx>
97 #include <TopTrans_CurveTransition.hxx>
98 #include <TopAbs_State.hxx>
99 #include <TopAbs_Orientation.hxx>
100 #include <TColStd_Array1OfInteger.hxx>
101 #include <TColStd_Array1OfReal.hxx>
102
103 #include <IntSurf_SequenceOfInteriorPoint.hxx>
104 #include <IntSurf_QuadricTool.hxx>
105 #include <GeomAbs_SurfaceType.hxx>
106
107
108 static void DecomposeResult(Handle(IntPatch_Line)&   Line,
109                             Standard_Boolean         IsReversed,
110                             IntSurf_Quadric&         Quad,
111                             Handle(Adaptor3d_TopolTool)&    PDomain,
112                             Handle(Adaptor3d_HSurface)&              QSurf,
113                             Standard_Real            TolArc,
114                             IntPatch_SequenceOfLine& Lines);
115 static 
116   void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
117                         IntSurf_SequenceOfPathPoint& seqpdep,
118                         const Handle(Adaptor3d_TopolTool)& Domain,
119                         IntPatch_TheSurfFunction& Func,
120                         const Handle(Adaptor3d_HSurface)& PSurf,
121                         TColStd_Array1OfInteger& Destination);
122 static 
123   void Recadre(const Standard_Boolean ,
124                GeomAbs_SurfaceType typeS1,
125                GeomAbs_SurfaceType typeS2,
126                IntPatch_Point&  pt,
127                const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
128                Standard_Integer Param,
129                Standard_Real U1,
130                Standard_Real V1,
131                Standard_Real U2,
132                Standard_Real V2);
133
134 //=======================================================================
135 //function : ComputeTangency
136 //purpose  : 
137 //=======================================================================
138 void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
139                       IntSurf_SequenceOfPathPoint& seqpdep,
140                       const Handle(Adaptor3d_TopolTool)& Domain,
141                       IntPatch_TheSurfFunction& Func,
142                       const Handle(Adaptor3d_HSurface)& PSurf,
143                       TColStd_Array1OfInteger& Destination)
144 {
145   Standard_Integer i,k, NbPoints, seqlength;
146   Standard_Real theparam,test;
147   Standard_Boolean fairpt, ispassing;
148   TopAbs_Orientation arcorien,vtxorien;
149   Handle(Adaptor2d_HCurve2d) thearc;
150   Handle(Adaptor3d_HVertex) vtx,vtxbis;
151   //Standard_Boolean ispassing;
152   IntPatch_ThePathPointOfTheSOnBounds PStart;
153   IntSurf_PathPoint PPoint;
154   gp_Vec vectg;
155   gp_Dir2d dirtg;
156   gp_Pnt ptbid;
157   gp_Vec d1u,d1v,v1,v2;
158   gp_Pnt2d p2d;
159   gp_Vec2d d2d;
160   //
161   double aX[2], aF[1], aD[1][2];
162   math_Vector X(aX, 1, 2);
163   math_Vector F(aF, 1, 1);
164   math_Matrix D(aD, 1, 1, 1, 2); 
165   //
166   seqlength = 0;
167   NbPoints = solrst.NbPoints();
168   for (i=1; i<= NbPoints; i++) {
169     if (Destination(i) == 0) {
170       PStart = solrst.Point(i);
171       thearc   = PStart.Arc();
172       theparam = PStart.Parameter();
173       arcorien = Domain->Orientation(thearc);
174       ispassing = (arcorien == TopAbs_INTERNAL || 
175                    arcorien == TopAbs_EXTERNAL);
176       
177       thearc->D0(theparam,p2d);
178       X(1) = p2d.X(); 
179       X(2) = p2d.Y();
180       PPoint.SetValue(PStart.Value(),X(1),X(2));
181       
182       Func.Values(X,F,D);
183       if (Func.IsTangent()) {
184         PPoint.SetTangency(Standard_True);
185         Destination(i) = seqlength+1;
186         if (!PStart.IsNew()) {
187           vtx = PStart.Vertex();
188           for (k=i+1; k<=NbPoints; k++) {
189             if (Destination(k) ==0) {
190               PStart = solrst.Point(k);
191               if (!PStart.IsNew()) {
192                 vtxbis = PStart.Vertex();
193                 if (Domain->Identical(vtx,vtxbis)) {
194                   thearc   = PStart.Arc();
195                   theparam = PStart.Parameter();
196                   arcorien = Domain->Orientation(thearc);
197                   ispassing = ispassing && (arcorien == TopAbs_INTERNAL ||
198                                             arcorien == TopAbs_EXTERNAL);
199                   
200                   thearc->D0(theparam,p2d);
201                   PPoint.AddUV(p2d.X(),p2d.Y());
202                   Destination(k) = seqlength+1;
203                 }
204               }
205             }
206           }
207         }
208         PPoint.SetPassing(ispassing);
209         seqpdep.Append(PPoint);
210         seqlength++;
211       }
212       else { // on a un point de depart potentiel
213
214         vectg = Func.Direction3d();
215         dirtg = Func.Direction2d();
216       
217         PSurf->D1(X(1),X(2),ptbid,d1u,d1v);
218         thearc->D1(theparam,p2d,d2d);
219         v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
220         v1 = d1u.Crossed(d1v);
221
222         test = vectg.Dot(v1.Crossed(v2));
223         if (PStart.IsNew()) {
224           if ((test < 0. && arcorien == TopAbs_FORWARD) ||
225               (test > 0. && arcorien == TopAbs_REVERSED)) {
226             vectg.Reverse();
227             dirtg.Reverse();
228           }
229           PPoint.SetDirections(vectg,dirtg);
230           PPoint.SetPassing(ispassing);
231           Destination(i) = seqlength+1;
232           seqpdep.Append(PPoint);
233           seqlength++;
234         }
235         else { // traiter la transition complexe
236           gp_Dir bidnorm(1.,1.,1.);
237           Standard_Real tole = 1.e-8;
238           TopAbs_Orientation LocTrans;
239           TopTrans_CurveTransition comptrans;
240           comptrans.Reset(vectg,bidnorm,0.);
241           if (arcorien == TopAbs_FORWARD || 
242               arcorien == TopAbs_REVERSED) {
243             // pour essai
244
245             vtx = PStart.Vertex();
246             vtxorien = Domain->Orientation(vtx);
247             if (Abs(test) <= tole) {
248               LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
249             }
250             else {
251               if (((test > 0.)&& arcorien == TopAbs_FORWARD) ||
252                   ((test < 0.)&& arcorien == TopAbs_REVERSED)){
253                 LocTrans = TopAbs_FORWARD;
254               }
255               else {
256                 LocTrans = TopAbs_REVERSED;
257               }
258               if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
259             }
260
261             comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
262           }
263           Destination(i) = seqlength+1;
264           for (k= i+1; k<=NbPoints; k++) {
265             if (Destination(k) == 0) {
266               PStart = solrst.Point(k);
267               if (!PStart.IsNew()) {
268                 vtxbis = PStart.Vertex();
269                 if (Domain->Identical(vtx,vtxbis)) {
270                   thearc   = PStart.Arc();
271                   theparam = PStart.Parameter();
272                   arcorien = Domain->Orientation(thearc);
273                   
274                   PPoint.AddUV(X(1),X(2));
275
276                   thearc->D1(theparam,p2d,d2d);
277                   PPoint.AddUV(p2d.X(),p2d.Y());
278
279                   if (arcorien == TopAbs_FORWARD || 
280                       arcorien == TopAbs_REVERSED) {
281                     ispassing = Standard_False;
282                     v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
283
284                     test = vectg.Dot(v1.Crossed(v2));
285                     vtxorien = Domain->Orientation(PStart.Vertex());
286                     if (Abs(test) <= tole) {
287                       LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
288                     }
289                     else {
290                       if (((test > 0.)&& arcorien == TopAbs_FORWARD) ||
291                           ((test < 0.)&& arcorien == TopAbs_REVERSED)){
292                         LocTrans = TopAbs_FORWARD;
293                       }
294                       else {
295                         LocTrans = TopAbs_REVERSED;
296                       }
297                       if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
298                     }
299
300                     comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
301                   }
302                   Destination(k) = seqlength+1;
303                 }
304               }
305             }
306           }
307           fairpt = Standard_True;
308           if (!ispassing) {
309             TopAbs_State Before = comptrans.StateBefore();
310             TopAbs_State After  = comptrans.StateAfter();
311             if ((Before == TopAbs_UNKNOWN)||(After == TopAbs_UNKNOWN)) {
312               fairpt = Standard_False;
313             }
314             else if (Before == TopAbs_IN) {
315               if (After == TopAbs_IN) {
316                 ispassing = Standard_True;
317               }
318               else {
319                 vectg.Reverse();
320                 dirtg.Reverse();
321               }
322             }
323             else {
324               if (After !=TopAbs_IN) {
325                 fairpt = Standard_False;
326               }
327             }
328           }
329           if (fairpt) {
330             PPoint.SetDirections(vectg,dirtg);
331             PPoint.SetPassing(ispassing);
332             seqpdep.Append(PPoint);
333             seqlength++;
334           }
335           else { // il faut remettre en "ordre" si on ne garde pas le point.
336             for (k=i; k <=NbPoints ; k++) {
337               if (Destination(k)==seqlength + 1) {
338                 Destination(k) = -Destination(k);
339               }
340             }
341           }
342         }
343       }
344     }
345   }
346 }
347 //=======================================================================
348 //function : Recadre
349 //purpose  : 
350 //=======================================================================
351 void Recadre(const Standard_Boolean ,
352              GeomAbs_SurfaceType typeS1,
353              GeomAbs_SurfaceType typeS2,
354              IntPatch_Point&  pt,
355              const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
356              Standard_Integer Param,
357              Standard_Real U1,
358              Standard_Real V1,
359              Standard_Real U2,
360              Standard_Real V2)
361 {
362   Standard_Real U1p,V1p,U2p,V2p;
363   iwline->Line()->Value(Param).Parameters(U1p,V1p,U2p,V2p);
364   switch(typeS1)
365   {
366     case GeomAbs_Torus:
367       while(V1<(V1p-1.5*M_PI)) V1+=M_PI+M_PI;
368       while(V1>(V1p+1.5*M_PI)) V1-=M_PI+M_PI;
369     case GeomAbs_Cylinder:
370     case GeomAbs_Cone:
371     case GeomAbs_Sphere:
372       while(U1<(U1p-1.5*M_PI)) U1+=M_PI+M_PI;
373       while(U1>(U1p+1.5*M_PI)) U1-=M_PI+M_PI;
374     default:
375       break;
376   }
377   switch(typeS2)
378   { 
379     case GeomAbs_Torus:
380       while(V2<(V2p-1.5*M_PI)) V2+=M_PI+M_PI;
381       while(V2>(V2p+1.5*M_PI)) V2-=M_PI+M_PI;
382     case GeomAbs_Cylinder:
383     case GeomAbs_Cone:
384     case GeomAbs_Sphere:
385       while(U2<(U2p-1.5*M_PI)) U2+=M_PI+M_PI;
386       while(U2>(U2p+1.5*M_PI)) U2-=M_PI+M_PI;
387     default:
388       break;
389   }
390   pt.SetParameters(U1,V1,U2,V2);
391 }
392
393 //=======================================================================
394 //function : Perform
395 //purpose  : 
396 //=======================================================================
397 void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
398                                            const Handle(Adaptor3d_TopolTool)& D1,
399                                            const Handle(Adaptor3d_HSurface)& Surf2,
400                                            const Handle(Adaptor3d_TopolTool)& D2,
401                                            const Standard_Real TolArc,
402                                            const Standard_Real TolTang,
403                                            const Standard_Real Fleche,
404                                            const Standard_Real Pas)
405 {
406   Standard_Boolean reversed, procf, procl, dofirst, dolast;
407   Standard_Integer indfirst = 0, indlast = 0, ind2, i,j,k, NbSegm;
408   Standard_Integer NbPointIns, NbPointRst, Nblines, Nbpts, NbPointDep;
409   Standard_Real U1,V1,U2,V2,paramf,paraml,currentparam;
410   
411   IntPatch_TheSegmentOfTheSOnBounds thesegm;
412   IntSurf_PathPoint PPoint;
413
414   Handle(IntPatch_RLine) rline;
415   Handle(IntPatch_WLine) wline;
416   IntPatch_ThePathPointOfTheSOnBounds PStart,PStartf,PStartl;
417   IntPatch_Point ptdeb,ptfin,ptbis;
418   
419   IntPatch_IType typ;
420   IntSurf_Transition TLine,TArc;
421   IntSurf_TypeTrans trans1,trans2;
422   gp_Pnt valpt,ptbid;
423   gp_Vec tgline,tgrst,norm1,norm2,d1u,d1v;
424   gp_Dir DirNormale;
425   gp_Vec VecNormale;
426   
427   gp_Pnt2d p2d;
428   gp_Vec2d d2d;
429   
430   Handle(Adaptor2d_HCurve2d) currentarc;
431   GeomAbs_SurfaceType typeS1, typeS2;
432   IntSurf_Quadric Quad;
433   IntPatch_TheSurfFunction Func;
434   IntPatch_ArcFunction AFunc;
435   //
436   typeS1 = Surf1->GetType();
437   typeS2 = Surf2->GetType();
438   
439   paramf =0.;
440   paraml =0.;
441   trans1 = IntSurf_Undecided;
442   trans2 = IntSurf_Undecided;
443   //
444   done = Standard_False;
445   empt = Standard_True;
446   slin.Clear();
447   spnt.Clear();
448   //
449   reversed = Standard_False;
450   switch (typeS1) {
451     case GeomAbs_Plane:
452       Quad.SetValue(Surf1->Plane());
453       break;
454
455     case GeomAbs_Cylinder:
456       Quad.SetValue(Surf1->Cylinder());
457       break;
458
459     case GeomAbs_Sphere:
460       Quad.SetValue(Surf1->Sphere());
461       break;
462
463     case GeomAbs_Cone:
464       Quad.SetValue(Surf1->Cone());
465       break;
466
467     default: {
468       reversed = Standard_True;
469       switch (typeS2) {
470         case GeomAbs_Plane:
471           Quad.SetValue(Surf2->Plane());
472           break;
473
474         case GeomAbs_Cylinder:
475           Quad.SetValue(Surf2->Cylinder());
476           break;
477           
478         case GeomAbs_Sphere:
479           Quad.SetValue(Surf2->Sphere());
480           break;
481
482         case GeomAbs_Cone:
483           Quad.SetValue(Surf2->Cone());
484           break;
485         default: {
486           Standard_ConstructionError::Raise();
487           break;
488         }
489       } 
490     }
491     break;
492   }
493   //
494   Func.SetImplicitSurface(Quad);
495   Func.Set(IntSurf_QuadricTool::Tolerance(Quad));
496   AFunc.SetQuadric(Quad);
497   //
498   if (!reversed) {
499     Func.Set(Surf2);
500     AFunc.Set(Surf2);
501   }
502   else {
503     Func.Set(Surf1);
504     AFunc.Set(Surf1);
505   }
506   //
507   if (!reversed) {
508     solrst.Perform(AFunc,D2,TolArc,TolTang);
509   }
510   else {
511     solrst.Perform(AFunc,D1,TolArc,TolTang);
512   }
513   if (!solrst.IsDone()) {
514     return;
515   }
516   //
517   IntSurf_SequenceOfPathPoint seqpdep;
518   IntSurf_SequenceOfInteriorPoint seqpins;
519   //
520   NbPointRst = solrst.NbPoints();
521   TColStd_Array1OfInteger Destination(1,NbPointRst+1); Destination.Init(0);
522   if (NbPointRst) {
523     if (!reversed) {
524       ComputeTangency(solrst,seqpdep,D2,Func,Surf2,Destination);
525     }
526     else {
527       ComputeTangency(solrst,seqpdep,D1,Func,Surf1,Destination);
528     }
529   }
530   //
531   // Recherche des points interieurs
532   if (!reversed) {
533     if (myIsStartPnt)
534       solins.Perform(Func,Surf2,myUStart,myVStart);
535     else
536       solins.Perform(Func,Surf2,D2,TolTang);
537   }
538   else {
539     if (myIsStartPnt)
540       solins.Perform(Func,Surf1,myUStart,myVStart);
541     else
542       solins.Perform(Func,Surf1,D1,TolTang);
543   }
544   //
545   NbPointIns = solins.NbPoints();
546   for (i=1; i <= NbPointIns; i++) {
547     seqpins.Append(solins.Value(i));
548   }
549   NbPointDep=seqpdep.Length();
550   //
551   if (NbPointDep || NbPointIns) {
552     IntPatch_TheIWalking iwalk(TolTang,Fleche,Pas);
553     if (!reversed) {
554       iwalk.Perform(seqpdep,seqpins,Func,Surf2);
555     }
556     else {
557       iwalk.Perform(seqpdep,seqpins,Func,Surf1,Standard_True);
558     }
559     if(!iwalk.IsDone()) {
560       return;
561     }
562     
563     Standard_Real Vmin, Vmax, TolV = 1.e-14;
564     if (!reversed) { //Surf1 is quadric
565       Vmin = Surf1->FirstVParameter();
566       Vmax = Surf1->LastVParameter();
567     }
568     else { //Surf2 is quadric
569       Vmin = Surf2->FirstVParameter();
570       Vmax = Surf2->LastVParameter();
571     }
572     //
573     Nblines = iwalk.NbLines();
574     for (j=1; j<=Nblines; j++) {
575       const Handle(IntPatch_TheIWLineOfTheIWalking)&  iwline  = iwalk.Value(j);
576       const Handle(IntSurf_LineOn2S)&                 thelin  = iwline->Line();
577       
578       Nbpts = thelin->NbPoints();
579       if(Nbpts>=2) { 
580         
581         tgline = iwline->TangentVector(k);      
582         if(k>=1 && k<=Nbpts) { } else { k=Nbpts>>1; } 
583         valpt = thelin->Value(k).Value();       
584         
585         if (!reversed) {
586           thelin->Value(k).ParametersOnS2(U2,V2);
587           norm1 = Quad.Normale(valpt);
588           Surf2->D1(U2,V2,ptbid,d1u,d1v);
589           norm2 = d1u.Crossed(d1v);
590         }
591         else {
592           thelin->Value(k).ParametersOnS1(U2,V2);
593           norm2 = Quad.Normale(valpt);
594           Surf1->D1(U2,V2,ptbid,d1u,d1v);
595           norm1 = d1u.Crossed(d1v);
596         }
597         if (tgline.DotCross(norm2,norm1) > 0.) {
598           trans1 = IntSurf_Out;
599           trans2 = IntSurf_In;
600         }
601         else {
602           trans1 = IntSurf_In;
603           trans2 = IntSurf_Out;
604         }
605
606         //
607         Standard_Real AnU1,AnU2,AnV2;
608
609         GeomAbs_SurfaceType typQuad = Quad.TypeQuadric();
610         Standard_Boolean arecadr=Standard_False;
611         valpt = thelin->Value(1).Value();
612         Quad.Parameters(valpt,AnU1,V1);
613
614         if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
615         if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
616
617         if(reversed) { 
618           thelin->SetUV(1,Standard_False,AnU1,V1); //-- on va lire u2,v2
619           thelin->Value(1).ParametersOnS1(AnU2,AnV2);
620         }
621         else { 
622           thelin->SetUV(1,Standard_True,AnU1,V1);  //-- on va lire u1,v1 
623           thelin->Value(1).ParametersOnS2(AnU2,AnV2);
624         }
625         
626         if(typQuad==GeomAbs_Cylinder || 
627            typQuad==GeomAbs_Cone || 
628            typQuad==GeomAbs_Sphere) { 
629           arecadr=Standard_True; 
630         } 
631         //
632         for (k=2; k<=Nbpts; ++k) {
633           valpt = thelin->Value(k).Value();
634           Quad.Parameters(valpt,U1,V1);
635           //
636           if((V1 < Vmin) && (Vmin-V1 < TolV)) {
637             V1 = Vmin;
638           }
639           if((V1 > Vmax) && (V1-Vmax < TolV)) {
640             V1 = Vmax;
641           }
642           //
643           if(arecadr) {
644             //modified by NIZNHY-PKV Fri Mar 28 15:06:01 2008f
645             Standard_Real aCf, aTwoPI;
646             //
647             aCf=0.;
648             aTwoPI=M_PI+M_PI;
649             if ((U1-AnU1) >  1.5*M_PI) { 
650               while ((U1-AnU1) > (1.5*M_PI+aCf*aTwoPI)) {
651                 aCf=aCf+1.;
652               }
653               U1=U1-aCf*aTwoPI;
654             } 
655             //
656             else {
657               while ((U1-AnU1) < (-1.5*M_PI-aCf*aTwoPI)) {
658                 aCf=aCf+1.;
659               }
660               U1=U1+aCf*aTwoPI;
661             }
662             // was:
663             //if ((U1-AnU1) >  1.5*M_PI) { 
664             //  U1-=M_PI+M_PI;
665             //}
666             //else if ((U1-AnU1) < -1.5*M_PI) { 
667             //  U1+=M_PI+M_PI; 
668             //}
669             //modified by NIZNHY-PKV Fri Mar 28 15:06:11 2008t
670           }
671           //
672           if(reversed) { 
673             thelin->SetUV(k,Standard_False,U1,V1);
674             
675             thelin->Value(k).ParametersOnS1(U2,V2);
676             switch(typeS1) { 
677             case GeomAbs_Cylinder:
678             case GeomAbs_Cone:
679             case GeomAbs_Sphere:
680             case GeomAbs_Torus:
681               while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
682               while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
683               break;
684             default: 
685               break;
686             }
687             if(typeS2==GeomAbs_Torus) { 
688               while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
689               while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
690             }
691             thelin->SetUV(k,Standard_True,U2,V2);
692           }
693           else { 
694             thelin->SetUV(k,Standard_True,U1,V1);
695
696             thelin->Value(k).ParametersOnS2(U2,V2);
697             switch(typeS2) { 
698             case GeomAbs_Cylinder:
699             case GeomAbs_Cone:
700             case GeomAbs_Sphere:
701             case GeomAbs_Torus:
702               while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
703               while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
704               break;
705             default: 
706               break;
707             }
708             if(typeS2==GeomAbs_Torus) { 
709               while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
710               while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
711             }
712             thelin->SetUV(k,Standard_False,U2,V2);
713
714           }
715
716           AnU1=U1;
717           AnU2=U2;
718           AnV2=V2;
719         }
720         // <-A
721         wline = new IntPatch_WLine(thelin,Standard_False,trans1,trans2);
722         
723         if (   iwline->HasFirstPoint() 
724             && iwline->IsTangentAtBegining() == Standard_False) {
725           indfirst = iwline->FirstPointIndex();
726           PPoint = seqpdep(indfirst);
727           tgline = PPoint.Direction3d();
728           Standard_Integer themult = PPoint.Multiplicity();
729           for (i=NbPointRst; i>=1; i--) {
730             if (Destination(i) == indfirst) {
731               if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
732                 Quad.Parameters(PPoint.Value(),U1,V1);
733
734                 if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
735                 if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
736
737                 PPoint.Parameters(themult,U2,V2);
738                 Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
739               }
740               else {  //-- typeS1 != Pln && Cyl && Sph && Cone
741                 Quad.Parameters(PPoint.Value(),U2,V2);
742
743                 if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
744                 if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
745
746                 PPoint.Parameters(themult,U1,V1);
747                 Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
748               }
749
750               VecNormale = d1u.Crossed(d1v);                      
751               //-- Modif du 27 Septembre 94 (Recadrage des pts U,V) 
752               ptdeb.SetValue(PPoint.Value(),TolArc,Standard_False);
753               ptdeb.SetParameters(U1,V1,U2,V2);
754               ptdeb.SetParameter(1.);
755               
756               Recadre(reversed,typeS1,typeS2,ptdeb,iwline,1,U1,V1,U2,V2);
757               
758               currentarc = solrst.Point(i).Arc();
759               currentparam = solrst.Point(i).Parameter();
760               currentarc->D1(currentparam,p2d,d2d);
761               tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
762               
763               Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
764               if(squaremagnitudeVecNormale > 1e-13) { 
765                 DirNormale=VecNormale;
766                 IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
767               }
768               else { 
769                 TLine.SetValue(Standard_True,IntSurf_Undecided);
770                 TArc.SetValue(Standard_True,IntSurf_Undecided);
771               }
772               
773               ptdeb.SetArc(reversed,currentarc,currentparam,TLine,TArc);
774               if (!solrst.Point(i).IsNew()) {
775                 ptdeb.SetVertex(reversed,solrst.Point(i).Vertex());
776               }
777               wline->AddVertex(ptdeb);
778               if (themult == 0) {
779                 wline->SetFirstPoint(wline->NbVertex());
780               }
781               
782               themult--;
783             }
784           }
785         }
786         else if (iwline->IsTangentAtBegining()) {
787           gp_Pnt psol = thelin->Value(1).Value();
788           thelin->Value(1).ParametersOnS1(U1,V1);
789           thelin->Value(1).ParametersOnS2(U2,V2);
790           ptdeb.SetValue(psol,TolArc,Standard_True);
791           ptdeb.SetParameters(U1,V1,U2,V2);
792           ptdeb.SetParameter(1.);
793           wline->AddVertex(ptdeb);
794           wline->SetFirstPoint(wline->NbVertex());
795         }
796         else { 
797           gp_Pnt psol = thelin->Value(1).Value();
798           thelin->Value(1).ParametersOnS1(U1,V1);
799           thelin->Value(1).ParametersOnS2(U2,V2);
800           ptdeb.SetValue(psol,TolArc,Standard_False);
801           ptdeb.SetParameters(U1,V1,U2,V2);
802           ptdeb.SetParameter(1.);
803           wline->AddVertex(ptdeb);
804           wline->SetFirstPoint(wline->NbVertex());
805         }
806         
807         
808         if (   iwline->HasLastPoint() 
809             && iwline->IsTangentAtEnd() == Standard_False) {
810           indlast = iwline->LastPointIndex();
811           PPoint = seqpdep(indlast);
812           tgline = PPoint.Direction3d().Reversed();
813           Standard_Integer themult = PPoint.Multiplicity();
814           for (i=NbPointRst; i >=1; i--) {
815             if (Destination(i) == indlast) {
816               if (!reversed) {
817                 Quad.Parameters(PPoint.Value(),U1,V1);
818
819                 if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
820                 if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
821
822                 PPoint.Parameters(themult,U2,V2);
823                 Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
824                 VecNormale = d1u.Crossed(d1v);                    //-- @@@@
825               }
826               else {
827                 Quad.Parameters(PPoint.Value(),U2,V2);
828
829                 if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
830                 if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
831
832                 PPoint.Parameters(themult,U1,V1);
833                 Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
834                 VecNormale = d1u.Crossed(d1v);                    //-- @@@@
835               }
836               
837               ptfin.SetValue(PPoint.Value(),TolArc,Standard_False);
838               ptfin.SetParameters(U1,V1,U2,V2);
839               ptfin.SetParameter(Nbpts);
840               
841               Recadre(reversed,typeS1,typeS2,ptfin,iwline,Nbpts-1,U1,V1,U2,V2);
842               
843               currentarc = solrst.Point(i).Arc();
844               currentparam = solrst.Point(i).Parameter();
845               currentarc->D1(currentparam,p2d,d2d);
846               tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
847               
848
849               Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
850               if(squaremagnitudeVecNormale > 1e-13) { 
851                 DirNormale=VecNormale;
852                 IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
853               }
854               else { 
855                 TLine.SetValue(Standard_True,IntSurf_Undecided);
856                 TArc.SetValue(Standard_True,IntSurf_Undecided);
857               }
858               
859               
860               ptfin.SetArc(reversed,currentarc,currentparam,TLine,TArc);
861               if (!solrst.Point(i).IsNew()) {
862                 ptfin.SetVertex(reversed,solrst.Point(i).Vertex());
863               }
864               wline->AddVertex(ptfin);
865               if (themult == 0) {
866                 wline->SetLastPoint(wline->NbVertex());
867               }
868               
869               themult--;
870             }
871           }
872         }
873         else if (iwline->IsTangentAtEnd()) {
874           gp_Pnt psol = thelin->Value(Nbpts).Value();
875           thelin->Value(Nbpts).ParametersOnS1(U1,V1);
876           thelin->Value(Nbpts).ParametersOnS2(U2,V2);
877           ptfin.SetValue(psol,TolArc,Standard_True);
878           ptfin.SetParameters(U1,V1,U2,V2);
879           ptfin.SetParameter(Nbpts);
880           wline->AddVertex(ptfin);
881           wline->SetLastPoint(wline->NbVertex());
882         }
883         else { 
884           gp_Pnt psol = thelin->Value(Nbpts).Value();
885           thelin->Value(Nbpts).ParametersOnS1(U1,V1);
886           thelin->Value(Nbpts).ParametersOnS2(U2,V2);
887           ptfin.SetValue(psol,TolArc,Standard_False);
888           ptfin.SetParameters(U1,V1,U2,V2);
889           ptfin.SetParameter(Nbpts);
890           wline->AddVertex(ptfin);
891           wline->SetLastPoint(wline->NbVertex());
892         }
893         //
894         // Il faut traiter les points de passage.
895         slin.Append(wline);
896       }// if(Nbpts>=2) {
897     }// for (j=1; j<=Nblines; j++) {
898
899     // ON GERE LES RACCORDS ENTRE LIGNES. ELLE NE PEUVENT SE RACCORDER
900     // QUE SUR DES POINTS DE TANGENCE
901
902
903     Nblines = slin.Length();
904     for (j=1; j<=Nblines-1; j++) {
905       dofirst = dolast = Standard_False;
906       const  Handle(IntPatch_Line)& slinj = slin(j);
907       const Handle(IntPatch_WLine)& wlin1 = *((Handle(IntPatch_WLine)*)&slinj);
908       if (wlin1->HasFirstPoint()) {
909         ptdeb = wlin1->FirstPoint(indfirst);
910         if (ptdeb.IsTangencyPoint()) {
911           dofirst = Standard_True;
912         }
913       }
914       if (wlin1->HasLastPoint()) {
915         ptfin = wlin1->LastPoint(indlast);
916         if (ptfin.IsTangencyPoint()) {
917           dolast = Standard_True;
918         }
919       }
920       
921       if (dofirst || dolast) {
922         for (k=j+1; k<=Nblines;k++) {
923           const  Handle(IntPatch_Line)& slink = slin(k);
924           const  Handle(IntPatch_WLine)& wlin2 = *((Handle(IntPatch_WLine)*)&slink);
925           if (wlin2->HasFirstPoint()) {
926             ptbis = wlin2->FirstPoint(ind2);
927             if (ptbis.IsTangencyPoint()) {
928               if (dofirst ) {
929                 if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
930                   ptdeb.SetMultiple(Standard_True);
931                   if (!ptbis.IsMultiple()) {
932                     ptbis.SetMultiple(Standard_True);
933                     wlin2->Replace(ind2,ptbis);
934                   }
935                 }
936               }
937               if (dolast ) {
938                 if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
939                   ptfin.SetMultiple(Standard_True);
940                   if (!ptbis.IsMultiple()) {
941                     ptbis.SetMultiple(Standard_True);
942                     wlin2->Replace(ind2,ptbis);
943                   }
944                 }
945               }
946             }
947           }
948           if (wlin2->HasLastPoint()) {
949             ptbis = wlin2->LastPoint(ind2);
950             if (ptbis.IsTangencyPoint()) {
951               if (dofirst ) {
952                 if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
953                   ptdeb.SetMultiple(Standard_True);
954                   if (!ptbis.IsMultiple()) {
955                     ptbis.SetMultiple(Standard_True);
956                     wlin2->Replace(ind2,ptbis);
957                   }
958                 }
959               }
960               if (dolast ) {
961                 if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
962                   ptfin.SetMultiple(Standard_True);
963                   if (!ptbis.IsMultiple()) {
964                     ptbis.SetMultiple(Standard_True);
965                     wlin2->Replace(ind2,ptbis);
966                   }
967                 }
968               }
969             }
970           }
971         }
972         if(dofirst) 
973           wlin1->Replace(indfirst,ptdeb);
974         if(dolast) 
975           wlin1->Replace(indlast,ptfin);
976       }
977     }
978   }// if (seqpdep.Length() != 0 || seqpins.Length() != 0) {
979   //
980   // Treatment the segments
981   NbSegm = solrst.NbSegments();
982   if (NbSegm) {
983     for(i=1; i<=NbSegm; i++) {
984       thesegm = solrst.Segment(i);  
985       //----------------------------------------------------------------------      
986       // on cree une ligne d intersection contenant uniquement le segment.
987       // VOIR POUR LA TRANSITION DE LA LIGNE
988       // On ajoute aussi un polygone pour le traitement des intersections
989       // entre ligne et restrictions de la surface implicite (PutVertexOnLine)
990       //----------------------------------------------------------------------
991       //-- Calcul de la transition sur la rline (12 fev 97)
992       //-- reversed a le sens de OnFirst
993       //--
994       dofirst = dolast  = Standard_False;
995       procf = Standard_False;
996       procl = Standard_False;
997       IntSurf_Transition TLineUnk,TArcUnk;
998
999       IntPatch_Point _thepointAtBeg;
1000       IntPatch_Point _thepointAtEnd;
1001       
1002       Standard_Boolean TransitionOK=Standard_False;
1003
1004       if(thesegm.HasFirstPoint()) { 
1005         Standard_Real _u1,_v1,_u2,_v2;
1006
1007         dofirst = Standard_True;
1008         PStartf = thesegm.FirstPoint();
1009         paramf = PStartf.Parameter();
1010
1011         gp_Pnt2d _p2d     = thesegm.Curve()->Value(paramf);
1012         Handle(Adaptor3d_HVertex) _vtx;
1013         if(PStartf.IsNew()==Standard_False) 
1014           _vtx= PStartf.Vertex();
1015         const gp_Pnt& _Pp = PStartf.Value();
1016         _thepointAtBeg.SetValue(_Pp,PStartf.Tolerance(),Standard_False);
1017         if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
1018           Quad.Parameters(_Pp,_u1,_v1);
1019           _u2=_p2d.X(); _v2=_p2d.Y();
1020         }
1021         else {  //-- typeS1 != Pln && Cyl && Sph && Cone
1022           Quad.Parameters(_Pp,_u2,_v2);
1023           _u1=_p2d.X(); _v1=_p2d.Y();
1024         }
1025         _thepointAtBeg.SetParameters(_u1,_v1,_u2,_v2);
1026         _thepointAtBeg.SetParameter(paramf);
1027         if(PStartf.IsNew()==Standard_False) 
1028           _thepointAtBeg.SetVertex(reversed,_vtx);
1029         _thepointAtBeg.SetArc(reversed,thesegm.Curve(),paramf,TLineUnk,TArcUnk);
1030
1031         
1032         gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
1033         Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
1034         norm1 = d1u1.Crossed(d1v1);
1035         Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
1036         norm2 = d1u2.Crossed(d1v2);
1037         
1038         thesegm.Curve()->D1(paramf,_p2d,_d2d);
1039         if(reversed) { 
1040           tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
1041         }
1042         else { 
1043           tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
1044         }
1045         _u1=tgline.DotCross(norm2,norm1);
1046         TransitionOK=Standard_True;
1047         if (_u1 > 0.00000001) {
1048           trans1 = IntSurf_Out;
1049           trans2 = IntSurf_In;
1050         }
1051         else if(_u1 < -0.00000001) { 
1052           trans1 = IntSurf_In;
1053           trans2 = IntSurf_Out;
1054         }
1055         else { 
1056           TransitionOK=Standard_False;
1057         }
1058       }
1059       if(thesegm.HasLastPoint()) {  
1060         Standard_Real _u1,_v1,_u2,_v2;
1061
1062         dolast = Standard_True;
1063         PStartl = thesegm.LastPoint();
1064         paraml = PStartl.Parameter();
1065         
1066         gp_Pnt2d _p2d = thesegm.Curve()->Value(paraml);
1067         Handle(Adaptor3d_HVertex) _vtx;
1068         if(PStartl.IsNew()==Standard_False) 
1069           _vtx = PStartl.Vertex();
1070         const gp_Pnt& _Pp = PStartl.Value();
1071         IntPatch_Point _thepoint;
1072         _thepointAtEnd.SetValue(_Pp,PStartl.Tolerance(),Standard_False);
1073         if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
1074           Quad.Parameters(_Pp,_u1,_v1);
1075           _u2=_p2d.X(); _v2=_p2d.Y();
1076         }
1077         else {  //-- typeS1 != Pln && Cyl && Sph && Cone
1078           Quad.Parameters(_Pp,_u2,_v2);
1079           _u1=_p2d.X(); _v1=_p2d.Y();
1080         }
1081         _thepointAtEnd.SetParameters(_u1,_v1,_u2,_v2);
1082         _thepointAtEnd.SetParameter(paraml);
1083         if(PStartl.IsNew()==Standard_False)
1084           _thepointAtEnd.SetVertex(reversed,_vtx);
1085         _thepointAtEnd.SetArc(reversed,thesegm.Curve(),paraml,TLineUnk,TArcUnk);
1086
1087         
1088         
1089         gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
1090         Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
1091         norm1 = d1u1.Crossed(d1v1);
1092         Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
1093         norm2 = d1u2.Crossed(d1v2);
1094         
1095         thesegm.Curve()->D1(paraml,_p2d,_d2d);
1096         if(reversed) { 
1097           tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
1098         }
1099         else { 
1100           tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
1101         }
1102         _u1=tgline.DotCross(norm2,norm1);
1103         TransitionOK=Standard_True;
1104         if (_u1 > 0.00000001) {
1105           trans1 = IntSurf_Out;
1106           trans2 = IntSurf_In;
1107         }
1108         else if(_u1 < -0.00000001) { 
1109           trans1 = IntSurf_In;
1110           trans2 = IntSurf_Out;
1111         }
1112         else { 
1113           TransitionOK=Standard_False;
1114         }       
1115       }
1116       if(TransitionOK==Standard_False) { 
1117         //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False);
1118         rline =  new IntPatch_RLine (Standard_False);
1119         if(reversed) { 
1120           rline->SetArcOnS1(thesegm.Curve());
1121         }
1122         else { 
1123           rline->SetArcOnS2(thesegm.Curve());
1124         }
1125       }
1126       else { 
1127         //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False,trans1,trans2);
1128         rline =  new IntPatch_RLine (Standard_False,trans1,trans2);
1129         if(reversed) { 
1130           rline->SetArcOnS1(thesegm.Curve());
1131         }
1132         else { 
1133           rline->SetArcOnS2(thesegm.Curve());
1134         }
1135       }
1136
1137       //------------------------------
1138       //-- Ajout des points 
1139       //--
1140       if (thesegm.HasFirstPoint()) {
1141         rline->AddVertex(_thepointAtBeg);
1142         rline->SetFirstPoint(rline->NbVertex());
1143       }
1144       
1145       if (thesegm.HasLastPoint()) {
1146         rline->AddVertex(_thepointAtEnd);
1147         rline->SetLastPoint(rline->NbVertex());
1148       }
1149
1150       // Polygone sur restriction solution
1151       if (dofirst && dolast) {
1152         Standard_Real prm;
1153         gp_Pnt ptpoly;
1154         IntSurf_PntOn2S p2s;
1155         Handle(IntSurf_LineOn2S) Thelin = new IntSurf_LineOn2S ();
1156         Handle(Adaptor2d_HCurve2d) arcsegm = thesegm.Curve();
1157         Standard_Integer nbsample = 100;
1158
1159         if (!reversed) {
1160           for (j=1; j<=nbsample; j++) {
1161             prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
1162             arcsegm->D0(prm,p2d);
1163             Surf2->D0(p2d.X(),p2d.Y(),ptpoly);
1164
1165             Quad.Parameters(ptpoly,U1,V1);
1166             p2s.SetValue(ptpoly,U1,V1,p2d.X(),p2d.Y());
1167             Thelin->Add(p2s);
1168           }
1169         }
1170         else {
1171           for (j=1; j<=nbsample; j++) {
1172             prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
1173             arcsegm->D0(prm,p2d);
1174             Surf1->D0(p2d.X(),p2d.Y(),ptpoly);
1175
1176             Quad.Parameters(ptpoly,U2,V2);
1177             p2s.SetValue(ptpoly,p2d.X(),p2d.Y(),U2,V2);
1178             Thelin->Add(p2s);
1179           }
1180         }
1181         rline->Add(Thelin);
1182       }
1183
1184       if (dofirst || dolast) {
1185         Nblines = slin.Length();
1186         for (j=1; j<=Nblines; j++) {
1187           const Handle(IntPatch_Line)& slinj = slin(j);
1188           typ = slinj->ArcType();
1189           if (typ == IntPatch_Walking) {
1190             Nbpts = (*((Handle(IntPatch_WLine)*)&slinj))->NbVertex();
1191           }
1192           else {
1193             Nbpts = (*((Handle(IntPatch_RLine)*)&slinj))->NbVertex();
1194           }
1195           for (k=1; k<=Nbpts;k++) {
1196             if (typ == IntPatch_Walking) {
1197               ptdeb = (*((Handle(IntPatch_WLine)*)&slinj))->Vertex(k);
1198             }
1199             else {
1200               ptdeb = (*((Handle(IntPatch_RLine)*)&slinj))->Vertex(k);
1201             }
1202             if (dofirst) {
1203
1204               if (ptdeb.Value().Distance(PStartf.Value()) <=TolArc) {
1205                 ptdeb.SetMultiple(Standard_True);
1206                 if (typ == IntPatch_Walking) {
1207                   (*((Handle(IntPatch_WLine)*)&slinj))->Replace(k,ptdeb);
1208                 }
1209                 else {
1210                   (*((Handle(IntPatch_RLine)*)&slinj))->Replace(k,ptdeb);
1211                 }
1212                 ptdeb.SetParameter(paramf);
1213                 rline->AddVertex(ptdeb);
1214                 if (!procf){
1215                   procf=Standard_True;
1216                   rline->SetFirstPoint(rline->NbVertex());
1217                 }
1218               }
1219             }
1220             if (dolast) {
1221               if(dofirst) { //-- on recharge le ptdeb
1222                 if (typ == IntPatch_Walking) {
1223                   ptdeb = (*((Handle(IntPatch_WLine)*)&slinj))->Vertex(k);
1224                 }
1225                 else {
1226                   ptdeb = (*((Handle(IntPatch_RLine)*)&slinj))->Vertex(k);
1227                 }
1228               }
1229               if (ptdeb.Value().Distance(PStartl.Value()) <=TolArc) {
1230                 ptdeb.SetMultiple(Standard_True);
1231                 if (typ == IntPatch_Walking) {
1232                   (*((Handle(IntPatch_WLine)*)&slinj))->Replace(k,ptdeb);
1233                 }
1234                 else {
1235                   (*((Handle(IntPatch_RLine)*)&slinj))->Replace(k,ptdeb);
1236                 }
1237                 ptdeb.SetParameter(paraml);
1238                 rline->AddVertex(ptdeb);
1239                 if (!procl){
1240                   procl=Standard_True;
1241                   rline->SetLastPoint(rline->NbVertex());
1242                 }
1243               }
1244             }
1245           }
1246         }
1247       }
1248       slin.Append(rline);
1249     }
1250   }// if (NbSegm) 
1251   //
1252   // on traite les restrictions de la surface implicite
1253   for (i=1; i<=slin.Length(); i++)
1254   {
1255     if (!reversed)
1256       IntPatch_RstInt::PutVertexOnLine(slin(i),Surf1,D1,Surf2,Standard_True,TolTang);
1257     else
1258       IntPatch_RstInt::PutVertexOnLine(slin(i),Surf2,D2,Surf1,Standard_False,TolTang);
1259   }
1260   empt = (slin.Length() == 0 && spnt.Length() == 0);
1261   done = Standard_True;
1262   //
1263   // post processing for cones and spheres
1264   if((Quad.TypeQuadric() == GeomAbs_Cone ||
1265       Quad.TypeQuadric() == GeomAbs_Sphere) && 
1266      slin.Length()) {
1267
1268     Handle(Adaptor3d_TopolTool) PDomain = (reversed) ? D1 : D2;
1269     Handle(Adaptor3d_HSurface) QSurf = (reversed) ? Surf2 : Surf1;
1270
1271     IntPatch_SequenceOfLine dslin;
1272     for(i = 1; i <= slin.Length(); i++ )
1273       DecomposeResult(slin(i),reversed,Quad,PDomain,QSurf,TolArc,dslin);
1274
1275     slin.Clear();
1276     for(i = 1; i <= dslin.Length(); i++ )
1277       slin.Append(dslin(i));
1278   }
1279 }
1280
1281 //==================================================================
1282 // Decomposition of WLine goes through seam and/or apex routines
1283 //==================================================================
1284
1285 // reject all points and vertices not in V range on Quadric
1286 static Handle(IntSurf_LineOn2S) VSplitLine(Handle(IntSurf_LineOn2S)& Line,
1287                                            Standard_Boolean          IsReversed,
1288                                            Handle(Adaptor3d_HSurface)&               QSurf,
1289                                            Handle(IntSurf_LineOn2S)& Vertices)
1290 {
1291   Handle(IntSurf_LineOn2S) line = new IntSurf_LineOn2S();
1292
1293   Standard_Real VF = QSurf->FirstVParameter();
1294   Standard_Real VL = QSurf->LastVParameter();
1295
1296   Standard_Integer i = 0;
1297   Standard_Real U = 0., V = 0.;
1298
1299   for(i = 1; i <= Line->NbPoints(); i++) {
1300
1301     const IntSurf_PntOn2S& aP = Line->Value(i);
1302     if(IsReversed)
1303       aP.ParametersOnS2(U,V); // S2 - quadric
1304     else
1305       aP.ParametersOnS1(U,V); // S1 - quadric
1306
1307     if(VF <= V && V <= VL)
1308       line->Add(aP);
1309   }
1310
1311   Standard_Boolean isDeleted = Standard_True;
1312   Standard_Integer k = 0;
1313   while(isDeleted) {
1314     
1315     isDeleted = Standard_False;
1316     k = 0;
1317
1318     for(i = 1; i <= Vertices->NbPoints(); i++) {
1319
1320       const IntSurf_PntOn2S& aV = Vertices->Value(i);
1321       if(IsReversed)
1322         aV.ParametersOnS2(U,V); // S2 - quadric
1323       else
1324         aV.ParametersOnS1(U,V); // S1 - quadric
1325
1326       if(VF <= V && V <= VL)
1327         continue;
1328       else {
1329         k = i;
1330         break;
1331       }
1332     }
1333
1334     if(k != 0) {
1335       isDeleted = Standard_True;
1336       Vertices->RemovePoint(k);
1337     }
1338     else
1339       break;
1340   }
1341
1342   return line;
1343 }
1344
1345 // correct U parameter of the start point of line on Quadric
1346 // (change 0->2PI or vs, if necessary)
1347 static Standard_Real AdjustUFirst(Standard_Real U1,Standard_Real U2)
1348 {
1349   Standard_Real u = U1;
1350
1351   // case: no adjustment
1352   if( U1 > 0. && U1 < (2.*M_PI) )
1353     return u;
1354
1355   // case: near '0'
1356   if( U1 == 0. || fabs(U1) <= 1.e-9 ) {
1357     if( U2 > 0. && U2 < (2.*M_PI) )
1358       u = ( U2 < ((2.*M_PI)-U2) ) ? 0. : (2.*M_PI);
1359     else {
1360       Standard_Real uu = U2;
1361       if( U2 > (2.*M_PI) )
1362         while( uu > (2.*M_PI) )
1363           uu -= (2.*M_PI);
1364       else 
1365         while( uu < 0.)
1366           uu += (2.*M_PI);
1367       
1368       u = ( uu < ((2.*M_PI)-uu) ) ? 0. : (2.*M_PI);
1369     }
1370   }
1371   // case: near '2PI'
1372   else if( U1 == (2.*M_PI) || fabs((2.*M_PI)-fabs(U1)) <= 1.e-9 ) {
1373     if( U2 > 0. && U2 < (2.*M_PI) )
1374       u = ( U2 < ((2.*M_PI)-U2) ) ? 0. : (2.*M_PI);
1375     else {
1376       Standard_Real uu = U2;
1377       if( U2 > (2.*M_PI) )
1378         while( uu > (2.*M_PI) )
1379           uu -= (2.*M_PI);
1380       else 
1381         while( uu < 0.)
1382           uu += (2.*M_PI);
1383       
1384       u = ( uu < ((2.*M_PI)-uu) ) ? 0. : (2.*M_PI);
1385     }
1386   }
1387   // case: '<0. || >2PI'
1388   else {
1389     if(U1 < 0.)
1390       while(u < 0.)
1391         u += 2.*M_PI;
1392     if(U1 > (2.*M_PI))
1393       while(u > (2.*M_PI))
1394         u -= (2.*M_PI);
1395   }
1396
1397   return u;
1398 }
1399
1400 // collect vertices, reject equals
1401 static Handle(IntSurf_LineOn2S) GetVertices(const Handle(IntPatch_WLine)& WLine,
1402                                             const Standard_Boolean  ,//IsReversed,
1403                                             const Standard_Real     TOL3D,
1404                                             const Standard_Real     TOL2D)
1405 {
1406 //  Standard_Real TOL3D = 1.e-12, TOL2D = 1.e-8;
1407
1408   Handle(IntSurf_LineOn2S) vertices = new IntSurf_LineOn2S();
1409
1410   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0.;
1411   Standard_Integer i = 0, k = 0;
1412   Standard_Integer NbVrt = WLine->NbVertex();
1413   
1414   TColStd_Array1OfInteger anVrts(1,NbVrt);
1415   anVrts.Init(0);
1416
1417   // check equal vertices
1418   for(i = 1; i <= NbVrt; i++) {
1419
1420     if( anVrts(i) == -1 ) continue;
1421
1422     const IntPatch_Point& Pi = WLine->Vertex(i);
1423
1424     for(k = (i+1); k <= NbVrt; k++) {
1425
1426       if( anVrts(k) == -1 ) continue;
1427
1428       const IntPatch_Point& Pk = WLine->Vertex(k);
1429
1430       if(Pi.Value().Distance(Pk.Value()) <= TOL3D) {
1431         // suggest the points are equal;
1432         // test 2d parameters on surface
1433         Standard_Boolean sameU1 = Standard_False;
1434         Standard_Boolean sameV1 = Standard_False;
1435         Standard_Boolean sameU2 = Standard_False;
1436         Standard_Boolean sameV2 = Standard_False;
1437
1438         Pi.ParametersOnS1(U1,V1);
1439         Pk.ParametersOnS1(U2,V2);
1440         if(fabs(U1-U2) <= TOL2D) sameU1 = Standard_True;
1441         if(fabs(V1-V2) <= TOL2D) sameV1 = Standard_True;
1442
1443         Pi.ParametersOnS2(U1,V1);
1444         Pk.ParametersOnS2(U2,V2);
1445         if(fabs(U1-U2) <= TOL2D) sameU2 = Standard_True;
1446         if(fabs(V1-V2) <= TOL2D) sameV2 = Standard_True;
1447
1448         if((sameU1 && sameV1) && (sameU2 && sameV2))
1449           anVrts(k) = -1;
1450       }
1451     }
1452   }
1453
1454   // copy further processed vertices
1455   for(i = 1; i <= NbVrt; i++) {
1456     if( anVrts(i) == -1 ) continue;
1457     vertices->Add(WLine->Vertex(i).PntOn2S());
1458   }
1459   return vertices;
1460 }
1461
1462 static Standard_Boolean AreSamePoints(const IntSurf_PntOn2S& P1,
1463                                       const IntSurf_PntOn2S& P2)
1464 {
1465   Standard_Boolean result = Standard_False;
1466   Standard_Real T2D = 1.e-9, T3D = 1.e-8;
1467   const gp_Pnt& P3D1 = P1.Value();
1468   const gp_Pnt& P3D2 = P2.Value();
1469   if(P3D1.Distance(P3D2) <= T3D) {
1470     Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0., U3 = 0., V3 = 0., U4 = 0., V4 = 0.;
1471     P1.ParametersOnS1(U1,V1);
1472     P1.ParametersOnS2(U2,V2);
1473     P2.ParametersOnS1(U3,V3);
1474     P2.ParametersOnS2(U4,V4);
1475     gp_Pnt2d P2D11(U1,V1);
1476     gp_Pnt2d P2D12(U2,V2);
1477     gp_Pnt2d P2D21(U3,V3);
1478     gp_Pnt2d P2D22(U4,V4);
1479     Standard_Boolean sameS1 = (P2D11.Distance(P2D21) <= T2D) ? Standard_True : Standard_False;
1480     Standard_Boolean sameS2 = (P2D12.Distance(P2D22) <= T2D) ? Standard_True : Standard_False;
1481     if(sameS1 && sameS2)
1482       result = Standard_True;
1483   }
1484   return result;
1485 }
1486
1487 static void ForcedPurgePoints(Handle(IntSurf_LineOn2S)& Result,
1488                               Standard_Boolean          IsReversed,
1489                               IntSurf_Quadric&          Quad)
1490 {
1491   if(Result->NbPoints() <= 30) return;
1492   Standard_Integer Index = 0, IndexLimF = 8, IndexLimL = 8;
1493
1494   Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
1495     if(IsReversed) {
1496       Result->Value(1).ParametersOnS2(U1,V1);
1497       Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
1498     }
1499     else {
1500       Result->Value(1).ParametersOnS1(U1,V1);   
1501       Result->Value(Result->NbPoints()).ParametersOnS1(U2,V2);
1502     }
1503
1504   if(Quad.TypeQuadric() == GeomAbs_Cone) {
1505     Standard_Real Uapx = 0., Vapx = 0.;
1506     Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
1507     
1508     if(fabs(V1-Vapx) <= 1.e-3)
1509       IndexLimF = 12;
1510     if(fabs(V2-Vapx) <= 1.e-3)
1511       IndexLimL = 12;
1512   }
1513
1514   if(Quad.TypeQuadric() == GeomAbs_Sphere) {
1515     Standard_Real Vapx1 = M_PI/2., Vapx2 = -M_PI/2.;
1516     
1517     if(fabs(V1-Vapx1) <= 1.e-3 || fabs(V1-Vapx2) <= 1.e-3)
1518       IndexLimF = 12;
1519     if(fabs(V2-Vapx1) <= 1.e-3 || fabs(V2-Vapx2) <= 1.e-3)
1520       IndexLimL = 12;
1521   }
1522
1523   while(Result->NbPoints() > 2 && Index < IndexLimF) {
1524     Result->RemovePoint(2);
1525     Index++;
1526   }
1527   Index = 0;
1528   while(Result->NbPoints() > 2 && Index < IndexLimL) {
1529     Result->RemovePoint(Result->NbPoints()-1);
1530     Index++;
1531   }
1532 }
1533
1534 // DEBUG FUNCTION !!!
1535 #if 0
1536 static void DumpLine(Handle(IntSurf_LineOn2S)& Line,
1537                      Standard_Boolean          IsReversed,
1538                      Standard_Integer          Number)
1539 {
1540   cout << "DUMP LINE" << endl;
1541   Standard_Integer i;
1542   Standard_Real U,V;
1543   for(i = 1; i <= Line->NbPoints(); i++) {
1544     if(i <= Number || i >= (Line->NbPoints()-Number)) {
1545       if(IsReversed)
1546         Line->Value(i).ParametersOnS2(U,V); // S2 - quadric
1547       else
1548         Line->Value(i).ParametersOnS1(U,V); // S1 - quadric
1549       cout << "point p" << i << " " << U << " " << V << endl;
1550     }
1551   }
1552   cout << endl;
1553 }
1554 #endif
1555 // DEBUG FUNCTION !!!
1556
1557 static void SearchVertices(const Handle(IntSurf_LineOn2S)& Line,
1558                            const Handle(IntSurf_LineOn2S)& Vertices,
1559                            TColStd_Array1OfInteger&        PTypes)
1560 {
1561   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
1562   Standard_Integer ip = 0, iv = 0;
1563   for(ip = 1; ip <= nbp; ip++) {
1564     const IntSurf_PntOn2S& aP = Line->Value(ip);
1565     Standard_Integer type = 0;
1566     for(iv = 1; iv <= nbv; iv++) {
1567       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
1568       if(AreSamePoints(aP,aV)) {
1569         type = iv; 
1570         break;
1571       }
1572     }
1573     PTypes(ip) = type;
1574   }
1575 }
1576
1577 static inline Standard_Boolean IsSeamParameter(const Standard_Real U,
1578                                                const Standard_Real TOL2D)
1579 {
1580   return (fabs(U) <= TOL2D || fabs(2.*M_PI - U) <= TOL2D);
1581 }
1582
1583 static inline Standard_Real AdjustU(const Standard_Real U)
1584 {
1585   Standard_Real u = U, DBLPI = 2.*M_PI;
1586   if(u < 0. || u > DBLPI) {
1587     if(u < 0.)
1588       while(u < 0.)
1589         u += DBLPI;
1590     else
1591       while(u > DBLPI)
1592         u -= DBLPI;
1593   }
1594   return u;
1595 }
1596
1597 static inline void Correct2DBounds(const Standard_Real UF,
1598                                    const Standard_Real UL,
1599                                    const Standard_Real VF,
1600                                    const Standard_Real VL,
1601                                    const Standard_Real TOL2D,
1602                                    Standard_Real&      U,
1603                                    Standard_Real&      V)
1604 {
1605   Standard_Real Eps = 1.e-16;
1606   Standard_Real dUF = fabs(U - UF);
1607   Standard_Real dUL = fabs(U - UL);
1608   Standard_Real dVF = fabs(V - VF);
1609   Standard_Real dVL = fabs(V - VL);
1610   if(dUF <= TOL2D && dUF > Eps) U = UF;
1611   if(dUL <= TOL2D && dUL > Eps) U = UL;
1612   if(dVF <= TOL2D && dVF > Eps) V = VF;
1613   if(dVL <= TOL2D && dVL > Eps) V = VL;
1614 }
1615
1616 static void AdjustLine(Handle(IntSurf_LineOn2S)& Line,
1617                        const Standard_Boolean    IsReversed,
1618                        const Handle(Adaptor3d_HSurface)&         QSurf,
1619                        const Standard_Real       TOL2D)
1620 {
1621   Standard_Real VF = QSurf->FirstVParameter();
1622   Standard_Real VL = QSurf->LastVParameter();
1623   Standard_Real UF = QSurf->FirstUParameter();
1624   Standard_Real UL = QSurf->LastUParameter();
1625
1626   Standard_Integer nbp = Line->NbPoints(), ip = 0;
1627   Standard_Real U = 0., V = 0.;
1628   for(ip = 1; ip <= nbp; ip++) {
1629     if(IsReversed) {
1630       Line->Value(ip).ParametersOnS2(U,V);
1631       U = AdjustU(U);
1632       Correct2DBounds(UF,UL,VF,VL,TOL2D,U,V);
1633       Line->SetUV(ip,Standard_False,U,V);
1634     }
1635     else {
1636       Line->Value(ip).ParametersOnS1(U,V);
1637       U = AdjustU(U);
1638       Correct2DBounds(UF,UL,VF,VL,TOL2D,U,V);
1639       Line->SetUV(ip,Standard_True,U,V);
1640     }
1641   }
1642 }
1643
1644 static Standard_Boolean InsertSeamVertices(Handle(IntSurf_LineOn2S)&       Line,
1645                                            const Standard_Boolean          IsReversed,
1646                                            Handle(IntSurf_LineOn2S)&       Vertices,
1647                                            const TColStd_Array1OfInteger&  PTypes,
1648                                            const Standard_Real             TOL2D)
1649 {
1650   Standard_Boolean result = Standard_False;
1651   Standard_Integer ip = 0, nbp = Line->NbPoints();
1652   Standard_Real U = 0., V = 0.;
1653   for(ip = 1; ip <= nbp; ip++) {
1654     Standard_Integer ipt = PTypes(ip);
1655     if(ipt != 0) {
1656       const IntSurf_PntOn2S& aP = Line->Value(ip);
1657       if(IsReversed)
1658         aP.ParametersOnS2(U,V); // S2 - quadric
1659       else
1660         aP.ParametersOnS1(U,V); // S1 - quadric
1661       U = AdjustU(U);
1662       if(IsSeamParameter(U,TOL2D)) {
1663         if(ip == 1 || ip == nbp) {
1664           Standard_Real U1 = 0., V1 = 0.;
1665           Standard_Integer ipp = (ip == 1) ? (ip+1) : (ip-1);
1666           if(IsReversed)
1667             Line->Value(ipp).ParametersOnS2(U1,V1); // S2 - quadric
1668           else
1669             Line->Value(ipp).ParametersOnS1(U1,V1); // S1 - quadric
1670           Standard_Real u = AdjustUFirst(U,U1);
1671           if(fabs(u-U) >= 1.5*M_PI) {
1672             Standard_Real U2 = 0., V2 = 0.;
1673             if(IsReversed) {
1674               Line->Value(ip).ParametersOnS1(U2,V2); // prm
1675               Line->SetUV(ip,Standard_False,u,V);
1676               Line->SetUV(ip,Standard_True,U2,V2);
1677             }
1678             else {
1679               Line->Value(ip).ParametersOnS2(U2,V2); // prm
1680               Line->SetUV(ip,Standard_True,u,V);
1681               Line->SetUV(ip,Standard_False,U2,V2);
1682             }
1683           }
1684         }
1685         else {
1686           Standard_Integer ipp = ip - 1;
1687           Standard_Integer ipn = ip + 1;
1688           Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
1689           if(IsReversed) {
1690             Line->Value(ipp).ParametersOnS2(U1,V1); // quad
1691             Line->Value(ipn).ParametersOnS2(U2,V2); // quad
1692           }
1693           else {
1694             Line->Value(ipp).ParametersOnS1(U1,V1); // quad
1695             Line->Value(ipn).ParametersOnS1(U2,V2); // quad
1696           }
1697           U1 = AdjustU(U1);
1698           U2 = AdjustU(U2);
1699           Standard_Boolean pnearZero = (fabs(U1) < fabs(2.*M_PI-U1)) ? Standard_True : Standard_False;
1700           Standard_Boolean cnearZero = (fabs(U) < fabs(2.*M_PI-U)) ? Standard_True : Standard_False;
1701           if(pnearZero == cnearZero) {
1702             if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
1703               Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
1704               IntSurf_PntOn2S nP;
1705               nP.SetValue(aP.Value());
1706               Standard_Real U3 = 0., V3 = 0.;
1707               if(IsReversed) {
1708                 Line->Value(ip).ParametersOnS1(U3,V3); // prm
1709                 nP.SetValue(Standard_False,nU,V);
1710                 nP.SetValue(Standard_True,U3,V3);
1711               }
1712               else {
1713                 Line->Value(ip).ParametersOnS2(U3,V3); // prm
1714                 nP.SetValue(Standard_True,nU,V);
1715                 nP.SetValue(Standard_False,U3,V3);
1716               }
1717               Line->InsertBefore(ipn,nP);
1718               Vertices->Add(nP);
1719               result = Standard_True;
1720               break;
1721             }
1722           }
1723           else {
1724             if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
1725               Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
1726               IntSurf_PntOn2S nP;
1727               nP.SetValue(aP.Value());
1728               Standard_Real U3 = 0., V3 = 0.;
1729               if(IsReversed) {
1730                 Line->Value(ip).ParametersOnS1(U3,V3); // prm
1731                 nP.SetValue(Standard_False,nU,V);
1732                 nP.SetValue(Standard_True,U3,V3);
1733               }
1734               else {
1735                 Line->Value(ip).ParametersOnS2(U3,V3); // prm
1736                 nP.SetValue(Standard_True,nU,V);
1737                 nP.SetValue(Standard_False,U3,V3);
1738               }
1739               Line->InsertBefore(ip,nP);
1740               Vertices->Add(nP);
1741               result = Standard_True;
1742               break;
1743             }
1744             else {
1745 //            Line->InsertBefore(ip,Line->Value(ipn));
1746 //            Line->RemovePoint(ip+2);
1747 //            result = Standard_True;
1748 //            cout << "swap vertex " << endl;
1749 //            break;
1750             }
1751           }
1752         }
1753       }
1754     }
1755   }
1756   return result;
1757 }
1758
1759 static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
1760                      Standard_Boolean          IsReversed,
1761                      IntSurf_Quadric&          Quad,
1762                      Standard_Boolean          IsFirst,
1763                      Standard_Real&            D3D)
1764 {
1765   if(Line->NbPoints() <= 10)
1766     return;
1767   
1768   D3D = 0.;
1769   Standard_Integer NbTestPnts = Line->NbPoints() / 5;
1770   if(NbTestPnts < 5) NbTestPnts = 5;
1771
1772   Standard_Integer startp = (IsFirst) ? 2 : (Line->NbPoints() - NbTestPnts - 2);
1773   Standard_Integer ip = 0;
1774   Standard_Real Uc = 0., Vc = 0., Un = 0., Vn = 0., DDU = 0., DDV = 0.;
1775
1776   for(ip = startp; ip <= NbTestPnts; ip++) {
1777     if(IsReversed) {
1778       Line->Value(ip).ParametersOnS2(Uc,Vc); // S2 - quadric
1779       Line->Value(ip+1).ParametersOnS2(Un,Vn);
1780     }
1781     else {
1782       Line->Value(ip).ParametersOnS1(Uc,Vc); // S1 - quadric
1783       Line->Value(ip+1).ParametersOnS1(Un,Vn);
1784     }
1785     DDU += fabs(fabs(Uc)-fabs(Un));
1786     DDV += fabs(fabs(Vc)-fabs(Vn));
1787     
1788     if(ip > startp) {
1789       Standard_Real DP = Line->Value(ip).Value().Distance(Line->Value(ip-1).Value());
1790       D3D += DP;
1791     }
1792   }
1793
1794   DDU /= (Standard_Real) NbTestPnts + 1;
1795   DDV /= (Standard_Real) NbTestPnts + 1;
1796   
1797   D3D /= (Standard_Real) NbTestPnts + 1;
1798
1799
1800   Standard_Integer Index1 = (IsFirst) ? 1 : (Line->NbPoints());
1801   Standard_Integer Index2 = (IsFirst) ? 2 : (Line->NbPoints()-1);
1802   Standard_Integer Index3 = (IsFirst) ? 3 : (Line->NbPoints()-2);
1803
1804   Standard_Boolean doU = Standard_False;
1805
1806   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., U3 = 0., V3 = 0.;
1807
1808   if(IsReversed) {
1809     Line->Value(Index1).ParametersOnS2(U1,V1); // S2 - quadric
1810     Line->Value(Index2).ParametersOnS2(U2,V2);
1811     Line->Value(Index3).ParametersOnS2(U3,V3);
1812   }
1813   else {
1814     Line->Value(Index1).ParametersOnS1(U1,V1); // S1 - quadric
1815     Line->Value(Index2).ParametersOnS1(U2,V2);
1816     Line->Value(Index3).ParametersOnS1(U3,V3);
1817   }
1818
1819   if(!doU && Quad.TypeQuadric() == GeomAbs_Sphere) {
1820     if(fabs(fabs(U1)-fabs(U2)) > (M_PI/16.)) doU = Standard_True;
1821     
1822     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
1823       if(fabs(V1-M_PI/2.) <= 1.e-9 || fabs(V1+M_PI/2.) <= 1.e-9) {}
1824       else {
1825         doU = Standard_False;
1826       }
1827     }
1828   }
1829   
1830   if(Quad.TypeQuadric() == GeomAbs_Cone) {
1831     Standard_Real Uapx = 0., Vapx = 0.;
1832     Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
1833
1834     if(fabs(fabs(U1)-fabs(U2)) > M_PI/32.) doU = Standard_True;
1835
1836     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
1837       if(fabs(V1-Vapx) <= 1.e-9) {}
1838       else {
1839         doU = Standard_False;
1840       }
1841     }
1842   }
1843
1844   if(doU) {
1845     Standard_Real dU = Min((DDU/10.),5.e-8);
1846     Standard_Real U = (U2 > U3) ? (U2 + dU) : (U2 - dU);
1847     if(IsReversed)
1848       Line->SetUV(Index1,Standard_False,U,V1);
1849     else
1850       Line->SetUV(Index1,Standard_True,U,V1);
1851     U1 = U;
1852   }
1853
1854
1855 static Standard_Boolean TestMiddleOnPrm(const IntSurf_PntOn2S& aP,
1856                                         const IntSurf_PntOn2S& aV,
1857                                         const Standard_Boolean IsReversed,
1858                                         const Standard_Real    ArcTol,
1859                                         Handle(Adaptor3d_TopolTool)&  PDomain)
1860                                         
1861 {
1862   Standard_Boolean result = Standard_False;
1863   Standard_Real Up = 0., Vp = 0., Uv = 0., Vv = 0.;
1864   if(IsReversed) {
1865     aP.ParametersOnS1(Up,Vp); //S1 - parametric
1866     aV.ParametersOnS1(Uv,Vv);
1867   }
1868   else {
1869     aP.ParametersOnS2(Up,Vp); // S2 - parametric
1870     aV.ParametersOnS2(Uv,Vv);
1871   }
1872   Standard_Real Um = (Up + Uv)*0.5, Vm = (Vp + Vv)*0.5;
1873   gp_Pnt2d a2DPntM(Um,Vm);
1874   TopAbs_State PosM = PDomain->Classify(a2DPntM,ArcTol);
1875   if(PosM == TopAbs_ON || PosM == TopAbs_IN )
1876     result = Standard_True;
1877   return result;
1878 }
1879
1880 static void VerifyVertices(Handle(IntSurf_LineOn2S)& Line,
1881                            Standard_Boolean          IsReversed,
1882                            Handle(IntSurf_LineOn2S)& Vertices,
1883                            Standard_Real             TOL2D,
1884                            const Standard_Real       ArcTol,
1885                            Handle(Adaptor3d_TopolTool)&     PDomain,
1886                            IntSurf_PntOn2S&          VrtF,
1887                            Standard_Boolean&         AddFirst,
1888                            IntSurf_PntOn2S&          VrtL,
1889                            Standard_Boolean&         AddLast)
1890 {
1891   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
1892   Standard_Integer FIndexSame = 0, FIndexNear = 0, LIndexSame = 0, LIndexNear = 0;
1893   const IntSurf_PntOn2S& aPF = Line->Value(1);
1894   const IntSurf_PntOn2S& aPL = Line->Value(nbp);
1895   Standard_Real UF = 0., VF = 0., UL = 0., VL = 0.;
1896   if(IsReversed) {
1897     aPF.ParametersOnS2(UF,VF);
1898     aPL.ParametersOnS2(UL,VL);
1899   }
1900   else {
1901     aPF.ParametersOnS1(UF,VF);
1902     aPL.ParametersOnS1(UL,VL);
1903   }
1904   gp_Pnt2d a2DPF(UF,VF);
1905   gp_Pnt2d a2DPL(UL,VL);
1906   Standard_Real DistMinF = 1.e+100, DistMinL = 1.e+100;
1907   Standard_Integer FConjugated = 0, LConjugated = 0;
1908
1909   Standard_Integer iv = 0;
1910
1911   for(iv = 1; iv <= nbv; iv++) {
1912     Standard_Real Uv = 0., Vv = 0.;
1913     if(IsReversed) {
1914       Vertices->Value(iv).ParametersOnS2(Uv,Vv);
1915       Uv = AdjustU(Uv);
1916       Vertices->SetUV(iv,Standard_False,Uv,Vv);
1917     }
1918     else {
1919       Vertices->Value(iv).ParametersOnS1(Uv,Vv);
1920       Uv = AdjustU(Uv);
1921       Vertices->SetUV(iv,Standard_True,Uv,Vv);
1922     }
1923   }
1924
1925   for(iv = 1; iv <= nbv; iv++) {
1926     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
1927     if(AreSamePoints(aPF,aV)) {
1928       FIndexSame = iv;
1929       break;
1930     }
1931     else {
1932       Standard_Real Uv = 0., Vv = 0.;
1933       if(IsReversed)
1934         aV.ParametersOnS2(Uv,Vv);
1935       else
1936         aV.ParametersOnS1(Uv,Vv);
1937       gp_Pnt2d a2DV(Uv,Vv);
1938       Standard_Real Dist = a2DV.Distance(a2DPF);
1939       if(Dist < DistMinF) {
1940         DistMinF = Dist;
1941         FIndexNear = iv;
1942         if(FConjugated != 0)
1943           FConjugated = 0;
1944       }
1945       if(IsSeamParameter(Uv,TOL2D)) {
1946         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
1947         gp_Pnt2d a2DCV(Ucv,Vv);
1948         Standard_Real CDist = a2DCV.Distance(a2DPF);
1949         if(CDist < DistMinF) {
1950           DistMinF = CDist;
1951           FConjugated = iv;
1952           FIndexNear = iv;
1953         }
1954       }
1955     }
1956   }
1957
1958   for(iv = 1; iv <= nbv; iv++) {
1959     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
1960     if(AreSamePoints(aPL,aV)) {
1961       LIndexSame = iv;
1962       break;
1963     }
1964     else {
1965       Standard_Real Uv = 0., Vv = 0.;
1966       if(IsReversed)
1967         aV.ParametersOnS2(Uv,Vv);
1968       else
1969         aV.ParametersOnS1(Uv,Vv);
1970       gp_Pnt2d a2DV(Uv,Vv);
1971       Standard_Real Dist = a2DV.Distance(a2DPL);
1972       if(Dist < DistMinL) {
1973         DistMinL = Dist;
1974         LIndexNear = iv;
1975         if(LConjugated != 0)
1976           LConjugated = 0;
1977       }
1978       if(IsSeamParameter(Uv,TOL2D)) {
1979         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
1980         gp_Pnt2d a2DCV(Ucv,Vv);
1981         Standard_Real CDist = a2DCV.Distance(a2DPL);
1982         if(CDist < DistMinL) {
1983           DistMinL = CDist;
1984           LConjugated = iv;
1985           LIndexNear = iv;
1986         }
1987       }
1988     }
1989   }
1990
1991   AddFirst = Standard_False;
1992   AddLast  = Standard_False;
1993
1994   if(FIndexSame == 0) {
1995     if(FIndexNear != 0) {
1996       const IntSurf_PntOn2S& aV = Vertices->Value(FIndexNear);
1997       Standard_Real Uv = 0., Vv = 0.;
1998       if(IsReversed)
1999         aV.ParametersOnS2(Uv,Vv);
2000       else
2001         aV.ParametersOnS1(Uv,Vv);
2002       if(IsSeamParameter(Uv,TOL2D)) {
2003         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2004         Standard_Boolean test = TestMiddleOnPrm(aPF,aV,IsReversed,ArcTol,PDomain);
2005         if(test) {
2006           VrtF.SetValue(aV.Value());
2007           if(IsReversed) {
2008             Standard_Real U2 = 0., V2 = 0.;
2009             aV.ParametersOnS1(U2,V2); // S1 - prm
2010             VrtF.SetValue(Standard_True,U2,V2);
2011             if(FConjugated == 0)
2012               VrtF.SetValue(Standard_False,Uv,Vv);
2013             else
2014               VrtF.SetValue(Standard_False,Ucv,Vv);
2015           }
2016           else {
2017             Standard_Real U2 = 0., V2 = 0.;
2018             aV.ParametersOnS2(U2,V2); // S2 - prm
2019             VrtF.SetValue(Standard_False,U2,V2);
2020             if(FConjugated == 0)
2021               VrtF.SetValue(Standard_True,Uv,Vv);
2022             else
2023               VrtF.SetValue(Standard_True,Ucv,Vv);
2024           }
2025           Standard_Real Dist3D = VrtF.Value().Distance(aPF.Value());
2026           if(Dist3D > 1.5e-7 && DistMinF > TOL2D) {
2027             AddFirst = Standard_True;
2028           }
2029         }
2030       }
2031       else {
2032         // to do: analyze internal vertex
2033       }
2034     }
2035   }
2036   
2037   if(LIndexSame == 0) {
2038     if(LIndexNear != 0) {
2039       const IntSurf_PntOn2S& aV = Vertices->Value(LIndexNear);
2040       Standard_Real Uv = 0., Vv = 0.;
2041       if(IsReversed)
2042         aV.ParametersOnS2(Uv,Vv);
2043       else
2044         aV.ParametersOnS1(Uv,Vv);
2045       if(IsSeamParameter(Uv,TOL2D)) {
2046         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2047         Standard_Boolean test = TestMiddleOnPrm(aPL,aV,IsReversed,ArcTol,PDomain);
2048         if(test) {
2049           VrtL.SetValue(aV.Value());
2050           if(IsReversed) {
2051             Standard_Real U2 = 0., V2 = 0.;
2052             aV.ParametersOnS1(U2,V2); // S1 - prm
2053             VrtL.SetValue(Standard_True,U2,V2);
2054             if(LConjugated == 0)
2055               VrtL.SetValue(Standard_False,Uv,Vv);
2056             else
2057               VrtL.SetValue(Standard_False,Ucv,Vv);
2058           }
2059           else {
2060             Standard_Real U2 = 0., V2 = 0.;
2061             aV.ParametersOnS2(U2,V2); // S2 - prm
2062             VrtL.SetValue(Standard_False,U2,V2);
2063             if(LConjugated == 0)
2064               VrtL.SetValue(Standard_True,Uv,Vv);
2065             else
2066               VrtL.SetValue(Standard_True,Ucv,Vv);
2067           }
2068           Standard_Real Dist3D = VrtL.Value().Distance(aPL.Value());
2069           if(Dist3D > 1.5e-7 && DistMinL > TOL2D) {
2070             AddLast = Standard_True;
2071           }
2072         }
2073       }
2074       else {
2075         // to do: analyze internal vertex
2076       }
2077     }
2078   }
2079 }
2080
2081 static Standard_Boolean AddVertices(Handle(IntSurf_LineOn2S)& Line,
2082                                     const IntSurf_PntOn2S&    VrtF,
2083                                     const Standard_Boolean    AddFirst,
2084                                     const IntSurf_PntOn2S&    VrtL,
2085                                     const Standard_Boolean    AddLast,
2086                                     const Standard_Real       D3DF,
2087                                     const Standard_Real       D3DL)
2088 {
2089   Standard_Boolean result = Standard_False;
2090   if(AddFirst) {
2091     Standard_Real DF = Line->Value(1).Value().Distance(VrtF.Value());
2092     if((D3DF*2.) > DF && DF > 1.5e-7) {
2093       Line->InsertBefore(1,VrtF);
2094       result = Standard_True;
2095     }
2096   }
2097   if(AddLast) {
2098     Standard_Real DL = Line->Value(Line->NbPoints()).Value().Distance(VrtL.Value());
2099     if((D3DL*2.) > DL && DL > 1.5e-7) {
2100       Line->Add(VrtL);
2101       result = Standard_True;
2102     }
2103   }
2104   return result;
2105 }
2106  
2107
2108 static void PutIntVertices(Handle(IntPatch_Line)&    Line,
2109                            Handle(IntSurf_LineOn2S)& Result,
2110                            Standard_Boolean          ,//IsReversed,
2111                            Handle(IntSurf_LineOn2S)& Vertices,
2112                            const Standard_Real       ArcTol)
2113 {
2114   Standard_Integer nbp = Result->NbPoints(), nbv = Vertices->NbPoints();
2115
2116   if(nbp < 3)
2117     return;
2118
2119   Handle(IntPatch_WLine)& WLine = (*((Handle(IntPatch_WLine)*)&Line));
2120   Standard_Integer ip = 0, iv = 0;
2121   gp_Pnt aPnt;
2122   IntPatch_Point thePnt;
2123   Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
2124   
2125   for(ip = 2; ip <= (nbp-1); ip++) {
2126     const IntSurf_PntOn2S& aP = Result->Value(ip);
2127     for(iv = 1; iv <= nbv; iv++) {
2128       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2129       if(AreSamePoints(aP,aV)) {
2130         aPnt = Result->Value(ip).Value();
2131         Result->Value(ip).ParametersOnS1(U1,V1);
2132         Result->Value(ip).ParametersOnS2(U2,V2);
2133         thePnt.SetValue(aPnt,ArcTol,Standard_False);
2134         thePnt.SetParameters(U1,V1,U2,V2);
2135         thePnt.SetParameter((Standard_Real)ip);
2136         WLine->AddVertex(thePnt);
2137       }
2138     }
2139   }
2140 }
2141
2142 static Standard_Boolean HasInternals(Handle(IntSurf_LineOn2S)& Line,
2143                                      Handle(IntSurf_LineOn2S)& Vertices)
2144 {
2145   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
2146   Standard_Integer ip = 0, iv = 0;
2147   Standard_Boolean result = Standard_False;
2148   
2149   if(nbp < 3)
2150     return result;
2151
2152   for(ip = 2; ip <= (nbp-1); ip++) {
2153     const IntSurf_PntOn2S& aP = Line->Value(ip);
2154     for(iv = 1; iv <= nbv; iv++) {
2155       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2156       if(AreSamePoints(aP,aV)) {
2157         result = Standard_True;
2158         break;
2159       }
2160     }
2161     if(result)
2162       break;
2163   }
2164   
2165   return result;
2166 }
2167 static Handle(IntPatch_WLine) MakeSplitWLine (Handle(IntPatch_WLine)&        WLine,
2168                                         Standard_Boolean         Tang,
2169                                         IntSurf_TypeTrans        Trans1,
2170                                         IntSurf_TypeTrans        Trans2,
2171                                         Standard_Real            ArcTol,
2172                                         Standard_Integer         ParFirst,
2173                                         Standard_Integer         ParLast)
2174 {
2175   Handle(IntSurf_LineOn2S) SLine = WLine->Curve();
2176   Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
2177  
2178  Standard_Integer ip = 0;
2179   for(ip = ParFirst; ip <= ParLast; ip++)
2180     sline->Add(SLine->Value(ip));
2181
2182   Handle(IntPatch_WLine) wline = new IntPatch_WLine(sline,Tang,Trans1,Trans2);
2183
2184   gp_Pnt aSPnt;
2185   IntPatch_Point TPntF,TPntL;
2186   Standard_Real uu1 = 0., vv1 = 0., uu2 = 0., vv2 = 0.;
2187
2188   aSPnt = sline->Value(1).Value();
2189   sline->Value(1).ParametersOnS1(uu1,vv1);
2190   sline->Value(1).ParametersOnS2(uu2,vv2);
2191   TPntF.SetValue(aSPnt,ArcTol,Standard_False);
2192   TPntF.SetParameters(uu1,vv1,uu2,vv2);
2193   TPntF.SetParameter(1.);
2194   wline->AddVertex(TPntF);
2195   wline->SetFirstPoint(1);
2196         
2197   aSPnt =  sline->Value(sline->NbPoints()).Value();
2198   sline->Value(sline->NbPoints()).ParametersOnS1(uu1,vv1);
2199   sline->Value(sline->NbPoints()).ParametersOnS2(uu2,vv2);
2200   TPntL.SetValue(aSPnt,ArcTol,Standard_False);
2201   TPntL.SetParameters(uu1,vv1,uu2,vv2);
2202   TPntL.SetParameter((Standard_Real)sline->NbPoints());
2203   wline->AddVertex(TPntL);
2204   wline->SetLastPoint(sline->NbPoints());
2205
2206   return wline;
2207 }
2208
2209 static Standard_Boolean SplitOnSegments(Handle(IntPatch_WLine)&        WLine,
2210                                         Standard_Boolean         Tang,
2211                                         IntSurf_TypeTrans        Trans1,
2212                                         IntSurf_TypeTrans        Trans2,
2213                                         Standard_Real            ArcTol,
2214                                         IntPatch_SequenceOfLine& Segments)
2215 {
2216   Standard_Boolean result = Standard_False;
2217   Segments.Clear();
2218
2219   Standard_Integer nbv = WLine->NbVertex();
2220   if(nbv > 3) {
2221     Standard_Integer iv = 0;
2222     for(iv = 1; iv < nbv; iv++) {
2223       Standard_Integer firstPar = (Standard_Integer) WLine->Vertex(iv).ParameterOnLine();
2224       Standard_Integer lastPar  = (Standard_Integer) WLine->Vertex(iv+1).ParameterOnLine();
2225       if((lastPar - firstPar) <= 1)
2226         continue;
2227       else {
2228         Handle(IntPatch_WLine) splitwline = MakeSplitWLine(WLine,Tang,Trans1,Trans2,ArcTol,firstPar,lastPar);
2229         Segments.Append(splitwline);
2230         if(!result)
2231           result = Standard_True;
2232       }
2233     }
2234   }
2235   return result;
2236 }
2237
2238 void DecomposeResult(Handle(IntPatch_Line)&   Line,
2239                      Standard_Boolean         IsReversed,
2240                      IntSurf_Quadric&         Quad,
2241                      Handle(Adaptor3d_TopolTool)&    PDomain,
2242                      Handle(Adaptor3d_HSurface)&              QSurf,
2243                      Standard_Real            ArcTol,
2244                      IntPatch_SequenceOfLine& Lines)
2245 {
2246   if( Line->ArcType() != IntPatch_Walking ) {
2247     Lines.Append(Line);
2248     return;
2249   }
2250
2251   Handle(IntPatch_WLine)& WLine = (*((Handle(IntPatch_WLine)*)&Line));
2252   Handle(IntSurf_LineOn2S) SLine = WLine->Curve();
2253
2254   if(SLine->NbPoints() <= 2) {
2255     Lines.Append(Line);
2256     return;
2257   }
2258
2259   Standard_Real TOL3D = 1.e-10, TOL2D = 1.e-8, TOL2DS = 1.e-5;
2260
2261   Handle(IntSurf_LineOn2S) vline = GetVertices(WLine,IsReversed,TOL3D,TOL2D);
2262   Handle(IntSurf_LineOn2S) SSLine = VSplitLine(SLine,IsReversed,QSurf,vline);
2263
2264   if(SSLine->NbPoints() <= 1)
2265     return;
2266
2267   AdjustLine(SSLine,IsReversed,QSurf,TOL2D);
2268
2269   Standard_Boolean isInserted = Standard_True;
2270   while(isInserted) {
2271     Standard_Integer NbPnts = SSLine->NbPoints();
2272     TColStd_Array1OfInteger ptypes(1,NbPnts);
2273     SearchVertices(SSLine,vline,ptypes);
2274     isInserted = InsertSeamVertices(SSLine,IsReversed,vline,ptypes,TOL2D);
2275   }
2276
2277   if(SSLine->NbPoints() == 2) {
2278     Standard_Real uu1 = 0., vv1 = 0., uu2 = 0., vv2 = 0.;
2279     if(IsReversed) { 
2280       SSLine->Value(1).ParametersOnS2(uu1,vv1);
2281       SSLine->Value(2).ParametersOnS2(uu2,vv2);
2282     }
2283     else {
2284       SSLine->Value(1).ParametersOnS1(uu1,vv1);
2285       SSLine->Value(2).ParametersOnS1(uu2,vv2);
2286     }
2287     gp_Pnt2d p1(uu1,vv1);
2288     gp_Pnt2d p2(uu2,vv2);
2289     if(p1.Distance(p2) > 2.e-7) {
2290       Standard_Real Dist3D = SSLine->Value(1).Value().Distance(SSLine->Value(2).Value());
2291       if(Dist3D > 3.e-7) {
2292         gp_Pnt aSPnt;
2293         IntPatch_Point TPntF,TPntL;
2294         Handle(IntPatch_WLine) wline = new IntPatch_WLine(SSLine,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2());
2295         aSPnt = SSLine->Value(1).Value();
2296         SSLine->Value(1).ParametersOnS1(uu1,vv1);
2297         SSLine->Value(1).ParametersOnS2(uu2,vv2);
2298         TPntF.SetValue(aSPnt,ArcTol,Standard_False);
2299         TPntF.SetParameters(uu1,vv1,uu2,vv2);
2300         TPntF.SetParameter(1.);
2301         wline->AddVertex(TPntF);
2302         wline->SetFirstPoint(1);
2303         aSPnt =  SSLine->Value(SSLine->NbPoints()).Value();
2304         SSLine->Value(SSLine->NbPoints()).ParametersOnS1(uu1,vv1);
2305         SSLine->Value(SSLine->NbPoints()).ParametersOnS2(uu2,vv2);
2306         TPntL.SetValue(aSPnt,ArcTol,Standard_False);
2307         TPntL.SetParameters(uu1,vv1,uu2,vv2);
2308         TPntL.SetParameter(SSLine->NbPoints());
2309         wline->AddVertex(TPntL);
2310         wline->SetLastPoint(SSLine->NbPoints());
2311         Lines.Append(wline);
2312         return;
2313       }
2314       else
2315         return;
2316     }
2317     else
2318       return;
2319   }
2320
2321   Standard_Integer NbPnts = SSLine->NbPoints();
2322
2323   Standard_Real BSEAM = 1.5*M_PI; // delta U crossing seam
2324   Standard_Real BAPEX = M_PI/16.;  // delta U crossing apex
2325   
2326   Standard_Integer k = 0;
2327   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., AnU1 = 0.;
2328   Standard_Integer Findex = 1, Lindex = NbPnts, Bindex = 0;
2329
2330   gp_Pnt aPnt, aSPnt;
2331   IntPatch_Point TPntF,TPntL,TPntS;
2332
2333   Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
2334   Handle(IntPatch_WLine) wline;
2335   IntPatch_SequenceOfLine lines;
2336
2337   // build WLine parts (if any)
2338   nextline:;
2339
2340   //if((Lindex-Findex+1) <= 2 )
2341   if((Lindex-Findex+1) <= 1 )
2342     return;
2343
2344   // reset variables
2345   Standard_Boolean isDecomposited = Standard_False;
2346   U1 = 0.; V1 = 0.; U2 = 0.; V2 = 0.; AnU1 = 0.;
2347
2348   // analyze other points
2349   for(k = Findex; k <= Lindex; k++) {
2350
2351     if( k == Findex ) {
2352  
2353       if(IsReversed) { 
2354         SSLine->Value(k).ParametersOnS2(AnU1,V1);   // S2 - quadric, set U,V by Pnt3D
2355       }
2356       else { 
2357         SSLine->Value(k).ParametersOnS1(AnU1,V1);    // S1 - quadric, set U,V by Pnt3D
2358       }
2359       sline->Add(SSLine->Value(k));
2360       continue;
2361     }
2362
2363     if(IsReversed) { 
2364       SSLine->Value(k).ParametersOnS2(U1,V1);   // S2 - quadric, set U,V by Pnt3D
2365     }
2366     else { 
2367       SSLine->Value(k).ParametersOnS1(U1,V1);    // S1 - quadric, set U,V by Pnt3D
2368     }
2369
2370     Standard_Real DeltaU = fabs(U1-AnU1);
2371     if(DeltaU > BSEAM) {
2372       Bindex = k;
2373       isDecomposited = Standard_True;
2374       break;
2375     }
2376     else if((DeltaU > BAPEX) && (k >= (Findex+10) && k <= (Lindex-10))) {
2377       Bindex = k;
2378       isDecomposited = Standard_True;
2379       break;
2380     }
2381     else {}
2382
2383     sline->Add(SSLine->Value(k));
2384     AnU1=U1;
2385   }
2386  
2387   IntSurf_PntOn2S aVF,aVL;
2388   Standard_Boolean addVF = Standard_False, addVL = Standard_False;
2389   VerifyVertices(sline,IsReversed,vline,TOL2DS,ArcTol,PDomain,aVF,addVF,aVL,addVL);
2390
2391   Standard_Boolean hasInternals = HasInternals(sline,vline);
2392
2393   Standard_Real D3F = 0., D3L = 0.;
2394   ToSmooth(sline,IsReversed,Quad,Standard_True,D3F);
2395   ToSmooth(sline,IsReversed,Quad,Standard_False,D3L);
2396
2397   if(D3F <= 1.5e-7 && sline->NbPoints() >=3) {
2398     D3F = sline->Value(2).Value().Distance(sline->Value(3).Value());
2399   }
2400   if(D3L <= 1.5e-7 && sline->NbPoints() >=3) {
2401     D3L = sline->Value(sline->NbPoints()-1).Value().Distance(sline->Value(sline->NbPoints()-2).Value());
2402   }
2403
2404   if(addVF || addVL) {
2405     Standard_Boolean isAdded = AddVertices(sline,aVF,addVF,aVL,addVL,D3F,D3L);
2406     if(isAdded) {
2407       ToSmooth(sline,IsReversed,Quad,Standard_True,D3F);
2408       ToSmooth(sline,IsReversed,Quad,Standard_False,D3L);
2409     }
2410   }
2411
2412   if(!hasInternals) {
2413     ForcedPurgePoints(sline,IsReversed,Quad);
2414   }
2415
2416   wline = new IntPatch_WLine(sline,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2());
2417
2418   aSPnt = sline->Value(1).Value();
2419   sline->Value(1).ParametersOnS1(U1,V1);
2420   sline->Value(1).ParametersOnS2(U2,V2);
2421   TPntF.SetValue(aSPnt,ArcTol,Standard_False);
2422   TPntF.SetParameters(U1,V1,U2,V2);
2423   TPntF.SetParameter(1.);
2424   wline->AddVertex(TPntF);
2425   wline->SetFirstPoint(1);
2426
2427   if(hasInternals) {
2428     PutIntVertices(wline,sline,IsReversed,vline,ArcTol);
2429   }
2430   
2431   aSPnt =  sline->Value(sline->NbPoints()).Value();
2432   sline->Value(sline->NbPoints()).ParametersOnS1(U1,V1);
2433   sline->Value(sline->NbPoints()).ParametersOnS2(U2,V2);
2434   TPntL.SetValue(aSPnt,ArcTol,Standard_False);
2435   TPntL.SetParameters(U1,V1,U2,V2);
2436   TPntL.SetParameter(sline->NbPoints());
2437   wline->AddVertex(TPntL);
2438   wline->SetLastPoint(sline->NbPoints());
2439
2440   IntPatch_SequenceOfLine segm;
2441   Standard_Boolean isSplited = SplitOnSegments(wline,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2(),ArcTol,segm);
2442
2443   if(!isSplited)
2444     Lines.Append(wline);
2445   else {
2446     Standard_Integer nbsegms = segm.Length();
2447     Standard_Integer iseg = 0;
2448     for(iseg = 1; iseg <= nbsegms; iseg++)
2449       Lines.Append(segm(iseg));
2450   }
2451
2452   if(isDecomposited) {
2453     Findex = Bindex;
2454     sline = new IntSurf_LineOn2S();
2455     goto nextline;
2456   }
2457 }
2458 /*
2459 // <-A
2460         {
2461           Standard_Integer aNbPnts;
2462           Standard_Real aU1,aV1,aU2,aV2;
2463           gp_Pnt aPx;
2464           //
2465           aNbPnts=thelin->NbPoints(); 
2466           printf(" WLine: aNbPnts=%d\n", aNbPnts);
2467           for(i=1; i <= aNbPnts; ++i) {
2468             const IntSurf_PntOn2S& aPoint = thelin->Value(i);
2469             aPx=aPoint.Value();
2470             aPoint.Parameters(aU1, aV1, aU2, aV2);
2471             printf(" point %d %lf %lf %lf %lf %lf %lf %lf\n", 
2472                    i, aPx.X(), aPx.Y(), aPx.Z(), aU1, aV1, aU2, aV2);
2473           }
2474         }
2475 */