1 /*
2  * Software License Agreement (BSD License)
3  *
4  *  Copyright (c) 2011, Willow Garage, Inc.
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *   * Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *   * Redistributions in binary form must reproduce the above
14  *     copyright notice, this list of conditions and the following
15  *     disclaimer in the documentation and/or other materials provided
16  *     with the distribution.
17  *   * Neither the name of Willow Garage, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  * @author: Koen Buys, Anatoly Baksheev
35  */
36 
37 #pragma once
38 
39 #include <pcl/memory.h>
40 #include <pcl/pcl_exports.h>
41 #include <pcl/point_types.h>
42 #include <pcl/point_cloud.h>
43 #include <pcl/gpu/containers/device_array.h>
44 #include <pcl/gpu/people/colormap.h>
45 #include <pcl/gpu/people/label_blob2.h>
46 #include <pcl/gpu/people/label_common.h>
47 #include "pcl/gpu/people/person_attribs.h"
48 
49 #include <string>
50 #include <vector>
51 
52 namespace pcl
53 {
54   namespace device
55   {
56     // This just seems hacky ...
57     class MultiTreeLiveProc;
58   }
59 
60   namespace gpu
61   {
62     namespace people
63     {
64       class PCL_EXPORTS RDFBodyPartsDetector
65       {
66         public:
67           using Ptr = shared_ptr<RDFBodyPartsDetector>;
68           using ConstPtr = shared_ptr<const RDFBodyPartsDetector>;
69           using BlobMatrix = std::vector<std::vector<Blob2, Eigen::aligned_allocator<Blob2> > >;
70 
71           using Labels = DeviceArray2D<unsigned char>;
72           using Depth = DeviceArray2D<unsigned short>;
73           using Image = DeviceArray2D<pcl::RGB>;
74 
75           /** \brief This is the constructor **/
76           RDFBodyPartsDetector(const std::vector<std::string>& tree_files,
77               int default_buffer_rows = 480, int default_buffer_cols = 640);
78 
79           /**
80            * \brief This function does the complete RDF evaluation in a discrete manner and builds the blob matrix
81            */
82           void process(const Depth& depth, const PointCloud<PointXYZ>& cloud, int min_pts_per_cluster);
83           // This are the different sub-parts of process()
84           /**
85            * \brief This function processes based on the RDF with probabilistic voting scheme
86            */
87           void processProb (const Depth& depth);
88 
89           /**
90            * \brief This smooths the labels and does the connected components
91            */
92           void processSmooth (const Depth& depth, const PointCloud<PointXYZ>& cloud, int min_pts_per_cluster);
93 
94           /**
95            * \brief This processes the blob_matrix_
96            * \return negative if failed
97            */
98           int
99           processRelations ();
100 
101           /**
102            * \brief This processes the blob_matrix_
103            * \param[in] person_attribs the custom person parameters
104            * \return negative if failed
105            */
106           int
107           processRelations (PersonAttribs::Ptr person_attribs);
108 
109           //getters
110           const Labels& getLabels() const;
111           const pcl::device::LabelProbability& getProbability() const;
112           const pcl::device::LabelProbability& getProbability1() const;
113           const pcl::device::LabelProbability& getProbability2() const;
114           const pcl::device::LabelProbability& getPrevProbability1() const;
115           const pcl::device::LabelProbability& getPrevProbability2() const;
116           std::size_t getNumberTrees() const;
117           const BlobMatrix& getBlobMatrix() const;
118 
119 
120           /** \brief This contains the final body part labels **/
121           Labels labels_;
122           /** \brief This contains the smoothed final body part labels **/
123           Labels labels_smoothed_;
124 
125           /** These contain the histograms of the labels for this detector **/
126           pcl::device::LabelProbability P_l_;             // the one is current worked in
127           pcl::device::LabelProbability P_l_Gaus_;        // the current Gaussian buffer
128           pcl::device::LabelProbability P_l_Gaus_Temp_;   // the current Gaussian buffer to store the intermediate
129           pcl::device::LabelProbability P_l_1_;           // storage for the first iteration
130           pcl::device::LabelProbability P_l_2_;           // storage for the second iteration
131 
132           /** These contain the histograms of the labels for this detector of the previous timestep **/
133           pcl::device::LabelProbability P_l_prev_1_;  // for the first iteration
134           pcl::device::LabelProbability P_l_prev_2_;  // for the second iteration
135 
136         private:
137           std::shared_ptr<device::MultiTreeLiveProc> impl_;
138 
139           int max_cluster_size_;
140           float cluster_tolerance_;
141 
142           BlobMatrix blob_matrix_;
143 
144           // hide this buffer using pImpl
145           std::vector<unsigned char>    lmap_host_;
146           std::vector<int>              dst_labels_;
147           std::vector<int>              region_sizes_;
148           std::vector<int>              remap_;
149           std::vector<float>            means_storage_;
150           DeviceArray2D<int>            comps_;
151           DeviceArray2D<unsigned char>  edges_;
152 
153           void allocate_buffers(int rows = 480, int cols = 640);
154       };
155     }
156   }
157 }
158