FreeFOAM The Cross-Platform CFD Toolkit
rotatedBoxToCell.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "rotatedBoxToCell.H"
27 #include <OpenFOAM/polyMesh.H>
28 #include <OpenFOAM/cellModeller.H>
29 
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 defineTypeNameAndDebug(rotatedBoxToCell, 0);
38 
39 addToRunTimeSelectionTable(topoSetSource, rotatedBoxToCell, word);
40 
41 addToRunTimeSelectionTable(topoSetSource, rotatedBoxToCell, istream);
42 
43 }
44 
45 
46 Foam::topoSetSource::addToUsageTable Foam::rotatedBoxToCell::usage_
47 (
48  rotatedBoxToCell::typeName,
49  "\n Usage: rotatedBoxToCell (originx originy originz)"
50  " (ix iy iz) (jx jy jz) (kx ky kz)\n\n"
51  " Select all cells with cellCentre within parallelopiped\n\n"
52 );
53 
54 
55 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
56 
57 void Foam::rotatedBoxToCell::combine(topoSet& set, const bool add) const
58 {
59  // Define a cell for the box
60  pointField boxPoints(8);
61  boxPoints[0] = origin_;
62  boxPoints[1] = origin_ + i_;
63  boxPoints[2] = origin_ + i_ + j_;
64  boxPoints[3] = origin_ + j_;
65  boxPoints[4] = origin_ + k_;
66  boxPoints[5] = origin_ + k_ + i_;
67  boxPoints[6] = origin_ + k_ + i_ + j_;
68  boxPoints[7] = origin_ + k_ + j_;
69 
70  labelList boxVerts(8);
71  forAll(boxVerts, i)
72  {
73  boxVerts[i] = i;
74  }
75 
76  const cellModel& hex = *(cellModeller::lookup("hex"));
77 
78  // Get outwards pointing faces.
79  faceList boxFaces(cellShape(hex, boxVerts).faces());
80 
81  // Precalculate normals
82  vectorField boxFaceNormals(boxFaces.size());
83  forAll(boxFaces, i)
84  {
85  boxFaceNormals[i] = boxFaces[i].normal(boxPoints);
86 
87  Pout<< "Face:" << i << " position:" << boxFaces[i].centre(boxPoints)
88  << " normal:" << boxFaceNormals[i] << endl;
89  }
90 
91  // Check whether cell centre is inside all faces of box.
92 
93  const pointField& ctrs = mesh_.cellCentres();
94 
95  forAll(ctrs, cellI)
96  {
97  bool inside = true;
98 
99  forAll(boxFaces, i)
100  {
101  const face& f = boxFaces[i];
102 
103  if (((ctrs[cellI] - boxPoints[f[0]]) & boxFaceNormals[i]) > 0)
104  {
105  inside = false;
106  break;
107  }
108  }
109 
110  if (inside)
111  {
112  addOrDelete(set, cellI, add);
113  }
114  }
115 }
116 
117 
118 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
119 
120 // Construct from components
122 (
123  const polyMesh& mesh,
124  const vector& origin,
125  const vector& i,
126  const vector& j,
127  const vector& k
128 )
129 :
130  topoSetSource(mesh),
131  origin_(origin),
132  i_(i),
133  j_(j),
134  k_(k)
135 {}
136 
137 
138 // Construct from dictionary
140 (
141  const polyMesh& mesh,
142  const dictionary& dict
143 )
144 :
145  topoSetSource(mesh),
146  origin_(dict.lookup("origin")),
147  i_(dict.lookup("i")),
148  j_(dict.lookup("j")),
149  k_(dict.lookup("k"))
150 {}
151 
152 
153 // Construct from Istream
155 :
156  topoSetSource(mesh),
157  origin_(is),
158  i_(is),
159  j_(is),
160  k_(is)
161 {}
162 
163 
164 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
165 
167 {}
168 
169 
170 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
171 
173 (
174  const topoSetSource::setAction action,
175  topoSet& set
176 ) const
177 {
178  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
179  {
180  Info<< " Adding cells with center within rotated box " << endl;
181 
182  combine(set, true);
183  }
184  else if (action == topoSetSource::DELETE)
185  {
186  Info<< " Removing cells with center within rotated box " << endl;
187 
188  combine(set, false);
189  }
190 }
191 
192 
193 // ************************ vim: set sw=4 sts=4 et: ************************ //