|
| 1 | +// Copyright Contributors to the OpenVDB Project |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include <openvdb/openvdb.h> |
| 5 | +#include <openvdb/tools/LevelSetFilter.h> |
| 6 | +#include <openvdb/tools/LevelSetSphere.h> |
| 7 | +#include <openvdb/io/File.h> |
| 8 | +#include <openvdb/tools/Composite.h> // for csgUnion() |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | + |
| 12 | +using namespace openvdb; |
| 13 | + |
| 14 | +class TestLevelSetFilter: public ::testing::Test |
| 15 | +{ |
| 16 | +public: |
| 17 | + void SetUp() override { openvdb::initialize(); } |
| 18 | + void TearDown() override { openvdb::uninitialize(); } |
| 19 | + |
| 20 | + void testLevelSetFillet(); |
| 21 | +}; // class TestLevelSetFilter |
| 22 | + |
| 23 | + |
| 24 | +//////////////////////////////////////// |
| 25 | + |
| 26 | + |
| 27 | +TEST_F(TestLevelSetFilter, testLevelSetFillet) |
| 28 | +{ |
| 29 | + using GridT = FloatGrid; |
| 30 | + using FilterT = tools::LevelSetFilter<GridT>; |
| 31 | + |
| 32 | + const float radius = 5.0f; |
| 33 | + const float voxelSize = 1.0f; |
| 34 | + const float halfWidth = 3.0f; |
| 35 | + typename GridT::Ptr sdfGrid = tools::createLevelSetSphere<GridT>(/*radius=*/radius, |
| 36 | + /*center=*/Vec3f(-radius, 0.0f, 0.0f), |
| 37 | + /*dx=*/voxelSize, /*halfWidth*/ halfWidth); |
| 38 | + typename GridT::Ptr sdfGridB = tools::createLevelSetSphere<GridT>(/*radius=*/radius, |
| 39 | + /*center=*/Vec3f(radius, 0.0f, 0.0f), |
| 40 | + /*dx=*/voxelSize, /*halfWidth*/ halfWidth); |
| 41 | + typename GridT::Accessor acc = sdfGrid->getAccessor(); |
| 42 | + |
| 43 | + EXPECT_TRUE(sdfGrid); |
| 44 | + EXPECT_TRUE(sdfGridB); |
| 45 | + |
| 46 | + tools::csgUnion(*sdfGrid, *sdfGridB); |
| 47 | + |
| 48 | + { |
| 49 | + EXPECT_TRUE(sdfGrid); |
| 50 | + |
| 51 | + Coord ijk(0, 3, 0); |
| 52 | + |
| 53 | + // We expect that the intersection between the two spheres are at (0, 0, 0) |
| 54 | + // so the SDF value of the union in these offsets locations should be > 0 |
| 55 | + EXPECT_TRUE(acc.getValue(ijk) > 0.f); |
| 56 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0, 0, 1)) > 0.0f); |
| 57 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0, 0,-1)) > 0.0f); |
| 58 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0,-1, 0)) > 0.0f); |
| 59 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0,-1, 1)) > 0.0f); |
| 60 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0,-1,-1)) > 0.0f); |
| 61 | + } |
| 62 | + |
| 63 | + FilterT filter(*sdfGrid); |
| 64 | + filter.fillet(); |
| 65 | + |
| 66 | + { |
| 67 | + EXPECT_TRUE(sdfGrid); |
| 68 | + |
| 69 | + Coord ijk(0, 3, 0); |
| 70 | + |
| 71 | + // After the fillet operation, we expect that the zero-isocontour is |
| 72 | + // pushed outward. |
| 73 | + EXPECT_TRUE(acc.getValue(ijk) < 0.f); |
| 74 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0, 0, 1)) < 0.0f); |
| 75 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0, 0,-1)) < 0.0f); |
| 76 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0,-1, 0)) < 0.0f); |
| 77 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0,-1, 1)) < 0.0f); |
| 78 | + EXPECT_TRUE(acc.getValue(ijk.offsetBy(0,-1,-1)) < 0.0f); |
| 79 | + } |
| 80 | +} |
0 commit comments