summaryrefslogtreecommitdiff
path: root/inc/GProp_UFunction.gxx
blob: 07fcbd90a02331424550c53d04dc479c6d334dc9 (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
// File:	GProp_UFunction.gxx
// Created:	Fri Dec  9 16:18:05 2005
// Author:	Sergey KHROMOV
//		<skv@dimox>


//=======================================================================
//function : Constructor.
//purpose  : 
//=======================================================================

GProp_UFunction::GProp_UFunction(const Face             &theSurface,
				 const gp_Pnt           &theVertex,
				 const Standard_Boolean  IsByPoint,
				 const Standard_Address  theCoeffs)
     : mySurface(theSurface),
       myVertex(theVertex),
       myCoeffs(theCoeffs),
       myVParam(0.),
       myValueType(GProp_Unknown),
       myIsByPoint(IsByPoint)
{
}

//=======================================================================
//function : Value
//purpose  : Returns a value of the function.
//=======================================================================

Standard_Boolean GProp_UFunction::Value(const Standard_Real  X,
					      Standard_Real &F)
{
  // Volume computation
  if (myValueType == GProp_Mass) {
    gp_XYZ        aPMP0;
    Standard_Real aTmpPar1;
    Standard_Real aTmpPar2;

    F = VolumeValue(X, aPMP0, aTmpPar1, aTmpPar2);

    return Standard_True;
  }

  // Center of mass computation
  if (myValueType == GProp_CenterMassX ||
      myValueType == GProp_CenterMassY ||
      myValueType == GProp_CenterMassZ)
    return CenterMassValue(X, F);

  // Inertia computation
  if (myValueType == GProp_InertiaXX ||
      myValueType == GProp_InertiaYY ||
      myValueType == GProp_InertiaZZ ||
      myValueType == GProp_InertiaXY ||
      myValueType == GProp_InertiaXZ ||
      myValueType == GProp_InertiaYZ)
    return InertiaValue(X, F);

  return Standard_False;
}

//=======================================================================
//function : VolumeValue
//purpose  : Returns the value for volume computation.
//=======================================================================

Standard_Real GProp_UFunction::VolumeValue(const Standard_Real  X,
					         gp_XYZ        &thePMP0,
					         Standard_Real &theS,
					         Standard_Real &theD1)
{
  gp_Pnt aPnt;
  gp_Vec aNorm;

  mySurface.Normal(X, myVParam, aPnt, aNorm);

  thePMP0 = aPnt.XYZ().Subtracted(myVertex.XYZ());

  // Volume computation for ByPoint mode.
  if (myIsByPoint)
    return thePMP0.Dot(aNorm.XYZ());

  // Volume and additional coefficients computation for ByPlane mode.
  Standard_Real *aCoeff = (Standard_Real *)myCoeffs;

  theS  =   aNorm.X()*aCoeff[0] + aNorm.Y()*aCoeff[1] + aNorm.Z()*aCoeff[2];
  theD1 =   thePMP0.X()*aCoeff[0] + thePMP0.Y()*aCoeff[1]
          + thePMP0.Z()*aCoeff[2] - aCoeff[3];

  return theS*theD1;
}

//=======================================================================
//function : CenterMassValue
//purpose  : Returns a value for the center of mass computation.
//=======================================================================

Standard_Boolean GProp_UFunction::CenterMassValue(const Standard_Real  X,
						        Standard_Real &F)
{
  gp_XYZ        aPmP0;
  Standard_Real aS;
  Standard_Real aD1 = 0;

  F = VolumeValue(X, aPmP0, aS, aD1);

  // Center of mass computation for ByPoint mode.
  if (myIsByPoint) {
    switch (myValueType) {
    case GProp_CenterMassX:   F *= aPmP0.X(); break;
    case GProp_CenterMassY:   F *= aPmP0.Y(); break;
    case GProp_CenterMassZ:   F *= aPmP0.Z(); break;
    default:
      return Standard_False;
    }

    return Standard_True;
  }

  // Center of mass computation for ByPlane mode.
  Standard_Real *aCoeff = (Standard_Real *)myCoeffs;

  switch (myValueType) {
  case GProp_CenterMassX:   F *= (aPmP0.X() - 0.5*aCoeff[0]*aD1); break;
  case GProp_CenterMassY:   F *= (aPmP0.Y() - 0.5*aCoeff[1]*aD1); break;
  case GProp_CenterMassZ:   F *= (aPmP0.Z() - 0.5*aCoeff[2]*aD1); break;
  default:
    return Standard_False;
  }

  return Standard_True;
}

//=======================================================================
//function : InertiaValue
//purpose  : Compute the value of intertia.
//=======================================================================

Standard_Boolean GProp_UFunction::InertiaValue(const Standard_Real  X,
					             Standard_Real &F)
{
  gp_XYZ        aPmP0;
  Standard_Real aS = 0;
  Standard_Real aD1 = 0;
  Standard_Real aParam1;
  Standard_Real aParam2;
  Standard_Real *aCoeffs = (Standard_Real *)myCoeffs;

  F = VolumeValue(X, aPmP0, aS, aD1);

  // Inertia computation for ByPoint mode.
  if (myIsByPoint) {
    switch(myValueType) {
    case GProp_InertiaXX:
    case GProp_InertiaYZ:
      aParam1 = aPmP0.Y() - aCoeffs[1];
      aParam2 = aPmP0.Z() - aCoeffs[2];
      break;
    case GProp_InertiaYY:
    case GProp_InertiaXZ:
      aParam1 = aPmP0.X() - aCoeffs[0];
      aParam2 = aPmP0.Z() - aCoeffs[2];
      break;
    case GProp_InertiaZZ:
    case GProp_InertiaXY:
      aParam1 = aPmP0.X() - aCoeffs[0];
      aParam2 = aPmP0.Y() - aCoeffs[1];
      break;
    default:
      return Standard_False;
    }

    if (myValueType == GProp_InertiaXX ||
	myValueType == GProp_InertiaYY ||
	myValueType == GProp_InertiaZZ)
      F *=  aParam1*aParam1 + aParam2*aParam2;
    else
      F *= -aParam1*aParam2;

    return Standard_True;
  }

  // Inertia computation for ByPlane mode.
  Standard_Real aD2 = aD1*aD1;
  Standard_Real aD3 = aD1*aD2/3.;
  Standard_Real aPPar1;
  Standard_Real aPPar2;
  Standard_Real aCoeff1;
  Standard_Real aCoeff2;

  // Inertia computation for XX, YY and ZZ.
  if (myValueType == GProp_InertiaXX ||
      myValueType == GProp_InertiaYY ||
      myValueType == GProp_InertiaZZ) {

    if (myValueType == GProp_InertiaXX) {
      aPPar1  = aPmP0.Y();
      aPPar2  = aPmP0.Z();
      aCoeff1 = aCoeffs[1];
      aCoeff2 = aCoeffs[2];
    } else if (myValueType == GProp_InertiaYY) {
      aPPar1  = aPmP0.X();
      aPPar2  = aPmP0.Z();
      aCoeff1 = aCoeffs[0];
      aCoeff2 = aCoeffs[2];
    } else { // myValueType == GProp_InertiaZZ
      aPPar1  = aPmP0.X();
      aPPar2  = aPmP0.Y();
      aCoeff1 = aCoeffs[0];
      aCoeff2 = aCoeffs[1];
    }

    aPPar1  -= aCoeff1*aD1;
    aPPar2  -= aCoeff2*aD1;
    aParam1  = aPPar1*aPPar1*aD1 + aPPar1*aCoeff1*aD2 + aCoeff1*aCoeff1*aD3;
    aParam2  = aPPar2*aPPar2*aD1 + aPPar2*aCoeff2*aD2 + aCoeff2*aCoeff2*aD3;

    F = (aParam1 + aParam2)*aS;

    return Standard_True;
  }

  // Inertia computation for XY, YZ and XZ.
  if (myValueType == GProp_InertiaXY ||
      myValueType == GProp_InertiaYZ ||
      myValueType == GProp_InertiaXZ) {

    if (myValueType == GProp_InertiaXY) {
      aPPar1  = aPmP0.X();
      aPPar2  = aPmP0.Y();
      aCoeff1 = aCoeffs[0];
      aCoeff2 = aCoeffs[1];
    } else if (myValueType == GProp_InertiaYZ) {
      aPPar1  = aPmP0.Y();
      aPPar2  = aPmP0.Z();
      aCoeff1 = aCoeffs[1];
      aCoeff2 = aCoeffs[2];
    } else { // myValueType == GProp_InertiaXZ
      aPPar1  = aPmP0.X();
      aPPar2  = aPmP0.Z();
      aCoeff1 = aCoeffs[0];
      aCoeff2 = aCoeffs[2];
    }

    aD2     *=   0.5;
    aPPar1  -=   aCoeff1*aD1;
    aPPar2  -=   aCoeff2*aD1;
    aParam1  =   aPPar1*aPPar2*aD1
               + (aPPar1*aCoeff2 + aPPar2*aCoeff1)*aD2 + aCoeff1*aCoeff2*aD3;

    F = -aParam1*aS;

    return Standard_True;
  }

  return Standard_False;
}