mv3d: keep pointcloud as a shared_ptr form instead of raw void*
authorSeungbae Shin <seungbae.shin@samsung.com>
Fri, 23 Sep 2022 05:37:53 +0000 (14:37 +0900)
committerKwanghoon Son <k.son@samsung.com>
Wed, 14 Dec 2022 06:33:18 +0000 (15:33 +0900)
The shared_ptr is exactly what we got from geometry::PointCloud::CreateFrom RGBDImage(),
eventually, this change gives us better readability and safety.

[Issue type] refactoring
[Version] 0.26.3

Change-Id: I20e4f95e272dbd8a05ce18c267c1dc84539387d0

include/mv_3d_private.h
mv_3d/3d/include/Mv3d.h
mv_3d/3d/src/Mv3d.cpp
mv_3d/3d/src/mv_3d_internal.cpp
packaging/capi-media-vision.spec

index 37bcc0b..6015d13 100644 (file)
 #include <cstdio>
 #include "mv_3d_type.h"
 
+#ifdef MV_3D_POINTCLOUD_IS_AVAILABLE
+#include <open3d/Open3D.h>
+
+using PointCloudPtr = std::shared_ptr<open3d::geometry::PointCloud>;
+
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
@@ -37,8 +44,10 @@ extern "C" {
   *
   */
 typedef struct {
-       mv_3d_pointcloud_type_e type;
-       void *pointcloud;
+       mv_3d_pointcloud_type_e type {MV_3D_POINTCLOUD_TYPE_PCD_BIN};
+#ifdef MV_3D_POINTCLOUD_IS_AVAILABLE
+       PointCloudPtr pointcloud;
+#endif
 } mv_3d_pointcloud_s;
 
 /**
index d47438e..54298db 100644 (file)
@@ -74,7 +74,7 @@ private:
        static gpointer DfsThreadLoop(gpointer data);
 
 #ifdef MV_3D_POINTCLOUD_IS_AVAILABLE
-       void GetPointcloudFromSource(DfsInputData &intput, DfsOutputData &depthData, mv_3d_pointcloud_s &pointcloud);
+       PointCloudPtr GetPointcloudFromSource(DfsInputData &intput, DfsOutputData &depthData);
 #endif
 public:
        Mv3d();
index 56cf271..bff8f47 100644 (file)
@@ -251,7 +251,7 @@ void Mv3d::GetDfsDataFromSources(mv_source_h baseSource, mv_source_h extraSource
 }
 
 #ifdef MV_3D_POINTCLOUD_IS_AVAILABLE
-void Mv3d::GetPointcloudFromSource(DfsInputData &input, DfsOutputData &depthData, mv_3d_pointcloud_s &pointcloud)
+PointCloudPtr Mv3d::GetPointcloudFromSource(DfsInputData &input, DfsOutputData &depthData)
 {
        camera::PinholeCameraIntrinsic intrinsic;
        io::ReadIJsonConvertible(mIntrinsicPath, intrinsic);
@@ -289,7 +289,7 @@ void Mv3d::GetPointcloudFromSource(DfsInputData &input, DfsOutputData &depthData
                std::tie(pcd, pt_map) = pcd->RemoveRadiusOutliers(mOutlierRemovalPoints, mOutlierRemovalRadius, true);
        }
 
-       pointcloud.pointcloud = static_cast<void *>(new std::shared_ptr<open3d::geometry::PointCloud>(std::move(pcd)));
+       return pcd;
 }
 #endif
 
@@ -325,14 +325,10 @@ int Mv3d::Run(mv_source_h baseSource, mv_source_h extraSource)
                                           mDepthUserData);
 #ifdef MV_3D_POINTCLOUD_IS_AVAILABLE
                if (mPointcloudCallback) {
-                       mv_3d_pointcloud_s p = { .type = MV_3D_POINTCLOUD_TYPE_PCD_BIN, .pointcloud = NULL };
-                       GetPointcloudFromSource(input, depthData, p);
-
-                       mv_3d_pointcloud_h pcd = &p;
-                       mPointcloudCallback(baseSource, pcd, mPointcloudUserData);
-                       auto pPcd = static_cast<std::shared_ptr<open3d::geometry::PointCloud> *>(p.pointcloud);
-                       auto _pcd = std::move(pPcd);
-                       delete _pcd;
+                       mv_3d_pointcloud_s p { .type = MV_3D_POINTCLOUD_TYPE_PCD_BIN,
+                                                                  .pointcloud = GetPointcloudFromSource(input, depthData) };
+
+                       mPointcloudCallback(baseSource, static_cast<mv_3d_pointcloud_h>(&p), mPointcloudUserData);
                }
 #endif
                delete[] static_cast<unsigned char *>(input.data);
@@ -428,18 +424,11 @@ gpointer Mv3d::DfsThreadLoop(gpointer data)
                                                           handle->mDepthUserData);
 #ifdef MV_3D_POINTCLOUD_IS_AVAILABLE
                if (handle->mPointcloudCallback) {
-                       mv_3d_pointcloud_s p = { .type = MV_3D_POINTCLOUD_TYPE_PCD_BIN, .pointcloud = NULL };
-                       //mPointcloudThread = g_thread_new("pointcloud_thread",
-                       //              &Mv3d::PointcloudThreadLoop,
-                       //              static_cast<gpointer>(this));
-                       handle->GetPointcloudFromSource(*pInput, depthData, p);
-
-                       mv_3d_pointcloud_h pcd = &p;
-                       handle->mPointcloudCallback(static_cast<mv_source_h>(handle->mInternalSource), pcd,
+                       mv_3d_pointcloud_s p { .type = MV_3D_POINTCLOUD_TYPE_PCD_BIN,
+                                                                  .pointcloud = handle->GetPointcloudFromSource(*pInput, depthData) };
+
+                       handle->mPointcloudCallback(handle->mInternalSource, static_cast<mv_3d_pointcloud_h>(&p),
                                                                                handle->mPointcloudUserData);
-                       auto pPcd = static_cast<std::shared_ptr<open3d::geometry::PointCloud> *>(p.pointcloud);
-                       auto _pcd = std::move(pPcd);
-                       delete _pcd;
                }
 #endif
                delete[] static_cast<unsigned char *>(pInput->data);
@@ -453,8 +442,8 @@ gpointer Mv3d::DfsThreadLoop(gpointer data)
 int Mv3d::WritePointcloudFile(mv_3d_pointcloud_h pointcloud, mv_3d_pointcloud_type_e type, char *fileName)
 {
 #ifdef MV_3D_POINTCLOUD_IS_AVAILABLE
-       mv_3d_pointcloud_s *s = (mv_3d_pointcloud_s *) pointcloud;
-       if (s == NULL) {
+       auto s = static_cast<mv_3d_pointcloud_s *>(pointcloud);
+       if (!s) {
                LOGE("Pointcloud data is NULL");
                return MEDIA_VISION_ERROR_INVALID_PARAMETER;
        }
@@ -463,35 +452,27 @@ int Mv3d::WritePointcloudFile(mv_3d_pointcloud_h pointcloud, mv_3d_pointcloud_ty
                if (errno == EACCES || errno == EPERM) {
                        utility::LogInfo("Fail to access path[%s]: Permission Denied", mPointcloudOutputPath.c_str());
                        return MEDIA_VISION_ERROR_PERMISSION_DENIED;
-                       ;
-               } else {
-                       utility::LogInfo("Fail to access path[%s]: Invalid Path", mPointcloudOutputPath.c_str());
-                       return MEDIA_VISION_ERROR_INVALID_PARAMETER;
                }
-       }
 
-       bool bText = false;
-       if (type == MV_3D_POINTCLOUD_TYPE_PCD_TXT || type == MV_3D_POINTCLOUD_TYPE_PLY_TXT)
-               bText = true;
-       else
-               bText = false;
+               utility::LogInfo("Fail to access path[%s]: Invalid Path", mPointcloudOutputPath.c_str());
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+       }
 
-       auto pPcd = static_cast<std::shared_ptr<open3d::geometry::PointCloud> *>(s->pointcloud);
-       open3d::geometry::PointCloud p;
-       p += **pPcd;
+       bool bText = (type == MV_3D_POINTCLOUD_TYPE_PCD_TXT || type == MV_3D_POINTCLOUD_TYPE_PLY_TXT);
 
        std::string fullPath = mPointcloudOutputPath + std::string("/") + std::string(fileName);
 
-       if (io::WritePointCloud(fullPath.c_str(), p, { bText, false, false, {} })) {
-               utility::LogInfo("Successfully wrote {}", fullPath.c_str());
-       } else {
+       if (!io::WritePointCloud(fullPath.c_str(), *(s->pointcloud), { bText, false, false, {} })) {
                utility::LogError("Failed to write {}", fullPath.c_str());
                return MEDIA_VISION_ERROR_INTERNAL;
        }
+
+       utility::LogInfo("Successfully wrote {}", fullPath.c_str());
+
+       return MEDIA_VISION_ERROR_NONE;
 #else
        return MEDIA_VISION_ERROR_NOT_SUPPORTED;
 #endif
-       return MEDIA_VISION_ERROR_NONE;
-}
-}
 }
+} // namespace mv3d
+} // namespace mediavision
index 50f8a72..685f10c 100644 (file)
@@ -125,14 +125,14 @@ int mv_3d_pointcloud_segment_plane(mv_3d_h mv3d, mv_3d_pointcloud_h pointcloud,
        MEDIA_VISION_NULL_ARG_CHECK(inlier);
        MEDIA_VISION_FUNCTION_ENTER();
 
-       mv_3d_pointcloud_s *s = (mv_3d_pointcloud_s *) pointcloud;
+       auto s = static_cast<mv_3d_pointcloud_s *>(pointcloud);
+
        Eigen::Vector4d *best_plane_model = new Eigen::Vector4d;
        std::vector<size_t> *plane_inlier = new std::vector<size_t>;
-       auto pPcd = static_cast<std::shared_ptr<open3d::geometry::PointCloud> *>(s->pointcloud);
-       open3d::geometry::PointCloud p;
-       p += **pPcd;
 
-       std::tie(*best_plane_model, *plane_inlier) = p.SegmentPlane(DISTANCE_THRESHOLD, RANSAC_NUMBER, NUM_ITERATIONS);
+       std::tie(*best_plane_model, *plane_inlier) =
+                       s->pointcloud->SegmentPlane(DISTANCE_THRESHOLD, RANSAC_NUMBER, NUM_ITERATIONS);
+
        *model = (mv_3d_pointcloud_plane_model_h) best_plane_model;
        *inlier = (mv_3d_pointcloud_plane_inlier_h) plane_inlier;
 
@@ -158,19 +158,14 @@ int mv_3d_pointcloud_plane_write_file(mv_3d_h mv3d, mv_3d_pointcloud_plane_model
        MEDIA_VISION_NULL_ARG_CHECK(inlier);
        MEDIA_VISION_FUNCTION_ENTER();
 
-       mv_3d_pointcloud_s *s = (mv_3d_pointcloud_s *) pointcloud;
-       std::vector<size_t> *plane_inlier = (std::vector<size_t> *) inlier;
-       auto pPcd = static_cast<std::shared_ptr<open3d::geometry::PointCloud> *>(s->pointcloud);
-       open3d::geometry::PointCloud p;
-       p += **pPcd;
+       auto plane_inlier = static_cast<std::vector<size_t> *>(inlier);
+       auto s = static_cast<mv_3d_pointcloud_s *>(pointcloud);
 
-       std::shared_ptr<geometry::PointCloud> plane = p.SelectByIndex(*plane_inlier);
-       mv_3d_pointcloud_s _pcd = { .type = MV_3D_POINTCLOUD_TYPE_PCD_BIN, .pointcloud = NULL };
-       _pcd.pointcloud = static_cast<void *>(new std::shared_ptr<open3d::geometry::PointCloud>(std::move(plane)));
-       mv_3d_pointcloud_h pcd = &_pcd;
+       mv_3d_pointcloud_s _pcd { .type = MV_3D_POINTCLOUD_TYPE_PCD_BIN,
+                                                         .pointcloud = s->pointcloud->SelectByIndex(*plane_inlier) };
 
        auto pMv3d = static_cast<Mv3d *>(mv3d);
-       int ret = pMv3d->WritePointcloudFile(pcd, type, filename);
+       int ret = pMv3d->WritePointcloudFile(static_cast<mv_3d_pointcloud_h>(&_pcd), type, filename);
        if (ret != MEDIA_VISION_ERROR_NONE) {
                LOGE("Failed to write pointcloud plane file");
                ret = MEDIA_VISION_ERROR_INTERNAL;
index e555415..ca35f79 100644 (file)
@@ -1,6 +1,6 @@
 Name:        capi-media-vision
 Summary:     Media Vision library for Tizen Native API
-Version:     0.26.2
+Version:     0.26.3
 Release:     0
 Group:       Multimedia/Framework
 License:     Apache-2.0 and BSD-3-Clause