Skip to content

Commit 93af010

Browse files
[octree] use PCL_ERROR instead of assert (#5321)
* [refactor] octree: use PCL_ERROR instead of assert * PCL_THROW_EXCEPTION for invalid resolution * revert to assert for min_child_idx * use assert for performance * use assert for performance reason * use assert for performance reason * use assert to circumvent "error: reference to local variable 'node' returned [-Werror=return-local-addr]" * [doc] add doc for child_idx_arg * use more assert * Revert some changes in octree_pointcloud.hpp * Revert some changes * Revert some changes * Re-add include --------- Co-authored-by: Markus Vieth <39675748+mvieth@users.noreply.github.com>
1 parent b551ee4 commit 93af010

File tree

7 files changed

+82
-22
lines changed

7 files changed

+82
-22
lines changed

octree/include/pcl/octree/impl/octree2buf_base.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ Octree2BufBase<LeafContainerT, BranchContainerT>::setMaxVoxelIndex(
7171
{
7272
uindex_t treeDepth;
7373

74-
assert(max_voxel_index_arg > 0);
74+
if (max_voxel_index_arg <= 0) {
75+
PCL_ERROR("[pcl::octree::Octree2BufBase::setMaxVoxelIndex] Max voxel index (%lu) "
76+
"must be > 0!\n",
77+
max_voxel_index_arg);
78+
return;
79+
}
7580

7681
// tree depth == amount of bits of maxVoxels
7782
treeDepth =
@@ -88,7 +93,12 @@ template <typename LeafContainerT, typename BranchContainerT>
8893
void
8994
Octree2BufBase<LeafContainerT, BranchContainerT>::setTreeDepth(uindex_t depth_arg)
9095
{
91-
assert(depth_arg > 0);
96+
if (depth_arg <= 0) {
97+
PCL_ERROR(
98+
"[pcl::octree::Octree2BufBase::setTreeDepth] Tree depth (%lu) must be > 0!\n",
99+
depth_arg);
100+
return;
101+
}
92102

93103
// set octree depth
94104
octree_depth_ = depth_arg;

octree/include/pcl/octree/impl/octree_base.hpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ OctreeBase<LeafContainerT, BranchContainerT>::setMaxVoxelIndex(
7171
{
7272
uindex_t tree_depth;
7373

74-
assert(max_voxel_index_arg > 0);
74+
if (max_voxel_index_arg <= 0) {
75+
PCL_ERROR("[pcl::octree::OctreeBase::setMaxVoxelIndex] Max voxel index (%lu) must "
76+
"be > 0!\n",
77+
max_voxel_index_arg);
78+
return;
79+
}
7580

7681
// tree depth == bitlength of maxVoxels
7782
tree_depth =
@@ -86,8 +91,18 @@ template <typename LeafContainerT, typename BranchContainerT>
8691
void
8792
OctreeBase<LeafContainerT, BranchContainerT>::setTreeDepth(uindex_t depth_arg)
8893
{
89-
assert(depth_arg > 0);
90-
assert(depth_arg <= OctreeKey::maxDepth);
94+
if (depth_arg <= 0) {
95+
PCL_ERROR("[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be > 0!\n",
96+
depth_arg);
97+
return;
98+
}
99+
if (depth_arg > OctreeKey::maxDepth) {
100+
PCL_ERROR("[pcl::octree::OctreeBase::setTreeDepth] Tree depth (%lu) must be <= max "
101+
"depth(%lu)!\n",
102+
depth_arg,
103+
OctreeKey::maxDepth);
104+
return;
105+
}
91106

92107
// set octree depth
93108
octree_depth_ = depth_arg;

octree/include/pcl/octree/impl/octree_pointcloud.hpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ pcl::octree::OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeT>
6666
, bounding_box_defined_(false)
6767
, max_objs_per_leaf_(0)
6868
{
69-
assert(resolution > 0.0f);
69+
if (resolution <= 0.0) {
70+
PCL_THROW_EXCEPTION(InitFailedException,
71+
"[pcl::octree::OctreePointCloud::OctreePointCloud] Resolution "
72+
<< resolution << " must be > 0!");
73+
}
7074
}
7175

7276
//////////////////////////////////////////////////////////////////////////////////////////////
@@ -339,7 +343,12 @@ pcl::octree::OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeT>
339343
PointT max_pt;
340344

341345
// bounding box cannot be changed once the octree contains elements
342-
assert(this->leaf_count_ == 0);
346+
if (this->leaf_count_ != 0) {
347+
PCL_ERROR("[pcl::octree::OctreePointCloud::defineBoundingBox] Leaf count (%lu) "
348+
"must be 0\n",
349+
this->leaf_count_);
350+
return;
351+
}
343352

344353
pcl::getMinMax3D(*input_, min_pt, max_pt);
345354

@@ -372,7 +381,12 @@ pcl::octree::OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeT>
372381
const double max_z_arg)
373382
{
374383
// bounding box cannot be changed once the octree contains elements
375-
assert(this->leaf_count_ == 0);
384+
if (this->leaf_count_ != 0) {
385+
PCL_ERROR("[pcl::octree::OctreePointCloud::defineBoundingBox] Leaf count (%lu) "
386+
"must be 0\n",
387+
this->leaf_count_);
388+
return;
389+
}
376390

377391
min_x_ = std::min(min_x_arg, max_x_arg);
378392
min_y_ = std::min(min_y_arg, max_y_arg);
@@ -400,7 +414,12 @@ pcl::octree::OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeT>
400414
const double max_z_arg)
401415
{
402416
// bounding box cannot be changed once the octree contains elements
403-
assert(this->leaf_count_ == 0);
417+
if (this->leaf_count_ != 0) {
418+
PCL_ERROR("[pcl::octree::OctreePointCloud::defineBoundingBox] Leaf count (%lu) "
419+
"must be 0\n",
420+
this->leaf_count_);
421+
return;
422+
}
404423

405424
min_x_ = std::min(0.0, max_x_arg);
406425
min_y_ = std::min(0.0, max_y_arg);
@@ -426,7 +445,12 @@ pcl::octree::OctreePointCloud<PointT, LeafContainerT, BranchContainerT, OctreeT>
426445
defineBoundingBox(const double cubeLen_arg)
427446
{
428447
// bounding box cannot be changed once the octree contains elements
429-
assert(this->leaf_count_ == 0);
448+
if (this->leaf_count_ != 0) {
449+
PCL_ERROR("[pcl::octree::OctreePointCloud::defineBoundingBox] Leaf count (%lu) "
450+
"must be 0\n",
451+
this->leaf_count_);
452+
return;
453+
}
430454

431455
min_x_ = std::min(0.0, cubeLen_arg);
432456
min_y_ = std::min(0.0, cubeLen_arg);

octree/include/pcl/octree/octree2buf_base.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class BufferedBranchNode : public OctreeNode {
8989
}
9090

9191
/** \brief Get child pointer in current branch node
92-
* \param buffer_arg: buffer selector
93-
* \param index_arg: index of child in node
92+
* \param buffer_arg: buffer selector, must be less than 2
93+
* \param index_arg: index of child in node, must be less than 8
9494
* \return pointer to child node
9595
*/
9696
inline OctreeNode*
@@ -101,8 +101,8 @@ class BufferedBranchNode : public OctreeNode {
101101
}
102102

103103
/** \brief Set child pointer in current branch node
104-
* \param buffer_arg: buffer selector
105-
* \param index_arg: index of child in node
104+
* \param buffer_arg: buffer selector, must be less than 2
105+
* \param index_arg: index of child in node, must be less than 8
106106
* \param newNode_arg: pointer to new child node
107107
*/
108108
inline void
@@ -115,8 +115,8 @@ class BufferedBranchNode : public OctreeNode {
115115
}
116116

117117
/** \brief Check if branch is pointing to a particular child node
118-
* \param buffer_arg: buffer selector
119-
* \param index_arg: index of child in node
118+
* \param buffer_arg: buffer selector, must be less than 2
119+
* \param index_arg: index of child in node, must be less than 8
120120
* \return "true" if pointer to child node exists; "false" otherwise
121121
*/
122122
inline bool

octree/include/pcl/octree/octree_container.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#pragma once
4040

41+
#include <pcl/console/print.h>
4142
#include <pcl/types.h>
4243

4344
#include <cassert>
@@ -161,7 +162,8 @@ class OctreeContainerEmpty : public OctreeContainerBase {
161162
index_t
162163
getPointIndex() const override
163164
{
164-
assert("getPointIndex: undefined point index");
165+
PCL_ERROR(
166+
"[pcl::octree::OctreeContainerBase::getPointIndex] Undefined point index!\n");
165167
return -1;
166168
}
167169

octree/include/pcl/octree/octree_nodes.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class OctreeBranchNode : public OctreeNode {
216216
~OctreeBranchNode() override = default;
217217

218218
/** \brief Access operator.
219-
* \param child_idx_arg: index to child node
219+
* \param child_idx_arg: index to child node, must be less than 8
220220
* \return OctreeNode pointer
221221
* */
222222
inline OctreeNode*&
@@ -227,7 +227,7 @@ class OctreeBranchNode : public OctreeNode {
227227
}
228228

229229
/** \brief Get pointer to child
230-
* \param child_idx_arg: index to child node
230+
* \param child_idx_arg: index to child node, must be less than 8
231231
* \return OctreeNode pointer
232232
* */
233233
inline OctreeNode*
@@ -238,6 +238,7 @@ class OctreeBranchNode : public OctreeNode {
238238
}
239239

240240
/** \brief Get pointer to child
241+
* \param index: index to child node, must be less than 8
241242
* \return OctreeNode pointer
242243
* */
243244
inline void
@@ -248,7 +249,7 @@ class OctreeBranchNode : public OctreeNode {
248249
}
249250

250251
/** \brief Check if branch is pointing to a particular child node
251-
* \param child_idx_arg: index to child node
252+
* \param child_idx_arg: index to child node, must be less than 8
252253
* \return "true" if pointer to child node exists; "false" otherwise
253254
* */
254255
inline bool

octree/include/pcl/octree/octree_pointcloud.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ class OctreePointCloud : public OctreeT {
164164
setResolution(double resolution_arg)
165165
{
166166
// octree needs to be empty to change its resolution
167-
assert(this->leaf_count_ == 0);
167+
if (this->leaf_count_ > 0) {
168+
PCL_ERROR("[pcl::octree::OctreePointCloud::setResolution] Octree needs to be "
169+
"empty to change its resolution(leaf count should must be 0)!\n");
170+
return;
171+
}
168172

169173
resolution_ = resolution_arg;
170174

@@ -416,7 +420,11 @@ class OctreePointCloud : public OctreeT {
416420
inline void
417421
enableDynamicDepth(std::size_t maxObjsPerLeaf)
418422
{
419-
assert(this->leaf_count_ == 0);
423+
if (this->leaf_count_ > 0) {
424+
PCL_ERROR("[pcl::octree::OctreePointCloud::enableDynamicDepth] Leaf count should "
425+
"must be 0!\n");
426+
return;
427+
}
420428
max_objs_per_leaf_ = maxObjsPerLeaf;
421429

422430
this->dynamic_depth_enabled_ = max_objs_per_leaf_ > 0;

0 commit comments

Comments
 (0)