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-2025 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 // * * * * * * * * * * * * * * * * 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
57  #endif
58 }
59 
60 
61 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
62 
63 inline bool Foam::ijkAddressing::empty() const noexcept
64 {
65  return (!sizes_.x() || !sizes_.y() || !sizes_.z());
66 }
67 
68 
69 inline Foam::label Foam::ijkAddressing::size() const noexcept
70 {
71  // Could also use cmptProduct(sizes_);
72  return (sizes_.x() * sizes_.y() * sizes_.z());
73 }
74 
75 
76 inline Foam::label Foam::ijkAddressing::size
77 (
78  const vector::components cmpt
79 ) const
80 {
81  return sizes_[cmpt];
82 }
83 
84 
86 {
87  sizes_ = Foam::zero{};
88 }
89 
90 
92 (
93  const label ni,
94  const label nj,
95  const label nk
96 )
97 {
98  sizes_.x() = ni;
99  sizes_.y() = nj;
100  sizes_.z() = nk;
102  #ifdef FULLDEBUG
103  checkSizes();
104  #endif
105 }
106 
107 
108 inline void Foam::ijkAddressing::reset(const labelVector& newSizes)
109 {
110  sizes_ = newSizes;
111 
112  #ifdef FULLDEBUG
113  checkSizes();
114  #endif
115 }
116 
117 
118 inline Foam::label Foam::ijkAddressing::index
119 (
120  const label i,
121  const label j,
122  const label k
123 ) const
124 {
125  #ifdef FULLDEBUG
126  checkIndex(i, j, k);
127  #endif
128 
129  return (i + (sizes_.x() * (j + (sizes_.y() * k))));
130 }
131 
132 
133 inline Foam::label Foam::ijkAddressing::index(const labelVector& ijk) const
134 {
135  #ifdef FULLDEBUG
136  checkIndex(ijk);
137  #endif
138 
139  return (ijk.x() + (sizes_.x() * (ijk.y() + (sizes_.y() * ijk.z()))));
140 }
141 
142 
143 inline Foam::labelVector Foam::ijkAddressing::index(const label idx) const
144 {
145  // No checkIndex
146 
147  return labelVector
148  (
149  idx % (sizes_.x()),
150  idx / (sizes_.x()),
151  idx / (sizes_.x() * sizes_.y())
152  );
153 }
154 
155 
157 (
158  const label i,
159  const label j,
160  const label k,
161  const bool allowExtra
162 ) const
163 {
164  const label extra = (allowExtra ? 1 : 0);
165 
166  if (i < 0 || i >= (sizes_.x() + extra))
167  {
169  << "The i-index " << i
170  << " is out of range [0," << (sizes_.x() + extra) << ']' << nl
171  << abort(FatalError);
172  }
173  if (j < 0 || j >= (sizes_.y() + extra))
174  {
176  << "The j-index " << j
177  << " is out of range [0," << (sizes_.y() + extra) << ']' << nl
178  << abort(FatalError);
179  }
180  if (k < 0 || k >= (sizes_.z() + extra))
181  {
183  << "The k-index " << k
184  << " is out of range [0," << (sizes_.z() + extra) << ']' << nl
185  << abort(FatalError);
186  }
187 }
188 
189 
191 (
192  const labelVector& ijk,
193  const bool allowExtra
194 ) const
195 {
196  checkIndex(ijk.x(), ijk.y(), ijk.z(), allowExtra);
197 }
198 
199 
200 inline void Foam::ijkAddressing::checkSizes() const
201 {
202  if (sizes_.x() < 0)
203  {
205  << "The i-size is negative" << nl
206  << abort(FatalError);
207  }
208  if (sizes_.y() < 0)
209  {
211  << "The j-size is negative" << nl
212  << abort(FatalError);
213  }
214  if (sizes_.z() < 0)
215  {
217  << "The k-size is negative" << nl
218  << abort(FatalError);
219  }
220 }
221 
222 
223 inline void Foam::ijkAddressing::checkSizes(const labelVector& other) const
224 {
225  if (sizes_ != other)
226  {
228  << "The i-j-k sizes are different. "
229  << sizes_ << " vs. " << other << nl
230  << abort(FatalError);
231  }
232 }
233 
234 
235 inline void Foam::ijkAddressing::checkSizes(const label nTotal) const
236 {
237  if (size() != nTotal)
238  {
240  << "The total size is different. "
241  << size() << " vs. " << nTotal << nl
242  << abort(FatalError);
243  }
244 }
245 
246 
247 // ************************************************************************* //
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
const Cmpt & y() const noexcept
Access to the vector y component.
Definition: Vector.H:140
label k
Boltzmann constant.
void reset(const label ni, const label nj, const label nk)
Change the sizing parameters.
bool empty() const noexcept
Addressing is considered empty if any component is zero.
void checkSizes() const
Check that all components of sizes() are non-negative.
label index(const label i, const label j, const label k) const
Linear addressing index (offset) for an (i,j,k) position.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
void checkIndex(const label i, const label j, const label k, const bool allowExtra=false) const
Check indices are within ni,nj,nk range.
const direction noexcept
Definition: scalarImpl.H:255
const Cmpt & x() const noexcept
Access to the vector x component.
Definition: Vector.H:135
ijkAddressing()
Construct zero-size addressing.
void clear()
Reset to (0,0,0) sizing.
const Cmpt & z() const noexcept
Access to the vector z component.
Definition: Vector.H:145
label size() const noexcept
Return the total i*j*k size.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
components
Component labeling enumeration.
Definition: Vector.H:83
Vector< label > labelVector
Vector of labels.
Definition: labelVector.H:47