1 // Created on: 1996-12-05
2 // Created by: Jean-Pierre COMBE/Odile Olivier
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
19 #include <AIS_Shape.hxx>
20 #include <AIS_TangentRelation.hxx>
21 #include <Bnd_Box.hxx>
22 #include <BRep_Tool.hxx>
23 #include <BRepAdaptor_Surface.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <DsgPrs_TangentPresentation.hxx>
27 #include <Geom_Circle.hxx>
28 #include <Geom_Ellipse.hxx>
29 #include <Geom_Line.hxx>
30 #include <Geom_Plane.hxx>
31 #include <Geom_Transformation.hxx>
32 #include <GeomAPI_ExtremaCurveCurve.hxx>
33 #include <gp_Circ.hxx>
38 #include <Precision.hxx>
39 #include <Prs3d_Presentation.hxx>
40 #include <Prs3d_Projector.hxx>
41 #include <Select3D_SensitiveSegment.hxx>
42 #include <SelectMgr_EntityOwner.hxx>
43 #include <SelectMgr_Selection.hxx>
44 #include <Standard_NotImplemented.hxx>
45 #include <Standard_Type.hxx>
46 #include <TColStd_ListIteratorOfListOfTransient.hxx>
47 #include <TopAbs_ShapeEnum.hxx>
48 #include <TopExp_Explorer.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopoDS_Vertex.hxx>
55 IMPLEMENT_STANDARD_RTTIEXT(AIS_TangentRelation,AIS_Relation)
57 //=======================================================================
58 //function : Constructor
60 //=======================================================================
61 AIS_TangentRelation::AIS_TangentRelation(const TopoDS_Shape& aFShape,
62 const TopoDS_Shape& aSShape,
63 const Handle(Geom_Plane)& aPlane,
64 const Standard_Integer anExternRef)
65 :myExternRef(anExternRef)
70 myAutomaticPosition = Standard_False;
73 //=======================================================================
74 //function : ExternRef
76 //=======================================================================
77 Standard_Integer AIS_TangentRelation::ExternRef()
82 //=======================================================================
83 //function : SetExternRef
85 //=======================================================================
86 void AIS_TangentRelation::SetExternRef(const Standard_Integer aRef)
91 //=======================================================================
94 //=======================================================================
95 void AIS_TangentRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&,
96 const Handle(Prs3d_Presentation)& aPresentation,
97 const Standard_Integer)
99 switch (myFShape.ShapeType())
103 ComputeTwoFacesTangent(aPresentation);
108 ComputeTwoEdgesTangent(aPresentation);
116 //=======================================================================
118 //purpose : to avoid warning
119 //=======================================================================
120 void AIS_TangentRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
121 const Handle(Prs3d_Presentation)& aPresentation)
123 // throw Standard_NotImplemented("AIS_TangentRelation::Compute(const Handle(Prs3d_Projector)&,const Handle(Prs3d_Presentation)&)");
124 PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
127 void AIS_TangentRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
128 const Handle(Geom_Transformation)& aTransformation,
129 const Handle(Prs3d_Presentation)& aPresentation)
131 // throw Standard_NotImplemented("AIS_TangentRelation::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
132 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
135 //=======================================================================
136 //function : ComputeSelection
138 //=======================================================================
139 void AIS_TangentRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
140 const Standard_Integer)
143 gp_Vec vec1 = vec.Multiplied(myLength);
144 gp_Vec vec2 = vec.Multiplied(-myLength);
145 gp_Pnt p1 = myPosition.Translated(vec1);
146 gp_Pnt p2 = myPosition.Translated(vec2);
148 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
149 Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(own,p1,p2);
150 aSelection->Add(seg);
153 //=======================================================================
154 //function : ComputeTwoFacesTangent
156 //=======================================================================
157 void AIS_TangentRelation::ComputeTwoFacesTangent
158 (const Handle(Prs3d_Presentation)& /*aPresentation*/)
162 // jfa 19/10/2000 begin
163 //=======================================================================
164 //function : ComputeTangencyPoint
166 //=======================================================================
167 static Standard_Boolean ComputeTangencyPoint(const Handle(Geom_Curve)& GC1,
168 const Handle(Geom_Curve)& GC2,
171 Standard_Real U1f = GC1->FirstParameter();
172 Standard_Real U1l = GC1->LastParameter();
173 Standard_Real U2f = GC2->FirstParameter();
174 Standard_Real U2l = GC2->LastParameter();
177 Standard_Real mindist=0;
178 GeomAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
179 for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++)
183 Standard_Real dist = P1.Distance(P2);
191 if ( (dist < mindist) || (dist < Precision::Confusion()) )
197 if ( dist < Precision::Confusion() )
199 if (GC1->IsInstance(STANDARD_TYPE(Geom_Line)))
201 continue; // tangent line and conic can have only one point with zero distance
203 gp_Vec aVector1,aVector2;
204 if (GC1->IsInstance(STANDARD_TYPE(Geom_Circle)))
206 Handle(Geom_Circle) circle (Handle(Geom_Circle)::DownCast (GC1));
207 Standard_Real par_inter = ElCLib::Parameter(circle->Circ(), P1);
208 ElCLib::D1(par_inter,circle->Circ(),P1,aVector1);
210 else if (GC1->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
212 Handle(Geom_Ellipse) ellipse (Handle(Geom_Ellipse)::DownCast (GC1));
213 Standard_Real par_inter = ElCLib::Parameter(ellipse->Elips(), P1);
214 ElCLib::D1(par_inter,ellipse->Elips(),P1,aVector1);
216 if (GC2->IsInstance(STANDARD_TYPE(Geom_Circle)))
218 Handle(Geom_Circle) circle (Handle(Geom_Circle)::DownCast (GC2));
219 Standard_Real par_inter = ElCLib::Parameter(circle->Circ(), P2);
220 ElCLib::D1(par_inter,circle->Circ(),P2,aVector2);
222 else if (GC2->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
224 Handle(Geom_Ellipse) ellipse (Handle(Geom_Ellipse)::DownCast (GC2));
225 Standard_Real par_inter = ElCLib::Parameter(ellipse->Elips(), P2);
226 ElCLib::D1(par_inter,ellipse->Elips(),P2,aVector2);
228 // if ( aVector1.IsParallel(aVector2, 100*Precision::Angular()) ) break;
229 if ( aVector1.IsParallel(aVector2, M_PI / 360.0) ) break; // 0.5 graduce
233 return Standard_True;
235 // jfa 19/10/2000 end
237 //=======================================================================
238 //function : ComputeTwoEdgesTangent
240 //=======================================================================
241 void AIS_TangentRelation::ComputeTwoEdgesTangent(const Handle(Prs3d_Presentation)& aPresentation)
243 Handle(Geom_Curve) copy1,copy2;
244 gp_Pnt ptat11,ptat12,ptat21,ptat22;
245 Standard_Boolean isInfinite1,isInfinite2;
246 Handle(Geom_Curve) extCurv;
247 if (!AIS::ComputeGeometry(TopoDS::Edge(myFShape),
248 TopoDS::Edge(mySShape),
257 isInfinite1,isInfinite2,
263 aPresentation->SetInfiniteState(isInfinite1 || isInfinite2);
265 BRepBuilderAPI_MakeFace makeface(myPlane->Pln());
266 TopoDS_Face face(makeface.Face());
267 BRepAdaptor_Surface adp(makeface.Face());
269 Standard_Integer typArg(0);
271 if (copy1->IsInstance(STANDARD_TYPE(Geom_Line)))
275 else if (copy1->IsInstance(STANDARD_TYPE(Geom_Circle)))
279 else if (copy1->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
285 if (copy2->IsInstance(STANDARD_TYPE(Geom_Line)))
289 else if (copy2->IsInstance(STANDARD_TYPE(Geom_Circle)))
293 else if (copy2->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
299 //First find the tangengy vector if exists
301 TopExp_Explorer expF(TopoDS::Edge(myFShape),TopAbs_VERTEX);
302 TopExp_Explorer expS(TopoDS::Edge(mySShape),TopAbs_VERTEX);
305 for (p = 0; expF.More(); expF.Next(),p++)
307 tab[p] = TopoDS::Vertex(expF.Current());
309 Standard_Boolean found(Standard_False);
310 for ( ; expS.More() && !found; expS.Next())
312 for ( Standard_Integer l = 0; l<=p && !found; l++)
314 found = ( expS.Current().IsSame(tab[l]));
315 if (found) VCom = TopoDS::Vertex(expS.Current());
320 gp_Pnt pint3d; // tangency point
321 gp_Dir theDir; // tangency direction
322 Standard_Real par_inter = 0.0; // parameter of tangency point
326 pint3d = BRep_Tool::Pnt(VCom);
329 // Otherwise it is found as if it was known that 2 curves
330 // are tangents (which must be the cases)
333 case 12: // circle line
335 Handle(Geom_Line) line (Handle(Geom_Line)::DownCast (copy1));
336 Handle(Geom_Circle) circle (Handle(Geom_Circle)::DownCast (copy2));
340 // it is enough to project the circus center on the straight line
341 par_inter = ElCLib::Parameter(line->Lin(), circle->Location());
342 pint3d = ElCLib::Value(par_inter, line->Lin());
345 theDir = line->Lin().Direction();
346 myLength = circle->Radius()/5.;
349 Standard_Real copy1Length = ptat12.Distance(ptat11);
350 if ( copy1Length < myLength )
351 myLength = copy1Length/3.;
355 case 21: // circle line
357 Handle(Geom_Circle) circle (Handle(Geom_Circle)::DownCast (copy1));
358 Handle(Geom_Line) line (Handle(Geom_Line)::DownCast (copy2));
362 // it is enough to project the circus center on the straight line
363 par_inter = ElCLib::Parameter(line->Lin(), circle->Location());
364 pint3d = ElCLib::Value(par_inter, line->Lin());
367 theDir = line->Lin().Direction();
368 myLength = circle->Radius()/5.;
371 Standard_Real copy2Length = ptat21.Distance(ptat22);
372 if ( copy2Length < myLength )
373 myLength = copy2Length/3.;
377 // jfa 19/10/2000 begin
378 case 13: // line ellipse
380 Handle(Geom_Line) line (Handle(Geom_Line)::DownCast (copy1));
381 Handle(Geom_Ellipse) ellipse (Handle(Geom_Ellipse)::DownCast (copy2));
385 ComputeTangencyPoint(line,ellipse,pint3d);
388 theDir = line->Lin().Direction();
389 myLength = ellipse->MajorRadius()/5.;
393 Standard_Real copy1Length = ptat12.Distance(ptat11);
394 if ( copy1Length < myLength )
395 myLength = copy1Length/3.;
399 case 31: // ellipse line
401 Handle(Geom_Ellipse) ellipse (Handle(Geom_Ellipse)::DownCast (copy1));
402 Handle(Geom_Line) line (Handle(Geom_Line)::DownCast (copy2));
406 ComputeTangencyPoint(line,ellipse,pint3d);
409 theDir = line->Lin().Direction();
410 myLength = ellipse->MajorRadius()/5.;
414 Standard_Real copy2Length = ptat21.Distance(ptat22);
415 if ( copy2Length < myLength )
416 myLength = copy2Length/3.;
420 case 22: // circle circle
422 Handle(Geom_Circle) circle1 (Handle(Geom_Circle)::DownCast (copy1));
423 Handle(Geom_Circle) circle2 (Handle(Geom_Circle)::DownCast (copy2));
424 Standard_Real R1 = circle1->Radius();
425 Standard_Real R2 = circle2->Radius();
426 myLength = Max(R1,R2)/5.0;
429 if ( (circle1->Location()).IsEqual(circle2->Location(),Precision::Confusion()) )
433 ElCLib::D1(par_inter,circle1->Circ(),pint3d,theVector);
437 ElCLib::D1(par_inter,circle2->Circ(),pint3d,theVector);
444 par_inter = ElCLib::Parameter(circle1->Circ(), circle2->Location());
445 ElCLib::D1(par_inter,circle1->Circ(),pint3d,theVector);
449 par_inter = ElCLib::Parameter(circle2->Circ(), circle1->Location());
450 ElCLib::D1(par_inter,circle2->Circ(),pint3d,theVector);
456 par_inter = ElCLib::Parameter(circle1->Circ(), pint3d);
457 ElCLib::D1(par_inter,circle1->Circ(),pint3d,theVector);
459 theDir = gp_Dir(theVector);
462 case 23: // circle ellipse
464 Handle(Geom_Circle) circle (Handle(Geom_Circle)::DownCast (copy1));
465 Handle(Geom_Ellipse) ellipse (Handle(Geom_Ellipse)::DownCast (copy2));
466 Standard_Real R1 = circle->Radius();
467 Standard_Real R2 = ellipse->MajorRadius();
468 myLength = Max(R1,R2)/5.0;
473 ComputeTangencyPoint(circle,ellipse,pint3d);
474 par_inter = ElCLib::Parameter(circle->Circ(), pint3d);
475 ElCLib::D1(par_inter,circle->Circ(),pint3d,theVector);
479 ComputeTangencyPoint(ellipse,circle,pint3d);
480 par_inter = ElCLib::Parameter(ellipse->Elips(), pint3d);
481 ElCLib::D1(par_inter,ellipse->Elips(),pint3d,theVector);
486 par_inter = ElCLib::Parameter(circle->Circ(), pint3d);
487 ElCLib::D1(par_inter,circle->Circ(),pint3d,theVector);
489 theDir = gp_Dir(theVector);
492 case 32: // ellipse circle
494 Handle(Geom_Ellipse) ellipse (Handle(Geom_Ellipse)::DownCast (copy1));
495 Handle(Geom_Circle) circle (Handle(Geom_Circle)::DownCast (copy2));
496 Standard_Real R1 = ellipse->MajorRadius();
497 Standard_Real R2 = circle->Radius();
498 myLength = Max(R1,R2)/5.0;
503 ComputeTangencyPoint(ellipse,circle,pint3d);
504 par_inter = ElCLib::Parameter( ellipse->Elips(), pint3d);
505 ElCLib::D1(par_inter,ellipse->Elips(),pint3d,theVector);
509 ComputeTangencyPoint(circle,ellipse,pint3d);
510 par_inter = ElCLib::Parameter( circle->Circ(), pint3d);
511 ElCLib::D1(par_inter,circle->Circ(),pint3d,theVector);
516 par_inter = ElCLib::Parameter(circle->Circ(), pint3d);
517 ElCLib::D1(par_inter,circle->Circ(),pint3d,theVector);
519 theDir = gp_Dir(theVector);
522 case 33: // ellipse ellipse
524 Handle(Geom_Ellipse) ellipse1 (Handle(Geom_Ellipse)::DownCast (copy1));
525 Handle(Geom_Ellipse) ellipse2 (Handle(Geom_Ellipse)::DownCast (copy2));
526 Standard_Real R1 = ellipse1->MajorRadius();
527 Standard_Real R2 = ellipse2->MajorRadius();
528 myLength = Max(R1,R2)/5.0;
533 ComputeTangencyPoint(ellipse1,ellipse2,pint3d);
534 par_inter = ElCLib::Parameter( ellipse1->Elips(), pint3d);
535 ElCLib::D1(par_inter,ellipse1->Elips(),pint3d,theVector);
539 ComputeTangencyPoint(ellipse2,ellipse1,pint3d);
540 par_inter = ElCLib::Parameter( ellipse2->Elips(), pint3d);
541 ElCLib::D1(par_inter,ellipse2->Elips(),pint3d,theVector);
546 par_inter = ElCLib::Parameter(ellipse1->Elips(), pint3d);
547 ElCLib::D1(par_inter,ellipse1->Elips(),pint3d,theVector);
549 theDir = gp_Dir(theVector);
552 // jfa 19/10/2000 end
560 myLength = Min(myLength,myArrowSize);
562 DsgPrs_TangentPresentation::Add(aPresentation,myDrawer,myAttach,myDir,myLength);
563 if ( (myExtShape != 0) && !extCurv.IsNull())
566 if ( myExtShape == 1 )
573 ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(myFShape),copy1,pf,pl);
582 ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(mySShape),copy2,pf,pl);