0031431: Visualization, PrsMgr_PresentableObject - simplify HLR computing interface
[occt.git] / src / AIS / AIS_ConcentricRelation.cxx
1 // Created on: 1996-12-05
2 // Created by: Flore Lantheaume/Odile Olivier
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <AIS.hxx>
19 #include <AIS_ConcentricRelation.hxx>
20 #include <BRepAdaptor_Curve.hxx>
21 #include <DsgPrs_ConcentricPresentation.hxx>
22 #include <Geom_Circle.hxx>
23 #include <Geom_Plane.hxx>
24 #include <Geom_Transformation.hxx>
25 #include <GeomAbs_CurveType.hxx>
26 #include <gp_Ax1.hxx>
27 #include <gp_Ax2.hxx>
28 #include <gp_Dir.hxx>
29 #include <gp_Pln.hxx>
30 #include <gp_Pnt.hxx>
31 #include <gp_Vec.hxx>
32 #include <Prs3d_Presentation.hxx>
33 #include <Select3D_SensitiveCircle.hxx>
34 #include <Select3D_SensitiveSegment.hxx>
35 #include <SelectMgr_EntityOwner.hxx>
36 #include <SelectMgr_Selection.hxx>
37 #include <Standard_NotImplemented.hxx>
38 #include <Standard_Type.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
42
43 IMPLEMENT_STANDARD_RTTIEXT(AIS_ConcentricRelation,AIS_Relation)
44
45 //=======================================================================
46 //function : Constructor
47 //purpose  : 
48 //=======================================================================
49 AIS_ConcentricRelation::AIS_ConcentricRelation(
50         const TopoDS_Shape& aFShape, 
51         const TopoDS_Shape& aSShape, 
52         const Handle(Geom_Plane)& aPlane)
53 {
54   myFShape = aFShape;
55   mySShape = aSShape;
56   myPlane = aPlane;
57   myDir = aPlane->Pln().Axis().Direction();
58 }
59
60 //=======================================================================
61 //function : Compute
62 //purpose  : 
63 //=======================================================================
64 void AIS_ConcentricRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&, 
65                                      const Handle(Prs3d_Presentation)& aPresentation, 
66                                      const Standard_Integer)
67 {
68   TopAbs_ShapeEnum type2(mySShape.ShapeType());
69   aPresentation->SetInfiniteState(Standard_True);
70   switch (myFShape.ShapeType()) {
71   case TopAbs_EDGE: 
72     {
73       if (type2 == TopAbs_EDGE) ComputeTwoEdgesConcentric(aPresentation);
74       else if (type2 == TopAbs_VERTEX) ComputeEdgeVertexConcentric(aPresentation);
75     }
76   break;
77   
78   case TopAbs_VERTEX: 
79     {
80       if (type2 == TopAbs_VERTEX) ComputeTwoVerticesConcentric(aPresentation);
81       else if (type2 == TopAbs_EDGE) ComputeEdgeVertexConcentric(aPresentation);      
82     }
83   break;
84   default: {return;}
85   }  
86 }
87
88 //=======================================================================
89 //function : ComputeTwoEdgesConcentric
90 //purpose  : 
91 //=======================================================================
92 void AIS_ConcentricRelation::ComputeEdgeVertexConcentric(const Handle(Prs3d_Presentation)& aPresentation)
93 {
94   TopoDS_Edge E;
95   TopoDS_Vertex V;
96   if (myFShape.ShapeType() == TopAbs_EDGE) {
97     E = TopoDS::Edge(myFShape);
98     V = TopoDS::Vertex(mySShape);
99   }
100   else {
101     E = TopoDS::Edge(mySShape);
102     V = TopoDS::Vertex(myFShape);
103   }
104   gp_Pnt p1,p2;
105   Handle(Geom_Curve) C;
106   Handle(Geom_Curve) extCurv;
107   Standard_Boolean isInfinite;
108   Standard_Boolean isOnPlanEdge, isOnPlanVertex;
109   if (!AIS::ComputeGeometry(E,C,p1,p2,extCurv,isInfinite,isOnPlanEdge,myPlane)) return;
110   gp_Pnt P;
111   AIS::ComputeGeometry(V,P, myPlane, isOnPlanVertex);
112
113   Handle(Geom_Circle) CIRCLE (Handle(Geom_Circle)::DownCast (C));
114   myCenter = CIRCLE->Location();
115   myRad = Min(CIRCLE->Radius()/5.,15.);
116   gp_Dir vec(p1.XYZ() - myCenter.XYZ() );
117   gp_Vec vectrans(vec);
118   myPnt = myCenter.Translated(vectrans.Multiplied(myRad));
119   DsgPrs_ConcentricPresentation::Add(aPresentation,myDrawer,myCenter,myRad,myDir,myPnt);
120   if (!isOnPlanEdge) AIS::ComputeProjEdgePresentation(aPresentation,myDrawer,E,CIRCLE,p1,p2);
121   if (!isOnPlanVertex) AIS::ComputeProjVertexPresentation(aPresentation,myDrawer,V,P);
122 }
123
124 //=======================================================================
125 //function : ComputeTwoEdgesConcentric
126 //purpose  : 
127 //=======================================================================
128 void AIS_ConcentricRelation::ComputeTwoVerticesConcentric(const Handle(Prs3d_Presentation)& aPresentation)
129 {
130   TopoDS_Vertex V1,V2;
131   V1 = TopoDS::Vertex(myFShape);
132   V2 = TopoDS::Vertex(myFShape);  
133   Standard_Boolean isOnPlanVertex1(Standard_True),isOnPlanVertex2(Standard_True);
134   gp_Pnt P1,P2;
135   AIS::ComputeGeometry(V1,P1, myPlane,isOnPlanVertex1);
136   AIS::ComputeGeometry(V2,P2, myPlane,isOnPlanVertex2);
137   myCenter = P1;
138   myRad    = 15.;
139   gp_Dir vec(myPlane->Pln().Position().XDirection());
140   gp_Vec vectrans(vec);
141   myPnt = myCenter.Translated(vectrans.Multiplied(myRad));
142   DsgPrs_ConcentricPresentation::Add(aPresentation,myDrawer,myCenter,myRad,myDir,myPnt);
143   if (!isOnPlanVertex1) AIS::ComputeProjVertexPresentation(aPresentation,myDrawer,V1,P1);
144   if (!isOnPlanVertex2) AIS::ComputeProjVertexPresentation(aPresentation,myDrawer,V2,P2);
145 }
146
147 //=======================================================================
148 //function : ComputeTwoEdgesConcentric
149 //purpose  : 
150 //=======================================================================
151 void AIS_ConcentricRelation::ComputeTwoEdgesConcentric(const Handle(Prs3d_Presentation)& aPresentation)
152 {
153   BRepAdaptor_Curve curv1(TopoDS::Edge(myFShape));
154   BRepAdaptor_Curve curv2(TopoDS::Edge(mySShape));
155   
156   gp_Pnt ptat11,ptat12,ptat21,ptat22;
157   Handle(Geom_Curve) geom1,geom2;
158   Standard_Boolean isInfinite1,isInfinite2;
159   Handle(Geom_Curve) extCurv;
160   if (!AIS::ComputeGeometry(TopoDS::Edge(myFShape),
161                             TopoDS::Edge(mySShape),
162                             myExtShape,
163                             geom1,
164                             geom2,
165                             ptat11,
166                             ptat12,
167                             ptat21,
168                             ptat22,
169                             extCurv,
170                             isInfinite1,isInfinite2,
171                             myPlane)) {
172     return;
173   }
174   
175   Handle(Geom_Circle) gcirc1 (Handle(Geom_Circle)::DownCast (geom1));
176   Handle(Geom_Circle) gcirc2 (Handle(Geom_Circle)::DownCast (geom2));
177   
178   myCenter = gcirc1->Location();
179   
180   // choose the radius equal to 1/5 of the smallest radius of 
181   // 2 circles. Limit is imposed ( 0.02 by chance)
182   Standard_Real aRad1 = gcirc1->Radius();
183   Standard_Real aRad2 = gcirc2->Radius();
184   myRad = (aRad1 > aRad2 ) ? aRad2 : aRad1;
185   myRad /= 5;
186   if (myRad > 15.) myRad =15.;
187   
188   
189   //Calculate a point of circle of radius myRad
190   gp_Dir vec(ptat11.XYZ() - myCenter.XYZ() );
191   gp_Vec vectrans(vec);
192   myPnt = myCenter.Translated(vectrans.Multiplied(myRad));
193   
194   DsgPrs_ConcentricPresentation::Add(aPresentation,
195                                      myDrawer,
196                                      myCenter,
197                                      myRad,
198                                      myDir,
199                                      myPnt);
200   if ( (myExtShape != 0) &&  !extCurv.IsNull()) {
201     gp_Pnt pf, pl;
202     if ( myExtShape == 1 ) {
203       if (!isInfinite1) {
204         pf = ptat11; 
205         pl = ptat12;
206       }
207       ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(myFShape),gcirc1,pf,pl);
208     }
209     else {
210       if (!isInfinite2) {
211         pf = ptat21; 
212         pl = ptat22;
213       }
214       ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(mySShape),gcirc2,pf,pl);
215     }
216   }
217 }
218
219 //=======================================================================
220 //function : ComputeSelection
221 //purpose  : 
222 //=======================================================================
223
224 void AIS_ConcentricRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
225                                               const Standard_Integer)
226 {
227   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
228   
229   //Creation of 2 sensitive circles
230      // the greater
231   gp_Ax2 ax(myCenter, myDir);
232   Handle(Geom_Circle) Circ = new Geom_Circle(ax, myRad) ;
233   Handle(Select3D_SensitiveCircle) 
234     sensit = new Select3D_SensitiveCircle (own,
235                                            Circ);
236   aSelection->Add(sensit);
237      // the smaller
238   Circ->SetRadius(myRad/2);
239   sensit = new Select3D_SensitiveCircle (own,
240                                          Circ);
241   aSelection->Add(sensit);
242
243   //Creation of 2 segments sensitive for the cross
244   Handle(Select3D_SensitiveSegment) seg;
245   gp_Pnt otherPnt = myPnt.Mirrored(myCenter);
246   seg = new Select3D_SensitiveSegment(own,
247                                       otherPnt,
248                                       myPnt);
249   aSelection->Add(seg);
250
251   gp_Ax1 RotateAxis(myCenter, myDir);
252   gp_Pnt FPnt = myCenter.Rotated(RotateAxis, M_PI/2);
253   gp_Pnt SPnt = myCenter.Rotated(RotateAxis, -M_PI/2);
254   seg = new Select3D_SensitiveSegment(own,
255                                       FPnt,
256                                       SPnt);
257   aSelection->Add(seg);
258
259 }
260