0024153: Adding OpenCL to OCCT prerequisites
[occt.git] / src / AIS / AIS_MinRadiusDimension.cxx
1 // Created on: 1998-01-27
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #define BUC60915        //GG 05/06/01 Enable to compute the requested arrow size
23 //                      if any in all dimensions.
24
25 #include <Standard_NotImplemented.hxx>
26
27 #include <AIS_MinRadiusDimension.ixx>
28
29 #include <AIS_EllipseRadiusDimension.hxx>
30 #include <TCollection_ExtendedString.hxx>
31
32 #include <Prs3d_LengthAspect.hxx>
33 #include <Prs3d_ArrowAspect.hxx>
34 #include <Prs3d_Drawer.hxx>
35 #include <Prs3d_TextAspect.hxx>
36 #include <Prs3d_Text.hxx>
37
38 #include <Select3D_SensitiveSegment.hxx>
39 #include <Select3D_SensitiveCurve.hxx>
40 #include <Select3D_SensitiveBox.hxx>
41 #include <SelectMgr_EntityOwner.hxx>
42
43 #include <ElCLib.hxx>
44 #include <ElSLib.hxx>
45
46 #include <TopoDS.hxx>
47
48 #include <BRepAdaptor_Surface.hxx>
49 #include <BRepAdaptor_Curve.hxx>
50
51 #include <Geom_Ellipse.hxx>
52 #include <Geom_TrimmedCurve.hxx>
53 #include <Geom_Plane.hxx>
54 #include <Geom_Surface.hxx>
55 #include <Geom_CylindricalSurface.hxx>
56 #include <Geom_SurfaceOfRevolution.hxx>
57 #include <Geom_CylindricalSurface.hxx>
58 #include <Geom_SurfaceOfLinearExtrusion.hxx>
59
60 #include <gp_Pln.hxx>
61 #include <gp_Pnt.hxx>
62 #include <gp_Lin.hxx>
63 #include <gp_Ax1.hxx>
64 #include <gp_Dir.hxx>
65 #include <gp_Vec.hxx>
66
67 #include <AIS.hxx>
68 #include <AIS_Drawer.hxx>
69
70 #include <Precision.hxx>
71 #include <DsgPrs_EllipseRadiusPresentation.hxx>
72
73
74 //=======================================================================
75 //function : AIS_MinRadiusDimension
76 //purpose  : 
77 //=======================================================================
78
79 AIS_MinRadiusDimension::AIS_MinRadiusDimension(const TopoDS_Shape& aShape, 
80                                                const Standard_Real aVal, 
81                                                const TCollection_ExtendedString& aText)
82 :AIS_EllipseRadiusDimension(aShape, aText)
83 {
84   myVal = aVal;
85   mySymbolPrs = DsgPrs_AS_LASTAR;
86   myAutomaticPosition = Standard_True;
87   myArrowSize = myVal / 100.;
88 }
89
90 //=======================================================================
91 //function : AIS_MinRadiusDimension
92 //purpose  : 
93 //=======================================================================
94
95 AIS_MinRadiusDimension::AIS_MinRadiusDimension(const TopoDS_Shape& aShape, 
96                                                const Standard_Real aVal, 
97                                                const TCollection_ExtendedString& aText,
98                                                const gp_Pnt& aPosition, 
99                                                const DsgPrs_ArrowSide aSymbolPrs,
100                                                const Standard_Real anArrowSize)
101 :AIS_EllipseRadiusDimension(aShape, aText)
102 {
103   myVal = aVal;
104   mySymbolPrs = aSymbolPrs;
105   myPosition = aPosition;
106   myAutomaticPosition = Standard_False;
107 #ifdef BUC60915
108   SetArrowSize( anArrowSize );
109 #else
110   myArrowSize = anArrowSize;
111 #endif
112 }
113
114 //=======================================================================
115 //function : Compute
116 //purpose  : 
117 //=======================================================================
118
119 void AIS_MinRadiusDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
120                                      const Handle(Prs3d_Presentation)& aPresentation, 
121                                      const Standard_Integer /*aMode*/)
122 {
123   aPresentation->Clear();
124
125 //  if( myAutomaticPosition )
126     //{ //ota : recompute ellipse always
127   ComputeGeometry();
128   myEllipse.SetMinorRadius(myVal);
129   gp_Vec v1(myEllipse.YAxis().Direction());
130   v1 *=myVal;
131   myApexP = myEllipse.Location().Translated(v1); 
132   myApexN = myEllipse.Location().Translated(-v1); 
133 //   }
134   if(myIsAnArc) ComputeArcOfEllipse(aPresentation);
135   else 
136     ComputeEllipse(aPresentation);
137 }
138
139 //=======================================================================
140 //function : Compute
141 //purpose  : to avoid warning
142 //=======================================================================
143
144 void  AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
145                                       const Handle(Prs3d_Presentation)& aPresentation)
146 {
147 // Standard_NotImplemented::Raise("AIS_MinRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)");
148  PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
149 }
150
151 //=======================================================================
152 //function : Compute
153 //purpose  : 
154 //=======================================================================
155
156 void AIS_MinRadiusDimension::Compute(const Handle_Prs3d_Projector& aProjector,
157                                      const Handle_Geom_Transformation& aTransformation,
158                                      const Handle_Prs3d_Presentation& aPresentation)
159 {
160 // Standard_NotImplemented::Raise("AIS_MinRadiusDimension::Compute(const Handle_Prs3d_Projector&, const Handle_Geom_Transformation&, const Handle_Prs3d_Presentation&)");
161  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
162 }
163
164 //=======================================================================
165 //function : ComputeEllipse
166 //purpose  : 
167 //=======================================================================
168
169 void AIS_MinRadiusDimension::ComputeEllipse(const Handle(Prs3d_Presentation)& aPresentation)
170 {
171
172   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
173   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
174   
175   // size
176 #ifdef BUC60915
177   if( !myArrowSizeIsDefined ) {
178     myArrowSize = Min(myArrowSize,myVal/5.);
179   }
180   arr->SetLength(myArrowSize);
181 #else
182   if (myVal/5. > myArrowSize) {
183     arr->SetLength(myArrowSize);
184   }
185   else {
186     arr->SetLength(myVal/5.);
187   }
188 #endif
189
190   Standard_Real U;//,V;
191   gp_Pnt curPos, Center;
192   Center = myEllipse.Location();
193   if( myAutomaticPosition )
194     {
195       myPosition = Center;
196       myEndOfArrow = myApexP;
197       myAutomaticPosition = Standard_True;   
198       
199       if ( myIsSetBndBox )
200         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
201                                                 myBndBox );        
202       curPos = myPosition;  
203     }
204   else //!AutomaticPosition
205     {
206       curPos = myPosition;
207       gp_Lin L1(myEllipse.YAxis());
208       U = ElCLib::Parameter ( L1, curPos );
209       curPos = ElCLib::Value (U, L1);
210       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
211         myEndOfArrow = myApexP ;
212       else
213         myEndOfArrow = myApexN ;
214       myPosition = curPos;
215     }
216  
217   // Presenatation  
218   DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, curPos,
219                                         myEndOfArrow, Center, Standard_False, mySymbolPrs);
220
221 }
222
223 //=======================================================================
224 //function : ComputeArcOfEllipse
225 //purpose  : 
226 //=======================================================================
227
228 void AIS_MinRadiusDimension::ComputeArcOfEllipse(const Handle(Prs3d_Presentation)& aPresentation)
229 {
230
231   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
232   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
233   
234   // size
235 #ifdef BUC60915
236   if( !myArrowSizeIsDefined ) {
237     myArrowSize = Min(myArrowSize,myVal/5.);
238   }
239   arr->SetLength(myArrowSize);
240 #else
241   if (myVal/5. > myArrowSize) {
242     arr->SetLength(myArrowSize);
243   }
244   else {
245     arr->SetLength(myVal/5.);
246   }
247 #endif
248   
249   Standard_Real par;
250   gp_Pnt curPos, Center;
251   Center = myEllipse.Location();
252   Standard_Boolean IsInDomain = Standard_True;
253   if( myAutomaticPosition )
254     {
255       myEndOfArrow = AIS::NearestApex(myEllipse, myApexP, myApexN,
256                                       myFirstPar, myLastPar, IsInDomain);
257       myPosition = Center;
258       myAutomaticPosition = Standard_True;
259       if ( myIsSetBndBox )
260         myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
261                                                 myBndBox );
262       curPos = myPosition;  
263
264     }
265   else //!AutomaticPosition
266     {
267       curPos = myPosition;
268       gp_Lin L1(myEllipse.YAxis());
269       par = ElCLib::Parameter ( L1, curPos );
270       curPos = ElCLib::Value (par, L1);
271       if (curPos.Distance(myApexP) < curPos.Distance(myApexN)) 
272         myEndOfArrow = myApexP ;
273       else
274         myEndOfArrow = myApexN ;
275       par = ElCLib::Parameter ( myEllipse, myEndOfArrow );
276       IsInDomain = AIS::InDomain(myFirstPar, myLastPar, par);
277       myPosition = curPos;
278     }
279
280   Standard_Real parStart =0.;
281   if( !IsInDomain )
282     {
283       if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
284          AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
285         parStart = myFirstPar;
286       else
287         parStart = myLastPar;
288
289     }
290
291   if(!myIsOffset)
292     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myEllipse,
293                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
294                                           Standard_True, mySymbolPrs);
295   else 
296     DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myOffsetCurve,
297                                           curPos, myEndOfArrow, Center, parStart, IsInDomain,
298                                           Standard_True, mySymbolPrs);
299 }
300
301
302 //=======================================================================
303 //function : ComputeSelection
304 //purpose  : 
305 //=======================================================================
306
307 void AIS_MinRadiusDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
308                                               const Standard_Integer /*aMode*/)
309 {
310
311     gp_Pnt        center   = myEllipse.Location();
312     gp_Pnt AttachmentPoint = myPosition;
313     Standard_Real dist    = center.Distance(AttachmentPoint);
314     Standard_Real aRadius = myVal;
315     //Standard_Real inside  = Standard_False;
316     gp_Pnt pt1;
317     if (dist > aRadius) pt1 = AttachmentPoint; 
318     else 
319       pt1 = myEndOfArrow;
320     Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
321     Handle(Select3D_SensitiveSegment) 
322       seg = new Select3D_SensitiveSegment(own, center , pt1);
323     aSelection->Add(seg);
324
325     // Text
326     Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
327     Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
328                                                                     AttachmentPoint.X(),
329                                                                     AttachmentPoint.Y(),
330                                                                     AttachmentPoint.Z(),
331                                                                     AttachmentPoint.X()+size,
332                                                                     AttachmentPoint.Y()+size,
333                                                                     AttachmentPoint.Z()+size);
334     aSelection->Add(box);
335
336   // Arc of Ellipse
337     if(myIsAnArc)
338       {
339         
340         Standard_Real parEnd = ElCLib::Parameter ( myEllipse, myEndOfArrow );
341         if(!AIS::InDomain(myFirstPar, myLastPar, parEnd))
342           {
343             Standard_Real parStart, par;
344             if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
345                AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
346               par = myFirstPar;
347             else
348               par = myLastPar;
349             gp_Vec Vapex(center, ElCLib::Value( parEnd, myEllipse )) ;
350             gp_Vec Vpnt (center, ElCLib::Value( par, myEllipse )) ;
351             gp_Dir dir(Vpnt ^ Vapex);
352             if(myEllipse.Position().Direction().IsOpposite( dir, Precision::Angular())) {
353               parStart = parEnd;
354               parEnd   = par;
355             }
356             else 
357               parStart = par;
358             Handle(Geom_TrimmedCurve)TrimCurve;
359             if(myIsOffset)
360               {
361                 Handle(Geom_Curve) aCurve = myOffsetCurve;
362                 TrimCurve = new Geom_TrimmedCurve( aCurve,  parStart, parEnd );
363               }
364             else
365               {
366                 Handle(Geom_Ellipse) Ellipse = new Geom_Ellipse( myEllipse );
367                 TrimCurve = new Geom_TrimmedCurve( Ellipse,  parStart, parEnd );
368               }
369             Handle( Select3D_SensitiveCurve ) SensArc;
370             SensArc = new Select3D_SensitiveCurve( own, TrimCurve );
371             aSelection->Add( SensArc );
372           }
373     }
374
375 }