|
| 1 | +# Sparse Volume Grids |
| 2 | + |
| 3 | +Sparse volume grid structures visualize data defined on a sparse subset of cells in an axis-aligned 3D grid. Unlike the dense [Volume Grid]([[url.prefix]]/structures/volume_grid/basics), a sparse volume grid only stores and renders the cells you specify, making it well-suited for adaptive grids, occupancy maps, and other data that occupies only a portion of a regular grid. |
| 4 | + |
| 5 | +<video width=100% autoplay muted loop> |
| 6 | + <source src="[[url.prefix]]/media/sparse_volume_grid.mp4" type="video/mp4"> |
| 7 | + Your browser does not support the video tag. |
| 8 | +</video> |
| 9 | + |
| 10 | +As usual, first you register a sparse volume grid by specifying a grid origin, cell size, and list of occupied cells. Then you can add quantities such as scalar or color data defined per-cell or per-node. |
| 11 | + |
| 12 | +**Example:** registering a sparse volume grid and adding a scalar quantity |
| 13 | +```cpp |
| 14 | +#include "polyscope/polyscope.h" |
| 15 | +#include "polyscope/sparse_volume_grid.h" |
| 16 | + |
| 17 | +polyscope::init(); |
| 18 | + |
| 19 | +// define the grid parameters |
| 20 | +glm::vec3 origin{0., 0., 0.}; |
| 21 | +glm::vec3 cellWidth{0.1, 0.1, 0.1}; |
| 22 | + |
| 23 | +// list of occupied cells by their integer grid indices |
| 24 | +std::vector<glm::ivec3> occupiedCells = { |
| 25 | + {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, |
| 26 | + {1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1}, |
| 27 | +}; |
| 28 | + |
| 29 | +// register the grid |
| 30 | +polyscope::SparseVolumeGrid* psGrid = polyscope::registerSparseVolumeGrid( |
| 31 | + "sample sparse grid", origin, cellWidth, occupiedCells); |
| 32 | + |
| 33 | +// add a scalar function on the grid (one value per cell) |
| 34 | +std::vector<float> cellScalars(occupiedCells.size()); |
| 35 | +for (size_t i = 0; i < cellScalars.size(); i++) cellScalars[i] = static_cast<float>(i); |
| 36 | + |
| 37 | +psGrid->addCellScalarQuantity("cell scalar", cellScalars); |
| 38 | + |
| 39 | +polyscope::show(); |
| 40 | +``` |
| 41 | +
|
| 42 | +### Registering a sparse volume grid |
| 43 | +
|
| 44 | +!!! note "Origin and cell width" |
| 45 | +
|
| 46 | + The `origin` parameter specifies the position of the node/corner origin of the grid. Cell `(0,0,0)` has its lower-left corner at the origin. If you are thinking in terms of cell centers, shift the origin you pass in by half a cell width: `origin = cellCenter - 0.5 * cellWidth`. |
| 47 | +
|
| 48 | + The `cellWidth` parameter gives the size of each grid cell along each axis. All cells have the same size. |
| 49 | +
|
| 50 | +???+ func "`#!cpp polyscope::registerSparseVolumeGrid(std::string name, glm::vec3 origin, glm::vec3 gridCellWidth, const T& occupiedCells)`" |
| 51 | +
|
| 52 | + Add a new sparse volume grid structure to Polyscope. |
| 53 | +
|
| 54 | + - `name` the name of the structure |
| 55 | + - `origin` a `glm::vec3` giving the xyz position of the node/corner origin |
| 56 | + - `gridCellWidth` a `glm::vec3` giving the width of each cell along each axis |
| 57 | + - `occupiedCells` an array of `glm::ivec3` giving the integer grid indices of all occupied cells. The type should be [adaptable]([[url.prefix]]/data_adaptors) to an array of `glm::ivec3`. |
| 58 | +
|
| 59 | +
|
| 60 | +--- |
| 61 | +
|
| 62 | +### Render modes |
| 63 | +
|
| 64 | +The sparse volume grid supports two render modes, controlled by `SparseVolumeGrid::setRenderMode()`: |
| 65 | +
|
| 66 | +- **`SparseVolumeGridRenderMode::Gridcube`** (default) renders each occupied cell as a filled cube. Quantities are visualized on the cube faces. |
| 67 | +- **`SparseVolumeGridRenderMode::Wireframe`** renders only the grid outline as wireframe edges. This mode is useful for seeing through the grid to inspect its structure. Quantities are not drawn in wireframe mode. |
| 68 | +
|
| 69 | + |
| 70 | +
|
| 71 | +The wireframe appearance can be adjusted with `SparseVolumeGrid::setWireframeRadius()` and `SparseVolumeGrid::setWireframeColor()`. |
| 72 | +
|
| 73 | +```cpp |
| 74 | +polyscope::SparseVolumeGrid* psGrid = polyscope::registerSparseVolumeGrid( |
| 75 | + "sample sparse grid", origin, cellWidth, occupiedCells); |
| 76 | +
|
| 77 | +psGrid->setRenderMode(SparseVolumeGridRenderMode::Wireframe); |
| 78 | +psGrid->setWireframeRadius(0.5); |
| 79 | +psGrid->setWireframeColor(glm::vec3{1.f, 0.f, 0.f}); |
| 80 | +``` |
| 81 | + |
| 82 | +### Slice planes |
| 83 | + |
| 84 | +As shown in the video above, [slice planes]([[url.prefix]]/features/slice_planes) are useful for inspecting the structure of a sparse volume grid. Slice planes can be manipulated programmatically or manually in the GUI; see the slice plane documentation for more details. |
| 85 | + |
| 86 | +### Picking |
| 87 | + |
| 88 | +"Picking" refers to selecting and inspecting elements by clicking on the object in the scene. Picking sparse volume grid elements works similarly to other structures, see [the overview of Selection / Picking]([[url.prefix]]/basics/interactive_UIs_and_animation/#picking-selection-and-querying-the-scene) for general information. |
| 89 | + |
| 90 | +By default, only cells can be selected, until you have added some data on nodes. You can override this behavior by calling `SparseVolumeGrid::markNodesAsUsed()`, to act as if a node quantity had been added. |
| 91 | + |
| 92 | +As with other structures, you can call `interpretPickResult()` to get additional info about a click. |
| 93 | + |
| 94 | +```cpp |
| 95 | +struct SparseVolumeGridPickResult { |
| 96 | + SparseVolumeGridElement elementType; // which kind of element was clicked (enum values: {CELL, NODE}) |
| 97 | + glm::ivec3 cellIndex; // integer grid index of the clicked cell (only populated if cell) |
| 98 | + uint64_t cellFlatIndex; // flat index into the occupied cells array (only populated if cell) |
| 99 | + glm::ivec3 nodeIndex; // integer grid index of the clicked node (only populated if node) |
| 100 | +}; |
| 101 | +``` |
| 102 | +
|
| 103 | +??? func "`#!cpp SparseVolumeGridPickResult SparseVolumeGrid::interpretPickResult(PickResult result)`" |
| 104 | +
|
| 105 | + Get additional information about a click, specific to this structure type (if it was the structure which was clicked on). |
| 106 | +
|
| 107 | +
|
| 108 | +### Options |
| 109 | +
|
| 110 | +See [structure management]([[url.prefix]]/structures/structure_management/#structure-options) for options common to all structures such as enabling/disabling, transforms, and transparency. |
| 111 | +
|
| 112 | +**Parameter** | **Meaning** | **Getter** | **Setter** | **Persistent?** |
| 113 | +--- | --- | --- | --- | --- |
| 114 | +color | the color of the volume | `#!cpp glm::vec3 getColor()` | `#!cpp setColor(glm::vec3 val)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) |
| 115 | +edge width | how thick to draw mesh edges, use `0.` to disable and `1.` for reasonable edges | `#!cpp double getEdgeWidth()` | `#!cpp setEdgeWidth(double val)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) |
| 116 | +edge color | the color of the grid edges | `#!cpp glm::vec3 getEdgeColor()` | `#!cpp setEdgeColor(glm::vec3 val)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) |
| 117 | +cube size factor | shrink factor from 0-1 to draw gaps between cells, 0 is no shrink (default) | `#!cpp double getCubeSizeFactor()` | `#!cpp setCubeSizeFactor(double val)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) |
| 118 | +material | what [material]([[url.prefix]]/features/materials) to use | `#!cpp std::string getMaterial()` | `#!cpp setMaterial(std::string name)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) | |
| 119 | +render mode | how to render the voxels: `Gridcube` (default, filled cubes) or `Wireframe` (grid outline only) | `#!cpp SparseVolumeGridRenderMode getRenderMode()` | `#!cpp setRenderMode(SparseVolumeGridRenderMode mode)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) |
| 120 | +wireframe radius | relative radius for wireframe spheres/cylinders; `1` means radius is half the smallest cell width | `#!cpp double getWireframeRadius()` | `#!cpp setWireframeRadius(double val)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) |
| 121 | +wireframe color | color used for wireframe rendering | `#!cpp glm::vec3 getWireframeColor()` | `#!cpp setWireframeColor(glm::vec3 val)` | [yes]([[url.prefix]]/basics/parameters/#persistent-values) |
0 commit comments