0025154: Collections in BRepMesh package are named in non-conformant manner
[occt.git] / src / BRepMesh / BRepMesh_CircleTool.cxx
1 // Created on: 1993-06-15
2 // Created by: Didier PIFFAULT
3 // Copyright (c) 1993-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 #include <BRepMesh_CircleTool.hxx>
18 #include <BRepMesh_GeomTool.hxx>
19 #include <gp_Circ2d.hxx>
20 #include <Precision.hxx>
21 #include <BRepMesh_Circle.hxx>
22 #include <BRepMesh_CircleInspector.hxx>
23
24 //=======================================================================
25 //function : Inspect
26 //purpose  : 
27 //=======================================================================
28 NCollection_CellFilter_Action BRepMesh_CircleInspector::Inspect(
29   const Standard_Integer theTargetIndex)
30 {
31   const BRepMesh_Circle& aCircle = myCircles(theTargetIndex);
32   Standard_Real aRadius = aCircle.Radius();
33   if(aRadius < 0.)
34     return CellFilter_Purge;
35
36   const gp_XY& aLoc = aCircle.Location();
37
38   if ((myPoint - aLoc).SquareModulus() - (aRadius * aRadius) <= myTolerance)
39     myResIndices.Append(theTargetIndex);
40
41   return CellFilter_Keep;
42 }
43
44
45 //=======================================================================
46 //function : BRepMesh_CircleTool
47 //purpose  : 
48 //=======================================================================
49 BRepMesh_CircleTool::BRepMesh_CircleTool(
50   const Handle(NCollection_IncAllocator)& theAllocator)
51 : myTolerance (Precision::PConfusion() * Precision::PConfusion()),
52   myAllocator (theAllocator),
53   myCellFilter(10, theAllocator),
54   mySelector  (myTolerance, 64, theAllocator)
55 {
56 }
57
58 //=======================================================================
59 //function : BRepMesh_CircleTool
60 //purpose  : 
61 //=======================================================================
62 BRepMesh_CircleTool::BRepMesh_CircleTool(
63   const Standard_Integer                  theReservedSize,
64   const Handle(NCollection_IncAllocator)& theAllocator)
65 : myTolerance (Precision::PConfusion() * Precision::PConfusion()),
66   myAllocator (theAllocator),
67   myCellFilter(10, theAllocator),
68   mySelector  (myTolerance, Max(theReservedSize, 64), theAllocator)
69 {
70 }
71
72 //=======================================================================
73 //function : bind
74 //purpose  : 
75 //=======================================================================
76 void BRepMesh_CircleTool::bind(const Standard_Integer theIndex,
77                                const gp_XY&           theLocation,
78                                const Standard_Real    theRadius)
79 {
80   BRepMesh_Circle aCirle(theLocation, theRadius);
81
82   //compute coords
83   Standard_Real aMaxX = Min(theLocation.X() + theRadius, myFaceMax.X());
84   Standard_Real aMinX = Max(theLocation.X() - theRadius, myFaceMin.X());
85   Standard_Real aMaxY = Min(theLocation.Y() + theRadius, myFaceMax.Y());
86   Standard_Real aMinY = Max(theLocation.Y() - theRadius, myFaceMin.Y());
87
88   gp_XY aMinPnt(aMinX, aMinY);
89   gp_XY aMaxPnt(aMaxX, aMaxY);
90
91   myCellFilter.Add(theIndex, aMinPnt, aMaxPnt);
92   mySelector.Bind(theIndex, aCirle);
93 }
94
95 //=======================================================================
96 //function : Bind
97 //purpose  : 
98 //=======================================================================
99 void BRepMesh_CircleTool::Bind(const Standard_Integer theIndex,
100                                const gp_Circ2d&       theCircle)
101 {
102   gp_XY         aCoord  = theCircle.Location().Coord();
103   Standard_Real aRadius = theCircle.Radius();
104   bind(theIndex, aCoord, aRadius);
105 }
106
107 //=======================================================================
108 //function : Bind
109 //purpose  : 
110 //=======================================================================
111 Standard_Boolean BRepMesh_CircleTool::Bind(const Standard_Integer theIndex,
112                                            const gp_XY&           thePoint1,
113                                            const gp_XY&           thePoint2,
114                                            const gp_XY&           thePoint3)
115 {
116   const Standard_Real aPrecision   = Precision::PConfusion();
117   const Standard_Real aSqPrecision = aPrecision * aPrecision;
118
119   const gp_XY aPoints[3] = { thePoint1, thePoint2, thePoint3 };
120
121   gp_XY aNorm[3];
122   gp_XY aMidPnt[3];
123   for (Standard_Integer i = 0; i < 3; ++i)
124   {
125     const gp_XY& aPnt1 = aPoints[i];
126     const gp_XY& aPnt2 = aPoints[(i + 1) % 3];
127
128     aMidPnt[i] = (aPnt1 + aPnt2) / 2.;
129
130     gp_XY aLink(aPnt2 - aPnt1);
131     if (aLink.SquareModulus() < aSqPrecision)
132       return Standard_False;
133
134     aNorm[i] = gp_XY(aLink.Y(), -aLink.X());
135     aNorm[i].Add(aMidPnt[i]);
136   }
137
138   gp_XY aIntPnt;
139   Standard_Real aParam[2];
140   BRepMesh_GeomTool::IntFlag aIntFlag = 
141     BRepMesh_GeomTool::IntLinLin(aMidPnt[0], aNorm[0],
142       aMidPnt[1], aNorm[1], aIntPnt, aParam);
143
144   if (aIntFlag != BRepMesh_GeomTool::Cross)
145     return Standard_False;
146
147   Standard_Real aRadius = (aPoints[0] - aIntPnt).Modulus();
148   bind(theIndex, aIntPnt, aRadius);
149   return Standard_True;
150 }
151
152 //=======================================================================
153 //function : Delete
154 //purpose  : 
155 //=======================================================================
156 void BRepMesh_CircleTool::Delete(const Standard_Integer theIndex)
157 {
158   BRepMesh_Circle& aCircle = mySelector.Circle(theIndex);
159   if(aCircle.Radius() > 0.)
160     aCircle.SetRadius(-1);
161 }
162
163 //=======================================================================
164 //function : Select
165 //purpose  : 
166 //=======================================================================
167 BRepMesh::ListOfInteger& BRepMesh_CircleTool::Select(const gp_XY& thePoint)
168 {
169   mySelector.SetPoint(thePoint);
170   myCellFilter.Inspect(thePoint, mySelector);
171   return mySelector.GetShotCircles();
172 }
173
174 //=======================================================================
175 //function : MocBind
176 //purpose  : 
177 //=======================================================================
178 void BRepMesh_CircleTool::MocBind(const Standard_Integer theIndex)
179 {
180   BRepMesh_Circle aNullCir(gp::Origin2d().Coord(), -1.);
181   mySelector.Bind(theIndex, aNullCir);
182 }