1 // Created on: 1996-02-06
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Geom_Circle.hxx>
19 #include <Select3D_Pnt.hxx>
20 #include <Select3D_SensitiveTriangle.hxx>
21 #include <Precision.hxx>
23 #include <Select3D_SensitiveCircle.hxx>
26 static Standard_Integer GetCircleNbPoints (const Handle(Geom_Circle)& theCircle,
27 const Standard_Integer theNbPnts)
29 // Check if number of points is invalid.
30 // In this case myPolyg raises Standard_ConstructionError
31 // exception (look constructor bellow).
35 if (theCircle->Radius() > Precision::Confusion())
36 return 2 * theNbPnts + 1;
38 // The radius is too small and circle degenerates into point
42 static Standard_Integer GetArcNbPoints (const Handle(Geom_Circle)& theCircle,
43 const Standard_Integer theNbPnts)
45 // There is no need to check number of points here.
46 // In case of invalid number of points this method returns
47 // -1 or smaller value.
48 if (theCircle->Radius() > Precision::Confusion())
49 return 2 * theNbPnts - 1;
51 // The radius is too small and circle degenerates into point
55 //=======================================================================
56 //function : Select3D_SensitiveCircle (constructor)
57 //purpose : Definition of a sensitive circle
58 //=======================================================================
59 Select3D_SensitiveCircle::Select3D_SensitiveCircle(const Handle(SelectBasics_EntityOwner)& theOwnerId,
60 const Handle(Geom_Circle)& theCircle,
61 const Standard_Boolean theIsFilled,
62 const Standard_Integer theNbPnts)
63 : Select3D_SensitivePoly (theOwnerId, !theIsFilled, GetCircleNbPoints (theCircle, theNbPnts)),
68 mySensType = theIsFilled ? Select3D_TOS_INTERIOR : Select3D_TOS_BOUNDARY;
69 if (myPolyg.Size() != 1)
73 Standard_Real anUStart = theCircle->FirstParameter();
74 Standard_Real anUEnd = theCircle->LastParameter();
75 Standard_Real aStep = (anUEnd - anUStart) / theNbPnts;
76 Standard_Real aRadius = theCircle->Radius();
77 Standard_Integer aPntIdx = 1;
78 Standard_Real aCurU = anUStart;
79 for (Standard_Integer anIndex = 1; anIndex <= theNbPnts; anIndex++)
81 theCircle->D1 (aCurU, aP1, aV1);
84 myPolyg.SetPnt (aPntIdx - 1, aP1);
86 aP2 = gp_Pnt (aP1.X() + aV1.X() * tan (aStep / 2.0) * aRadius,
87 aP1.Y() + aV1.Y() * tan (aStep / 2.0) * aRadius,
88 aP1.Z() + aV1.Z() * tan (aStep / 2.0) * aRadius);
89 myPolyg.SetPnt (aPntIdx - 1, aP2);
94 // Copy the first point to the last point of myPolyg
95 myPolyg.SetPnt (theNbPnts * 2, myPolyg.Pnt (0));
97 myCenter3D = theCircle->Location();
102 myPolyg.SetPnt (0, theCircle->Location());
104 myCenter3D = myPolyg.Pnt (0);
107 if (mySensType == Select3D_TOS_BOUNDARY)
109 SetSensitivityFactor (6.0);
113 //=======================================================================
114 //function : Select3D_SensitiveCircle (constructor)
115 //purpose : Definition of a sensitive arc
116 //=======================================================================
117 Select3D_SensitiveCircle::Select3D_SensitiveCircle (const Handle(SelectBasics_EntityOwner)& theOwnerId,
118 const Handle(Geom_Circle)& theCircle,
119 const Standard_Real theU1,
120 const Standard_Real theU2,
121 const Standard_Boolean theIsFilled,
122 const Standard_Integer theNbPnts)
123 : Select3D_SensitivePoly (theOwnerId, !theIsFilled, GetArcNbPoints (theCircle, theNbPnts)),
124 myCircle (theCircle),
125 myStart (Min (theU1, theU2)),
126 myEnd (Max (theU1, theU2))
128 mySensType = theIsFilled ? Select3D_TOS_INTERIOR : Select3D_TOS_BOUNDARY;
130 if (myPolyg.Size() != 1)
135 Standard_Real aStep = (myEnd - myStart) / (theNbPnts - 1);
136 Standard_Real aRadius = theCircle->Radius();
137 Standard_Integer aPntIdx = 1;
138 Standard_Real aCurU = myStart;
140 for (Standard_Integer anIndex = 1; anIndex <= theNbPnts - 1; anIndex++)
142 theCircle->D1 (aCurU, aP1, aV1);
144 myPolyg.SetPnt (aPntIdx - 1, aP1);
146 aP2 = gp_Pnt (aP1.X() + aV1.X() * tan (aStep /2.0) * aRadius,
147 aP1.Y() + aV1.Y() * tan (aStep /2.0) * aRadius,
148 aP1.Z() + aV1.Z() * tan (aStep /2.0) * aRadius);
149 myPolyg.SetPnt (aPntIdx - 1, aP2);
153 theCircle->D0 (myEnd, aP1);
154 myPolyg.SetPnt (theNbPnts * 2 - 2, aP1);
156 myCenter3D = theCircle->Location();
160 myPolyg.SetPnt (0, theCircle->Location());
162 myCenter3D = myPolyg.Pnt (0);
165 if (mySensType == Select3D_TOS_BOUNDARY)
167 SetSensitivityFactor (6.0);
171 //=======================================================================
172 //function : Select3D_SensitiveCircle
174 //=======================================================================
175 Select3D_SensitiveCircle::Select3D_SensitiveCircle(const Handle(SelectBasics_EntityOwner)& theOwnerId,
176 const Handle(TColgp_HArray1OfPnt)& thePnts3d,
177 const Standard_Boolean theIsFilled)
178 : Select3D_SensitivePoly (theOwnerId, thePnts3d, (Standard_Boolean )!theIsFilled),
182 mySensType = theIsFilled ? Select3D_TOS_INTERIOR : Select3D_TOS_BOUNDARY;
184 if (myPolyg.Size() != 1)
187 myCenter3D = myPolyg.Pnt (0);
189 if (mySensType == Select3D_TOS_BOUNDARY)
191 SetSensitivityFactor (6.0);
195 //=======================================================================
196 //function : Select3D_SensitiveCircle
198 //=======================================================================
200 Select3D_SensitiveCircle::Select3D_SensitiveCircle(const Handle(SelectBasics_EntityOwner)& theOwnerId,
201 const TColgp_Array1OfPnt& thePnts3d,
202 const Standard_Boolean theIsFilled)
203 : Select3D_SensitivePoly (theOwnerId, thePnts3d, !theIsFilled),
207 mySensType = theIsFilled ? Select3D_TOS_INTERIOR : Select3D_TOS_BOUNDARY;
209 if (myPolyg.Size() != 1)
212 myCenter3D = myPolyg.Pnt (0);
214 if (mySensType == Select3D_TOS_BOUNDARY)
216 SetSensitivityFactor (6.0);
220 //=======================================================================
222 // purpose : Builds BVH tree for a circle's edge segments if needed
223 //=======================================================================
224 void Select3D_SensitiveCircle::BVH()
226 if (mySensType == Select3D_TOS_BOUNDARY)
228 Select3D_SensitivePoly::BVH();
232 //=======================================================================
233 // function : Matches
234 // purpose : Checks whether the circle overlaps current selecting volume
235 //=======================================================================
236 Standard_Boolean Select3D_SensitiveCircle::Matches (SelectBasics_SelectingVolumeManager& theMgr,
237 SelectBasics_PickResult& thePickResult)
239 Standard_Real aDepth = RealLast();
240 Standard_Real aDistToCOG = RealLast();
242 if (mySensType == Select3D_TOS_BOUNDARY)
244 if (!Select3D_SensitivePoly::Matches (theMgr, thePickResult))
246 thePickResult = SelectBasics_PickResult (aDepth, aDistToCOG);
247 return Standard_False;
250 else if (mySensType == Select3D_TOS_INTERIOR)
252 Handle(TColgp_HArray1OfPnt) anArrayOfPnt;
253 Points3D (anArrayOfPnt);
254 if (!theMgr.IsOverlapAllowed())
256 thePickResult = SelectBasics_PickResult (aDepth, aDistToCOG);
257 for (Standard_Integer aPntIdx = anArrayOfPnt->Lower(); aPntIdx <= anArrayOfPnt->Upper(); ++aPntIdx)
259 Standard_Real aDummy;
260 if (!theMgr.Overlaps (anArrayOfPnt->Value (aPntIdx), aDummy))
261 return Standard_False;
263 return Standard_True;
266 if (!theMgr.Overlaps (anArrayOfPnt, Select3D_TOS_INTERIOR, aDepth))
268 thePickResult = SelectBasics_PickResult (aDepth, aDistToCOG);
269 return Standard_False;
274 aDistToCOG = theMgr.DistToGeometryCenter (myCenter3D);
275 thePickResult = SelectBasics_PickResult (aDepth, aDistToCOG);
276 return Standard_True;
279 void Select3D_SensitiveCircle::ArrayBounds (Standard_Integer & theLow,
280 Standard_Integer & theUp) const
283 theUp = myPolyg.Size() - 1;
286 //=======================================================================
287 //function : GetPoint3d
289 //=======================================================================
290 gp_Pnt Select3D_SensitiveCircle::GetPoint3d (const Standard_Integer thePntIdx) const
292 if (thePntIdx >= 0 && thePntIdx < myPolyg.Size())
293 return myPolyg.Pnt (thePntIdx);
298 //=======================================================================
299 //function : GetConnected
301 //=======================================================================
303 Handle(Select3D_SensitiveEntity) Select3D_SensitiveCircle::GetConnected()
305 Standard_Boolean isFilled = mySensType == Select3D_TOS_INTERIOR;
306 // Create a copy of this
307 Handle(Select3D_SensitiveEntity) aNewEntity;
308 // this was constructed using Handle(Geom_Circle)
309 if(!myCircle.IsNull())
311 if ((myEnd - myStart) > Precision::Confusion())
314 aNewEntity = new Select3D_SensitiveCircle (myOwnerId, myCircle, myStart, myEnd, isFilled);
319 aNewEntity = new Select3D_SensitiveCircle (myOwnerId, myCircle, isFilled);
322 // this was constructed using TColgp_Array1OfPnt
325 Standard_Integer aSize = myPolyg.Size();
326 TColgp_Array1OfPnt aPolyg (1, aSize);
327 for(Standard_Integer anIndex = 1; anIndex <= aSize; ++anIndex)
329 aPolyg.SetValue(anIndex, myPolyg.Pnt (anIndex-1));
331 aNewEntity = new Select3D_SensitiveCircle (myOwnerId, aPolyg, isFilled);
337 //=======================================================================
338 //function : computeCenter3D
340 //=======================================================================
341 void Select3D_SensitiveCircle::computeCenter3D()
344 Standard_Integer aNbPnts = myPolyg.Size();
347 // The mass of points system
348 Standard_Integer aMass = aNbPnts - 1;
349 // Find the circle barycenter
350 for (Standard_Integer anIndex = 0; anIndex < aNbPnts - 1; ++anIndex)
352 aCenter += myPolyg.Pnt(anIndex);
354 myCenter3D = aCenter / aMass;
358 myCenter3D = myPolyg.Pnt(0);
362 //=======================================================================
363 // function : CenterOfGeometry
364 // purpose : Returns center of the circle. If location transformation
365 // is set, it will be applied
366 //=======================================================================
367 gp_Pnt Select3D_SensitiveCircle::CenterOfGeometry() const