-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoint.hpp
executable file
·236 lines (207 loc) · 5.55 KB
/
Point.hpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef CS207_POINT_HPP
#define CS207_POINT_HPP
#include <iostream>
#include <cmath>
/** @file Point.hpp
* @brief Define the Point class for 3D points.
*/
#define for_i for(std::size_t i = 0; i != 3; ++i)
/** @class Point
* @brief Class representing 3D points and vectors.
*
* Point contains methods that support use as points in 3D space, and
* use as 3-dimensional vectors (that can, for example, be cross-producted).
*
* Its x, y, and z components are publicly accessible under those names. They
* can also be accessed as coordinate[0], coordinate[1], and coordinate[2],
* respectively.
*/
struct Point {
union {
struct {
double x;
double y;
double z;
};
double elem[3];
};
typedef double value_type;
typedef double& reference;
typedef const double& const_reference;
typedef double* iterator;
typedef const double* const_iterator;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// CONSTRUCTORS
Point() {
for_i elem[i] = value_type();
}
explicit Point(value_type b) {
for_i elem[i] = b;
}
Point(value_type b0, value_type b1, value_type b2) {
elem[0] = b0; elem[1] = b1; elem[2] = b2;
}
// COMPARATORS
bool operator==(const Point& b) const {
for_i if (elem[i] != b[i]) return false;
return true;
}
bool operator!=(const Point& b) const {
return !(*this == b);
}
// MODIFIERS
/** Add scalar @a b to this Point */
Point& operator+=(value_type b) {
for_i elem[i] += b;
return *this;
}
/** Subtract scalar @a b from this Point */
Point& operator-=(value_type b) {
for_i elem[i] -= b;
return *this;
}
/** Scale this Point up by scalar @a b */
Point& operator*=(value_type b) {
for_i elem[i] *= b;
return *this;
}
/** Scale this Point down by scalar @a b */
Point& operator/=(value_type b) {
for_i elem[i] /= b;
return *this;
}
/** Add Point @a b to this Point */
Point& operator+=(const Point& b) {
for_i elem[i] += b[i];
return *this;
}
/** Subtract Point @a b from this Point */
Point& operator-=(const Point& b) {
for_i elem[i] -= b[i];
return *this;
}
/** Scale this Point up by factors in @a b */
Point& operator*=(const Point& b) {
for_i elem[i] *= b[i];
return *this;
}
/** Scale this Point down by factors in @a b */
Point& operator/=(const Point& b) {
for_i elem[i] /= b[i];
return *this;
}
// ACCESSORS
reference operator[](size_type i) { return elem[i]; }
const_reference operator[](size_type i) const { return elem[i]; }
iterator data() { return elem; }
const_iterator data() const { return elem; }
reference front() { return elem[0]; }
const_reference front() const { return elem[0]; }
reference back() { return elem[2]; }
const_reference back() const { return elem[2]; }
static constexpr size_type size() { return 3; }
static constexpr size_type max_size() { return 3; }
static constexpr bool empty() { return false; }
// ITERATORS
iterator begin() { return elem; }
const_iterator begin() const { return elem; }
const_iterator cbegin() const { return elem; }
iterator end() { return elem+3; }
const_iterator end() const { return elem+3; }
const_iterator cend() const { return elem+3; }
};
// STREAM OPERATORS
/** Write a Point to an output stream */
std::ostream& operator<<(std::ostream& s, const Point& a) {
return (s << a.x << ' ' << a.y << ' ' << a.z);
}
/** Read a Vec from an input stream */
std::istream& operator>>(std::istream& s, Point& a) {
return (s >> a.x >> a.y >> a.z);
}
// ARITHMETIC OPERATORS
/** Unary negation: Return -@a a */
Point operator-(const Point& a) {
return Point(-a.x, -a.y, -a.z);
}
/** Unary plus: Return @a a. ("+a" should work if "-a" works.) */
Point operator+(const Point& a) {
return a;
}
Point operator+(Point a, const Point& b) {
return a += b;
}
Point operator+(Point a, double b) {
return a += b;
}
Point operator+(double b, Point a) {
return a += b;
}
Point operator-(Point a, const Point& b) {
return a -= b;
}
Point operator-(Point a, double b) {
return a -= b;
}
Point operator-(double b, const Point& a) {
return (-a) += b;
}
Point operator*(Point a, const Point& b) {
return a *= b;
}
Point operator*(Point a, double b) {
return a *= b;
}
Point operator*(double b, Point a) {
return a *= b;
}
Point operator/(Point a, const Point& b) {
return a /= b;
}
Point operator/(Point a, double b) {
return a /= b;
}
// NORMS AND MATH OPERATORS
/** Compute cross product of two 3D Points */
Point cross(const Point& a, const Point& b) {
return Point(a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]);
}
/** Compute the inner product of two Points */
double inner_prod(const Point& a, const Point& b) {
double v = 0;
for_i v += a[i]*b[i];
return v;
}
double dot(const Point& a, const Point& b) {
return inner_prod(a,b);
}
/** Compute the squared L2 norm of a Point */
double normSq(const Point& a) {
double v = 0;
for_i v += a[i]*a[i];
return v;
}
/** Compute the L2 norm of a Point */
double norm(const Point& a) {
return std::sqrt(normSq(a));
}
double norm_2(const Point& a) {
return norm(a);
}
/** Compute the L1 norm of a Point */
double norm_1(const Point& a) {
double v = 0;
for_i v += std::abs(a[i]);
return v;
}
/** Compute the L-infinity norm of a Point */
double norm_inf(const Point& a) {
double v = 0;
for_i v = std::max(v, std::abs(a[i]));
return v;
}
#undef for_i
#endif