0022969: Wrong confusion in BRepLib_MakeEdge
[occt.git] / src / BRepLib / BRepLib_MakeEdge.cxx
1 // File:        BRepLib_MakeEdge.cxx
2 // Created:     Fri Jul 23 15:51:46 1993
3 // Author:      Remi LEQUETTE
4 //              <rle@nonox>
5 // Modified:    Wed Oct 23 09:17:47 1996
6 // Author:      Joelle CHAUVET
7 //              <jct@sgi38>
8 //              check ponctuallity (PRO4896)
9
10 #include <BRepLib_MakeEdge.ixx>
11 #include <BRepLib.hxx>
12 #include <BRep_Tool.hxx>
13 #include <BRep_Builder.hxx>
14 #include <TopoDS.hxx>
15 #include <Geom_Line.hxx>
16 #include <Geom_Circle.hxx>
17 #include <Geom_Ellipse.hxx>
18 #include <Geom_Parabola.hxx>
19 #include <Geom_Hyperbola.hxx>
20 #include <Geom_TrimmedCurve.hxx>
21 #include <Geom2d_TrimmedCurve.hxx>
22 #include <GeomAdaptor_Curve.hxx>
23 #include <Geom2dAdaptor_HCurve.hxx>
24 #include <GeomAdaptor_HSurface.hxx>
25 #include <Adaptor3d_CurveOnSurface.hxx>
26 #include <Extrema_ExtPC.hxx>
27 #include <gp.hxx>
28 #include <ElCLib.hxx>
29 #include <Precision.hxx>
30
31
32 //=======================================================================
33 //function : Project
34 //purpose  : project a vertex on a curve
35 //=======================================================================
36
37 static Standard_Boolean Project(const Handle(Geom_Curve)& C,
38                                 const TopoDS_Vertex& V,
39                                 Standard_Real& p)
40 {
41   Standard_Real Eps2 = BRep_Tool::Tolerance(V);
42   Eps2 *= Eps2;
43   
44   gp_Pnt P = BRep_Tool::Pnt(V);
45   GeomAdaptor_Curve GAC(C);
46   
47   // Afin de faire les extremas, on verifie les distances en bout
48   Standard_Real D1,D2;
49   gp_Pnt P1,P2;
50   P1 = GAC.Value(GAC.FirstParameter());
51   P2 = GAC.Value(GAC.LastParameter());
52   D1 = P1.SquareDistance(P);
53   D2 = P2.SquareDistance(P);
54   if ( (D1 < D2) && (D1 <= Eps2) ) {
55     p = GAC.FirstParameter();
56     return Standard_True;
57   }
58   else if ( (D2 < D1) && (D2 <= Eps2) ) {
59     p = GAC.LastParameter();
60     return Standard_True;
61   }
62
63   // Sinon, on calcule les extremas.
64
65   Extrema_ExtPC extrema(P,GAC);
66   if (extrema.IsDone()) {
67     Standard_Integer i, index = 0, n = extrema.NbExt();
68     Standard_Real Dist2 = RealLast(), dist2min;
69
70     for (i = 1; i <= n; i++) {
71       dist2min = extrema.SquareDistance(i);
72       if (dist2min < Dist2) {
73         index = i;
74         Dist2 = dist2min;
75       }
76     }
77
78     if (index != 0) {
79       if (Dist2 <= Eps2) {
80         p = (extrema.Point(index)).Parameter();
81         return Standard_True;
82       }
83     }
84   }
85   return Standard_False;
86 }
87
88
89 //=======================================================================
90 //function : Project
91 //purpose  : project a vertex on a curve on surface
92 //=======================================================================
93
94 static Standard_Boolean Project(const Handle(Geom2d_Curve)& C,
95                                 const Handle(Geom_Surface)& S,
96                                 const TopoDS_Vertex& V,
97                                 Standard_Real& p)
98 {
99   gp_Pnt P = BRep_Tool::Pnt(V);
100   Standard_Real Eps2 = BRep_Tool::Tolerance(V);
101   Eps2 *= Eps2;
102   
103   Handle(Geom2dAdaptor_HCurve) HG2AHC = new Geom2dAdaptor_HCurve();
104   HG2AHC->Set(C);
105   Handle(GeomAdaptor_HSurface) HGAHS = new GeomAdaptor_HSurface();
106   HGAHS->Set(S);
107   Adaptor3d_CurveOnSurface ACOS(HG2AHC,HGAHS);
108
109   Standard_Real D1,D2;
110   gp_Pnt P1,P2;
111   P1 = ACOS.Value(ACOS.FirstParameter());
112   P2 = ACOS.Value(ACOS.LastParameter());
113   D1 = P1.SquareDistance(P);
114   D2 = P2.SquareDistance(P);
115   if ( (D1 < D2) && (D1 <= Eps2) ) {
116     p = ACOS.FirstParameter();
117     return Standard_True;
118   }
119   else if ( (D2 < D1) && (D2 <= Eps2) ) {
120     p = ACOS.LastParameter();
121     return Standard_True;
122   }
123   
124   
125   Extrema_ExtPC extrema(P,ACOS);
126   
127   if (extrema.IsDone()) {
128     Standard_Integer i, index = 0, n = extrema.NbExt();
129     Standard_Real Dist2 = RealLast(), dist2min;
130     
131     for (i = 1; i <= n; i++) {
132       dist2min = extrema.SquareDistance(i);
133       if (dist2min < Dist2) {
134         index = i;
135         Dist2 = dist2min;
136       }
137     }
138     
139     if (index != 0) {
140       Extrema_POnCurv POC = extrema.Point(index);
141       if (P.SquareDistance(POC.Value()) <= Precision::Confusion() * Precision::Confusion()) {
142         p = POC.Parameter();
143         return Standard_True;
144       }
145     }
146   }
147   return Standard_False;
148 }
149
150 //=======================================================================
151 //function : BRepLib_MakeEdge
152 //purpose  : 
153 //=======================================================================
154
155 BRepLib_MakeEdge::BRepLib_MakeEdge()
156 {}
157
158 //=======================================================================
159 //function : BRepLib_MakeEdge
160 //purpose  : 
161 //=======================================================================
162
163 BRepLib_MakeEdge::BRepLib_MakeEdge(const TopoDS_Vertex& V1, 
164                                    const TopoDS_Vertex& V2)
165 {
166   gp_Pnt P1 = BRep_Tool::Pnt(V1);
167   gp_Pnt P2 = BRep_Tool::Pnt(V2);
168   Standard_Real l = P1.Distance(P2);
169   if (l <= gp::Resolution()) {
170     myError = BRepLib_LineThroughIdenticPoints;
171     return;
172   }
173   gp_Lin L(P1,gp_Vec(P1,P2));
174   Handle(Geom_Line) GL = new Geom_Line(L);
175   Init(GL,V1,V2,0,l);
176 }
177
178
179 //=======================================================================
180 //function : BRepLib_MakeEdge
181 //purpose  : 
182 //=======================================================================
183
184 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Pnt& P1, 
185                                    const gp_Pnt& P2)
186 {
187   Standard_Real l = P1.Distance(P2);
188   if (l <= gp::Resolution()) {
189     myError = BRepLib_LineThroughIdenticPoints;
190     return;
191   }
192   gp_Lin L(P1,gp_Vec(P1,P2));
193   Handle(Geom_Line) GL = new Geom_Line(L);
194   Init(GL,P1,P2,0,l);
195 }
196
197
198 //=======================================================================
199 //function : BRepLib_MakeEdge
200 //purpose  : 
201 //=======================================================================
202
203 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Lin& L)
204 {
205   Handle(Geom_Line) GL = new Geom_Line(L);
206   Init(GL);
207 }
208
209
210 //=======================================================================
211 //function : BRepLib_MakeEdge
212 //purpose  : 
213 //=======================================================================
214
215 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Lin& L, 
216                                    const Standard_Real p1, 
217                                    const Standard_Real p2)
218 {
219   Handle(Geom_Line) GL = new Geom_Line(L);
220   Init(GL,p1,p2);
221 }
222
223
224 //=======================================================================
225 //function : BRepLib_MakeEdge
226 //purpose  : 
227 //=======================================================================
228
229 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Lin& L, 
230                                    const gp_Pnt& P1, 
231                                    const gp_Pnt& P2)
232 {
233   Handle(Geom_Line) GL = new Geom_Line(L);
234   Init(GL,P1,P2);
235 }
236
237
238 //=======================================================================
239 //function : BRepLib_MakeEdge
240 //purpose  : 
241 //=======================================================================
242
243 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Lin& L, 
244                                    const TopoDS_Vertex& V1, 
245                                    const TopoDS_Vertex& V2)
246 {
247   Handle(Geom_Line) GL = new Geom_Line(L);
248   Init(GL,V1,V2);
249 }
250
251
252 //=======================================================================
253 //function : BRepLib_MakeEdge
254 //purpose  : 
255 //=======================================================================
256
257 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Circ& C) 
258 {
259   Handle(Geom_Circle) GC = new Geom_Circle(C);
260   Init(GC);
261 }
262
263
264 //=======================================================================
265 //function : BRepLib_MakeEdge
266 //purpose  : 
267 //=======================================================================
268
269 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Circ& C,
270                                    const Standard_Real p1,
271                                    const Standard_Real p2)
272 {
273   Handle(Geom_Circle) GC = new Geom_Circle(C);
274   Init(GC,p1,p2);
275 }
276
277
278 //=======================================================================
279 //function : BRepLib_MakeEdge
280 //purpose  : 
281 //=======================================================================
282
283 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Circ& C,
284                                    const gp_Pnt& P1,
285                                    const gp_Pnt& P2)
286 {
287   Handle(Geom_Circle) GC = new Geom_Circle(C);
288   Init(GC,P1,P2);
289 }
290
291
292 //=======================================================================
293 //function : BRepLib_MakeEdge
294 //purpose  : 
295 //=======================================================================
296
297 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Circ& C,
298                                    const TopoDS_Vertex& V1,
299                                    const TopoDS_Vertex& V2)
300 {
301   Handle(Geom_Circle) GC = new Geom_Circle(C);
302   Init(GC,V1,V2);
303 }
304
305
306 //=======================================================================
307 //function : BRepLib_MakeEdge
308 //purpose  : 
309 //=======================================================================
310
311 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Elips& E) 
312 {
313   Handle(Geom_Ellipse) GE = new Geom_Ellipse(E);
314   Init(GE);
315 }
316
317
318 //=======================================================================
319 //function : BRepLib_MakeEdge
320 //purpose  : 
321 //=======================================================================
322
323 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Elips& E,
324                                    const Standard_Real p1,
325                                    const Standard_Real p2)
326 {
327   Handle(Geom_Ellipse) GE = new Geom_Ellipse(E);
328   Init(GE,p1,p2);
329 }
330
331
332 //=======================================================================
333 //function : BRepLib_MakeEdge
334 //purpose  : 
335 //=======================================================================
336
337 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Elips& E,
338                                    const gp_Pnt& P1,
339                                    const gp_Pnt& P2)
340 {
341   Handle(Geom_Ellipse) GE = new Geom_Ellipse(E);
342   Init(GE,P1,P2);
343 }
344
345
346 //=======================================================================
347 //function : BRepLib_MakeEdge
348 //purpose  : 
349 //=======================================================================
350
351 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Elips& E,
352                                    const TopoDS_Vertex& V1,
353                                    const TopoDS_Vertex& V2)
354 {
355   Handle(Geom_Ellipse) GE = new Geom_Ellipse(E);
356   Init(GE,V1,V2);
357 }
358
359
360 //=======================================================================
361 //function : BRepLib_MakeEdge
362 //purpose  : 
363 //=======================================================================
364
365 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Hypr& H)
366 {
367   Handle(Geom_Hyperbola) GH = new Geom_Hyperbola(H);
368   Init(GH);
369 }
370
371
372 //=======================================================================
373 //function : BRepLib_MakeEdge
374 //purpose  : 
375 //=======================================================================
376
377 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Hypr& H,
378                                    const Standard_Real p1,
379                                    const Standard_Real p2)
380 {
381   Handle(Geom_Hyperbola) GH = new Geom_Hyperbola(H);
382   Init(GH,p1,p2);
383 }
384
385
386 //=======================================================================
387 //function : BRepLib_MakeEdge
388 //purpose  : 
389 //=======================================================================
390
391 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Hypr& H,
392                                    const gp_Pnt& P1,
393                                    const gp_Pnt& P2)
394 {
395   Handle(Geom_Hyperbola) GH = new Geom_Hyperbola(H);
396   Init(GH,P1,P2);
397 }
398
399
400 //=======================================================================
401 //function : BRepLib_MakeEdge
402 //purpose  : 
403 //=======================================================================
404
405 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Hypr& H,
406                                    const TopoDS_Vertex& V1,
407                                    const TopoDS_Vertex& V2)
408 {
409   Handle(Geom_Hyperbola) GH = new Geom_Hyperbola(H);
410   Init(GH,V1,V2);
411 }
412
413
414 //=======================================================================
415 //function : BRepLib_MakeEdge
416 //purpose  : 
417 //=======================================================================
418
419 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Parab& P)
420 {
421   Handle(Geom_Parabola) GP = new Geom_Parabola(P);
422   Init(GP);
423 }
424
425
426 //=======================================================================
427 //function : BRepLib_MakeEdge
428 //purpose  : 
429 //=======================================================================
430
431 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Parab& P,
432                                    const Standard_Real p1,
433                                    const Standard_Real p2)
434 {
435   Handle(Geom_Parabola) GP = new Geom_Parabola(P);
436   Init(GP,p1,p2);
437 }
438
439
440 //=======================================================================
441 //function : BRepLib_MakeEdge
442 //purpose  : 
443 //=======================================================================
444
445 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Parab& P,
446                                    const gp_Pnt& P1,
447                                    const gp_Pnt& P2)
448 {
449   Handle(Geom_Parabola) GP = new Geom_Parabola(P);
450   Init(GP,P1,P2);
451 }
452
453
454 //=======================================================================
455 //function : BRepLib_MakeEdge
456 //purpose  : 
457 //=======================================================================
458
459 BRepLib_MakeEdge::BRepLib_MakeEdge(const gp_Parab& P,
460                                    const TopoDS_Vertex& V1,
461                                    const TopoDS_Vertex& V2)
462 {
463   Handle(Geom_Parabola) GP = new Geom_Parabola(P);
464   Init(GP,V1,V2);
465 }
466
467
468 //=======================================================================
469 //function : BRepLib_MakeEdge
470 //purpose  : 
471 //=======================================================================
472
473 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom_Curve)& L)
474 {
475   Init(L);
476 }
477
478
479 //=======================================================================
480 //function : BRepLib_MakeEdge
481 //purpose  : 
482 //=======================================================================
483
484 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom_Curve)& L,
485                                    const Standard_Real p1,
486                                    const Standard_Real p2)
487 {
488   Init(L,p1,p2);
489 }
490
491
492 //=======================================================================
493 //function : BRepLib_MakeEdge
494 //purpose  : 
495 //=======================================================================
496
497 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom_Curve)& L,
498                                    const gp_Pnt& P1,
499                                    const gp_Pnt& P2)
500 {
501   Init(L,P1,P2);
502 }
503
504 //=======================================================================
505 //function : BRepLib_MakeEdge
506 //purpose  : 
507 //=======================================================================
508
509 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom_Curve)& L,
510                                    const TopoDS_Vertex& V1,
511                                    const TopoDS_Vertex& V2)
512 {
513   Init(L,V1,V2);
514 }
515
516
517 //=======================================================================
518 //function : BRepLib_MakeEdge
519 //purpose  : 
520 //=======================================================================
521
522 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom_Curve)& L,
523                                    const gp_Pnt& P1,
524                                    const gp_Pnt& P2,
525                                    const Standard_Real p1,
526                                    const Standard_Real p2)
527 {
528   Init(L,P1,P2,p1,p2);
529 }
530
531
532 //=======================================================================
533 //function : BRepLib_MakeEdge
534 //purpose  : 
535 //=======================================================================
536
537 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom_Curve)& L,
538                                    const TopoDS_Vertex& V1,
539                                    const TopoDS_Vertex& V2,
540                                    const Standard_Real p1,
541                                    const Standard_Real p2)
542 {
543   Init(L,V1,V2,p1,p2);
544 }
545
546
547
548 //=======================================================================
549 //function : BRepLib_MakeEdge
550 //purpose  : 
551 //=======================================================================
552
553 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom2d_Curve)& L,
554                                    const Handle(Geom_Surface)& S)
555 {
556   Init(L,S);
557 }
558
559
560 //=======================================================================
561 //function : BRepLib_MakeEdge
562 //purpose  : 
563 //=======================================================================
564
565 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom2d_Curve)& L,
566                                    const Handle(Geom_Surface)& S,
567                                    const Standard_Real p1,
568                                    const Standard_Real p2)
569 {
570   Init(L,S,p1,p2);
571 }
572
573
574 //=======================================================================
575 //function : BRepLib_MakeEdge
576 //purpose  : 
577 //=======================================================================
578
579 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom2d_Curve)& L,
580                                    const Handle(Geom_Surface)& S,
581                                    const gp_Pnt& P1,
582                                    const gp_Pnt& P2)
583 {
584   Init(L,S,P1,P2);
585 }
586
587 //=======================================================================
588 //function : BRepLib_MakeEdge
589 //purpose  : 
590 //=======================================================================
591
592 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom2d_Curve)& L,
593                                    const Handle(Geom_Surface)& S,
594                                    const TopoDS_Vertex& V1,
595                                    const TopoDS_Vertex& V2)
596 {
597   Init(L,S,V1,V2);
598 }
599
600
601 //=======================================================================
602 //function : BRepLib_MakeEdge
603 //purpose  : 
604 //=======================================================================
605
606 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom2d_Curve)& L,
607                                    const Handle(Geom_Surface)& S,
608                                    const gp_Pnt& P1,
609                                    const gp_Pnt& P2,
610                                    const Standard_Real p1,
611                                    const Standard_Real p2)
612 {
613   Init(L,S,P1,P2,p1,p2);
614 }
615
616
617 //=======================================================================
618 //function : BRepLib_MakeEdge
619 //purpose  : 
620 //=======================================================================
621
622 BRepLib_MakeEdge::BRepLib_MakeEdge(const Handle(Geom2d_Curve)& L,
623                                    const Handle(Geom_Surface)& S,
624                                    const TopoDS_Vertex& V1,
625                                    const TopoDS_Vertex& V2,
626                                    const Standard_Real p1,
627                                    const Standard_Real p2)
628 {
629   Init(L,S,V1,V2,p1,p2);
630 }
631
632
633 //=======================================================================
634 //function : Init
635 //purpose  : 
636 //=======================================================================
637
638 void  BRepLib_MakeEdge::Init(const Handle(Geom_Curve)& C)
639 {
640   Init(C,C->FirstParameter(),C->LastParameter());
641 }
642
643
644 //=======================================================================
645 //function : Init
646 //purpose  : 
647 //=======================================================================
648
649 void  BRepLib_MakeEdge::Init(const Handle(Geom_Curve)& C,
650                              const Standard_Real p1,
651                              const Standard_Real p2)
652 {
653 //  BRep_Builder B;
654
655   TopoDS_Vertex V1,V2;
656   Init(C,V1,V2,p1,p2);
657 }
658
659
660 //=======================================================================
661 //function : Init
662 //purpose  : 
663 //=======================================================================
664
665 void  BRepLib_MakeEdge::Init(const Handle(Geom_Curve)& C,
666                              const gp_Pnt& P1,
667                              const gp_Pnt& P2)
668 {
669   Standard_Real Tol = BRepLib::Precision();
670
671   BRep_Builder B;
672   TopoDS_Vertex V1,V2;
673   B.MakeVertex(V1,P1,Tol);
674   if (P1.Distance(P2) < Tol)
675     V2 = V1;
676   else
677     B.MakeVertex(V2,P2,Tol);
678   
679   Init(C,V1,V2);
680 }
681
682
683 //=======================================================================
684 //function : Init
685 //purpose  : 
686 //=======================================================================
687
688 void  BRepLib_MakeEdge::Init(const Handle(Geom_Curve)& C,
689                              const TopoDS_Vertex& V1,
690                              const TopoDS_Vertex& V2)
691 {
692   // try projecting the vertices on the curve
693
694   Standard_Real p1,p2;
695   
696   if (V1.IsNull())
697     p1 = C->FirstParameter();
698   else
699     if (!Project(C,V1,p1)) {
700       myError = BRepLib_PointProjectionFailed;
701       return;
702     }
703   if (V2.IsNull())
704     p2 = C->LastParameter();
705   else
706     if (!Project(C,V2,p2))  {
707       myError = BRepLib_PointProjectionFailed;
708       return;
709     }
710   
711   Init(C,V1,V2,p1,p2);
712 }
713
714
715 //=======================================================================
716 //function : Init
717 //purpose  : 
718 //=======================================================================
719
720 void  BRepLib_MakeEdge::Init(const Handle(Geom_Curve)& C,
721                              const gp_Pnt& P1,
722                              const gp_Pnt& P2,
723                              const Standard_Real p1,
724                              const Standard_Real p2)
725 {
726   Standard_Real Tol = BRepLib::Precision();
727   BRep_Builder B;
728
729   TopoDS_Vertex V1,V2;
730   B.MakeVertex(V1,P1,Tol);
731   if (P1.Distance(P2) < Tol)
732     V2 = V1;
733   else
734     B.MakeVertex(V2,P2,Tol);
735   
736   Init(C,V1,V2,p1,p2);
737 }
738
739
740 //=======================================================================
741 //function : Init
742 //purpose  : this one really makes the job ...
743 //=======================================================================
744
745 void  BRepLib_MakeEdge::Init(const Handle(Geom_Curve)& CC,
746                              const TopoDS_Vertex& VV1,
747                              const TopoDS_Vertex& VV2,
748                              const Standard_Real pp1,
749                              const Standard_Real pp2)
750 {
751   // kill trimmed curves
752   Handle(Geom_Curve) C = CC;
753   Handle(Geom_TrimmedCurve) CT = Handle(Geom_TrimmedCurve)::DownCast(C);
754   while (!CT.IsNull()) {
755     C = CT->BasisCurve();
756     CT = Handle(Geom_TrimmedCurve)::DownCast(C);
757   }
758
759   // check parameters
760   Standard_Real p1 = pp1;
761   Standard_Real p2 = pp2;
762   Standard_Real cf = C->FirstParameter();
763   Standard_Real cl = C->LastParameter();
764   Standard_Real epsilon = Precision::PConfusion();
765   Standard_Boolean periodic = C->IsPeriodic();
766
767
768   TopoDS_Vertex V1,V2;
769   if (periodic) {
770     // adjust in period
771     ElCLib::AdjustPeriodic(cf,cl,epsilon,p1,p2);
772     V1 = VV1;
773     V2 = VV2;
774   }
775   else {
776     // reordonate
777     if (p1 < p2) {
778       V1 = VV1;
779       V2 = VV2;
780     }
781     else {
782       V2 = VV1;
783       V1 = VV2;
784       Standard_Real x = p1;
785       p1 = p2;
786       p2 = x;
787     }
788
789     // check range
790     if ((cf - p1 > epsilon) || (p2 - cl > epsilon)) {
791       myError = BRepLib_ParameterOutOfRange;
792       return;
793     }
794
795     // check ponctuallity
796     if ((p2-p1) <= gp::Resolution()) {
797       myError = BRepLib_LineThroughIdenticPoints;
798       return;
799     }
800   }
801   
802   // compute points on the curve
803   Standard_Boolean p1inf = Precision::IsNegativeInfinite(p1);
804   Standard_Boolean p2inf = Precision::IsPositiveInfinite(p2);
805   gp_Pnt P1,P2;
806   if (!p1inf) P1 = C->Value(p1);
807   if (!p2inf) P2 = C->Value(p2);
808
809   Standard_Real preci = BRepLib::Precision();
810   BRep_Builder B;
811
812   // check for closed curve
813   Standard_Boolean closed = Standard_False;
814   if (!p1inf && !p2inf)
815     closed = (P1.Distance(P2) <= preci);
816
817   // check if the vertices are on the curve
818   if (closed) {
819     if (V1.IsNull() && V2.IsNull()) {
820       B.MakeVertex(V1,P1,preci);
821       V2 = V1;
822     }
823     else if (V1.IsNull())
824       V1 = V2;
825     else if (V2.IsNull())
826       V2 = V1;
827     else {
828       if (!V1.IsSame(V2)) {
829         myError = BRepLib_DifferentPointsOnClosedCurve;
830         return;
831       }
832       else if (P1.Distance(BRep_Tool::Pnt(V1)) > 
833                Max(preci,BRep_Tool::Tolerance(V1))) {
834         myError = BRepLib_DifferentPointsOnClosedCurve;
835         return;
836       }
837     }
838   }
839
840   else {    // not closed
841
842     if (p1inf) {
843       if (!V1.IsNull()) {
844         myError = BRepLib_PointWithInfiniteParameter;
845         return;
846       }
847     }
848     else {
849       if (V1.IsNull()) {
850         B.MakeVertex(V1,P1,preci);
851       }
852       else if (P1.Distance(BRep_Tool::Pnt(V1)) >
853                Max(preci,BRep_Tool::Tolerance(V1))) {
854         myError = BRepLib_DifferentsPointAndParameter;
855         return;
856       }
857     }
858     
859     if (p2inf) {
860       if (!V2.IsNull()) {
861         myError = BRepLib_PointWithInfiniteParameter;
862         return;
863       }
864     }
865     else {
866       if (V2.IsNull()) {
867         B.MakeVertex(V2,P2,preci);
868       }
869       else if (P2.Distance(BRep_Tool::Pnt(V2)) >
870                Max(preci,BRep_Tool::Tolerance(V2))){
871         myError = BRepLib_DifferentsPointAndParameter;
872         return;
873       }
874     }
875   }
876
877   V1.Orientation(TopAbs_FORWARD);
878   V2.Orientation(TopAbs_REVERSED);
879   myVertex1 = V1;
880   myVertex2 = V2;
881
882   TopoDS_Edge& E = TopoDS::Edge(myShape);
883   B.MakeEdge(E,C,preci);
884   if (!V1.IsNull()) {
885     B.Add(E,V1);
886   }
887   if (!V2.IsNull()) {
888     B.Add(E,V2);
889   }
890   B.Range(E,p1,p2);
891
892   myError = BRepLib_EdgeDone;
893   Done();
894 }
895
896 //=======================================================================
897 //function : Init
898 //purpose  : 
899 //=======================================================================
900
901 void  BRepLib_MakeEdge::Init(const Handle(Geom2d_Curve)& C,
902                              const Handle(Geom_Surface)& S)
903 {
904   Init(C,S,C->FirstParameter(),C->LastParameter());
905 }
906
907
908 //=======================================================================
909 //function : Init
910 //purpose  : 
911 //=======================================================================
912
913 void  BRepLib_MakeEdge::Init(const Handle(Geom2d_Curve)& C,
914                              const Handle(Geom_Surface)& S,
915                              const Standard_Real p1,
916                              const Standard_Real p2)
917 {
918 //  BRep_Builder B;
919
920   TopoDS_Vertex V1,V2;
921   Init(C,S,V1,V2,p1,p2);
922 }
923
924
925 //=======================================================================
926 //function : Init
927 //purpose  : 
928 //=======================================================================
929
930 void  BRepLib_MakeEdge::Init(const Handle(Geom2d_Curve)& C,
931                              const Handle(Geom_Surface)& S,
932                              const gp_Pnt& P1,
933                              const gp_Pnt& P2)
934 {
935   Standard_Real Tol = BRepLib::Precision();
936   
937   BRep_Builder B;
938   TopoDS_Vertex V1,V2;
939   B.MakeVertex(V1,P1,Tol);
940   if (P1.Distance(P2) < Tol)
941     V2 = V1;
942   else
943     B.MakeVertex(V2,P2,Tol);
944   
945   Init(C,S,V1,V2);
946 }
947
948
949 //=======================================================================
950 //function : Init
951 //purpose  : 
952 //=======================================================================
953
954 void  BRepLib_MakeEdge::Init(const Handle(Geom2d_Curve)& C,
955                              const Handle(Geom_Surface)& S,
956                              const TopoDS_Vertex& V1,
957                              const TopoDS_Vertex& V2)
958 {
959   // try projecting the vertices on the curve
960
961   Standard_Real p1,p2;
962   
963   if (V1.IsNull())
964     p1 = C->FirstParameter();
965   else
966     if (!Project(C,S,V1,p1)) {
967       myError = BRepLib_PointProjectionFailed;
968       return;
969     }
970   if (V2.IsNull())
971     p2 = C->LastParameter();
972   else
973     if (!Project(C,S,V2,p2))  {
974       myError = BRepLib_PointProjectionFailed;
975       return;
976     }
977   
978   Init(C,S,V1,V2,p1,p2);
979 }
980
981
982 //=======================================================================
983 //function : Init
984 //purpose  : 
985 //=======================================================================
986
987 void  BRepLib_MakeEdge::Init(const Handle(Geom2d_Curve)& C,
988                              const Handle(Geom_Surface)& S,
989                              const gp_Pnt& P1,
990                              const gp_Pnt& P2,
991                              const Standard_Real p1,
992                              const Standard_Real p2)
993 {
994   Standard_Real Tol = BRepLib::Precision();
995   BRep_Builder B;
996
997   TopoDS_Vertex V1,V2;
998   B.MakeVertex(V1,P1,Tol);
999   if (P1.Distance(P2) < Tol)
1000     V2 = V1;
1001   else
1002     B.MakeVertex( V2, P2, Tol);
1003   
1004   Init(C,S,V1,V2,p1,p2);
1005 }
1006
1007
1008 //=======================================================================
1009 //function : Init
1010 //purpose  : this one really makes the job ...
1011 //=======================================================================
1012
1013 void  BRepLib_MakeEdge::Init(const Handle(Geom2d_Curve)& CC,
1014                              const Handle(Geom_Surface)& S,
1015                              const TopoDS_Vertex& VV1,
1016                              const TopoDS_Vertex& VV2,
1017                              const Standard_Real pp1,
1018                              const Standard_Real pp2)
1019 {
1020   // kill trimmed curves
1021   Handle(Geom2d_Curve) C = CC;
1022   Handle(Geom2d_TrimmedCurve) CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
1023   while (!CT.IsNull()) {
1024     C = CT->BasisCurve();
1025     CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
1026   }
1027
1028   // check parameters
1029   Standard_Real p1 = pp1;
1030   Standard_Real p2 = pp2;
1031   Standard_Real cf = C->FirstParameter();
1032   Standard_Real cl = C->LastParameter();
1033   Standard_Real epsilon = Precision::PConfusion();
1034   Standard_Boolean periodic = C->IsPeriodic();
1035
1036
1037   TopoDS_Vertex V1,V2;
1038   Standard_Boolean reverse = Standard_False;
1039
1040   if (periodic) {
1041     // adjust in period
1042     ElCLib::AdjustPeriodic(cf,cl,epsilon,p1,p2);
1043     V1 = VV1;
1044     V2 = VV2;
1045   }
1046   else {
1047     // reordonate
1048     if (p1 < p2) {
1049       V1 = VV1;
1050       V2 = VV2;
1051     }
1052     else {
1053       V2 = VV1;
1054       V1 = VV2;
1055       Standard_Real x = p1;
1056       p1 = p2;
1057       p2 = x;
1058       reverse = Standard_True;
1059     }
1060
1061     // check range
1062     if ((cf - p1 > epsilon) || (p2 - cl > epsilon)) {
1063       myError = BRepLib_ParameterOutOfRange;
1064       return;
1065     }
1066   }
1067   
1068   // compute points on the curve
1069   Standard_Boolean p1inf = Precision::IsNegativeInfinite(p1);
1070   Standard_Boolean p2inf = Precision::IsPositiveInfinite(p2);
1071   gp_Pnt P1,P2;
1072   gp_Pnt2d P2d1,P2d2;
1073   if (!p1inf) {
1074     P2d1 = C->Value(p1);
1075     P1   = S->Value(P2d1.X(),P2d1.Y());
1076   }
1077   if (!p2inf) {
1078     P2d2 = C->Value(p2);
1079     P2   = S->Value(P2d2.X(),P2d2.Y());
1080   }
1081
1082   Standard_Real preci = BRepLib::Precision();
1083   BRep_Builder B;
1084
1085   // check for closed curve
1086   Standard_Boolean closed = Standard_False;
1087   if (!p1inf && !p2inf)
1088     closed = (P1.Distance(P2) <= preci);
1089
1090   // check if the vertices are on the curve
1091   if (closed) {
1092     if (V1.IsNull() && V2.IsNull()) {
1093       B.MakeVertex(V1,P1,preci);
1094       V2 = V1;
1095     }
1096     else if (V1.IsNull())
1097       V1 = V2;
1098     else if (V2.IsNull())
1099       V2 = V1;
1100     else {
1101       if (!V1.IsSame(V2)) {
1102         myError = BRepLib_DifferentPointsOnClosedCurve;
1103         return;
1104       }
1105       else if (P1.Distance(BRep_Tool::Pnt(V1)) > 
1106                Max(preci,BRep_Tool::Tolerance(V1))) {
1107         myError = BRepLib_DifferentPointsOnClosedCurve;
1108         return;
1109       }
1110     }
1111   }
1112
1113   else {    // not closed
1114
1115     if (p1inf) {
1116       if (!V1.IsNull()) {
1117         myError = BRepLib_PointWithInfiniteParameter;
1118         return;
1119       }
1120     }
1121     else {
1122       if (V1.IsNull()) {
1123         B.MakeVertex(V1,P1,preci);
1124       }
1125       else if (P1.Distance(BRep_Tool::Pnt(V1)) >
1126                Max(preci,BRep_Tool::Tolerance(V1))) {
1127         myError = BRepLib_DifferentsPointAndParameter;
1128         return;
1129       }
1130     }
1131     
1132     if (p2inf) {
1133       if (!V2.IsNull()) {
1134         myError = BRepLib_PointWithInfiniteParameter;
1135         return;
1136       }
1137     }
1138     else {
1139       if (V2.IsNull()) {
1140         B.MakeVertex(V2,P2,preci);
1141       }
1142       else if (P2.Distance(BRep_Tool::Pnt(V2)) >
1143                Max(preci,BRep_Tool::Tolerance(V2))){
1144         myError = BRepLib_DifferentsPointAndParameter;
1145         return;
1146       }
1147     }
1148   }
1149
1150   V1.Orientation(TopAbs_FORWARD);
1151   V2.Orientation(TopAbs_REVERSED);
1152   myVertex1 = V1;
1153   myVertex2 = V2;
1154
1155   TopoDS_Edge& E = TopoDS::Edge(myShape);
1156   B.MakeEdge(E);
1157   B.UpdateEdge(E,C,S,TopLoc_Location(),preci);
1158
1159   if (!V1.IsNull()) {
1160     B.Add(E,V1);
1161   }
1162   if (!V2.IsNull()) {
1163     B.Add(E,V2);
1164   }
1165   B.Range(E,p1,p2);
1166
1167   if (reverse)
1168     E.Orientation(TopAbs_REVERSED);
1169
1170   myError = BRepLib_EdgeDone;
1171   Done();
1172 }
1173
1174 //=======================================================================
1175 //function : Error
1176 //purpose  : 
1177 //=======================================================================
1178
1179 BRepLib_EdgeError BRepLib_MakeEdge::Error() const
1180 {
1181   return myError;
1182 }
1183
1184 //=======================================================================
1185 //function : Edge
1186 //purpose  : 
1187 //=======================================================================
1188
1189 const TopoDS_Edge&  BRepLib_MakeEdge::Edge()const 
1190 {
1191   return TopoDS::Edge(Shape());
1192 }
1193
1194
1195 //=======================================================================
1196 //function : Vertex1
1197 //purpose  : 
1198 //=======================================================================
1199
1200 const TopoDS_Vertex&  BRepLib_MakeEdge::Vertex1()const 
1201 {
1202   Check();
1203   return myVertex1;
1204 }
1205
1206
1207 //=======================================================================
1208 //function : Vertex2
1209 //purpose  : 
1210 //=======================================================================
1211
1212 const TopoDS_Vertex&  BRepLib_MakeEdge::Vertex2()const 
1213 {
1214   Check();
1215   return myVertex2;
1216 }
1217
1218
1219
1220 //=======================================================================
1221 //function : operator
1222 //purpose  : 
1223 //=======================================================================
1224
1225 BRepLib_MakeEdge::operator TopoDS_Edge() const
1226 {
1227   return Edge();
1228 }