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-------------------------------------------------------------------------------
10License
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
33template<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
77template<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
122template<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
182template<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// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
label k
OFstream that keeps track of vertices.
Definition: OBJstream.H:61
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
virtual char fill() const =0
Get padding character.
int overlaps
Flag to control which overlap calculations are performed.
Definition: PDRparams.H:97
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:64
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:91
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:97
virtual bool write()
Write the output fields.
static labelVector index3(const labelVector &nDivs, const label voxeli)
Combined voxel index to individual indices.
static label index(const labelVector &nDivs, const labelVector &voxel)
Find cells. Returns number of cells found.
static labelVector offset(const labelVector &nDivs)
Change in combined voxel index for change in components.
const labelVector & nDivs() const
Number of voxels for local mesh.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Vector< label > labelVector
Vector of labels.
Definition: labelVector.H:51
Cmpt cmptProduct(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:598
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
uint8_t direction
Definition: direction.H:56
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130