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