forked from dhmhd/poly-split
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.h
134 lines (110 loc) · 2.86 KB
/
vector.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
#ifndef VECTOR_H
#define VECTOR_H
#include <cmath>
#include <ostream>
#include <vector>
#define POLY_SPLIT_EPS 1E-6
class Vector
{
public:
double x, y, z;
Vector(double x = 0.0f, double y = 0.0f, double z = 0.0f)
: x(x), y(y), z(z) {}
inline Vector operator-() const
{
return Vector(-x, -y, -z);
}
inline Vector &operator+=(const Vector &v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
inline Vector &operator-=(const Vector &v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
inline Vector &operator*=(double v)
{
x *= v;
y *= v;
z *= v;
return *this;
}
inline Vector &operator/=(double v)
{
x /= v;
y /= v;
z /= v;
return *this;
}
inline Vector operator-(const Vector &v) const
{
return Vector(x - v.x, y - v.y, z - v.z);
}
inline Vector operator+(const Vector &v) const
{
return Vector(x + v.x, y + v.y, z + v.z);
}
inline Vector operator*(const double v) const
{
return Vector(x * v, y * v, z * v);
}
inline Vector operator/(const double v) const
{
return Vector(x / v, y / v, z / v);
}
inline double dot(const Vector &v) const
{
return x * v.x + y * v.y + z * v.z;
}
inline double length(void) const
{
return sqrt(x * x + y * y + z * z);
}
inline double squareLength(void) const
{
return x * x + y * y + z * z;
}
inline Vector norm(void) const
{
double l = length();
if(l == 0)
return Vector();
else
return Vector(x / l, y / l, z / l);
}
inline bool operator ==(const Vector &v) const
{
return x == v.x && y == v.y && z == v.z;
}
inline bool operator !=(const Vector &v) const
{
return fabs(x - v.x) >= POLY_SPLIT_EPS || fabs(y - v.y) >= POLY_SPLIT_EPS || fabs(z - v.z) >= POLY_SPLIT_EPS;
}
friend std::ostream& operator<< (std::ostream &out, const Vector &v)
{
out << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return out;
}
/*Added the following angle() function to determine the angle for the polygon vertex.*/
static double angle(const Vector &v2, const Vector &v1, const Vector &v3)
{
double dx21 = v2.x-v1.x;
double dx31 = v3.x-v1.x;
double dy21 = v2.y-v1.y;
double dy31 = v3.y-v1.y;
double m12 = sqrt( dx21*dx21 + dy21*dy21 );
double m13 = sqrt( dx31*dx31 + dy31*dy31 );
double theta = acos( (dx21*dx31 + dy21*dy31) / (m12 * m13) );
return theta*180/M_PI;
}
};
typedef std::vector<Vector> Vectors;
typedef std::vector<Vector>::iterator VectIter;
typedef std::vector<Vector>::const_iterator CVectIter;
#endif // VECTOR_H