Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / meshTools / octree / octreeDataCell.C
blobd235ed7a06c44a44dcc3a9725bdfee86f41a695d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "octreeDataCell.H"
27 #include "polyMesh.H"
28 #include "primitiveMesh.H"
29 #include "treeNode.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 // Construct from components
34 Foam::octreeDataCell::octreeDataCell
36     const polyMesh& mesh,
37     const labelList& cellLabels,
38     const treeBoundBoxList& bbs
41     mesh_(mesh),
42     cellLabels_(cellLabels),
43     bbs_(bbs)
47 // Construct from mesh (assumes all cells)
48 Foam::octreeDataCell::octreeDataCell
50     const polyMesh& mesh
53     mesh_(mesh),
54     cellLabels_(mesh_.nCells()),
55     bbs_
56     (
57         mesh_.nCells(),
58         treeBoundBox::invertedBox
59     )
61     // Set one-one indexing
62     for (label i=0; i < mesh_.nCells(); i++)
63     {
64         cellLabels_[i] = i;
65     }
67     const pointField& points = mesh_.points();
68     const faceList& faces = mesh_.faces();
69     const cellList& cells = mesh_.cells();
71     forAll(cells, celli)
72     {
73         const labelList& facesi = cells[celli];
75         forAll(facesi, facei)
76         {
77             const labelList& pointsi = faces[facesi[facei]];
79             forAll(pointsi, pointi)
80             {
81                 const point& p = points[pointsi[pointi]];
83                 bbs_[celli].min() = min(bbs_[celli].min(), p);
84                 bbs_[celli].max() = max(bbs_[celli].max(), p);
85             }
86         }
87     }
91 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
93 Foam::label Foam::octreeDataCell::getSampleType
95     octree<octreeDataCell>&,
96     const point&
97 ) const
99     return octree<octreeDataCell>::UNKNOWN;
103 bool Foam::octreeDataCell::overlaps
105     const label index,
106     const treeBoundBox& cubeBb
107 ) const
109     return cubeBb.overlaps(bbs_[index]);
113 bool Foam::octreeDataCell::contains
115     const label index,
116     const point& sample
117 ) const
119     return mesh_.pointInCell(sample, cellLabels_[index]);
123 bool Foam::octreeDataCell::intersects
125     const label,
126     const point&,
127     const point&,
128     point&
129 ) const
131     //Hack: don't know what to do here.
133     notImplemented
134     (
135         "octreeDataCell::intersects(const label, const point&,"
136         "const point&, point&)"
137     );
139     return false;
143 bool Foam::octreeDataCell::findTightest
145     const label index,
146     const point& sample,
147     treeBoundBox& tightest
148 ) const
151     // get nearest and furthest away vertex
152     point myNear, myFar;
153     bbs_[index].calcExtremities(sample, myNear, myFar);
155     const point dist = myFar - sample;
156     scalar myFarDist = mag(dist);
158     point tightestNear, tightestFar;
159     tightest.calcExtremities(sample, tightestNear, tightestFar);
161     scalar tightestFarDist = mag(tightestFar - sample);
163     if (tightestFarDist < myFarDist)
164     {
165         // Keep current tightest.
166         return false;
167     }
168     else
169     {
170         // Construct bb around sample and myFar
171         const point dist2(fabs(dist.x()), fabs(dist.y()), fabs(dist.z()));
173         tightest.min() = sample - dist2;
174         tightest.max() = sample + dist2;
176         return true;
177     }
181 // Determine numerical value of sign of sample compared to shape at index
182 Foam::scalar Foam::octreeDataCell::calcSign
184     const label,
185     const point&,
186     vector& n
187 ) const
189     n = vector::zero;
191     return GREAT;
195 // Calculate nearest point on/in shapei
196 Foam::scalar Foam::octreeDataCell::calcNearest
198     const label index,
199     const point& sample,
200     point& nearest
201 ) const
203     nearest = mesh_.cellCentres()[cellLabels_[index]];
205     return mag(nearest - sample);
209 // Calculate nearest point on/in shapei
210 Foam::scalar Foam::octreeDataCell::calcNearest
212     const label index,
213     const linePointRef& ln,
214     point& linePt,
215     point& shapePt
216 ) const
218     notImplemented
219     (
220         "octreeDataCell::calcNearest(const label, const linePointRef&"
221         ", point& linePt, point&)"
222     );
223     return GREAT;
227 void Foam::octreeDataCell::write
229     Ostream& os,
230     const label index
231 ) const
233     os << cellLabels_[index] << " " << bbs_[index];
237 // ************************************************************************* //