Library of Assembled Shared Sources
xyz.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::prim::XYZ
46 * @brief cyclic iterator over xyz indices
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_XYZ_H
51#define LASS_GUARDIAN_OF_INCLUSION_PRIM_XYZ_H
52
53#include "prim_common.h"
54#include "../num/modulo.h"
55
56namespace lass
57{
58namespace prim
59{
60
61class XYZ
62{
63public:
64
65 static constexpr size_t dimension = 3;
66
67 /** intializes iterator to @a x axis.
68 */
69 constexpr XYZ(): value_(0) {}
70
71 /** initializes iterator to an axis by character: 'x', 'y' or 'z'.
72 */
73 constexpr XYZ(char axis)
74 {
75 switch (axis)
76 {
77 case 0:
78 case 'x':
79 case 'X':
80 value_ = 0;
81 break;
82
83 case 1:
84 case 'y':
85 case 'Y':
86 value_ = 1;
87 break;
88
89 case 2:
90 case 'z':
91 case 'Z':
92 value_ = 2;
93 break;
94
95 default:
96 throw util::Exception("Invalid parameter axis. Try 'x', 'X', 'y', 'Y', 'z' or 'Z'.", LASS_PRETTY_FUNCTION);
97 }
98 }
99
100 /** initializes iterator to an axis by number.
101 * ..., -1 == @a z, 0 == @a x, 1 == @a y, 2 == @a z, 3 == @a x, ...
102 */
103 explicit constexpr XYZ(int value): value_(value) {}
104
105 /** initializes iterator to an axis by character: "x", "y" or "z".
106 */
107 explicit XYZ(const std::string& axis)
108 {
109 if (axis.length() != 1)
110 {
111 LASS_THROW("Invalid parameter axis '" << axis << "'. It must be a single character.");
112 }
113 *this = XYZ(axis[0]);
114 }
115
116 constexpr XYZ(const XYZ& other) = default;
117 constexpr XYZ& operator=(const XYZ& other) = default;
118
119 /** return axis by character: 'x', 'y' or 'z'.
120 */
121 constexpr char axis() const
122 {
123 LASS_ASSERT(value_ >= 0 && value_ < static_cast<int>(dimension));
124 constexpr char axes[] = { 'x', 'y', 'z' };
125 return axes[value_];
126 }
127
128 constexpr operator int() const { return value_; } /**< convert axis to integer. 0 == @a x, 1 == @a y, 2 == @a z */
129 constexpr operator size_t() const { return static_cast<size_t>(value_); } /**< convert axis to integer. 0 == @a x, 1 == @a y, 2 == @a z */
130
131 constexpr XYZ& operator++()
132 {
133 ++value_;
134 return *this;
135 }
136 constexpr XYZ& operator--()
137 {
138 --value_;
139 return *this;
140 }
141
142 constexpr XYZ operator++(int)
143 {
144 const XYZ result(*this);
145 ++*this;
146 return result;
147 }
148 constexpr XYZ operator--(int)
149 {
150 const XYZ result(*this);
151 --*this;
152 return result;
153 }
154
155 constexpr XYZ& operator+=(int offset)
156 {
157 value_ += offset;
158 return *this;
159 }
160 constexpr XYZ& operator-=(int offset)
161 {
162 value_ -= offset;
163 return *this;
164 }
165
166 constexpr XYZ operator+(int offset) const
167 {
168 return XYZ(value_ + offset);
169 }
170 constexpr XYZ operator-(int offset) const
171 {
172 return XYZ(value_ - offset);
173 }
174
175 constexpr bool operator==(XYZ other) const
176 {
177 return value_ == other.value_;
178 }
179 constexpr bool operator==(int other) const
180 {
181 return static_cast<int>(value_) == other;
182 }
183 constexpr bool operator==(size_t other) const
184 {
185 return static_cast<size_t>(value_) == other;
186 }
187 constexpr bool operator==(char other) const
188 {
189 return *this == XYZ(other);
190 }
191 bool operator==(const std::string& other) const
192 {
193 return *this == XYZ(other);
194 }
195
196 constexpr bool operator!=(XYZ other) const
197 {
198 return value_ != other.value_;
199 }
200 constexpr bool operator!=(int other) const
201 {
202 return static_cast<int>(value_) != other;
203 }
204 constexpr bool operator!=(size_t other) const
205 {
206 return static_cast<size_t>(value_) != other;
207 }
208 constexpr bool operator!=(char other) const
209 {
210 return *this != XYZ(other);
211 }
212 bool operator!=(const std::string& other) const
213 {
214 return *this != XYZ(other);
215 }
216
217private:
218
219 typedef num::Modulo<dimension, int> TValue;
220
221 TValue value_;
222};
223
224/** @relates lass::prim::XYZ
225 */
226inline constexpr bool operator==(int a, const XYZ& b)
227{
228 return b == a;
229}
230
231/** @relates lass::prim::XYZ
232 */
233inline constexpr bool operator==(size_t a, const XYZ& b)
234{
235 return b == a;
236}
237
238/** @relates lass::prim::XYZ
239 */
240inline constexpr bool operator==(char a, const XYZ& b)
241{
242 return b == a;
243}
244
245/** @relates lass::prim::XYZ
246 */
247inline bool operator==(const std::string& a, const XYZ& b)
248{
249 return b == a;
250}
251
252/** @relates lass::prim::XYZ
253 */
254inline constexpr bool operator!=(int a, const XYZ& b)
255{
256 return b != a;
257}
258
259/** @relates lass::prim::XYZ
260 */
261inline constexpr bool operator!=(size_t a, const XYZ& b)
262{
263 return b != a;
264}
265
266/** @relates lass::prim::XYZ
267 */
268inline constexpr bool operator!=(char a, const XYZ& b)
269{
270 return b != a;
271}
272
273/** @relates lass::prim::XYZ
274 */
275inline bool operator!=(const std::string& a, const XYZ& b)
276{
277 return b != a;
278}
279
280/** @relates lass::prim::XYZ
281 */
282template <typename Char, typename Traits>
283std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const XYZ& xyz)
284{
285 stream << xyz.axis();
286 return stream;
287}
288
289
290
291}
292
293}
294
295#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_XYZ
296#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
298#endif
299
300#endif
301
302// EOF
cyclic iterator over xyz indices
Definition xyz.h:62
XYZ(const std::string &axis)
initializes iterator to an axis by character: "x", "y" or "z".
Definition xyz.h:107
constexpr XYZ(int value)
initializes iterator to an axis by number.
Definition xyz.h:103
constexpr XYZ(char axis)
initializes iterator to an axis by character: 'x', 'y' or 'z'.
Definition xyz.h:73
constexpr char axis() const
return axis by character: 'x', 'y' or 'z'.
Definition xyz.h:121
constexpr XYZ()
intializes iterator to x axis.
Definition xyz.h:69
type of all exceptions in lass
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53