Library of Assembled Shared Sources
xyzw.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::XYZW
46 * @brief cyclic iterator over xyzw indices
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_XYZW_H
51#define LASS_GUARDIAN_OF_INCLUSION_PRIM_XYZW_H
52
53#include "prim_common.h"
54#include "../num/modulo.h"
55
56namespace lass
57{
58namespace prim
59{
60
61class XYZW
62{
63public:
64
65 static constexpr size_t dimension = 4;
66
67 /** intializes iterator to @a x axis.
68 */
69 constexpr XYZW(): value_(0) {}
70
71 /** initializes iterator to an axis by character: 'x', 'y', 'z' or 'w'.
72 */
73 constexpr XYZW(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 case 3:
96 case 'w':
97 case 'W':
98 value_ = 3;
99 break;
100
101 default:
102 throw util::Exception("Invalid parameter axis. Try 'x', 'X', 'y', 'Y', 'z', 'Z', 'w' or 'W'.", LASS_PRETTY_FUNCTION);
103 }
104 }
105
106 /** initializes iterator to an axis by number.
107 * ..., -1 == @a w, 0 == @a x, 1 == @a y, 2 == @a z, 3 == @a w, 4 == @a x...
108 */
109 explicit constexpr XYZW(int value): value_(value) {}
110
111 /** initializes iterator to an axis by character: "x", "y", "z" or "w".
112 */
113 explicit XYZW(const std::string& axis)
114 {
115 if (axis.length() != 1)
116 {
117 LASS_THROW("Invalid parameter axis '" << axis << "'. It must be a single character.");
118 }
119 *this = XYZW(axis[0]);
120 }
121
122 constexpr XYZW(const XYZW& other) = default;
123 constexpr XYZW& operator=(const XYZW& other) = default;
124
125 /** return axis by character: 'x', 'y', 'z' or 'w'.
126 */
127 constexpr char axis() const
128 {
129 LASS_ASSERT(value_ >= 0 && value_ < static_cast<int>(dimension));
130 constexpr char axes[] = { 'x', 'y', 'z', 'w' };
131 return axes[value_];
132 }
133
134 constexpr operator int() const { return value_; } /**< convert axis to integer. 0 == @a x, 1 == @a y, 2 == @a z, 3 == @a w */
135 constexpr operator size_t() const { return static_cast<size_t>(value_); } /**< convert axis to integer. 0 == @a x, 1 == @a y, 2 == @a z, 3 == @a w */
136
137 constexpr XYZW& operator++()
138 {
139 ++value_;
140 return *this;
141 }
142 constexpr XYZW& operator--()
143 {
144 --value_;
145 return *this;
146 }
147
148 constexpr XYZW operator++(int)
149 {
150 const XYZW result(*this);
151 ++*this;
152 return result;
153 }
154 constexpr XYZW operator--(int)
155 {
156 const XYZW result(*this);
157 --*this;
158 return result;
159 }
160
161 constexpr XYZW& operator+=(int offset)
162 {
163 value_ += offset;
164 return *this;
165 }
166 constexpr XYZW& operator-=(int offset)
167 {
168 value_ -= offset;
169 return *this;
170 }
171
172 constexpr XYZW operator+(int offset) const
173 {
174 return XYZW(value_ + offset);
175 }
176 constexpr XYZW operator-(int offset) const
177 {
178 return XYZW(value_ - offset);
179 }
180
181 constexpr bool operator==(XYZW other) const
182 {
183 return value_ == other.value_;
184 }
185 constexpr bool operator==(int other) const
186 {
187 return static_cast<int>(value_) == other;
188 }
189 constexpr bool operator==(size_t other) const
190 {
191 return static_cast<size_t>(value_) == other;
192 }
193 constexpr bool operator==(char other) const
194 {
195 return *this == XYZW(other);
196 }
197 bool operator==(const std::string& other) const
198 {
199 return *this == XYZW(other);
200 }
201
202 constexpr bool operator!=(XYZW other) const
203 {
204 return value_ != other.value_;
205 }
206 constexpr bool operator!=(int other) const
207 {
208 return static_cast<int>(value_) != other;
209 }
210 constexpr bool operator!=(size_t other) const
211 {
212 return static_cast<size_t>(value_) != other;
213 }
214 constexpr bool operator!=(char other) const
215 {
216 return *this != XYZW(other);
217 }
218 bool operator!=(const std::string& other) const
219 {
220 return *this != XYZW(other);
221 }
222
223private:
224
225 typedef num::Modulo<dimension, int> TValue;
226
227 TValue value_;
228};
229
230/** @relates lass::prim::XYZW
231 */
232inline constexpr bool operator==(int a, const XYZW& b)
233{
234 return b == a;
235}
236
237/** @relates lass::prim::XYZW
238 */
239inline constexpr bool operator==(size_t a, const XYZW& b)
240{
241 return b == a;
242}
243
244/** @relates lass::prim::XYZW
245 */
246inline constexpr bool operator==(char a, const XYZW& b)
247{
248 return b == a;
249}
250
251/** @relates lass::prim::XYZW
252 */
253inline bool operator==(const std::string& a, const XYZW& b)
254{
255 return b == a;
256}
257
258/** @relates lass::prim::XYZW
259 */
260inline constexpr bool operator!=(int a, const XYZW& b)
261{
262 return b != a;
263}
264
265/** @relates lass::prim::XYZW
266 */
267inline constexpr bool operator!=(size_t a, const XYZW& b)
268{
269 return b != a;
270}
271
272/** @relates lass::prim::XYZW
273 */
274inline constexpr bool operator!=(char a, const XYZW& b)
275{
276 return b != a;
277}
278
279/** @relates lass::prim::XYZW
280 */
281inline bool operator!=(const std::string& a, const XYZW& b)
282{
283 return b != a;
284}
285
286/** @relates lass::prim::XYZW
287 */
288template <typename Char, typename Traits>
289std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const XYZW& xyzw)
290{
291 stream << xyzw.axis();
292 return stream;
293}
294
295
296
297}
298
299}
300
301#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_XYZW
302#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
304#endif
305
306#endif
307
308// EOF
cyclic iterator over xyzw indices
Definition xyzw.h:62
constexpr XYZW()
intializes iterator to x axis.
Definition xyzw.h:69
constexpr char axis() const
return axis by character: 'x', 'y', 'z' or 'w'.
Definition xyzw.h:127
XYZW(const std::string &axis)
initializes iterator to an axis by character: "x", "y", "z" or "w".
Definition xyzw.h:113
constexpr XYZW(char axis)
initializes iterator to an axis by character: 'x', 'y', 'z' or 'w'.
Definition xyzw.h:73
constexpr XYZW(int value)
initializes iterator to an axis by number.
Definition xyzw.h:109
type of all exceptions in lass
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53