-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.cpp
executable file
·172 lines (151 loc) · 4.81 KB
/
util.cpp
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
/*
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.
*/
#include "io.hpp"
#include "mesh.hpp"
#include "util.hpp"
#include <algorithm>
#include <cstdarg>
#include <cstdio>
#include <iomanip>
#include <limits>
#include <map>
#include <sstream>
using namespace std;
void Stats::add(double x) {
xs.push_back(x);
sum += x;
sorted = false;
}
void Stats::sort() const {
if (sorted)
return;
std::sort(xs.begin(), xs.end());
sorted = true;
}
double Stats::min() const {
sort();
return xs.front();
}
double Stats::max() const {
sort();
return xs.back();
}
double Stats::mean() const { return sum / xs.size(); }
double Stats::median() const { return quantile(0.5); }
double Stats::quantile(double q) const {
sort();
return xs[(int)(q * xs.size())];
}
ostream &operator<<(ostream &out, const Stats &stats) {
if (stats.xs.empty())
out << "no data";
else
out << stats.min() << " " << stats.quantile(0.05) << " "
<< stats.quantile(0.25) << " " << stats.median() << " "
<< stats.quantile(0.75) << " " << stats.quantile(0.95) << " "
<< stats.max();
return out;
}
inline string stringf(string format, ...) {
char buf[256];
va_list args;
// va_start(args, std::string(format));
va_start(args, format);
vsnprintf(buf, 256, format.c_str(), args);
va_end(args);
return std::string(buf);
}
template <typename T> string name(const T *p) {
stringstream ss;
ss << setw(3) << setfill('0') << hex << ((size_t)p / sizeof(T)) % 0xfff;
return ss.str();
}
ostream &operator<<(ostream &out, const Vert *vert) {
out << "v:" << name(vert);
return out;
}
ostream &operator<<(ostream &out, const Node *node) {
out << "n:" << name(node) << node->verts;
return out;
}
ostream &operator<<(ostream &out, const Edge *edge) {
out << "e:" << name(edge) << "(" << edge->n[0] << "-" << edge->n[1] << ")";
return out;
}
ostream &operator<<(ostream &out, const Face *face) {
out << "f:" << name(face) << "(" << face->v[0] << "-" << face->v[1] << "-"
<< face->v[2] << ")";
return out;
}
const double infinity = numeric_limits<double>::infinity();
int solve_quadratic(double a, double b, double c, double x[2]) {
// http://en.wikipedia.org/wiki/Quadratic_formula#Floating_point_implementation
double d = b * b - 4 * a * c;
if (d < 0) {
x[0] = -b / (2 * a);
return 0;
}
double q = -(b + sgn(b) * sqrt(d)) / 2;
int i = 0;
if (abs(a) > 1e-12 * abs(q))
x[i++] = q / a;
if (abs(q) > 1e-12 * abs(c))
x[i++] = c / q;
if (i == 2 && x[0] > x[1])
swap(x[0], x[1]);
return i;
}
bool is_seam_or_boundary(const Vert *v) { return is_seam_or_boundary(v->node); }
bool is_seam_or_boundary(const Node *n) {
for (int e = 0; e < n->adje.size(); e++)
if (is_seam_or_boundary(n->adje[e]))
return true;
return false;
}
bool is_seam_or_boundary(const Edge *e) {
return !e->adjf[0] || !e->adjf[1] || edge_vert(e, 0, 0) != edge_vert(e, 1, 0);
}
bool is_seam_or_boundary(const Face *f) {
return is_seam_or_boundary(f->adje[0]) || is_seam_or_boundary(f->adje[1]) ||
is_seam_or_boundary(f->adje[2]);
}
void debug_save_meshes(const vector<Mesh *> &meshvec, const string &name,
int n) {
static map<string, int> savecount;
if (n == -1)
n = savecount[name];
else
savecount[name] = n;
save_objs(meshvec, stringf("tmp/%s%04d", name.c_str(), n));
savecount[name]++;
}
void debug_save_mesh(const Mesh &mesh, const string &name, int n) {
static map<string, int> savecount;
if (n == -1)
n = savecount[name];
else
savecount[name] = n;
save_obj(mesh, stringf("tmp/%s%04d.obj", name.c_str(), n));
savecount[name]++;
}