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