-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.h
More file actions
309 lines (233 loc) · 9.44 KB
/
Copy pathscript.h
File metadata and controls
309 lines (233 loc) · 9.44 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#pragma once
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
// #include <GLFW/glfw3.h>
// #include <imgui.h>
// Salamesh core
#include <app_interface.h>
#include <script.h>
#include <input_states.h>
#include <models/model.h>
#include <models/volume_model.h>
#include <models/surface_model.h>
#include <renderers/line_renderer.h>
#include <element.h>
// Ultimaille
#include <ultimaille/all.h>
// ImGui
// #include "imgui.h"
// #include "imgui_internal.h"
// Std
#include <iostream>
#include <set>
struct MyScript final : public Script {
// Modify constructor body in order to
// register your functions / types here... using sol2 lib
MyScript(IApp &app, sol::state &lua) : Script(app) {
auto polyInspector = lua.create_table();
lua["poly_inspector"] = polyInspector;
// Example, registering some functions to lua script
polyInspector.set_function("hello", &MyScript::hello, this);
polyInspector.set_function("get_radiuses", &MyScript::getRadiuses, this);
polyInspector.set_function("compute_overlaps", &MyScript::computeOverlaps, this);
polyInspector.set_function("debug_lines", [this](IApp &app, Model &model, std::vector<float> &radiuses) {
return debugLines(app, model, radiuses);
});
polyInspector.set_function("iterate_vertices", &MyScript::iterate_vertices, this);
polyInspector.set_function("get_vertices", &MyScript::get_vertices, this);
}
// TODO create a register function that need to be override
void hello() {
std::cout << "hello" << std::endl;
}
// Get the radius of a point in world-space cartesian coordinates
// Given its point size in pixel screen-space coordinates
float getRadius(glm::mat4 &pvm, glm::vec3 worldPos, float pointSize, glm::vec2 &viewport) {
// ---- 1. Project point to screen ----
// Clip space
glm::vec4 clip = pvm * glm::vec4(worldPos, 1.0);
// Compute screen pos (2D) in pixel of center of point
glm::vec4 ndc = clip / clip.w;
glm::vec2 screen = (glm::vec2{ndc.x, ndc.y} * 0.5f + 0.5f) * viewport;
// ---- 2. Move point by half of point size ----
screen.x += pointSize * .5f;
// ---- 3. Unproject offset point to world space ----
// Back to NDC
glm::vec2 ndcOff = screen / viewport * 2.f - 1.f;
// Inverse of perspective division, return to clip space
glm::vec4 clipOff = glm::vec4(ndcOff * clip.w, clip.z, clip.w);
// Back to world space (homogeneous coordinates)
glm::mat4 invPVM = glm::inverse(pvm);
glm::vec4 worldPosOff = invPVM * clipOff;
// Homogeneous coordinates to Cartesian coordinates
worldPosOff /= worldPosOff.w;
// Above can be replaced by
// worldPosOff = glm::unProject(screen, vm, p, viewport);
// ---- 4. Compute distance between point and offset point in world-space cartesian coordinates ----
// return glm::distance(glm::vec3(worldPosOff), worldPos);
float dist = glm::distance(glm::vec3(worldPosOff), worldPos);
return 0.577 * dist;
}
std::vector<float> getRadiuses(IApp &app, Model &model) {
auto &triModel = model.as<TriModel>();
auto &m = triModel.getMesh();
float pointSize = model.getPointsRenderer().getPointSize();
// Compute PVM matrix
glm::mat4 modelMat = glm::mat4(1.0f);
modelMat = glm::translate(modelMat, model.getPosition());
auto pvm = app.getCurrentCamera().getProjectionMatrix() * app.getCurrentCamera().getViewMatrix() * modelMat;
// TODO important here seems wrong, app.getSurface().width / app.getSurface().height
// glm::vec2 viewport = { app.getWidth(), app.getHeight() };
glm::vec2 viewport = { app.getSurfaceWidth(), app.getSurfaceHeight() };
std::vector<float> radiuses(m.nverts());
for (auto &v : m.iter_vertices()) {
radiuses[v] = getRadius(pvm, sl::um2glm(v.pos()), pointSize, viewport);
}
return radiuses;
}
void iterate_vertices(Model &model, std::function<void(vec3, int)> f) {
auto &triModel = model.as<TriModel>();
auto &m = triModel.getMesh();
for (auto &v : m.iter_vertices())
f(v.pos(), v);
}
std::vector<sol::table> get_vertices(Model &model, sol::this_state s) {
std::vector<sol::table> verts;
sol::state_view lua(s);
auto &triModel = model.as<TriModel>();
auto &m = triModel.getMesh();
for (auto &v : m.iter_vertices()) {
int vi = v;
sol::table t = lua.create_table();
t["pos"] = v.pos();
t["index"] = vi;
verts.push_back(t);
}
return verts;
}
void debugLines(IApp &app, Model &model, std::vector<float> &radiuses) {
auto renderer = app.addRenderer("LineRenderer", "debug_line_renderer");
if (!renderer) {
return;
}
auto &lr = renderer->as<LineRenderer>();
auto &triModel = model.as<TriModel>();
auto &m = triModel.getMesh();
lr.clearLines();
for (auto &v : m.iter_vertices()) {
// Push lines for debug
lr.addLine({sl::um2glm(v.pos()), sl::um2glm(v.pos()) + glm::vec3(radiuses[v], 0.f, 0.f), {1.f, 0.f, 0.f}});
lr.addLine({sl::um2glm(v.pos()), sl::um2glm(v.pos()) + glm::vec3(-radiuses[v], 0.f, 0.f), {1.f, 0.f, 0.f}});
lr.addLine({sl::um2glm(v.pos()), sl::um2glm(v.pos()) + glm::vec3(0.f, radiuses[v], 0.f), {1.f, 0.f, 0.f}});
lr.addLine({sl::um2glm(v.pos()), sl::um2glm(v.pos()) + glm::vec3(0.f, -radiuses[v], 0.f), {1.f, 0.f, 0.f}});
lr.addLine({sl::um2glm(v.pos()), sl::um2glm(v.pos()) + glm::vec3(0.f, 0.f, radiuses[v]), {1.f, 0.f, 0.f}});
lr.addLine({sl::um2glm(v.pos()), sl::um2glm(v.pos()) + glm::vec3(0.f, 0.f, -radiuses[v]), {1.f, 0.f, 0.f}});
glm::vec3 color{1.f, 0.3f, 1.f};
glm::vec3 c = sl::um2glm(v.pos());
float r = radiuses[v];
// Define the 8 corners of the cube
glm::vec3 corners[8] = {
c + glm::vec3(-r, -r, -r), // 0: bottom-left-back
c + glm::vec3( r, -r, -r), // 1: bottom-right-back
c + glm::vec3( r, r, -r), // 2: top-right-back
c + glm::vec3(-r, r, -r), // 3: top-left-back
c + glm::vec3(-r, -r, r), // 4: bottom-left-front
c + glm::vec3( r, -r, r), // 5: bottom-right-front
c + glm::vec3( r, r, r), // 6: top-right-front
c + glm::vec3(-r, r, r) // 7: top-left-front
};
// Draw 12 edges (4 bottom, 4 top, 4 vertical)
// Bottom face (z = -r)
lr.addLine({corners[0], corners[1], color});
lr.addLine({corners[1], corners[2], color});
lr.addLine({corners[2], corners[3], color});
lr.addLine({corners[3], corners[0], color});
// Top face (z = +r)
lr.addLine({corners[4], corners[5], color});
lr.addLine({corners[5], corners[6], color});
lr.addLine({corners[6], corners[7], color});
lr.addLine({corners[7], corners[4], color});
// Vertical edges
lr.addLine({corners[0], corners[4], color});
lr.addLine({corners[1], corners[5], color});
lr.addLine({corners[2], corners[6], color});
lr.addLine({corners[3], corners[7], color});
}
lr.push();
}
std::vector<std::vector<long>> computeOverlaps(Model &model, std::vector<float> &radiuses) {
auto &triModel = model.as<TriModel>();
auto &m = triModel.getMesh();
auto begin = std::chrono::system_clock::now();
// Init hbox
HBoxes3 hbox;
std::vector<BBox3> bboxes(m.nverts());
for (auto &v : m.iter_vertices()) {
const float r = radiuses[v];
BBox3 bb;
bb.add(v.pos() - vec3{r, r, r});
bb.add(v.pos() + vec3{r, r, r});
bboxes[v] = bb;
}
auto end = std::chrono::system_clock::now();
std::cout << "Prepare HBox = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << std::endl;
begin = std::chrono::system_clock::now();
hbox.init(bboxes);
end = std::chrono::system_clock::now();
std::cout << "Init HBox = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << std::endl;
// Highlight degenerated points
PointAttribute<float> pointHl;
pointHl.bind("_highlight", triModel.getSurfaceAttributes(), triModel.getMesh());
pointHl.fill(0.f);
begin = std::chrono::system_clock::now();
std::vector<std::set<long>> pointOverlaps(m.nverts());
int nOverlaps = 0;
for (auto &a : m.iter_vertices()) {
BBox3 bbox;
bbox.add(a.pos() - vec3{radiuses[a], radiuses[a], radiuses[a]});
bbox.add(a.pos() + vec3{radiuses[a], radiuses[a], radiuses[a]});
std::vector<int> results;
hbox.intersect(bbox, results);
if (results.size() <= 1)
continue;
// bool isOverlaps = false;
for (auto &b : results) {
if (b == a) continue;
pointHl[a] = 1.f;
pointHl[b] = 1.f;
pointOverlaps[a].insert(b);
pointOverlaps[b].insert(a);
// isOverlaps = true;
}
// if (isOverlaps)
// ++nOverlaps;
}
// for (auto &a : m.iter_vertices()) {
// for (auto &b : m.iter_vertices()) {
// if ((a.pos() - b.pos()).norm2() <= (radiuses[a] + radiuses[b]) * .5 && a != b) {
// pointHl[a] = 1.f;
// pointHl[b] = 1.f;
// pointOverlaps[a].insert(b);
// pointOverlaps[b].insert(a);
// }
// }
// }
end = std::chrono::system_clock::now();
std::cout << "Intersect = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << std::endl;
begin = std::chrono::system_clock::now();
triModel.setHighlight(ElementKind::POINTS_ELT);
end = std::chrono::system_clock::now();
std::cout << "Set Highlight = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << std::endl;
// Must do this because sol2 doesn't support
// conversion std::vector<std::set<T>> to table directly
std::vector<std::vector<long>> tablePointOverlaps;
for (auto &x : pointOverlaps) {
std::vector<long> overlaps;
for (auto y : x) {
overlaps.push_back(y);
}
tablePointOverlaps.push_back(overlaps);
}
return tablePointOverlaps;
}
};