-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.h
More file actions
135 lines (115 loc) · 3.11 KB
/
vector.h
File metadata and controls
135 lines (115 loc) · 3.11 KB
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
#ifndef __MESH3D__VECTOR_H__
#define __MESH3D__VECTOR_H__
#include <cmath>
namespace mesh3d {
/** A struct representing 3D vector */
struct vector {
double x; //!< vector x component
double y; //!< vector y component
double z; //!< vector z component
/** Construct zero vector */
vector() : x(0), y(0), z(0) { }
/** Construct a vector with all components equal to a */
explicit vector(double a) : x(a), y(a), z(a) { }
/** Construct vector from another vector r */
vector(const vector &r) : x(r.x), y(r.y), z(r.z) { }
/** Construct vector from components */
vector(double x, double y, double z) : x(x), y(y), z(z) { }
#define BINOP(op) \
const vector operator op(const vector &r) const { \
return vector(x op r.x, y op r.y, z op r.z); \
}
/** Add two vectors */
BINOP(+)
/** Subtract two vectors */
BINOP(-)
/** Element-wise multiply two vectors */
BINOP(*)
/** Element-wise divide two vectors */
BINOP(/)
#undef BINOP
/** Multiply vector by number a */
const vector operator *(const double a) const {
return vector(a * x, a * y, a * z);
}
/** Divide vector by number a */
const vector operator /(const double a) const {
return vector(x / a, y / a, z / a);
}
/** Negate a vector */
const vector operator -() const {
return vector(-x, -y, -z);
}
/** Scale vector by number a */
vector &operator *=(double a) {
x *= a;
y *= a;
z *= a;
return *this;
}
/** Cross-product vector with another vector b */
const vector operator %(const vector &b) const {
const vector &a = *this;
return vector(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
/** Return square of vector's euclidean norm */
double norm2() const {
return x * x + y * y + z * z;
}
/** Return vector's euclidean norm */
double norm() const {
return sqrt(norm2());
}
/** Dot-product vector with another vector b */
double dot(const vector &b) const {
return x * b.x + y * b.y + z * b.z;
}
#define COMPOP(op) \
vector &operator op(const vector &b) { \
x op b.x; \
y op b.y; \
z op b.z; \
return *this; \
}
/** Add another vector to this vector */
COMPOP(+=)
/** Subtract another vector from this vector */
COMPOP(-=)
/** Element-wise multiply this vector by another vector */
COMPOP(*=)
/** Element-wise divide this vector by another vector */
COMPOP(/=)
#undef COMPOP
};
/** Multiply vector b by number a */
inline const vector operator *(double a, const vector &b) {
return b * a;
}
/** Return dot-product of two vectors */
inline double dot(const vector &a, const vector &b) {
return a.dot(b);
}
/** Return triple product of three vectors, i.e. a.dot(b.cross(c)) */
inline double triple(const vector &a, const vector &b, const vector &c) {
return a.dot(b % c);
}
/** Return squared norm of vector a */
inline double norm2(const vector &a) {
return a.norm2();
}
/** Return norm of vector a */
inline double norm(const vector &a) {
return a.norm();
}
}
#include <ostream>
namespace mesh3d {
/** Pretty print vector r to output stream o */
inline std::ostream &operator <<(std::ostream &o, const vector &r) {
return o << "(" << r.x << ", " << r.y << ", " << r.z << ")";
}
}
#endif