-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.hpp
executable file
·240 lines (190 loc) · 6.82 KB
/
util.hpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Copyright ©2013 The Regents of the University of California
(Regents). All Rights Reserved. Permission to use, copy, modify, and
distribute this software and its documentation for educational,
research, and not-for-profit purposes, without fee and without a
signed licensing agreement, is hereby granted, provided that the
above copyright notice, this paragraph and the following two
paragraphs appear in all copies, modifications, and
distributions. Contact The Office of Technology Licensing, UC
Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620,
(510) 643-7201, for commercial licensing opportunities.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#ifndef UTIL_HPP
#define UTIL_HPP
#include "mesh.hpp"
#include "spline.hpp"
#include <iostream>
#include <string> // aa: win
#include <string>
#include <vector>
#define EPSILON 1e-7f
// i+1 and i-1 modulo 3
// This way of computing it tends to be faster than using %
#define NEXT(i) ((i) < 2 ? (i) + 1 : (i)-2)
#define PREV(i) ((i) > 0 ? (i)-1 : (i) + 2)
typedef unsigned int uint;
struct Transformation;
// Quick-and easy statistics
struct Stats {
mutable std::vector<double> xs;
double sum;
mutable bool sorted;
Stats() : sum(0) {}
void add(double x);
void sort() const;
double min() const, max() const, mean() const, median() const,
quantile(double) const;
};
std::ostream &operator<<(std::ostream &out, const Stats &stats);
// sprintf for std::strings
std::string stringf(std::string format, ...);
// Easy reporting of vertices and faces
std::ostream &operator<<(std::ostream &out, const Vert *vert);
std::ostream &operator<<(std::ostream &out, const Node *node);
std::ostream &operator<<(std::ostream &out, const Edge *edge);
std::ostream &operator<<(std::ostream &out, const Face *face);
// Math utilities
extern const double infinity;
template <typename T> T clamp(const T &x, const T &a, const T &b) {
return std::min(std::max(x, a), b);
}
template <typename T> T min(const T &a, const T &b, const T &c) {
return std::min(a, std::min(b, c));
}
template <typename T> T min(const T &a, const T &b, const T &c, const T &d) {
return std::min(std::min(a, b), std::min(c, d));
}
template <typename T> T max(const T &a, const T &b, const T &c) {
return std::max(a, std::max(b, c));
}
template <typename T> T max(const T &a, const T &b, const T &c, const T &d) {
return std::max(std::max(a, b), std::max(c, d));
}
template <typename T> T sgn(const T &x) { return x < 0 ? -1 : 1; }
int solve_quadratic(double a, double b, double c, double x[2]);
// Find, replace, and all that jazz
template <typename T> inline int find(const T *x, T *const *xs, int n = 3) {
for (int i = 0; i < n; i++)
if (xs[i] == x)
return i;
return -1;
}
template <typename T> inline int find(const T &x, const T *xs, int n = 3) {
for (int i = 0; i < n; i++)
if (xs[i] == x)
return i;
return -1;
}
template <typename T> inline int find(const T &x, const std::vector<T> &xs) {
for (int i = 0; i < xs.size(); i++)
if (xs[i] == x)
return i;
return -1;
}
template <typename T> inline bool is_in(const T *x, T *const *xs, int n = 3) {
return find(x, xs, n) != -1;
}
template <typename T> inline bool is_in(const T &x, const T *xs, int n = 3) {
return find(x, xs, n) != -1;
}
template <typename T> inline bool is_in(const T &x, const std::vector<T> &xs) {
return find(x, xs) != -1;
}
template <typename T> inline void include(const T &x, std::vector<T> &xs) {
if (!is_in(x, xs))
xs.push_back(x);
}
template <typename T> inline void remove(int i, std::vector<T> &xs) {
xs[i] = xs.back();
xs.pop_back();
}
template <typename T> inline void exclude(const T &x, std::vector<T> &xs) {
int i = find(x, xs);
if (i != -1)
remove(i, xs);
}
template <typename T> inline void replace(const T &v0, const T &v1, T vs[3]) {
int i = find(v0, vs);
if (i != -1)
vs[i] = v1;
}
template <typename T>
inline void replace(const T &x0, const T &x1, std::vector<T> &xs) {
int i = find(x0, xs);
if (i != -1)
xs[i] = x1;
}
template <typename T>
inline bool subset(const std::vector<T> &xs, const std::vector<T> &ys) {
for (int i = 0; i < xs.size(); i++)
if (!is_in(xs[i], ys))
return false;
return true;
}
template <typename T>
inline void append(std::vector<T> &xs, const std::vector<T> &ys) {
xs.insert(xs.end(), ys.begin(), ys.end());
}
// Comparisons on vectors
#define VEC_CMP(op) \
template <int n, typename T> \
bool operator op(const Vec<n, T> &u, const Vec<n, T> &v) { \
for (int i = 0; i < n; i++) \
if (!(u[i] op v[i])) \
return false; \
return true; \
}
VEC_CMP(<)
VEC_CMP(<=)
VEC_CMP(>)
VEC_CMP(>=)
#undef VEC_CMP
template <int n, typename T>
Vec<n, T> vec_min(const Vec<n, T> &u, const Vec<n, T> &v) {
Vec<n, T> m;
for (int i = 0; i < n; i++)
m[i] = std::min(u[i], v[i]);
return m;
}
template <int n, typename T>
Vec<n, T> vec_max(const Vec<n, T> &u, const Vec<n, T> &v) {
Vec<n, T> m;
for (int i = 0; i < n; i++)
m[i] = std::max(u[i], v[i]);
return m;
}
// Mesh utilities
bool is_seam_or_boundary(const Vert *v);
bool is_seam_or_boundary(const Node *n);
bool is_seam_or_boundary(const Edge *e);
bool is_seam_or_boundary(const Face *f);
// Debugging
void debug_save_mesh(const Mesh &mesh, const std::string &name, int n = -1);
void debug_save_meshes(const std::vector<Mesh *> &meshes,
const std::string &name, int n = -1);
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
out << "[";
for (int i = 0; i < v.size(); i++)
out << (i == 0 ? "" : ", ") << v[i];
out << "]";
return out;
}
#define ECHO(x) std::cout << #x << std::endl
#define REPORT(x) std::cout << #x << " = " << (x) << std::endl
#define REPORT_ARRAY(x, n) \
std::cout << #x << "[" << #n << "] = " << vector<double>(&(x)[0], &(x)[n]) \
<< std::endl
#endif