1  | 
     | 
     | 
    /*  | 
    
    
    2  | 
     | 
     | 
     * Software License Agreement (BSD License)  | 
    
    
    3  | 
     | 
     | 
     *  | 
    
    
    4  | 
     | 
     | 
     *  Copyright (c) 2011-2014, Willow Garage, Inc.  | 
    
    
    5  | 
     | 
     | 
     *  Copyright (c) 2014-2016, Open Source Robotics Foundation  | 
    
    
    6  | 
     | 
     | 
     *  Copyright (c) 2016, Toyota Research Institute  | 
    
    
    7  | 
     | 
     | 
     *  All rights reserved.  | 
    
    
    8  | 
     | 
     | 
     *  | 
    
    
    9  | 
     | 
     | 
     *  Redistribution and use in source and binary forms, with or without  | 
    
    
    10  | 
     | 
     | 
     *  modification, are permitted provided that the following conditions  | 
    
    
    11  | 
     | 
     | 
     *  are met:  | 
    
    
    12  | 
     | 
     | 
     *  | 
    
    
    13  | 
     | 
     | 
     *   * Redistributions of source code must retain the above copyright  | 
    
    
    14  | 
     | 
     | 
     *     notice, this list of conditions and the following disclaimer.  | 
    
    
    15  | 
     | 
     | 
     *   * Redistributions in binary form must reproduce the above  | 
    
    
    16  | 
     | 
     | 
     *     copyright notice, this list of conditions and the following  | 
    
    
    17  | 
     | 
     | 
     *     disclaimer in the documentation and/or other materials provided  | 
    
    
    18  | 
     | 
     | 
     *     with the distribution.  | 
    
    
    19  | 
     | 
     | 
     *   * Neither the name of Open Source Robotics Foundation nor the names of its  | 
    
    
    20  | 
     | 
     | 
     *     contributors may be used to endorse or promote products derived  | 
    
    
    21  | 
     | 
     | 
     *     from this software without specific prior written permission.  | 
    
    
    22  | 
     | 
     | 
     *  | 
    
    
    23  | 
     | 
     | 
     *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  | 
    
    
    24  | 
     | 
     | 
     *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  | 
    
    
    25  | 
     | 
     | 
     *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS  | 
    
    
    26  | 
     | 
     | 
     *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE  | 
    
    
    27  | 
     | 
     | 
     *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,  | 
    
    
    28  | 
     | 
     | 
     *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  | 
    
    
    29  | 
     | 
     | 
     *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  | 
    
    
    30  | 
     | 
     | 
     *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER  | 
    
    
    31  | 
     | 
     | 
     *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
    
    
    32  | 
     | 
     | 
     *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  | 
    
    
    33  | 
     | 
     | 
     *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  | 
    
    
    34  | 
     | 
     | 
     *  POSSIBILITY OF SUCH DAMAGE.  | 
    
    
    35  | 
     | 
     | 
     */  | 
    
    
    36  | 
     | 
     | 
     | 
    
    
    37  | 
     | 
     | 
    /** @author Jia Pan */  | 
    
    
    38  | 
     | 
     | 
     | 
    
    
    39  | 
     | 
     | 
    #ifndef HPP_FCL_MORTON_INL_H  | 
    
    
    40  | 
     | 
     | 
    #define HPP_FCL_MORTON_INL_H  | 
    
    
    41  | 
     | 
     | 
     | 
    
    
    42  | 
     | 
     | 
    #include "hpp/fcl/broadphase/detail/morton.h"  | 
    
    
    43  | 
     | 
     | 
     | 
    
    
    44  | 
     | 
     | 
    namespace hpp { | 
    
    
    45  | 
     | 
     | 
    namespace fcl {  /// @cond IGNORE | 
    
    
    46  | 
     | 
     | 
    namespace detail { | 
    
    
    47  | 
     | 
     | 
     | 
    
    
    48  | 
     | 
     | 
    //==============================================================================  | 
    
    
    49  | 
     | 
     | 
    template <typename S>  | 
    
    
    50  | 
     | 
    76026  | 
    uint32_t quantize(S x, uint32_t n) { | 
    
    
    51  | 
     | 
    76026  | 
      return std::max(std::min((uint32_t)(x * (S)n), uint32_t(n - 1)), uint32_t(0));  | 
    
    
    52  | 
     | 
     | 
    }  | 
    
    
    53  | 
     | 
     | 
     | 
    
    
    54  | 
     | 
     | 
    //==============================================================================  | 
    
    
    55  | 
     | 
     | 
    template <typename S>  | 
    
    
    56  | 
     | 
    86  | 
    morton_functor<S, uint32_t>::morton_functor(const AABB& bbox)  | 
    
    
    57  | 
     | 
    86  | 
        : base(bbox.min_),  | 
    
    
    58  | 
    ✓✗ | 
    86  | 
          inv(1.0 / (bbox.max_[0] - bbox.min_[0]),  | 
    
    
    59  | 
    ✓✗✓✗
  | 
    86  | 
              1.0 / (bbox.max_[1] - bbox.min_[1]),  | 
    
    
    60  | 
    ✓✗✓✗
  | 
    172  | 
              1.0 / (bbox.max_[2] - bbox.min_[2])) { | 
    
    
    61  | 
     | 
     | 
      // Do nothing  | 
    
    
    62  | 
     | 
    86  | 
    }  | 
    
    
    63  | 
     | 
     | 
     | 
    
    
    64  | 
     | 
     | 
    //==============================================================================  | 
    
    
    65  | 
     | 
     | 
    template <typename S>  | 
    
    
    66  | 
     | 
    25342  | 
    uint32_t morton_functor<S, uint32_t>::operator()(const Vec3f& point) const { | 
    
    
    67  | 
     | 
    25342  | 
      uint32_t x = detail::quantize((point[0] - base[0]) * inv[0], 1024u);  | 
    
    
    68  | 
     | 
    25342  | 
      uint32_t y = detail::quantize((point[1] - base[1]) * inv[1], 1024u);  | 
    
    
    69  | 
     | 
    25342  | 
      uint32_t z = detail::quantize((point[2] - base[2]) * inv[2], 1024u);  | 
    
    
    70  | 
     | 
     | 
     | 
    
    
    71  | 
     | 
    25342  | 
      return detail::morton_code(x, y, z);  | 
    
    
    72  | 
     | 
     | 
    }  | 
    
    
    73  | 
     | 
     | 
     | 
    
    
    74  | 
     | 
     | 
    //==============================================================================  | 
    
    
    75  | 
     | 
     | 
    template <typename S>  | 
    
    
    76  | 
     | 
     | 
    morton_functor<S, uint64_t>::morton_functor(const AABB& bbox)  | 
    
    
    77  | 
     | 
     | 
        : base(bbox.min_),  | 
    
    
    78  | 
     | 
     | 
          inv(1.0 / (bbox.max_[0] - bbox.min_[0]),  | 
    
    
    79  | 
     | 
     | 
              1.0 / (bbox.max_[1] - bbox.min_[1]),  | 
    
    
    80  | 
     | 
     | 
              1.0 / (bbox.max_[2] - bbox.min_[2])) { | 
    
    
    81  | 
     | 
     | 
      // Do nothing  | 
    
    
    82  | 
     | 
     | 
    }  | 
    
    
    83  | 
     | 
     | 
     | 
    
    
    84  | 
     | 
     | 
    //==============================================================================  | 
    
    
    85  | 
     | 
     | 
    template <typename S>  | 
    
    
    86  | 
     | 
     | 
    uint64_t morton_functor<S, uint64_t>::operator()(const Vec3f& point) const { | 
    
    
    87  | 
     | 
     | 
      uint32_t x = detail::quantize((point[0] - base[0]) * inv[0], 1u << 20);  | 
    
    
    88  | 
     | 
     | 
      uint32_t y = detail::quantize((point[1] - base[1]) * inv[1], 1u << 20);  | 
    
    
    89  | 
     | 
     | 
      uint32_t z = detail::quantize((point[2] - base[2]) * inv[2], 1u << 20);  | 
    
    
    90  | 
     | 
     | 
     | 
    
    
    91  | 
     | 
     | 
      return detail::morton_code60(x, y, z);  | 
    
    
    92  | 
     | 
     | 
    }  | 
    
    
    93  | 
     | 
     | 
     | 
    
    
    94  | 
     | 
     | 
    //==============================================================================  | 
    
    
    95  | 
     | 
     | 
    template <typename S>  | 
    
    
    96  | 
     | 
     | 
    constexpr size_t morton_functor<S, uint64_t>::bits() { | 
    
    
    97  | 
     | 
     | 
      return 60;  | 
    
    
    98  | 
     | 
     | 
    }  | 
    
    
    99  | 
     | 
     | 
     | 
    
    
    100  | 
     | 
     | 
    //==============================================================================  | 
    
    
    101  | 
     | 
     | 
    template <typename S>  | 
    
    
    102  | 
     | 
    172  | 
    constexpr size_t morton_functor<S, uint32_t>::bits() { | 
    
    
    103  | 
     | 
    172  | 
      return 30;  | 
    
    
    104  | 
     | 
     | 
    }  | 
    
    
    105  | 
     | 
     | 
     | 
    
    
    106  | 
     | 
     | 
    //==============================================================================  | 
    
    
    107  | 
     | 
     | 
    template <typename S, size_t N>  | 
    
    
    108  | 
     | 
     | 
    morton_functor<S, std::bitset<N>>::morton_functor(const AABB& bbox)  | 
    
    
    109  | 
     | 
     | 
        : base(bbox.min_),  | 
    
    
    110  | 
     | 
     | 
          inv(1.0 / (bbox.max_[0] - bbox.min_[0]),  | 
    
    
    111  | 
     | 
     | 
              1.0 / (bbox.max_[1] - bbox.min_[1]),  | 
    
    
    112  | 
     | 
     | 
              1.0 / (bbox.max_[2] - bbox.min_[2])) { | 
    
    
    113  | 
     | 
     | 
      // Do nothing  | 
    
    
    114  | 
     | 
     | 
    }  | 
    
    
    115  | 
     | 
     | 
     | 
    
    
    116  | 
     | 
     | 
    //==============================================================================  | 
    
    
    117  | 
     | 
     | 
    template <typename S, size_t N>  | 
    
    
    118  | 
     | 
     | 
    std::bitset<N> morton_functor<S, std::bitset<N>>::operator()(  | 
    
    
    119  | 
     | 
     | 
        const Vec3f& point) const { | 
    
    
    120  | 
     | 
     | 
      S x = (point[0] - base[0]) * inv[0];  | 
    
    
    121  | 
     | 
     | 
      S y = (point[1] - base[1]) * inv[1];  | 
    
    
    122  | 
     | 
     | 
      S z = (point[2] - base[2]) * inv[2];  | 
    
    
    123  | 
     | 
     | 
      int start_bit = bits() - 1;  | 
    
    
    124  | 
     | 
     | 
      std::bitset<N> bset;  | 
    
    
    125  | 
     | 
     | 
     | 
    
    
    126  | 
     | 
     | 
      x *= 2;  | 
    
    
    127  | 
     | 
     | 
      y *= 2;  | 
    
    
    128  | 
     | 
     | 
      z *= 2;  | 
    
    
    129  | 
     | 
     | 
     | 
    
    
    130  | 
     | 
     | 
      for (size_t i = 0; i < bits() / 3; ++i) { | 
    
    
    131  | 
     | 
     | 
        bset[start_bit--] = ((z < 1) ? 0 : 1);  | 
    
    
    132  | 
     | 
     | 
        bset[start_bit--] = ((y < 1) ? 0 : 1);  | 
    
    
    133  | 
     | 
     | 
        bset[start_bit--] = ((x < 1) ? 0 : 1);  | 
    
    
    134  | 
     | 
     | 
        x = ((x >= 1) ? 2 * (x - 1) : 2 * x);  | 
    
    
    135  | 
     | 
     | 
        y = ((y >= 1) ? 2 * (y - 1) : 2 * y);  | 
    
    
    136  | 
     | 
     | 
        z = ((z >= 1) ? 2 * (z - 1) : 2 * z);  | 
    
    
    137  | 
     | 
     | 
      }  | 
    
    
    138  | 
     | 
     | 
     | 
    
    
    139  | 
     | 
     | 
      return bset;  | 
    
    
    140  | 
     | 
     | 
    }  | 
    
    
    141  | 
     | 
     | 
     | 
    
    
    142  | 
     | 
     | 
    //==============================================================================  | 
    
    
    143  | 
     | 
     | 
    template <typename S, size_t N>  | 
    
    
    144  | 
     | 
     | 
    constexpr size_t morton_functor<S, std::bitset<N>>::bits() { | 
    
    
    145  | 
     | 
     | 
      return N;  | 
    
    
    146  | 
     | 
     | 
    }  | 
    
    
    147  | 
     | 
     | 
     | 
    
    
    148  | 
     | 
     | 
    }  // namespace detail  | 
    
    
    149  | 
     | 
     | 
    /// @endcond  | 
    
    
    150  | 
     | 
     | 
    }  // namespace fcl  | 
    
    
    151  | 
     | 
     | 
    }  // namespace hpp  | 
    
    
    152  | 
     | 
     | 
     | 
    
    
    153  | 
     | 
     | 
    #endif  |