Skip to content

Commit b1f5ae8

Browse files
authored
add buffer index view helper, fix seam curves from vertex param (#355)
* add buffer view getter, fix curve networks for vertices, cleanup
1 parent 16139ad commit b1f5ae8

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

include/polyscope/render/managed_buffer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ class ManagedBuffer : public virtual WeakReferrable {
178178
// same view will be returned repeatedly at no additional cost.
179179
std::shared_ptr<render::AttributeBuffer> getIndexedRenderAttributeBuffer(ManagedBuffer<uint32_t>& indices);
180180

181+
// Get a copy of the data viewed through an index, such that view[i] = data[indices[i]].
182+
//
183+
// This follows the same logic as above, but rather than returning a render buffer it simply returns a host-side
184+
// copy (which is not cached).
185+
std::vector<T> getIndexedView(ManagedBuffer<uint32_t>& indices);
186+
181187
// ========================================================================
182188
// == Direct access to the GPU (device-side) render texture buffer
183189
// ========================================================================

include/polyscope/surface_parameterization_quantity.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class SurfaceParameterizationQuantity : public SurfaceMeshQuantity,
4848
void createProgram();
4949
size_t nFaces(); // works around an incomplete def of the parent mesh
5050
virtual void fillCoordBuffers(render::ShaderProgram& p) = 0;
51+
virtual std::vector<glm::vec2> getCornerCoords() = 0;
5152
};
5253

5354

@@ -66,6 +67,7 @@ class SurfaceCornerParameterizationQuantity : public SurfaceParameterizationQuan
6667

6768
protected:
6869
virtual void fillCoordBuffers(render::ShaderProgram& p) override;
70+
virtual std::vector<glm::vec2> getCornerCoords() override;
6971
};
7072

7173

@@ -84,6 +86,7 @@ class SurfaceVertexParameterizationQuantity : public SurfaceParameterizationQuan
8486

8587
protected:
8688
virtual void fillCoordBuffers(render::ShaderProgram& p) override;
89+
virtual std::vector<glm::vec2> getCornerCoords() override;
8790
};
8891

8992

src/render/managed_buffer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ ManagedBuffer<T>::getIndexedRenderAttributeBuffer(ManagedBuffer<uint32_t>& indic
392392
return newBuffer;
393393
}
394394

395+
template <typename T>
396+
std::vector<T> ManagedBuffer<T>::getIndexedView(ManagedBuffer<uint32_t>& indices) {
397+
checkDeviceBufferTypeIs(DeviceBufferType::Attribute);
398+
ensureHostBufferPopulated();
399+
indices.ensureHostBufferPopulated();
400+
return gather(data, indices.data);
401+
}
402+
395403
template <typename T>
396404
void ManagedBuffer<T>::updateIndexedViews() {
397405
checkDeviceBufferTypeIs(DeviceBufferType::Attribute);

src/surface_parameterization_quantity.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ CurveNetwork* SurfaceParameterizationQuantity::createCurveNetworkFromSeams(std::
109109
}
110110

111111
// Populate data on the host
112-
coords.ensureHostBufferPopulated();
113112
parent.triangleCornerInds.ensureHostBufferPopulated();
114113
parent.triangleVertexInds.ensureHostBufferPopulated();
115114
parent.edgeIsReal.ensureHostBufferPopulated();
116115
parent.vertexPositions.ensureHostBufferPopulated();
117116

117+
// expand out the coords buffer based on how the quantity is indexed
118+
std::vector<glm::vec2> cornerCoords = getCornerCoords();
119+
118120
// helper to canonicalize edge direction
119121
auto canonicalizeEdge = [](std::pair<int32_t, int32_t>& inds, std::pair<glm::vec2, glm::vec2>& coords)
120122
{
@@ -141,7 +143,7 @@ CurveNetwork* SurfaceParameterizationQuantity::createCurveNetworkFromSeams(std::
141143
int32_t iC_tail = parent.triangleCornerInds.data[3*iT + (k+0)%3];
142144
int32_t iC_tip = parent.triangleCornerInds.data[3*iT + (k+1)%3];
143145
std::pair<int32_t, int32_t> eInd (iV_tail, iV_tip);
144-
std::pair<glm::vec2, glm::vec2> eC (coords.data[iC_tail], coords.data[iC_tip]);
146+
std::pair<glm::vec2, glm::vec2> eC (cornerCoords[iC_tail], cornerCoords[iC_tip]);
145147
canonicalizeEdge(eInd, eC); // make sure ordering is consistent
146148

147149
// increment the count
@@ -178,7 +180,7 @@ CurveNetwork* SurfaceParameterizationQuantity::createCurveNetworkFromSeams(std::
178180
for(const std::pair<int32_t, int32_t>& edge: seamEdges) {
179181
int32_t vA = edge.first;
180182
int32_t vB = edge.second;
181-
183+
182184
// get unique vertices for the edges
183185
if(vertexIndToDense.find(vA) == vertexIndToDense.end()) {
184186
vertexIndToDense[vA] = seamEdgeNodes.size();
@@ -196,7 +198,6 @@ CurveNetwork* SurfaceParameterizationQuantity::createCurveNetworkFromSeams(std::
196198
}
197199

198200
// add the curve network
199-
200201
return registerCurveNetwork(structureName, seamEdgeNodes, seamEdgeInds);
201202
}
202203

@@ -227,6 +228,11 @@ void SurfaceCornerParameterizationQuantity::fillCoordBuffers(render::ShaderProgr
227228
p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleCornerInds));
228229
}
229230

231+
232+
std::vector<glm::vec2> SurfaceCornerParameterizationQuantity::getCornerCoords() {
233+
return coords.getIndexedView(parent.triangleCornerInds);
234+
}
235+
230236
void SurfaceCornerParameterizationQuantity::buildCornerInfoGUI(size_t cInd) {
231237

232238
glm::vec2 coord = coords.getValue(cInd);
@@ -254,6 +260,10 @@ void SurfaceVertexParameterizationQuantity::fillCoordBuffers(render::ShaderProgr
254260
p.setAttribute("a_value2", coords.getIndexedRenderAttributeBuffer(parent.triangleVertexInds));
255261
}
256262

263+
std::vector<glm::vec2> SurfaceVertexParameterizationQuantity::getCornerCoords() {
264+
return coords.getIndexedView(parent.triangleVertexInds);
265+
}
266+
257267
void SurfaceVertexParameterizationQuantity::buildVertexInfoGUI(size_t vInd) {
258268

259269
glm::vec2 coord = coords.getValue(vInd);

test/src/curve_network_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ TEST_F(PolyscopeTest, ShowCurveNetwork) {
1818
EXPECT_FALSE(polyscope::hasCurveNetwork("test1"));
1919
}
2020

21+
TEST_F(PolyscopeTest, EmptyCurveNetwork) {
22+
std::vector<glm::vec3> points;
23+
std::vector<std::array<size_t, 2>> edges;
24+
polyscope::registerCurveNetwork("empty", points, edges);
25+
polyscope::show(3);
26+
polyscope::removeAllStructures();
27+
}
28+
2129
TEST_F(PolyscopeTest, CurveNetworkAppearance) {
2230
auto psCurve = registerCurveNetwork();
2331

test/src/surface_mesh_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ TEST_F(PolyscopeTest, SurfaceMeshVertexParam) {
497497
q1->setStyle(polyscope::ParamVizStyle::LOCAL_RAD);
498498
polyscope::show(3);
499499

500+
// create the curve network
501+
q1->createCurveNetworkFromSeams();
502+
polyscope::show(3);
503+
500504
polyscope::removeAllStructures();
501505
}
502506

0 commit comments

Comments
 (0)