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