PointCloudProvider.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Core
19  * @author David Gonzalez Aguirre (david dot gonzalez at kit dot edu)
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
32 
33 
34 #include <VisionX/interface/core/DataTypes.h>
35 #include <VisionX/interface/core/PointCloudProcessorInterface.h>
36 #include <VisionX/interface/core/PointCloudProviderInterface.h>
37 
38 #include "PointCloudConversions.h"
39 
40 namespace visionx
41 {
42 
45  {
46  public:
49  {
50  defineOptionalProperty<std::string>("frameName", "", "name of the source");
51  }
52  };
53 
54  // ====================================================================== //
55  // == class PointCloudProvider declaration ============================== //
56  // ====================================================================== //
61  virtual public armarx::Component,
62  virtual public PointCloudProviderInterface
63  {
64 
65  public:
66  // ================================================================== //
67  // == PointCloudProvider ice interface ============================== //
68  // ================================================================== //
69 
73  armarx::Blob getPointCloud(MetaPointCloudFormatPtr& info, const Ice::Current& c = Ice::emptyCurrent) override;
74 
78  MetaPointCloudFormatPtr getPointCloudFormat(const Ice::Current& c = Ice::emptyCurrent) override;
79 
80  bool hasSharedMemorySupport(const Ice::Current& c = Ice::emptyCurrent) override
81  {
82  return true;
83  }
84 
85  void enablePointCloudVisualization(bool enable, bool preferRGB, const Ice::Current& c = Ice::emptyCurrent);
86 
87  protected:
88  // ================================================================== //
89  // == Interface of PointCloudProvider =============================== //
90  // ================================================================== //
97  virtual void onInitPointCloudProvider() = 0;
98 
102  virtual void onConnectPointCloudProvider() {}
103 
109  virtual void onExitPointCloudProvider() = 0;
110 
111 
115  template<typename PointCloudPtrT>
116  void providePointCloud(PointCloudPtrT pointCloudPtr)
117  {
118  using PointCloudT = typename PointCloudPtrT::element_type;
119  using PointT = typename PointCloudT::PointType;
120 
121  if (!sharedMemoryProvider)
122  {
123  ARMARX_WARNING << "Shared memory provider is null (possibly shutting down)";
124  return;
125  }
126  MetaPointCloudFormatPtr info(MetaPointCloudFormatPtr::dynamicCast(sharedMemoryProvider->getMetaInfo()->ice_clone()));
128  info->type = visionx::tools::getPointContentType<PointT>();
129  info->size = pointCloudPtr->width * pointCloudPtr->height * visionx::tools::getBytesPerPoint(info->type);
130 
131  if (info->size > info->capacity)
132  {
133  const size_t maxPoints = info->capacity / visionx::tools::getBytesPerPoint(info->type);
134  ARMARX_ERROR << deactivateSpam(1) << "Exceeding the capacity limit of supported points ("
135  << (maxPoints) << " points). The provided point cloud contains "
136  << pointCloudPtr->width* pointCloudPtr->height
137  << " points. Cropping the point cloud to fit into limit.";
138 
139  pointCloudPtr->width = maxPoints;
140  pointCloudPtr->height = 1;
141  pointCloudPtr->resize(maxPoints);
142  info->size = maxPoints * visionx::tools::getBytesPerPoint(info->type);
143  }
144 
145  info->width = pointCloudPtr->width;
146  info->height = pointCloudPtr->height;
147 
148  if (pointCloudPtr->header.stamp)
149  {
150  info->timeProvided = pointCloudPtr->header.stamp;
151  }
152  else
153  {
154  info->timeProvided = armarx::TimeUtil::GetTime().toMicroSeconds();
155  }
156 
157  if (pointCloudPtr->header.seq)
158  {
159  info->seq = pointCloudPtr->header.seq;
160  }
161  else
162  {
163  info->seq = ++seq;
164  }
165 
166  // if (!pointCloudPtr->header.frame_id.empty())
167  // {
168  // info->frameId = pointCloudPtr->header.frame_id;
169  // }
170  ARMARX_CHECK_GREATER_EQUAL((int)intermediateBuffer.size(), info->capacity);
171  ARMARX_CHECK_GREATER_EQUAL((int)intermediateBuffer.size(), info->size);
172  ARMARX_CHECK_GREATER(intermediateBuffer.size(), 0);
173  auto buf = intermediateBuffer.data();
175  visionx::tools::convertFromPCL(pointCloudPtr, (void**) &buf);
176  {
177  armarx::SharedMemoryScopedWriteLockPtr lock(sharedMemoryProvider->getScopedWriteLock());
178 
179  sharedMemoryProvider->setMetaInfo(info, false);
180 
181  unsigned char* buffer = sharedMemoryProvider->getBuffer();
182 
183  memcpy(buffer, intermediateBuffer.data(), info->size);
184  }
186  // notify processors
187  pointCloudProcessorProxy->reportPointCloudAvailable(getName());
188  }
189 
190  // ================================================================== //
191  // == Component implementation ====================================== //
192  // ================================================================== //
196  void onInitComponent() override;
197 
201  void onConnectComponent() override;
202 
203  void onDisconnectComponent() override;
204 
208  void onExitComponent() override;
209 
210  protected:
211 
217  bool isExiting() const
218  {
219  return exiting;
220  }
221 
222  void updateComponentMetaInfo(const MetaPointCloudFormatPtr& info);
223 
227  PointCloudProcessorInterfacePrx pointCloudProcessorProxy;
228 
232  virtual MetaPointCloudFormatPtr getDefaultPointCloudFormat();
233 
234  private:
235 
240 
244  MetaPointCloudFormatPtr pointCloudFormat;
245 
249  bool exiting;
250 
251  int seq;
252  FPSCounter fps;
253 
254  std::vector<unsigned char> intermediateBuffer;
255  };
256 }
257 
visionx::PointCloudProvider::updateComponentMetaInfo
void updateComponentMetaInfo(const MetaPointCloudFormatPtr &info)
Definition: PointCloudProvider.cpp:118
armarx::IceSharedMemoryProvider::getBuffer
MemoryObject * getBuffer()
Retrieve pointer to buffer.
Definition: IceSharedMemoryProvider.h:144
visionx::PointCloudProvider::onConnectComponent
void onConnectComponent() override
Definition: PointCloudProvider.cpp:77
armarx::IceSharedMemoryProvider::getScopedWriteLock
SharedMemoryScopedWriteLockPtr getScopedWriteLock() const
Retrieve scoped lock for writing to the memory.
Definition: IceSharedMemoryProvider.h:156
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:204
ARMARX_CHECK_GREATER
#define ARMARX_CHECK_GREATER(lhs, rhs)
This macro evaluates whether lhs is greater (>) than rhs and if it turns out to be false it will thro...
Definition: ExpressionException.h:114
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
visionx::PointCloudProvider::hasSharedMemorySupport
bool hasSharedMemorySupport(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PointCloudProvider.h:80
armarx::IceSharedMemoryProvider::setMetaInfo
void setMetaInfo(const typename MemoryObjectMetaInfo::PointerType &info, bool threadSafe=true)
Definition: IceSharedMemoryProvider.h:228
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:46
visionx::PointCloudProvider::getPointCloud
armarx::Blob getPointCloud(MetaPointCloudFormatPtr &info, const Ice::Current &c=Ice::emptyCurrent) override
Retrieve point clouds via Ice.
Definition: PointCloudProvider.cpp:37
armarx::IceSharedMemoryProvider::getMetaInfo
MemoryObjectMetaInfo::PointerType getMetaInfo(bool threadSafe=true) const
getMetaInfo returns a copy of the memory object information
Definition: IceSharedMemoryProvider.h:243
visionx::PointCloudProvider::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: PointCloudProvider.cpp:95
visionx::PointCloudProvider::onExitComponent
void onExitComponent() override
Definition: PointCloudProvider.cpp:106
IceSharedMemoryProvider.h
visionx::tools::convertFromPCL
void convertFromPCL(pcl::PointCloud< pcl::PointXYZRGBA >::Ptr sourcePtr, void **target)
Definition: PointCloudConversions.cpp:121
visionx::PointCloudProviderPropertyDefinitions::PointCloudProviderPropertyDefinitions
PointCloudProviderPropertyDefinitions(std::string prefix)
Definition: PointCloudProvider.h:47
visionx::PointCloudProvider::getPointCloudFormat
MetaPointCloudFormatPtr getPointCloudFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the point cloud format info struct via Ice.
Definition: PointCloudProvider.cpp:46
visionx::PointCloudProviderPropertyDefinitions
Definition: PointCloudProvider.h:43
visionx::PointCloudProvider::pointCloudProcessorProxy
PointCloudProcessorInterfacePrx pointCloudProcessorProxy
Ice proxy of the point cloud processor interface.
Definition: PointCloudProvider.h:227
visionx::PointCloudProvider::onInitComponent
void onInitComponent() override
Definition: PointCloudProvider.cpp:59
armarx::PointCloudT
pcl::PointCloud< PointT > PointCloudT
Definition: Common.h:30
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:121
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
visionx::PointCloudProvider::getDefaultPointCloudFormat
virtual MetaPointCloudFormatPtr getDefaultPointCloudFormat()
default point cloud format used to initialize shared memory
Definition: PointCloudProvider.cpp:132
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
ExpressionException.h
visionx::PointCloudProvider::enablePointCloudVisualization
void enablePointCloudVisualization(bool enable, bool preferRGB, const Ice::Current &c=Ice::emptyCurrent)
PointCloudConversions.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:70
visionx::PointCloudProvider::providePointCloud
void providePointCloud(PointCloudPtrT pointCloudPtr)
offer the new point cloud.
Definition: PointCloudProvider.h:116
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
TimeUtil.h
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::ComponentPropertyDefinitions::ComponentPropertyDefinitions
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition: Component.cpp:37
visionx::PointCloudProvider::onInitPointCloudProvider
virtual void onInitPointCloudProvider()=0
This is called when the Component::onInitComponent() is called.
visionx::tools::getBytesPerPoint
size_t getBytesPerPoint(visionx::PointContentType pointContent)
Definition: PointCloudConversions.cpp:64
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
visionx::PointCloudProvider::isExiting
bool isExiting() const
Retrieve whether provider is exiting.
Definition: PointCloudProvider.h:217
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
FPSCounter.h
visionx::PointCloudProvider::onExitPointCloudProvider
virtual void onExitPointCloudProvider()=0
This is called when the Component::onExitComponent() setup is called.
visionx::PointCloudProvider
PointCloudProvider abstract class defines a component which provide point clouds via ice or shared me...
Definition: PointCloudProvider.h:60
visionx::PointCloudProvider::onConnectPointCloudProvider
virtual void onConnectPointCloudProvider()
This is called when the Component::onConnectComponent() setup is called.
Definition: PointCloudProvider.h:102
visionx::FPSCounter
The FPSCounter class provides methods for calculating the frames per second (FPS) count in periodic t...
Definition: FPSCounter.h:36
visionx::PointT
pcl::PointXYZRGBA PointT
Definition: MaskRCNNPointCloudObjectLocalizer.h:79