-
Notifications
You must be signed in to change notification settings - Fork 3
/
veccpp.h
164 lines (160 loc) · 3.2 KB
/
veccpp.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
/*
* Copyright(c) 2016-2019 Nicolas Sauzede ([email protected])
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <vector>
#include <iostream>
#if 0
template<int n> class vec;
typedef vec<3> v3;
template<int n> class vec {
#else
class vec;
typedef vec v3;
class vec {
static const int n = 3;
#endif
public:
vec( double d1 = 0, double d2 = 0, double d3 = 0) {
m_d[0] = d1;
m_d[1] = d2;
m_d[2] = d3;
}
vec( const double *d) {
for (unsigned ii = 0; ii < n; ii++) {
m_d[ii] = *d++;
}
}
const double& at( unsigned i) const {
assert( (i < n));
return m_d[i];
}
vec operator^( const vec& r) const {
vec res;
res.m_d[0] = this->m_d[1] * r.m_d[2] - this->m_d[2] * r.m_d[1];
res.m_d[1] = this->m_d[2] * r.m_d[0] - this->m_d[0] * r.m_d[2];
res.m_d[2] = this->m_d[0] * r.m_d[1] - this->m_d[1] * r.m_d[0];
return res;
}
const vec& operator/=( const double& r) {
for (unsigned ii = 0; ii < n; ii++) {
m_d[ii] /= r;
}
return *this;
}
vec operator/( const double& r) const {
vec res = *this;
res /= r;
return res;
}
const vec& operator*=( const double& r) {
for (unsigned ii = 0; ii < n; ii++) {
m_d[ii] *= r;
}
return *this;
}
const vec& operator*=( const vec& r) {
for (unsigned ii = 0; ii < n; ii++) {
m_d[ii] *= r.m_d[ii];
}
return *this;
}
vec operator*( const double& r) const {
vec res = *this;
res *= r;
return res;
}
vec operator*( const vec& r) const {
vec res = *this;
res *= r;
return res;
}
const vec& operator+=( const vec& r) {
for (unsigned ii = 0; ii < n; ii++) {
m_d[ii] += r.m_d[ii];
}
return *this;
}
vec operator+( const vec& r) const {
vec res = *this;
res += r;
return res;
}
const vec& operator-=( const vec& r) {
for (unsigned ii = 0; ii < n; ii++) {
m_d[ii] -= r.m_d[ii];
}
return *this;
}
vec operator-( const vec& r) const {
vec res = *this;
res -= r;
return res;
}
double operator!() const {
return sqrt( *this % *this);
}
const double& operator[]( unsigned i) const {
assert( (i < n));
return m_d[i];
}
double& operator[]( unsigned i) {
assert( (i < n));
return m_d[i];
}
vec operator~() const {
vec res = *this;
res /= !res;
return res;
}
double operator%( const vec& r) const {
double result = 0;
for (unsigned ii = 0; ii < n; ii++) {
result += m_d[ii] * r.m_d[ii];
}
return result;
}
void Print() const {
for (unsigned ii = 0; ii < n; ii++) {
if (ii > 0)
printf( ",");
printf( "%f", m_d[ii]);
}
printf( "\n");
}
void Print( std::ostream& out) const {
for (unsigned ii = 0; ii < n; ii++) {
if (ii > 0)
out << ", ";
out << m_d[ii];
}
}
void Json( std::ostream& out) const {
out << "[";
for (unsigned ii = 0; ii < n; ii++) {
if (ii > 0)
out << ", ";
out << m_d[ii];
}
out << "]";
}
friend std::ostream& operator<<( std::ostream& out, const vec& v) {
for (unsigned ii = 0; ii < n; ii++) {
if (ii > 0)
out << " ";
out << v[ii];
}
return out;
}
private:
double m_d[n];
};
inline void vprint( const v3 &v) {
printf( "%f,%f,%f\n", v[0], v[1], v[2]);
}
inline void vprint( const char *t, const v3 &v) {
printf( "%s={%f,%f,%f}\n", t, v[0], v[1], v[2]);
}