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
45class Model;
46
47namespace 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);
71 "isEnabled", true, "enable the capturing process immediately");
72
74 "ImageProviderName", "ImageProvider", "Name of the input image provider.");
75
76
78 "FramesBetweenUpdates", 1, "Number of frames between two point cloud updates.");
79
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
88 "DownsamplingLeafSize",
89 5.0f,
90 "If > 0, the result point cloud will be downsampled with the given leaf size.");
91
93 "ShowTimeStats",
94 false,
95 "If true, an execution time report is logged periodically.");
96
98 "ReferenceFrameName", "DepthCamera", "reference frame name.");
99
100 // COFUSION PARAMS
101 defineOptionalProperty<int>("cofusion.TimeDelta", 200, "The CoFusion time delta.");
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.");
110 "cofusion.PhotoThresh", 115, "The CoFusion photo thresh.");
112 "cofusion.InitConfidenceGlobal", 4, "The CoFusion init confidence global.");
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.");
119 "cofusion.FernThresh", 0.3095, "The CoFusion fern thresh.");
120 defineOptionalProperty<bool>("cofusion.So3", true, "The CoFusion so3.");
122 "cofusion.FrameToFrameRGB", false, "The CoFusion frame to frame r g b.");
124 "cofusion.ModelSpawnOffset", 20, "The CoFusion model spawn offset.");
126 "cofusion.MatchingType", Model::MatchingType::Drost, "The CoFusion matching type.");
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, "");
136 "cofusion.FrameQueueSize", 5, "Set size of frame-queue manually");
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
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
constexpr T c
Brief description of class CoFusionProcessor.
virtual void onInitComponent() override
Pure virtual hook for the subclass.
virtual void onConnectImageProcessor() override
Allocates imageRgb and imageDepth and sets images[].
virtual bool doCapture() override
Main capturing function.
virtual void onExitCapturingPointCloudProvider() override
This is called when the Component::onExitComponent() setup is called.
virtual void onDisconnectComponent() override
Hook for subclass.
virtual void onExitImageProcessor() override
Exit the ImapeProcessor component.
void onNewModel(std::shared_ptr< Model > model)
virtual void onStartCapture(float framesPerSecond) override
This is called when the point cloud provider capturing has been started.
virtual visionx::MetaPointCloudFormatPtr getDefaultPointCloudFormat() override
default point cloud format used to initialize shared memory
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
virtual void process() override
Performs the image processing.
virtual void onInitCapturingPointCloudProvider() override
This is called when the Component::onInitComponent() is called.
virtual void onInitImageProcessor() override
Setup the vision component.
virtual void onConnectComponent() override
Pure virtual hook for the subclass.
virtual void onStopCapture() override
This is called when the point cloud provider capturing has been stopped.
virtual void onDisconnectImageProcessor() override
Frees imageRgb and imageDepth.
virtual void onExitComponent() override
Hook for subclass.
virtual void startCaptureForNumFrames(Ice::Int numFrames, const Ice::Current &c=Ice::emptyCurrent) override
std::string getDefaultName() const override
void onInactiveModel(std::shared_ptr< Model > model)
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
The CapturingPointCloudProvider provides a callback function to trigger the capturing of point clouds...
ImageProcessorPropertyDefinitions(std::string prefix)
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx
ArmarX headers.
std::unique_ptr< CByteImage > CByteImageUPtr