PLaSK library

Loading...
Searching...
No Matches
primitives.cpp
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#include "primitives.hpp"
15
16#include <algorithm>
17
18 namespace plask {
19
20 static inline void ensureLo(double& to_be_lo, double how_lo) {
21 if (how_lo < to_be_lo) to_be_lo = how_lo;
22}
23
24 static inline void ensureHi(double& to_be_hi, double how_hi) {
25 if (how_hi > to_be_hi) to_be_hi = how_hi;
26}
27
28//------------- ---------------------
29
30 bool Box2D::operator ==(const Box2D &r) const {
31 return lower == r.lower && upper == r.upper;
32}
33
34 bool Box2D::operator !=(const Box2D &r) const {
35 return lower != r.lower || upper != r.upper;
36}
37
38 void Box2D::fix() {
39 if (lower.c0 > upper.c0) std::swap(lower.c0, upper.c0);
40 if (lower.c1 > upper.c1) std::swap(lower.c1, upper.c1);
41}
42
43
44 bool Box2D::contains(const Vec<2, double >& p) const {
45 return lower.c0 <= p.c0 && p.c0 <= upper.c0 &&
46 lower.c1 <= p.c1 && p.c1 <= upper.c1;
47}
48
49 bool Box2D::intersects(const plask::Box2D& other) const {
50 return !(
51 lower.c0 > other.upper.c0 ||
52 lower.c1 > other.upper.c1 ||
53 upper.c0 < other.lower.c0 ||
54 upper.c1 < other.lower.c1
55 );
56}
57
58 void Box2D::makeInclude(const Vec<2, double >& p) {
59 if (p.c0 < lower.c0) lower.c0 = p.c0; else ensureHi(upper.c0, p.c0);
60 if (p.c1 < lower.c1) lower.c1 = p.c1; else ensureHi(upper.c1, p.c1);
61}
62
63 void Box2D::makeInclude(const plask::Box2D& other) {
64 ensureLo(lower.c0, other.lower.c0);
65 ensureLo(lower.c1, other.lower.c1);
66 ensureHi(upper.c0, other.upper.c0);
67 ensureHi(upper.c1, other.upper.c1);
68}
69
70 void Box2D::makeIntersection(const Box2D &other) {
71 ensureHi(lower.c0, other.lower.c0);
72 ensureHi(lower.c1, other.lower.c1);
73 ensureLo(upper.c0, other.upper.c0);
74 ensureLo(upper.c1, other.upper.c1);
75}
76
77 Box2D Box2D::intersection(Box2D other) const {
78 other.makeIntersection(*this);
79 return other;
80}
81
82 Box2D Box2D::extension(Box2D other) const {
83 other.makeInclude(*this);
84 return other;
85}
86
87 Vec<2,double> Box2D::moveInside(Vec<2,double> p) const {
88 if (p.c0 < lower.c0) p.c0 = lower.c0; else ensureLo(p.c0, upper.c0);
89 if (p.c1 < lower.c1) p.c1 = lower.c1; else ensureLo(p.c1, upper.c1);
90 return p;
91}
92
93
94//------------- ---------------------
95
96 bool Box3D::operator ==(const Box3D &r) const {
97 return lower == r.lower && upper == r.upper;
98}
99
100 bool Box3D::operator !=(const Box3D &r) const {
101 return lower != r.lower || upper != r.upper;
102}
103
104 void Box3D::fix() {
105 if (lower.c0 > upper.c0) std::swap(lower.c0, upper.c0);
106 if (lower.c1 > upper.c1) std::swap(lower.c1, upper.c1);
107 if (lower.c2 > upper.c2) std::swap(lower.c2, upper.c2);
108}
109
110
111 bool Box3D::contains(const Vec<3, double >& p) const {
112 return lower.c0 <= p.c0 && p.c0 <= upper.c0 &&
113 lower.c1 <= p.c1 && p.c1 <= upper.c1 &&
114 lower.c2 <= p.c2 && p.c2 <= upper.c2;
115}
116
117 bool Box3D::intersects(const plask::Box3D& other) const {
118 return !(
119 lower.c0 > other.upper.c0 ||
120 lower.c1 > other.upper.c1 ||
121 lower.c2 > other.upper.c2 ||
122 upper.c0 < other.lower.c0 ||
123 upper.c1 < other.lower.c1 ||
124 upper.c2 < other.lower.c2
125 );
126}
127
128 void Box3D::makeInclude(const Vec<3, double >& p) {
129 if (p.c0 < lower.c0) lower.c0 = p.c0; else ensureHi(upper.c0, p.c0);
130 if (p.c1 < lower.c1) lower.c1 = p.c1; else ensureHi(upper.c1, p.c1);
131 if (p.c2 < lower.c2) lower.c2 = p.c2; else ensureHi(upper.c2, p.c2);
132}
133
134 void Box3D::makeInclude(const plask::Box3D& other) {
135 ensureLo(lower.c0, other.lower.c0);
136 ensureLo(lower.c1, other.lower.c1);
137 ensureLo(lower.c2, other.lower.c2);
138 ensureHi(upper.c0, other.upper.c0);
139 ensureHi(upper.c1, other.upper.c1);
140 ensureHi(upper.c2, other.upper.c2);
141}
142
143 void Box3D::makeIntersection(const Box3D &other) {
144 ensureHi(lower.c0, other.lower.c0);
145 ensureHi(lower.c1, other.lower.c1);
146 ensureHi(lower.c2, other.lower.c2);
147 ensureLo(upper.c0, other.upper.c0);
148 ensureLo(upper.c1, other.upper.c1);
149 ensureLo(upper.c2, other.upper.c2);
150}
151
152 Box3D Box3D::extension(Box3D other) const {
153 other.makeInclude(*this);
154 return other;
155}
156
157 Box3D Box3D::intersection(Box3D other) const {
158 other.makeIntersection(*this);
159 return other;
160}
161
162 Vec<3,double> Box3D::moveInside(Vec<3,double> p) const {
163 if (p.c0 < lower.c0) p.c0 = lower.c0; else ensureLo(p.c0, upper.c0);
164 if (p.c1 < lower.c1) p.c1 = lower.c1; else ensureLo(p.c1, upper.c1);
165 if (p.c2 < lower.c2) p.c2 = lower.c2; else ensureLo(p.c2, upper.c2);
166 return p;
167}
168
169 PLASK_API const char* const DIRECTION_NAMES[] = { "longitudinal", "transverse", "vertical" };
170
171 const Primitive<1>::DVec Primitive<1>::ZERO_VEC = 0.0;
172 const Primitive<2>::DVec Primitive<2>::ZERO_VEC = Vec<2>(0.0, 0.0);
173 const Primitive<3>::DVec Primitive<3>::ZERO_VEC = Vec<3>(0.0, 0.0, 0.0);
174
175 const Primitive<2>::DVec Primitive<2>::NAN_VEC = Vec<2>(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN());
176 const Primitive<3>::DVec Primitive<3>::NAN_VEC = Vec<3>(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN());
177
178 const Primitive<2>::Box Primitive<2>::INF_BOX = Box2D(- std::numeric_limits<double>::infinity(), - std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
179 const Primitive<3>::Box Primitive<3>::INF_BOX = Box3D(- std::numeric_limits<double>::infinity(), - std::numeric_limits<double>::infinity(), - std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
180}