lineRefinement.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "lineRefinement.H"
28 #include "boundBox.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
41 :
43  p0_(),
44  p1_()
45 {}
46 
48 (
49  const word& name,
50  const scalar cellSize,
51  const direction additionalRefLevels,
52  const point& p0,
53  const point& p1
54 )
55 :
57  p0_(p0),
58  p1_(p1)
59 {
60  setName(name);
61  setCellSize(cellSize);
62  setAdditionalRefinementLevels(additionalRefLevels);
63 }
64 
66 (
67  const word& name,
68  const dictionary& dict
69 )
70 :
72 {
73  this->operator=(dict);
74 }
75 
76 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
77 
79 {
80  //- check if the cube contains start point or end point
81  const scalar l = bb.max().x() - bb.min().x();
82 
83  const point min = bb.min() - l * vector(SMALL, SMALL, SMALL);
84  const point max = bb.max() + l * vector(SMALL, SMALL, SMALL);
85 
86  //- check for intersections of line with the cube faces
87  const vector v(p1_ - p0_);
88  if( mag(v.x()) > SMALL )
89  {
90  if(
91  ((p0_.x() < min.x()) && (p1_.x() < min.x())) ||
92  ((p0_.x() > max.x()) && (p1_.x() > max.x()))
93  )
94  return false;
95 
96  {
97  //- x-min face
98  const vector n(-1, 0, 0);
99  const scalar t = (n & (min - p0_)) / (n & v);
100  const point i = p0_ + t * v;
101  if(
102  (t > -SMALL) && (t < (1.0+SMALL))
103  )
104  if(
105  (i.y() > min.y()) && (i.y() < max.y()) &&
106  (i.z() > min.z()) && (i.z() < max.z())
107  )
108  return true;
109  }
110  {
111  //- x-max face
112  const vector n(1, 0, 0);
113  const scalar t = (n & (max - p0_)) / (n & v);
114  const point i = p0_ + t * v;
115  if(
116  (t > -SMALL) && (t < (1.0+SMALL))
117  )
118  if(
119  (i.y() > min.y()) && (i.y() < max.y()) &&
120  (i.z() > min.z()) && (i.z() < max.z())
121  )
122  return true;
123  }
124  }
125 
126  if( mag(v.y()) > SMALL)
127  {
128  if(
129  ((p0_.y() < min.y()) && (p1_.y() < min.y())) ||
130  ((p0_.y() > max.y()) && (p1_.y() > max.y()))
131  )
132  return false;
133 
134  {
135  //- y-min face
136  const vector n(0, -1, 0);
137  const scalar t = (n & (min - p0_)) / (n & v);
138  const point i = p0_ + t * v;
139  if(
140  (t > -SMALL) && (t < (1.0+SMALL))
141  )
142  if(
143  (i.x() > min.x()) && (i.x() < max.x()) &&
144  (i.z() > min.z()) && (i.z() < max.z())
145  )
146  return true;
147  }
148  {
149  //- y-max face
150  const vector n(0, 1, 0);
151  const scalar t = (n & (max - p0_)) / (n & v);
152  const point i = p0_ + t * v;
153  if(
154  (t > -SMALL) && (t < (1.0+SMALL))
155  )
156  if(
157  (i.x() > min.x()) && (i.x() < max.x()) &&
158  (i.z() > min.z()) && (i.z() < max.z())
159  )
160  return true;
161  }
162  }
163  if( mag(v.z()) > SMALL )
164  {
165  if(
166  ((p0_.z() < min.z()) && (p1_.z() < min.z())) ||
167  ((p0_.z() > max.z()) && (p1_.z() > max.z()))
168  )
169  return false;
170 
171  {
172  //- z-min face
173  const vector n(0, 0, -1);
174  const scalar t = (n & (min - p0_)) / (n & v);
175  const point i = p0_ + t * v;
176  if(
177  (t > -SMALL) && (t < (1.0+SMALL)) &&
178  (i.x() > min.x()) && (i.x() < max.x()) &&
179  (i.y() > min.y()) && (i.y() < max.y())
180  )
181  return true;
182  }
183  {
184  //- z-min face
185  const vector n(0, 0, 1);
186  const scalar t = (n & (max - p0_)) / (n & v);
187  const point i = p0_ + t * v;
188  if(
189  (t > -SMALL) && (t < (1.0+SMALL)) &&
190  (i.x() > min.x()) && (i.x() < max.x()) &&
191  (i.y() > min.y()) && (i.y() < max.y())
192  )
193  return true;
194  }
195  }
196 
197  if(
198  (p0_.x() > min.x()) && (p0_.x() < max.x()) &&
199  (p0_.y() > min.y()) && (p0_.y() < max.y()) &&
200  (p0_.z() > min.z()) && (p0_.z() < max.z())
201  )
202  return true;
203 
204  return false;
205 }
206 
207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 
209 dictionary lineRefinement::dict(bool /*ignoreType*/) const
210 {
212 
213  if( additionalRefinementLevels() == 0 && cellSize() >= 0.0 )
214  {
215  dict.add("cellSize", cellSize());
216  }
217  else
218  {
219  dict.add("additionalRefinementLevels", additionalRefinementLevels());
220  }
221 
222  dict.add("type", type());
223 
224  dict.add("p0", p0_);
225  dict.add("p1", p1_);
226 
227  return dict;
228 }
229 
231 {
232  os << " type: " << type()
233  << " p0: " << p0_
234  << " p1: " << p1_;
235 }
236 
237 void lineRefinement::writeDict(Ostream& os, bool subDict) const
238 {
239  if( subDict )
240  {
241  os << indent << token::BEGIN_BLOCK << incrIndent << nl;
242  }
243 
244  if( additionalRefinementLevels() == 0 && cellSize() >= 0.0 )
245  {
246  os.writeKeyword("cellSize") << cellSize() << token::END_STATEMENT << nl;
247  }
248  else
249  {
250  os.writeKeyword("additionalRefinementLevels")
252  << token::END_STATEMENT << nl;
253  }
254 
255  // only write type for derived types
256  if( type() != typeName_() )
257  {
258  os.writeKeyword("type") << type() << token::END_STATEMENT << nl;
259  }
260 
261  os.writeKeyword("p0") << p0_ << token::END_STATEMENT << nl;
262  os.writeKeyword("p1") << p1_ << token::END_STATEMENT << nl;
263 
264  if( subDict )
265  {
266  os << decrIndent << indent << token::END_BLOCK << endl;
267  }
268 }
269 
271 {
272  // allow as embedded sub-dictionary "coordinateSystem"
273  const dictionary& dict =
274  (
275  d.found(typeName_())
276  ? d.subDict(typeName_())
277  : d
278  );
279 
280  // unspecified centre is (0 0 0)
281  if( dict.found("p0") )
282  {
283  dict.lookup("p0") >> p0_;
284  }
285  else
286  {
288  (
289  "void lineRefinement::operator=(const dictionary& d)"
290  ) << "Entry p0 is not specified!" << exit(FatalError);
291  p0_ = vector::zero;
292  }
293 
294  // specify radius
295  if( dict.found("p1") )
296  {
297  dict.lookup("p1") >> p1_;
298  }
299  else
300  {
302  (
303  "void lineRefinement::operator=(const dictionary& d)"
304  ) << "Entry p1 is not specified!" << exit(FatalError);
305  p1_ = vector::zero;
306  }
307 }
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
312 {
313  os << "name " << name() << nl;
314  os << "cell size " << cellSize() << nl;
315  os << "additionalRefinementLevels " << additionalRefinementLevels() << endl;
316 
317  write(os);
318  return os;
319 }
320 
321 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322 
323 } // End namespace Foam
324 
325 // ************************************************************************* //
Foam::token::END_STATEMENT
@ END_STATEMENT
Definition: token.H:99
Foam::Vector< scalar >::zero
static const Vector zero
Definition: Vector.H:80
Foam::lineRefinement::lineRefinement
lineRefinement()
Null construct.
Definition: lineRefinement.C:40
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::lineRefinement::dict
dictionary dict(bool ignoreType=false) const
Return as dictionary of entries.
Definition: lineRefinement.C:209
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::incrIndent
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:228
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::objectRefinement::cellSize
scalar cellSize() const
return cell size
Definition: objectRefinement.H:80
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::lineRefinement::p0_
point p0_
start point of the line
Definition: lineRefinement.H:56
Foam::objectRefinement
Definition: objectRefinement.H:54
Foam::objectRefinement::additionalRefinementLevels
direction additionalRefinementLevels() const
return the number of additional refinement levels
Definition: objectRefinement.H:157
Foam::lineRefinement::operator=
void operator=(const dictionary &)
assign from dictionary
Definition: lineRefinement.C:270
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
Foam::lineRefinement::p1_
point p1_
end point of the line
Definition: lineRefinement.H:59
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::Vector::x
const Cmpt & x() const
Definition: VectorI.H:65
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::lineRefinement::intersectsObject
bool intersectsObject(const boundBox &) const
check if the line intersects the box
Definition: lineRefinement.C:78
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
Foam::decrIndent
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:235
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::lineRefinement::writeDict
void writeDict(Ostream &, bool subDict=true) const
Write dictionary.
Definition: lineRefinement.C:237
lineRefinement.H
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
boundBox.H
Foam::token::END_BLOCK
@ END_BLOCK
Definition: token.H:105
Foam::Vector< scalar >
Foam::Ostream::writeKeyword
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
Foam::lineRefinement::write
void write(Ostream &) const
Write.
Definition: lineRefinement.C:230
Foam::direction
unsigned char direction
Definition: direction.H:43
Foam::boundBox
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:55
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Definition: token.H:104
Foam::objectRefinement::name
const word & name() const
Return name.
Definition: objectRefinement.H:136
Foam::Vector::y
const Cmpt & y() const
Definition: VectorI.H:71
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
lineRefinement
Refine objects intersected by a line given by two points.
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::lineRefinement::operator<<
Ostream & operator<<(Ostream &) const
Definition: lineRefinement.C:311
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729
objectRefinement
Base class for coordinate systems. All systems are defined by an origin and a coordinate rotation.