summaryrefslogtreecommitdiff
path: root/src/ShapeAnalysis/ShapeAnalysis_FreeBoundsProperties.cxx
blob: 14dd52c1c94d71edb59a82b5c3d2e0d210e4f26e (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// File:	ShapeAnalysis_ShellBounds.cxx
// Created:	Tue Aug  4 14:03:19 1998
// Author:	Pavel DURANDIN
//		<pdn@nnov.matra-dtv.fr>
// 25.12.98 pdn renaming of ShapeAnalysis_FreeBounds and ShapeAnalysis_Wire methods
//szv#4 S4163

#include <ShapeAnalysis_FreeBoundsProperties.ixx>
#include <ShapeAnalysis_FreeBounds.hxx>
#include <ShapeAnalysis_Edge.hxx>
#include <ShapeAnalysis_Wire.hxx>
#include <ShapeAnalysis_FreeBoundData.hxx>

#include <ShapeExtend_Explorer.hxx>
#include <ShapeExtend_WireData.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>

#include <BRepTools_WireExplorer.hxx>
#include <BRep_Builder.hxx>
#include <TopTools_HSequenceOfShape.hxx>

#include <gp_Pnt.hxx>
#include <gp_XYZ.hxx>
#include <gp_Vec.hxx>

#include <Geom_Curve.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <Precision.hxx>

#define NbControl 23

static void ContourProperties(TopoDS_Wire wire,
			      Standard_Real& countourArea,
			      Standard_Real& countourLength)
{
  Standard_Integer nbe = 0;
  Standard_Real length = 0.0;
  gp_XYZ area(.0,.0,.0);
  gp_XYZ prev, cont;
  
  for (BRepTools_WireExplorer exp(wire); exp.More(); exp.Next()) {
    TopoDS_Edge Edge = exp.Current();  nbe++;
      
    Standard_Real First, Last;
    Handle(Geom_Curve) c3d;
    ShapeAnalysis_Edge sae;
    if (!sae.Curve3d(Edge,c3d,First,Last)) continue;
      
    Standard_Integer ibeg = 0;
    if ( nbe == 1 ) {
      gp_Pnt pntIni = c3d->Value(First);
      prev = pntIni.XYZ();
      cont = prev;
      ibeg = 1;
    }

    for ( Standard_Integer i = ibeg; i < NbControl; i++) {
      Standard_Real prm = ((NbControl-1-i)*First + i*Last)/(NbControl-1);
      gp_Pnt pntCurr = c3d->Value(prm);
      gp_XYZ curr = pntCurr.XYZ();
      gp_XYZ delta = curr - prev;
      length += delta.Modulus();
      area += curr ^ prev;
      prev = curr;
    }
  }

  area +=  cont ^ prev;
  countourArea = area.Modulus()/2;
  countourLength = length;
}

//=======================================================================
//function : ShapeAnalysis_FreeBoundsProperties
//purpose  : Empty constructor
//=======================================================================

ShapeAnalysis_FreeBoundsProperties::ShapeAnalysis_FreeBoundsProperties()
{
  myClosedFreeBounds = new ShapeAnalysis_HSequenceOfFreeBounds();
  myOpenFreeBounds   = new ShapeAnalysis_HSequenceOfFreeBounds();
  myTolerance = 0.;
}

//=======================================================================
//function : ShapeAnalysis_FreeBoundsProperties
//purpose  : Creates the object and calls corresponding Init.
//    	     <shape> should be a compound of faces.
//=======================================================================

ShapeAnalysis_FreeBoundsProperties::ShapeAnalysis_FreeBoundsProperties(const TopoDS_Shape& shape,
								       const Standard_Real tolerance,
								       const Standard_Boolean splitclosed,
								       const Standard_Boolean splitopen)
{
  myClosedFreeBounds = new ShapeAnalysis_HSequenceOfFreeBounds();
  myOpenFreeBounds   = new ShapeAnalysis_HSequenceOfFreeBounds();
  Init(shape, tolerance, splitclosed, splitopen);
}

//=======================================================================
//function : ShapeAnalysis_FreeBoundsProperties
//purpose  : Creates the object and calls corresponding Init.
//    	     <shape> should be a compound of shells.
//=======================================================================

ShapeAnalysis_FreeBoundsProperties::ShapeAnalysis_FreeBoundsProperties(const TopoDS_Shape& shape,
								       const Standard_Boolean splitclosed,
								       const Standard_Boolean splitopen)
{
  myClosedFreeBounds = new ShapeAnalysis_HSequenceOfFreeBounds();
  myOpenFreeBounds   = new ShapeAnalysis_HSequenceOfFreeBounds();
  myTolerance =0.;
  Init(shape, splitclosed, splitopen);
}

//=======================================================================
//function : Init
//purpose  : Initializes the object with given parameters.
//   	     <shape> should be a compound of faces.
//=======================================================================

void ShapeAnalysis_FreeBoundsProperties::Init(const TopoDS_Shape& shape,
					      const Standard_Real tolerance,
					      const Standard_Boolean splitclosed,
					      const Standard_Boolean splitopen) 
{
  Init(shape, splitclosed, splitopen);
  myTolerance = tolerance;
}


//=======================================================================
//function : Init
//purpose  : Initializes the object with given parameters.
//   	     <shape> should be a compound of shells.
//=======================================================================

void ShapeAnalysis_FreeBoundsProperties::Init(const TopoDS_Shape& shape,
					      const Standard_Boolean splitclosed,
					      const Standard_Boolean splitopen) 
{
  myShape = shape;
  mySplitClosed = splitclosed;
  mySplitOpen = splitopen;
}


//=======================================================================
//function : Perform
//purpose  : Builds and analyzes free bounds of the shape.
//           First calls ShapeAnalysis_FreeBounds for building free 
//           bounds.
//           Then on each free bound computes its properties:
//           - area of the contour,
//           - perimeter of the contour,
//           - ratio of average length to average width of the contour,
//           - average width of contour,
//           - notches on the contour and for each notch
//            - maximum width of the notch.
//returns:   True  - if no fails and free bounds are found,
//           False - if fail or no free bounds are found
//=======================================================================

Standard_Boolean ShapeAnalysis_FreeBoundsProperties::Perform() 
{
  Standard_Boolean result = Standard_False;
  result |= DispatchBounds();
  result |= CheckNotches();
  result |= CheckContours();
  return result;
}    


//=======================================================================
//function : DispatchBounds
//purpose  : 
//=======================================================================

Standard_Boolean ShapeAnalysis_FreeBoundsProperties::DispatchBounds() 
{
  if (!IsLoaded()) return Standard_False;

  TopoDS_Compound tmpClosedBounds, tmpOpenBounds;
  if ( myTolerance > 0.) {
    ShapeAnalysis_FreeBounds safb(myShape, myTolerance, mySplitClosed, mySplitOpen);
    tmpClosedBounds = safb.GetClosedWires();
    tmpOpenBounds   = safb.GetOpenWires();
  }
  else {
    ShapeAnalysis_FreeBounds safb(myShape, mySplitClosed, mySplitOpen);
    tmpClosedBounds = safb.GetClosedWires();
    tmpOpenBounds   = safb.GetOpenWires();
  }
  
  ShapeExtend_Explorer shexpl;
  Handle(TopTools_HSequenceOfShape) tmpSeq = shexpl.SeqFromCompound(tmpClosedBounds,Standard_False);
  Standard_Integer i; // svv Jan11 2000 : porting on DEC
  for (i = 1; i<=tmpSeq->Length(); i++) { 
    TopoDS_Wire wire = TopoDS::Wire(tmpSeq->Value(i));
    Handle(ShapeAnalysis_FreeBoundData) fbData= new ShapeAnalysis_FreeBoundData() ;
    fbData->SetFreeBound(wire);
    myClosedFreeBounds -> Append(fbData);
  }                       
  
  Handle(TopTools_HSequenceOfShape) tmpSeq2 = shexpl.SeqFromCompound(tmpOpenBounds,Standard_False);
  for(i = 1; i<=tmpSeq2->Length(); i++) {
    TopoDS_Wire wire = TopoDS::Wire(tmpSeq2->Value(i));
    Handle(ShapeAnalysis_FreeBoundData) fbData = new ShapeAnalysis_FreeBoundData;
    fbData->SetFreeBound(wire);
    myOpenFreeBounds -> Append(fbData); 
  }

  return Standard_True;
}


//=======================================================================
//function : CheckNotches
//purpose  : 
//=======================================================================

Standard_Boolean ShapeAnalysis_FreeBoundsProperties::CheckNotches(const Standard_Real prec) 
{
  Standard_Integer i; // svv Jan11 2000 : porting on DEC
  for(i = 1; i <= myClosedFreeBounds->Length(); i++) {
    Handle(ShapeAnalysis_FreeBoundData) fbData = myClosedFreeBounds->Value(i);
    CheckNotches(fbData, prec);
  }
  for( i = 1; i <= myOpenFreeBounds->Length(); i++) {
    Handle(ShapeAnalysis_FreeBoundData) fbData = myOpenFreeBounds->Value(i);
    CheckNotches(fbData, prec);
  }

  return Standard_True;
}

//=======================================================================
//function : CheckNotches
//purpose  : 
//=======================================================================

Standard_Boolean ShapeAnalysis_FreeBoundsProperties::CheckNotches(Handle(ShapeAnalysis_FreeBoundData)& fbData,
								  const Standard_Real prec)
{
  ShapeExtend_WireData swd(fbData->FreeBound());
  if (swd.NbEdges() > 1)
    for (Standard_Integer j=1; j <= swd.NbEdges(); j++) {
      TopoDS_Wire notch;
      Standard_Real dMax;
      if (CheckNotches(fbData->FreeBound(), j, notch, dMax, prec))
	fbData->AddNotch(notch, dMax);
    }

  return Standard_True;
}  

//=======================================================================
//function : CheckContours
//purpose  : 
//=======================================================================

Standard_Boolean ShapeAnalysis_FreeBoundsProperties::CheckContours(const Standard_Real prec) 
{
  Standard_Boolean status = Standard_False;
  Standard_Integer i; // svv Jan11 2000 : porting on DEC
  for( i = 1; i <= myClosedFreeBounds->Length(); i++) {
    Handle(ShapeAnalysis_FreeBoundData) fbData = myClosedFreeBounds->Value(i);
    status |= FillProperties(fbData,prec);
  }
  for ( i = 1; i <= myOpenFreeBounds->Length(); i++) {
    Handle(ShapeAnalysis_FreeBoundData) fbData = myOpenFreeBounds->Value(i);
    status |= FillProperties(fbData,prec);
  }

  return status;
}


//=======================================================================
//function : CheckNotches
//purpose  : 
//=======================================================================

Standard_Boolean ShapeAnalysis_FreeBoundsProperties::CheckNotches(const TopoDS_Wire& wire,
								  const Standard_Integer num,
								  TopoDS_Wire& notch,
								  Standard_Real& distMax, 
								  const Standard_Real /*prec*/) 
{
  Standard_Real tol = Max ( myTolerance, Precision::Confusion());
  Handle(ShapeExtend_WireData) wdt = new ShapeExtend_WireData(wire);
  BRep_Builder B;
  B.MakeWire(notch);
  
  if ( (num <= 0)||(num > wdt->NbEdges()) ) return Standard_False;
  
  Standard_Integer n1 = ( num > 0 ? num : wdt->NbEdges() );
  Standard_Integer n2 = ( n1  < wdt->NbEdges() ? n1+1 : 1 );
  
  TopoDS_Edge E1 = wdt->Edge(n1);
  B.Add(notch,E1);

  Handle(ShapeAnalysis_Wire) saw = new ShapeAnalysis_Wire;
  saw->Load(wdt);
  saw->SetPrecision(myTolerance);
  if ( saw->CheckSmall( n2, tol ) ) {
    B.Add(notch,wdt->Edge(n2));
    n2 = ( n2 < wdt->NbEdges() ? n2+1 : 1); 
  }

  TopoDS_Edge E2 = wdt->Edge(n2);
  B.Add(notch,E2);
  
  Standard_Real First1, Last1, First2, Last2;
  Handle(Geom_Curve) c3d1, c3d2;
  ShapeAnalysis_Edge sae;
  //szv#4:S4163:12Mar99 optimized
  if ( !sae.Curve3d(E1, c3d1, First1, Last1) ||
       !sae.Curve3d(E2, c3d2, First2, Last2) ) return Standard_False;
  
  gp_Pnt pnt;
  gp_Vec vec1, vec2;
  c3d1->D1(Last1, pnt, vec1);
  c3d2->D1(First2,pnt, vec2);
  if ( E1.Orientation() == TopAbs_REVERSED ) vec1.Reverse();
  if ( E2.Orientation() == TopAbs_REVERSED ) vec2.Reverse();
  
  Standard_Real angl = Abs( vec1.Angle(vec2));
  if (angl > 0.95*PI) {
    distMax = .0;
    for (Standard_Integer i = 0; i < NbControl; i++) {
      Standard_Real prm = ((NbControl-1-i)*First1 + i*Last1)/(NbControl-1);
      gp_Pnt pntCurr = c3d1->Value(prm);

      Standard_Real p1, p2;
      if ( First2 < Last2 ) {
	p1 = First2;
	p2 = Last2;
      }
      else {
	p1 = Last2;
	p2 = First2;
      }

      //szv#4:S4163:12Mar99 warning
      GeomAPI_ProjectPointOnCurve ppc(pntCurr, c3d2, p1, p2);
      Standard_Real newDist = (ppc.NbPoints() ? ppc.LowerDistance() : 0);
      if ( newDist > distMax ) distMax = newDist;
    }

    return Standard_True;  
  }

  return Standard_False;
}


//=======================================================================
//function : FillProperties
//purpose  : 
//=======================================================================

Standard_Boolean ShapeAnalysis_FreeBoundsProperties::FillProperties(Handle(ShapeAnalysis_FreeBoundData)& fbData,
								    const Standard_Real /*prec*/) 
{  
  Standard_Real area, length;
  ContourProperties(fbData->FreeBound(), area, length);

  Standard_Real r = 0;
  Standard_Real aver = 0;

  if ( length != 0. ) { //szv#4:S4163:12Mar99 anti-exception
    Standard_Real k =  area /(length*length); //szv#4:S4163:12Mar99
    //szv#4:S4163:12Mar99 optimized
    if ( k != 0. ) { //szv#4:S4163:12Mar99 anti-exception
      Standard_Real aux =  1. - 16.*k;
      if ( aux >= 0.) {
	r = (1. + sqrt(aux))/(8.*k);
	aver = length/(2.*r);
	r -= 1.;
      }
    }
  }
  
  fbData->SetArea(area);
  fbData->SetPerimeter(length);
  fbData->SetRatio(r);
  fbData->SetWidth(aver);

  return Standard_True;
}