DGtal 2.1.0
Loading...
Searching...
No Matches
benchmarkDigitalSetByOctree.cpp
1#include "DGtal/helpers/StdDefs.h"
2#include "DGtal/helpers/Shortcuts.h"
3#include "DGtal/io/readers/VolReader.h"
4#include "DGtal/kernel/sets/DigitalSetByOctree.h"
5
6#include "DGtalCatch.h"
7
8using namespace DGtal;
9using namespace std;
10
11using SH3 = Shortcuts<Z3i::KSpace>;
14
15const auto CountingFunction = [](Z3i::Point, const std::vector<Z3i::Point>& neighborhood) {
16 return neighborhood.size();
17};
18
19// Context for each benchmark
21{
22 inline static std::vector<std::vector<Z3i::Point>> shapes;
23 inline static std::vector<Z3i::Domain> domains;
24 inline static std::vector<std::string> names;
25
27 if (shapes.size() != 0) return;
28
29 const auto polys = SH3::getPolynomialList();
30 const double steps[] = { 1.00, 0.50, 0.25 };
31 const double noises[] = { 0.00, 0.10 };
32
33 for (const auto& poly : polys) {
34 for (const auto& step : steps) {
35 for (const auto& noise : noises) {
36
37 auto params = SH3::defaultParameters();
38 params( "polynomial", poly.second )
39 ( "gridstep", step)
40 ( "noise", noise);
41
42 auto ishape = SH3::makeImplicitShape3D(params);
43 auto dshape = SH3::makeDigitizedImplicitShape3D(ishape, params);
44 auto nshape = SH3::makeBinaryImage(dshape, params);
45
46 const std::string name = poly.first + "_" + std::to_string(step) + "_" + std::to_string(noise);
47 const auto domain = nshape->domain();
48
49 names.push_back(name);
50 domains.push_back(nshape->domain());
51 shapes.push_back({});
52
53 for (auto it = domain.begin(); it != domain.end(); ++it) {
54 if (nshape->operator()(*it) != 0) {
55 shapes.back().push_back(*it);
56 }
57 }
58 }
59 }
60 }
61
62 std::cout << "Shapes: " << std::endl;
63 for (const auto& n : names) {
64 std::cout << "\t -" << n << std::endl;
65 }
66 }
67};
68
69TEST_CASE_METHOD(BenchInfo, "Benchmarking DigitalSetByOctree using Catch2", "[catch]")
70{
71 auto i = GENERATE(range(0, (int)names.size()));
72
73 const auto& name = names[i];
74 const auto& domain = domains[i];
75 const auto& pts = shapes[i];
76
77 Octree tree(domain);
78 for (const auto& pt : pts) {
79 tree.insert(pt);
80 }
81
82
83 BENCHMARK("Octree insert: " + name)
84 {
85 Octree subtree(domain);
86 for (const auto& pt : pts) {
87 subtree.insert(pt);
88 }
89 };
90
91 BENCHMARK_ADVANCED("Octree To DAG: " + name)(Catch::Benchmark::Chronometer meter)
92 {
93 Octree subtree = tree;
94 // Only measure conversion, not copy
95 meter.measure([&] {
96 subtree.convertToDAG();
97 });
98 };
99
100 BENCHMARK("Iterating octree: " + name)
101 {
102 size_t count = 0;
103 for (auto it = tree.begin(); it != tree.end(); ++it) {
104 count++;
105 }
106 return count;
107 };
108
109 size_t oldMemory = tree.memoryFootprint();
110 tree.convertToDAG();
111 size_t count = 0; for (auto it = tree.begin(); it != tree.end(); ++it) count++;
112
113 std::cout << "\n\n Stats:\n" << std::endl;
114 std::cout << "\t Domain: " << tree.domain() << std::endl;
115 std::cout << "\t Memory footprint (octree): " << oldMemory << " bytes." << std::endl;
116 std::cout << "\t Memory footprint (dag) : " << tree.memoryFootprint() << " bytes." << std::endl;
117 std::cout << "\t Voxel count (source): " << pts.size() << std::endl;
118 std::cout << "\t Voxel count (dag) : " << count << std::endl;
119 std::cout << std::endl;
120
121 BENCHMARK("Counting number of voxel: " + name)
122 {
123 tree.computeFunction(tree.begin(), tree.end(), 1, CountingFunction);
124 };
125};
A DigitalSet that stores voxels as an octree, or a DAG.
void convertToDAG()
Converts the octree to DAG.
Aim: implements association bewteen points lying in a digital domain and values.
Definition Image.h:70
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition Shortcuts.h:102
static std::map< std::string, std::string > getPolynomialList()
Definition Shortcuts.h:233
static CountedPtr< DigitizedImplicitShape3D > makeDigitizedImplicitShape3D(CountedPtr< ImplicitShape3D > shape, Parameters params=parametersDigitizedImplicitShape3D())
Definition Shortcuts.h:520
static Parameters defaultParameters()
Definition Shortcuts.h:200
static CountedPtr< BinaryImage > makeBinaryImage(Domain shapeDomain)
Definition Shortcuts.h:558
static CountedPtr< ImplicitShape3D > makeImplicitShape3D(const Parameters &params=parametersImplicitShape3D())
Definition Shortcuts.h:279
Space::Point Point
Definition StdDefs.h:168
DGtal is the top-level namespace which contains all DGtal functions and types.
STL namespace.
static std::vector< std::vector< Z3i::Point > > shapes
static std::vector< Z3i::Domain > domains
static std::vector< std::string > names
BENCHMARK(BM_StringCreation)
Domain domain