forked from AcademySoftwareFoundation/openvdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dan Bailey <danbailey@ilm.com>
- Loading branch information
1 parent
fcdf2a2
commit 6a823ab
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |