forked from mabl/wgms3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.h
169 lines (131 loc) · 4.06 KB
/
geometry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
wgms3d - a full-vectorial finite-difference mode solver.
Copyright (C) 2005-2012 Michael Krause <[email protected]>
This file contains some class definitions and code from lib2geom
snapshot 20101112, written and Copyright (C) by Marco Cecchetti
<mrcekets at gmail.com>, Michael G. Sloan <[email protected]>, and
others, and originally released under LGPL v2.1.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GEOMETRY_H
#define __GEOMETRY_H
#include <vector>
typedef double Coord;
class Point {
private:
Coord _pt[2];
public:
Point (Coord x,
Coord y) {
_pt[0] = x; _pt[1] = y;
}
Coord operator[] (unsigned i) const {
return _pt[i];
}
Point &operator+= (Point const &o) {
for ( unsigned i = 0 ; i < 2 ; ++i ) {
_pt[i] += o._pt[i];
}
return *this;
}
const Point operator+ (const Point &other) const {
Point result = *this;
result += other;
return result;
}
Point &operator-= (Point const &o) {
for ( unsigned i = 0 ; i < 2 ; ++i ) {
_pt[i] -= o._pt[i];
}
return *this;
}
const Point operator- (const Point &other) const {
Point result = *this;
result -= other;
return result;
}
Point operator-() const {
return Point(-_pt[0], -_pt[1]);
}
Point &operator*= (Coord s) {
for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
return *this;
}
const Point operator* (const Coord &s) const {
Point result = *this;
result *= s;
return result;
}
};
inline std::ostream &operator<< (std::ostream &out_file, const Point &in_pnt) {
out_file << "X: " << in_pnt[0] << " Y: " << in_pnt[1];
return out_file;
}
class Interval {
private:
Coord _b[2];
public:
explicit Interval() { _b[0] = 0; _b[1] = 0; }
Interval(Coord u, Coord v) {
if (u <= v) {
_b[0] = u; _b[1] = v;
} else {
_b[0] = v; _b[1] = u;
}
}
Coord operator[] (unsigned i) const { return _b[i]; }
void expandTo(Coord val) {
if(val < _b[0]) _b[0] = val;
if(val > _b[1]) _b[1] = val; //no else, as we want to handle NaN
}
/** @brief Check whether the interval includes this number. */
bool contains(Coord val) const { return _b[0] <= val && val <= _b[1]; }
/** @brief Check whether the interval includes the given interval. */
bool contains(Interval const &val) const { return _b[0] <= val._b[0] && val._b[1] <= _b[1]; }
bool intersects(Interval const &val) const {
return contains(val._b[0]) || contains(val._b[1]) || val.contains(*this);
}
};
class Rect {
private:
Interval f[2];
public:
Rect() {
f[0] = f[1] = Interval();
}
Rect (Point const & a,
Point const & b) {
f[0] = Interval(a[0], b[0]);
f[1] = Interval(a[1], b[1]);
}
template <typename InputIterator>
static Rect from_range(InputIterator start, InputIterator end) {
assert(start != end);
Point p1 = *start++;
Rect result(p1, p1);
for (; start != end; ++start) {
result.expandTo(*start);
}
return result;
}
void expandTo(Point p) {
f[0].expandTo(p[0]);
f[1].expandTo(p[1]);
}
bool intersects(Rect const &r) const {
return f[0].intersects(r[0]) && f[1].intersects(r[1]);
}
Interval const & operator[] (unsigned i) const {
return f[i];
}
};
#endif // __GEOMETRY_H