The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

aux/tracking: remove MatFrame class from t_tracker_slam

+6 -105
+6 -105
src/xrt/auxiliary/tracking/t_tracker_slam.cpp
··· 108 108 109 109 using xrt::auxiliary::math::RelationHistory; 110 110 111 - using cv::Mat; 112 - using cv::MatAllocator; 113 - using cv::UMatData; 114 - using cv::UMatUsageFlags; 115 - 116 - #define USING_OPENCV_3_3_1 (CV_VERSION_MAJOR == 3 && CV_VERSION_MINOR == 3 && CV_VERSION_REVISION == 1) 117 - 118 - #if defined(XRT_HAVE_KIMERA) && !USING_OPENCV_3_3_1 119 - #pragma message "Kimera-VIO uses OpenCV 3.3.1, use that to prevent conflicts" 120 - #endif 121 - 122 - //! @todo These defs should make OpenCV 4 work but it wasn't tested against a 123 - //! SLAM system that supports that version yet. 124 - #if CV_VERSION_MAJOR < 4 125 - #define ACCESS_RW 0 126 - typedef int AccessFlag; 127 - #define CV_AUTOSTEP 0x7fffffff // From opencv2/core/core_c.h 128 - #else 129 - using cv::ACCESS_RW; 130 - using cv::AccessFlag; 131 - #define CV_AUTOSTEP cv::Mat::AUTO_STEP 132 - #endif 133 - 134 - /*! 135 - * @brief Wraps a @ref xrt_frame with a `cv::Mat` (conversely to @ref FrameMat). 136 - * 137 - * It works by implementing a `cv::MatAllocator` which determines what to do 138 - * when a `cv::Mat` refcount reaches zero. In that case, it decrements the @ref 139 - * xrt_frame refcount once the `cv::Mat` own refcount has reached zero. 140 - * 141 - * @note a @ref MatFrame `cv::Mat` can wrap a @ref FrameMat @ref xrt_frame, 142 - * which in turns wraps a `cv::Mat`, with little overhead, and that is precisely 143 - * how it is being used in this file when the @ref xrt_frame is a @ref FrameMat. 144 - */ 145 - class MatFrame final : public MatAllocator 146 - { 147 - public: 148 - //! Wraps a @ref xrt_frame in a `cv::Mat` 149 - Mat 150 - wrap(struct xrt_frame *frame) 151 - { 152 - SLAM_DASSERT_(frame->format == XRT_FORMAT_L8 || frame->format == XRT_FORMAT_R8G8B8); 153 - auto img_type = frame->format == XRT_FORMAT_L8 ? CV_8UC1 : CV_8UC3; 154 - 155 - // Wrap the frame data into a cv::Mat header 156 - cv::Mat img{(int)frame->height, (int)frame->width, img_type, frame->data, frame->stride}; 157 - 158 - // Enable reference counting for a user-allocated cv::Mat (i.e., using existing frame->data) 159 - img.u = this->allocate(img.dims, img.size.p, img.type(), img.data, img.step.p, ACCESS_RW, 160 - cv::USAGE_DEFAULT); 161 - SLAM_DASSERT_(img.u->refcount == 0); 162 - img.addref(); 163 - 164 - // Keep a reference to the xrt_frame in the cv userdata field for when the cv::Mat reference reaches 0 165 - SLAM_DASSERT_(img.u->userdata == NULL); // Should be default-constructed 166 - xrt_frame_reference((struct xrt_frame **)&img.u->userdata, frame); 167 - 168 - return img; 169 - } 170 - 171 - //! Allocates a `cv::UMatData` object which is in charge of reference counting for a `cv::Mat` 172 - UMatData * 173 - allocate( 174 - int dims, const int *sizes, int type, void *data0, size_t *step, AccessFlag, UMatUsageFlags) const override 175 - { 176 - SLAM_DASSERT_(dims == 2 && sizes && data0 && step && step[0] != CV_AUTOSTEP); 177 - UMatData *u = new UMatData(this); 178 - uchar *data = (uchar *)data0; 179 - u->data = u->origdata = data; 180 - u->size = step[0] * sizes[0]; // Row stride * row count 181 - u->flags |= UMatData::USER_ALLOCATED; // External data 182 - return u; 183 - } 184 - 185 - //! Necessary but unused virtual method for a `cv::MatAllocator` 186 - bool 187 - allocate(UMatData *, AccessFlag, UMatUsageFlags) const override 188 - { 189 - SLAM_ASSERT(false, "Shouldn't be reached"); 190 - return false; 191 - } 192 - 193 - //! When `cv::UMatData` refcount reaches zero this method is called, we just 194 - //! decrement the original @ref xrt_frame refcount as it is the one in charge 195 - //! of the memory. 196 - void 197 - deallocate(UMatData *u) const override 198 - { 199 - SLAM_DASSERT_(u->urefcount == 0 && u->refcount == 0); 200 - SLAM_DASSERT_(u->flags & UMatData::USER_ALLOCATED); 201 - xrt_frame_reference((struct xrt_frame **)&u->userdata, NULL); 202 - delete u; 203 - } 204 - }; 205 - 206 111 207 112 /* 208 113 * ··· 362 267 struct u_var_button reset_state_btn; //!< Reset tracker state button 363 268 364 269 enum u_logging_level log_level; //!< Logging level for the SLAM tracker, set by SLAM_LOG var 365 - MatFrame *cv_wrapper; //!< Wraps a xrt_frame in a cv::Mat to send to the SLAM system 366 270 367 271 struct xrt_slam_sinks *euroc_recorder; //!< EuRoC dataset recording sinks 368 272 struct openvr_tracker *ovr_tracker; //!< OpenVR lighthouse tracker ··· 1441 1345 last_ts = ts; 1442 1346 1443 1347 // Construct and send the image sample 1444 - cv::Mat img = t.cv_wrapper->wrap(frame); 1445 - 1446 1348 vit_img_sample sample = {}; 1447 1349 sample.cam_index = cam_index; 1448 1350 sample.timestamp = ts; 1449 - sample.data = img.ptr(); 1450 - sample.width = img.cols; 1451 - sample.height = img.rows; 1452 - sample.stride = img.step; 1453 - sample.size = img.cols * img.rows; 1351 + 1352 + sample.data = frame->data; 1353 + sample.width = frame->width; 1354 + sample.height = frame->height; 1355 + sample.stride = frame->stride; 1356 + sample.size = frame->size; 1454 1357 1455 1358 // TODO check format before 1456 1359 switch (frame->format) { ··· 1533 1436 os_mutex_destroy(&t.lock_ff); 1534 1437 m_ff_vec3_f32_free(&t.filter.pos_ff); 1535 1438 m_ff_vec3_f32_free(&t.filter.rot_ff); 1536 - 1537 - delete t_ptr->cv_wrapper; 1538 1439 1539 1440 t_ptr->vit.tracker_destroy(t_ptr->tracker); 1540 1441 t_vit_bundle_unload(&t_ptr->vit);