ArMemToImage.cpp
Go to the documentation of this file.
1 #include "ArMemToImage.h"
2 
3 #include <sstream>
4 
5 #include <Image/ByteImage.h>
6 
9 
13 
14 
15 namespace visionx::armem_images
16 {
17 
18  ArMemToImage::ArMemToImage() : detail::ImageAdapter("ArMemToImage")
19  {
20  }
21 
22 
23  template <class AronImageT>
24  void ArMemToImage::addImages(const armarx::armem::MemoryID& entityID, 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 
41  void ArMemToImage::addImagesRGB(const armarx::armem::MemoryID& entityID, const std::vector<size_t>& imageIndices)
42  {
43  addImages<arondto::ImageRGB>(entityID, imageIndices);
44  }
45 
46 
47  void ArMemToImage::addImagesDepth(const armarx::armem::MemoryID& entityID, const std::vector<size_t>& imageIndices)
48  {
49  addImages<arondto::ImageDepth>(entityID, imageIndices);
50  }
51 
52 
54  {
55  armarx::armem::MemoryID id = images.front()->instanceID.getMemoryID();
56  for (size_t i = 0; i < images.size(); ++i)
57  {
58  ARMARX_CHECK_EQUAL(images[i]->instanceID.memoryName, id.memoryName)
59  << "Currently, all images must be from the same memory.";
60  }
61  return id;
62  }
63 
64 
65  Eigen::Vector2i ArMemToImage::getImageDimensions() const
66  {
67  Eigen::Vector2i dims = dims.Zero();
68  for (const auto& image : images)
69  {
70  const cv::Mat& img = image->getImage();
71  dims(0) = std::max(dims(0), img.cols);
72  dims(1) = std::max(dims(1), img.rows);
73  }
74  return dims;
75  }
76 
77 
78  void ArMemToImage::fetchUpdates(const armarx::armem::MemoryID& entityID, const std::vector<armarx::armem::MemoryID>& updatedSnapshotIDs)
79  {
80  (void) entityID;
81  auto it = std::find_if(updatedSnapshotIDs.begin(), updatedSnapshotIDs.end(), [this](const armarx::armem::MemoryID & id)
82  {
83  return id.timestamp > timestamp;
84  });
85  if (it != updatedSnapshotIDs.end())
86  {
87  TIMING_START(Fetch);
88  fetchLatest();
90 
91  if (debugObserver)
92  {
93  debugObserver->setDebugChannel(debugObserverChannelName,
94  {
95  { "Fetch [us]", new armarx::Variant(Fetch.toMicroSecondsDouble()) },
97  });
98  }
99  }
100  }
101 
102 
104  {
105  std::stringstream ss;
106  for (size_t i = 0; i < images.size(); ++i)
107  {
108  ss << "- [" << images[i]->imageIndex << "] id=" << images[i]->instanceID << "\n";
109  }
110  return ss.str();
111  }
112 
113 
115  {
116  if (!memoryReader)
117  {
119  r.success = false;
120  r.errorMessage = "No memory reader.";
121  return r;
122  }
123 
125  for (const auto& image : images)
126  {
127  image->addQuery(qb);
128  }
129 
131  if (result.success)
132  {
133  updateAronImages(result.memory);
134  }
135  return result;
136  }
137 
138 
139  void ArMemToImage::updateAronImages(const armarx::armem::wm::Memory& memory)
140  {
141  for (const auto& image : images)
142  {
143  updated |= image->updateAronImage(memory);
144  timestamp = std::max(timestamp, image->instanceID.timestamp);
145  }
146  }
147 
148 
149  std::string ArMemToImage::printFormats() const
150  {
151  std::stringstream ss;
152  for (const auto& image : images)
153  {
154  ss << "- " << image->printFormat() << "\n";
155  }
156  return ss.str();
157  }
158 
159 }
visionx::armem_images::ArMemToImage::ArMemToImage
ArMemToImage()
Definition: ArMemToImage.cpp:18
armarx::armem::detail::SuccessHeader::success
bool success
Definition: SuccessHeader.h:20
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:224
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
TIMING_START
#define TIMING_START(name)
Definition: TimeUtil.h:280
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:116
client.h
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::armem::client::query::Builder::buildQueryInput
QueryInput buildQueryInput() const
Definition: Builder.cpp:11
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:53
detail
Definition: OpenCVUtil.cpp:127
armarx::core::time::Duration::toMilliSecondsDouble
double toMilliSecondsDouble() const
Returns the amount of milliseconds.
Definition: Duration.cpp:76
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:300
visionx::armem_images
Definition: ImageReader.cpp:29
ArMemToImage.h
armarx::armem::detail::SuccessHeader::errorMessage
std::string errorMessage
Definition: SuccessHeader.h:21
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:103
visionx::armem_images::ArMemToImage::printFormats
std::string printFormats() const
Definition: ArMemToImage.cpp:149
max
T max(T t1, T t2)
Definition: gdiam.h:48
visionx::armem_images::ArMemToImage::fetchLatest
armarx::armem::client::QueryResult fetchLatest()
Definition: ArMemToImage.cpp:114
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:47
visionx::armem_images::ArMemToImage::getImageDimensions
Eigen::Vector2i getImageDimensions() const
Definition: ArMemToImage.cpp:65
Builder.h
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:22
visionx::armem_images::ArMemToImage::fetchUpdates
void fetchUpdates(const armarx::armem::MemoryID &entityID, const std::vector< armarx::armem::MemoryID > &updatedSnapshotIDs)
Definition: ArMemToImage.cpp:78
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:33