0023024: Update headers of OCCT files
[occt.git] / src / BRepLib / BRepLib_MakeEdge2d.cxx
CommitLineData
b311480e 1// Created on: 1995-01-04
2// Created by: Bruno DUMORTIER
3// Copyright (c) 1995-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
7fd59977 21
22
23#include <BRepLib_MakeEdge2d.ixx>
24
25#include <BRepLib.hxx>
26#include <BRep_Tool.hxx>
27#include <BRep_Builder.hxx>
28#include <TopoDS.hxx>
29#include <Geom_Plane.hxx>
30#include <Geom2d_Line.hxx>
31#include <Geom2d_Circle.hxx>
32#include <Geom2d_Ellipse.hxx>
33#include <Geom2d_Parabola.hxx>
34#include <Geom2d_Hyperbola.hxx>
35#include <Geom2d_TrimmedCurve.hxx>
36#include <Geom2dAdaptor_Curve.hxx>
37#include <Extrema_ExtPC2d.hxx>
38#include <gp.hxx>
39#include <ElCLib.hxx>
40#include <ElSLib.hxx>
41#include <Precision.hxx>
42
43
44//=======================================================================
45//function : Point
46//purpose : make a 3d point on the current plane
47//=======================================================================
48
49static gp_Pnt Point(const gp_Pnt2d& P)
50{
51 return BRepLib::Plane()->Value(P.X(),P.Y());
52}
53
54//=======================================================================
55//function : Project
56//purpose : project a vertex on the current plane
57//=======================================================================
58
59static gp_Pnt2d Project(const TopoDS_Vertex& Ve)
60{
61 gp_Pnt P = BRep_Tool::Pnt(Ve);
62 Standard_Real U,V;
63 ElSLib::Parameters(BRepLib::Plane()->Pln(),P,U,V);
64 return gp_Pnt2d(U,V);
65}
66
67//=======================================================================
68//function : Project
69//purpose : project a vertex on a curve
70//=======================================================================
71
72static Standard_Boolean Project(const Handle(Geom2d_Curve)& C,
73 const TopoDS_Vertex& V,
74 Standard_Real& p)
75{
76 gp_Pnt2d P = Project(V);
77 Geom2dAdaptor_Curve AC(C);
78 if (AC.GetType() == GeomAbs_Line) {
79 p = ElCLib::LineParameter(AC.Line().Position(),P);
80 }
81 else if (AC.GetType() == GeomAbs_Circle) {
82 p = ElCLib::CircleParameter(AC.Circle().Position(),P);
83 }
84 else {
85 Extrema_ExtPC2d extrema(P,AC);
86 if (extrema.IsDone()) {
87 Standard_Integer i,n = extrema.NbExt();
88
89 Standard_Real d2 = RealLast();
90 for (i = 1; i <= n; i++) {
91 //OCC16852:if (extrema.IsMin(i)) {
92 const Standard_Real dd2 = extrema.SquareDistance(i);
93 if (dd2 < d2) {
94 d2 = dd2;
95 p = extrema.Point(i).Parameter();
96 }
97 //OCC16852:}
98 }
99 }
100 else
101 return Standard_False;
102 }
103 return Standard_True;
104}
105
106//=======================================================================
107//function : BRepLib_MakeEdge2d
108//purpose :
109//=======================================================================
110
111BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const TopoDS_Vertex& V1,
112 const TopoDS_Vertex& V2)
113{
114 gp_Pnt2d P1 = Project(V1);
115 gp_Pnt2d P2 = Project(V2);
116 Standard_Real l = P1.Distance(P2);
117 if (l <= gp::Resolution()) {
118 myError = BRepLib_LineThroughIdenticPoints;
119 return;
120 }
121 gp_Lin2d L(P1,gp_Vec2d(P1,P2));
122 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
123 Init(GL,V1,V2,0,l);
124}
125
126//=======================================================================
127//function : BRepLib_MakeEdge2d
128//purpose :
129//=======================================================================
130
131BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Pnt2d& P1,
132 const gp_Pnt2d& P2)
133{
134 Standard_Real l = P1.Distance(P2);
135 if (l <= gp::Resolution()) {
136 myError = BRepLib_LineThroughIdenticPoints;
137 return;
138 }
139 gp_Lin2d L(P1,gp_Vec2d(P1,P2));
140 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
141 Init(GL,P1,P2,0,l);
142}
143
144
145//=======================================================================
146//function : BRepLib_MakeEdge2d
147//purpose :
148//=======================================================================
149
150BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L)
151{
152 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
153 Init(GL);
154}
155
156//=======================================================================
157//function : BRepLib_MakeEdge2d
158//purpose :
159//=======================================================================
160
161BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
162 const Standard_Real p1,
163 const Standard_Real p2)
164{
165 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
166 Init(GL,p1,p2);
167}
168
169//=======================================================================
170//function : BRepLib_MakeEdge2d
171//purpose :
172//=======================================================================
173
174BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
175 const gp_Pnt2d& P1,
176 const gp_Pnt2d& P2)
177{
178 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
179 Init(GL,P1,P2);
180}
181
182//=======================================================================
183//function : BRepLib_MakeEdge2d
184//purpose :
185//=======================================================================
186
187BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Lin2d& L,
188 const TopoDS_Vertex& V1,
189 const TopoDS_Vertex& V2)
190{
191 Handle(Geom2d_Line) GL = new Geom2d_Line(L);
192 Init(GL,V1,V2);
193}
194
195//=======================================================================
196//function : BRepLib_MakeEdge2d
197//purpose :
198//=======================================================================
199
200BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C)
201{
202 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
203 Init(GC);
204}
205
206//=======================================================================
207//function : BRepLib_MakeEdge2d
208//purpose :
209//=======================================================================
210
211BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
212 const Standard_Real p1,
213 const Standard_Real p2)
214{
215 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
216 Init(GC,p1,p2);
217}
218
219//=======================================================================
220//function : BRepLib_MakeEdge2d
221//purpose :
222//=======================================================================
223
224BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
225 const gp_Pnt2d& P1,
226 const gp_Pnt2d& P2)
227{
228 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
229 Init(GC,P1,P2);
230}
231
232//=======================================================================
233//function : BRepLib_MakeEdge2d
234//purpose :
235//=======================================================================
236
237BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Circ2d& C,
238 const TopoDS_Vertex& V1,
239 const TopoDS_Vertex& V2)
240{
241 Handle(Geom2d_Circle) GC = new Geom2d_Circle(C);
242 Init(GC,V1,V2);
243}
244
245//=======================================================================
246//function : BRepLib_MakeEdge2d
247//purpose :
248//=======================================================================
249
250BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E)
251{
252 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
253 Init(GE);
254}
255
256//=======================================================================
257//function : BRepLib_MakeEdge2d
258//purpose :
259//=======================================================================
260
261BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
262 const Standard_Real p1,
263 const Standard_Real p2)
264{
265 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
266 Init(GE,p1,p2);
267}
268
269//=======================================================================
270//function : BRepLib_MakeEdge2d
271//purpose :
272//=======================================================================
273
274BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
275 const gp_Pnt2d& P1,
276 const gp_Pnt2d& P2)
277{
278 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
279 Init(GE,P1,P2);
280}
281
282//=======================================================================
283//function : BRepLib_MakeEdge2d
284//purpose :
285//=======================================================================
286
287BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Elips2d& E,
288 const TopoDS_Vertex& V1,
289 const TopoDS_Vertex& V2)
290{
291 Handle(Geom2d_Ellipse) GE = new Geom2d_Ellipse(E);
292 Init(GE,V1,V2);
293}
294
295//=======================================================================
296//function : BRepLib_MakeEdge2d
297//purpose :
298//=======================================================================
299
300BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H)
301{
302 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
303 Init(GH);
304}
305
306//=======================================================================
307//function : BRepLib_MakeEdge2d
308//purpose :
309//=======================================================================
310
311BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
312 const Standard_Real p1,
313 const Standard_Real p2)
314{
315 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
316 Init(GH,p1,p2);
317}
318
319//=======================================================================
320//function : BRepLib_MakeEdge2d
321//purpose :
322//=======================================================================
323
324BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
325 const gp_Pnt2d& P1,
326 const gp_Pnt2d& P2)
327{
328 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
329 Init(GH,P1,P2);
330}
331
332//=======================================================================
333//function : BRepLib_MakeEdge2d
334//purpose :
335//=======================================================================
336
337BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Hypr2d& H,
338 const TopoDS_Vertex& V1,
339 const TopoDS_Vertex& V2)
340{
341 Handle(Geom2d_Hyperbola) GH = new Geom2d_Hyperbola(H);
342 Init(GH,V1,V2);
343}
344
345//=======================================================================
346//function : BRepLib_MakeEdge2d
347//purpose :
348//=======================================================================
349
350BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P)
351{
352 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
353 Init(GP);
354}
355
356//=======================================================================
357//function : BRepLib_MakeEdge2d
358//purpose :
359//=======================================================================
360
361BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
362 const Standard_Real p1,
363 const Standard_Real p2)
364{
365 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
366 Init(GP,p1,p2);
367}
368
369//=======================================================================
370//function : BRepLib_MakeEdge2d
371//purpose :
372//=======================================================================
373
374BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
375 const gp_Pnt2d& P1,
376 const gp_Pnt2d& P2)
377{
378 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
379 Init(GP,P1,P2);
380}
381
382//=======================================================================
383//function : BRepLib_MakeEdge2d
384//purpose :
385//=======================================================================
386
387BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const gp_Parab2d& P,
388 const TopoDS_Vertex& V1,
389 const TopoDS_Vertex& V2)
390{
391 Handle(Geom2d_Parabola) GP = new Geom2d_Parabola(P);
392 Init(GP,V1,V2);
393}
394
395//=======================================================================
396//function : BRepLib_MakeEdge2d
397//purpose :
398//=======================================================================
399
400BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L)
401{
402 Init(L);
403}
404
405//=======================================================================
406//function : BRepLib_MakeEdge2d
407//purpose :
408//=======================================================================
409
410BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
411 const Standard_Real p1,
412 const Standard_Real p2)
413{
414 Init(L,p1,p2);
415}
416
417//=======================================================================
418//function : BRepLib_MakeEdge2d
419//purpose :
420//=======================================================================
421
422BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
423 const gp_Pnt2d& P1,
424 const gp_Pnt2d& P2)
425{
426 Init(L,P1,P2);
427}
428
429//=======================================================================
430//function : BRepLib_MakeEdge2d
431//purpose :
432//=======================================================================
433
434BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
435 const TopoDS_Vertex& V1,
436 const TopoDS_Vertex& V2)
437{
438 Init(L,V1,V2);
439}
440
441//=======================================================================
442//function : BRepLib_MakeEdge2d
443//purpose :
444//=======================================================================
445
446BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
447 const gp_Pnt2d& P1,
448 const gp_Pnt2d& P2,
449 const Standard_Real p1,
450 const Standard_Real p2)
451{
452 Init(L,P1,P2,p1,p2);
453}
454
455//=======================================================================
456//function : BRepLib_MakeEdge2d
457//purpose :
458//=======================================================================
459
460BRepLib_MakeEdge2d::BRepLib_MakeEdge2d(const Handle(Geom2d_Curve)& L,
461 const TopoDS_Vertex& V1,
462 const TopoDS_Vertex& V2,
463 const Standard_Real p1,
464 const Standard_Real p2)
465{
466 Init(L,V1,V2,p1,p2);
467}
468
469//=======================================================================
470//function : Init
471//purpose :
472//=======================================================================
473
474void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C)
475{
476 Init(C,C->FirstParameter(),C->LastParameter());
477}
478
479//=======================================================================
480//function : Init
481//purpose :
482//=======================================================================
483
484void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
485 const Standard_Real p1,
486 const Standard_Real p2)
487{
488// BRep_Builder B;
489
490 TopoDS_Vertex V1,V2;
491 Init(C,V1,V2,p1,p2);
492}
493
494//=======================================================================
495//function : Init
496//purpose :
497//=======================================================================
498
499void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
500 const gp_Pnt2d& P1,
501 const gp_Pnt2d& P2)
502{
503 BRep_Builder B;
504 TopoDS_Vertex V1,V2;
505 B.MakeVertex(V1,Point(P1),Precision::Confusion());
506 if (P1.Distance(P2) < Precision::Confusion())
507 V2 = V1;
508 else
509 B.MakeVertex(V2,Point(P2),Precision::Confusion());
510 Init(C,V1,V2);
511}
512
513//=======================================================================
514//function : Init
515//purpose :
516//=======================================================================
517
518void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
519 const TopoDS_Vertex& V1,
520 const TopoDS_Vertex& V2)
521{
522 // try projecting the vertices on the curve
523
524 Standard_Real p1,p2;
525
526 if (V1.IsNull())
527 p1 = C->FirstParameter();
528 else
529 if (!Project(C,V1,p1)) {
530 myError = BRepLib_PointProjectionFailed;
531 return;
532 }
533 if (V2.IsNull())
534 p2 = C->LastParameter();
535 else
536 if (!Project(C,V2,p2)) {
537 myError = BRepLib_PointProjectionFailed;
538 return;
539 }
540
541 Init(C,V1,V2,p1,p2);
542}
543
544
545//=======================================================================
546//function : Init
547//purpose :
548//=======================================================================
549
550void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& C,
551 const gp_Pnt2d& P1,
552 const gp_Pnt2d& P2,
553 const Standard_Real p1,
554 const Standard_Real p2)
555{
556 BRep_Builder B;
557
558 TopoDS_Vertex V1,V2;
559 B.MakeVertex(V1,Point(P1),Precision::Confusion());
560 if (P1.Distance(P2) < Precision::Confusion())
561 V2 = V1;
562 else
563 B.MakeVertex(V2,Point(P2),Precision::Confusion());
564
565
566 Init(C,V1,V2,p1,p2);
567}
568
569
570//=======================================================================
571//function : Init
572//purpose : this one really makes the job ...
573//=======================================================================
574
575void BRepLib_MakeEdge2d::Init(const Handle(Geom2d_Curve)& CC,
576 const TopoDS_Vertex& VV1,
577 const TopoDS_Vertex& VV2,
578 const Standard_Real pp1,
579 const Standard_Real pp2)
580{
581 // kill trimmed curves
582 Handle(Geom2d_Curve) C = CC;
583 Handle(Geom2d_TrimmedCurve) CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
584 while (!CT.IsNull()) {
585 C = CT->BasisCurve();
586 CT = Handle(Geom2d_TrimmedCurve)::DownCast(C);
587 }
588
589 // check parameters
590 Standard_Real p1 = pp1;
591 Standard_Real p2 = pp2;
592 Standard_Real cf = C->FirstParameter();
593 Standard_Real cl = C->LastParameter();
594 Standard_Real epsilon = Precision::Confusion();
595 Standard_Boolean periodic = C->IsPeriodic();
596
597
598 TopoDS_Vertex V1,V2;
599 if (periodic) {
600 // adjust in period
601 ElCLib::AdjustPeriodic(cf,cl,epsilon,p1,p2);
602 V1 = VV1;
603 V2 = VV2;
604 }
605 else {
606 // reordonate
607 if (p1 < p2) {
608 V1 = VV1;
609 V2 = VV2;
610 }
611 else {
612 V2 = VV1;
613 V1 = VV2;
614 Standard_Real x = p1;
615 p1 = p2;
616 p2 = x;
617 }
618
619 // check range
620 if ((cf - p1 > epsilon) || (p2 - cl > epsilon)) {
621 myError = BRepLib_ParameterOutOfRange;
622 return;
623 }
624 }
625
626 // compute points on the curve
627 Standard_Boolean p1inf = Precision::IsNegativeInfinite(p1);
628 Standard_Boolean p2inf = Precision::IsPositiveInfinite(p2);
629 gp_Pnt2d P1,P2;
630 if (!p1inf) P1 = C->Value(p1);
631 if (!p2inf) P2 = C->Value(p2);
632
633 Standard_Real preci = Precision::Confusion();
634 BRep_Builder B;
635
636 // check for closed curve
637 Standard_Boolean closed = Standard_False;
638 if (!p1inf && !p2inf)
639 closed = (P1.Distance(P2) <= preci);
640
641 // check if the vertices are on the curve
642 if (closed) {
643 if (V1.IsNull() && V2.IsNull()) {
644 B.MakeVertex(V1,Point(P1),preci);
645 V2 = V1;
646 }
647 else if (V1.IsNull())
648 V1 = V2;
649 else if (V2.IsNull())
650 V2 = V1;
651 else {
652 if (!V1.IsSame(V2)) {
653 myError = BRepLib_DifferentPointsOnClosedCurve;
654 return;
655 }
656 else if (Point(P1).Distance(BRep_Tool::Pnt(V1)) > preci) {
657 myError = BRepLib_DifferentPointsOnClosedCurve;
658 return;
659 }
660 }
661 }
662
663 else { // not closed
664
665 if (p1inf) {
666 if (!V1.IsNull()) {
667 myError = BRepLib_PointWithInfiniteParameter;
668 return;
669 }
670 }
671 else {
672 gp_Pnt P = Point(P1);
673 if (V1.IsNull()) {
674 B.MakeVertex(V1,P,preci);
675 }
676#if 0
677 // desctivate control (RLE) for speed in sketcher
678 else if (P.Distance(BRep_Tool::Pnt(V1)) > preci) {
679 myError = BRepLib_DifferentsPointAndParameter;
680 return;
681 }
682#endif
683 }
684
685 if (p2inf) {
686 if (!V2.IsNull()) {
687 myError = BRepLib_PointWithInfiniteParameter;
688 return;
689 }
690 }
691 else {
692 gp_Pnt P = Point(P2);
693 if (V2.IsNull()) {
694 B.MakeVertex(V2,P,preci);
695 }
696#if 0
697 // desctivate control (RLE) for speed in sketcher
698 else if (P.Distance(BRep_Tool::Pnt(V2)) > preci){
699 myError = BRepLib_DifferentsPointAndParameter;
700 return;
701 }
702#endif
703 }
704 }
705
706 V1.Orientation(TopAbs_FORWARD);
707 V2.Orientation(TopAbs_REVERSED);
708 myVertex1 = V1;
709 myVertex2 = V2;
710
711 TopoDS_Edge& E = TopoDS::Edge(myShape);
712 B.MakeEdge(E);
713 B.UpdateEdge(E,C,BRepLib::Plane(),TopLoc_Location(),preci);
714 if (!V1.IsNull()) {
715 B.Add(E,V1);
716 }
717 if (!V2.IsNull()) {
718 B.Add(E,V2);
719 }
720 B.Range(E,p1,p2);
721 Done();
722}
723
724//=======================================================================
725//function : Error
726//purpose :
727//=======================================================================
728
729BRepLib_EdgeError BRepLib_MakeEdge2d::Error() const
730{
731 return myError;
732}
733
734//=======================================================================
735//function : Edge
736//purpose :
737//=======================================================================
738
739const TopoDS_Edge& BRepLib_MakeEdge2d::Edge()const
740{
741 return TopoDS::Edge(Shape());
742}
743
744
745//=======================================================================
746//function : Vertex1
747//purpose :
748//=======================================================================
749
750const TopoDS_Vertex& BRepLib_MakeEdge2d::Vertex1()const
751{
752 Check();
753 return myVertex1;
754}
755
756
757//=======================================================================
758//function : Vertex2
759//purpose :
760//=======================================================================
761
762const TopoDS_Vertex& BRepLib_MakeEdge2d::Vertex2()const
763{
764 Check();
765 return myVertex2;
766}
767
768
769
770//=======================================================================
771//function : operator
772//purpose :
773//=======================================================================
774
775BRepLib_MakeEdge2d::operator TopoDS_Edge() const
776{
777 return Edge();
778}