PLaSK library
Loading...
Searching...
No Matches
lateral.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2024 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK_VECTOR_LATERAL_HPP
15#define PLASK_VECTOR_LATERAL_HPP
16
17#include "../math.hpp"
18#include "../vec.hpp"
19
20namespace plask {
21
22template <typename T>
23struct LateralVec {
24
25 static const int DIMS = 2;
26
27 T c0, c1;
28
29 T& lon() { return c0; }
30 constexpr const T& tran() const { return c0; }
31
32 T& tran() { return c1; }
33 constexpr const T& vert() const { return c1; }
34
38 typedef T* iterator;
39
43 typedef const T* const_iterator;
44
47
52 template <typename OtherT>
53 constexpr LateralVec(const LateralVec<OtherT>& p): c0(p.c0), c1(p.c1) {}
54
59 template <int other_dim, typename OtherT>
60 constexpr LateralVec(const Vec<other_dim,OtherT>& p): c0(p.c0), c1(p.c1) {}
61
62
67 constexpr LateralVec(T c0, T c1): c0(c0), c1(c1) {}
68
73 template <typename T0, typename T1>
74 constexpr LateralVec(const std::pair<T0,T1>& comp): c0(comp.first), c1(comp.second) {}
75
81 template <typename InputIteratorType>
84 result.c0 = T(*inputIt);
85 result.c1 = T(*++inputIt);
86 return result;
87 }
88
93 iterator begin() { return &c0; }
94
99 const_iterator begin() const { return &c0; }
100
105 iterator end() { return &c0 + 2; }
106
111 const_iterator end() const { return &c0 + 2; }
112
118 template <typename OtherT>
119 constexpr bool operator==(const LateralVec<OtherT>& p) const { return p.c0 == c0 && p.c1 == c1; }
120
127 template <typename OtherT, typename SuprType>
128 constexpr bool equals(const LateralVec< OtherT>& p, const SuprType& abs_supremum) const {
129 return is_zero(p.c0 - c0, abs_supremum) && is_zero(p.c1 - c1, abs_supremum); }
130
136 template <typename OtherT>
137 constexpr bool equals(const LateralVec< OtherT>& p) const {
138 return is_zero(p.c0 - c0) && is_zero(p.c1 - c1); }
139
145 template <typename OtherT>
146 constexpr bool operator!=(const LateralVec<OtherT>& p) const { return p.c0 != c0 || p.c1 != c1; }
147
154 inline T& operator[](size_t i) {
155 assert(i < 2);
156 return *(&c0 + i);
157 }
158
165 inline const T& operator[](size_t i) const {
166 assert(i < 2);
167 return *(&c0 + i);
168 }
169
175 template <typename OtherT>
176 constexpr auto operator+(const LateralVec<OtherT>& other) const -> LateralVec<decltype(c0 + other.c0)> {
177 return LateralVec<decltype(this->c0 + other.c0)>(c0 + other.c0, c1 + other.c1);
178 }
179
186 c0 += other.c0;
187 c1 += other.c1;
188 return *this;
189 }
190
196 template <typename OtherT>
197 constexpr auto operator-(const LateralVec<OtherT>& other) const -> LateralVec<decltype(c0 - other.c0)> {
198 return LateralVec<decltype(this->c0 - other.c0)>(c0 - other.c0, c1 - other.c1);
199 }
200
207 c0 -= other.c0;
208 c1 -= other.c1;
209 return *this;
210 }
211
217 template <typename OtherT>
218 constexpr auto operator*(const OtherT scale) const -> LateralVec<decltype(c0*scale)> {
220 return LateralVec<decltype(c0*scale)>(c0 * scale, c1 * scale);
222 }
223
230 c0 *= scalar;
231 c1 *= scalar;
232 return *this;
233 }
234
240 constexpr LateralVec<T> operator/(const T scale) const { return LateralVec<T>(c0 / scale, c1 / scale); }
241
248 c0 /= scalar;
249 c1 /= scalar;
250 return *this;
251 }
252
257 constexpr LateralVec<T> operator-() const {
258 return LateralVec<T>(-c0, -c1);
259 }
260
266 return LateralVec<T>(c0*c0, c1*c1);
267 }
268
274 c0 *= c0; c1 *= c1;
275 return *this;
276 }
277
282 template <typename OtherT>
284 return LateralVec<T>(std::pow(c0, a), std::pow(c1, a));
285 }
286
292 inline void flip(size_t i) {
293 assert(i < 2);
294 operator[](i) = -operator[](i);
295 }
296
303 inline LateralVec<T> flipped(size_t i) {
304 LateralVec<T> res = *this;
305 res.flip(i);
306 return res;
307 }
308
315 friend inline std::ostream& operator<<(std::ostream& out, const LateralVec<T>& to_print) {
316 return out << '[' << to_print.c0 << ", " << to_print.c1 << ']';
317 }
318
326 template<class OT> inline
327 bool operator< (LateralVec< OT> const& v) const {
328 if (dbl_compare_lt(this->c0, v.c0)) return true;
329 if (dbl_compare_gt(this->c0, v.c0)) return false;
330 return dbl_compare_lt(this->c1, v.c1);
331 }
332};
333
340template <typename T1, typename T2>
341inline auto dot(const LateralVec<T1>& v1, const LateralVec<T2>& v2) -> decltype(v1.c0*v2.c0) {
342 return ::plask::fma(v1.c0, v2.c0, v1.c1 * v2.c1); //MSVC needs ::plask::
343}
344
350template <typename T1, typename T2>
351inline auto cross(const LateralVec<T1>& v1, const LateralVec<T2>& v2) -> decltype(v1.c0*v2.c1) {
352 return ::plask::fma(v1.c0, v2.c1, - v1.c1 * v2.c0); //MSVC needs ::plask::
353}
354
361template <typename T, typename OtherT>
362inline constexpr auto operator*(const OtherT& scale, const LateralVec<T>& v) -> decltype(v*scale) {
363 return v * scale;
364}
365
366
369
370} // namespace plask
371
372namespace std {
373 template <typename T, typename OtherT>
375 return vec.pow(a);
376 }
377}
378
379#endif