Skip to content

Commit 9e7085c

Browse files
committed
split sparse volume quantities to separate classes, improve picking
1 parent b680cc7 commit 9e7085c

9 files changed

Lines changed: 484 additions & 142 deletions

‎include/polyscope/sparse_volume_grid.h‎

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@
1818
namespace polyscope {
1919

2020
class SparseVolumeGrid;
21-
class SparseVolumeGridScalarQuantity;
22-
class SparseVolumeGridColorQuantity;
21+
class SparseVolumeGridCellScalarQuantity;
22+
class SparseVolumeGridNodeScalarQuantity;
23+
class SparseVolumeGridCellColorQuantity;
24+
class SparseVolumeGridNodeColorQuantity;
25+
26+
struct SparseVolumeGridPickResult {
27+
SparseVolumeGridElement elementType;
28+
glm::ivec3 cellIndex; // only populated if cell
29+
uint64_t cellFlatIndex; // only populated if cell
30+
glm::ivec3 nodeIndex; // only populated if node
31+
};
2332

2433
class SparseVolumeGrid : public Structure {
2534
public:
2635
// Construct a new sparse volume grid structure
36+
// The origin is the NODE/CORNER orgin. That is, the cell 0,0,0, will have its lower-left corner sitting at this
37+
// origin. If you wish to specify the CENTER of the the 0,0,0 cell, you should pass (cellOrigin - 0.5 *
38+
// gridCellWidth).
2739
SparseVolumeGrid(std::string name, glm::vec3 origin, glm::vec3 gridCellWidth, std::vector<glm::ivec3> occupiedCells);
2840

2941
// === Overloads
@@ -78,27 +90,34 @@ class SparseVolumeGrid : public Structure {
7890

7991
// Cell scalar. Values array must be passed in the same order as initial input cell list.
8092
template <class T>
81-
SparseVolumeGridScalarQuantity* addCellScalarQuantity(std::string name, const T& values,
82-
DataType type = DataType::STANDARD);
93+
SparseVolumeGridCellScalarQuantity* addCellScalarQuantity(std::string name, const T& values,
94+
DataType type = DataType::STANDARD);
8395

8496
// Node scalar. Indices are _node_ indices; the nodes are a shifted sparse grid offset from the cell enumeration. For
8597
// a cell with indices ijk, its corrners are the nodes with indices (i k j, i+1 j k, ..., i+1 j+1, k+1). Node values
8698
// are passed via a paired set of arrays, giving the node index and node value for each. Node values may be passed in
8799
// any order, and having extra entries is fine too, as long as all required nodes values are present.
88100
template <class TI, class TV>
89-
SparseVolumeGridScalarQuantity* addNodeScalarQuantity(std::string name, const TI& nodeIndices, const TV& nodeValues,
90-
DataType type = DataType::STANDARD);
101+
SparseVolumeGridNodeScalarQuantity* addNodeScalarQuantity(std::string name, const TI& nodeIndices,
102+
const TV& nodeValues, DataType type = DataType::STANDARD);
91103

92104
// Cell color. Values array must be passed in the same order as initial input cell list.
93105
template <class T>
94-
SparseVolumeGridColorQuantity* addCellColorQuantity(std::string name, const T& colors);
106+
SparseVolumeGridCellColorQuantity* addCellColorQuantity(std::string name, const T& colors);
95107

96108
// Node color. Indices are _node_ indices; the nodes are a shifted sparse grid offset from the cell enumeration. For
97109
// a cell with indices ijk, its corrners are the nodes with indices (i k j, i+1 j k, ..., i+1 j+1, k+1). Node values
98110
// are passed via a paired set of arrays, giving the node index and node value for each. Node values may be passed in
99111
// any order, and having extra entries is fine too, as long as all required nodes values are present.
100112
template <class TI, class TC>
101-
SparseVolumeGridColorQuantity* addNodeColorQuantity(std::string name, const TI& nodeIndices, const TC& nodeColors);
113+
SparseVolumeGridNodeColorQuantity* addNodeColorQuantity(std::string name, const TI& nodeIndices,
114+
const TC& nodeColors);
115+
116+
// Force the grid to act as if nodes are in use (enable them for picking)
117+
void markNodesAsUsed();
118+
119+
// Get data related to picking/selection
120+
SparseVolumeGridPickResult interpretPickResult(const PickResult& result);
102121

103122
// Rendering related helpers
104123
void setCellGeometryAttributes(render::ShaderProgram& p);
@@ -159,36 +178,46 @@ class SparseVolumeGrid : public Structure {
159178
void computeCornerNodeIndices();
160179

161180
// Picking-related
162-
size_t globalPickConstant = INVALID_IND_64;
163-
glm::vec3 pickColor;
181+
bool nodesHaveBeenUsed = false;
182+
void buildCellInfoGUI(const SparseVolumeGridPickResult& result);
183+
void buildNodeInfoGUI(const SparseVolumeGridPickResult& result);
164184

165185
// Drawing related things
166186
std::shared_ptr<render::ShaderProgram> program;
167187
std::shared_ptr<render::ShaderProgram> pickProgram;
168188

169189
// === Helpers
170190
void checkForDuplicateCells();
191+
size_t findCellFlatIndex(glm::ivec3 cellInd3);
192+
size_t findNodeFlatIndex(glm::ivec3 nodeInd3);
171193
void ensureRenderProgramPrepared();
172194
void ensurePickProgramPrepared();
173195

174196
// Quantity impl methods
175-
SparseVolumeGridScalarQuantity* addCellScalarQuantityImpl(std::string name, const std::vector<float>& data,
176-
DataType type);
177-
SparseVolumeGridScalarQuantity* addNodeScalarQuantityImpl(std::string name,
178-
const std::vector<glm::ivec3>& nodeIndices,
179-
const std::vector<float>& nodeValues, DataType type);
180-
SparseVolumeGridColorQuantity* addCellColorQuantityImpl(std::string name, const std::vector<glm::vec3>& colors);
181-
SparseVolumeGridColorQuantity* addNodeColorQuantityImpl(std::string name, const std::vector<glm::ivec3>& nodeIndices,
182-
const std::vector<glm::vec3>& nodeColors);
197+
SparseVolumeGridCellScalarQuantity* addCellScalarQuantityImpl(std::string name, const std::vector<float>& data,
198+
DataType type);
199+
SparseVolumeGridNodeScalarQuantity* addNodeScalarQuantityImpl(std::string name,
200+
const std::vector<glm::ivec3>& nodeIndices,
201+
const std::vector<float>& nodeValues, DataType type);
202+
SparseVolumeGridCellColorQuantity* addCellColorQuantityImpl(std::string name, const std::vector<glm::vec3>& colors);
203+
SparseVolumeGridNodeColorQuantity* addNodeColorQuantityImpl(std::string name,
204+
const std::vector<glm::ivec3>& nodeIndices,
205+
const std::vector<glm::vec3>& nodeColors);
183206
};
184207

185208

186209
// Register a sparse volume grid
210+
// The origin is the NODE/CORNER orgin. That is, the cell 0,0,0, will have its lower-left corner sitting at this
211+
// origin. If you wish to specify the CENTER of the the 0,0,0 cell, you should pass (cellOrigin - 0.5 *
212+
// gridCellWidth).
187213
template <class T>
188214
SparseVolumeGrid* registerSparseVolumeGrid(std::string name, glm::vec3 origin, glm::vec3 gridCellWidth,
189215
const T& occupiedCells);
190216

191-
// Non-template overloads
217+
// Register a sparse volume grid (non-templated overload)
218+
// The origin is the NODE/CORNER orgin. That is, the cell 0,0,0, will have its lower-left corner sitting at this
219+
// origin. If you wish to specify the CENTER of the the 0,0,0 cell, you should pass (cellOrigin - 0.5 *
220+
// gridCellWidth).
192221
SparseVolumeGrid* registerSparseVolumeGrid(std::string name, glm::vec3 origin, glm::vec3 gridCellWidth,
193222
const std::vector<glm::ivec3>& occupiedCells);
194223

‎include/polyscope/sparse_volume_grid.ipp‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ std::vector<T> SparseVolumeGrid::canonicalizeNodeValueArray(const std::string& q
115115
// =====================================================
116116

117117
template <class T>
118-
SparseVolumeGridScalarQuantity* SparseVolumeGrid::addCellScalarQuantity(std::string name, const T& values,
119-
DataType type) {
118+
SparseVolumeGridCellScalarQuantity* SparseVolumeGrid::addCellScalarQuantity(std::string name, const T& values,
119+
DataType type) {
120120
validateSize(values, nCells(), "sparse volume grid cell scalar quantity " + name);
121121
return addCellScalarQuantityImpl(name, standardizeArray<float, T>(values), type);
122122
}
123123

124124
template <class TI, class TV>
125-
SparseVolumeGridScalarQuantity* SparseVolumeGrid::addNodeScalarQuantity(std::string name, const TI& nodeIndices,
126-
const TV& nodeValues, DataType type) {
125+
SparseVolumeGridNodeScalarQuantity* SparseVolumeGrid::addNodeScalarQuantity(std::string name, const TI& nodeIndices,
126+
const TV& nodeValues, DataType type) {
127127
if (adaptorF_size(nodeIndices) != adaptorF_size(nodeValues)) {
128128
exception("SparseVolumeGrid::addNodeScalarQuantity: nodeIndices and nodeValues must have the same size");
129129
}
@@ -132,14 +132,14 @@ SparseVolumeGridScalarQuantity* SparseVolumeGrid::addNodeScalarQuantity(std::str
132132
}
133133

134134
template <class T>
135-
SparseVolumeGridColorQuantity* SparseVolumeGrid::addCellColorQuantity(std::string name, const T& colors) {
135+
SparseVolumeGridCellColorQuantity* SparseVolumeGrid::addCellColorQuantity(std::string name, const T& colors) {
136136
validateSize(colors, nCells(), "sparse volume grid cell color quantity " + name);
137137
return addCellColorQuantityImpl(name, standardizeVectorArray<glm::vec3, 3>(colors));
138138
}
139139

140140
template <class TI, class TC>
141-
SparseVolumeGridColorQuantity* SparseVolumeGrid::addNodeColorQuantity(std::string name, const TI& nodeIndices,
142-
const TC& nodeColors) {
141+
SparseVolumeGridNodeColorQuantity* SparseVolumeGrid::addNodeColorQuantity(std::string name, const TI& nodeIndices,
142+
const TC& nodeColors) {
143143
if (adaptorF_size(nodeIndices) != adaptorF_size(nodeColors)) {
144144
exception("SparseVolumeGrid::addNodeColorQuantity: nodeIndices and nodeColors must have the same size");
145145
}

‎include/polyscope/sparse_volume_grid_color_quantity.h‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,50 @@ namespace polyscope {
1313
class SparseVolumeGridColorQuantity : public SparseVolumeGridQuantity,
1414
public ColorQuantity<SparseVolumeGridColorQuantity> {
1515
public:
16-
// Cell color constructor
17-
SparseVolumeGridColorQuantity(std::string name, SparseVolumeGrid& grid, const std::vector<glm::vec3>& colors);
18-
1916
// Node color constructor
20-
SparseVolumeGridColorQuantity(std::string name, SparseVolumeGrid& grid, const std::vector<glm::ivec3>& nodeIndices,
21-
const std::vector<glm::vec3>& nodeColors);
17+
SparseVolumeGridColorQuantity(std::string name, SparseVolumeGrid& grid, const std::string& definedOn_,
18+
const std::vector<glm::vec3>& colors_);
2219

2320
virtual void draw() override;
2421
virtual void refresh() override;
2522

2623
virtual std::string niceName() override;
2724

25+
protected:
26+
const std::string definedOn;
27+
std::shared_ptr<render::ShaderProgram> program;
28+
virtual void createProgram() = 0;
29+
};
30+
31+
// ========================================================
32+
// ========== Cell Color ==========
33+
// ========================================================
34+
35+
class SparseVolumeGridCellColorQuantity : public SparseVolumeGridColorQuantity {
36+
public:
37+
SparseVolumeGridCellColorQuantity(std::string name, SparseVolumeGrid& grid, const std::vector<glm::vec3>& cellColors);
38+
39+
virtual void createProgram() override;
40+
virtual void buildCellInfoGUI(size_t cellInd) override;
41+
};
42+
43+
44+
// ========================================================
45+
// ========== Node Color ==========
46+
// ========================================================
47+
48+
class SparseVolumeGridNodeColorQuantity : public SparseVolumeGridColorQuantity {
49+
public:
50+
SparseVolumeGridNodeColorQuantity(std::string name, SparseVolumeGrid& grid,
51+
const std::vector<glm::ivec3>& nodeIndices,
52+
const std::vector<glm::vec3>& nodeColors);
53+
54+
virtual void createProgram() override;
55+
virtual void buildNodeInfoGUI(size_t nodeInd) override;
2856
bool getNodeIndicesAreCanonical() const { return nodeIndicesAreCanonical; }
2957

30-
private:
31-
bool isNodeQuantity = false;
58+
protected:
3259
bool nodeIndicesAreCanonical; // true if user-provided indices matched canonical order exactly (set by constructor)
33-
void createProgram();
34-
std::shared_ptr<render::ShaderProgram> program;
3560
};
3661

37-
3862
} // namespace polyscope

‎include/polyscope/sparse_volume_grid_quantity.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class SparseVolumeGridQuantity : public Quantity {
1414
SparseVolumeGridQuantity(std::string name, SparseVolumeGrid& parentStructure, bool dominates = false);
1515
virtual ~SparseVolumeGridQuantity() {};
1616

17+
// Build info GUI for picked elements (overridden by subclasses to display quantity values)
18+
virtual void buildCellInfoGUI(size_t cellInd);
19+
virtual void buildNodeInfoGUI(size_t nodeInd);
20+
1721
SparseVolumeGrid& parent; // shadows and hides the generic member in Quantity
1822
};
1923

‎include/polyscope/sparse_volume_grid_scalar_quantity.h‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,52 @@ namespace polyscope {
1313

1414
class SparseVolumeGridScalarQuantity : public SparseVolumeGridQuantity,
1515
public ScalarQuantity<SparseVolumeGridScalarQuantity> {
16-
1716
public:
18-
// Cell scalar constructor
19-
SparseVolumeGridScalarQuantity(std::string name, SparseVolumeGrid& grid, const std::vector<float>& values,
20-
DataType dataType);
21-
22-
// Node scalar constructor
23-
SparseVolumeGridScalarQuantity(std::string name, SparseVolumeGrid& grid, const std::vector<glm::ivec3>& nodeIndices,
24-
const std::vector<float>& nodeValues, DataType dataType);
17+
SparseVolumeGridScalarQuantity(std::string name, SparseVolumeGrid& grid, const std::string& definedOn_,
18+
const std::vector<float>& values_, DataType dataType_);
2519

2620
virtual void draw() override;
2721
virtual void buildCustomUI() override;
2822
virtual void refresh() override;
2923

3024
virtual std::string niceName() override;
3125

26+
protected:
27+
const std::string definedOn;
28+
std::shared_ptr<render::ShaderProgram> program;
29+
virtual void createProgram() = 0;
30+
};
31+
32+
// ========================================================
33+
// ========== Cell Scalar ==========
34+
// ========================================================
35+
36+
class SparseVolumeGridCellScalarQuantity : public SparseVolumeGridScalarQuantity {
37+
public:
38+
SparseVolumeGridCellScalarQuantity(std::string name, SparseVolumeGrid& grid, const std::vector<float>& cellValues,
39+
DataType dataType);
40+
41+
virtual void createProgram() override;
42+
virtual void buildCellInfoGUI(size_t cellInd) override;
43+
};
44+
45+
46+
// ========================================================
47+
// ========== Node Scalar ==========
48+
// ========================================================
49+
50+
class SparseVolumeGridNodeScalarQuantity : public SparseVolumeGridScalarQuantity {
51+
public:
52+
SparseVolumeGridNodeScalarQuantity(std::string name, SparseVolumeGrid& grid,
53+
const std::vector<glm::ivec3>& nodeIndices,
54+
const std::vector<float>& nodeValues, DataType dataType);
55+
56+
virtual void createProgram() override;
57+
virtual void buildNodeInfoGUI(size_t nodeInd) override;
3258
bool getNodeIndicesAreCanonical() const { return nodeIndicesAreCanonical; }
3359

34-
private:
35-
bool isNodeQuantity = false;
60+
protected:
3661
bool nodeIndicesAreCanonical; // true if user-provided indices matched canonical order exactly (set by constructor)
37-
void createProgram();
38-
std::shared_ptr<render::ShaderProgram> program;
3962
};
4063

41-
4264
} // namespace polyscope

‎include/polyscope/types.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ POLYSCOPE_DEFINE_ENUM_NAMES(VolumeGridElement,
158158
{VolumeGridElement::CELL, "Cell"}
159159
);
160160

161+
enum class SparseVolumeGridElement { CELL = 0, NODE };
162+
POLYSCOPE_DEFINE_ENUM_NAMES(SparseVolumeGridElement,
163+
{SparseVolumeGridElement::CELL, "Cell"},
164+
{SparseVolumeGridElement::NODE, "Node"}
165+
);
166+
161167
enum class IsolineStyle { Stripe = 0, Contour };
162168
POLYSCOPE_DEFINE_ENUM_NAMES(IsolineStyle,
163169
{IsolineStyle::Stripe, "Stripe"},

0 commit comments

Comments
 (0)