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
20namespace plask {
21
27template <int dim, typename T>
28inline double abs2(const Vec<dim,T>& v) { return dot(v,v); }
29
30#ifndef DOXYGEN
31
32template <>
33inline double abs2<2,dcomplex>(const Vec<2,dcomplex>& v) { return dot(v,v).real(); }
34
35template <>
36inline double abs2<3,dcomplex>(const Vec<3,dcomplex>& v) { return dot(v,v).real(); }
37
38#endif // DOXYGEN
39
46template <int dim, typename T, typename OtherT>
47inline constexpr auto operator*(const OtherT& scale, const Vec<dim,T>& v) -> decltype(v*scale) {
48 return v * scale;
49}
50
51
57template <int dim, typename T>
58inline double abs(const Vec<dim,T>& v) { return sqrt(abs2<dim,T>(v)); }
59
60template <>
61inline double abs<2, double>(const Vec<2,double>& v) { return std::hypot(v.c0, v.c1); }
62
68template <int dim, typename T>
69bool 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
74namespace 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
99template <int dst_dim, typename T, int src_dim>
103
110template <typename T>
111inline Vec<3, T> vec(const Vec<2, T>& src, T lon) {
112 return Vec<3, T>(lon, src.tran(), src.vert());
113}
114
121 return vec(std::hypot(v.lon(), v.tran()), v.vert());
122}
123
130 return vec(std::copysign(std::hypot(v.lon(), v.tran()), v.tran()), v.vert());
131}
132
141template <typename T>
142inline 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
153template <typename T>
155 return Vec<3, T>(v3.lon(), v3.tran() + v2.tran(), v3.vert() + v2.vert());
156}
157
158}
159
160#endif // PLASK__VEC_H