···108108109109using xrt::auxiliary::math::RelationHistory;
110110111111-using cv::Mat;
112112-using cv::MatAllocator;
113113-using cv::UMatData;
114114-using cv::UMatUsageFlags;
115115-116116-#define USING_OPENCV_3_3_1 (CV_VERSION_MAJOR == 3 && CV_VERSION_MINOR == 3 && CV_VERSION_REVISION == 1)
117117-118118-#if defined(XRT_HAVE_KIMERA) && !USING_OPENCV_3_3_1
119119-#pragma message "Kimera-VIO uses OpenCV 3.3.1, use that to prevent conflicts"
120120-#endif
121121-122122-//! @todo These defs should make OpenCV 4 work but it wasn't tested against a
123123-//! SLAM system that supports that version yet.
124124-#if CV_VERSION_MAJOR < 4
125125-#define ACCESS_RW 0
126126-typedef int AccessFlag;
127127-#define CV_AUTOSTEP 0x7fffffff // From opencv2/core/core_c.h
128128-#else
129129-using cv::ACCESS_RW;
130130-using cv::AccessFlag;
131131-#define CV_AUTOSTEP cv::Mat::AUTO_STEP
132132-#endif
133133-134134-/*!
135135- * @brief Wraps a @ref xrt_frame with a `cv::Mat` (conversely to @ref FrameMat).
136136- *
137137- * It works by implementing a `cv::MatAllocator` which determines what to do
138138- * when a `cv::Mat` refcount reaches zero. In that case, it decrements the @ref
139139- * xrt_frame refcount once the `cv::Mat` own refcount has reached zero.
140140- *
141141- * @note a @ref MatFrame `cv::Mat` can wrap a @ref FrameMat @ref xrt_frame,
142142- * which in turns wraps a `cv::Mat`, with little overhead, and that is precisely
143143- * how it is being used in this file when the @ref xrt_frame is a @ref FrameMat.
144144- */
145145-class MatFrame final : public MatAllocator
146146-{
147147-public:
148148- //! Wraps a @ref xrt_frame in a `cv::Mat`
149149- Mat
150150- wrap(struct xrt_frame *frame)
151151- {
152152- SLAM_DASSERT_(frame->format == XRT_FORMAT_L8 || frame->format == XRT_FORMAT_R8G8B8);
153153- auto img_type = frame->format == XRT_FORMAT_L8 ? CV_8UC1 : CV_8UC3;
154154-155155- // Wrap the frame data into a cv::Mat header
156156- cv::Mat img{(int)frame->height, (int)frame->width, img_type, frame->data, frame->stride};
157157-158158- // Enable reference counting for a user-allocated cv::Mat (i.e., using existing frame->data)
159159- img.u = this->allocate(img.dims, img.size.p, img.type(), img.data, img.step.p, ACCESS_RW,
160160- cv::USAGE_DEFAULT);
161161- SLAM_DASSERT_(img.u->refcount == 0);
162162- img.addref();
163163-164164- // Keep a reference to the xrt_frame in the cv userdata field for when the cv::Mat reference reaches 0
165165- SLAM_DASSERT_(img.u->userdata == NULL); // Should be default-constructed
166166- xrt_frame_reference((struct xrt_frame **)&img.u->userdata, frame);
167167-168168- return img;
169169- }
170170-171171- //! Allocates a `cv::UMatData` object which is in charge of reference counting for a `cv::Mat`
172172- UMatData *
173173- allocate(
174174- int dims, const int *sizes, int type, void *data0, size_t *step, AccessFlag, UMatUsageFlags) const override
175175- {
176176- SLAM_DASSERT_(dims == 2 && sizes && data0 && step && step[0] != CV_AUTOSTEP);
177177- UMatData *u = new UMatData(this);
178178- uchar *data = (uchar *)data0;
179179- u->data = u->origdata = data;
180180- u->size = step[0] * sizes[0]; // Row stride * row count
181181- u->flags |= UMatData::USER_ALLOCATED; // External data
182182- return u;
183183- }
184184-185185- //! Necessary but unused virtual method for a `cv::MatAllocator`
186186- bool
187187- allocate(UMatData *, AccessFlag, UMatUsageFlags) const override
188188- {
189189- SLAM_ASSERT(false, "Shouldn't be reached");
190190- return false;
191191- }
192192-193193- //! When `cv::UMatData` refcount reaches zero this method is called, we just
194194- //! decrement the original @ref xrt_frame refcount as it is the one in charge
195195- //! of the memory.
196196- void
197197- deallocate(UMatData *u) const override
198198- {
199199- SLAM_DASSERT_(u->urefcount == 0 && u->refcount == 0);
200200- SLAM_DASSERT_(u->flags & UMatData::USER_ALLOCATED);
201201- xrt_frame_reference((struct xrt_frame **)&u->userdata, NULL);
202202- delete u;
203203- }
204204-};
205205-206111207112/*
208113 *
···362267 struct u_var_button reset_state_btn; //!< Reset tracker state button
363268364269 enum u_logging_level log_level; //!< Logging level for the SLAM tracker, set by SLAM_LOG var
365365- MatFrame *cv_wrapper; //!< Wraps a xrt_frame in a cv::Mat to send to the SLAM system
366270367271 struct xrt_slam_sinks *euroc_recorder; //!< EuRoC dataset recording sinks
368272 struct openvr_tracker *ovr_tracker; //!< OpenVR lighthouse tracker
···14411345 last_ts = ts;
1442134614431347 // Construct and send the image sample
14441444- cv::Mat img = t.cv_wrapper->wrap(frame);
14451445-14461348 vit_img_sample sample = {};
14471349 sample.cam_index = cam_index;
14481350 sample.timestamp = ts;
14491449- sample.data = img.ptr();
14501450- sample.width = img.cols;
14511451- sample.height = img.rows;
14521452- sample.stride = img.step;
14531453- sample.size = img.cols * img.rows;
13511351+13521352+ sample.data = frame->data;
13531353+ sample.width = frame->width;
13541354+ sample.height = frame->height;
13551355+ sample.stride = frame->stride;
13561356+ sample.size = frame->size;
1454135714551358 // TODO check format before
14561359 switch (frame->format) {
···15331436 os_mutex_destroy(&t.lock_ff);
15341437 m_ff_vec3_f32_free(&t.filter.pos_ff);
15351438 m_ff_vec3_f32_free(&t.filter.rot_ff);
15361536-15371537- delete t_ptr->cv_wrapper;
1538143915391440 t_ptr->vit.tracker_destroy(t_ptr->tracker);
15401441 t_vit_bundle_unload(&t_ptr->vit);