summaryrefslogtreecommitdiff
path: root/octree/include/pcl/octree/octree_search.h
blob: 6d97a606b3f3d6cc048abb5e070f700c3e4e05a5 (plain)
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
 * Software License Agreement (BSD License)
 *
 *  Point Cloud Library (PCL) - www.pointclouds.org
 *  Copyright (c) 2010-2012, Willow Garage, Inc.
 *
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. 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 DIRECT, 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.
 *
 * $Id$
 */

#pragma once

#include <pcl/octree/octree_pointcloud.h>
#include <pcl/point_cloud.h>

namespace pcl {
namespace octree {

/** \brief @b Octree pointcloud search class
 * \note This class provides several methods for spatial neighbor search based on octree
 * structure
 * \tparam PointT type of point used in pointcloud
 * \ingroup octree
 * \author Julius Kammerl (julius@kammerl.de)
 */
template <typename PointT,
          typename LeafContainerT = OctreeContainerPointIndices,
          typename BranchContainerT = OctreeContainerEmpty>
class OctreePointCloudSearch
: public OctreePointCloud<PointT, LeafContainerT, BranchContainerT> {
public:
  // public typedefs
  using IndicesPtr = shared_ptr<std::vector<int>>;
  using IndicesConstPtr = shared_ptr<const std::vector<int>>;

  using PointCloud = pcl::PointCloud<PointT>;
  using PointCloudPtr = typename PointCloud::Ptr;
  using PointCloudConstPtr = typename PointCloud::ConstPtr;

  // Boost shared pointers
  using Ptr =
      shared_ptr<OctreePointCloudSearch<PointT, LeafContainerT, BranchContainerT>>;
  using ConstPtr = shared_ptr<
      const OctreePointCloudSearch<PointT, LeafContainerT, BranchContainerT>>;

  // Eigen aligned allocator
  using AlignedPointTVector = std::vector<PointT, Eigen::aligned_allocator<PointT>>;

  using OctreeT = OctreePointCloud<PointT, LeafContainerT, BranchContainerT>;
  using LeafNode = typename OctreeT::LeafNode;
  using BranchNode = typename OctreeT::BranchNode;

  /** \brief Constructor.
   * \param[in] resolution octree resolution at lowest octree level
   */
  OctreePointCloudSearch(const double resolution)
  : OctreePointCloud<PointT, LeafContainerT, BranchContainerT>(resolution)
  {}

  /** \brief Search for neighbors within a voxel at given point
   * \param[in] point point addressing a leaf node voxel
   * \param[out] point_idx_data the resultant indices of the neighboring voxel points
   * \return "true" if leaf node exist; "false" otherwise
   */
  bool
  voxelSearch(const PointT& point, std::vector<int>& point_idx_data);

  /** \brief Search for neighbors within a voxel at given point referenced by a point
   * index
   * \param[in] index the index in input cloud defining the query point
   * \param[out] point_idx_data the resultant indices of the neighboring voxel points
   * \return "true" if leaf node exist; "false" otherwise
   */
  bool
  voxelSearch(const int index, std::vector<int>& point_idx_data);

  /** \brief Search for k-nearest neighbors at the query point.
   * \param[in] cloud the point cloud data
   * \param[in] index the index in \a cloud representing the query point
   * \param[in] k the number of neighbors to search for
   * \param[out] k_indices the resultant indices of the neighboring points (must be
   * resized to \a k a priori!)
   * \param[out] k_sqr_distances the resultant squared distances to the neighboring
   * points (must be resized to \a k a priori!)
   * \return number of neighbors found
   */
  inline int
  nearestKSearch(const PointCloud& cloud,
                 int index,
                 int k,
                 std::vector<int>& k_indices,
                 std::vector<float>& k_sqr_distances)
  {
    return (nearestKSearch(cloud[index], k, k_indices, k_sqr_distances));
  }

  /** \brief Search for k-nearest neighbors at given query point.
   * \param[in] p_q the given query point
   * \param[in] k the number of neighbors to search for
   * \param[out] k_indices the resultant indices of the neighboring points (must be
   * resized to k a priori!)
   * \param[out] k_sqr_distances  the resultant squared distances to the neighboring
   * points (must be resized to k a priori!)
   * \return number of neighbors found
   */
  int
  nearestKSearch(const PointT& p_q,
                 int k,
                 std::vector<int>& k_indices,
                 std::vector<float>& k_sqr_distances);

  /** \brief Search for k-nearest neighbors at query point
   * \param[in] index index representing the query point in the dataset given by \a
   * setInputCloud. If indices were given in setInputCloud, index will be the position
   * in the indices vector.
   * \param[in] k the number of neighbors to search for
   * \param[out] k_indices the resultant indices of the neighboring points (must be
   * resized to \a k a priori!)
   * \param[out] k_sqr_distances the resultant squared distances to the neighboring
   * points (must be resized to \a k a priori!)
   * \return number of neighbors found
   */
  int
  nearestKSearch(int index,
                 int k,
                 std::vector<int>& k_indices,
                 std::vector<float>& k_sqr_distances);

  /** \brief Search for approx. nearest neighbor at the query point.
   * \param[in] cloud the point cloud data
   * \param[in] query_index the index in \a cloud representing the query point
   * \param[out] result_index the resultant index of the neighbor point
   * \param[out] sqr_distance the resultant squared distance to the neighboring point
   * \return number of neighbors found
   */
  inline void
  approxNearestSearch(const PointCloud& cloud,
                      int query_index,
                      int& result_index,
                      float& sqr_distance)
  {
    return (approxNearestSearch(cloud.points[query_index], result_index, sqr_distance));
  }

  /** \brief Search for approx. nearest neighbor at the query point.
   * \param[in] p_q the given query point
   * \param[out] result_index the resultant index of the neighbor point
   * \param[out] sqr_distance the resultant squared distance to the neighboring point
   */
  void
  approxNearestSearch(const PointT& p_q, int& result_index, float& sqr_distance);

  /** \brief Search for approx. nearest neighbor at the query point.
   * \param[in] query_index index representing the query point in the dataset given by
   * \a setInputCloud. If indices were given in setInputCloud, index will be the
   * position in the indices vector.
   * \param[out] result_index the resultant index of the neighbor point
   * \param[out] sqr_distance the resultant squared distance to the neighboring point
   * \return number of neighbors found
   */
  void
  approxNearestSearch(int query_index, int& result_index, float& sqr_distance);

  /** \brief Search for all neighbors of query point that are within a given radius.
   * \param[in] cloud the point cloud data
   * \param[in] index the index in \a cloud representing the query point
   * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
   * \param[out] k_indices the resultant indices of the neighboring points
   * \param[out] k_sqr_distances the resultant squared distances to the neighboring
   * points
   * \param[in] max_nn if given, bounds the maximum returned neighbors to this value
   * \return number of neighbors found in radius
   */
  int
  radiusSearch(const PointCloud& cloud,
               int index,
               double radius,
               std::vector<int>& k_indices,
               std::vector<float>& k_sqr_distances,
               unsigned int max_nn = 0)
  {
    return (
        radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn));
  }

  /** \brief Search for all neighbors of query point that are within a given radius.
   * \param[in] p_q the given query point
   * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
   * \param[out] k_indices the resultant indices of the neighboring points
   * \param[out] k_sqr_distances the resultant squared distances to the neighboring
   * points
   * \param[in] max_nn if given, bounds the maximum returned neighbors to this value
   * \return number of neighbors found in radius
   */
  int
  radiusSearch(const PointT& p_q,
               const double radius,
               std::vector<int>& k_indices,
               std::vector<float>& k_sqr_distances,
               unsigned int max_nn = 0) const;

  /** \brief Search for all neighbors of query point that are within a given radius.
   * \param[in] index index representing the query point in the dataset given by \a
   * setInputCloud. If indices were given in setInputCloud, index will be the position
   * in the indices vector
   * \param[in] radius radius of the sphere bounding all of p_q's neighbors
   * \param[out] k_indices the resultant indices of the neighboring points
   * \param[out] k_sqr_distances the resultant squared distances to the neighboring
   * points
   * \param[in] max_nn if given, bounds the maximum returned neighbors to this value
   * \return number of neighbors found in radius
   */
  int
  radiusSearch(int index,
               const double radius,
               std::vector<int>& k_indices,
               std::vector<float>& k_sqr_distances,
               unsigned int max_nn = 0) const;

  /** \brief Get a PointT vector of centers of all voxels that intersected by a ray
   * (origin, direction).
   * \param[in] origin ray origin
   * \param[in] direction ray direction vector
   * \param[out] voxel_center_list results are written to this vector of PointT elements
   * \param[in] max_voxel_count stop raycasting when this many voxels intersected (0:
   * disable)
   * \return number of intersected voxels
   */
  int
  getIntersectedVoxelCenters(Eigen::Vector3f origin,
                             Eigen::Vector3f direction,
                             AlignedPointTVector& voxel_center_list,
                             int max_voxel_count = 0) const;

  /** \brief Get indices of all voxels that are intersected by a ray (origin,
   * direction).
   * \param[in] origin ray origin \param[in] direction ray direction vector
   * \param[out] k_indices resulting point indices from intersected voxels
   * \param[in] max_voxel_count stop raycasting when this many voxels intersected (0:
   * disable)
   * \return number of intersected voxels
   */
  int
  getIntersectedVoxelIndices(Eigen::Vector3f origin,
                             Eigen::Vector3f direction,
                             std::vector<int>& k_indices,
                             int max_voxel_count = 0) const;

  /** \brief Search for points within rectangular search area
   * Points exactly on the edges of the search rectangle are included.
   * \param[in] min_pt lower corner of search area
   * \param[in] max_pt upper corner of search area
   * \param[out] k_indices the resultant point indices
   * \return number of points found within search area
   */
  int
  boxSearch(const Eigen::Vector3f& min_pt,
            const Eigen::Vector3f& max_pt,
            std::vector<int>& k_indices) const;

protected:
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Octree-based search routines & helpers
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  /** \brief @b Priority queue entry for branch nodes
   *  \note This class defines priority queue entries for the nearest neighbor search.
   *  \author Julius Kammerl (julius@kammerl.de)
   */
  class prioBranchQueueEntry {
  public:
    /** \brief Empty constructor  */
    prioBranchQueueEntry() : node(), point_distance(0) {}

    /** \brief Constructor for initializing priority queue entry.
     * \param _node pointer to octree node
     * \param _key octree key addressing voxel in octree structure
     * \param[in] _point_distance distance of query point to voxel center
     */
    prioBranchQueueEntry(OctreeNode* _node, OctreeKey& _key, float _point_distance)
    : node(_node), point_distance(_point_distance), key(_key)
    {}

    /** \brief Operator< for comparing priority queue entries with each other.
     * \param[in] rhs the priority queue to compare this against
     */
    bool
    operator<(const prioBranchQueueEntry rhs) const
    {
      return (this->point_distance > rhs.point_distance);
    }

    /** \brief Pointer to octree node. */
    const OctreeNode* node;

    /** \brief Distance to query point. */
    float point_distance;

    /** \brief Octree key. */
    OctreeKey key;
  };

  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  /** \brief @b Priority queue entry for point candidates
   * \note This class defines priority queue entries for the nearest neighbor point
   * candidates.
   * \author Julius Kammerl (julius@kammerl.de)
   */
  class prioPointQueueEntry {
  public:
    /** \brief Empty constructor  */
    prioPointQueueEntry() : point_idx_(0), point_distance_(0) {}

    /** \brief Constructor for initializing priority queue entry.
     * \param[in] point_idx index for a dataset point given by \a setInputCloud
     * \param[in] point_distance distance of query point to voxel center
     */
    prioPointQueueEntry(unsigned int& point_idx, float point_distance)
    : point_idx_(point_idx), point_distance_(point_distance)
    {}

    /** \brief Operator< for comparing priority queue entries with each other.
     * \param[in] rhs priority queue to compare this against
     */
    bool
    operator<(const prioPointQueueEntry& rhs) const
    {
      return (this->point_distance_ < rhs.point_distance_);
    }

    /** \brief Index representing a point in the dataset given by \a setInputCloud. */
    int point_idx_;

    /** \brief Distance to query point. */
    float point_distance_;
  };

  /** \brief Helper function to calculate the squared distance between two points
   * \param[in] point_a point A
   * \param[in] point_b point B
   * \return squared distance between point A and point B
   */
  float
  pointSquaredDist(const PointT& point_a, const PointT& point_b) const;

  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Recursive search routine methods
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  /** \brief Recursive search method that explores the octree and finds neighbors within
   * a given radius
   * \param[in] point query point \param[in] radiusSquared squared search radius
   * \param[in] node current octree node to be explored
   * \param[in] key octree key addressing a leaf node.
   * \param[in] tree_depth current depth/level in the octree
   * \param[out] k_indices vector of indices found to be neighbors of query point
   * \param[out] k_sqr_distances squared distances of neighbors to query point
   * \param[in] max_nn maximum of neighbors to be found
   */
  void
  getNeighborsWithinRadiusRecursive(const PointT& point,
                                    const double radiusSquared,
                                    const BranchNode* node,
                                    const OctreeKey& key,
                                    unsigned int tree_depth,
                                    std::vector<int>& k_indices,
                                    std::vector<float>& k_sqr_distances,
                                    unsigned int max_nn) const;

  /** \brief Recursive search method that explores the octree and finds the K nearest
   * neighbors
   * \param[in] point query point
   * \param[in] K amount of nearest neighbors to be found
   * \param[in] node current octree node to be explored
   * \param[in] key octree key addressing a leaf node.
   * \param[in] tree_depth current depth/level in the octree
   * \param[in] squared_search_radius squared search radius distance
   * \param[out] point_candidates priority queue of nearest neigbor point candidates
   * \return squared search radius based on current point candidate set found
   */
  double
  getKNearestNeighborRecursive(
      const PointT& point,
      unsigned int K,
      const BranchNode* node,
      const OctreeKey& key,
      unsigned int tree_depth,
      const double squared_search_radius,
      std::vector<prioPointQueueEntry>& point_candidates) const;

  /** \brief Recursive search method that explores the octree and finds the approximate
   * nearest neighbor
   * \param[in] point query point
   * \param[in] node current octree node to be explored
   * \param[in] key octree key addressing a leaf node.
   * \param[in] tree_depth current depth/level in the octree
   * \param[out] result_index result index is written to this reference
   * \param[out] sqr_distance squared distance to search
   */
  void
  approxNearestSearchRecursive(const PointT& point,
                               const BranchNode* node,
                               const OctreeKey& key,
                               unsigned int tree_depth,
                               int& result_index,
                               float& sqr_distance);

  /** \brief Recursively search the tree for all intersected leaf nodes and return a
   * vector of voxel centers. This algorithm is based off the paper An Efficient
   * Parametric Algorithm for Octree Traversal:
   * http://wscg.zcu.cz/wscg2000/Papers_2000/X31.pdf
   * \param[in] min_x octree nodes X coordinate of lower bounding box corner
   * \param[in] min_y octree nodes Y coordinate of lower bounding box corner
   * \param[in] min_z octree nodes Z coordinate of lower bounding box corner
   * \param[in] max_x octree nodes X coordinate of upper bounding box corner
   * \param[in] max_y octree nodes Y coordinate of upper bounding box corner
   * \param[in] max_z octree nodes Z coordinate of upper bounding box corner
   * \param[in] a
   * \param[in] node current octree node to be explored
   * \param[in] key octree key addressing a leaf node.
   * \param[out] voxel_center_list results are written to this vector of PointT elements
   * \param[in] max_voxel_count stop raycasting when this many voxels intersected (0:
   * disable)
   * \return number of voxels found
   */
  int
  getIntersectedVoxelCentersRecursive(double min_x,
                                      double min_y,
                                      double min_z,
                                      double max_x,
                                      double max_y,
                                      double max_z,
                                      unsigned char a,
                                      const OctreeNode* node,
                                      const OctreeKey& key,
                                      AlignedPointTVector& voxel_center_list,
                                      int max_voxel_count) const;

  /** \brief Recursive search method that explores the octree and finds points within a
   * rectangular search area
   * \param[in] min_pt lower corner of search area
   * \param[in] max_pt upper corner of search area
   * \param[in] node current octree node to be explored
   * \param[in] key octree key addressing a leaf node.
   * \param[in] tree_depth current depth/level in the octree
   * \param[out] k_indices the resultant point indices
   */
  void
  boxSearchRecursive(const Eigen::Vector3f& min_pt,
                     const Eigen::Vector3f& max_pt,
                     const BranchNode* node,
                     const OctreeKey& key,
                     unsigned int tree_depth,
                     std::vector<int>& k_indices) const;

  /** \brief Recursively search the tree for all intersected leaf nodes and return a
   * vector of indices. This algorithm is based off the paper An Efficient Parametric
   * Algorithm for Octree Traversal: http://wscg.zcu.cz/wscg2000/Papers_2000/X31.pdf
   * \param[in] min_x octree nodes X coordinate of lower bounding box corner
   * \param[in] min_y octree nodes Y coordinate of lower bounding box corner
   * \param[in] min_z octree nodes Z coordinate of lower bounding box corner
   * \param[in] max_x octree nodes X coordinate of upper bounding box corner
   * \param[in] max_y octree nodes Y coordinate of upper bounding box corner
   * \param[in] max_z octree nodes Z coordinate of upper bounding box corner
   * \param[in] a
   * \param[in] node current octree node to be explored
   * \param[in] key octree key addressing a leaf node.
   * \param[out] k_indices resulting indices
   * \param[in] max_voxel_count stop raycasting when this many voxels intersected (0:
   * disable)
   * \return number of voxels found
   */
  int
  getIntersectedVoxelIndicesRecursive(double min_x,
                                      double min_y,
                                      double min_z,
                                      double max_x,
                                      double max_y,
                                      double max_z,
                                      unsigned char a,
                                      const OctreeNode* node,
                                      const OctreeKey& key,
                                      std::vector<int>& k_indices,
                                      int max_voxel_count) const;

  /** \brief Initialize raytracing algorithm
   * \param origin
   * \param direction
   * \param[in] min_x octree nodes X coordinate of lower bounding box corner
   * \param[in] min_y octree nodes Y coordinate of lower bounding box corner
   * \param[in] min_z octree nodes Z coordinate of lower bounding box corner
   * \param[in] max_x octree nodes X coordinate of upper bounding box corner
   * \param[in] max_y octree nodes Y coordinate of upper bounding box corner
   * \param[in] max_z octree nodes Z coordinate of upper bounding box corner
   * \param a
   */
  inline void
  initIntersectedVoxel(Eigen::Vector3f& origin,
                       Eigen::Vector3f& direction,
                       double& min_x,
                       double& min_y,
                       double& min_z,
                       double& max_x,
                       double& max_y,
                       double& max_z,
                       unsigned char& a) const
  {
    // Account for division by zero when direction vector is 0.0
    const float epsilon = 1e-10f;
    if (direction.x() == 0.0)
      direction.x() = epsilon;
    if (direction.y() == 0.0)
      direction.y() = epsilon;
    if (direction.z() == 0.0)
      direction.z() = epsilon;

    // Voxel childIdx remapping
    a = 0;

    // Handle negative axis direction vector
    if (direction.x() < 0.0) {
      origin.x() = static_cast<float>(this->min_x_) + static_cast<float>(this->max_x_) -
                   origin.x();
      direction.x() = -direction.x();
      a |= 4;
    }
    if (direction.y() < 0.0) {
      origin.y() = static_cast<float>(this->min_y_) + static_cast<float>(this->max_y_) -
                   origin.y();
      direction.y() = -direction.y();
      a |= 2;
    }
    if (direction.z() < 0.0) {
      origin.z() = static_cast<float>(this->min_z_) + static_cast<float>(this->max_z_) -
                   origin.z();
      direction.z() = -direction.z();
      a |= 1;
    }
    min_x = (this->min_x_ - origin.x()) / direction.x();
    max_x = (this->max_x_ - origin.x()) / direction.x();
    min_y = (this->min_y_ - origin.y()) / direction.y();
    max_y = (this->max_y_ - origin.y()) / direction.y();
    min_z = (this->min_z_ - origin.z()) / direction.z();
    max_z = (this->max_z_ - origin.z()) / direction.z();
  }

  /** \brief Find first child node ray will enter
   * \param[in] min_x octree nodes X coordinate of lower bounding box corner
   * \param[in] min_y octree nodes Y coordinate of lower bounding box corner
   * \param[in] min_z octree nodes Z coordinate of lower bounding box corner
   * \param[in] mid_x octree nodes X coordinate of bounding box mid line
   * \param[in] mid_y octree nodes Y coordinate of bounding box mid line
   * \param[in] mid_z octree nodes Z coordinate of bounding box mid line
   * \return the first child node ray will enter
   */
  inline int
  getFirstIntersectedNode(double min_x,
                          double min_y,
                          double min_z,
                          double mid_x,
                          double mid_y,
                          double mid_z) const
  {
    int currNode = 0;

    if (min_x > min_y) {
      if (min_x > min_z) {
        // max(min_x, min_y, min_z) is min_x. Entry plane is YZ.
        if (mid_y < min_x)
          currNode |= 2;
        if (mid_z < min_x)
          currNode |= 1;
      }
      else {
        // max(min_x, min_y, min_z) is min_z. Entry plane is XY.
        if (mid_x < min_z)
          currNode |= 4;
        if (mid_y < min_z)
          currNode |= 2;
      }
    }
    else {
      if (min_y > min_z) {
        // max(min_x, min_y, min_z) is min_y. Entry plane is XZ.
        if (mid_x < min_y)
          currNode |= 4;
        if (mid_z < min_y)
          currNode |= 1;
      }
      else {
        // max(min_x, min_y, min_z) is min_z. Entry plane is XY.
        if (mid_x < min_z)
          currNode |= 4;
        if (mid_y < min_z)
          currNode |= 2;
      }
    }

    return currNode;
  }

  /** \brief Get the next visited node given the current node upper
   *   bounding box corner. This function accepts three float values, and
   *   three int values. The function returns the ith integer where the
   *   ith float value is the minimum of the three float values.
   * \param[in] x current nodes X coordinate of upper bounding box corner
   * \param[in] y current nodes Y coordinate of upper bounding box corner
   * \param[in] z current nodes Z coordinate of upper bounding box corner
   * \param[in] a next node if exit Plane YZ
   * \param[in] b next node if exit Plane XZ
   * \param[in] c next node if exit Plane XY
   * \return the next child node ray will enter or 8 if exiting
   */
  inline int
  getNextIntersectedNode(double x, double y, double z, int a, int b, int c) const
  {
    if (x < y) {
      if (x < z)
        return a;
      return c;
    }
    if (y < z)
      return b;
    return c;
  }
};
} // namespace octree
} // namespace pcl

#ifdef PCL_NO_PRECOMPILE
#include <pcl/octree/impl/octree_search.hpp>
#endif