cellPointWeight.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "cellPointWeight.H"
00028 #include "polyMesh.H"
00029
00030
00031
00032 int Foam::cellPointWeight::debug(debug::debugSwitch("cellPointWeight", 0));
00033 Foam::scalar Foam::cellPointWeight::tol(SMALL);
00034
00035
00036
00037 void Foam::cellPointWeight::findTetrahedron
00038 (
00039 const polyMesh& mesh,
00040 const vector& position,
00041 const label cellIndex
00042 )
00043 {
00044 if (debug)
00045 {
00046 Pout<< "\nFoam::cellPointWeight::findTetrahedron" << nl
00047 << "position = " << position << nl
00048 << "cellIndex = " << cellIndex << endl;
00049 }
00050
00051
00052 scalar minUVWClose = VGREAT;
00053 label pointIClose = 0;
00054 label faceClose = 0;
00055
00056 const vector& P0 = mesh.cellCentres()[cellIndex];
00057 const labelList& cellFaces = mesh.cells()[cellIndex];
00058 const scalar cellVolume = mesh.cellVolumes()[cellIndex];
00059
00060
00061 forAll(cellFaces, faceI)
00062 {
00063
00064
00065 const labelList& facePoints = mesh.faces()[cellFaces[faceI]];
00066
00067 label pointI = 1;
00068 while ((pointI + 1) < facePoints.size())
00069 {
00070
00071 const vector& P1 = mesh.points()[facePoints[0]];
00072 const vector& P2 = mesh.points()[facePoints[pointI]];
00073 const vector& P3 = mesh.points()[facePoints[pointI + 1]];
00074
00075
00076 const vector e1 = P1 - P0;
00077 const vector e2 = P2 - P0;
00078 const vector e3 = P3 - P0;
00079
00080
00081
00082
00083 const vector rhs = position - P0;
00084
00085
00086
00087 const scalar detA =
00088 e1.x()*e2.y()*e3.z() + e2.x()*e3.y()*e1.z()
00089 + e3.x()*e1.y()*e2.z() - e1.x()*e3.y()*e2.z()
00090 - e2.x()*e1.y()*e3.z() - e3.x()*e2.y()*e1.z();
00091
00092 if (mag(detA/cellVolume) > tol)
00093 {
00094
00095 const scalar u =
00096 (
00097 rhs.x()*e2.y()*e3.z() + e2.x()*e3.y()*rhs.z()
00098 + e3.x()*rhs.y()*e2.z() - rhs.x()*e3.y()*e2.z()
00099 - e2.x()*rhs.y()*e3.z() - e3.x()*e2.y()*rhs.z()
00100 )/detA;
00101
00102 const scalar v =
00103 (
00104 e1.x()*rhs.y()*e3.z() + rhs.x()*e3.y()*e1.z()
00105 + e3.x()*e1.y()*rhs.z() - e1.x()*e3.y()*rhs.z()
00106 - rhs.x()*e1.y()*e3.z() - e3.x()*rhs.y()*e1.z()
00107 )/detA;
00108
00109 const scalar w =
00110 (
00111 e1.x()*e2.y()*rhs.z() + e2.x()*rhs.y()*e1.z()
00112 + rhs.x()*e1.y()*e2.z() - e1.x()*rhs.y()*e2.z()
00113 - e2.x()*e1.y()*rhs.z() - rhs.x()*e2.y()*e1.z()
00114 )/detA;
00115
00116
00117
00118 if
00119 (
00120 (u + tol > 0) && (v + tol > 0) && (w + tol > 0)
00121 && (u + v + w < 1 + tol)
00122 )
00123 {
00124 faceVertices_[0] = facePoints[0];
00125 faceVertices_[1] = facePoints[pointI];
00126 faceVertices_[2] = facePoints[pointI + 1];
00127
00128 weights_[0] = u;
00129 weights_[1] = v;
00130 weights_[2] = w;
00131 weights_[3] = 1.0 - (u + v + w);
00132
00133 return;
00134 }
00135 else
00136 {
00137 scalar minU = mag(u);
00138 scalar minV = mag(v);
00139 scalar minW = mag(w);
00140 if (minU > 1.0)
00141 {
00142 minU -= 1.0;
00143 }
00144 if (minV > 1.0)
00145 {
00146 minV -= 1.0;
00147 }
00148 if (minW > 1.0)
00149 {
00150 minW -= 1.0;
00151 }
00152 const scalar minUVW = mag(minU + minV + minW);
00153
00154 if (minUVW < minUVWClose)
00155 {
00156 minUVWClose = minUVW;
00157 pointIClose = pointI;
00158 faceClose = faceI;
00159 }
00160 }
00161 }
00162
00163 pointI++;
00164 }
00165 }
00166
00167 if (debug)
00168 {
00169 Pout<< "cellPointWeight::findTetrahedron" << nl
00170 << " Tetrahedron search failed; using closest tet values to "
00171 << "point " << nl << " cell: " << cellIndex << nl << endl;
00172 }
00173
00174 const labelList& facePointsClose = mesh.faces()[cellFaces[faceClose]];
00175 faceVertices_[0] = facePointsClose[0];
00176 faceVertices_[1] = facePointsClose[pointIClose];
00177 faceVertices_[2] = facePointsClose[pointIClose + 1];
00178
00179 weights_[0] = 0.25;
00180 weights_[1] = 0.25;
00181 weights_[2] = 0.25;
00182 weights_[3] = 0.25;
00183 }
00184
00185
00186 void Foam::cellPointWeight::findTriangle
00187 (
00188 const polyMesh& mesh,
00189 const vector& position,
00190 const label faceIndex
00191 )
00192 {
00193 if (debug)
00194 {
00195 Pout<< "\nbool Foam::cellPointWeight::findTriangle" << nl
00196 << "position = " << position << nl
00197 << "faceIndex = " << faceIndex << endl;
00198 }
00199
00200
00201 scalar minUVClose = VGREAT;
00202 label pointIClose = 0;
00203
00204
00205
00206 const labelList& facePoints = mesh.faces()[faceIndex];
00207
00208 const scalar faceArea2 = magSqr(mesh.faceAreas()[faceIndex]);
00209
00210 label pointI = 1;
00211 while ((pointI + 1) < facePoints.size())
00212 {
00213
00214 const vector& P1 = mesh.points()[facePoints[0]];
00215 const vector& P2 = mesh.points()[facePoints[pointI]];
00216 const vector& P3 = mesh.points()[facePoints[pointI + 1]];
00217
00218
00219 vector v1 = position - P1;
00220 const vector v2 = P2 - P1;
00221 const vector v3 = P3 - P1;
00222
00223
00224 vector n = v2 ^ v3;
00225 n /= mag(n);
00226
00227
00228 v1 -= (n & v1)*v1;
00229
00230
00231 const scalar d12 = v1 & v2;
00232 const scalar d13 = v1 & v3;
00233 const scalar d22 = v2 & v2;
00234 const scalar d23 = v2 & v3;
00235 const scalar d33 = v3 & v3;
00236
00237
00238
00239 const scalar detA = d22*d33 - d23*d23;
00240
00241 if (0.25*detA/faceArea2 > tol)
00242 {
00243
00244 const scalar u = (d12*d33 - d23*d13)/detA;
00245 const scalar v = (d22*d13 - d12*d23)/detA;
00246
00247
00248 if ((u + tol > 0) && (v + tol > 0) && (u + v < 1 + tol))
00249 {
00250
00251 faceVertices_[0] = facePoints[0];
00252 faceVertices_[1] = facePoints[pointI];
00253 faceVertices_[2] = facePoints[pointI + 1];
00254
00255 weights_[0] = u;
00256 weights_[1] = v;
00257 weights_[2] = 1.0 - (u + v);
00258 weights_[3] = 0.0;
00259
00260 return;
00261 }
00262 else
00263 {
00264 scalar minU = mag(u);
00265 scalar minV = mag(v);
00266 if (minU > 1.0)
00267 {
00268 minU -= 1.0;
00269 }
00270 if (minV > 1.0)
00271 {
00272 minV -= 1.0;
00273 }
00274 const scalar minUV = mag(minU + minV);
00275
00276 if (minUV < minUVClose)
00277 {
00278 minUVClose = minUV;
00279 pointIClose = pointI;
00280 }
00281 }
00282 }
00283
00284 pointI++;
00285 }
00286
00287 if (debug)
00288 {
00289 Pout<< "Foam::cellPointWeight::findTriangle"
00290 << "Triangle search failed; using closest triangle to point" << nl
00291 << " cell face: " << faceIndex << nl << endl;
00292 }
00293
00294
00295 faceVertices_[0] = facePoints[0];
00296 faceVertices_[1] = facePoints[pointIClose];
00297 faceVertices_[2] = facePoints[pointIClose + 1];
00298
00299 weights_[0] = 1.0/3.0;
00300 weights_[1] = 1.0/3.0;
00301 weights_[2] = 1.0/3.0;
00302 weights_[3] = 0.0;
00303 }
00304
00305
00306
00307
00308 Foam::cellPointWeight::cellPointWeight
00309 (
00310 const polyMesh& mesh,
00311 const vector& position,
00312 const label cellIndex,
00313 const label faceIndex
00314 )
00315 :
00316 cellIndex_(cellIndex)
00317 {
00318 if (faceIndex < 0)
00319 {
00320
00321 findTetrahedron(mesh, position, cellIndex);
00322 }
00323 else
00324 {
00325
00326 findTriangle(mesh, position, faceIndex);
00327 }
00328 }
00329
00330
00331
Copyright © 2000-2009 OpenCFD Ltd