Skip to content

Commit

Permalink
Add tools::activeVoxelCount()
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bailey <danbailey@ilm.com>
  • Loading branch information
danrbailey committed Mar 18, 2021
1 parent fcdf2a2 commit 6a823ab
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions openvdb/openvdb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ set(OPENVDB_LIBRARY_TOOLS_INCLUDE_FILES
tools/ChangeBackground.h
tools/Clip.h
tools/Composite.h
tools/Count.h
tools/Dense.h
tools/DenseSparseTools.h
tools/Diagnostics.h
Expand Down
83 changes: 83 additions & 0 deletions openvdb/openvdb/tools/Count.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
//
/// @file Count.h
///
/// @brief Functions to count tiles, nodes or voxels in a grid
///
/// @author Dan Bailey
///

#ifndef OPENVDB_TOOLS_COUNT_HAS_BEEN_INCLUDED
#define OPENVDB_TOOLS_COUNT_HAS_BEEN_INCLUDED

#include <openvdb/version.h>
#include <openvdb/tree/NodeManager.h>

namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace tools {


/// @brief Return the total number of active voxels in the tree.
template <typename TreeT>
Index64 activeVoxelCount(const TreeT& tree, bool threaded = true);


////////////////////////////////////////


namespace count_internal {

template<typename TreeType>
struct ActiveVoxelCountOp
{
using LeafT = typename TreeType::LeafNodeType;

ActiveVoxelCountOp() = default;
ActiveVoxelCountOp(const ActiveVoxelCountOp&, tbb::split) { }

template<typename NodeT>
bool operator()(const NodeT& node, size_t)
{
for (auto iter = node.cbeginValueOn(); iter; ++iter) {
count += NodeT::ChildNodeType::NUM_VOXELS;
}
return true;
}

bool operator()(const LeafT& leaf, size_t)
{
count += leaf.onVoxelCount();
return false;
}

void join(const ActiveVoxelCountOp& other)
{
count += other.count;
}

openvdb::Index64 count{0};
};

} // namespace count_internal


////////////////////////////////////////


template <typename TreeT>
Index64 activeVoxelCount(const TreeT& tree, bool threaded)
{
count_internal::ActiveVoxelCountOp<TreeT> op;
tree::DynamicNodeManager<const TreeT> nodeManager(tree);
nodeManager.reduceTopDown(op, threaded);
return op.count;
}

} // namespace tools
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb

#endif // OPENVDB_TOOLS_COUNT_HAS_BEEN_INCLUDED
1 change: 1 addition & 0 deletions openvdb/openvdb/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(UNITTEST_SOURCE_FILES
TestClip.cc
TestConjGradient.cc
TestCoord.cc
TestCount.cc
TestCpt.cc
TestCurl.cc
TestDelayedLoadMetadata.cc
Expand Down
30 changes: 30 additions & 0 deletions openvdb/openvdb/unittest/TestCount.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0

#include "gtest/gtest.h"

#include <openvdb/openvdb.h>
#include <openvdb/tools/Count.h>
#include <openvdb/tools/LevelSetSphere.h>


class TestCount: public ::testing::Test
{
};


////////////////////////////////////////


TEST_F(TestCount, testActiveVoxelCount)
{
using namespace openvdb;

auto grid = tools::createLevelSetSphere<FloatGrid>(10.0f, Vec3f(0), 0.1f);

Index64 activeVoxelCount1 = grid->tree().activeVoxelCount();

Index64 activeVoxelCount2 = tools::activeVoxelCount(grid->tree());

EXPECT_EQ(activeVoxelCount1, activeVoxelCount2);
}

0 comments on commit 6a823ab

Please sign in to comment.