OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ImagingInterface.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_ImagingInterface_h_GUID_E9CA5277_A0F5_488E_9E65_8541D437187C
26 #define INCLUDED_ImagingInterface_h_GUID_E9CA5277_A0F5_488E_9E65_8541D437187C
27 
28 // Internal Includes
34 
35 // Library/third-party includes
36 #include <opencv2/core/core.hpp>
37 
38 // Standard includes
39 #include <iosfwd>
40 #include <cstring> // for std::memcpy
41 
42 namespace osvr {
43 namespace pluginkit {
51  class ImagingMessage {
56  public:
60  ImagingMessage(cv::Mat const &frame, OSVR_ChannelCount sensor = 0)
61  : m_sensor(sensor) {
62  std::size_t bytes = frame.total() * frame.elemSize();
63  m_buf = reinterpret_cast<OSVR_ImageBufferElement *>(
64  util::alignedAlloc(bytes));
65  std::memcpy(m_buf, frame.data, bytes);
66  m_frame = cv::Mat(frame.size(), frame.type(), m_buf);
67  }
68  ~ImagingMessage() {
69  util::alignedFree(m_buf);
70  m_buf = NULL;
71  }
72 
74  cv::Mat const &getFrame() const { return m_frame; }
75 
77  OSVR_ImageBufferElement *getBuf() const { return m_buf; }
78 
80  OSVR_ChannelCount getSensor() const { return m_sensor; }
81 
82  private:
83  // noncopyable
85  // nonassignable
86  ImagingMessage &operator=(ImagingMessage const &);
87  cv::Mat m_frame;
89  OSVR_ChannelCount m_sensor;
90  };
91 
94  public:
99  : m_iface(iface) {}
100 
104  OSVR_ChannelCount numSensors = 1) {
105  OSVR_ReturnCode ret =
106  osvrDeviceImagingConfigure(opts, &m_iface, numSensors);
107  if (OSVR_RETURN_SUCCESS != ret) {
108  throw std::logic_error("Could not initialize an Imaging "
109  "Interface with the device options "
110  "given!");
111  }
112  }
113 
116  void send(DeviceToken &dev, ImagingMessage const &message,
117  OSVR_TimeValue const &timestamp) {
118  if (!m_iface) {
119  throw std::logic_error(
120  "Must initialize the imaging interface before using it!");
121  }
122  cv::Mat const &frame(message.getFrame());
123  util::NumberTypeData typedata =
124  util::opencvNumberTypeData(frame.type());
125  OSVR_ImagingMetadata metadata;
126  metadata.channels = frame.channels();
127  metadata.depth = static_cast<OSVR_ImageDepth>(typedata.getSize());
128  metadata.width = frame.cols;
129  metadata.height = frame.rows;
130  metadata.type = typedata.isFloatingPoint()
131  ? OSVR_IVT_FLOATING_POINT
132  : (typedata.isSigned() ? OSVR_IVT_SIGNED_INT
133  : OSVR_IVT_UNSIGNED_INT);
134 
135  OSVR_ReturnCode ret = osvrDeviceImagingReportFrame(
136  dev, m_iface, metadata, message.getBuf(), message.getSensor(),
137  &timestamp);
138  if (OSVR_RETURN_SUCCESS != ret) {
139  throw std::runtime_error("Could not send imaging message!");
140  }
141  }
142 
143  private:
145  };
147 } // namespace pluginkit
148 } // namespace osvr
149 
150 #endif // INCLUDED_ImagingInterface_h_GUID_E9CA5277_A0F5_488E_9E65_8541D437187C
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
Wrapper class for OSVR_DeviceToken.
Runtime data on numeric types.
ImagingInterface(OSVR_ImagingDeviceInterface iface=NULL)
Default constructor or constructor from an existing OSVR_ImagingDeviceInterface (assumed to be regist...
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
A class wrapping a cv::Mat representing a frame, as well as the sensor ID it corresponds to...
typedefOSVR_EXTERN_C_BEGIN struct OSVR_ImagingDeviceInterfaceObject * OSVR_ImagingDeviceInterface
Opaque type used in conjunction with a device token to send data on an imaging interface.
ImagingInterface(OSVR_DeviceInitOptions opts, OSVR_ChannelCount numSensors=1)
Constructor that registers an imaging interface with the device init options object.
A class wrapping an imaging interface for a device.
struct OSVR_DeviceInitObject * OSVR_DeviceInitOptions
Opaque type of a device initialization object.
Header defining an aligned memory allocator.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
OSVR_ChannelCount getSensor() const
Gets the sensor number.
void send(DeviceToken &dev, ImagingMessage const &message, OSVR_TimeValue const &timestamp)
Send method - usually called by osvr::pluginkit::DeviceToken::send()
void * alignedAlloc(size_t bytes, size_t alignment=OSVR_DEFAULT_ALIGN_SIZE)
Aligned allocation function, gives a pointer to a block of memory aligned to a memory boundary...
Definition: AlignedMemory.h:45
OSVR_ImageBufferElement * getBuf() const
Retrieves the (cloned) buffer pointer.
OSVR_ReturnCode osvrDeviceImagingConfigure(OSVR_DeviceInitOptions opts, OSVR_ImagingDeviceInterface *iface, OSVR_ChannelCount numSensors=1)
Specify that your device will implement the Imaging interface.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
unsigned char OSVR_ImageBufferElement
Type for raw buffer access to image data.
OSVR_ReturnCode osvrDeviceImagingReportFrame(OSVR_DeviceToken dev, OSVR_ImagingDeviceInterface iface, OSVR_ImagingMetadata metadata, OSVR_ImageBufferElement *imageData, OSVR_ChannelCount sensor, OSVR_TimeValue const *timestamp)
Report a frame for a sensor. Takes ownership of the buffer and frees it with the osvrAlignedFree func...
void alignedFree(void *p)
Aligned deallocation function, uses the pointer to the original memory block to deallocate it...
Definition: AlignedMemory.h:56
cv::Mat const & getFrame() const
Retrieves a reference to the cv::Mat object.
Header providing C++ interface wrappers around functionality in DeviceInterfaceC.h.
ImagingMessage(cv::Mat const &frame, OSVR_ChannelCount sensor=0)
Constructor, optionally taking a sensor number. Clones the supplied image/data as the imaging interfa...