7eab0bd50f0a5a3a87747b6972fd3bf519ef8bdf
[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   if(Quad.TypeQuadric() == GeomAbs_Cone) {
1514     Standard_Real Uapx = 0., Vapx = 0.;
1515     Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
1516     Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
1517     if(IsReversed) {
1518       Result->Value(1).ParametersOnS2(U1,V1);
1519       Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
1520     }
1521     else {
1522       Result->Value(1).ParametersOnS2(U1,V1);
1523       Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
1524     }
1525
1526     if(fabs(V1-Vapx) <= 1.e-3)
1527       IndexLimF = 12;
1528     if(fabs(V2-Vapx) <= 1.e-3)
1529       IndexLimL = 12;
1530   }
1531
1532   if(Quad.TypeQuadric() == GeomAbs_Sphere) {
1533     Standard_Real Vapx1 = M_PI/2., Vapx2 = -M_PI/2.;
1534     Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
1535     if(IsReversed) {
1536       Result->Value(1).ParametersOnS2(U1,V1);
1537       Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
1538     }
1539     else {
1540       Result->Value(1).ParametersOnS2(U1,V1);
1541       Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
1542     }
1543
1544     if(fabs(V1-Vapx1) <= 1.e-3 || fabs(V1-Vapx2) <= 1.e-3)
1545       IndexLimF = 12;
1546     if(fabs(V2-Vapx1) <= 1.e-3 || fabs(V2-Vapx2) <= 1.e-3)
1547       IndexLimL = 12;
1548   }
1549
1550   while(Result->NbPoints() > 2 && Index < IndexLimF) {
1551     Result->RemovePoint(2);
1552     Index++;
1553   }
1554   Index = 0;
1555   while(Result->NbPoints() > 2 && Index < IndexLimL) {
1556     Result->RemovePoint(Result->NbPoints()-1);
1557     Index++;
1558   }
1559 }
1560
1561 // DEBUG FUNCTION !!!
1562 #if 0
1563 static void DumpLine(Handle(IntSurf_LineOn2S)& Line,
1564                      Standard_Boolean          IsReversed,
1565                      Standard_Integer          Number)
1566 {
1567   cout << "DUMP LINE" << endl;
1568   Standard_Integer i;
1569   Standard_Real U,V;
1570   for(i = 1; i <= Line->NbPoints(); i++) {
1571     if(i <= Number || i >= (Line->NbPoints()-Number)) {
1572       if(IsReversed)
1573         Line->Value(i).ParametersOnS2(U,V); // S2 - quadric
1574       else
1575         Line->Value(i).ParametersOnS1(U,V); // S1 - quadric
1576       cout << "point p" << i << " " << U << " " << V << endl;
1577     }
1578   }
1579   cout << endl;
1580 }
1581 #endif
1582 // DEBUG FUNCTION !!!
1583
1584 static void SearchVertices(const Handle(IntSurf_LineOn2S)& Line,
1585                            const Handle(IntSurf_LineOn2S)& Vertices,
1586                            TColStd_Array1OfInteger&        PTypes)
1587 {
1588   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
1589   Standard_Integer ip = 0, iv = 0;
1590   for(ip = 1; ip <= nbp; ip++) {
1591     const IntSurf_PntOn2S& aP = Line->Value(ip);
1592     Standard_Integer type = 0;
1593     for(iv = 1; iv <= nbv; iv++) {
1594       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
1595       if(AreSamePoints(aP,aV)) {
1596         type = iv; 
1597         break;
1598       }
1599     }
1600     PTypes(ip) = type;
1601   }
1602 }
1603
1604 static inline Standard_Boolean IsSeamParameter(const Standard_Real U,
1605                                                const Standard_Real TOL2D)
1606 {
1607   return (fabs(U) <= TOL2D || fabs(2.*M_PI - U) <= TOL2D);
1608 }
1609
1610 static inline Standard_Real AdjustU(const Standard_Real U)
1611 {
1612   Standard_Real u = U, DBLPI = 2.*M_PI;
1613   if(u < 0. || u > DBLPI) {
1614     if(u < 0.)
1615       while(u < 0.)
1616         u += DBLPI;
1617     else
1618       while(u > DBLPI)
1619         u -= DBLPI;
1620   }
1621   return u;
1622 }
1623
1624 static inline void Correct2DBounds(const Standard_Real UF,
1625                                    const Standard_Real UL,
1626                                    const Standard_Real VF,
1627                                    const Standard_Real VL,
1628                                    const Standard_Real TOL2D,
1629                                    Standard_Real&      U,
1630                                    Standard_Real&      V)
1631 {
1632   Standard_Real Eps = 1.e-16;
1633   Standard_Real dUF = fabs(U - UF);
1634   Standard_Real dUL = fabs(U - UL);
1635   Standard_Real dVF = fabs(V - VF);
1636   Standard_Real dVL = fabs(V - VL);
1637   if(dUF <= TOL2D && dUF > Eps) U = UF;
1638   if(dUL <= TOL2D && dUL > Eps) U = UL;
1639   if(dVF <= TOL2D && dVF > Eps) V = VF;
1640   if(dVL <= TOL2D && dVL > Eps) V = VL;
1641 }
1642
1643 static void AdjustLine(Handle(IntSurf_LineOn2S)& Line,
1644                        const Standard_Boolean    IsReversed,
1645                        const Handle(Adaptor3d_HSurface)&         QSurf,
1646                        const Standard_Real       TOL2D)
1647 {
1648   Standard_Real VF = QSurf->FirstVParameter();
1649   Standard_Real VL = QSurf->LastVParameter();
1650   Standard_Real UF = QSurf->FirstUParameter();
1651   Standard_Real UL = QSurf->LastUParameter();
1652
1653   Standard_Integer nbp = Line->NbPoints(), ip = 0;
1654   Standard_Real U = 0., V = 0.;
1655   for(ip = 1; ip <= nbp; ip++) {
1656     if(IsReversed) {
1657       Line->Value(ip).ParametersOnS2(U,V);
1658       U = AdjustU(U);
1659       Correct2DBounds(UF,UL,VF,VL,TOL2D,U,V);
1660       Line->SetUV(ip,Standard_False,U,V);
1661     }
1662     else {
1663       Line->Value(ip).ParametersOnS1(U,V);
1664       U = AdjustU(U);
1665       Correct2DBounds(UF,UL,VF,VL,TOL2D,U,V);
1666       Line->SetUV(ip,Standard_True,U,V);
1667     }
1668   }
1669 }
1670
1671 static Standard_Boolean InsertSeamVertices(Handle(IntSurf_LineOn2S)&       Line,
1672                                            const Standard_Boolean          IsReversed,
1673                                            Handle(IntSurf_LineOn2S)&       Vertices,
1674                                            const TColStd_Array1OfInteger&  PTypes,
1675                                            const Standard_Real             TOL2D)
1676 {
1677   Standard_Boolean result = Standard_False;
1678   Standard_Integer ip = 0, nbp = Line->NbPoints();
1679   Standard_Real U = 0., V = 0.;
1680   for(ip = 1; ip <= nbp; ip++) {
1681     Standard_Integer ipt = PTypes(ip);
1682     if(ipt != 0) {
1683       const IntSurf_PntOn2S& aP = Line->Value(ip);
1684       if(IsReversed)
1685         aP.ParametersOnS2(U,V); // S2 - quadric
1686       else
1687         aP.ParametersOnS1(U,V); // S1 - quadric
1688       U = AdjustU(U);
1689       if(IsSeamParameter(U,TOL2D)) {
1690         if(ip == 1 || ip == nbp) {
1691           Standard_Real U1 = 0., V1 = 0.;
1692           Standard_Integer ipp = (ip == 1) ? (ip+1) : (ip-1);
1693           if(IsReversed)
1694             Line->Value(ipp).ParametersOnS2(U1,V1); // S2 - quadric
1695           else
1696             Line->Value(ipp).ParametersOnS1(U1,V1); // S1 - quadric
1697           Standard_Real u = AdjustUFirst(U,U1);
1698           if(fabs(u-U) >= 1.5*M_PI) {
1699             Standard_Real U2 = 0., V2 = 0.;
1700             if(IsReversed) {
1701               Line->Value(ip).ParametersOnS1(U2,V2); // prm
1702               Line->SetUV(ip,Standard_False,u,V);
1703               Line->SetUV(ip,Standard_True,U2,V2);
1704             }
1705             else {
1706               Line->Value(ip).ParametersOnS2(U2,V2); // prm
1707               Line->SetUV(ip,Standard_True,u,V);
1708               Line->SetUV(ip,Standard_False,U2,V2);
1709             }
1710           }
1711         }
1712         else {
1713           Standard_Integer ipp = ip - 1;
1714           Standard_Integer ipn = ip + 1;
1715           Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
1716           if(IsReversed) {
1717             Line->Value(ipp).ParametersOnS2(U1,V1); // quad
1718             Line->Value(ipn).ParametersOnS2(U2,V2); // quad
1719           }
1720           else {
1721             Line->Value(ipp).ParametersOnS1(U1,V1); // quad
1722             Line->Value(ipn).ParametersOnS1(U2,V2); // quad
1723           }
1724           U1 = AdjustU(U1);
1725           U2 = AdjustU(U2);
1726           Standard_Boolean pnearZero = (fabs(U1) < fabs(2.*M_PI-U1)) ? Standard_True : Standard_False;
1727           Standard_Boolean cnearZero = (fabs(U) < fabs(2.*M_PI-U)) ? Standard_True : Standard_False;
1728           if(pnearZero == cnearZero) {
1729             if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
1730               Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
1731               IntSurf_PntOn2S nP;
1732               nP.SetValue(aP.Value());
1733               Standard_Real U3 = 0., V3 = 0.;
1734               if(IsReversed) {
1735                 Line->Value(ip).ParametersOnS1(U3,V3); // prm
1736                 nP.SetValue(Standard_False,nU,V);
1737                 nP.SetValue(Standard_True,U3,V3);
1738               }
1739               else {
1740                 Line->Value(ip).ParametersOnS2(U3,V3); // prm
1741                 nP.SetValue(Standard_True,nU,V);
1742                 nP.SetValue(Standard_False,U3,V3);
1743               }
1744               Line->InsertBefore(ipn,nP);
1745               Vertices->Add(nP);
1746               result = Standard_True;
1747               break;
1748             }
1749           }
1750           else {
1751             if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
1752               Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
1753               IntSurf_PntOn2S nP;
1754               nP.SetValue(aP.Value());
1755               Standard_Real U3 = 0., V3 = 0.;
1756               if(IsReversed) {
1757                 Line->Value(ip).ParametersOnS1(U3,V3); // prm
1758                 nP.SetValue(Standard_False,nU,V);
1759                 nP.SetValue(Standard_True,U3,V3);
1760               }
1761               else {
1762                 Line->Value(ip).ParametersOnS2(U3,V3); // prm
1763                 nP.SetValue(Standard_True,nU,V);
1764                 nP.SetValue(Standard_False,U3,V3);
1765               }
1766               Line->InsertBefore(ip,nP);
1767               Vertices->Add(nP);
1768               result = Standard_True;
1769               break;
1770             }
1771             else {
1772 //            Line->InsertBefore(ip,Line->Value(ipn));
1773 //            Line->RemovePoint(ip+2);
1774 //            result = Standard_True;
1775 //            cout << "swap vertex " << endl;
1776 //            break;
1777             }
1778           }
1779         }
1780       }
1781     }
1782   }
1783   return result;
1784 }
1785
1786 static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
1787                      Standard_Boolean          IsReversed,
1788                      IntSurf_Quadric&          Quad,
1789                      Standard_Boolean          IsFirst,
1790                      Standard_Real&            D3D)
1791 {
1792   if(Line->NbPoints() <= 10)
1793     return;
1794   
1795   D3D = 0.;
1796   Standard_Integer NbTestPnts = Line->NbPoints() / 5;
1797   if(NbTestPnts < 5) NbTestPnts = 5;
1798
1799   Standard_Integer startp = (IsFirst) ? 2 : (Line->NbPoints() - NbTestPnts - 2);
1800   Standard_Integer ip = 0;
1801   Standard_Real Uc = 0., Vc = 0., Un = 0., Vn = 0., DDU = 0., DDV = 0.;
1802
1803   for(ip = startp; ip <= NbTestPnts; ip++) {
1804     if(IsReversed) {
1805       Line->Value(ip).ParametersOnS2(Uc,Vc); // S2 - quadric
1806       Line->Value(ip+1).ParametersOnS2(Un,Vn);
1807     }
1808     else {
1809       Line->Value(ip).ParametersOnS1(Uc,Vc); // S1 - quadric
1810       Line->Value(ip+1).ParametersOnS1(Un,Vn);
1811     }
1812     DDU += fabs(fabs(Uc)-fabs(Un));
1813     DDV += fabs(fabs(Vc)-fabs(Vn));
1814     
1815     if(ip > startp) {
1816       Standard_Real DP = Line->Value(ip).Value().Distance(Line->Value(ip-1).Value());
1817       D3D += DP;
1818     }
1819   }
1820
1821   DDU /= (Standard_Real) NbTestPnts + 1;
1822   DDV /= (Standard_Real) NbTestPnts + 1;
1823   
1824   D3D /= (Standard_Real) NbTestPnts + 1;
1825
1826
1827   Standard_Integer Index1 = (IsFirst) ? 1 : (Line->NbPoints());
1828   Standard_Integer Index2 = (IsFirst) ? 2 : (Line->NbPoints()-1);
1829   Standard_Integer Index3 = (IsFirst) ? 3 : (Line->NbPoints()-2);
1830
1831   Standard_Boolean doU = Standard_False;
1832   Standard_Boolean doV = Standard_False;
1833
1834   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., U3 = 0., V3 = 0.;
1835
1836   if(IsReversed) {
1837     Line->Value(Index1).ParametersOnS2(U1,V1); // S2 - quadric
1838     Line->Value(Index2).ParametersOnS2(U2,V2);
1839     Line->Value(Index3).ParametersOnS2(U3,V3);
1840   }
1841   else {
1842     Line->Value(Index1).ParametersOnS1(U1,V1); // S1 - quadric
1843     Line->Value(Index2).ParametersOnS1(U2,V2);
1844     Line->Value(Index3).ParametersOnS1(U3,V3);
1845   }
1846
1847   if(!doU && Quad.TypeQuadric() == GeomAbs_Sphere) {
1848     if(fabs(fabs(U1)-fabs(U2)) > (M_PI/16.)) doU = Standard_True;
1849     
1850     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
1851       if(fabs(V1-M_PI/2.) <= 1.e-9 || fabs(V1+M_PI/2.) <= 1.e-9) {}
1852       else {
1853         doU = Standard_False;
1854       }
1855     }
1856   }
1857   
1858   if(Quad.TypeQuadric() == GeomAbs_Cone) {
1859     Standard_Real Uapx = 0., Vapx = 0.;
1860     Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
1861
1862     if(fabs(fabs(U1)-fabs(U2)) > M_PI/32.) doU = Standard_True;
1863
1864     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
1865       if(fabs(V1-Vapx) <= 1.e-9) {}
1866       else {
1867         doU = Standard_False;
1868       }
1869     }
1870   }
1871
1872   if(fabs(fabs(V1)-fabs(V2)) > DDV) doV = Standard_True;
1873
1874   if(doU) {
1875     Standard_Real dU = Min((DDU/10.),5.e-8);
1876     Standard_Real U = (U2 > U3) ? (U2 + dU) : (U2 - dU);
1877     if(IsReversed)
1878       Line->SetUV(Index1,Standard_False,U,V1);
1879     else
1880       Line->SetUV(Index1,Standard_True,U,V1);
1881     U1 = U;
1882   }
1883
1884
1885 static Standard_Boolean TestMiddleOnPrm(const IntSurf_PntOn2S& aP,
1886                                         const IntSurf_PntOn2S& aV,
1887                                         const Standard_Boolean IsReversed,
1888                                         const Standard_Real    ArcTol,
1889                                         Handle(Adaptor3d_TopolTool)&  PDomain)
1890                                         
1891 {
1892   Standard_Boolean result = Standard_False;
1893   Standard_Real Up = 0., Vp = 0., Uv = 0., Vv = 0.;
1894   if(IsReversed) {
1895     aP.ParametersOnS1(Up,Vp); //S1 - parametric
1896     aV.ParametersOnS1(Uv,Vv);
1897   }
1898   else {
1899     aP.ParametersOnS2(Up,Vp); // S2 - parametric
1900     aV.ParametersOnS2(Uv,Vv);
1901   }
1902   Standard_Real Um = (Up + Uv)*0.5, Vm = (Vp + Vv)*0.5;
1903   gp_Pnt2d a2DPntM(Um,Vm);
1904   TopAbs_State PosM = PDomain->Classify(a2DPntM,ArcTol);
1905   if(PosM == TopAbs_ON || PosM == TopAbs_IN )
1906     result = Standard_True;
1907   return result;
1908 }
1909
1910 static void VerifyVertices(Handle(IntSurf_LineOn2S)& Line,
1911                            Standard_Boolean          IsReversed,
1912                            Handle(IntSurf_LineOn2S)& Vertices,
1913                            Standard_Real             TOL2D,
1914                            const Standard_Real       ArcTol,
1915                            Handle(Adaptor3d_TopolTool)&     PDomain,
1916                            IntSurf_PntOn2S&          VrtF,
1917                            Standard_Boolean&         AddFirst,
1918                            IntSurf_PntOn2S&          VrtL,
1919                            Standard_Boolean&         AddLast)
1920 {
1921   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
1922   Standard_Integer FIndexSame = 0, FIndexNear = 0, LIndexSame = 0, LIndexNear = 0;
1923   const IntSurf_PntOn2S& aPF = Line->Value(1);
1924   const IntSurf_PntOn2S& aPL = Line->Value(nbp);
1925   Standard_Real UF = 0., VF = 0., UL = 0., VL = 0.;
1926   if(IsReversed) {
1927     aPF.ParametersOnS2(UF,VF);
1928     aPL.ParametersOnS2(UL,VL);
1929   }
1930   else {
1931     aPF.ParametersOnS1(UF,VF);
1932     aPL.ParametersOnS1(UL,VL);
1933   }
1934   gp_Pnt2d a2DPF(UF,VF);
1935   gp_Pnt2d a2DPL(UL,VL);
1936   Standard_Real DistMinF = 1.e+100, DistMinL = 1.e+100;
1937   Standard_Integer FConjugated = 0, LConjugated = 0;
1938
1939   Standard_Integer iv = 0;
1940
1941   for(iv = 1; iv <= nbv; iv++) {
1942     Standard_Real Uv = 0., Vv = 0.;
1943     if(IsReversed) {
1944       Vertices->Value(iv).ParametersOnS2(Uv,Vv);
1945       Uv = AdjustU(Uv);
1946       Vertices->SetUV(iv,Standard_False,Uv,Vv);
1947     }
1948     else {
1949       Vertices->Value(iv).ParametersOnS1(Uv,Vv);
1950       Uv = AdjustU(Uv);
1951       Vertices->SetUV(iv,Standard_True,Uv,Vv);
1952     }
1953   }
1954
1955   for(iv = 1; iv <= nbv; iv++) {
1956     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
1957     if(AreSamePoints(aPF,aV)) {
1958       FIndexSame = iv;
1959       break;
1960     }
1961     else {
1962       Standard_Real Uv = 0., Vv = 0.;
1963       if(IsReversed)
1964         aV.ParametersOnS2(Uv,Vv);
1965       else
1966         aV.ParametersOnS1(Uv,Vv);
1967       gp_Pnt2d a2DV(Uv,Vv);
1968       Standard_Real Dist = a2DV.Distance(a2DPF);
1969       if(Dist < DistMinF) {
1970         DistMinF = Dist;
1971         FIndexNear = iv;
1972         if(FConjugated != 0)
1973           FConjugated = 0;
1974       }
1975       if(IsSeamParameter(Uv,TOL2D)) {
1976         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
1977         gp_Pnt2d a2DCV(Ucv,Vv);
1978         Standard_Real CDist = a2DCV.Distance(a2DPF);
1979         if(CDist < DistMinF) {
1980           DistMinF = CDist;
1981           FConjugated = iv;
1982           FIndexNear = iv;
1983         }
1984       }
1985     }
1986   }
1987
1988   for(iv = 1; iv <= nbv; iv++) {
1989     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
1990     if(AreSamePoints(aPL,aV)) {
1991       LIndexSame = iv;
1992       break;
1993     }
1994     else {
1995       Standard_Real Uv = 0., Vv = 0.;
1996       if(IsReversed)
1997         aV.ParametersOnS2(Uv,Vv);
1998       else
1999         aV.ParametersOnS1(Uv,Vv);
2000       gp_Pnt2d a2DV(Uv,Vv);
2001       Standard_Real Dist = a2DV.Distance(a2DPL);
2002       if(Dist < DistMinL) {
2003         DistMinL = Dist;
2004         LIndexNear = iv;
2005         if(LConjugated != 0)
2006           LConjugated = 0;
2007       }
2008       if(IsSeamParameter(Uv,TOL2D)) {
2009         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2010         gp_Pnt2d a2DCV(Ucv,Vv);
2011         Standard_Real CDist = a2DCV.Distance(a2DPL);
2012         if(CDist < DistMinL) {
2013           DistMinL = CDist;
2014           LConjugated = iv;
2015           LIndexNear = iv;
2016         }
2017       }
2018     }
2019   }
2020
2021   AddFirst = Standard_False;
2022   AddLast  = Standard_False;
2023
2024   if(FIndexSame == 0) {
2025     if(FIndexNear != 0) {
2026       const IntSurf_PntOn2S& aV = Vertices->Value(FIndexNear);
2027       Standard_Real Uv = 0., Vv = 0.;
2028       if(IsReversed)
2029         aV.ParametersOnS2(Uv,Vv);
2030       else
2031         aV.ParametersOnS1(Uv,Vv);
2032       if(IsSeamParameter(Uv,TOL2D)) {
2033         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2034         Standard_Boolean test = TestMiddleOnPrm(aPF,aV,IsReversed,ArcTol,PDomain);
2035         if(test) {
2036           VrtF.SetValue(aV.Value());
2037           if(IsReversed) {
2038             Standard_Real U2 = 0., V2 = 0.;
2039             aV.ParametersOnS1(U2,V2); // S1 - prm
2040             VrtF.SetValue(Standard_True,U2,V2);
2041             if(FConjugated == 0)
2042               VrtF.SetValue(Standard_False,Uv,Vv);
2043             else
2044               VrtF.SetValue(Standard_False,Ucv,Vv);
2045           }
2046           else {
2047             Standard_Real U2 = 0., V2 = 0.;
2048             aV.ParametersOnS2(U2,V2); // S2 - prm
2049             VrtF.SetValue(Standard_False,U2,V2);
2050             if(FConjugated == 0)
2051               VrtF.SetValue(Standard_True,Uv,Vv);
2052             else
2053               VrtF.SetValue(Standard_True,Ucv,Vv);
2054           }
2055           Standard_Real Dist3D = VrtF.Value().Distance(aPF.Value());
2056           if(Dist3D > 1.5e-7 && DistMinF > TOL2D) {
2057             AddFirst = Standard_True;
2058           }
2059         }
2060       }
2061       else {
2062         // to do: analyze internal vertex
2063       }
2064     }
2065   }
2066   
2067   if(LIndexSame == 0) {
2068     if(LIndexNear != 0) {
2069       const IntSurf_PntOn2S& aV = Vertices->Value(LIndexNear);
2070       Standard_Real Uv = 0., Vv = 0.;
2071       if(IsReversed)
2072         aV.ParametersOnS2(Uv,Vv);
2073       else
2074         aV.ParametersOnS1(Uv,Vv);
2075       if(IsSeamParameter(Uv,TOL2D)) {
2076         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2077         Standard_Boolean test = TestMiddleOnPrm(aPL,aV,IsReversed,ArcTol,PDomain);
2078         if(test) {
2079           VrtL.SetValue(aV.Value());
2080           if(IsReversed) {
2081             Standard_Real U2 = 0., V2 = 0.;
2082             aV.ParametersOnS1(U2,V2); // S1 - prm
2083             VrtL.SetValue(Standard_True,U2,V2);
2084             if(LConjugated == 0)
2085               VrtL.SetValue(Standard_False,Uv,Vv);
2086             else
2087               VrtL.SetValue(Standard_False,Ucv,Vv);
2088           }
2089           else {
2090             Standard_Real U2 = 0., V2 = 0.;
2091             aV.ParametersOnS2(U2,V2); // S2 - prm
2092             VrtL.SetValue(Standard_False,U2,V2);
2093             if(LConjugated == 0)
2094               VrtL.SetValue(Standard_True,Uv,Vv);
2095             else
2096               VrtL.SetValue(Standard_True,Ucv,Vv);
2097           }
2098           Standard_Real Dist3D = VrtL.Value().Distance(aPL.Value());
2099           if(Dist3D > 1.5e-7 && DistMinL > TOL2D) {
2100             AddLast = Standard_True;
2101           }
2102         }
2103       }
2104       else {
2105         // to do: analyze internal vertex
2106       }
2107     }
2108   }
2109 }
2110
2111 static Standard_Boolean AddVertices(Handle(IntSurf_LineOn2S)& Line,
2112                                     const IntSurf_PntOn2S&    VrtF,
2113                                     const Standard_Boolean    AddFirst,
2114                                     const IntSurf_PntOn2S&    VrtL,
2115                                     const Standard_Boolean    AddLast,
2116                                     const Standard_Real       D3DF,
2117                                     const Standard_Real       D3DL)
2118 {
2119   Standard_Boolean result = Standard_False;
2120   if(AddFirst) {
2121     Standard_Real DF = Line->Value(1).Value().Distance(VrtF.Value());
2122     if((D3DF*2.) > DF && DF > 1.5e-7) {
2123       Line->InsertBefore(1,VrtF);
2124       result = Standard_True;
2125     }
2126   }
2127   if(AddLast) {
2128     Standard_Real DL = Line->Value(Line->NbPoints()).Value().Distance(VrtL.Value());
2129     if((D3DL*2.) > DL && DL > 1.5e-7) {
2130       Line->Add(VrtL);
2131       result = Standard_True;
2132     }
2133   }
2134   return result;
2135 }
2136  
2137
2138 static void PutIntVertices(Handle(IntPatch_Line)&    Line,
2139                            Handle(IntSurf_LineOn2S)& Result,
2140                            Standard_Boolean          ,//IsReversed,
2141                            Handle(IntSurf_LineOn2S)& Vertices,
2142                            const Standard_Real       ArcTol)
2143 {
2144   Standard_Integer nbp = Result->NbPoints(), nbv = Vertices->NbPoints();
2145
2146   if(nbp < 3)
2147     return;
2148
2149   Handle(IntPatch_WLine)& WLine = (*((Handle(IntPatch_WLine)*)&Line));
2150   Standard_Integer ip = 0, iv = 0;
2151   gp_Pnt aPnt;
2152   IntPatch_Point thePnt;
2153   Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
2154   
2155   for(ip = 2; ip <= (nbp-1); ip++) {
2156     const IntSurf_PntOn2S& aP = Result->Value(ip);
2157     for(iv = 1; iv <= nbv; iv++) {
2158       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2159       if(AreSamePoints(aP,aV)) {
2160         aPnt = Result->Value(ip).Value();
2161         Result->Value(ip).ParametersOnS1(U1,V1);
2162         Result->Value(ip).ParametersOnS2(U2,V2);
2163         thePnt.SetValue(aPnt,ArcTol,Standard_False);
2164         thePnt.SetParameters(U1,V1,U2,V2);
2165         thePnt.SetParameter((Standard_Real)ip);
2166         WLine->AddVertex(thePnt);
2167       }
2168     }
2169   }
2170 }
2171
2172 static Standard_Boolean HasInternals(Handle(IntSurf_LineOn2S)& Line,
2173                                      Handle(IntSurf_LineOn2S)& Vertices)
2174 {
2175   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
2176   Standard_Integer ip = 0, iv = 0;
2177   Standard_Boolean result = Standard_False;
2178   
2179   if(nbp < 3)
2180     return result;
2181
2182   for(ip = 2; ip <= (nbp-1); ip++) {
2183     const IntSurf_PntOn2S& aP = Line->Value(ip);
2184     for(iv = 1; iv <= nbv; iv++) {
2185       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2186       if(AreSamePoints(aP,aV)) {
2187         result = Standard_True;
2188         break;
2189       }
2190     }
2191     if(result)
2192       break;
2193   }
2194   
2195   return result;
2196 }
2197 static Handle(IntPatch_WLine) MakeSplitWLine (Handle(IntPatch_WLine)&        WLine,
2198                                         Standard_Boolean         Tang,
2199                                         IntSurf_TypeTrans        Trans1,
2200                                         IntSurf_TypeTrans        Trans2,
2201                                         Standard_Real            ArcTol,
2202                                         Standard_Integer         ParFirst,
2203                                         Standard_Integer         ParLast)
2204 {
2205   Handle(IntSurf_LineOn2S) SLine = WLine->Curve();
2206   Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
2207  
2208  Standard_Integer ip = 0;
2209   for(ip = ParFirst; ip <= ParLast; ip++)
2210     sline->Add(SLine->Value(ip));
2211
2212   Handle(IntPatch_WLine) wline = new IntPatch_WLine(sline,Tang,Trans1,Trans2);
2213
2214   gp_Pnt aSPnt;
2215   IntPatch_Point TPntF,TPntL;
2216   Standard_Real uu1 = 0., vv1 = 0., uu2 = 0., vv2 = 0.;
2217
2218   aSPnt = sline->Value(1).Value();
2219   sline->Value(1).ParametersOnS1(uu1,vv1);
2220   sline->Value(1).ParametersOnS2(uu2,vv2);
2221   TPntF.SetValue(aSPnt,ArcTol,Standard_False);
2222   TPntF.SetParameters(uu1,vv1,uu2,vv2);
2223   TPntF.SetParameter(1.);
2224   wline->AddVertex(TPntF);
2225   wline->SetFirstPoint(1);
2226         
2227   aSPnt =  sline->Value(sline->NbPoints()).Value();
2228   sline->Value(sline->NbPoints()).ParametersOnS1(uu1,vv1);
2229   sline->Value(sline->NbPoints()).ParametersOnS2(uu2,vv2);
2230   TPntL.SetValue(aSPnt,ArcTol,Standard_False);
2231   TPntL.SetParameters(uu1,vv1,uu2,vv2);
2232   TPntL.SetParameter((Standard_Real)sline->NbPoints());
2233   wline->AddVertex(TPntL);
2234   wline->SetLastPoint(sline->NbPoints());
2235
2236   return wline;
2237 }
2238
2239 static Standard_Boolean SplitOnSegments(Handle(IntPatch_WLine)&        WLine,
2240                                         Standard_Boolean         Tang,
2241                                         IntSurf_TypeTrans        Trans1,
2242                                         IntSurf_TypeTrans        Trans2,
2243                                         Standard_Real            ArcTol,
2244                                         IntPatch_SequenceOfLine& Segments)
2245 {
2246   Standard_Boolean result = Standard_False;
2247   Segments.Clear();
2248
2249   Standard_Integer nbv = WLine->NbVertex();
2250   if(nbv > 3) {
2251     Standard_Integer iv = 0;
2252     for(iv = 1; iv < nbv; iv++) {
2253       Standard_Integer firstPar = (Standard_Integer) WLine->Vertex(iv).ParameterOnLine();
2254       Standard_Integer lastPar  = (Standard_Integer) WLine->Vertex(iv+1).ParameterOnLine();
2255       if((lastPar - firstPar) <= 1)
2256         continue;
2257       else {
2258         Handle(IntPatch_WLine) splitwline = MakeSplitWLine(WLine,Tang,Trans1,Trans2,ArcTol,firstPar,lastPar);
2259         Segments.Append(splitwline);
2260         if(!result)
2261           result = Standard_True;
2262       }
2263     }
2264   }
2265   return result;
2266 }
2267
2268 void DecomposeResult(Handle(IntPatch_Line)&   Line,
2269                      Standard_Boolean         IsReversed,
2270                      IntSurf_Quadric&         Quad,
2271                      Handle(Adaptor3d_TopolTool)&    PDomain,
2272                      Handle(Adaptor3d_HSurface)&              QSurf,
2273                      Standard_Real            ArcTol,
2274                      IntPatch_SequenceOfLine& Lines)
2275 {
2276   if( Line->ArcType() != IntPatch_Walking ) {
2277     Lines.Append(Line);
2278     return;
2279   }
2280
2281   Handle(IntPatch_WLine)& WLine = (*((Handle(IntPatch_WLine)*)&Line));
2282   Handle(IntSurf_LineOn2S) SLine = WLine->Curve();
2283
2284   if(SLine->NbPoints() <= 2) {
2285     Lines.Append(Line);
2286     return;
2287   }
2288
2289   Standard_Real TOL3D = 1.e-10, TOL2D = 1.e-8, TOL2DS = 1.e-5;
2290
2291   Handle(IntSurf_LineOn2S) vline = GetVertices(WLine,IsReversed,TOL3D,TOL2D);
2292   Handle(IntSurf_LineOn2S) SSLine = VSplitLine(SLine,IsReversed,QSurf,vline);
2293
2294   if(SSLine->NbPoints() <= 1)
2295     return;
2296
2297   AdjustLine(SSLine,IsReversed,QSurf,TOL2D);
2298
2299   Standard_Boolean isInserted = Standard_True;
2300   while(isInserted) {
2301     Standard_Integer NbPnts = SSLine->NbPoints();
2302     TColStd_Array1OfInteger ptypes(1,NbPnts);
2303     SearchVertices(SSLine,vline,ptypes);
2304     isInserted = InsertSeamVertices(SSLine,IsReversed,vline,ptypes,TOL2D);
2305   }
2306
2307   if(SSLine->NbPoints() == 2) {
2308     Standard_Real uu1 = 0., vv1 = 0., uu2 = 0., vv2 = 0.;
2309     if(IsReversed) { 
2310       SSLine->Value(1).ParametersOnS2(uu1,vv1);
2311       SSLine->Value(2).ParametersOnS2(uu2,vv2);
2312     }
2313     else {
2314       SSLine->Value(1).ParametersOnS1(uu1,vv1);
2315       SSLine->Value(2).ParametersOnS1(uu2,vv2);
2316     }
2317     gp_Pnt2d p1(uu1,vv1);
2318     gp_Pnt2d p2(uu2,vv2);
2319     if(p1.Distance(p2) > 2.e-7) {
2320       Standard_Real Dist3D = SSLine->Value(1).Value().Distance(SSLine->Value(2).Value());
2321       if(Dist3D > 3.e-7) {
2322         gp_Pnt aSPnt;
2323         IntPatch_Point TPntF,TPntL;
2324         Handle(IntPatch_WLine) wline = new IntPatch_WLine(SSLine,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2());
2325         aSPnt = SSLine->Value(1).Value();
2326         SSLine->Value(1).ParametersOnS1(uu1,vv1);
2327         SSLine->Value(1).ParametersOnS2(uu2,vv2);
2328         TPntF.SetValue(aSPnt,ArcTol,Standard_False);
2329         TPntF.SetParameters(uu1,vv1,uu2,vv2);
2330         TPntF.SetParameter(1.);
2331         wline->AddVertex(TPntF);
2332         wline->SetFirstPoint(1);
2333         aSPnt =  SSLine->Value(SSLine->NbPoints()).Value();
2334         SSLine->Value(SSLine->NbPoints()).ParametersOnS1(uu1,vv1);
2335         SSLine->Value(SSLine->NbPoints()).ParametersOnS2(uu2,vv2);
2336         TPntL.SetValue(aSPnt,ArcTol,Standard_False);
2337         TPntL.SetParameters(uu1,vv1,uu2,vv2);
2338         TPntL.SetParameter(SSLine->NbPoints());
2339         wline->AddVertex(TPntL);
2340         wline->SetLastPoint(SSLine->NbPoints());
2341         Lines.Append(wline);
2342         return;
2343       }
2344       else
2345         return;
2346     }
2347     else
2348       return;
2349   }
2350
2351   Standard_Integer NbPnts = SSLine->NbPoints();
2352
2353   Standard_Real BSEAM = 1.5*M_PI; // delta U crossing seam
2354   Standard_Real BAPEX = M_PI/16.;  // delta U crossing apex
2355   
2356   Standard_Integer k = 0;
2357   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., AnU1 = 0., AnV1 = 0., DU1 = 0., DV1 = 0.;
2358   Standard_Integer Findex = 1, Lindex = NbPnts, Bindex = 0;
2359
2360   gp_Pnt aPnt, aSPnt;
2361   IntPatch_Point TPntF,TPntL,TPntS;
2362
2363   Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
2364   Handle(IntPatch_WLine) wline;
2365   IntPatch_SequenceOfLine lines;
2366
2367   // build WLine parts (if any)
2368   nextline:;
2369
2370   //if((Lindex-Findex+1) <= 2 )
2371   if((Lindex-Findex+1) <= 1 )
2372     return;
2373
2374   // reset variables
2375   Standard_Boolean isDecomposited = Standard_False;
2376   Standard_Boolean is2PIDecomposed = Standard_False;
2377   U1 = 0.; V1 = 0.; U2 = 0.; V2 = 0.; AnU1 = 0.; AnV1 = 0.; DU1 = 0.; DV1 = 0.;
2378
2379   // analyze other points
2380   for(k = Findex; k <= Lindex; k++) {
2381
2382     if( k == Findex ) {
2383  
2384       if(IsReversed) { 
2385         SSLine->Value(k).ParametersOnS2(AnU1,V1);   // S2 - quadric, set U,V by Pnt3D
2386       }
2387       else { 
2388         SSLine->Value(k).ParametersOnS1(AnU1,V1);    // S1 - quadric, set U,V by Pnt3D
2389       }
2390       sline->Add(SSLine->Value(k));
2391       AnV1 = V1;
2392       continue;
2393     }
2394
2395     if(IsReversed) { 
2396       SSLine->Value(k).ParametersOnS2(U1,V1);   // S2 - quadric, set U,V by Pnt3D
2397     }
2398     else { 
2399       SSLine->Value(k).ParametersOnS1(U1,V1);    // S1 - quadric, set U,V by Pnt3D
2400     }
2401
2402     Standard_Real DeltaU = fabs(U1-AnU1);
2403     if(DeltaU > BSEAM) {
2404       Bindex = k;
2405       isDecomposited = Standard_True;
2406       is2PIDecomposed = Standard_True;
2407       break;
2408     }
2409     else if((DeltaU > BAPEX) && (k >= (Findex+10) && k <= (Lindex-10))) {
2410       Bindex = k;
2411       isDecomposited = Standard_True;
2412       break;
2413     }
2414     else {}
2415
2416     sline->Add(SSLine->Value(k));
2417     AnU1=U1;
2418     AnV1=V1;
2419   }
2420  
2421   IntSurf_PntOn2S aVF,aVL;
2422   Standard_Boolean addVF = Standard_False, addVL = Standard_False;
2423   VerifyVertices(sline,IsReversed,vline,TOL2DS,ArcTol,PDomain,aVF,addVF,aVL,addVL);
2424
2425   Standard_Boolean hasInternals = HasInternals(sline,vline);
2426
2427   Standard_Real D3F = 0., D3L = 0.;
2428   ToSmooth(sline,IsReversed,Quad,Standard_True,D3F);
2429   ToSmooth(sline,IsReversed,Quad,Standard_False,D3L);
2430
2431   if(D3F <= 1.5e-7 && sline->NbPoints() >=3) {
2432     D3F = sline->Value(2).Value().Distance(sline->Value(3).Value());
2433   }
2434   if(D3L <= 1.5e-7 && sline->NbPoints() >=3) {
2435     D3L = sline->Value(sline->NbPoints()-1).Value().Distance(sline->Value(sline->NbPoints()-2).Value());
2436   }
2437
2438   if(addVF || addVL) {
2439     Standard_Boolean isAdded = AddVertices(sline,aVF,addVF,aVL,addVL,D3F,D3L);
2440     if(isAdded) {
2441       ToSmooth(sline,IsReversed,Quad,Standard_True,D3F);
2442       ToSmooth(sline,IsReversed,Quad,Standard_False,D3L);
2443     }
2444   }
2445
2446   if(!hasInternals) {
2447     ForcedPurgePoints(sline,IsReversed,Quad);
2448   }
2449
2450   wline = new IntPatch_WLine(sline,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2());
2451
2452   aSPnt = sline->Value(1).Value();
2453   sline->Value(1).ParametersOnS1(U1,V1);
2454   sline->Value(1).ParametersOnS2(U2,V2);
2455   TPntF.SetValue(aSPnt,ArcTol,Standard_False);
2456   TPntF.SetParameters(U1,V1,U2,V2);
2457   TPntF.SetParameter(1.);
2458   wline->AddVertex(TPntF);
2459   wline->SetFirstPoint(1);
2460
2461   if(hasInternals) {
2462     PutIntVertices(wline,sline,IsReversed,vline,ArcTol);
2463   }
2464   
2465   aSPnt =  sline->Value(sline->NbPoints()).Value();
2466   sline->Value(sline->NbPoints()).ParametersOnS1(U1,V1);
2467   sline->Value(sline->NbPoints()).ParametersOnS2(U2,V2);
2468   TPntL.SetValue(aSPnt,ArcTol,Standard_False);
2469   TPntL.SetParameters(U1,V1,U2,V2);
2470   TPntL.SetParameter(sline->NbPoints());
2471   wline->AddVertex(TPntL);
2472   wline->SetLastPoint(sline->NbPoints());
2473
2474   IntPatch_SequenceOfLine segm;
2475   Standard_Boolean isSplited = SplitOnSegments(wline,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2(),ArcTol,segm);
2476
2477   if(!isSplited)
2478     Lines.Append(wline);
2479   else {
2480     Standard_Integer nbsegms = segm.Length();
2481     Standard_Integer iseg = 0;
2482     for(iseg = 1; iseg <= nbsegms; iseg++)
2483       Lines.Append(segm(iseg));
2484   }
2485
2486   if(isDecomposited) {
2487     Findex = Bindex;
2488     sline = new IntSurf_LineOn2S();
2489     goto nextline;
2490   }
2491 }
2492 /*
2493 // <-A
2494         {
2495           Standard_Integer aNbPnts;
2496           Standard_Real aU1,aV1,aU2,aV2;
2497           gp_Pnt aPx;
2498           //
2499           aNbPnts=thelin->NbPoints(); 
2500           printf(" WLine: aNbPnts=%d\n", aNbPnts);
2501           for(i=1; i <= aNbPnts; ++i) {
2502             const IntSurf_PntOn2S& aPoint = thelin->Value(i);
2503             aPx=aPoint.Value();
2504             aPoint.Parameters(aU1, aV1, aU2, aV2);
2505             printf(" point %d %lf %lf %lf %lf %lf %lf %lf\n", 
2506                    i, aPx.X(), aPx.Y(), aPx.Z(), aU1, aV1, aU2, aV2);
2507           }
2508         }
2509 */