0025773: Visualization - remove AIS_Drawer class and transfer its Link() logic to...
[occt.git] / src / AIS / AIS_EqualRadiusRelation.cxx
1 // Created on: 1998-01-20
2 // Created by: Julia GERASIMOVA
3 // Copyright (c) 1998-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 <Standard_NotImplemented.hxx>
18
19 #include <AIS_EqualRadiusRelation.ixx>
20
21 #include <AIS.hxx>
22 #include <DsgPrs_EqualRadiusPresentation.hxx>
23 #include <BRepAdaptor_Curve.hxx>
24 #include <TopoDS.hxx>
25 #include <gp_Circ.hxx>
26 #include <Geom_Circle.hxx>
27 #include <ElCLib.hxx>
28 #include <SelectMgr_EntityOwner.hxx>
29 #include <Select3D_SensitiveSegment.hxx>
30 #include <Select3D_SensitiveBox.hxx>
31 #include <Precision.hxx>
32 #include <GeomAPI_ProjectPointOnSurf.hxx>
33 #include <Prs3d_DimensionAspect.hxx>
34 #include <Prs3d_ArrowAspect.hxx>
35 #include <Prs3d_Drawer.hxx>
36
37 //=======================================================================
38 //function : AIS_EqualRadiusRelation
39 //purpose  : 
40 //=======================================================================
41
42 AIS_EqualRadiusRelation::AIS_EqualRadiusRelation( const TopoDS_Edge& aFirstEdge,
43                                                   const TopoDS_Edge& aSecondEdge,
44                                                   const Handle( Geom_Plane )& aPlane )
45      :AIS_Relation()
46 {
47   myFShape = aFirstEdge;
48   mySShape = aSecondEdge;
49   myPlane  = aPlane;
50 }
51
52 //=======================================================================
53 //function : Compute
54 //purpose  :
55 //=======================================================================
56
57 void AIS_EqualRadiusRelation::Compute( const Handle( PrsMgr_PresentationManager3d )&,
58                                        const Handle( Prs3d_Presentation )& aPresentation,
59                                        const Standard_Integer ) 
60 {
61   aPresentation->Clear();
62
63   BRepAdaptor_Curve FirstCurve( TopoDS::Edge( myFShape ) ), SecondCurve( TopoDS::Edge( mySShape ) );
64
65   Standard_Real FirstPar1 = FirstCurve.FirstParameter(), LastPar1 = FirstCurve.LastParameter(),
66                 FirstPar2 = SecondCurve.FirstParameter(), LastPar2 = SecondCurve.LastParameter();
67
68   Handle( Geom_Curve ) FirstProjCurve = FirstCurve.Curve().Curve(),
69                        SecondProjCurve = SecondCurve.Curve().Curve();
70   gp_Pnt FirstPoint1, LastPoint1, FirstPoint2, LastPoint2;
71   Standard_Boolean isFirstOnPlane, isSecondOnPlane;
72
73   AIS::ComputeGeomCurve( FirstProjCurve, FirstPar1, LastPar1, FirstPoint1, LastPoint1, myPlane, isFirstOnPlane );
74   AIS::ComputeGeomCurve( SecondProjCurve, FirstPar2, LastPar2, FirstPoint2, LastPoint2, myPlane, isSecondOnPlane );
75
76   if (!isFirstOnPlane)
77     ComputeProjEdgePresentation( aPresentation, TopoDS::Edge( myFShape ), FirstProjCurve, FirstPoint1, LastPoint1 );
78   if (! isSecondOnPlane)
79     ComputeProjEdgePresentation( aPresentation, TopoDS::Edge( mySShape ), SecondProjCurve, FirstPoint2, LastPoint2 );
80
81   gp_Circ FirstCirc = (Handle( Geom_Circle )::DownCast( FirstProjCurve ))->Circ();
82   gp_Circ SecondCirc = (Handle( Geom_Circle )::DownCast( SecondProjCurve ))->Circ();
83   
84   myFirstCenter = FirstCirc.Location();
85   mySecondCenter = SecondCirc.Location();
86   
87   //ota -- begin --
88   if (myAutomaticPosition)
89     {
90       myFirstPoint = ElCLib::Value( (FirstPar1 + LastPar1)*0.5, FirstCirc );
91       mySecondPoint = ElCLib::Value( (FirstPar2 + LastPar2)*0.5, SecondCirc );
92     }
93   else {
94     Standard_Real aPar =  ElCLib::Parameter(FirstCirc, myFirstPoint);
95     if (IntegerPart(0.5*LastPar1/M_PI) != 0 &&  aPar < FirstPar1 )
96       aPar +=2*M_PI*IntegerPart(0.5*LastPar1/M_PI);
97     Standard_Real aRadius = FirstCirc.Radius();
98
99     if (Abs(myFirstPoint.Distance(myFirstCenter) - aRadius) >= Precision::Confusion())
100       myFirstPoint = ElCLib::Value(aPar, FirstCirc);
101     if ( FirstPoint1.Distance(LastPoint1) > Precision::Confusion()){
102       //check where is myFirstPoint
103       if (aPar > LastPar1 || aPar < FirstPar1)
104         {
105           //myFirstPoint is out of Arc of FirstCircle
106           if (FirstPoint1.Distance(myFirstPoint)< LastPoint1.Distance(myFirstPoint))
107             myFirstPoint = FirstPoint1; 
108           else
109             myFirstPoint = LastPoint1; 
110         }
111     }
112   
113     
114     aPar =  ElCLib::Parameter(SecondCirc, mySecondPoint);
115     if (IntegerPart(0.5*LastPar2/M_PI) != 0 &&  aPar < FirstPar2 )
116       aPar +=2*M_PI*IntegerPart(0.5*LastPar2/M_PI);
117     
118     aRadius = SecondCirc.Radius();
119     if (Abs(mySecondPoint.Distance(mySecondCenter) - aRadius) >= Precision::Confusion())
120       mySecondPoint =  ElCLib::Value(aPar, SecondCirc);
121     if (FirstPoint2.Distance(LastPoint2) > Precision::Confusion()){
122       if (aPar > LastPar2 || aPar < FirstPar2)
123         { //mySecondPoint is out of Arc of mySecondCircle
124           if (FirstPoint2.Distance(mySecondPoint)< LastPoint2.Distance(mySecondPoint))
125             mySecondPoint = FirstPoint2; 
126           else
127             mySecondPoint = LastPoint2;
128         }
129     }
130   }
131   if( !myArrowSizeIsDefined )
132     myArrowSize = (Min(myFirstCenter.Distance(myFirstPoint),
133                      mySecondCenter.Distance(mySecondPoint)))*0.05;
134   
135   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
136   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
137   arr->SetLength(myArrowSize);
138   
139   //ota -- end --
140   
141   DsgPrs_EqualRadiusPresentation::Add(aPresentation, myDrawer, 
142                                       myFirstCenter, mySecondCenter, myFirstPoint, mySecondPoint, myPlane );
143 }
144
145 //=======================================================================
146 //function : Compute
147 //purpose  : to avoid warning at compilation (SUN)
148 //=======================================================================
149
150 void AIS_EqualRadiusRelation::Compute( const Handle( Prs3d_Projector )& /*aProjector*/,
151                                        const Handle( Prs3d_Presentation )& /*aPresentation*/)
152 {
153 // Standard_NotImplemented::Raise("AIS_EqualRadiusRelation::Compute( const Handle( Prs3d_Projector )&,const Handle( Prs3d_Presentation )& )");
154 // PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
155 }
156
157 //=======================================================================
158 //function : Compute
159 //purpose  : 
160 //=======================================================================
161
162 void AIS_EqualRadiusRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
163                                       const Handle(Geom_Transformation)& aTransformation,
164                                       const Handle(Prs3d_Presentation)& aPresentation)
165 {
166 // Standard_NotImplemented::Raise("AIS_EqualRadiusRelation::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
167  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
168 }
169
170 //=======================================================================
171 //function : ComputeSelection
172 //purpose  : 
173 //=======================================================================
174
175 void AIS_EqualRadiusRelation::ComputeSelection( const Handle( SelectMgr_Selection )& aSelection,
176                                                 const Standard_Integer ) 
177 {
178   Handle( SelectMgr_EntityOwner ) own = new SelectMgr_EntityOwner( this, 7 );
179   Handle( Select3D_SensitiveSegment ) seg;
180
181   seg = new Select3D_SensitiveSegment( own, myFirstCenter, myFirstPoint );
182   aSelection->Add( seg );
183   
184   if(!myAutomaticPosition) 
185     ComputeRadiusPosition();
186   
187   seg = new Select3D_SensitiveSegment( own, mySecondCenter, mySecondPoint );
188   aSelection->Add( seg );
189   
190   seg = new Select3D_SensitiveSegment( own, myFirstCenter, mySecondCenter );
191   aSelection->Add( seg );
192   
193   
194   // Two small lines
195   gp_Pnt Middle( (myFirstCenter.XYZ() + mySecondCenter.XYZ())*0.5 );
196
197   Standard_Real SmallDist = .001;
198   //Should be changed as the domain of small lines could be changed.
199   Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox(own,
200                                                                   Middle.X() - SmallDist,
201                                                                   Middle.Y() - SmallDist,
202                                                                   Middle.Z() - SmallDist,
203                                                                   Middle.X() + SmallDist,
204                                                                   Middle.Y() + SmallDist,
205                                                                   Middle.Z() + SmallDist );
206   aSelection->Add(box);
207 }
208
209 //=================================================================
210 //function : ComputeRadiusPosition
211 //purpose  :
212 //=================================================================
213 void AIS_EqualRadiusRelation::ComputeRadiusPosition() 
214 {
215   if (myAutomaticPosition ||
216       myFirstCenter.Distance(myPosition) < Precision::Confusion() ||
217       mySecondCenter.Distance(myPosition) <  Precision::Confusion())
218       return;
219
220   gp_Pnt aPosition;
221
222   //project myPosition to the plane of constraint
223   GeomAPI_ProjectPointOnSurf aProj(myPosition, myPlane);
224   aPosition =  aProj.NearestPoint();
225                 
226   Standard_Real aDist1 = myFirstPoint.Distance(aPosition);
227   Standard_Real aDist2 = mySecondPoint.Distance(aPosition);
228   
229   if(aDist1<aDist2)
230     {
231       Standard_Real Rad1 = myFirstPoint.Distance(myFirstCenter);
232       const gp_Dir aNewDir1(aPosition.XYZ() - myFirstCenter.XYZ());
233       const gp_Vec aTVec (aNewDir1.XYZ()*Rad1); 
234       myFirstPoint = myFirstCenter.Translated(aTVec);
235     }
236   else {
237     Standard_Real Rad2 = mySecondPoint.Distance(mySecondCenter);
238     const gp_Dir aNewDir2(aPosition.XYZ() - mySecondCenter.XYZ());
239     gp_Vec aTVec (aNewDir2.XYZ()*Rad2); 
240     mySecondPoint = mySecondCenter.Translated(aTVec);
241   }
242   
243 }
244