|
12 | 12 | #include "polyscope/point_cloud.h" |
13 | 13 | #include "polyscope/render/managed_buffer.h" |
14 | 14 | #include "polyscope/simple_triangle_mesh.h" |
| 15 | +#include "polyscope/sparse_volume_grid.h" |
15 | 16 | #include "polyscope/surface_mesh.h" |
16 | 17 | #include "polyscope/types.h" |
17 | 18 | #include "polyscope/view.h" |
18 | 19 | #include "polyscope/volume_grid.h" |
19 | 20 | #include "polyscope/volume_mesh.h" |
20 | 21 |
|
21 | 22 | #include <iostream> |
| 23 | +#include <set> |
22 | 24 | #include <unordered_set> |
23 | 25 | #include <utility> |
24 | 26 |
|
@@ -544,6 +546,90 @@ void addVolumeGrid() { |
544 | 546 | } |
545 | 547 |
|
546 | 548 |
|
| 549 | +void addSparseVolumeGrid() { |
| 550 | + glm::vec3 origin{-1., -1., -1.}; |
| 551 | + int32_t N = 20; |
| 552 | + glm::vec3 cellWidth{1.0f / N, 1.0f / N, 1.0f / N}; |
| 553 | + |
| 554 | + // Build a spherical shell of occupied cells |
| 555 | + std::vector<glm::ivec3> occupiedCells; |
| 556 | + for (int i = -N; i <= N; i++) { |
| 557 | + for (int j = -N; j <= N; j++) { |
| 558 | + for (int k = -N; k <= N; k++) { |
| 559 | + glm::vec3 cellCenter = origin + (glm::vec3(i, j, k) + 0.5f) * cellWidth; |
| 560 | + float dist = glm::length(cellCenter - origin); |
| 561 | + if (dist >= 0.35f && dist <= 1.0f) { |
| 562 | + occupiedCells.push_back({i, j, k}); |
| 563 | + } |
| 564 | + } |
| 565 | + } |
| 566 | + } |
| 567 | + // Gather all unique nodes for the occupied cells |
| 568 | + // Node (ci+dx, cj+dy, ck+dz) for dx,dy,dz in {0,1} |
| 569 | + std::set<std::tuple<int, int, int>> nodeSet; |
| 570 | + for (const auto& ci : occupiedCells) { |
| 571 | + for (int dx = 0; dx < 2; dx++) { |
| 572 | + for (int dy = 0; dy < 2; dy++) { |
| 573 | + for (int dz = 0; dz < 2; dz++) { |
| 574 | + nodeSet.insert({ci.x + dx, ci.y + dy, ci.z + dz}); |
| 575 | + } |
| 576 | + } |
| 577 | + } |
| 578 | + } |
| 579 | + |
| 580 | + std::cout << "adding sparse volume grid with " << occupiedCells.size() << " cells" << std::endl; |
| 581 | + polyscope::SparseVolumeGrid* psGrid = |
| 582 | + polyscope::registerSparseVolumeGrid("sparse grid", origin, cellWidth, occupiedCells); |
| 583 | + |
| 584 | + // Cell scalar: distance from origin |
| 585 | + { |
| 586 | + std::vector<float> cellDist(occupiedCells.size()); |
| 587 | + for (size_t i = 0; i < occupiedCells.size(); i++) { |
| 588 | + glm::vec3 cellCenter = origin + (glm::vec3(occupiedCells[i]) + 0.5f) * cellWidth; |
| 589 | + cellDist[i] = glm::length(cellCenter - origin); |
| 590 | + } |
| 591 | + psGrid->addCellScalarQuantity("cell distance", cellDist); |
| 592 | + } |
| 593 | + |
| 594 | + // Node scalar: x-coordinate at node positions |
| 595 | + { |
| 596 | + |
| 597 | + std::vector<glm::ivec3> nodeIndices; |
| 598 | + std::vector<float> nodeValues; |
| 599 | + for (const auto& nodeInds : nodeSet) { |
| 600 | + int32_t ni, nj, nk; |
| 601 | + std::tie(ni, nj, nk) = nodeInds; |
| 602 | + nodeIndices.push_back({ni, nj, nk}); |
| 603 | + glm::vec3 nodePos = origin + glm::vec3(ni, nj, nk) * cellWidth; |
| 604 | + nodeValues.push_back(nodePos.x); |
| 605 | + } |
| 606 | + psGrid->addNodeScalarQuantity("node x-coord", nodeIndices, nodeValues); |
| 607 | + } |
| 608 | + |
| 609 | + // Cell color: random colors |
| 610 | + { |
| 611 | + std::vector<glm::vec3> cellColors(occupiedCells.size()); |
| 612 | + for (size_t i = 0; i < occupiedCells.size(); i++) { |
| 613 | + cellColors[i] = glm::vec3{polyscope::randomUnit(), polyscope::randomUnit(), polyscope::randomUnit()}; |
| 614 | + } |
| 615 | + psGrid->addCellColorQuantity("cell color", cellColors); |
| 616 | + } |
| 617 | + |
| 618 | + // Node color: random colors |
| 619 | + { |
| 620 | + std::vector<glm::ivec3> nodeIndices; |
| 621 | + std::vector<glm::vec3> nodeColors; |
| 622 | + for (const auto& nodeInds : nodeSet) { |
| 623 | + int32_t ni, nj, nk; |
| 624 | + std::tie(ni, nj, nk) = nodeInds; |
| 625 | + nodeIndices.push_back({ni, nj, nk}); |
| 626 | + nodeColors.push_back(glm::vec3{polyscope::randomUnit(), polyscope::randomUnit(), polyscope::randomUnit()}); |
| 627 | + } |
| 628 | + psGrid->addNodeColorQuantity("node color", nodeIndices, nodeColors); |
| 629 | + } |
| 630 | +} |
| 631 | + |
| 632 | + |
547 | 633 | void loadFloatingImageData(polyscope::CameraView* targetView = nullptr) { |
548 | 634 |
|
549 | 635 | // load an image from disk as example data |
@@ -864,6 +950,10 @@ void callback() { |
864 | 950 | addVolumeGrid(); |
865 | 951 | } |
866 | 952 |
|
| 953 | + if (ImGui::Button("add sparse volume grid")) { |
| 954 | + addSparseVolumeGrid(); |
| 955 | + } |
| 956 | + |
867 | 957 | // ImPlot |
868 | 958 | // dummy data |
869 | 959 | if (ImGui::TreeNode("ImPlot")) { |
@@ -911,7 +1001,7 @@ int main(int argc, char** argv) { |
911 | 1001 | // polyscope::view::windowWidth = 600; |
912 | 1002 | // polyscope::view::windowHeight = 800; |
913 | 1003 | // polyscope::options::maxFPS = -1; |
914 | | - polyscope::options::verbosity = 101; |
| 1004 | + polyscope::options::verbosity = 200; |
915 | 1005 | polyscope::options::enableRenderErrorChecks = true; |
916 | 1006 | polyscope::options::allowHeadlessBackends = true; |
917 | 1007 |
|
|
0 commit comments