voxelMeshSearchTemplates.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) 2017 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "voxelMeshSearch.H"
29 #include "OBJstream.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Container, class Type>
35 (
36  Container& elems,
37  const boundBox& bb,
38  const labelVector& nDivs,
39  const boundBox& subBb,
40  const Type val
41 )
42 {
43  labelVector minIds(index3(bb, nDivs, subBb.min()));
44  labelVector maxIds(index3(bb, nDivs, subBb.max()));
45 
46  for (direction cmpt = 0; cmpt < 3; cmpt++)
47  {
48  if (maxIds[cmpt] < 0 || minIds[cmpt] >= nDivs[cmpt])
49  {
50  return;
51  }
52  // Clip
53  maxIds[cmpt] = min(maxIds[cmpt], nDivs[cmpt]-1);
54  minIds[cmpt] = max(minIds[cmpt], 0);
55  }
56 
57  const labelVector off(offset(nDivs));
58  label voxeli = index(nDivs, minIds);
59  for (label k = minIds[2]; k <= maxIds[2]; k++)
60  {
61  const label start1 = voxeli;
62  for (label j = minIds[1]; j <= maxIds[1]; j++)
63  {
64  const label start0 = voxeli;
65  for (label i = minIds[0]; i <= maxIds[0]; i++)
66  {
67  elems[voxeli] = val;
68  voxeli += off[0];
69  }
70  voxeli = start0 + off[1];
71  }
72  voxeli = start1 + off[2];
73  }
74 }
75 
76 
77 template<class Container, class Type, class CombineOp>
79 (
80  Container& elems,
81  const boundBox& bb,
82  const labelVector& nDivs,
83  const boundBox& subBb,
84  const Type val,
85  const CombineOp& cop
86 )
87 {
88  labelVector minIds(index3(bb, nDivs, subBb.min()));
89  labelVector maxIds(index3(bb, nDivs, subBb.max()));
90 
91  for (direction cmpt = 0; cmpt < 3; cmpt++)
92  {
93  if (maxIds[cmpt] < 0 || minIds[cmpt] >= nDivs[cmpt])
94  {
95  return;
96  }
97  // Clip
98  maxIds[cmpt] = min(maxIds[cmpt], nDivs[cmpt]-1);
99  minIds[cmpt] = max(minIds[cmpt], 0);
100  }
101 
102  const labelVector off(offset(nDivs));
103  label voxeli = index(nDivs, minIds);
104  for (label k = minIds[2]; k <= maxIds[2]; k++)
105  {
106  const label start1 = voxeli;
107  for (label j = minIds[1]; j <= maxIds[1]; j++)
108  {
109  const label start0 = voxeli;
110  for (label i = minIds[0]; i <= maxIds[0]; i++)
111  {
112  cop(elems[voxeli], val);
113  voxeli += off[0];
114  }
115  voxeli = start0 + off[1];
116  }
117  voxeli = start1 + off[2];
118  }
119 }
120 
121 
122 template<class Container, class Type>
124 (
125  const boundBox& bb,
126  const labelVector& nDivs,
127  const boundBox& subBb,
128  const Container& elems,
129  const Type val,
130  const bool isNot
131 )
132 {
133  // Checks if subBb overlaps any voxel set to val
134 
135  labelVector minIds(index3(bb, nDivs, subBb.min()));
136  labelVector maxIds(index3(bb, nDivs, subBb.max()));
137 
138  for (direction cmpt = 0; cmpt < 3; cmpt++)
139  {
140  if (maxIds[cmpt] < 0 || minIds[cmpt] >= nDivs[cmpt])
141  {
142  return false;
143  }
144  // Clip
145  maxIds[cmpt] = min(maxIds[cmpt], nDivs[cmpt]-1);
146  minIds[cmpt] = max(minIds[cmpt], 0);
147  }
148 
149  if (elems.size() != cmptProduct(nDivs))
150  {
152  << "sizes:" << elems.size() << " and " << nDivs
153  << exit(FatalError);
154  }
155 
156 
157  const labelVector off(offset(nDivs));
158  label voxeli = index(nDivs, minIds);
159  for (label k = minIds[2]; k <= maxIds[2]; k++)
160  {
161  const label start1 = voxeli;
162  for (label j = minIds[1]; j <= maxIds[1]; j++)
163  {
164  const label start0 = voxeli;
165  for (label i = minIds[0]; i <= maxIds[0]; i++)
166  {
167  const Type elemVal = elems[voxeli];
168  if (isNot != (elemVal == val))
169  {
170  return true;
171  }
172  voxeli += off[0];
173  }
174  voxeli = start0 + off[1];
175  }
176  voxeli = start1 + off[2];
177  }
178  return false;
179 }
180 
181 
182 template<class Container, class Type>
184 (
185  OBJstream& os,
186  const boundBox& bb,
187  const labelVector& nDivs,
188  const Container& elems,
189  const Type val,
190  const bool isNot
191 )
192 {
193  if (elems.size() != cmptProduct(nDivs))
194  {
196  << "sizes:" << elems.size() << " and " << nDivs
197  << exit(FatalError);
198  }
199 
200  const labelVector off(offset(nDivs));
201  label voxeli = index(nDivs, labelVector(0, 0, 0));
202  for (label k = 0; k < nDivs[2]; k++)
203  {
204  const label start1 = voxeli;
205  for (label j = 0; j < nDivs[1]; j++)
206  {
207  const label start0 = voxeli;
208  for (label i = 0; i < nDivs[0]; i++)
209  {
210  const Type& elemVal = elems[voxeli];
211  if (isNot != (elemVal == val))
212  {
213  os.write(centre(bb, nDivs, labelVector(i, j, k)));
214  }
215  voxeli += off[0];
216  }
217  voxeli = start0 + off[1];
218  }
219  voxeli = start1 + off[2];
220  }
221 }
222 
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::voxelMeshSearch::write
static void write(OBJstream &, const boundBox &bb, const labelVector &nDivs, const Container &elems, const Type val, const bool isNot=false)
Debug: write points for every set element.
Definition: voxelMeshSearchTemplates.C:184
Foam::OBJstream
OFstream that keeps track of vertices.
Definition: OBJstream.H:58
voxelMeshSearch.H
Foam::cmptProduct
Cmpt cmptProduct(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:596
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:97
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:91
Foam::labelVector
Vector< label > labelVector
Vector of labels.
Definition: labelVector.H:51
Foam::voxelMeshSearch::fill
static void fill(Container &elems, const boundBox &bb, const labelVector &nDivs, const boundBox &subBb, const Type val)
Fill voxels indicated by bounding box.
Definition: voxelMeshSearchTemplates.C:35
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::Vector< label >
Foam::voxelMeshSearch::overlaps
static bool overlaps(const boundBox &bb, const labelVector &nDivs, const boundBox &subBb, const Container &elems, const Type val, const bool isNot=false)
Check if any voxel inside bounding box is set to val or.
Definition: voxelMeshSearchTemplates.C:124
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::boundBox
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
OBJstream.H