summaryrefslogtreecommitdiff
path: root/inc/GProp_VGPropsGK.gxx
blob: 4650ea747f4314359ab135f006e4b3b077d460ae (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// File:	GProp_VGPropsGK.gxx
// Created:	Fri Dec  9 14:00:04 2005
// Author:	Sergey KHROMOV
//		<skv@dimox>


#include <TColStd_HArray1OfReal.hxx>
#include <TColStd_Array1OfBoolean.hxx>
#include <math_KronrodSingleIntegration.hxx>
#include <math_Vector.hxx>
#include <math.hxx>

//==========================================================================
//function : Constructor
//           
//==========================================================================

GProp_VGPropsGK::GProp_VGPropsGK(      Face          &theSurface,
				 const gp_Pnt        &theLocation,
				 const Standard_Real  theTolerance,
				 const Standard_Boolean theCGFlag,
				 const Standard_Boolean theIFlag)
     : myErrorReached(0.)
{
  SetLocation(theLocation);
  Perform(theSurface, theTolerance, theCGFlag, theIFlag);
}

//==========================================================================
//function : Constructor
//           
//==========================================================================

GProp_VGPropsGK::GProp_VGPropsGK(      Face          &theSurface,
				 const gp_Pnt        &thePoint,
				 const gp_Pnt        &theLocation,
				 const Standard_Real  theTolerance,
				 const Standard_Boolean theCGFlag,
				 const Standard_Boolean theIFlag)

     : myErrorReached(0.)
{
  SetLocation(theLocation);
  Perform(theSurface, thePoint, theTolerance, theCGFlag, theIFlag);
}

//==========================================================================
//function : Constructor
//           
//==========================================================================

GProp_VGPropsGK::GProp_VGPropsGK(      Face          &theSurface,
				 Domain        &theDomain,
				 const gp_Pnt        &theLocation,
				 const Standard_Real  theTolerance,
				 const Standard_Boolean theCGFlag,
				 const Standard_Boolean theIFlag)

     : myErrorReached(0.)
{
  SetLocation(theLocation);
  Perform(theSurface, theDomain, theTolerance, theCGFlag, theIFlag);
}

//==========================================================================
//function : Constructor
//           
//==========================================================================

GProp_VGPropsGK::GProp_VGPropsGK(      Face          &theSurface,
			           Domain        &theDomain,
				 const gp_Pnt        &thePoint,
				 const gp_Pnt        &theLocation,
				 const Standard_Real  theTolerance,
				 const Standard_Boolean theCGFlag,
				 const Standard_Boolean theIFlag)

     : myErrorReached(0.)
{
  SetLocation(theLocation);
  Perform(theSurface, theDomain, thePoint, theTolerance, theCGFlag, theIFlag);
}

//==========================================================================
//function : Constructor
//           
//==========================================================================

GProp_VGPropsGK::GProp_VGPropsGK(      Face          &theSurface,
				 const gp_Pln        &thePlane,
				 const gp_Pnt        &theLocation,
				 const Standard_Real  theTolerance,
				 const Standard_Boolean theCGFlag,
				 const Standard_Boolean theIFlag)

     : myErrorReached(0.)
{
  SetLocation(theLocation);
  Perform(theSurface, thePlane, theTolerance, theCGFlag, theIFlag);
}

//==========================================================================
//function : Constructor
//           
//==========================================================================

GProp_VGPropsGK::GProp_VGPropsGK(      Face          &theSurface,
			           Domain        &theDomain,
				 const gp_Pln        &thePlane,
				 const gp_Pnt        &theLocation,
				 const Standard_Real  theTolerance,
				 const Standard_Boolean theCGFlag,
				 const Standard_Boolean theIFlag)

     : myErrorReached(0.)
{
  SetLocation(theLocation);
  Perform(theSurface, theDomain, thePlane, theTolerance, theCGFlag, theIFlag);
}

//==========================================================================
//function : Perform
//           Compute the properties.
//==========================================================================

Standard_Real GProp_VGPropsGK::Perform(      Face          &theSurface,
				       const Standard_Real  theTolerance,
				       const Standard_Boolean theCGFlag,
				       const Standard_Boolean theIFlag)

{
  Standard_Real aShift[] = { 0., 0., 0. };

  return PrivatePerform(theSurface, NULL, Standard_True, &aShift, theTolerance, 
			theCGFlag, theIFlag);
}

//==========================================================================
//function : Perform
//           Compute the properties.
//==========================================================================

Standard_Real GProp_VGPropsGK::Perform(      Face          &theSurface,
				       const gp_Pnt        &thePoint,
				       const Standard_Real  theTolerance,
				       const Standard_Boolean theCGFlag,
				       const Standard_Boolean theIFlag)

{
  gp_XYZ        aXYZ(thePoint.XYZ().Subtracted(loc.XYZ()));
  Standard_Real aShift[3];

  aXYZ.Coord(aShift[0], aShift[1], aShift[2]);

  return PrivatePerform(theSurface, NULL, Standard_True, &aShift, theTolerance, 
			theCGFlag, theIFlag);
}

//==========================================================================
//function : Perform
//           Compute the properties.
//==========================================================================

Standard_Real GProp_VGPropsGK::Perform(      Face          &theSurface,
				       Domain        &theDomain,
				       const Standard_Real  theTolerance,
				       const Standard_Boolean theCGFlag,
				       const Standard_Boolean theIFlag)

{
  Standard_Real aShift[] = { 0., 0., 0. };

  return PrivatePerform(theSurface, &theDomain,
			Standard_True, &aShift, theTolerance, 
			theCGFlag, theIFlag);
}

//==========================================================================
//function : Perform
//           Compute the properties.
//==========================================================================

Standard_Real GProp_VGPropsGK::Perform(      Face          &theSurface,
				       Domain        &theDomain,
				       const gp_Pnt        &thePoint,
				       const Standard_Real  theTolerance,
				       const Standard_Boolean theCGFlag,
				       const Standard_Boolean theIFlag)

{
  gp_XYZ        aXYZ(thePoint.XYZ().Subtracted(loc.XYZ()));
  Standard_Real aShift[3];

  aXYZ.Coord(aShift[0], aShift[1], aShift[2]);

  return PrivatePerform(theSurface, &theDomain,
			Standard_True, &aShift, theTolerance, 
			theCGFlag, theIFlag);
}

//==========================================================================
//function : Perform
//           Compute the properties.
//==========================================================================

Standard_Real GProp_VGPropsGK::Perform(      Face          &theSurface,
				       const gp_Pln        &thePlane,
				       const Standard_Real  theTolerance,
				       const Standard_Boolean theCGFlag,
				       const Standard_Boolean theIFlag)

{
  Standard_Real aCoeff[4];
  Standard_Real aXLoc;
  Standard_Real aYLoc;
  Standard_Real aZLoc;

  loc.Coord(aXLoc, aYLoc, aZLoc);
  thePlane.Coefficients (aCoeff[0], aCoeff[1], aCoeff[2], aCoeff[3]);
  aCoeff[3] = aCoeff[3] - aCoeff[0]*aXLoc - aCoeff[1]*aYLoc - aCoeff[2]*aZLoc;

  return PrivatePerform(theSurface, NULL,
			Standard_False, &aCoeff, theTolerance, 
			theCGFlag, theIFlag);
}

//==========================================================================
//function : Perform
//           Compute the properties.
//==========================================================================

Standard_Real GProp_VGPropsGK::Perform(      Face          &theSurface,
				       Domain        &theDomain,
				       const gp_Pln        &thePlane,
				       const Standard_Real  theTolerance,
				       const Standard_Boolean theCGFlag,
				       const Standard_Boolean theIFlag)

{
  Standard_Real aCoeff[4];
  Standard_Real aXLoc;
  Standard_Real aYLoc;
  Standard_Real aZLoc;

  loc.Coord(aXLoc, aYLoc, aZLoc);
  thePlane.Coefficients (aCoeff[0], aCoeff[1], aCoeff[2], aCoeff[3]);
  aCoeff[3] = aCoeff[3] - aCoeff[0]*aXLoc - aCoeff[1]*aYLoc - aCoeff[2]*aZLoc;

  return PrivatePerform(theSurface, &theDomain,
			Standard_False, &aCoeff, theTolerance, 
			theCGFlag, theIFlag);
}

//==========================================================================
//function : PrivatePerform
//           Compute the properties.
//==========================================================================

Standard_Real GProp_VGPropsGK::PrivatePerform
              (      Face             &theSurface,
	       const Standard_Address  thePtrDomain,
	       const Standard_Boolean  IsByPoint,
	       const Standard_Address  theCoeffs,
	       const Standard_Real     theTolerance,
	       const Standard_Boolean theCGFlag,
	       const Standard_Boolean theIFlag)

{

  const Standard_Real aTTol = 1.e-9;
  Standard_Real *aCoeffs = (Standard_Real *)theCoeffs;

  // Compute the number of 2d bounding curves of the face.
  Domain           *aPDomain = NULL;
  Standard_Integer  aNbCurves = 0;

  // If the pointer to the domain is NULL, there is only one curve to treat:
  // U isoline with the UMax parameter.
  if (thePtrDomain == NULL)
    aNbCurves = 1;
  else {
    aPDomain = (Domain *)thePtrDomain;

    for (aPDomain->Init(); aPDomain->More(); aPDomain->Next())
      aNbCurves++;
  }

  if (aNbCurves == 0) {
    myErrorReached = -1.;

    return myErrorReached;
  }

  //Standard_Real    aCrvTol = 0.5*theTolerance/aNbCurves;
  Standard_Real    aCrvTol = 0.1*theTolerance;
  Standard_Real    aUMin;
  Standard_Real    aUMax;
  Standard_Real    aTMin;
  Standard_Real    aTMax;
  Standard_Integer aNbPnts;
  Standard_Integer aNbMaxIter = 1000;
  Standard_Integer aNbVal = 10;
  Standard_Integer k;
  math_Vector      aLocalValue(1, aNbVal);
  math_Vector      aLocalTolReached(1, aNbVal);
  math_Vector      aValue(1, aNbVal);
  math_Vector      aTolReached(1, aNbVal);
  TColStd_Array1OfBoolean CFlags(1, aNbVal); 
  CFlags.Init(Standard_False);
  Standard_Boolean isMore;

  //aNbVal = 1;
  aValue.Init(0.);
  aTolReached.Init(0.);

  CFlags.Init(Standard_False);
  CFlags(1) = Standard_True;

  if(theCGFlag || theIFlag) {
    Standard_Integer i;
    for(i = 2; i <= 4; ++i) {CFlags(i) = Standard_True;}
  }

  if(theIFlag) {
    Standard_Integer i;
    for(i = 5; i <= 10; ++i) {CFlags(i) = Standard_True;}
  }

  theSurface.Bounds(aUMin, aUMax, aTMin, aTMax);

  if (thePtrDomain == NULL)
    isMore = Standard_True;
  else {
    aPDomain->Init();
    isMore = aPDomain->More();
  }

  while(isMore) {
    // If the pointer to the domain is NULL, there is only one curve to treat:
    // U isoline with the UMax parameter.

    if (thePtrDomain == NULL)
      theSurface.Load(Standard_False, GeomAbs_IsoU);
    else
      theSurface.Load(aPDomain->Value());

    aTMin = theSurface.FirstParameter();
    aTMax = theSurface.LastParameter();


    // Get the spans on the curve.
    Handle(TColStd_HArray1OfReal) aTKnots;
    GProp_TFunction               aTFunc(theSurface, loc, IsByPoint, theCoeffs,
					 aUMin, aCrvTol);

    theSurface.GetTKnots(aTMin, aTMax, aTKnots);

    Standard_Integer              iU             = aTKnots->Upper();
    Standard_Integer              aNbTIntervals  = aTKnots->Length() - 1;
    //Standard_Real                 aTolSpan       = aCrvTol/aNbTIntervals;
    Standard_Real                 aTolSpan       = 0.9*theTolerance; //Relative error
    math_KronrodSingleIntegration anIntegral;
    GProp_ValueType               aValueType;


    // Empirical criterion.
    aNbPnts = Min(15, theSurface.IntegrationOrder()/aNbTIntervals + 1);
    aNbPnts = Max(5, aNbPnts);
//     aNbPnts = theSurface.IntegrationOrder();

    aLocalValue.Init(0.);
    aLocalTolReached.Init(0.);

    for (k = 1; k <= aNbVal; k++) {

      if(!CFlags(k)) continue;

      Standard_Integer i = aTKnots->Lower();

      switch (k) {
      case 1:   aValueType = GProp_Mass;        break;
      case 2:   aValueType = GProp_CenterMassX; break;
      case 3:   aValueType = GProp_CenterMassY; break;
      case 4:   aValueType = GProp_CenterMassZ; break;
      case 5:   aValueType = GProp_InertiaXX;   break;
      case 6:   aValueType = GProp_InertiaYY;   break;
      case 7:   aValueType = GProp_InertiaZZ;   break;
      case 8:   aValueType = GProp_InertiaXY;   break;
      case 9:   aValueType = GProp_InertiaXZ;   break;
      case 10:  aValueType = GProp_InertiaYZ;   break;

      default: myErrorReached = -1.; return myErrorReached;
      }
      aTFunc.SetValueType(aValueType);

      Standard_Real err1 = 0.;
      while (i < iU) {

	//cout << "-------------- Span " << i << " nbp: " << aNbPnts << endl;
	Standard_Real aT1 = aTKnots->Value(i++);
	Standard_Real aT2 = aTKnots->Value(i);

	if(aT2 - aT1 < aTTol) continue;

	aTFunc.SetNbKronrodPoints(aNbPnts);
	aTFunc.Init();
	aTFunc.SetTolerance(aCrvTol/(aT2-aT1));
	anIntegral.Perform(aTFunc, aT1, aT2, aNbPnts, aTolSpan, aNbMaxIter);

	if (!anIntegral.IsDone()) {
	  myErrorReached = -1.;

	  return myErrorReached;
	}

	aLocalValue(k)      += anIntegral.Value();
	err1 = aTFunc.AbsolutError()*(aT2 - aT1); 
	//cout << "Errors: " << anIntegral.NbIterReached() << " " << anIntegral.AbsolutError() << " " << err1 << endl;
	aLocalTolReached(k) += anIntegral.AbsolutError() + err1; 
	//cout << "--- Errors: " << anIntegral.NbIterReached() << " " << anIntegral.AbsolutError() << " " << err1 << endl;
      }

      aValue(k)      += aLocalValue(k);
      aTolReached(k) += aLocalTolReached(k);
    }

    // If the pointer to the domain is NULL, there is only one curve to treat:
    // U isoline with the UMax parameter.
    if (thePtrDomain == NULL)
      isMore = Standard_False;
    else {
      aPDomain->Next();
      isMore = aPDomain->More();
    }
  }

  // Get volume value.
  dim            = aValue(1);
  myErrorReached = aTolReached(1);
  myAbsolutError = myErrorReached;
  Standard_Real anAbsDim = Abs(dim);
  Standard_Real aVolTol  = Epsilon(myAbsolutError);
  if(anAbsDim >= aVolTol) myErrorReached /= anAbsDim;

  if(theCGFlag || theIFlag) {
    // Compute values of center of mass.
    if(anAbsDim >= aVolTol) {
      if (IsByPoint) {
	aValue(2) = aCoeffs[0] + aValue(2)/dim;
	aValue(3) = aCoeffs[1] + aValue(3)/dim;
	aValue(4) = aCoeffs[2] + aValue(4)/dim;
      } else {
	aValue(2) /= dim;
	aValue(3) /= dim;
	aValue(4) /= dim;
      }
    } else {
      aValue(2) = 0.;
      aValue(3) = 0.;
      aValue(4) = 0.;
      dim = 0.;
    }
    g.SetCoord(aValue(2), aValue(3), aValue(4));
  }

  if(theIFlag) {
    // Fill the matrix of inertia.
    inertia.SetCols (gp_XYZ (aValue(5), aValue(8),  aValue(9)),
		     gp_XYZ (aValue(8), aValue(6),  aValue(10)),
		     gp_XYZ (aValue(9), aValue(10), aValue(7)));
  }
  //return myErrorReached;
  return myAbsolutError;
}