primitiveMeshFindCell.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 "primitiveMesh.H"
00028 #include "cell.H"
00029
00030
00031
00032
00033
00034 bool Foam::primitiveMesh::pointInCellBB(const point& p, label celli) const
00035 {
00036 const pointField& points = this->points();
00037 const faceList& f = faces();
00038 const vectorField& centres = cellCentres();
00039 const cellList& cf = cells();
00040
00041 labelList cellVertices = cf[celli].labels(f);
00042
00043 vector bbmax = -GREAT*vector::one;
00044 vector bbmin = GREAT*vector::one;
00045
00046 forAll (cellVertices, vertexI)
00047 {
00048 bbmax = max(bbmax, points[cellVertices[vertexI]]);
00049 bbmin = min(bbmin, points[cellVertices[vertexI]]);
00050 }
00051
00052 scalar distance = mag(centres[celli] - p);
00053
00054 if ((distance - mag(bbmax - bbmin)) < SMALL)
00055 {
00056 return true;
00057 }
00058 else
00059 {
00060 return false;
00061 }
00062 }
00063
00064
00065
00066 bool Foam::primitiveMesh::pointInCell(const point& p, label celli) const
00067 {
00068 const labelList& f = cells()[celli];
00069 const labelList& owner = this->faceOwner();
00070 const vectorField& cf = faceCentres();
00071 const vectorField& Sf = faceAreas();
00072
00073 bool inCell = true;
00074
00075 forAll(f, facei)
00076 {
00077 label nFace = f[facei];
00078 vector proj = p - cf[nFace];
00079 vector normal = Sf[nFace];
00080 if (owner[nFace] != celli)
00081 {
00082 normal = -normal;
00083 }
00084 inCell = inCell && ((normal & proj) <= 0);
00085 }
00086
00087 return inCell;
00088 }
00089
00090
00091
00092 Foam::label Foam::primitiveMesh::findNearestCell(const point& location) const
00093 {
00094 const vectorField& centres = cellCentres();
00095
00096 label nearestCelli = 0;
00097 scalar minProximity = magSqr(centres[0] - location);
00098
00099 for (label celli = 1; celli < centres.size(); celli++)
00100 {
00101 scalar proximity = magSqr(centres[celli] - location);
00102
00103 if (proximity < minProximity)
00104 {
00105 nearestCelli = celli;
00106 minProximity = proximity;
00107 }
00108 }
00109
00110 return nearestCelli;
00111 }
00112
00113
00114
00115 Foam::label Foam::primitiveMesh::findCell(const point& location) const
00116 {
00117 if (nCells() == 0)
00118 {
00119 return -1;
00120 }
00121
00122
00123 label celli = findNearestCell(location);
00124
00125
00126 if (pointInCell(location, celli))
00127 {
00128 return celli;
00129 }
00130 else
00131 {
00132 bool cellFound = false;
00133 label n = 0;
00134
00135 while ((!cellFound) && (n < nCells()))
00136 {
00137 if (pointInCell(location, n))
00138 {
00139 cellFound = true;
00140 celli = n;
00141 }
00142 else
00143 {
00144 n++;
00145 }
00146 }
00147 if (cellFound)
00148 {
00149 return celli;
00150 }
00151 else
00152 {
00153 return -1;
00154 }
00155 }
00156 }
00157
00158
00159
Copyright © 2000-2009 OpenCFD Ltd