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