-
Notifications
You must be signed in to change notification settings - Fork 2k
/
geometry.h
212 lines (176 loc) · 6.21 KB
/
geometry.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
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
#pragma once
#include <cmath>
#include <cassert>
#include <iostream>
template<int n> struct vec {
double data[n] = {0};
double& operator[](const int i) { assert(i>=0 && i<n); return data[i]; }
double operator[](const int i) const { assert(i>=0 && i<n); return data[i]; }
double norm2() const { return *this * *this; }
double norm() const { return std::sqrt(norm2()); }
};
template<int n> double operator*(const vec<n>& lhs, const vec<n>& rhs) {
double ret = 0;
for (int i=n; i--; ret+=lhs[i]*rhs[i]);
return ret;
}
template<int n> vec<n> operator+(const vec<n>& lhs, const vec<n>& rhs) {
vec<n> ret = lhs;
for (int i=n; i--; ret[i]+=rhs[i]);
return ret;
}
template<int n> vec<n> operator-(const vec<n>& lhs, const vec<n>& rhs) {
vec<n> ret = lhs;
for (int i=n; i--; ret[i]-=rhs[i]);
return ret;
}
template<int n> vec<n> operator*(const double& rhs, const vec<n> &lhs) {
vec<n> ret = lhs;
for (int i=n; i--; ret[i]*=rhs);
return ret;
}
template<int n> vec<n> operator*(const vec<n>& lhs, const double& rhs) {
vec<n> ret = lhs;
for (int i=n; i--; ret[i]*=rhs);
return ret;
}
template<int n> vec<n> operator/(const vec<n>& lhs, const double& rhs) {
vec<n> ret = lhs;
for (int i=n; i--; ret[i]/=rhs);
return ret;
}
template<int n1,int n2> vec<n1> embed(const vec<n2> &v, double fill=1) {
vec<n1> ret;
for (int i=n1; i--; ret[i]=(i<n2?v[i]:fill));
return ret;
}
template<int n1,int n2> vec<n1> proj(const vec<n2> &v) {
vec<n1> ret;
for (int i=n1; i--; ret[i]=v[i]);
return ret;
}
template<int n> std::ostream& operator<<(std::ostream& out, const vec<n>& v) {
for (int i=0; i<n; i++) out << v[i] << " ";
return out;
}
template<> struct vec<2> {
double x = 0, y = 0;
double& operator[](const int i) { assert(i>=0 && i<2); return i ? y : x; }
double operator[](const int i) const { assert(i>=0 && i<2); return i ? y : x; }
double norm2() const { return *this * *this; }
double norm() const { return std::sqrt(norm2()); }
vec<2> normalized() { return (*this)/norm(); }
};
template<> struct vec<3> {
double x = 0, y = 0, z = 0;
double& operator[](const int i) { assert(i>=0 && i<3); return i ? (1==i ? y : z) : x; }
double operator[](const int i) const { assert(i>=0 && i<3); return i ? (1==i ? y : z) : x; }
double norm2() const { return *this * *this; }
double norm() const { return std::sqrt(norm2()); }
vec<3> normalized() { return (*this)/norm(); }
};
typedef vec<2> vec2;
typedef vec<3> vec3;
typedef vec<4> vec4;
vec3 cross(const vec3 &v1, const vec3 &v2);
template<int n> struct dt;
template<int nrows,int ncols> struct mat {
vec<ncols> rows[nrows] = {{}};
vec<ncols>& operator[] (const int idx) { assert(idx>=0 && idx<nrows); return rows[idx]; }
const vec<ncols>& operator[] (const int idx) const { assert(idx>=0 && idx<nrows); return rows[idx]; }
vec<nrows> col(const int idx) const {
assert(idx>=0 && idx<ncols);
vec<nrows> ret;
for (int i=nrows; i--; ret[i]=rows[i][idx]);
return ret;
}
void set_col(const int idx, const vec<nrows> &v) {
assert(idx>=0 && idx<ncols);
for (int i=nrows; i--; rows[i][idx]=v[i]);
}
static mat<nrows,ncols> identity() {
mat<nrows,ncols> ret;
for (int i=nrows; i--; )
for (int j=ncols;j--; ret[i][j]=(i==j));
return ret;
}
double det() const {
return dt<ncols>::det(*this);
}
mat<nrows-1,ncols-1> get_minor(const int row, const int col) const {
mat<nrows-1,ncols-1> ret;
for (int i=nrows-1; i--; )
for (int j=ncols-1;j--; ret[i][j]=rows[i<row?i:i+1][j<col?j:j+1]);
return ret;
}
double cofactor(const int row, const int col) const {
return get_minor(row,col).det()*((row+col)%2 ? -1 : 1);
}
mat<nrows,ncols> adjugate() const {
mat<nrows,ncols> ret;
for (int i=nrows; i--; )
for (int j=ncols; j--; ret[i][j]=cofactor(i,j));
return ret;
}
mat<nrows,ncols> invert_transpose() const {
mat<nrows,ncols> ret = adjugate();
return ret/(ret[0]*rows[0]);
}
mat<nrows,ncols> invert() const {
return invert_transpose().transpose();
}
mat<ncols,nrows> transpose() const {
mat<ncols,nrows> ret;
for (int i=ncols; i--; ret[i]=this->col(i));
return ret;
}
};
template<int nrows,int ncols> vec<nrows> operator*(const mat<nrows,ncols>& lhs, const vec<ncols>& rhs) {
vec<nrows> ret;
for (int i=nrows; i--; ret[i]=lhs[i]*rhs);
return ret;
}
template<int R1,int C1,int C2>mat<R1,C2> operator*(const mat<R1,C1>& lhs, const mat<C1,C2>& rhs) {
mat<R1,C2> result;
for (int i=R1; i--; )
for (int j=C2; j--; result[i][j]=lhs[i]*rhs.col(j));
return result;
}
template<int nrows,int ncols>mat<nrows,ncols> operator*(const mat<nrows,ncols>& lhs, const double& val) {
mat<nrows,ncols> result;
for (int i=nrows; i--; result[i] = lhs[i]*val);
return result;
}
template<int nrows,int ncols>mat<nrows,ncols> operator/(const mat<nrows,ncols>& lhs, const double& val) {
mat<nrows,ncols> result;
for (int i=nrows; i--; result[i] = lhs[i]/val);
return result;
}
template<int nrows,int ncols>mat<nrows,ncols> operator+(const mat<nrows,ncols>& lhs, const mat<nrows,ncols>& rhs) {
mat<nrows,ncols> result;
for (int i=nrows; i--; )
for (int j=ncols; j--; result[i][j]=lhs[i][j]+rhs[i][j]);
return result;
}
template<int nrows,int ncols>mat<nrows,ncols> operator-(const mat<nrows,ncols>& lhs, const mat<nrows,ncols>& rhs) {
mat<nrows,ncols> result;
for (int i=nrows; i--; )
for (int j=ncols; j--; result[i][j]=lhs[i][j]-rhs[i][j]);
return result;
}
template<int nrows,int ncols> std::ostream& operator<<(std::ostream& out, const mat<nrows,ncols>& m) {
for (int i=0; i<nrows; i++) out << m[i] << std::endl;
return out;
}
template<int n> struct dt {
static double det(const mat<n,n>& src) {
double ret = 0;
for (int i=n; i--; ret += src[0][i]*src.cofactor(0,i));
return ret;
}
};
template<> struct dt<1> {
static double det(const mat<1,1>& src) {
return src[0][0];
}
};