Point Cloud Library (PCL)  1.14.0-dev
octree_ram_container.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  * Copyright (c) 2012, Urban Robotics, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of Willow Garage, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: octree_ram_container.hpp 6927 2012-08-23 02:34:54Z stfox88 $
38  */
39 
40 #ifndef PCL_OUTOFCORE_RAM_CONTAINER_IMPL_H_
41 #define PCL_OUTOFCORE_RAM_CONTAINER_IMPL_H_
42 
43 // C++
44 #include <sstream>
45 
46 // PCL (Urban Robotics)
47 #include <pcl/outofcore/octree_ram_container.h>
48 
49 namespace pcl
50 {
51  namespace outofcore
52  {
53  template<typename PointT>
55 
56  template<typename PointT>
57  std::mt19937 OutofcoreOctreeRamContainer<PointT>::rng_ ([] {std::random_device rd; return rd(); } ());
58 
59  template<typename PointT> void
60  OutofcoreOctreeRamContainer<PointT>::convertToXYZ (const boost::filesystem::path& path)
61  {
62  if (!container_.empty ())
63  {
64  FILE* fxyz = fopen (path.string ().c_str (), "we");
65 
66  std::uint64_t num = size ();
67  for (std::uint64_t i = 0; i < num; i++)
68  {
69  const PointT& p = container_[i];
70 
71  std::stringstream ss;
72  ss << std::fixed;
73  ss.precision (16);
74  ss << p.x << "\t" << p.y << "\t" << p.z << "\n";
75 
76  fwrite (ss.str ().c_str (), 1, ss.str ().size (), fxyz);
77  }
78 
79  assert ( fclose (fxyz) == 0 );
80  }
81  }
82 
83  ////////////////////////////////////////////////////////////////////////////////
84 
85  template<typename PointT> void
86  OutofcoreOctreeRamContainer<PointT>::insertRange (const PointT* start, const std::uint64_t count)
87  {
88  container_.insert (container_.end (), start, start + count);
89  }
90 
91  ////////////////////////////////////////////////////////////////////////////////
92 
93  template<typename PointT> void
94  OutofcoreOctreeRamContainer<PointT>::insertRange (const PointT* const * start, const std::uint64_t count)
95  {
97  temp.resize (count);
98  for (std::uint64_t i = 0; i < count; i++)
99  {
100  temp[i] = *start[i];
101  }
102  container_.insert (container_.end (), temp.begin (), temp.end ());
103  }
104 
105  ////////////////////////////////////////////////////////////////////////////////
106 
107  template<typename PointT> void
108  OutofcoreOctreeRamContainer<PointT>::readRange (const std::uint64_t start, const std::uint64_t count,
110  {
111  v.resize (count);
112  std::copy(container_.cbegin() + start, container_.cbegin() + start + count, v.begin());
113  }
114 
115  ////////////////////////////////////////////////////////////////////////////////
116 
117  template<typename PointT> void
119  const std::uint64_t count,
120  const double percent,
122  {
123  auto samplesize = static_cast<std::uint64_t> (percent * static_cast<double> (count));
124 
125  std::lock_guard<std::mutex> lock (rng_mutex_);
126 
127  std::uniform_int_distribution < std::uint64_t > buffdist (start, start + count);
128 
129  for (std::uint64_t i = 0; i < samplesize; i++)
130  {
131  std::uint64_t buffstart = buffdist (rng_);
132  v.push_back (container_[buffstart]);
133  }
134  }
135 
136  ////////////////////////////////////////////////////////////////////////////////
137 
138  }//namespace outofcore
139 }//namespace pcl
140 
141 #endif //PCL_OUTOFCORE_RAM_CONTAINER_IMPL_H_
void readRange(const std::uint64_t start, const std::uint64_t count, AlignedPointTVector &v)
void convertToXYZ(const boost::filesystem::path &path)
Writes ascii x,y,z point data to path.string().c_str()
void readRangeSubSample(const std::uint64_t start, const std::uint64_t count, const double percent, AlignedPointTVector &v)
grab percent*count random points.
void insertRange(const PointT *start, const std::uint64_t count)
inserts count number of points into container; uses the container_ type's insert function
typename OutofcoreAbstractNodeContainer< PointT >::AlignedPointTVector AlignedPointTVector
A point structure representing Euclidean xyz coordinates, and the RGB color.