ijkAddressingI.H
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) 2019 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// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29
31:
32 sizes_(0, 0, 0)
33{}
34
35
37:
38 sizes_(ijk)
39{
40 #ifdef FULLDEBUG
41 checkSizes();
42 #endif
43}
44
45
47(
48 const label ni,
49 const label nj,
50 const label nk
51)
52:
53 sizes_(ni, nj, nk)
54{
55 #ifdef FULLDEBUG
56 checkSizes();
57 #endif
58}
59
60
61// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
62
63inline bool Foam::ijkAddressing::empty() const
64{
65 return (!sizes_.x() || !sizes_.y() || !sizes_.z());
66}
67
68
70{
71 return sizes_;
72}
73
74
76{
77 return sizes_;
78}
79
80
81inline Foam::label Foam::ijkAddressing::size() const
82{
83 // Could also use cmptProduct(sizes_);
84 return (sizes_.x() * sizes_.y() * sizes_.z());
85}
86
87
88inline const Foam::label& Foam::ijkAddressing::size
89(
90 const vector::components cmpt
91) const
92{
93 return sizes_[cmpt];
94}
95
96
98{
99 sizes_ = Zero;
100}
101
102
104(
105 const label ni,
106 const label nj,
107 const label nk
108)
109{
110 sizes_.x() = ni;
111 sizes_.y() = nj;
112 sizes_.z() = nk;
113
114 #ifdef FULLDEBUG
115 checkSizes();
116 #endif
117}
118
119
120inline void Foam::ijkAddressing::reset(const labelVector& newSizes)
121{
122 sizes_ = newSizes;
123
124 #ifdef FULLDEBUG
125 checkSizes();
126 #endif
127}
128
129
131(
132 const label i,
133 const label j,
134 const label k
135) const
136{
137 #ifdef FULLDEBUG
138 checkIndex(i, j, k);
139 #endif
140
141 return (i + (sizes_.x() * (j + (sizes_.y() * k))));
142}
143
144
145inline Foam::label Foam::ijkAddressing::index(const labelVector& ijk) const
146{
147 #ifdef FULLDEBUG
148 checkIndex(ijk);
149 #endif
150
151 return (ijk.x() + (sizes_.x() * (ijk.y() + (sizes_.y() * ijk.z()))));
152}
153
154
156{
157 // No checkIndex
158
159 return labelVector
160 (
161 idx % (sizes_.x()),
162 idx / (sizes_.x()),
163 idx / (sizes_.x() * sizes_.y())
164 );
165}
166
167
169(
170 const label i,
171 const label j,
172 const label k,
173 const bool allowExtra
174) const
175{
176 const label extra = (allowExtra ? 1 : 0);
177
178 if (i < 0 || i >= (sizes_.x() + extra))
179 {
181 << "The i-index " << i
182 << " is out of range [0," << (sizes_.x() + extra) << ']' << nl
183 << abort(FatalError);
184 }
185 if (j < 0 || j >= (sizes_.y() + extra))
186 {
188 << "The j-index " << j
189 << " is out of range [0," << (sizes_.y() + extra) << ']' << nl
190 << abort(FatalError);
191 }
192 if (k < 0 || k >= (sizes_.z() + extra))
193 {
195 << "The k-index " << k
196 << " is out of range [0," << (sizes_.z() + extra) << ']' << nl
197 << abort(FatalError);
198 }
199}
200
201
203(
204 const labelVector& ijk,
205 const bool allowExtra
206) const
207{
208 checkIndex(ijk.x(), ijk.y(), ijk.z(), allowExtra);
209}
210
211
213{
214 if (sizes_.x() < 0)
215 {
217 << "The i-size is negative" << nl
218 << abort(FatalError);
219 }
220 if (sizes_.y() < 0)
221 {
223 << "The j-size is negative" << nl
224 << abort(FatalError);
225 }
226 if (sizes_.z() < 0)
227 {
229 << "The k-size is negative" << nl
230 << abort(FatalError);
231 }
232}
233
234
235inline void Foam::ijkAddressing::checkSizes(const labelVector& other) const
236{
237 if (sizes_ != other)
238 {
240 << "The i-j-k sizes are different. "
241 << sizes_ << " vs. " << other << nl
242 << abort(FatalError);
243 }
244}
245
246
247inline void Foam::ijkAddressing::checkSizes(const label nTotal) const
248{
249 if (size() != nTotal)
250 {
252 << "The total size is different. "
253 << size() << " vs. " << nTotal << nl
254 << abort(FatalError);
255 }
256}
257
258
259// ************************************************************************* //
label k
const Cmpt & z() const
Access to the vector z component.
Definition: VectorI.H:85
const Cmpt & y() const
Access to the vector y component.
Definition: VectorI.H:79
const Cmpt & x() const
Access to the vector x component.
Definition: VectorI.H:73
void reset()
Reset to defaults.
Extracts the components of elements of a field and outputs the result into new fields,...
Definition: components.H:188
void checkIndex(const label i, const label j, const label k, const bool allowExtra=false) const
Check indices are within ni,nj,nk range.
ijkAddressing()
Construct zero-size addressing.
label size() const
Return the total i*j*k size.
const labelVector & sizes() const
The (i,j,k) addressing dimensions.
bool empty() const
Addressing is considered empty if any component is zero.
void checkSizes() const
Check that all components of sizes() are non-negative.
void clear()
Reset to (0,0,0) sizing.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Vector< label > labelVector
Vector of labels.
Definition: labelVector.H:51
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53