0022627: Change OCCT memory management defaults
[occt.git] / src / AIS / AIS_EqualRadiusRelation.cxx
1 // File:        AIS_EqualRadiusRelation.cxx
2 // Created:     Tue Jan 20 19:19:54 1998
3 // Author:      Julia GERASIMOVA
4 //              <jgv@orthodox.nnov.matra-dtv.fr>
5
6 #define BUC60915        //GG 05/06/01 Enable to compute the requested arrow size
7 //                      if any in all dimensions.
8
9 #include <Standard_NotImplemented.hxx>
10
11 #include <AIS_EqualRadiusRelation.ixx>
12
13 #include <AIS.hxx>
14 #include <DsgPrs_EqualRadiusPresentation.hxx>
15 #include <BRepAdaptor_Curve.hxx>
16 #include <TopoDS.hxx>
17 #include <gp_Circ.hxx>
18 #include <Geom_Circle.hxx>
19 #include <ElCLib.hxx>
20 #include <SelectMgr_EntityOwner.hxx>
21 #include <Select3D_SensitiveSegment.hxx>
22 #include <Select3D_SensitiveBox.hxx>
23 #include <Precision.hxx>
24 #include <GeomAPI_ProjectPointOnSurf.hxx>
25 #include <Prs3d_LengthAspect.hxx>
26 #include <Prs3d_ArrowAspect.hxx>
27 #include <AIS_Drawer.hxx>
28
29 //=======================================================================
30 //function : AIS_EqualRadiusRelation
31 //purpose  : 
32 //=======================================================================
33
34 AIS_EqualRadiusRelation::AIS_EqualRadiusRelation( const TopoDS_Edge& aFirstEdge,
35                                                   const TopoDS_Edge& aSecondEdge,
36                                                   const Handle( Geom_Plane )& aPlane )
37      :AIS_Relation()
38 {
39   myFShape = aFirstEdge;
40   mySShape = aSecondEdge;
41   myPlane  = aPlane;
42 }
43
44 //=======================================================================
45 //function : Compute
46 //purpose  :
47 //=======================================================================
48
49 void AIS_EqualRadiusRelation::Compute( const Handle( PrsMgr_PresentationManager3d )&,
50                                        const Handle( Prs3d_Presentation )& aPresentation,
51                                        const Standard_Integer ) 
52 {
53   aPresentation->Clear();
54
55   BRepAdaptor_Curve FirstCurve( TopoDS::Edge( myFShape ) ), SecondCurve( TopoDS::Edge( mySShape ) );
56
57   Standard_Real FirstPar1 = FirstCurve.FirstParameter(), LastPar1 = FirstCurve.LastParameter(),
58                 FirstPar2 = SecondCurve.FirstParameter(), LastPar2 = SecondCurve.LastParameter();
59
60   Handle( Geom_Curve ) FirstProjCurve = FirstCurve.Curve().Curve(),
61                        SecondProjCurve = SecondCurve.Curve().Curve();
62   gp_Pnt FirstPoint1, LastPoint1, FirstPoint2, LastPoint2;
63   Standard_Boolean isFirstOnPlane, isSecondOnPlane;
64
65   AIS::ComputeGeomCurve( FirstProjCurve, FirstPar1, LastPar1, FirstPoint1, LastPoint1, myPlane, isFirstOnPlane );
66   AIS::ComputeGeomCurve( SecondProjCurve, FirstPar2, LastPar2, FirstPoint2, LastPoint2, myPlane, isSecondOnPlane );
67
68   if (!isFirstOnPlane)
69     ComputeProjEdgePresentation( aPresentation, TopoDS::Edge( myFShape ), FirstProjCurve, FirstPoint1, LastPoint1 );
70   if (! isSecondOnPlane)
71     ComputeProjEdgePresentation( aPresentation, TopoDS::Edge( mySShape ), SecondProjCurve, FirstPoint2, LastPoint2 );
72
73   gp_Circ FirstCirc = (Handle( Geom_Circle )::DownCast( FirstProjCurve ))->Circ();
74   gp_Circ SecondCirc = (Handle( Geom_Circle )::DownCast( SecondProjCurve ))->Circ();
75   
76   myFirstCenter = FirstCirc.Location();
77   mySecondCenter = SecondCirc.Location();
78   
79   //ota -- begin --
80   if (myAutomaticPosition)
81     {
82       myFirstPoint = ElCLib::Value( (FirstPar1 + LastPar1)*0.5, FirstCirc );
83       mySecondPoint = ElCLib::Value( (FirstPar2 + LastPar2)*0.5, SecondCirc );
84     }
85   else {
86     Standard_Real aPar =  ElCLib::Parameter(FirstCirc, myFirstPoint);
87     if (IntegerPart(0.5*LastPar1/M_PI) != 0 &&  aPar < FirstPar1 )
88       aPar +=2*M_PI*IntegerPart(0.5*LastPar1/M_PI);
89     Standard_Real aRadius = FirstCirc.Radius();
90
91     if (Abs(myFirstPoint.Distance(myFirstCenter) - aRadius) >= Precision::Confusion())
92       myFirstPoint = ElCLib::Value(aPar, FirstCirc);
93     if ( FirstPoint1.Distance(LastPoint1) > Precision::Confusion()){
94       //check where is myFirstPoint
95       if (aPar > LastPar1 || aPar < FirstPar1)
96         {
97           //myFirstPoint is out of Arc of FirstCircle
98           if (FirstPoint1.Distance(myFirstPoint)< LastPoint1.Distance(myFirstPoint))
99             myFirstPoint = FirstPoint1; 
100           else
101             myFirstPoint = LastPoint1; 
102         }
103     }
104   
105     
106     aPar =  ElCLib::Parameter(SecondCirc, mySecondPoint);
107     if (IntegerPart(0.5*LastPar2/M_PI) != 0 &&  aPar < FirstPar2 )
108       aPar +=2*M_PI*IntegerPart(0.5*LastPar2/M_PI);
109     
110     aRadius = SecondCirc.Radius();
111     if (Abs(mySecondPoint.Distance(mySecondCenter) - aRadius) >= Precision::Confusion())
112       mySecondPoint =  ElCLib::Value(aPar, SecondCirc);
113     if (FirstPoint2.Distance(LastPoint2) > Precision::Confusion()){
114       if (aPar > LastPar2 || aPar < FirstPar2)
115         { //mySecondPoint is out of Arc of mySecondCircle
116           if (FirstPoint2.Distance(mySecondPoint)< LastPoint2.Distance(mySecondPoint))
117             mySecondPoint = FirstPoint2; 
118           else
119             mySecondPoint = LastPoint2;
120         }
121     }
122   }
123 #ifdef BUC60915
124   if( !myArrowSizeIsDefined )
125 #endif
126     myArrowSize = (Min(myFirstCenter.Distance(myFirstPoint),
127                      mySecondCenter.Distance(mySecondPoint)))*0.05;
128   
129   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
130   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
131   arr->SetLength(myArrowSize);
132   
133   //ota -- end --
134   
135   DsgPrs_EqualRadiusPresentation::Add(aPresentation, myDrawer, 
136                                       myFirstCenter, mySecondCenter, myFirstPoint, mySecondPoint, myPlane );
137 }
138
139 //=======================================================================
140 //function : Compute
141 //purpose  : to avoid warning at compilation (SUN)
142 //=======================================================================
143
144 void AIS_EqualRadiusRelation::Compute( const Handle( Prs3d_Projector )& /*aProjector*/,
145                                        const Handle( Prs3d_Presentation )& /*aPresentation*/)
146 {
147 // Standard_NotImplemented::Raise("AIS_EqualRadiusRelation::Compute( const Handle( Prs3d_Projector )&,const Handle( Prs3d_Presentation )& )");
148 // PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
149 }
150
151 //=======================================================================
152 //function : Compute
153 //purpose  : to avoid warning at compilation (SUN)
154 //=======================================================================
155
156 void AIS_EqualRadiusRelation::Compute( const Handle( PrsMgr_PresentationManager2d )& /*aPresentationManager2d*/,
157                                        const Handle( Graphic2d_GraphicObject )& /*aGraphicObject*/,
158                                        const Standard_Integer /*anInteger*/)
159 {
160 // Standard_NotImplemented::Raise("AIS_EqualRadiusRelation::Compute( const Handle( PrsMgr_PresentationManager2d )&,const Handle( Graphic2d_GraphicObject )&,const Standard_Integer )");
161 // PrsMgr_PresentableObject::Compute( aPresentationManager2d ,aGraphicObject,anInteger) ;
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