0030853: Visualization, AIS_ViewController - fix 1 pixel Y shift while zooming
[occt.git] / src / AIS / AIS_MaxRadiusDimension.cxx
CommitLineData
b311480e 1// Created on: 1998-01-23
2// Created by: Sergey ZARITCHNY
3// Copyright (c) 1998-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <AIS.hxx>
7fd59977 19#include <AIS_EllipseRadiusDimension.hxx>
42cf5bc1 20#include <AIS_MaxRadiusDimension.hxx>
21#include <BRepAdaptor_Curve.hxx>
22#include <BRepAdaptor_Surface.hxx>
23#include <DsgPrs_EllipseRadiusPresentation.hxx>
7fd59977 24#include <ElCLib.hxx>
25#include <ElSLib.hxx>
42cf5bc1 26#include <Geom_CylindricalSurface.hxx>
7fd59977 27#include <Geom_Ellipse.hxx>
c04c30b3 28#include <Geom_OffsetCurve.hxx>
7fd59977 29#include <Geom_Plane.hxx>
30#include <Geom_Surface.hxx>
7fd59977 31#include <Geom_SurfaceOfLinearExtrusion.hxx>
42cf5bc1 32#include <Geom_SurfaceOfRevolution.hxx>
33#include <Geom_Transformation.hxx>
34#include <Geom_TrimmedCurve.hxx>
7fd59977 35#include <gp_Ax1.hxx>
36#include <gp_Dir.hxx>
42cf5bc1 37#include <gp_Lin.hxx>
38#include <gp_Pln.hxx>
39#include <gp_Pnt.hxx>
7fd59977 40#include <gp_Vec.hxx>
7fd59977 41#include <Precision.hxx>
42cf5bc1 42#include <Prs3d_ArrowAspect.hxx>
43#include <Prs3d_DimensionAspect.hxx>
44#include <Prs3d_Drawer.hxx>
45#include <Prs3d_Presentation.hxx>
46#include <Prs3d_Projector.hxx>
47#include <Prs3d_Text.hxx>
48#include <Prs3d_TextAspect.hxx>
49#include <Select3D_SensitiveBox.hxx>
50#include <Select3D_SensitiveCurve.hxx>
51#include <Select3D_SensitiveSegment.hxx>
52#include <SelectMgr_EntityOwner.hxx>
53#include <SelectMgr_Selection.hxx>
54#include <Standard_ConstructionError.hxx>
55#include <Standard_NotImplemented.hxx>
56#include <Standard_Type.hxx>
57#include <TCollection_ExtendedString.hxx>
58#include <TopoDS.hxx>
59#include <TopoDS_Shape.hxx>
7fd59977 60
92efcf78 61IMPLEMENT_STANDARD_RTTIEXT(AIS_MaxRadiusDimension,AIS_EllipseRadiusDimension)
62
7fd59977 63//=======================================================================
64//function : AIS_MaxRadiusDimension
65//purpose :
66//=======================================================================
7fd59977 67AIS_MaxRadiusDimension::AIS_MaxRadiusDimension(const TopoDS_Shape& aShape,
68 const Standard_Real aVal,
69 const TCollection_ExtendedString& aText)
70:AIS_EllipseRadiusDimension(aShape, aText)
71{
72 myVal = aVal;
73 mySymbolPrs = DsgPrs_AS_LASTAR;
74 myAutomaticPosition = Standard_True;
75 myArrowSize = myVal / 100.;
76}
77
78//=======================================================================
79//function : AIS_MaxRadiusDimension
80//purpose :
81//=======================================================================
82
83AIS_MaxRadiusDimension::AIS_MaxRadiusDimension(const TopoDS_Shape& aShape,
84 const Standard_Real aVal,
85 const TCollection_ExtendedString& aText,
86 const gp_Pnt& aPosition,
87 const DsgPrs_ArrowSide aSymbolPrs,
88 const Standard_Real anArrowSize)
89:AIS_EllipseRadiusDimension(aShape, aText)
90{
91 myVal = aVal;
92 mySymbolPrs = aSymbolPrs;
93 myPosition = aPosition;
94 myAutomaticPosition = Standard_False;
7fd59977 95 SetArrowSize( anArrowSize );
7fd59977 96}
97
98//=======================================================================
99//function : Compute
100//purpose :
101//=======================================================================
102
103void AIS_MaxRadiusDimension::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
104 const Handle(Prs3d_Presentation)& aPresentation,
105 const Standard_Integer /*aMode*/)
106{
7fd59977 107// if( myAutomaticPosition )
108 {//ota : recompute in any case
109 ComputeGeometry();
110 myEllipse.SetMajorRadius(myVal);
111 gp_Vec v1(myEllipse.XAxis().Direction());
112 v1 *=myVal;
113 myApexP = myEllipse.Location().Translated(v1);
114 myApexN = myEllipse.Location().Translated(-v1);
115 }
116 if(myIsAnArc) ComputeArcOfEllipse(aPresentation);
117 else
118 ComputeEllipse(aPresentation);
119}
120
121//=======================================================================
122//function : Compute
123//purpose : to avoid warning
124//=======================================================================
125
126void AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
127 const Handle(Prs3d_Presentation)& aPresentation)
128{
9775fa61 129// throw Standard_NotImplemented("AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Prs3d_Presentation)& aPresentation)");
7fd59977 130 PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
131}
132
7fd59977 133//=======================================================================
134//function : Compute
135//purpose :
136//=======================================================================
137
857ffd5e 138void AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)& aProjector,
139 const Handle(Geom_Transformation)& aTransformation,
140 const Handle(Prs3d_Presentation)& aPresentation)
7fd59977 141{
9775fa61 142// throw Standard_NotImplemented("AIS_MaxRadiusDimension::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
7fd59977 143 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
144}
145
146
147//=======================================================================
148//function : ComputeEllipse
149//purpose :
150//=======================================================================
151
152void AIS_MaxRadiusDimension::ComputeEllipse(const Handle(Prs3d_Presentation)& aPresentation)
153{
154
a6eb515f 155 Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
156 Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
7fd59977 157
158 // size
7fd59977 159 if( !myArrowSizeIsDefined ) {
160 myArrowSize = Min(myArrowSize,myVal / 5.);
161 }
162 arr->SetLength(myArrowSize);
7fd59977 163
164 Standard_Real U;//,V;
165 gp_Pnt curPos, Center;
166 Center = myEllipse.Location();
167 if( myAutomaticPosition )
168 {
169 myPosition = Center;
170 myEndOfArrow = myApexP;
171 myAutomaticPosition = Standard_True;
172
173 if ( myIsSetBndBox )
174 myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
175 myBndBox );
176 curPos = myPosition;
177 }
178 else //!AutomaticPosition
179 {
180 curPos = myPosition;
181 gp_Lin L1(myEllipse.XAxis());
182 U = ElCLib::Parameter ( L1, curPos );
183 curPos = ElCLib::Value (U, L1);
184 if (curPos.Distance(myApexP) < curPos.Distance(myApexN))
185 myEndOfArrow = myApexP ;
186 else
187 myEndOfArrow = myApexN ;
188 }
189
190 // Presenatation
191 DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, curPos,
192 myEndOfArrow, Center, Standard_True, mySymbolPrs);
193
194}
195
196//=======================================================================
197//function : ComputeArcOfEllipse
198//purpose :
199//=======================================================================
200
201void AIS_MaxRadiusDimension::ComputeArcOfEllipse(const Handle(Prs3d_Presentation)& aPresentation)
202{
203
a6eb515f 204 Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
205 Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
7fd59977 206
207 // size
7fd59977 208 if( !myArrowSizeIsDefined ) {
209 myArrowSize = Min(myArrowSize,myVal / 5.);
210 }
211 arr->SetLength(myArrowSize);
7fd59977 212
213 Standard_Real par;
214 gp_Pnt curPos, Center;
215 Center = myEllipse.Location();
216 Standard_Boolean IsInDomain = Standard_True;
217 if( myAutomaticPosition )
218 {
219 myEndOfArrow = AIS::NearestApex(myEllipse, myApexP, myApexN,
220 myFirstPar, myLastPar, IsInDomain);
221 myPosition = Center;
222 myAutomaticPosition = Standard_True;
223 if ( myIsSetBndBox )
224 myPosition = AIS::TranslatePointToBound(myPosition, gp_Dir(gp_Vec( Center, myPosition )),
225 myBndBox );
226 curPos = myPosition;
227 }
228 else //!AutomaticPosition
229 {
230 curPos = myPosition;
231// ElSLib::Parameters ( myPlane->Pln(), curPos, U, V );
232// curPos = ElSLib::Value (U, V, myPlane->Pln());
233 gp_Lin L1(myEllipse.XAxis());
234 par = ElCLib::Parameter ( L1, curPos );
235 curPos = ElCLib::Value (par, L1);
236 if (curPos.Distance(myApexP) < curPos.Distance(myApexN))
237 myEndOfArrow = myApexP ;
238 else
239 myEndOfArrow = myApexN ;
240 par = ElCLib::Parameter ( myEllipse, myEndOfArrow );
241 IsInDomain = AIS::InDomain( myFirstPar, myLastPar, par );
242 myPosition = curPos;
243 }
244
245// Standard_Real parEnd = ElCLib::Parameter ( myEllipse, myEndOfArrow );
246 Standard_Real parStart = 0.;
247 if( !IsInDomain )
248 {
249 if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
250 AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
251 parStart = myFirstPar;
252 else
253 parStart = myLastPar;
254 }
255 if(!myIsOffset)
256 DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myEllipse,
257 curPos, myEndOfArrow, Center, parStart, IsInDomain,
258 Standard_True, mySymbolPrs);
259 else
260 DsgPrs_EllipseRadiusPresentation::Add(aPresentation, myDrawer, myVal, myText, myOffsetCurve,
261 curPos, myEndOfArrow, Center, parStart, IsInDomain,
262 Standard_True, mySymbolPrs);
263}
264
265//=======================================================================
266//function : ComputeSelection
267//purpose :
268//=======================================================================
269
270void AIS_MaxRadiusDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
271 const Standard_Integer /*aMode*/)
272{
273
274 gp_Pnt center = myEllipse.Location();
275 gp_Pnt AttachmentPoint = myPosition;
276 Standard_Real dist = center.Distance(AttachmentPoint);
277 Standard_Real aRadius = myVal;
278 //Standard_Real inside = Standard_False;
279 gp_Pnt pt1;
280 if (dist > aRadius) pt1 = AttachmentPoint;
281 else
282 pt1 = myEndOfArrow;
283 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
284 Handle(Select3D_SensitiveSegment)
285 seg = new Select3D_SensitiveSegment(own, center , pt1);
286 aSelection->Add(seg);
287
288 // Text
289 Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
290 Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
291 AttachmentPoint.X(),
292 AttachmentPoint.Y(),
293 AttachmentPoint.Z(),
294 AttachmentPoint.X()+size,
295 AttachmentPoint.Y()+size,
296 AttachmentPoint.Z()+size);
297 aSelection->Add(box);
298
299 // Arc of Ellipse
300 if(myIsAnArc)
301 {
302
303 Standard_Real parEnd = ElCLib::Parameter ( myEllipse, myEndOfArrow );
304 if(!AIS::InDomain(myFirstPar, myLastPar, parEnd))
305 {
306 Standard_Real parStart, par;
307 if(AIS::DistanceFromApex (myEllipse, myEndOfArrow, myFirstPar) <
308 AIS::DistanceFromApex (myEllipse, myEndOfArrow, myLastPar))
309 par = myFirstPar;
310 else
311 par = myLastPar;
312 gp_Vec Vapex(center, ElCLib::Value( parEnd, myEllipse )) ;
313 gp_Vec Vpnt (center, ElCLib::Value( par, myEllipse )) ;
314 gp_Dir dir(Vpnt ^ Vapex);
315 if(myEllipse.Position().Direction().IsOpposite( dir, Precision::Angular())) {
316 parStart = parEnd;
317 parEnd = par;
318 }
319 else
320 parStart = par;
321
c04c30b3 322 Handle(Geom_Curve)TrimCurve;
7fd59977 323 if(myIsOffset)
324 {
325 Handle(Geom_Curve) aCurve = myOffsetCurve;
326 TrimCurve = new Geom_TrimmedCurve( aCurve, parStart, parEnd );
327 }
328 else
329 {
330 Handle(Geom_Ellipse) Ellipse = new Geom_Ellipse( myEllipse );
331 TrimCurve = new Geom_TrimmedCurve( Ellipse, parStart, parEnd );
332 }
333 Handle( Select3D_SensitiveCurve ) SensArc;
334 SensArc = new Select3D_SensitiveCurve( own, TrimCurve );
335 aSelection->Add( SensArc );
336 }
337 }
338
339}