Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/demo-app/demo_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void processFileOBJ(std::string filename) {
}
auto psMesh = polyscope::registerSurfaceMesh(niceName, vertexPositionsGLM, faceIndices);

auto psSimpleMesh = polyscope::registerSimpleTriangleMesh(niceName, vertexPositionsGLM, faceIndices);
auto psSimpleMesh = polyscope::registerSimpleTriangleMesh(niceName + " (simple)", vertexPositionsGLM, faceIndices);
psSimpleMesh->setEnabled(false);

// Useful data
Expand Down Expand Up @@ -154,6 +154,10 @@ void processFileOBJ(std::string filename) {
polyscope::getSurfaceMesh(niceName)->addVertexScalarQuantity("categorical vert", valCat,
polyscope::DataType::CATEGORICAL);

// Simple triangle mesh quantities
psSimpleMesh->addVertexScalarQuantity("cY", valY);
psSimpleMesh->addVertexColorQuantity("vColor", randColor);

polyscope::getSurfaceMesh(niceName)->addVertexDistanceQuantity("cY_dist", valY);
polyscope::getSurfaceMesh(niceName)->addVertexSignedDistanceQuantity("cY_signeddist", valY);

Expand Down Expand Up @@ -186,6 +190,9 @@ void processFileOBJ(std::string filename) {
polyscope::getSurfaceMesh(niceName)->addFaceScalarQuantity("categorical face", fCat,
polyscope::DataType::CATEGORICAL);

psSimpleMesh->addFaceScalarQuantity("face area", fArea, polyscope::DataType::MAGNITUDE);
psSimpleMesh->addFaceColorQuantity("fColor", fColor);


// size_t nEdges = psMesh->nEdges();

Expand Down
50 changes: 50 additions & 0 deletions include/polyscope/pick.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const uint64_t bitsForPickPacking = 22;

inline glm::vec3 indToVec(size_t globalInd) {

// NOTE: there is a duplicate version of this logic in a macro below, which must be kept in sync.

// Can comfortably fit a 22 bit integer exactly in a single precision float
uint64_t factor = 1 << bitsForPickPacking;
uint64_t mask = factor - 1;
Expand Down Expand Up @@ -48,5 +50,53 @@ inline uint64_t vecToInd(glm::vec3 vec) {
return ind;
}


// == Weird alternate implementation of indToVec()
// This is here because sometimes we want to evaluate the indToVec() bit-bashing logic in a shader, and get exactly
// the same result as the C++ version above. A GLSL implementation is not too bad, but it's hard to test to ensure
// it really matches.
//
// The solution here is a funky macro'd implementation, that compiles as C++ or GLSL. We compile it as C++ in the tests
// and verify it matches the usual indToVec() implementation, then compile it as GLSL in shaders.
//
// All of the macro stuff you see below is just boilerplate to make that possible.

#define POLYSCOPE_PICK_STR_H(...) #__VA_ARGS__
#define POLYSCOPE_PICK_STR(...) POLYSCOPE_PICK_STR_H(__VA_ARGS__)

// See note above. This is logic that is meant to be identical to indToVec().
// clang-format off
#define POLYSCOPE_PICK_INDEX_COLOR_BODY \
POLYSCOPE_PICK_UINT idxLow = pickStartLow + primID; \
POLYSCOPE_PICK_UINT carry = (idxLow < pickStartLow) ? 1u : 0u; \
POLYSCOPE_PICK_UINT idxHigh = pickStartHigh + carry; \
POLYSCOPE_PICK_UINT low22 = idxLow & 0x3FFFFFu; \
POLYSCOPE_PICK_UINT med22 = ((idxLow >> 22u) | (idxHigh << 10u)) & 0x3FFFFFu; \
POLYSCOPE_PICK_UINT high22 = (idxHigh >> 12u) & 0x3FFFFFu; \
return POLYSCOPE_PICK_VEC3(float(low22), float(med22), float(high22)) / 4194304.0f;
// clang-format on

// C++ version: compile the body with C++ types.
#define POLYSCOPE_PICK_UINT uint32_t
#define POLYSCOPE_PICK_VEC3 glm::vec3
inline glm::vec3 pickIndexToColorImpl(uint32_t pickStartLow, uint32_t pickStartHigh, uint32_t primID) {
POLYSCOPE_PICK_INDEX_COLOR_BODY
}
#undef POLYSCOPE_PICK_UINT
#undef POLYSCOPE_PICK_VEC3

// Convenience wrapper for C++ callers that have a combined uint64_t pickStart.
inline glm::vec3 pickIndexToColor(uint64_t pickStart, uint32_t primID) {
return pickIndexToColorImpl(static_cast<uint32_t>(pickStart & 0xFFFFFFFFull), static_cast<uint32_t>(pickStart >> 32),
primID);
}

// GLSL string macro. Stringifies POLYSCOPE_PICK_INDEX_COLOR_BODY using GLSL type names.
// REQUIRES: POLYSCOPE_PICK_UINT must equal "uint" and POLYSCOPE_PICK_VEC3 must equal "vec3"
// at the point of expansion (see common.cpp for the usage pattern).
#define POLYSCOPE_PICK_INDEX_TO_COLOR_GLSL \
"vec3 pickIndexToColor(uint pickStartLow, uint pickStartHigh, uint primID) { " POLYSCOPE_PICK_STR( \
POLYSCOPE_PICK_INDEX_COLOR_BODY) " }"

} // namespace pick
} // namespace polyscope
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extern const ShaderReplacementRule MESH_PROPAGATE_CULLPOS;
extern const ShaderReplacementRule MESH_PROPAGATE_PICK;
extern const ShaderReplacementRule MESH_PROPAGATE_PICK_SIMPLE;
extern const ShaderReplacementRule MESH_PROPAGATE_TYPE_AND_BASECOLOR2_SHADE;
extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_VALUE;
extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_COLOR;
extern const ShaderReplacementRule SIMPLE_MESH_PROPAGATE_FACE_PICK;


} // namespace backend_openGL3
Expand Down
51 changes: 47 additions & 4 deletions include/polyscope/simple_triangle_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
#include "polyscope/render/engine.h"
#include "polyscope/render/managed_buffer.h"
#include "polyscope/scaled_value.h"
#include "polyscope/standardize_data_array.h"
#include "polyscope/structure.h"
#include "polyscope/types.h"

#include "polyscope/simple_triangle_mesh_color_quantity.h"
#include "polyscope/simple_triangle_mesh_quantity.h"
#include "polyscope/simple_triangle_mesh_scalar_quantity.h"

#include <vector>

Expand All @@ -18,9 +24,16 @@ namespace polyscope {
class SimpleTriangleMesh;

// Forward declare quantity types
class SimpleTriangleMeshScalarQuantity;
class SimpleTriangleMeshVertexScalarQuantity;
class SimpleTriangleMeshFaceScalarQuantity;
class SimpleTriangleMeshColorQuantity;
class SimpleTriangleMeshVertexColorQuantity;
class SimpleTriangleMeshFaceColorQuantity;

struct SimpleTriangleMeshPickResult {
// this does nothing for now, just matching pattern from other structures
MeshElement elementType = MeshElement::FACE; // which kind of element was clicked
int64_t index = -1; // index of the clicked element (vertex or face)
};

class SimpleTriangleMesh : public Structure {
Expand Down Expand Up @@ -50,8 +63,27 @@ class SimpleTriangleMesh : public Structure {
render::ManagedBuffer<glm::vec3> vertices;
render::ManagedBuffer<glm::uvec3> faces;

size_t nVertices() { return vertices.size(); }
size_t nFaces() { return faces.size(); }

// === Quantities

// Scalars
template <class T>
SimpleTriangleMeshVertexScalarQuantity* addVertexScalarQuantity(std::string name, const T& values,
DataType type = DataType::STANDARD);

template <class T>
SimpleTriangleMeshFaceScalarQuantity* addFaceScalarQuantity(std::string name, const T& values,
DataType type = DataType::STANDARD);

// Colors
template <class T>
SimpleTriangleMeshVertexColorQuantity* addVertexColorQuantity(std::string name, const T& values);

template <class T>
SimpleTriangleMeshFaceColorQuantity* addFaceColorQuantity(std::string name, const T& values);

// === Mutate

template <class V>
Expand Down Expand Up @@ -84,6 +116,10 @@ class SimpleTriangleMesh : public Structure {
SimpleTriangleMesh* setBackFacePolicy(BackFacePolicy newPolicy);
BackFacePolicy getBackFacePolicy();

// Selection mode (controls vertex vs face pick threshold)
SimpleTriangleMesh* setSelectionMode(MeshSelectionMode newMode);
MeshSelectionMode getSelectionMode();

// Rendering helpers used by quantities
void setSimpleTriangleMeshUniforms(render::ShaderProgram& p, bool withSurfaceShade = true);
void setSimpleTriangleMeshProgramGeometryAttributes(render::ShaderProgram& p);
Expand All @@ -102,6 +138,7 @@ class SimpleTriangleMesh : public Structure {
PersistentValue<std::string> material;
PersistentValue<BackFacePolicy> backFacePolicy;
PersistentValue<glm::vec3> backFaceColor;
PersistentValue<MeshSelectionMode> selectionMode;

// Drawing related things
// if nullptr, prepare() (resp. preparePick()) needs to be called
Expand All @@ -112,13 +149,19 @@ class SimpleTriangleMesh : public Structure {
// Do setup work related to drawing, including allocating openGL data
void ensureRenderProgramPrepared();
void ensurePickProgramPrepared();
void setPickUniforms(render::ShaderProgram& p);
void setPickUniforms(render::ShaderProgram& p); // sets u_pickStartLow/High for SIMPLE_MESH_PROPAGATE_FACE_PICK

// === Quantity adder implementations
SimpleTriangleMeshVertexScalarQuantity* addVertexScalarQuantityImpl(std::string name, const std::vector<float>& data,
DataType type);
SimpleTriangleMeshFaceScalarQuantity* addFaceScalarQuantityImpl(std::string name, const std::vector<float>& data,
DataType type);
SimpleTriangleMeshVertexColorQuantity* addVertexColorQuantityImpl(std::string name,
const std::vector<glm::vec3>& colors);
SimpleTriangleMeshFaceColorQuantity* addFaceColorQuantityImpl(std::string name, const std::vector<glm::vec3>& colors);

// == Picking related things
size_t pickStart;
glm::vec3 pickColor;
size_t pickStart = 0;
};


Expand Down
31 changes: 31 additions & 0 deletions include/polyscope/simple_triangle_mesh.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ void SimpleTriangleMesh::update(const V& newPositions, const F& newFaces) {
faces.markHostBufferUpdated();
}

// =====================================================
// ============== Quantities
// =====================================================

template <class T>
SimpleTriangleMeshVertexScalarQuantity* SimpleTriangleMesh::addVertexScalarQuantity(std::string name, const T& values,
DataType type) {
validateSize(values, nVertices(), "vertex scalar quantity " + name);
return addVertexScalarQuantityImpl(name, standardizeArray<float, T>(values), type);
}

template <class T>
SimpleTriangleMeshFaceScalarQuantity* SimpleTriangleMesh::addFaceScalarQuantity(std::string name, const T& values,
DataType type) {
validateSize(values, nFaces(), "face scalar quantity " + name);
return addFaceScalarQuantityImpl(name, standardizeArray<float, T>(values), type);
}

template <class T>
SimpleTriangleMeshVertexColorQuantity* SimpleTriangleMesh::addVertexColorQuantity(std::string name, const T& values) {
validateSize(values, nVertices(), "vertex color quantity " + name);
return addVertexColorQuantityImpl(name, standardizeVectorArray<glm::vec3, 3>(values));
}

template <class T>
SimpleTriangleMeshFaceColorQuantity* SimpleTriangleMesh::addFaceColorQuantity(std::string name, const T& values) {
validateSize(values, nFaces(), "face color quantity " + name);
return addFaceColorQuantityImpl(name, standardizeVectorArray<glm::vec3, 3>(values));
}


// Shorthand to get a mesh from polyscope
inline SimpleTriangleMesh* getSimpleTriangleMesh(std::string name) {
return dynamic_cast<SimpleTriangleMesh*>(getStructure(SimpleTriangleMesh::structureTypeName, name));
Expand Down
61 changes: 61 additions & 0 deletions include/polyscope/simple_triangle_mesh_color_quantity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run

#pragma once

#include "polyscope/color_quantity.h"
#include "polyscope/simple_triangle_mesh_quantity.h"

#include <vector>

namespace polyscope {

class SimpleTriangleMeshColorQuantity : public SimpleTriangleMeshQuantity,
public ColorQuantity<SimpleTriangleMeshColorQuantity> {
public:
SimpleTriangleMeshColorQuantity(std::string name, const std::vector<glm::vec3>& colors, std::string definedOn,
SimpleTriangleMesh& mesh);

virtual void draw() override;
virtual void buildCustomUI() override;
virtual void buildColorOptionsUI() override;
virtual void refresh() override;
virtual std::string niceName() override;

const std::string definedOn;

protected:
std::shared_ptr<render::ShaderProgram> program;

virtual void createProgram() = 0;
};


// ========================================================
// ========== Vertex Color ==========
// ========================================================

class SimpleTriangleMeshVertexColorQuantity : public SimpleTriangleMeshColorQuantity {
public:
SimpleTriangleMeshVertexColorQuantity(std::string name, const std::vector<glm::vec3>& colors,
SimpleTriangleMesh& mesh);

virtual void createProgram() override;
virtual void buildVertexInfoGUI(size_t vInd) override;
};


// ========================================================
// ========== Face Color ==========
// ========================================================

class SimpleTriangleMeshFaceColorQuantity : public SimpleTriangleMeshColorQuantity {
public:
SimpleTriangleMeshFaceColorQuantity(std::string name, const std::vector<glm::vec3>& colors,
SimpleTriangleMesh& mesh);

virtual void createProgram() override;
virtual void buildFaceInfoGUI(size_t fInd) override;
};


} // namespace polyscope
25 changes: 25 additions & 0 deletions include/polyscope/simple_triangle_mesh_quantity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run

#pragma once

#include "polyscope/quantity.h"
#include "polyscope/structure.h"

namespace polyscope {

class SimpleTriangleMesh;

class SimpleTriangleMeshQuantity : public Quantity {
public:
SimpleTriangleMeshQuantity(std::string name, SimpleTriangleMesh& parentStructure, bool dominates = false);
virtual ~SimpleTriangleMeshQuantity() {};

SimpleTriangleMesh& parent; // shadows and hides the generic member in Quantity

// Called by buildPickUI() to display this quantity's value for the selected element.
// Override in vertex quantities (for vertex picks) and face quantities (for face picks).
virtual void buildVertexInfoGUI(size_t vInd) {}
virtual void buildFaceInfoGUI(size_t fInd) {}
};

} // namespace polyscope
62 changes: 62 additions & 0 deletions include/polyscope/simple_triangle_mesh_scalar_quantity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run

#pragma once

#include "polyscope/affine_remapper.h"
#include "polyscope/render/color_maps.h"
#include "polyscope/scalar_quantity.h"
#include "polyscope/simple_triangle_mesh_quantity.h"

#include <vector>

namespace polyscope {

class SimpleTriangleMeshScalarQuantity : public SimpleTriangleMeshQuantity,
public ScalarQuantity<SimpleTriangleMeshScalarQuantity> {
public:
SimpleTriangleMeshScalarQuantity(std::string name, const std::vector<float>& values, std::string definedOn,
SimpleTriangleMesh& mesh, DataType dataType);

virtual void draw() override;
virtual void buildCustomUI() override;
virtual void refresh() override;
virtual std::string niceName() override;

const std::string definedOn;

protected:
std::shared_ptr<render::ShaderProgram> program;

virtual void createProgram() = 0;
};


// ========================================================
// ========== Vertex Scalar ==========
// ========================================================

class SimpleTriangleMeshVertexScalarQuantity : public SimpleTriangleMeshScalarQuantity {
public:
SimpleTriangleMeshVertexScalarQuantity(std::string name, const std::vector<float>& values, SimpleTriangleMesh& mesh,
DataType dataType = DataType::STANDARD);

virtual void createProgram() override;
virtual void buildVertexInfoGUI(size_t vInd) override;
};


// ========================================================
// ========== Face Scalar ==========
// ========================================================

class SimpleTriangleMeshFaceScalarQuantity : public SimpleTriangleMeshScalarQuantity {
public:
SimpleTriangleMeshFaceScalarQuantity(std::string name, const std::vector<float>& values, SimpleTriangleMesh& mesh,
DataType dataType = DataType::STANDARD);

virtual void createProgram() override;
virtual void buildFaceInfoGUI(size_t fInd) override;
};


} // namespace polyscope
Loading