Point Cloud Library (PCL)  1.7.0
/tmp/buildd/pcl-1.7-1.7.0/octree/include/pcl/octree/octree_key.h
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2012, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #ifndef PCL_OCTREE_KEY_H
00039 #define PCL_OCTREE_KEY_H
00040 
00041 #include <string>
00042 
00043 namespace pcl
00044 {
00045   namespace octree
00046   {
00047     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00048     /** \brief @b Octree key class
00049      *  \note Octree keys contain integer indices for each coordinate axis in order to address an octree leaf node.
00050      *  \author Julius Kammerl (julius@kammerl.de)
00051      */
00052     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00053     class OctreeKey
00054     {
00055     public:
00056 
00057       /** \brief Empty constructor. */
00058       OctreeKey () :
00059           x (0), y (0), z (0)
00060       {
00061       }
00062 
00063       /** \brief Constructor for key initialization. */
00064       OctreeKey (unsigned int keyX, unsigned int keyY, unsigned int keyZ) :
00065           x (keyX), y (keyY), z (keyZ)
00066       {
00067       }
00068 
00069       /** \brief Copy constructor. */
00070       OctreeKey (const OctreeKey& source)
00071       {
00072         memcpy(key_, source.key_, sizeof(key_));
00073       }
00074 
00075       /** \brief Operator== for comparing octree keys with each other.
00076        *  \return "true" if leaf node indices are identical; "false" otherwise.
00077        * */
00078       bool
00079       operator == (const OctreeKey& b) const
00080       {
00081         return ((b.x == this->x) && (b.y == this->y) && (b.z == this->z));
00082       }
00083 
00084       /** \brief Operator<= for comparing octree keys with each other.
00085        *  \return "true" if key indices are not greater than the key indices of b  ; "false" otherwise.
00086        * */
00087       bool
00088       operator <= (const OctreeKey& b) const
00089       {
00090         return ((b.x >= this->x) && (b.y >= this->y) && (b.z >= this->z));
00091       }
00092 
00093       /** \brief Operator>= for comparing octree keys with each other.
00094        *  \return "true" if key indices are not smaller than the key indices of b  ; "false" otherwise.
00095        * */
00096       bool
00097       operator >= (const OctreeKey& b) const
00098       {
00099         return ((b.x <= this->x) && (b.y <= this->y) && (b.z <= this->z));
00100       }
00101 
00102       /** \brief push a child node to the octree key
00103        *  \param[in] childIndex index of child node to be added (0-7)
00104        * */
00105       inline void
00106       pushBranch (unsigned char childIndex)
00107       {
00108         this->x = (this->x << 1) | (!!(childIndex & (1 << 2)));
00109         this->y = (this->y << 1) | (!!(childIndex & (1 << 1)));
00110         this->z = (this->z << 1) | (!!(childIndex & (1 << 0)));
00111       }
00112 
00113       /** \brief pop child node from octree key
00114        * */
00115       inline void
00116       popBranch ()
00117       {
00118         this->x >>= 1;
00119         this->y >>= 1;
00120         this->z >>= 1;
00121       }
00122 
00123       /** \brief get child node index using depthMask
00124        *  \param[in] depthMask bit mask with single bit set at query depth
00125        *  \return child node index
00126        * */
00127       inline unsigned char
00128       getChildIdxWithDepthMask (unsigned int depthMask) const
00129       {
00130         return static_cast<unsigned char> (((!!(this->x & depthMask)) << 2)
00131                                          | ((!!(this->y & depthMask)) << 1)
00132                                          |  (!!(this->z & depthMask)));
00133       }
00134 
00135       /* \brief maximum depth that can be addressed */
00136       static const unsigned char maxDepth = static_cast<const unsigned char>(sizeof(uint32_t)*8);
00137 
00138       // Indices addressing a voxel at (X, Y, Z)
00139 
00140       union
00141       {
00142         struct
00143         {
00144           uint32_t x;
00145           uint32_t y;
00146           uint32_t z;
00147         };
00148         uint32_t key_[3];
00149       };
00150 
00151 
00152     };
00153   }
00154 }
00155 
00156 #endif