OCC22138 Remove *.gxx files from Mesh algorithm
[occt.git] / src / BRepMesh / BRepMesh_CircleTool.cxx
1 // File:        BRepMesh_CircleTool.cxx
2 // Created:     Tue Jun 15 19:10:25 1993
3 // Author:      Didier PIFFAULT
4 //              <dpf@zerox>
5
6 #include <BRepMesh_CircleTool.ixx>
7 #include <gp_XY.hxx>
8 #include <Precision.hxx>
9 #include <BRepMesh_Circ.hxx>
10 #include <BRepMesh_CircleInspector.hxx>
11 #include <BRepMesh_BaseAllocator.hxx>
12
13
14 //=======================================================================
15 //function : BRepMesh_CircleInspector
16 //purpose  : Constructor
17 //
18 //=======================================================================
19
20 BRepMesh_CircleInspector::BRepMesh_CircleInspector (Standard_Real theTol,
21                                                     Standard_Integer nbComp,
22                                                     const BRepMesh_BaseAllocator& theAlloc)
23                                                     : myTol(theTol*theTol),
24                                                     myResInd(theAlloc),
25                                                     myInitCircle(nbComp)
26 {
27   //  myTol = theTol*theTol;
28 }
29
30 //=======================================================================
31 //function : Inspect
32 //purpose  : 
33 //
34 //=======================================================================
35 NCollection_CellFilter_Action BRepMesh_CircleInspector::Inspect (const Standard_Integer theTarget)
36 {
37   const BRepMesh_Circ& Circ = myInitCircle(theTarget);
38   Standard_Real R = Circ.Radius();
39   if(R < 0)
40     return CellFilter_Purge;
41   Standard_Real dx,dy;
42   const gp_XY& aLoc=Circ.Location();
43   dx=myCurrent.X()-aLoc.X();
44   dy=myCurrent.Y()-aLoc.Y();
45   if ((dx*dx+dy*dy)-(R*R) <= myTol)
46     myResInd.Append(theTarget);
47   return CellFilter_Keep;
48 }
49
50
51 //=======================================================================
52 //function : BRepMesh_CircleTool
53 //purpose  : 
54 //=======================================================================
55 BRepMesh_CircleTool::BRepMesh_CircleTool(const BRepMesh_BaseAllocator& theAlloc)
56 : Tolerance(Precision::PConfusion()),
57 Allocator(theAlloc),
58 CellFilter(10, theAlloc),
59 Selector(Tolerance,64,theAlloc)
60 {
61   Tolerance=Tolerance*Tolerance;
62 }
63
64 //=======================================================================
65 //function : BRepMesh_CircleTool
66 //purpose  : 
67 //=======================================================================
68 BRepMesh_CircleTool::BRepMesh_CircleTool(const Standard_Integer nbComp,
69                                          const BRepMesh_BaseAllocator& theAlloc)
70                                          : Tolerance(Precision::PConfusion()),
71                                          Allocator(theAlloc),
72                                          CellFilter(10, theAlloc),
73                                          Selector(Tolerance,Max(nbComp,64),theAlloc)
74 {
75   Tolerance=Tolerance*Tolerance;
76 }
77
78
79 //=======================================================================
80 //function : Initialize
81 //purpose  : 
82 //=======================================================================
83 void BRepMesh_CircleTool::Initialize(const Standard_Integer /*nbComp*/)
84 {
85   Tolerance=Precision::PConfusion();
86   Tolerance=Tolerance*Tolerance;
87 }
88
89 void BRepMesh_CircleTool::SetCellSize(const Standard_Real theSize)
90 {
91   CellFilter.Reset(theSize, Allocator);
92 }
93
94 void BRepMesh_CircleTool::SetCellSize(const Standard_Real theXSize, 
95                                       const Standard_Real theYSize)
96 {
97   Standard_Real aCellSize[2];
98   aCellSize[0] = theXSize;
99   aCellSize[1] = theYSize;
100
101   CellFilter.Reset(aCellSize, Allocator);
102 }
103
104
105 void BRepMesh_CircleTool::SetMinMaxSize(const gp_XY& theMin,
106                                         const gp_XY& theMax)
107 {
108   FaceMin = theMin;
109   FaceMax = theMax;
110 }
111
112 //=======================================================================
113 //function : Add
114 //purpose  : 
115 //=======================================================================
116 void  BRepMesh_CircleTool::Add(const gp_Circ2d& theCirc,
117                                const Standard_Integer theIndex)
118 {
119   gp_XY aCoord(theCirc.Location().Coord());
120   Standard_Real R = theCirc.Radius();
121   BRepMesh_Circ aCir(aCoord, R);
122
123   //compute coords
124   Standard_Real xMax=Min(aCoord.X()+R,FaceMax.X());
125   Standard_Real xMin=Max(aCoord.X()-R,FaceMin.X());
126   Standard_Real yMax=Min(aCoord.Y()+R,FaceMax.Y());
127   Standard_Real yMin=Max(aCoord.Y()-R,FaceMin.Y());
128
129   gp_XY MinPnt(xMin,yMin);
130   gp_XY MaxPnt(xMax,yMax);
131
132   CellFilter.Add(theIndex, MinPnt, MaxPnt);
133   Selector.Add(theIndex, aCir);
134 }
135
136 //=======================================================================
137 //function : Add
138 //purpose  : 
139 //=======================================================================
140 Standard_Boolean BRepMesh_CircleTool::Add(const gp_XY& p1,
141                                           const gp_XY& p2,
142                                           const gp_XY& p3,
143                                           const Standard_Integer theIndex)
144 {
145   gp_XY m1((p1.X()+p2.X())/2., (p1.Y()+p2.Y())/2.);
146   gp_XY m2((p2.X()+p3.X())/2., (p2.Y()+p3.Y())/2.);
147   gp_XY m3((p3.X()+p1.X())/2., (p3.Y()+p1.Y())/2.);
148   Standard_Real dx=m1.X()-m2.X();
149   Standard_Real dy=m1.Y()-m2.Y();
150   Standard_Real d12=(dx*dx)+(dy*dy);
151   dx=m2.X()-m3.X();
152   dy=m2.Y()-m3.Y();
153   Standard_Real d23=(dx*dx)+(dy*dy);
154   dx=m3.X()-m1.X();
155   dy=m3.Y()-m1.Y();
156   Standard_Real d31=(dx*dx)+(dy*dy);
157   gp_XY pl11, pl12, pl21, pl22;
158
159   if (d12>d23 && d12>d31) {
160     dy=p2.Y()-p1.Y();
161     dx=p1.X()-p2.X();
162     if (dy!=0. || dx!=0.) {
163       pl11 = m1;
164       pl12 = gp_XY(dy, dx);
165     }
166     else return Standard_False;
167
168     dy=p3.Y()-p2.Y();
169     dx=p2.X()-p3.X();
170     if (dy!=0. || dx!=0.) {
171       pl21 = m2;
172       pl22 = gp_XY(dy, dx);
173     }
174     else return Standard_False;
175   }
176   else {
177     if (d23>d31) {
178       dy=p3.Y()-p2.Y();
179       dx=p2.X()-p3.X();
180       if (dy!=0. || dx!=0.) {
181         pl11 = m2;
182         pl12 = gp_XY(dy, dx);
183       }
184       else return Standard_False;
185
186       dy=p1.Y()-p3.Y();
187       dx=p3.X()-p1.X();
188       if (dy!=0. || dx!=0.) {
189         pl21 = m3;
190         pl22 = gp_XY(dy, dx);
191       }
192       else return Standard_False;
193     }
194     else {
195       dy=p1.Y()-p3.Y();
196       dx=p3.X()-p1.X();
197       if (dy!=0. || dx!=0.) {
198         pl11 = m3;
199         pl12 = gp_XY(dy, dx);
200       }
201       else return Standard_False;
202
203       dy=p2.Y()-p1.Y();
204       dx=p1.X()-p2.X();
205       if (dy!=0. || dx!=0.) {
206         pl21 = m1;
207         pl22 = gp_XY(dy, dx);
208       }
209       else return Standard_False;
210     }
211   }
212
213   gp_XY aVecO1O2 = pl21 - pl11;
214   Standard_Real aCrossD1D2 = pl12 ^ pl22;
215   Standard_Real theSinAngle = Abs(aCrossD1D2);
216   if (theSinAngle < gp::Resolution())
217     return Standard_False;
218   Standard_Real theParam1 = (aVecO1O2 ^ pl22) / aCrossD1D2;
219   gp_XY pInt = pl11+pl12*theParam1;
220   dx=p1.X()-pInt.X();
221   dy=p1.Y()-pInt.Y();
222   Standard_Real R = Sqrt(dx*dx+dy*dy);
223   BRepMesh_Circ aCir(pInt, R);
224
225   //compute coords
226   Standard_Real xMax=Min(pInt.X()+R,FaceMax.X());
227   Standard_Real xMin=Max(pInt.X()-R,FaceMin.X());
228   Standard_Real yMax=Min(pInt.Y()+R,FaceMax.Y());
229   Standard_Real yMin=Max(pInt.Y()-R,FaceMin.Y());
230
231   gp_XY MinPnt(xMin,yMin);
232   gp_XY MaxPnt(xMax,yMax);    
233
234   CellFilter.Add(theIndex, MinPnt, MaxPnt);
235
236   Selector.Add(theIndex, aCir);
237   return Standard_True;
238 }
239
240 //=======================================================================
241 //function : Delete
242 //purpose  : 
243 //=======================================================================
244 void  BRepMesh_CircleTool::Delete(const Standard_Integer theIndex)
245 {
246   BRepMesh_Circ& Circ = Selector.GetCirc(theIndex);
247   if(Circ.Radius() > 0.) {
248     Circ.SetRadius(-1);
249   }
250 }
251
252 //=======================================================================
253 //function : Select
254 //purpose  : 
255 //=======================================================================
256 BRepMesh_ListOfInteger&  BRepMesh_CircleTool::Select(const gp_XY& thePnt)
257 {
258   Selector.ClerResList();
259   Selector.SetCurrent(thePnt);
260   CellFilter.Inspect (thePnt, Selector);
261   return Selector.GetCoincidentInd();
262 }
263
264 void BRepMesh_CircleTool::MocAdd(const Standard_Integer theIndex)
265 {
266   gp_XY nullPnt(0.,0.);
267   BRepMesh_Circ theNullCir(nullPnt, -1.);
268   Selector.Add(theIndex, theNullCir);
269 }