File: TestFastSweeping.cc

package info (click to toggle)
openvdb 5.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,132 kB
  • sloc: cpp: 110,785; ansic: 5,195; makefile: 845; python: 518
file content (390 lines) | stat: -rw-r--r-- 19,136 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2018 DreamWorks Animation LLC
//
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
//
// Redistributions of source code must retain the above copyright
// and license notice and the following restrictions and disclaimer.
//
// *     Neither the name of DreamWorks Animation nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
// LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
//
///////////////////////////////////////////////////////////////////////////

//#define FAST_SWEEPING_DEBUG

#include <sstream>
#include <cppunit/extensions/HelperMacros.h>
#include <openvdb/Types.h>
#include <openvdb/openvdb.h>
#include <openvdb/tools/ChangeBackground.h>
#include <openvdb/tools/Diagnostics.h>
#include <openvdb/tools/FastSweeping.h>
#include <openvdb/tools/LevelSetSphere.h>
#include <openvdb/tools/LevelSetTracker.h>
#include <openvdb/tools/LevelSetRebuild.h>
#include <openvdb/tools/LevelSetPlatonic.h>
#include <openvdb/tools/LevelSetUtil.h>

#ifdef FAST_SWEEPING_DEBUG
#include <openvdb/util/CpuTimer.h>
// Uncomment to test on models from our web-site
//#define TestFastSweeping_DATA_PATH "/home/kmu/src/data/vdb/"
//#define TestFastSweeping_DATA_PATH "/usr/pic1/Data/OpenVDB/LevelSetModels/"
#endif


class TestFastSweeping: public CppUnit::TestFixture
{
public:
    virtual void setUp() { openvdb::initialize(); }
    virtual void tearDown() { openvdb::uninitialize(); }

    CPPUNIT_TEST_SUITE(TestFastSweeping);
    CPPUNIT_TEST(testDilateSDF);
    CPPUNIT_TEST(testExtendSDF);
    CPPUNIT_TEST(testMakeSDF);

    CPPUNIT_TEST_SUITE_END();

    void testDilateSDF();
    void testExtendSDF();
    void testMakeSDF();
    
    void write(const std::string &name, openvdb::FloatGrid::Ptr grid);
    
};// TestFastSweeping

CPPUNIT_TEST_SUITE_REGISTRATION(TestFastSweeping);

void
TestFastSweeping::write(const std::string &name, openvdb::FloatGrid::Ptr grid)
{
    openvdb::GridPtrVec grids;
    grids.push_back(grid);
    openvdb::io::File file(name);
    file.write(grids);
}

void
TestFastSweeping::testDilateSDF()
{
    using namespace openvdb;
    // Define parameters for the level set sphere to be re-normalized
    const float radius = 200.0f;
    const Vec3f center(0.0f, 0.0f, 0.0f);
    const float voxelSize = 1.0f;//half width
    const int width = 3, new_width = 50;//half width

    FloatGrid::Ptr grid = tools::createLevelSetSphere<FloatGrid>(radius, center, voxelSize, float(width));
    const size_t oldVoxelCount = grid->activeVoxelCount();

    tools::FastSweeping<FloatGrid> fs(*grid);
    CPPUNIT_ASSERT_EQUAL(size_t(0), fs.sweepingVoxelCount());
    CPPUNIT_ASSERT_EQUAL(size_t(0), fs.boundaryVoxelCount());
    fs.initDilateSDF(new_width - width);
    CPPUNIT_ASSERT(fs.sweepingVoxelCount() > 0);
    CPPUNIT_ASSERT(fs.boundaryVoxelCount() > 0);
    fs.sweep();
    CPPUNIT_ASSERT(fs.sweepingVoxelCount() > 0);
    CPPUNIT_ASSERT(fs.boundaryVoxelCount() > 0);
    fs.clear();
    CPPUNIT_ASSERT_EQUAL(size_t(0), fs.sweepingVoxelCount());
    CPPUNIT_ASSERT_EQUAL(size_t(0), fs.boundaryVoxelCount());
    const size_t sweepingVoxelCount = grid->activeVoxelCount();
    CPPUNIT_ASSERT(sweepingVoxelCount > oldVoxelCount);

    {// Check that the norm of the gradient for all active voxels is close to unity
        tools::Diagnose<FloatGrid> diagnose(*grid);
        tools::CheckNormGrad<FloatGrid> test(*grid, 0.99f, 1.01f);
        const std::string message = diagnose.check(test,
                                                   false,// don't generate a mask grid
                                                   true,// check active voxels
                                                   false,// ignore active tiles since a level set has none
                                                   false);// no need to check the background value
        CPPUNIT_ASSERT(message.empty());
        CPPUNIT_ASSERT_EQUAL(size_t(0), diagnose.failureCount());
        //std::cout << "\nOutput 1: " << message << std::endl;
    }
    {// Make sure all active voxels fail the following test
        tools::Diagnose<FloatGrid> diagnose(*grid);
        tools::CheckNormGrad<FloatGrid> test(*grid, std::numeric_limits<float>::min(), 0.99f);
        const std::string message = diagnose.check(test,
                                                   false,// don't generate a mask grid
                                                   true,// check active voxels
                                                   false,// ignore active tiles since a level set has none
                                                   false);// no need to check the background value
        CPPUNIT_ASSERT(!message.empty());
        CPPUNIT_ASSERT_EQUAL(sweepingVoxelCount, diagnose.failureCount());
        //std::cout << "\nOutput 2: " << message << std::endl;
    }
    {// Make sure all active voxels fail the following test
        tools::Diagnose<FloatGrid> diagnose(*grid);
        tools::CheckNormGrad<FloatGrid> test(*grid, 1.01f, std::numeric_limits<float>::max());
        const std::string message = diagnose.check(test,
                                                   false,// don't generate a mask grid
                                                   true,// check active voxels
                                                   false,// ignore active tiles since a level set has none
                                                   false);// no need to check the background value
        CPPUNIT_ASSERT(!message.empty());
        CPPUNIT_ASSERT_EQUAL(sweepingVoxelCount, diagnose.failureCount());
        //std::cout << "\nOutput 3: " << message << std::endl;
    }
}// testDilateSDF


void
TestFastSweeping::testExtendSDF()
{
    using namespace openvdb;
    // Define parameterS FOR the level set sphere to be re-normalized
    const float radius = 200.0f;
    const Vec3f center(0.0f, 0.0f, 0.0f);
    const float voxelSize = 1.0f, width = 3.0f;//half width
    const float new_width = 50;
    
    {// Use box as a mask
        //std::cerr << "\nUse box as a mask" << std::endl;
        FloatGrid::Ptr grid = tools::createLevelSetSphere<FloatGrid>(radius, center, voxelSize, width);
        CoordBBox bbox(Coord(150,-50,-50), Coord(250,50,50));
        MaskGrid mask;
        mask.sparseFill(bbox, true);
#ifdef FAST_SWEEPING_DEBUG
        this->write("/tmp/box_mask_input.vdb", grid);
        util::CpuTimer timer("\nParallel sparse fast sweeping with a box mask");
#endif

        tools::extendSDF(*grid, mask);
        //tools::FastSweeping<FloatGrid> fs(*grid);
        //fs.initExtendSDF(mask);
        //fs.sweep();
        //std::cerr << "voxel count = " << fs.sweepingVoxelCount() << std::endl;
        //std::cerr << "boundary count = " << fs.boundaryVoxelCount() << std::endl;
        //CPPUNIT_ASSERT(fs.sweepingVoxelCount() > 0);
#ifdef FAST_SWEEPING_DEBUG
        timer.stop();
        this->write("/tmp/box_mask_output.vdb", grid);
#endif
       
        {// Check that the norm of the gradient for all active voxels is close to unity
            tools::Diagnose<FloatGrid> diagnose(*grid);
            tools::CheckNormGrad<FloatGrid> test(*grid, 0.99f, 1.01f);
            const std::string message = diagnose.check(test,
                                                       false,// don't generate a mask grid
                                                       true,// check active voxels
                                                       false,// ignore active tiles since a level set has none
                                                       false);// no need to check the background value
            //std::cerr << message << std::endl;
            const double percent = 100.0*double(diagnose.failureCount())/double(grid->activeVoxelCount());
            //std::cerr << "Failures = " << percent << "%" << std::endl;
            //std::cerr << "Failed: " << diagnose.failureCount() << std::endl;
            //std::cerr << "Total : " << grid->activeVoxelCount() << std::endl;
            CPPUNIT_ASSERT(percent < 0.01);
            //CPPUNIT_ASSERT(message.empty());
            //CPPUNIT_ASSERT_EQUAL(size_t(0), diagnose.failureCount());
        }
    }

    {// Use sphere as a mask
        //std::cerr << "\nUse sphere as a mask" << std::endl;
        FloatGrid::Ptr grid = tools::createLevelSetSphere<FloatGrid>(radius, center, voxelSize, width);
        FloatGrid::Ptr mask = tools::createLevelSetSphere<FloatGrid>(radius, center, voxelSize, new_width);
#ifdef  FAST_SWEEPING_DEBUG
        this->write("/tmp/sphere_mask_input.vdb", grid);
        util::CpuTimer timer("\nParallel sparse fast sweeping with a sphere mask");
#endif
        tools::extendSDF(*grid, *mask);
        //tools::FastSweeping<FloatGrid> fs(*grid);
        //fs.initExtendSDF(*mask);
        //fs.sweep();
#ifdef FAST_SWEEPING_DEBUG
        timer.stop();
        this->write("/tmp/sphere_mask_output.vdb", grid);
#endif
        //std::cerr << "voxel count = " << fs.sweepingVoxelCount() << std::endl;
        //std::cerr << "boundary count = " << fs.boundaryVoxelCount() << std::endl;
        //CPPUNIT_ASSERT(fs.sweepingVoxelCount() > 0);
        
        {// Check that the norm of the gradient for all active voxels is close to unity
            tools::Diagnose<FloatGrid> diagnose(*grid);
            tools::CheckNormGrad<FloatGrid> test(*grid, 0.99f, 1.01f);
            const std::string message = diagnose.check(test,
                                                       false,// don't generate a mask grid
                                                       true,// check active voxels
                                                       false,// ignore active tiles since a level set has none
                                                       false);// no need to check the background value
            //std::cerr << message << std::endl;
            const double percent = 100.0*double(diagnose.failureCount())/double(grid->activeVoxelCount());
            //std::cerr << "Failures = " << percent << "%" << std::endl;
            //std::cerr << "Failed: " << diagnose.failureCount() << std::endl;
            //std::cerr << "Total : " << grid->activeVoxelCount() << std::endl;
            //CPPUNIT_ASSERT(message.empty());
            //CPPUNIT_ASSERT_EQUAL(size_t(0), diagnose.failureCount());
            CPPUNIT_ASSERT(percent < 0.01);
            //std::cout << "\nOutput 1: " << message << std::endl;
        }
    }
    
    {// Use dodecahedron as a mask
        //std::cerr << "\nUse dodecahedron as a mask" << std::endl;
        FloatGrid::Ptr grid = tools::createLevelSetSphere<FloatGrid>(radius, center, voxelSize, width);
        FloatGrid::Ptr mask = tools::createLevelSetDodecahedron<FloatGrid>(50, Vec3f(radius, 0.0f, 0.0f),
                                                                           voxelSize, 10);
#ifdef FAST_SWEEPING_DEBUG
        this->write("/tmp/dodecahedron_mask_input.vdb", grid);
        util::CpuTimer timer("\nParallel sparse fast sweeping with a dodecahedron mask");
#endif
        
        tools::extendSDF(*grid, *mask);
        //tools::FastSweeping<FloatGrid> fs(*grid);
        //fs.initExtendSDF(*mask);
        //std::cerr << "voxel count = " << fs.sweepingVoxelCount() << std::endl;
        //std::cerr << "boundary count = " << fs.boundaryVoxelCount() << std::endl;
        //CPPUNIT_ASSERT(fs.sweepingVoxelCount() > 0);
        //fs.sweep();
        //fs.clear();
#ifdef FAST_SWEEPING_DEBUG
        timer.stop();
        this->write("/tmp/dodecahedron_mask_output.vdb", grid);
#endif
       
        {// Check that the norm of the gradient for all active voxels is close to unity
            tools::Diagnose<FloatGrid> diagnose(*grid);
            tools::CheckNormGrad<FloatGrid> test(*grid, 0.99f, 1.01f);
            const std::string message = diagnose.check(test,
                                                       false,// don't generate a mask grid
                                                       true,// check active voxels
                                                       false,// ignore active tiles since a level set has none
                                                       false);// no need to check the background value
            //std::cerr << message << std::endl;
            const double percent = 100.0*double(diagnose.failureCount())/double(grid->activeVoxelCount());
            //std::cerr << "Failures = " << percent << "%" << std::endl;
            //std::cerr << "Failed: " << diagnose.failureCount() << std::endl;
            //std::cerr << "Total : " << grid->activeVoxelCount() << std::endl;
            //CPPUNIT_ASSERT(message.empty());
            //CPPUNIT_ASSERT_EQUAL(size_t(0), diagnose.failureCount());
            CPPUNIT_ASSERT(percent < 0.01);
            //std::cout << "\nOutput 1: " << message << std::endl;
        }
    }
#ifdef TestFastSweeping_DATA_PATH
     {// Use bunny as a mask
         //std::cerr << "\nUse bunny as a mask" << std::endl;
         FloatGrid::Ptr grid = tools::createLevelSetSphere<FloatGrid>(10.0f, Vec3f(-10,0,0), 0.05f, width);
         openvdb::initialize();//required whenever I/O of OpenVDB files is performed!
         const std::string path(TestFastSweeping_DATA_PATH);
         io::File file( path + "bunny.vdb" );
         file.open(false);//disable delayed loading
         FloatGrid::Ptr mask = openvdb::gridPtrCast<openvdb::FloatGrid>(file.getGrids()->at(0));
         
         this->write("/tmp/bunny_mask_input.vdb", grid);
         tools::FastSweeping<FloatGrid> fs(*grid);
         util::CpuTimer timer("\nParallel sparse fast sweeping with a bunny mask");

         fs.initExtendSDF(*mask);
         //std::cerr << "voxel count = " << fs.sweepingVoxelCount() << std::endl;
         //std::cerr << "boundary count = " << fs.boundaryVoxelCount() << std::endl;
         fs.sweep();
         fs.clear();
         timer.stop();

         this->write("/tmp/bunny_mask_output.vdb", grid);
         {// Check that the norm of the gradient for all active voxels is close to unity
             tools::Diagnose<FloatGrid> diagnose(*grid);
             tools::CheckNormGrad<FloatGrid> test(*grid, 0.99f, 1.01f);
             const std::string message = diagnose.check(test,
                                                        false,// don't generate a mask grid
                                                        true,// check active voxels
                                                        false,// ignore active tiles since a level set has none
                                                        false);// no need to check the background value
             //std::cerr << message << std::endl;
             const double percent = 100.0*double(diagnose.failureCount())/double(grid->activeVoxelCount());
             //std::cerr << "Failures = " << percent << "%" << std::endl;
             //std::cerr << "Failed: " << diagnose.failureCount() << std::endl;
             //std::cerr << "Total : " << grid->activeVoxelCount() << std::endl;
             //CPPUNIT_ASSERT(message.empty());
             //CPPUNIT_ASSERT_EQUAL(size_t(0), diagnose.failureCount());
             CPPUNIT_ASSERT(percent < 4.5);// crossing characteristics!
             //std::cout << "\nOutput 1: " << message << std::endl;
         }
     }
#endif
}// testExtendSDF

void
TestFastSweeping::testMakeSDF()
{
    using namespace openvdb;
    // Define parameterS FOR the level set sphere to be re-normalized
    const float radius = 200.0f;
    const Vec3f center(0.0f, 0.0f, 0.0f);
    const float voxelSize = 1.0f, width = 3.0f;//half width

    FloatGrid::Ptr grid = tools::createLevelSetSphere<FloatGrid>(radius, center, voxelSize, float(width));
    tools::sdfToFogVolume(*grid);
    const size_t sweepingVoxelCount = grid->activeVoxelCount();
#ifdef FAST_SWEEPING_DEBUG
    this->write("/tmp/ls_input.vdb", grid);
#endif
    tools::FastSweeping<FloatGrid> fs(*grid);
#ifdef FAST_SWEEPING_DEBUG
    util::CpuTimer timer("\nParallel sparse fast sweeping with a fog volume");
#endif
    fs.initMakeSDF(0.5);
    CPPUNIT_ASSERT(fs.sweepingVoxelCount() > 0);
    //std::cerr << "voxel count = " << fs.sweepingVoxelCount() << std::endl;
    //std::cerr << "boundary count = " << fs.boundaryVoxelCount() << std::endl;
    fs.sweep();
#ifdef FAST_SWEEPING_DEBUG
    timer.restart("getSweepingGrid");
#endif
    auto tmp = fs.getSweepingGrid();
#ifdef FAST_SWEEPING_DEBUG
    timer.stop();
#endif
    fs.clear();
    CPPUNIT_ASSERT(sweepingVoxelCount > tmp->activeVoxelCount());
#ifdef FAST_SWEEPING_DEBUG
    this->write("/tmp/sweepingGrid.vdb", tmp);
#endif
    CPPUNIT_ASSERT_EQUAL(sweepingVoxelCount, grid->activeVoxelCount());
#ifdef FAST_SWEEPING_DEBUG
    this->write("/tmp/fog_output.vdb", grid);
#endif
    {// Check that the norm of the gradient for all active voxels is close to unity
        tools::Diagnose<FloatGrid> diagnose(*grid);
        tools::CheckNormGrad<FloatGrid> test(*grid, 0.99f, 1.01f);
        const std::string message = diagnose.check(test,
                                                   false,// don't generate a mask grid
                                                   true,// check active voxels
                                                   false,// ignore active tiles since a level set has none
                                                   false);// no need to check the background value
        //std::cerr << message << std::endl;
        const double percent = 100.0*double(diagnose.failureCount())/double(grid->activeVoxelCount());
        //std::cerr << "Failures = " << percent << "%" << std::endl;
        //std::cerr << "Failed: " << diagnose.failureCount() << std::endl;
        //std::cerr << "Total : " << grid->activeVoxelCount() << std::endl;
        CPPUNIT_ASSERT(percent < 3.0);
    }
}// testMakeSDF

// Copyright (c) 2012-2018 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )