summaryrefslogtreecommitdiff
path: root/src/BRepMesh/BRepMesh_CircleTool.cxx
blob: ead438382e6b035dee0af746a06caccf2f68335c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// File:        BRepMesh_CircleTool.cxx
// Created:     Tue Jun 15 19:10:25 1993
// Author:      Didier PIFFAULT
//              <dpf@zerox>

#include <BRepMesh_CircleTool.ixx>
#include <gp_XY.hxx>
#include <Precision.hxx>
#include <BRepMesh_Circ.hxx>
#include <BRepMesh_CircleInspector.hxx>
#include <BRepMesh_BaseAllocator.hxx>


//=======================================================================
//function : BRepMesh_CircleInspector
//purpose  : Constructor
//
//=======================================================================

BRepMesh_CircleInspector::BRepMesh_CircleInspector (Standard_Real theTol,
                                                    Standard_Integer nbComp,
                                                    const BRepMesh_BaseAllocator& theAlloc)
                                                    : myTol(theTol*theTol),
                                                    myResInd(theAlloc),
                                                    myInitCircle(nbComp)
{
  //  myTol = theTol*theTol;
}

//=======================================================================
//function : Inspect
//purpose  : 
//
//=======================================================================
NCollection_CellFilter_Action BRepMesh_CircleInspector::Inspect (const Standard_Integer theTarget)
{
  const BRepMesh_Circ& Circ = myInitCircle(theTarget);
  Standard_Real R = Circ.Radius();
  if(R < 0)
    return CellFilter_Purge;
  Standard_Real dx,dy;
  const gp_XY& aLoc=Circ.Location();
  dx=myCurrent.X()-aLoc.X();
  dy=myCurrent.Y()-aLoc.Y();
  if ((dx*dx+dy*dy)-(R*R) <= myTol)
    myResInd.Append(theTarget);
  return CellFilter_Keep;
}


//=======================================================================
//function : BRepMesh_CircleTool
//purpose  : 
//=======================================================================
BRepMesh_CircleTool::BRepMesh_CircleTool(const BRepMesh_BaseAllocator& theAlloc)
: Tolerance(Precision::PConfusion()),
Allocator(theAlloc),
CellFilter(10, theAlloc),
Selector(Tolerance,64,theAlloc)
{
  Tolerance=Tolerance*Tolerance;
}

//=======================================================================
//function : BRepMesh_CircleTool
//purpose  : 
//=======================================================================
BRepMesh_CircleTool::BRepMesh_CircleTool(const Standard_Integer nbComp,
                                         const BRepMesh_BaseAllocator& theAlloc)
                                         : Tolerance(Precision::PConfusion()),
                                         Allocator(theAlloc),
                                         CellFilter(10, theAlloc),
                                         Selector(Tolerance,Max(nbComp,64),theAlloc)
{
  Tolerance=Tolerance*Tolerance;
}


//=======================================================================
//function : Initialize
//purpose  : 
//=======================================================================
void BRepMesh_CircleTool::Initialize(const Standard_Integer /*nbComp*/)
{
  Tolerance=Precision::PConfusion();
  Tolerance=Tolerance*Tolerance;
}

void BRepMesh_CircleTool::SetCellSize(const Standard_Real theSize)
{
  CellFilter.Reset(theSize, Allocator);
}

void BRepMesh_CircleTool::SetCellSize(const Standard_Real theXSize, 
                                      const Standard_Real theYSize)
{
  Standard_Real aCellSize[2];
  aCellSize[0] = theXSize;
  aCellSize[1] = theYSize;

  CellFilter.Reset(aCellSize, Allocator);
}


void BRepMesh_CircleTool::SetMinMaxSize(const gp_XY& theMin,
                                        const gp_XY& theMax)
{
  FaceMin = theMin;
  FaceMax = theMax;
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================
void  BRepMesh_CircleTool::Add(const gp_Circ2d& theCirc,
                               const Standard_Integer theIndex)
{
  gp_XY aCoord(theCirc.Location().Coord());
  Standard_Real R = theCirc.Radius();
  BRepMesh_Circ aCir(aCoord, R);

  //compute coords
  Standard_Real xMax=Min(aCoord.X()+R,FaceMax.X());
  Standard_Real xMin=Max(aCoord.X()-R,FaceMin.X());
  Standard_Real yMax=Min(aCoord.Y()+R,FaceMax.Y());
  Standard_Real yMin=Max(aCoord.Y()-R,FaceMin.Y());

  gp_XY MinPnt(xMin,yMin);
  gp_XY MaxPnt(xMax,yMax);

  CellFilter.Add(theIndex, MinPnt, MaxPnt);
  Selector.Add(theIndex, aCir);
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================
Standard_Boolean BRepMesh_CircleTool::Add(const gp_XY& p1,
                                          const gp_XY& p2,
                                          const gp_XY& p3,
                                          const Standard_Integer theIndex)
{
  gp_XY m1((p1.X()+p2.X())/2., (p1.Y()+p2.Y())/2.);
  gp_XY m2((p2.X()+p3.X())/2., (p2.Y()+p3.Y())/2.);
  gp_XY m3((p3.X()+p1.X())/2., (p3.Y()+p1.Y())/2.);
  Standard_Real dx=m1.X()-m2.X();
  Standard_Real dy=m1.Y()-m2.Y();
  Standard_Real d12=(dx*dx)+(dy*dy);
  dx=m2.X()-m3.X();
  dy=m2.Y()-m3.Y();
  Standard_Real d23=(dx*dx)+(dy*dy);
  dx=m3.X()-m1.X();
  dy=m3.Y()-m1.Y();
  Standard_Real d31=(dx*dx)+(dy*dy);
  gp_XY pl11, pl12, pl21, pl22;

  if (d12>d23 && d12>d31) {
    dy=p2.Y()-p1.Y();
    dx=p1.X()-p2.X();
    if (dy!=0. || dx!=0.) {
      pl11 = m1;
      pl12 = gp_XY(dy, dx);
    }
    else return Standard_False;

    dy=p3.Y()-p2.Y();
    dx=p2.X()-p3.X();
    if (dy!=0. || dx!=0.) {
      pl21 = m2;
      pl22 = gp_XY(dy, dx);
    }
    else return Standard_False;
  }
  else {
    if (d23>d31) {
      dy=p3.Y()-p2.Y();
      dx=p2.X()-p3.X();
      if (dy!=0. || dx!=0.) {
        pl11 = m2;
        pl12 = gp_XY(dy, dx);
      }
      else return Standard_False;

      dy=p1.Y()-p3.Y();
      dx=p3.X()-p1.X();
      if (dy!=0. || dx!=0.) {
        pl21 = m3;
        pl22 = gp_XY(dy, dx);
      }
      else return Standard_False;
    }
    else {
      dy=p1.Y()-p3.Y();
      dx=p3.X()-p1.X();
      if (dy!=0. || dx!=0.) {
        pl11 = m3;
        pl12 = gp_XY(dy, dx);
      }
      else return Standard_False;

      dy=p2.Y()-p1.Y();
      dx=p1.X()-p2.X();
      if (dy!=0. || dx!=0.) {
        pl21 = m1;
        pl22 = gp_XY(dy, dx);
      }
      else return Standard_False;
    }
  }

  gp_XY aVecO1O2 = pl21 - pl11;
  Standard_Real aCrossD1D2 = pl12 ^ pl22;
  Standard_Real theSinAngle = Abs(aCrossD1D2);
  if (theSinAngle < gp::Resolution())
    return Standard_False;
  Standard_Real theParam1 = (aVecO1O2 ^ pl22) / aCrossD1D2;
  gp_XY pInt = pl11+pl12*theParam1;
  dx=p1.X()-pInt.X();
  dy=p1.Y()-pInt.Y();
  Standard_Real R = Sqrt(dx*dx+dy*dy);
  BRepMesh_Circ aCir(pInt, R);

  //compute coords
  Standard_Real xMax=Min(pInt.X()+R,FaceMax.X());
  Standard_Real xMin=Max(pInt.X()-R,FaceMin.X());
  Standard_Real yMax=Min(pInt.Y()+R,FaceMax.Y());
  Standard_Real yMin=Max(pInt.Y()-R,FaceMin.Y());

  gp_XY MinPnt(xMin,yMin);
  gp_XY MaxPnt(xMax,yMax);    

  CellFilter.Add(theIndex, MinPnt, MaxPnt);

  Selector.Add(theIndex, aCir);
  return Standard_True;
}

//=======================================================================
//function : Delete
//purpose  : 
//=======================================================================
void  BRepMesh_CircleTool::Delete(const Standard_Integer theIndex)
{
  BRepMesh_Circ& Circ = Selector.GetCirc(theIndex);
  if(Circ.Radius() > 0.) {
    Circ.SetRadius(-1);
  }
}

//=======================================================================
//function : Select
//purpose  : 
//=======================================================================
BRepMesh_ListOfInteger&  BRepMesh_CircleTool::Select(const gp_XY& thePnt)
{
  Selector.ClerResList();
  Selector.SetCurrent(thePnt);
  CellFilter.Inspect (thePnt, Selector);
  return Selector.GetCoincidentInd();
}

void BRepMesh_CircleTool::MocAdd(const Standard_Integer theIndex)
{
  gp_XY nullPnt(0.,0.);
  BRepMesh_Circ theNullCir(nullPnt, -1.);
  Selector.Add(theIndex, theNullCir);
}