DGtal  1.3.beta
testArithmeticalDSSComputerOnSurfels.cpp
Go to the documentation of this file.
1 
30 #include <iostream>
31 #include <string>
32 #include <iterator>
33 
34 #include "DGtal/base/Common.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/helpers/Shortcuts.h"
37 #include "DGtal/topology/DigitalSurface2DSlice.h"
38 #include "ConfigTest.h"
39 #include "DGtalCatch.h"
40 
41 #include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
42 #include "DGtal/geometry/surfaces/ArithmeticalDSSComputerOnSurfels.h"
43 #include "DGtal/geometry/curves/SaturatedSegmentation.h"
44 
45 using namespace std;
46 using namespace DGtal;
47 
51 
54 
57 
58 struct Slice
59 {
60  Dimension dim1;
61  Dimension dim2;
62  Surfel start;
63  std::vector<Surfel> contour;
64 };
65 
66 std::pair<KSpace, Slice> getSlice (std::string const& shape = "ellipsoid", double gridstep = 1.0)
67 {
69 
70  auto params = SH3::defaultParameters();
71  params("polynomial", shape)("gridstep", gridstep);
72 
73  auto implicit_shape = SH3::makeImplicitShape3D(params);
74  auto digitized_shape = SH3::makeDigitizedImplicitShape3D(implicit_shape, params);
75  auto binary_image = SH3::makeBinaryImage(digitized_shape, params);
76  auto kspace = SH3::getKSpace(binary_image, params);
77  auto surface = SH3::makeDigitalSurface(binary_image, kspace, params);
78 
79  Surfel surfel = Surfaces<KSpace>::findABel(kspace, *binary_image, 10000);
80 
81  KSpace::DirIterator q1 = kspace.sDirs(surfel);
82  Dimension dim1 = *q1, dim2 = kspace.sOrthDir(surfel);
83  auto tracker = surface->container().newTracker(surfel);
84  SurfaceSlice surfaceSlice(tracker, dim1);
85  delete tracker;
86 
87  std::vector<Surfel> contour(surfaceSlice.begin(), surfaceSlice.end());
88 
89  Slice slice{dim1, dim2, surfel, contour};
90 
91  return { kspace, slice };
92 }
93 
94 std::vector<Z2i::Point> extractPoints (SegmentComputerOnSurfels const& sc, Slice const& slice)
95 {
96  std::vector<Z2i::Point> points;
97 
98  auto initialPoints = sc.projectSurfel(slice.start);
99  points.push_back(initialPoints.first);
100  points.push_back(initialPoints.second);
101 
102  int currentIdx = 0;
103  bool firstIt = true;
104  for (auto sit = slice.contour.begin() + 1; sit != slice.contour.end(); ++sit)
105  {
106  Surfel s = *sit;
107  auto projectedPoints = sc.projectSurfel(s);
108 
109  if (firstIt) {
110  if (projectedPoints.first == points[currentIdx]) {
111  points.push_back(projectedPoints.second);
112  } else if (projectedPoints.first == points[currentIdx + 1]) {
113  points.push_back(projectedPoints.second);
114  } else if (projectedPoints.second == points[currentIdx]) {
115  points.push_back(projectedPoints.first);
116  } else if (projectedPoints.second == points[currentIdx + 1]) {
117  points.push_back(projectedPoints.first);
118  } else {
119  assert(false);
120  }
121 
122  firstIt = false;
123  } else {
124  if (projectedPoints.first == points[currentIdx]) {
125  points.push_back(projectedPoints.second);
126  } else if (projectedPoints.second == points[currentIdx]) {
127  points.push_back(projectedPoints.first);
128  } else {
129  assert(false);
130  }
131  }
132 
133  currentIdx = points.size() - 1;
134  }
135 
136  return points;
137 }
138 
140 TEST_CASE("Testing ArithmeticalDSSComputerOnSurfels")
141 {
142  // Construct and extract a slice of a digital surface
143  KSpace kspace;
144  Slice slice;
145  std::tie(kspace, slice) = getSlice();
146 
147  // Do a segmentation using the surfel class
148  SegmentComputerOnSurfels recognitionAlgorithmSurfels(kspace, slice.dim1, slice.dim2);
149  SegmentationSurfels segmentationSurfels(slice.contour.begin(), slice.contour.end(), recognitionAlgorithmSurfels);
150 
151  // Extract the projected points
152  std::vector<Z2i::Point> points = extractPoints(recognitionAlgorithmSurfels, slice);
153 
154  // Do a segmentation on the projected points
155  SegmentComputer recognitionAlgorithm;
156  Segmentation segmentation(points.begin(), points.end(), recognitionAlgorithm);
157 
158  // The two segmentations must be the same
159  bool allEqual = true;
160  auto segIt = segmentation.begin();
161  auto segSurfelIt = segmentationSurfels.begin();
162  while (segIt != segmentation.end() && segSurfelIt != segmentationSurfels.end()) {
163  allEqual = allEqual && (segIt->primitive() == segSurfelIt->primitive());
164 
165  ++segIt;
166  ++segSurfelIt;
167  }
168 
169  REQUIRE(allEqual);
170 }
171 
DGtal::ArithmeticalDSSComputerOnSurfels::projectSurfel
std::pair< Point, Point > projectSurfel(SCell const &aSCell) const
DGtal::Surfaces
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
DGtal::ArithmeticalDSSComputerOnSurfels
Aim: This class is a wrapper around ArithmeticalDSS that is devoted to the dynamic recognition of dig...
Definition: ArithmeticalDSSComputerOnSurfels.h:82
DGtal::ArithmeticalDSSComputer< std::vector< Z2i::Point >::const_iterator, int, 4 >
DGtal::Dimension
DGtal::uint32_t Dimension
Definition: Common.h:137
DGtal::Shortcuts::makeDigitalSurface
static CountedPtr< DigitalSurface > makeDigitalSurface(CountedPtr< TPointPredicate > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1208
REQUIRE
REQUIRE(domain.isInside(aPoint))
DGtal::SaturatedSegmentation::end
SaturatedSegmentation::SegmentComputerIterator end() const
DGtal::Shortcuts
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:104
DGtal::SignedKhalimskyCell
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
Definition: KhalimskySpaceND.h:208
DGtal::SaturatedSegmentation::begin
SaturatedSegmentation::SegmentComputerIterator begin() const
TEST_CASE
TEST_CASE("Testing ArithmeticalDSSComputerOnSurfels")
Definition: testArithmeticalDSSComputerOnSurfels.cpp:140
getSlice
std::pair< KSpace, Slice > getSlice(std::string const &shape="ellipsoid", double gridstep=1.0)
Definition: testArithmeticalDSSComputerOnSurfels.cpp:66
KSpace
Z3i::KSpace KSpace
Definition: testArithmeticalDSSComputerOnSurfels.cpp:48
DGtal::Shortcuts::defaultParameters
static Parameters defaultParameters()
Definition: Shortcuts.h:202
DGtal::KhalimskySpaceND::SCell
SignedKhalimskyCell< dim, Integer > SCell
Definition: KhalimskySpaceND.h:417
DGtal::Shortcuts::makeImplicitShape3D
static CountedPtr< ImplicitShape3D > makeImplicitShape3D(const Parameters &params=parametersImplicitShape3D())
Definition: Shortcuts.h:281
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
extractPoints
std::vector< Z2i::Point > extractPoints(SegmentComputerOnSurfels const &sc, Slice const &slice)
Definition: testArithmeticalDSSComputerOnSurfels.cpp:94
DGtal::Shortcuts::makeDigitizedImplicitShape3D
static CountedPtr< DigitizedImplicitShape3D > makeDigitizedImplicitShape3D(CountedPtr< ImplicitShape3D > shape, Parameters params=parametersDigitizedImplicitShape3D())
Definition: Shortcuts.h:522
DGtal::KhalimskySpaceND::DirIterator
typename PreCellularGridSpace::DirIterator DirIterator
Definition: KhalimskySpaceND.h:422
DGtal::Shortcuts::getKSpace
static KSpace getKSpace(const Point &low, const Point &up, Parameters params=parametersKSpace())
Definition: Shortcuts.h:331
DGtal::DigitalSurface2DSlice
Aim: Represents a 2-dimensional slice in a DigitalSurface. In a sense, it is a 4-connected contour,...
Definition: DigitalSurface2DSlice.h:93
DGtal::SaturatedSegmentation
Aim: Computes the saturated segmentation, that is the whole set of maximal segments within a range gi...
Definition: SaturatedSegmentation.h:153
DGtal::Shortcuts::makeBinaryImage
static CountedPtr< BinaryImage > makeBinaryImage(Domain shapeDomain)
Definition: Shortcuts.h:560
DGtal::KhalimskySpaceND
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:64