CoFusionProcessor.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::CoFusionProcessor
17  * @author Rainer Kartmann ( rainer dot kartmann at student dot kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #ifndef _ARMARX_COMPONENT_VisionX_CoFusionProcessor_H
24 #define _ARMARX_COMPONENT_VisionX_CoFusionProcessor_H
25 
26 
27 #include <pcl/point_cloud.h>
28 #include <pcl/point_types.h>
29 
31 
34 #include <VisionX/interface/components/CoFusionProcessor.h>
35 
36 // from CoFusion
37 #include <RobotAPI/interface/core/RobotState.h>
38 
39 #include "CoFusionParams.h"
40 #include "Stopwatch.h"
41 #include <Core/Callbacks.h>
42 #include <Core/PoseMatch.h>
43 #include <Core/Utils/Intrinsics.h>
44 
45 class Model;
46 
47 namespace armarx
48 {
49  /**
50  * @class CoFusionProcessorPropertyDefinitions
51  * @brief
52  */
54  {
55  public:
58  {
59  //defineRequiredProperty<std::string>("PropertyName", "Description");
60  //defineOptionalProperty<std::string>("PropertyName", "DefaultValue", "Description");
61 
62  defineOptionalProperty<std::string>("RobotStateComponentName",
63  "RobotStateComponent",
64  "Name of the RobotState of the Robot");
65 
66  // Point Cloud Provider properties
67  defineOptionalProperty<float>("framerate", 30.0f, "framerate for the point clouds")
68  .setMin(0.0f)
69  .setMax(60.0f);
70  defineOptionalProperty<bool>(
71  "isEnabled", true, "enable the capturing process immediately");
72 
73  defineOptionalProperty<std::string>(
74  "ImageProviderName", "ImageProvider", "Name of the input image provider.");
75 
76 
77  defineOptionalProperty<int>(
78  "FramesBetweenUpdates", 1, "Number of frames between two point cloud updates.");
79 
80  defineOptionalProperty<visionx::ImageSyncMode>(
81  "PointCloudSyncMode",
82  visionx::eCaptureSynchronization,
83  "The point cloud sync mode ('capture' (default) or 'fps').")
84  .map("capture", visionx::eCaptureSynchronization)
85  .map("fps", visionx::eFpsSynchronization);
86 
87  defineOptionalProperty<float>(
88  "DownsamplingLeafSize",
89  5.0f,
90  "If > 0, the result point cloud will be downsampled with the given leaf size.");
91 
92  defineOptionalProperty<bool>(
93  "ShowTimeStats",
94  false,
95  "If true, an execution time report is logged periodically.");
96 
97  defineOptionalProperty<std::string>(
98  "ReferenceFrameName", "DepthCamera", "reference frame name.");
99 
100  // COFUSION PARAMS
101  defineOptionalProperty<int>("cofusion.TimeDelta", 200, "The CoFusion time delta.");
102  defineOptionalProperty<int>(
103  "cofusion.CountThresh", 35000, "The CoFusion count thresh.");
104  defineOptionalProperty<float>("cofusion.ErrThresh", 5e-05, "The CoFusion err thresh.");
105  defineOptionalProperty<float>("cofusion.CovThresh", 1e-05, "The CoFusion cov thresh.");
106  defineOptionalProperty<bool>("cofusion.CloseLoops", false, "The CoFusion close loops.");
107  defineOptionalProperty<bool>("cofusion.Iclnuim", false, "The CoFusion iclnuim.");
108  defineOptionalProperty<bool>("cofusion.Reloc", false, "The CoFusion reloc.");
109  defineOptionalProperty<float>(
110  "cofusion.PhotoThresh", 115, "The CoFusion photo thresh.");
111  defineOptionalProperty<float>(
112  "cofusion.InitConfidenceGlobal", 4, "The CoFusion init confidence global.");
113  defineOptionalProperty<float>(
114  "cofusion.InitConfidenceObject", 2, "The CoFusion init confidence object.");
115  defineOptionalProperty<float>("cofusion.DepthCut", 3, "The CoFusion depth cut.");
116  defineOptionalProperty<float>("cofusion.IcpThresh", 10, "The CoFusion icp thresh.");
117  defineOptionalProperty<bool>("cofusion.FastOdom", false, "The CoFusion fast odom.");
118  defineOptionalProperty<float>(
119  "cofusion.FernThresh", 0.3095, "The CoFusion fern thresh.");
120  defineOptionalProperty<bool>("cofusion.So3", true, "The CoFusion so3.");
121  defineOptionalProperty<bool>(
122  "cofusion.FrameToFrameRGB", false, "The CoFusion frame to frame r g b.");
123  defineOptionalProperty<unsigned>(
124  "cofusion.ModelSpawnOffset", 20, "The CoFusion model spawn offset.");
125  defineOptionalProperty<Model::MatchingType>(
126  "cofusion.MatchingType", Model::MatchingType::Drost, "The CoFusion matching type.");
127  defineOptionalProperty<std::string>(
128  "cofusion.ExportDirectory", "", "The CoFusion export directory.");
129  defineOptionalProperty<bool>("cofusion.ExportSegmentationResults",
130  false,
131  "The CoFusion export segmentation results.");
132 
133 #if USE_MASKFUSION == 1
134  defineOptionalProperty<bool>("cofusion.UsePrecomputedMasksOnly", false, "");
135  defineOptionalProperty<int>(
136  "cofusion.FrameQueueSize", 5, "Set size of frame-queue manually");
137  defineOptionalProperty<int>(
138  "cofusion.DeviceMaskRCNN", -1, "Select GPU (device ID) which is running MaskRCNN.");
139 
140  defineOptionalProperty<int>("cofusion.PreallocatedModelsCount",
141  1,
142  " Preallocate memory for a number of models, which can "
143  "increase performance (default: 0)");
144 #endif
145  }
146  };
147 
148  /**
149  * @defgroup Component-CoFusionProcessor CoFusionProcessor
150  * @ingroup VisionX-Components
151  * A description of the component CoFusionProcessor.
152  *
153  * @class CoFusionProcessor
154  * @ingroup Component-CoFusionProcessor
155  * @brief Brief description of class CoFusionProcessor.
156  *
157  * Detailed description of class CoFusionProcessor.
158  */
160  virtual public visionx::ImageProcessor,
162  virtual public visionx::CoFusionProcessorInterface
163  {
164  public:
165  /**
166  * @see armarx::ManagedIceObject::getDefaultName()
167  */
168  std::string
169  getDefaultName() const override
170  {
171  return "CoFusionProcessor";
172  }
173 
174  // CapturingPointCloudProviderInterface interface
175  virtual void startCaptureForNumFrames(Ice::Int numFrames,
176  const Ice::Current& c = Ice::emptyCurrent) override;
177 
178  protected:
179  // ImageProcessor interface
180  virtual void onInitImageProcessor() override;
181  /// Allocates imageRgb and imageDepth and sets images[].
182  virtual void onConnectImageProcessor() override;
183  /// Frees imageRgb and imageDepth.
184  virtual void onDisconnectImageProcessor() override;
185  virtual void onExitImageProcessor() override;
186 
187  /**
188  * @brief Performs the image processing.
189  *
190  * Processes input images, constructs FrameData and calls CoFusion::processFrame().
191  *
192  * This is the (only) thread that directly accesses cofusion.
193  * If cofusion is not allocated, it is initialized via initializeCoFusion().
194  *
195  * Every <framesBetweenUpdates> frames, updateSurfelMaps() is called.
196  */
197  virtual void process() override;
198 
199 
200  // CapturingPointCloudProvider interface
201  virtual void onInitCapturingPointCloudProvider() override;
202  virtual void onExitCapturingPointCloudProvider() override;
203  virtual void onStartCapture(float framesPerSecond) override;
204  virtual void onStopCapture() override;
205 
206  virtual bool doCapture() override;
207 
208  virtual visionx::MetaPointCloudFormatPtr getDefaultPointCloudFormat() override;
209 
210 
211  // ManagedIceObject interface (multiple inheritance)
212  virtual void onInitComponent() override;
213  virtual void onConnectComponent() override;
214  virtual void onDisconnectComponent() override;
215  virtual void onExitComponent() override;
216 
217 
218  void onNewModel(std::shared_ptr<Model> model);
219  void onInactiveModel(std::shared_ptr<Model> model);
220 
221 
222  /**
223  * @see PropertyUser::createPropertyDefinitions()
224  */
226 
227  private:
228  using OutPointT = pcl::PointXYZRGBL;
229 
230  /**
231  * @brief Initializes CoFusion.
232  * Initializes Pangolin, glew and creates a gl program.
233  * Allocates and sets cofusion.
234  */
235  void initializeCoFusion();
236 
237  /**
238  * @brief Constructs frame data from current imageRgb and imageDepth.
239  * @return the frame data
240  */
241  FrameData constructFrameData();
242 
243  /** @brief Updates surfelMaps with current CoFusion models. */
244  void updateSurfelMaps();
245 
246  /**
247  * \brief Constructs result point cloud from current surfelMaps.
248  * Allocates surfelMapMutex internally.
249  */
250  void reconstructResultPointCloud();
251 
252  /// Called periodically by stopwatchReportTask.
253  void reportStopwatchStats();
254 
255 
256  std::string imageProviderName;
257  visionx::ImageProviderInterfacePrx imageProvider;
258 
259 
260  // == CoFusion ==
261  CoFusionParams cofusionParams;
262 
263 #if USE_MASKFUSION == 1
264  std::unique_ptr<MaskFusion> cofusion;
265 #else
266  std::unique_ptr<CoFusion> cofusion;
267 #endif
268 
269 
270  // == input image processing ==
271  /// to be acquired when accessing images
272  std::mutex imagesMutex;
273 
274  /// input RGB image
275  visionx::CByteImageUPtr imageRgb;
276  /// input depth image
277  visionx::CByteImageUPtr imageDepth;
278  /// [ imageRgb, imageDepth ]
279  CByteImage* images[2];
280 
281 
282  /// Number of frames from one point cloud update to the next.
283  std::size_t framesBetweenUpdates;
284  /// Current number of frames since the latest point cloud update.
285  std::size_t framesSinceLastUpdate = 0;
286 
287 
288  // == output surfel maps for point cloud construction ==
289  /// to be acquired when accessing surfelMaps
290  std::mutex surfelMapMutex;
291 
292  /// The SurfelMaps of CoFusion models (up to framesBetweenUpdates frames old)
293  std::map<unsigned int, Model::SurfelMap> surfelMaps;
294  /// If not empty, contains the output point cloud constructed from the surfel maps.
295  pcl::PointCloud<OutPointT>::Ptr resultPointCloud;
296 
297  /// If > 0, this will be used for downsampling.
298  float downsamplingLeafSize = 0.f;
299 
300  /// Used for time stats
302 
304 
305  RobotStateComponentInterfacePrx robotStateComponent;
306 
307  std::string referenceFrameName;
308 
309  std::atomic<bool> newFrameAvailable = false;
310  };
311 } // namespace armarx
312 
313 #endif
armarx::CoFusionProcessor::getDefaultName
std::string getDefaultName() const override
Definition: CoFusionProcessor.h:169
CapturingPointCloudProvider.h
FrameData
Definition: TactileSensor.h:66
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
Stopwatch.h
visionx::ImageProcessor
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
Definition: ImageProcessor.h:98
armarx::CoFusionProcessor::onConnectComponent
virtual void onConnectComponent() override
Definition: CoFusionProcessor.cpp:154
armarx::CoFusionProcessor::onDisconnectImageProcessor
virtual void onDisconnectImageProcessor() override
Frees imageRgb and imageDepth.
Definition: CoFusionProcessor.cpp:76
armarx::CoFusionProcessor::startCaptureForNumFrames
virtual void startCaptureForNumFrames(Ice::Int numFrames, const Ice::Current &c=Ice::emptyCurrent) override
Definition: CoFusionProcessor.cpp:453
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
PeriodicTask.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::CoFusionProcessor::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: CoFusionProcessor.cpp:581
visionx::ImageProcessorPropertyDefinitions::ImageProcessorPropertyDefinitions
ImageProcessorPropertyDefinitions(std::string prefix)
Definition: ImageProcessor.h:64
armarx::CoFusionProcessor::process
virtual void process() override
Performs the image processing.
Definition: CoFusionProcessor.cpp:239
armarx::CoFusionProcessor::onDisconnectComponent
virtual void onDisconnectComponent() override
Hook for subclass.
Definition: CoFusionProcessor.cpp:212
armarx::CoFusionProcessor::onStopCapture
virtual void onStopCapture() override
This is called when the point cloud provider capturing has been stopped.
Definition: CoFusionProcessor.cpp:460
armarx::CoFusionProcessor::doCapture
virtual bool doCapture() override
Main capturing function.
Definition: CoFusionProcessor.cpp:465
armarx::CoFusionProcessor::onInitCapturingPointCloudProvider
virtual void onInitCapturingPointCloudProvider() override
This is called when the Component::onInitComponent() is called.
Definition: CoFusionProcessor.cpp:94
armarx::CoFusionProcessor::onInitComponent
virtual void onInitComponent() override
Definition: CoFusionProcessor.cpp:105
visionx::CByteImageUPtr
std::unique_ptr< CByteImage > CByteImageUPtr
Definition: ImageProvider.h:56
armarx::CoFusionProcessor::onStartCapture
virtual void onStartCapture(float framesPerSecond) override
This is called when the point cloud provider capturing has been started.
Definition: CoFusionProcessor.cpp:448
armarx::CoFusionProcessor::getDefaultPointCloudFormat
virtual visionx::MetaPointCloudFormatPtr getDefaultPointCloudFormat() override
default point cloud format used to initialize shared memory
Definition: CoFusionProcessor.cpp:565
ImageProcessor.h
armarx::CoFusionProcessor::onNewModel
void onNewModel(std::shared_ptr< Model > model)
Definition: CoFusionProcessor.cpp:226
stopwatch
Definition: Stopwatch.cpp:3
armarx::CoFusionProcessor::onExitCapturingPointCloudProvider
virtual void onExitCapturingPointCloudProvider() override
This is called when the Component::onExitComponent() setup is called.
Definition: CoFusionProcessor.cpp:100
armarx::CoFusionProcessor::onExitImageProcessor
virtual void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: CoFusionProcessor.cpp:89
stopwatch::Stopwatch
Definition: Stopwatch.h:30
armarx::CoFusionParams
Definition: CoFusionParams.h:14
visionx::CapturingPointCloudProvider
The CapturingPointCloudProvider provides a callback function to trigger the capturing of point clouds...
Definition: CapturingPointCloudProvider.h:58
CoFusionParams.h
armarx::CoFusionProcessor
Brief description of class CoFusionProcessor.
Definition: CoFusionProcessor.h:159
armarx::CoFusionProcessor::onInactiveModel
void onInactiveModel(std::shared_ptr< Model > model)
Definition: CoFusionProcessor.cpp:233
armarx::CoFusionProcessorPropertyDefinitions::CoFusionProcessorPropertyDefinitions
CoFusionProcessorPropertyDefinitions(std::string prefix)
Definition: CoFusionProcessor.h:56
armarx::CoFusionProcessor::onExitComponent
virtual void onExitComponent() override
Definition: CoFusionProcessor.cpp:219
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::ImageProcessorPropertyDefinitions
Definition: ImageProcessor.h:61
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
armarx::CoFusionProcessor::onInitImageProcessor
virtual void onInitImageProcessor() override
Setup the vision component.
Definition: CoFusionProcessor.cpp:44
armarx::CoFusionProcessor::onConnectImageProcessor
virtual void onConnectImageProcessor() override
Allocates imageRgb and imageDepth and sets images[].
Definition: CoFusionProcessor.cpp:52
armarx::CoFusionProcessorPropertyDefinitions
Definition: CoFusionProcessor.h:53
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27