-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point.h
148 lines (123 loc) · 3.23 KB
/
Point.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
#include "Vector.h"
#ifndef Point_h
#define Point_h
#include <iosfwd>
class Point {
public:
Point() {
}
Point(double x, double y, double z) {
data[0] = x; data[1] = y; data[2] = z;
}
Point(const Point& copy) {
for(int i=0;i<3;i++)
data[i] = copy.data[i];
}
Point(const Vector& copy) {
data[0] = copy.x();
data[1] = copy.y();
data[2] = copy.z();
}
//explicit Point(const Vector& copy) {
//data[0] = copy.x(); data[1] = copy.y(); data[2] = copy.z();
//}
Point& operator=(const Point& copy) {
for(int i=0;i<3;i++)
data[i] = copy.data[i];
return *this;
}
~Point() {
}
double x() const {
return data[0];
}
double y() const {
return data[1];
}
double z() const {
return data[2];
}
double length() const {
return sqrt(data[0]*data[0]+data[1]*data[1]+data[2]*data[2]);
}
Vector operator-(const Point& p) const {
return Vector(data[0]-p.data[0], data[1]-p.data[1], data[2]-p.data[2]);
}
Point operator+(const Vector& v) const {
return Point(data[0]+v.x(), data[1]+v.y(), data[2]+v.z());
}
Point operator+(const Point& p) const {
return Point(data[0]+p.data[0], data[1]+p.data[1], data[2]+p.data[2]);
}
Point operator-(const Vector& v) const {
return Point(data[0]-v.x(), data[1]-v.y(), data[2]-v.z());
}
Point& operator+=(const Vector& v) {
data[0]+=v.x();
data[1]+=v.y();
data[2]+=v.z();
return *this;
}
Point& operator-=(const Vector& v) {
data[0]-=v.x();
data[1]-=v.y();
data[2]-=v.z();
return *this;
}
Point& operator*(double v) {
data[0]*=v;
data[1]*=v;
data[2]*=v;
return *this;
}
Point& operator*(const Point& v) {
data[0]*=v.x();
data[1]*=v.y();
data[2]*=v.z();
return *this;
}
private:
double data[3];
};
inline Point operator+(const Vector& v, const Point& p)
{
return Point(v.x() + p.x(), v.y() + p.y(), v.z() + p.z());
}
inline Point Interpolate(double s2, const Point& p1, const Point& p2)
{
double s1 = 1-s2;
return Point(s1*p1.x() + s2*p2.x(), s1*p1.y() + s2*p2.y(),
s1*p1.z() + s2*p2.z());
}
inline Point AffineCombination(double s1, const Point& p1,
double s2, const Point& p2)
{
return Point(s1*p1.x() + s2*p2.x(), s1*p1.y() + s2*p2.y(),
s1*p1.z() + s2*p2.z());
}
inline Point AffineCombination(double s1, const Point& p1,
double s2, const Point& p2,
double s3, const Point& p3)
{
return Point(s1*p1.x() + s2*p2.x() + s3*p3.x(),
s1*p1.y() + s2*p2.y() + s3*p3.y(),
s1*p1.z() + s2*p2.z() + s3*p3.z());
}
inline double Dot(const Point& v1, const Vector& v2)
{
return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
}
inline double Dot(const Vector& v1, const Point& v2)
{
return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
}
inline Point Min(const Point& p1, const Point& p2)
{
return Point(Min(p1.x(), p2.x()), Min(p1.y(), p2.y()), Min(p1.z(), p2.z()));
}
inline Point Max(const Point& p1, const Point& p2)
{
return Point(Max(p1.x(), p2.x()), Max(p1.y(), p2.y()), Max(p1.z(), p2.z()));
}
std::ostream& operator<<(std::ostream&, const Point& v1);
#endif