ArMemToImage.cpp
Go to the documentation of this file.
1 #include "ArMemToImage.h"
2 
3 #include <sstream>
4 
7 
11 
12 #include <Image/ByteImage.h>
13 
14 namespace visionx::armem_images
15 {
16 
17  ArMemToImage::ArMemToImage() : detail::ImageAdapter("ArMemToImage")
18  {
19  }
20 
21  template <class AronImageT>
22  void
23  ArMemToImage::addImages(const armarx::armem::MemoryID& entityID,
24  const std::vector<size_t>& imageIndices)
25  {
26  for (size_t i = 0; i < imageIndices.size(); ++i)
27  {
28  armarx::armem::MemoryID instanceID = entityID;
29  instanceID.instanceIndex = static_cast<int>(i);
30 
31  size_t index = imageIndices[i];
32  if (index >= images.size())
33  {
34  images.resize(index + 1);
35  }
36  images[index].reset(new Image<AronImageT>(instanceID, index));
37  }
38  }
39 
40  void
42  const std::vector<size_t>& imageIndices)
43  {
44  addImages<arondto::ImageRGB>(entityID, imageIndices);
45  }
46 
47  void
49  const std::vector<size_t>& imageIndices)
50  {
51  addImages<arondto::ImageDepth>(entityID, imageIndices);
52  }
53 
56  {
57  armarx::armem::MemoryID id = images.front()->instanceID.getMemoryID();
58  for (size_t i = 0; i < images.size(); ++i)
59  {
60  ARMARX_CHECK_EQUAL(images[i]->instanceID.memoryName, id.memoryName)
61  << "Currently, all images must be from the same memory.";
62  }
63  return id;
64  }
65 
66  Eigen::Vector2i
68  {
69  Eigen::Vector2i dims = dims.Zero();
70  for (const auto& image : images)
71  {
72  const cv::Mat& img = image->getImage();
73  dims(0) = std::max(dims(0), img.cols);
74  dims(1) = std::max(dims(1), img.rows);
75  }
76  return dims;
77  }
78 
79  void
81  const std::vector<armarx::armem::MemoryID>& updatedSnapshotIDs)
82  {
83  (void)entityID;
84  auto it = std::find_if(updatedSnapshotIDs.begin(),
85  updatedSnapshotIDs.end(),
86  [this](const armarx::armem::MemoryID& id)
87  { return id.timestamp > timestamp; });
88  if (it != updatedSnapshotIDs.end())
89  {
90  TIMING_START(Fetch);
91  fetchLatest();
93 
94  if (debugObserver)
95  {
96  debugObserver->setDebugChannel(
98  {
99  {"Fetch [us]", new armarx::Variant(Fetch.toMicroSecondsDouble())},
100  {"Timestamp [ms]",
101  new armarx::Variant(
103  });
104  }
105  }
106  }
107 
108  std::string
110  {
111  std::stringstream ss;
112  for (size_t i = 0; i < images.size(); ++i)
113  {
114  ss << "- [" << images[i]->imageIndex << "] id=" << images[i]->instanceID << "\n";
115  }
116  return ss.str();
117  }
118 
121  {
122  if (!memoryReader)
123  {
125  r.success = false;
126  r.errorMessage = "No memory reader.";
127  return r;
128  }
129 
131  for (const auto& image : images)
132  {
133  image->addQuery(qb);
134  }
135 
137  if (result.success)
138  {
139  updateAronImages(result.memory);
140  }
141  return result;
142  }
143 
144  void
145  ArMemToImage::updateAronImages(const armarx::armem::wm::Memory& memory)
146  {
147  for (const auto& image : images)
148  {
149  updated |= image->updateAronImage(memory);
150  timestamp = std::max(timestamp, image->instanceID.timestamp);
151  }
152  }
153 
154  std::string
156  {
157  std::stringstream ss;
158  for (const auto& image : images)
159  {
160  ss << "- " << image->printFormat() << "\n";
161  }
162  return ss.str();
163  }
164 
165 } // namespace visionx::armem_images
visionx::armem_images::ArMemToImage::ArMemToImage
ArMemToImage()
Definition: ArMemToImage.cpp:17
armarx::armem::detail::SuccessHeader::success
bool success
Definition: SuccessHeader.h:19
visionx::armem_images::ArMemToImage::timestamp
armarx::armem::Time timestamp
Definition: ArMemToImage.h:53
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
TIMING_START
#define TIMING_START(name)
Definition: TimeUtil.h:289
armarx::armem::client::QueryResult::memory
wm::Memory memory
The slice of the memory that matched the query.
Definition: Query.h:58
visionx::armem_images::ArMemToImage::memoryReader
armarx::armem::client::Reader memoryReader
Definition: ArMemToImage.h:50
armarx::core::time::DateTime::toDurationSinceEpoch
Duration toDurationSinceEpoch() const
Definition: DateTime.cpp:105
client.h
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::armem::client::query::Builder::buildQueryInput
QueryInput buildQueryInput() const
Definition: Builder.cpp:12
visionx::armem_images::detail::ImageAdapter::debugObserverChannelName
std::string debugObserverChannelName
Definition: ImageAdapter.h:34
visionx::armem_images::ArMemToImage::getMemoryID
armarx::armem::MemoryID getMemoryID() const override
Definition: ArMemToImage.cpp:55
detail
Definition: OpenCVUtil.cpp:128
armarx::core::time::Duration::toMilliSecondsDouble
double toMilliSecondsDouble() const
Returns the amount of milliseconds.
Definition: Duration.cpp:66
query_fns.h
armarx::armem::MemoryID::instanceIndex
int instanceIndex
Definition: MemoryID.h:55
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
visionx::armem_images::ArMemToImage::updated
bool updated
Definition: ArMemToImage.h:54
TIMING_END_STREAM
#define TIMING_END_STREAM(name, os)
Definition: TimeUtil.h:310
visionx::armem_images
Definition: ImageReader.cpp:29
ArMemToImage.h
armarx::armem::detail::SuccessHeader::errorMessage
std::string errorMessage
Definition: SuccessHeader.h:20
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
visionx::armem_images::detail::ImageAdapter::debugObserver
armarx::DebugObserverInterfacePrx debugObserver
Definition: ImageAdapter.h:33
visionx::armem_images::ArMemToImage::addImagesRGB
void addImagesRGB(const armarx::armem::MemoryID &entityID, const std::vector< size_t > &imageIndices) override
Definition: ArMemToImage.cpp:41
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
visionx::armem_images::ArMemToImage::summarizeStructure
std::string summarizeStructure() const override
Definition: ArMemToImage.cpp:109
visionx::armem_images::ArMemToImage::printFormats
std::string printFormats() const
Definition: ArMemToImage.cpp:155
max
T max(T t1, T t2)
Definition: gdiam.h:51
visionx::armem_images::ArMemToImage::fetchLatest
armarx::armem::client::QueryResult fetchLatest()
Definition: ArMemToImage.cpp:120
armarx::armem::MemoryID::memoryName
std::string memoryName
Definition: MemoryID.h:50
TimeUtil.h
visionx::armem_images::ArMemToImage::addImagesDepth
void addImagesDepth(const armarx::armem::MemoryID &entityID, const std::vector< size_t > &imageIndices) override
Definition: ArMemToImage.cpp:48
visionx::armem_images::ArMemToImage::getImageDimensions
Eigen::Vector2i getImageDimensions() const
Definition: ArMemToImage.cpp:67
Builder.h
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:21
visionx::armem_images::ArMemToImage::fetchUpdates
void fetchUpdates(const armarx::armem::MemoryID &entityID, const std::vector< armarx::armem::MemoryID > &updatedSnapshotIDs)
Definition: ArMemToImage.cpp:80
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
Variant.h
visionx::armem_images::ArMemToImage::images
std::vector< std::unique_ptr< ImageBase > > images
Definition: ArMemToImage.h:52
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query.
Definition: Reader.cpp:32