searchableRotatedBox.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2014 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "searchableRotatedBox.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(searchableRotatedBox, 0);
38  (
39  searchableSurface,
40  searchableRotatedBox,
41  dict
42  );
44  (
45  searchableSurface,
46  searchableRotatedBox,
47  dict,
48  rotatedBox
49  );
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
55 Foam::searchableRotatedBox::searchableRotatedBox
56 (
57  const IOobject& io,
58  const vector& span,
59  const coordSystem::cartesian& csys
60 )
61 :
63  box_
64  (
65  IOobject
66  (
67  io.name() + "_box",
68  io.instance(),
69  io.local(),
70  io.db(),
71  io.readOpt(),
72  io.writeOpt(),
73  false // never register
74  ),
75  treeBoundBox(Zero, span)
76  ),
77  transform_(csys.origin(), csys.e3(), csys.e1())
78 {
79  points_ = transform_.globalPosition(box_.points());
80 }
81 
82 
83 Foam::searchableRotatedBox::searchableRotatedBox
84 (
85  const IOobject& io,
86  const dictionary& dict
87 )
88 :
90  (
91  io,
92  dict.get<vector>("span"),
94  (
95  dict.get<point>("origin"),
96  dict.get<vector>("e3"),
97  dict.get<vector>("e1")
98  )
99  )
100 {}
101 
102 
103 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104 
106 {
107  return box_.regions();
108 }
109 
110 
112 {
113  return transform_.globalPosition(box_.coordinates());
114 }
115 
116 
118 (
119  pointField& centres,
120  scalarField& radiusSqr
121 ) const
122 {
123  box_.boundingSpheres(centres, radiusSqr);
124  centres = transform_.globalPosition(centres);
125 }
126 
127 
129 {
130  return points_;
131 }
132 
133 
135 {
136  // (from treeDataPrimitivePatch.C)
137 
138  // 1. bounding box
139  if (!bb.overlaps(bounds()))
140  {
141  return false;
142  }
143 
144  // 2. Check if any corner points inside
145  if (bb.containsAny(points_))
146  {
147  return true;
148  }
149 
150  // 3. Difficult case: all points are outside but connecting edges might
151  // go through cube.
152 
153  const treeBoundBox treeBb(bb);
154 
155  // 3a. my edges through bb faces
156  for (const edge& e : treeBoundBox::edges)
157  {
158  point inter;
159  if (treeBb.intersects(points_[e[0]], points_[e[1]], inter))
160  {
161  return true;
162  }
163  }
164 
165  // 3b. bb edges through my faces
166 
167  const pointField bbPoints(bb.points());
168 
169  for (const face& f : treeBoundBox::faces)
170  {
171  const point fc = f.centre(points_);
172 
173  for (const edge& e : treeBoundBox::edges)
174  {
175  pointHit inter = f.intersection
176  (
177  bbPoints[e[0]],
178  bbPoints[e[1]],
179  fc,
180  points_,
182  );
183 
184  if (inter.hit() && inter.distance() <= 1)
185  {
186  return true;
187  }
188  }
189  }
190 
191  return false;
192 }
193 
194 
196 (
197  const point& sample,
198  const scalar nearestDistSqr
199 ) const
200 {
201  pointIndexHit boxNearest
202  (
203  box_.findNearest
204  (
205  transform_.localPosition(sample),
206  nearestDistSqr
207  )
208  );
209 
210  boxNearest.rawPoint() = transform_.globalPosition(boxNearest.rawPoint());
211 
212  return boxNearest;
213 }
214 
215 
217 (
218  const linePointRef& ln,
219  treeBoundBox& tightest,
220  point& linePoint
221 ) const
222 {
224  return pointIndexHit();
225 }
226 
227 
229 (
230  const point& start,
231  const point& end
232 ) const
233 {
234  pointIndexHit boxHit
235  (
236  box_.findLine
237  (
238  transform_.localPosition(start),
239  transform_.localPosition(end)
240  )
241  );
242 
243  boxHit.rawPoint() = transform_.globalPosition(boxHit.rawPoint());
244 
245  return boxHit;
246 }
247 
248 
250 (
251  const point& start,
252  const point& end
253 ) const
254 {
255  return findLine(start, end);
256 }
257 
258 
260 (
261  const pointField& samples,
262  const scalarField& nearestDistSqr,
263  List<pointIndexHit>& info
264 ) const
265 {
266  info.setSize(samples.size());
267 
268  forAll(samples, i)
269  {
270  info[i] = findNearest(samples[i], nearestDistSqr[i]);
271  }
272 }
273 
274 
276 (
277  const pointField& start,
278  const pointField& end,
279  List<pointIndexHit>& info
280 ) const
281 {
282  info.setSize(start.size());
283 
284  forAll(start, i)
285  {
286  info[i] = findLine(start[i], end[i]);
287  }
288 }
289 
290 
292 (
293  const pointField& start,
294  const pointField& end,
295  List<pointIndexHit>& info
296 ) const
297 {
298  info.setSize(start.size());
299 
300  forAll(start, i)
301  {
302  info[i] = findLineAny(start[i], end[i]);
303  }
304 }
305 
306 
308 (
309  const pointField& start,
310  const pointField& end,
312 ) const
313 {
314  info.setSize(start.size());
315 
316  // Work array
318 
319  // Tolerances:
320  // To find all intersections we add a small vector to the last intersection
321  // This is chosen such that
322  // - it is significant (SMALL is smallest representative relative tolerance;
323  // we need something bigger since we're doing calculations)
324  // - if the start-end vector is zero we still progress
325  const vectorField dirVec(end-start);
326  const scalarField magSqrDirVec(magSqr(dirVec));
327  const vectorField smallVec
328  (
329  Foam::sqrt(SMALL)*dirVec
330  + vector(ROOTVSMALL,ROOTVSMALL,ROOTVSMALL)
331  );
332 
333  forAll(start, pointI)
334  {
335  // See if any intersection between pt and end
336  pointIndexHit inter = findLine(start[pointI], end[pointI]);
337 
338  if (inter.hit())
339  {
340  hits.clear();
341  hits.append(inter);
342 
343  point pt = inter.hitPoint() + smallVec[pointI];
344 
345  while (((pt-start[pointI])&dirVec[pointI]) <= magSqrDirVec[pointI])
346  {
347  // See if any intersection between pt and end
348  pointIndexHit inter = findLine(pt, end[pointI]);
349 
350  // Check for not hit or hit same face as before (can happen
351  // if vector along surface of face)
352  if
353  (
354  !inter.hit()
355  || (inter.index() == hits.last().index())
356  )
357  {
358  break;
359  }
360  hits.append(inter);
361 
362  pt = inter.hitPoint() + smallVec[pointI];
363  }
364 
365  info[pointI].transfer(hits);
366  }
367  else
368  {
369  info[pointI].clear();
370  }
371  }
372 }
373 
374 
376 (
377  const List<pointIndexHit>& info,
378  labelList& region
379 ) const
380 {
381  region.setSize(info.size());
382  region = 0;
383 }
384 
385 
387 (
388  const List<pointIndexHit>& info,
389  vectorField& normal
390 ) const
391 {
392  // searchableBox does not use hitPoints so no need to transform
393  box_.getNormal(info, normal);
394 
395  normal = transform_.globalVector(normal);
396 }
397 
398 
400 (
401  const pointField& points,
402  List<volumeType>& volType
403 ) const
404 {
405  box_.getVolumeType(transform_.localPosition(points), volType);
406 }
407 
408 
409 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::coordinateSystem::e3
virtual const vector e3() const
The local Cartesian z-axis in global coordinates.
Definition: coordinateSystem.H:493
Foam::searchableRotatedBox::regions
virtual const wordList & regions() const
Names of regions.
Definition: searchableRotatedBox.C:105
Foam::intersection::HALF_RAY
Definition: intersection.H:75
Foam::searchableRotatedBox::points
virtual tmp< pointField > points() const
Get the points that define the surface.
Definition: searchableRotatedBox.C:128
Foam::IOobject::instance
const fileName & instance() const noexcept
Definition: IOobjectI.H:196
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::PointHit
Describes the interaction of a face and a point. It carries the info of a successful hit and (if succ...
Definition: PointHit.H:53
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::boundBox::containsAny
bool containsAny(const UList< point > &points) const
Contains any of the points? (inside or on edge)
Definition: boundBox.C:249
Foam::searchableRotatedBox::findLineAny
pointIndexHit findLineAny(const point &start, const point &end) const
Find any intersection of line between start and end.
Definition: searchableRotatedBox.C:250
Foam::treeBoundBox
Standard boundBox with extra functionality for use in octree.
Definition: treeBoundBox.H:86
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:63
Foam::searchableRotatedBox::getRegion
virtual void getRegion(const List< pointIndexHit > &, labelList &region) const
From a set of points and indices get the region.
Definition: searchableRotatedBox.C:376
Foam::PointHit::distance
scalar distance() const noexcept
Return distance to hit.
Definition: PointHit.H:139
Foam::searchableRotatedBox::getNormal
virtual void getNormal(const List< pointIndexHit > &, vectorField &normal) const
From a set of points and indices get the normal.
Definition: searchableRotatedBox.C:387
Foam::treeBoundBox::edges
static const edgeList edges
Edge to point addressing.
Definition: treeBoundBox.H:154
Foam::PointIndexHit::hitPoint
const point_type & hitPoint() const
Return hit point. Fatal if not hit.
Definition: PointIndexHit.H:154
Foam::IOobject::writeOpt
writeOption writeOpt() const noexcept
The write option.
Definition: IOobjectI.H:179
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::searchableRotatedBox::coordinates
virtual tmp< pointField > coordinates() const
Get representative set of element coordinates.
Definition: searchableRotatedBox.C:111
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Foam::boundBox::overlaps
bool overlaps(const boundBox &bb) const
Overlaps/touches boundingBox?
Definition: boundBoxI.H:221
Foam::coordinateSystem::origin
virtual const point & origin() const
Return origin.
Definition: coordinateSystem.H:469
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
Foam::PointIndexHit
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:52
Foam::PointIndexHit::hit
bool hit() const noexcept
Is there a hit?
Definition: PointIndexHit.H:130
Foam::searchableRotatedBox::overlaps
virtual bool overlaps(const boundBox &bb) const
Does any part of the surface overlap the supplied bound box?
Definition: searchableRotatedBox.C:134
Foam::Field< vector >
Foam::searchableRotatedBox::findLineAll
virtual void findLineAll(const pointField &start, const pointField &end, List< List< pointIndexHit >> &) const
Get all intersections in order from start to end.
Definition: searchableRotatedBox.C:308
Foam::treeBoundBox::intersects
bool intersects(const point &overallStart, const vector &overallVec, const point &start, const point &end, point &pt, direction &ptBits) const
Intersects segment; set point to intersection position and face,.
Definition: treeBoundBox.C:162
Foam::IOobject::local
const fileName & local() const noexcept
Definition: IOobjectI.H:208
Foam::treeBoundBox::faces
static const faceList faces
Face to point addressing.
Definition: treeBoundBox.H:151
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
Foam::searchableSurface
Base class of (analytical or triangulated) surface. Encapsulates all the search routines....
Definition: searchableSurface.H:69
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Foam::addNamedToRunTimeSelectionTable
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
Foam::searchableRotatedBox::findNearest
pointIndexHit findNearest(const point &sample, const scalar nearestDistSqr) const
Calculate nearest point on surface.
Definition: searchableRotatedBox.C:196
samples
scalarField samples(nIntervals, Zero)
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::PointIndexHit::rawPoint
const point_type & rawPoint() const noexcept
The point, no checks. Same as point()
Definition: PointIndexHit.H:178
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:51
Foam::searchableRotatedBox::findLine
pointIndexHit findLine(const point &start, const point &end) const
Find nearest intersection of line between start and end.
Definition: searchableRotatedBox.C:229
Foam::IOobject::readOpt
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
Foam::searchableRotatedBox::boundingSpheres
virtual void boundingSpheres(pointField &centres, scalarField &radiusSqr) const
Get bounding spheres (centre and radius squared), one per element.
Definition: searchableRotatedBox.C:118
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::searchableRotatedBox::getVolumeType
virtual void getVolumeType(const pointField &points, List< volumeType > &volType) const
Determine type (inside/outside/mixed) for point. unknown if.
Definition: searchableRotatedBox.C:400
Foam::pointIndexHit
PointIndexHit< point > pointIndexHit
A PointIndexHit for 3D points.
Definition: pointIndexHit.H:46
f
labelList f(nPoints)
Foam::coordSystem::cartesian
A Cartesian coordinate system.
Definition: cartesianCS.H:69
Foam::Vector< scalar >
Foam::searchableRotatedBox
Searching on a rotated box.
Definition: searchableRotatedBox.H:109
Foam::List< word >
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::PointIndexHit::index
label index() const noexcept
Return the hit index.
Definition: PointIndexHit.H:136
Foam::searchableBox::regions
virtual const wordList & regions() const
Names of regions.
Definition: searchableBox.C:242
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::line
A line primitive.
Definition: line.H:53
Foam::boundBox
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
Foam::coordinateSystem::e1
virtual const vector e1() const
The local Cartesian x-axis in global coordinates.
Definition: coordinateSystem.H:481
Foam::ln
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: MSwindows.C:925
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
searchableRotatedBox.H
Foam::boundBox::points
tmp< pointField > points() const
Corner points in an order corresponding to a 'hex' cell.
Definition: boundBox.C:118
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::PointHit::hit
bool hit() const noexcept
Is there a hit.
Definition: PointHit.H:121
sample
Minimal example by using system/controlDict.functions:
Foam::IOobject::db
const objectRegistry & db() const noexcept
Return the local objectRegistry.
Definition: IOobject.C:487