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