-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathsurface_parameterization_quantity.cpp
More file actions
278 lines (215 loc) · 9.8 KB
/
surface_parameterization_quantity.cpp
File metadata and controls
278 lines (215 loc) · 9.8 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
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/surface_parameterization_quantity.h"
#include <map>
#include <set>
#include "polyscope/curve_network.h"
#include "polyscope/file_helpers.h"
#include "polyscope/polyscope.h"
#include "polyscope/render/engine.h"
#include "imgui.h"
namespace polyscope {
// ==============================================================
// ================ Base Parameterization =====================
// ==============================================================
SurfaceParameterizationQuantity::SurfaceParameterizationQuantity(std::string name, SurfaceMesh& mesh_,
const std::vector<glm::vec2>& coords_,
MeshElement definedOn_, ParamCoordsType type_,
ParamVizStyle style_)
: SurfaceMeshQuantity(name, mesh_, true), ParameterizationQuantity(*this, coords_, type_, style_),
definedOn(definedOn_) {
// sanity check, this should basically never happen, but this guards against weird edge cases such
// as persistent values restoring the style, device updates, etc
if (getStyle() == ParamVizStyle::CHECKER_ISLANDS && !haveIslandLabels()) {
setStyle(ParamVizStyle::CHECKER);
}
}
void SurfaceParameterizationQuantity::draw() {
if (!isEnabled()) return;
if (program == nullptr) {
createProgram();
}
// Set uniforms
setParameterizationUniforms(*program);
parent.setStructureUniforms(*program);
parent.setSurfaceMeshUniforms(*program);
render::engine->setMaterialUniforms(*program, parent.getMaterial());
program->draw();
}
void SurfaceParameterizationQuantity::createProgram() {
// sanity check, this should basically never happen, but this guards against weird edge cases such
// as persistent values restoring the style, device updates, etc
if (getStyle() == ParamVizStyle::CHECKER_ISLANDS && !haveIslandLabels()) {
setStyle(ParamVizStyle::CHECKER);
}
// Create the program to draw this quantity
// clang-format off
program = render::engine->requestShader("MESH",
render::engine->addMaterialRules(parent.getMaterial(),
parent.addSurfaceMeshRules(
addParameterizationRules({
"MESH_PROPAGATE_VALUE2",
getStyle() == ParamVizStyle::CHECKER_ISLANDS ? "MESH_PROPAGATE_FLAT_VALUE" : "",
})
)
)
);
//
// Fill buffers
fillCoordBuffers(*program);
fillParameterizationBuffers(*program);
parent.setMeshGeometryAttributes(*program);
if(getStyle() == ParamVizStyle::CHECKER_ISLANDS) {
program->setAttribute("a_value", islandLabels.getIndexedRenderAttributeBuffer(parent.triangleFaceInds));
}
render::engine->setMaterial(*program, parent.getMaterial());
}
void SurfaceParameterizationQuantity::buildCustomUI() {
ImGui::SameLine();
// == Options popup
if (ImGui::Button("Options")) {
ImGui::OpenPopup("OptionsPopup");
}
if (ImGui::BeginPopup("OptionsPopup")) {
buildParameterizationOptionsUI();
if (ImGui::MenuItem("Create curve network from seams")) createCurveNetworkFromSeams();
ImGui::EndPopup();
}
buildParameterizationUI();
}
CurveNetwork* SurfaceParameterizationQuantity::createCurveNetworkFromSeams(std::string structureName) {
// set the name to default
if (structureName == "") {
structureName = parent.name + " - " + name + " - seams";
}
// Populate data on the host
parent.triangleCornerInds.ensureHostBufferPopulated();
parent.triangleVertexInds.ensureHostBufferPopulated();
parent.edgeIsReal.ensureHostBufferPopulated();
parent.vertexPositions.ensureHostBufferPopulated();
// expand out the coords buffer based on how the quantity is indexed
std::vector<glm::vec2> cornerCoords = getCornerCoords();
// helper to canonicalize edge direction
auto canonicalizeEdge = [](std::pair<int32_t, int32_t>& inds, std::pair<glm::vec2, glm::vec2>& coords)
{
if(inds.first > inds.second) {
std::swap(inds.first, inds.second);
std::swap(coords.first, coords.second);
}
};
// map to find matching & not-matching edges
// TODO set up combining hash to use unordered_map/set instead
std::map<std::pair<int32_t, int32_t>, std::pair<glm::vec2, glm::vec2>> edgeCoords;
std::map<std::pair<int32_t, int32_t>, int32_t> edgeCount;
std::set<std::pair<int32_t, int32_t>> seamEdges;
// loop over all edges
for(size_t iT = 0; iT < parent.nFacesTriangulation(); iT++) {
for(size_t k = 0; k < 3; k++) {
if(parent.edgeIsReal.data[3*iT][k] == 0.) continue; // skip internal tesselation edges
// gather data for the edge
int32_t iV_tail = parent.triangleVertexInds.data[3*iT + (k+0)%3];
int32_t iV_tip = parent.triangleVertexInds.data[3*iT + (k+1)%3];
int32_t iC_tail = parent.triangleCornerInds.data[3*iT + (k+0)%3];
int32_t iC_tip = parent.triangleCornerInds.data[3*iT + (k+1)%3];
std::pair<int32_t, int32_t> eInd (iV_tail, iV_tip);
std::pair<glm::vec2, glm::vec2> eC (cornerCoords[iC_tail], cornerCoords[iC_tip]);
canonicalizeEdge(eInd, eC); // make sure ordering is consistent
// increment the count
if(edgeCount.find(eInd) == edgeCount.end()) {
edgeCount[eInd] = 1;
} else {
edgeCount[eInd] ++;
}
// check for a collision against a previously seen copy of this edge
if(edgeCoords.find(eInd) == edgeCoords.end()) {
edgeCoords[eInd] = eC;
} else {
if( edgeCoords[eInd] != eC) {
// it's different! mark the seam
seamEdges.emplace(eInd);
}
}
}
}
// add all edges that appeared any number of times other than 2
// (boundaries and nonmanifold edges are always seams)
for(auto& entry : edgeCount) {
if(entry.second != 2) {
seamEdges.emplace(entry.first);
}
}
// densely enumerate the nodes of the seam curves
std::vector<std::array<int32_t, 2>> seamEdgeInds;
std::map<int32_t, int32_t> vertexIndToDense;
std::vector<glm::vec3> seamEdgeNodes;
for(const std::pair<int32_t, int32_t>& edge: seamEdges) {
int32_t vA = edge.first;
int32_t vB = edge.second;
// get unique vertices for the edges
if(vertexIndToDense.find(vA) == vertexIndToDense.end()) {
vertexIndToDense[vA] = seamEdgeNodes.size();
seamEdgeNodes.push_back(parent.vertexPositions.data[vA]);
}
int32_t nA = vertexIndToDense[vA];
if(vertexIndToDense.find(vB) == vertexIndToDense.end()) {
vertexIndToDense[vB] = seamEdgeNodes.size();
seamEdgeNodes.push_back(parent.vertexPositions.data[vB]);
}
int32_t nB = vertexIndToDense[vB];
// add the edge
seamEdgeInds.push_back({nA, nB});
}
// add the curve network
return registerCurveNetwork(structureName, seamEdgeNodes, seamEdgeInds);
}
size_t SurfaceParameterizationQuantity::nFaces() {
return parent.nFaces();
}
void SurfaceParameterizationQuantity::refresh() {
program.reset();
Quantity::refresh();
}
// ==============================================================
// =============== Corner Parameterization ====================
// ==============================================================
SurfaceCornerParameterizationQuantity::SurfaceCornerParameterizationQuantity(std::string name, SurfaceMesh& mesh_,
const std::vector<glm::vec2>& coords_,
ParamCoordsType type_,
ParamVizStyle style_)
: SurfaceParameterizationQuantity(name, mesh_, coords_, MeshElement::CORNER, type_, style_) {}
std::string SurfaceCornerParameterizationQuantity::niceName() { return name + " (corner parameterization)"; }
void SurfaceCornerParameterizationQuantity::fillCoordBuffers(render::ShaderProgram& p) {
p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds));
}
std::vector<glm::vec2> SurfaceCornerParameterizationQuantity::getCornerCoords() {
return coords.getIndexedView(parent.triangleCornerInds);
}
void SurfaceCornerParameterizationQuantity::buildCornerInfoGUI(size_t cInd) {
glm::vec2 coord = coords.getValue(cInd);
ImGui::TextUnformatted(name.c_str());
ImGui::NextColumn();
ImGui::Text("<%g,%g>", coord.x, coord.y);
ImGui::NextColumn();
}
// ==============================================================
// =============== Vertex Parameterization ====================
// ==============================================================
SurfaceVertexParameterizationQuantity::SurfaceVertexParameterizationQuantity(std::string name, SurfaceMesh& mesh_,
const std::vector<glm::vec2>& coords_,
ParamCoordsType type_,
ParamVizStyle style_)
: SurfaceParameterizationQuantity(name, mesh_, coords_, MeshElement::VERTEX, type_, style_) {}
std::string SurfaceVertexParameterizationQuantity::niceName() { return name + " (vertex parameterization)"; }
void SurfaceVertexParameterizationQuantity::fillCoordBuffers(render::ShaderProgram& p) {
p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds));
}
std::vector<glm::vec2> SurfaceVertexParameterizationQuantity::getCornerCoords() {
return coords.getIndexedView(parent.triangleVertexInds);
}
void SurfaceVertexParameterizationQuantity::buildVertexInfoGUI(size_t vInd) {
glm::vec2 coord = coords.getValue(vInd);
ImGui::TextUnformatted(name.c_str());
ImGui::NextColumn();
ImGui::Text("<%g,%g>", coord.x, coord.y);
ImGui::NextColumn();
}
} // namespace polyscope