PLaSK library

Loading...
Searching...
No Matches
vec.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) 2022 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__VEC_H
15#define PLASK__VEC_H
16
17#include "vector/2d.hpp"
18#include "vector/3d.hpp"
19
20 namespace plask {
21
27 template <int dim, typename T>
28 inline double abs2(const Vec<dim,T>& v) { return dot(v,v); }
29
30#ifndef DOXYGEN
31
32 template <>
33 inline double abs2<2,dcomplex>(const Vec<2,dcomplex>& v) { return dot(v,v).real(); }
34
35 template <>
36 inline double abs2<3,dcomplex>(const Vec<3,dcomplex>& v) { return dot(v,v).real(); }
37
38#endif // DOXYGEN
39
46 template <int dim, typename T, typename OtherT>
47 inline constexpr auto operator*(const OtherT& scale, const Vec<dim,T>& v) -> decltype(v*scale) {
48 return v * scale;
49}
50
51
57 template <int dim, typename T>
58 inline double abs(const Vec<dim,T>& v) { return sqrt(abs2<dim,T>(v)); }
59
60 template <>
61 inline double abs<2, double>(const Vec<2,double>& v) { return std::hypot(v.c0, v.c1); }
62
68 template <int dim, typename T>
69 bool isnan(const Vec<dim,T>& v) {
70 for (int i = 0; i < dim; ++i) if (isnan(v[i])) return true;
71 return false;
72}
73
74 namespace details {
75
76 //construct vector in dim space
77 template <int dim, typename T>
78 struct VecDimConverter {};
79
80 template <typename T>
81 struct VecDimConverter<2, T> {
82 static Vec<2,T>& get(const Vec<2,T>& src) { return src; }
83 static Vec<2,T> get(const Vec<3,T>& src) { return Vec<2, T>(src.tran(), src.vert()); }
84 };
85
86 template <typename T>
87 struct VecDimConverter<3, T> {
88 static Vec<3,T> get(const Vec<2,T>& src) { return Vec<3, T>(T(0.0), src.tran(), src.vert()); }
89 static Vec<3,T> get(const Vec<3,T>& src) { return src; }
90 };
91}
92
93
99 template <int dst_dim, typename T, int src_dim>
100 inline Vec<dst_dim, T> vec(const Vec<src_dim, T>& src) {
101 return details::VecDimConverter<dst_dim, T>::get(src);
102}
103
110 template <typename T>
111 inline Vec<3, T> vec(const Vec<2, T>& src, T lon) {
112 return Vec<3, T>(lon, src.tran(), src.vert());
113}
114
120 inline Vec<2, double> rotateToLonTranAbs(const Vec<3, double>& v) {
121 return vec(std::hypot(v.lon(), v.tran()), v.vert());
122}
123
129 inline Vec<2, double> rotateToLonTranSgn(const Vec<3, double>& v) {
130 return vec(std::copysign(std::hypot(v.lon(), v.tran()), v.tran()), v.vert());
131}
132
141 template <typename T>
142 inline Vec<3, T> vec3Dplus2D(const Vec<3, T>& v3, const Vec<2, T>& v2, T lon) {
143 return Vec<3, T>(v3.lon() + lon, v3.tran() + v2.tran(), v3.vert() + v2.vert());
144}
145
153 template <typename T>
154 inline Vec<3, T> vec3Dplus2D(const Vec<3, T>& v3, const Vec<2, T>& v2) {
155 return Vec<3, T>(v3.lon(), v3.tran() + v2.tran(), v3.vert() + v2.vert());
156}
157
158}
159
160#endif // PLASK__VEC_H