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
18namespace plask {
19
20static inline void ensureLo(double& to_be_lo, double how_lo) {
22}
23
24static inline void ensureHi(double& to_be_hi, double how_hi) {
26}
27
28//------------- ---------------------
29
30bool Box2D::operator ==(const Box2D &r) const {
31 return lower == r.lower && upper == r.upper;
32}
33
34bool Box2D::operator !=(const Box2D &r) const {
35 return lower != r.lower || upper != r.upper;
36}
37
38void 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
44bool 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
49bool 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
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
63void 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
70void 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
78 other.makeIntersection(*this);
79 return other;
80}
81
83 other.makeInclude(*this);
84 return other;
85}
86
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
96bool Box3D::operator ==(const Box3D &r) const {
97 return lower == r.lower && upper == r.upper;
98}
99
100bool Box3D::operator !=(const Box3D &r) const {
101 return lower != r.lower || upper != r.upper;
102}
103
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
111bool 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
117bool 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
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
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
143void 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
153 other.makeInclude(*this);
154 return other;
155}
156
158 other.makeIntersection(*this);
159 return other;
160}
161
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
172
173const Primitive<2>::DVec Primitive<2>::NAN_VEC = Vec<2>(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN());
174const 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());
175
176const 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());
177const 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());
178}