Skip to content

Commit e490001

Browse files
Rewrite unit test for NormalSpaceSampling
1 parent f4a1f20 commit e490001

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

filters/include/pcl/filters/impl/normal_space.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,12 @@ pcl::NormalSpaceSampling<PointT, NormalT>::applyFilter (std::vector<int> &indice
155155

156156
unsigned int pos = 0;
157157
unsigned int random_index = 0;
158-
// std::uniform_int_distribution<unsigned> rng_uniform_distribution (0u, M - 1u); Test fails with this
159-
std::uniform_int_distribution<unsigned> rng_uniform_distribution (0u, input_->size ());
158+
std::uniform_int_distribution<unsigned> rng_uniform_distribution (0u, M - 1u);
160159

161160
// Picking up a sample at random from jth bin
162161
do
163162
{
164-
// random_index = rng_uniform_distribution (rng_); // Test fails with this combo
165-
random_index = rng_uniform_distribution (rng_) % M;
163+
random_index = rng_uniform_distribution (rng_);
166164
pos = start_index[j] + random_index;
167165
} while (is_sampled_flag.test (pos));
168166

test/filters/test_sampling.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,31 +105,44 @@ TEST (CovarianceSampling, Filters)
105105
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
106106
TEST (NormalSpaceSampling, Filters)
107107
{
108+
// pcl::Normal is not precompiled by default so we use PointNormal
109+
auto cloud = pcl::make_shared<PointCloud<PointNormal>> ();
110+
// generate 16 points (8 unique) with unit norm
111+
cloud->reserve (16);
112+
// ensure that the normals have unit norm
113+
const auto value = std::sqrt(1/3.f);
114+
for (int unique = 0; unique < 8; ++unique) {
115+
const auto i = ((unique % 2) < 1) ? -1 : 1; // points alternate sign
116+
const auto j = ((unique % 4) < 2) ? -1 : 1; // 2 points negative, 2 positive
117+
const auto k = ((unique % 8) < 4) ? -1 : 1; // first 4 points negative, rest positive
118+
for (int duplicate = 0; duplicate < 2; ++duplicate) {
119+
cloud->emplace_back (0.f, 0.f, 0.f, i * value, j * value, k * value);
120+
}
121+
}
122+
108123
NormalSpaceSampling<PointNormal, PointNormal> normal_space_sampling;
109-
normal_space_sampling.setInputCloud (cloud_walls_normals);
110-
normal_space_sampling.setNormals (cloud_walls_normals);
111-
normal_space_sampling.setBins (4, 4, 4);
124+
normal_space_sampling.setInputCloud (cloud);
125+
normal_space_sampling.setNormals (cloud);
126+
normal_space_sampling.setBins (2, 2, 2);
112127
normal_space_sampling.setSeed (0);
113-
normal_space_sampling.setSample (static_cast<unsigned int> (cloud_walls_normals->size ()) / 4);
128+
normal_space_sampling.setSample (8);
114129

115-
IndicesPtr walls_indices (new std::vector<int> ());
130+
IndicesPtr walls_indices = pcl::make_shared<Indices> ();
116131
normal_space_sampling.filter (*walls_indices);
117132

118-
CovarianceSampling<PointNormal, PointNormal> covariance_sampling;
119-
covariance_sampling.setInputCloud (cloud_walls_normals);
120-
covariance_sampling.setNormals (cloud_walls_normals);
121-
covariance_sampling.setIndices (walls_indices);
122-
covariance_sampling.setNumberOfSamples (0);
123-
double cond_num_walls_sampled = covariance_sampling.computeConditionNumber ();
124-
133+
// The orientation space of the normals is divided into 2x2x2 buckets
134+
// points are samples arbitrarily from each bucket in succession until the
135+
// requested number of samples is met. This means we expect to see only one index
136+
// for every two elements in the original array e.g. 0, 3, 4, 6, etc...
137+
// if 0 is sampled, index 1 can no longer be there and so forth
138+
std::array<std::set<index_t>, 8> buckets;
139+
for (const auto index : *walls_indices)
140+
buckets[index/2].insert (index);
125141

126-
EXPECT_NEAR (33.04893, cond_num_walls_sampled, 1e-1);
127142

128-
EXPECT_EQ (1412, (*walls_indices)[0]);
129-
EXPECT_EQ (1943, (*walls_indices)[walls_indices->size () / 4]);
130-
EXPECT_EQ (2771, (*walls_indices)[walls_indices->size () / 2]);
131-
EXPECT_EQ (3215, (*walls_indices)[walls_indices->size () * 3 / 4]);
132-
EXPECT_EQ (2503, (*walls_indices)[walls_indices->size () - 1]);
143+
EXPECT_EQ (8u, walls_indices->size ());
144+
for (const auto& bucket : buckets)
145+
EXPECT_EQ (1u, bucket.size ());
133146
}
134147

135148
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)