From 21c6dec047d765bf952affe7fcbd5e6b5f32f1c0 Mon Sep 17 00:00:00 2001 From: Nikolaas Steenbergen Date: Tue, 25 Jun 2024 00:15:49 +0200 Subject: [PATCH 1/7] First version, colors are not sampled correctly --- cpp/open3d/geometry/TriangleMesh.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cpp/open3d/geometry/TriangleMesh.cpp b/cpp/open3d/geometry/TriangleMesh.cpp index 88f602770cb..a450618f0dc 100644 --- a/cpp/open3d/geometry/TriangleMesh.cpp +++ b/cpp/open3d/geometry/TriangleMesh.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "open3d/geometry/BoundingVolume.h" #include "open3d/geometry/IntersectionTest.h" @@ -440,7 +441,7 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( // sample point cloud bool has_vert_normal = HasVertexNormals(); - bool has_vert_color = HasVertexColors(); + // bool has_vert_color = HasVertexColors(); utility::random::UniformRealGenerator uniform_generator(0.0, 1.0); auto pcd = std::make_shared(); pcd->points_.resize(number_of_points); @@ -450,9 +451,9 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( if (use_triangle_normal && !HasTriangleNormals()) { ComputeTriangleNormals(true); } - if (has_vert_color) { + // if (has_vert_color) { pcd->colors_.resize(number_of_points); - } + // } for (size_t point_idx = 0; point_idx < number_of_points; ++point_idx) { double r1 = uniform_generator(); @@ -478,6 +479,18 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( b * vertex_colors_[triangle(1)] + c * vertex_colors_[triangle(2)]; } + // if (has_triangle_uvs && has_textures) { + Eigen::Vector2d uv = a * triangle_uvs_[triangle(0)] + + b * triangle_uvs_[triangle(1)] + + c * triangle_uvs_[triangle(2)]; + + int w = textures_[0].width_; + int h = textures_[0].height_; + pcd->colors_[point_idx] = Eigen::Vector3d( + (double) *textures_[0].PointerAt(uv(0) * w, uv(1) * h, 0) / 255 , + (double) *textures_[0].PointerAt(uv(0) * w, uv(1) * h, 1) / 255 , + (double) *textures_[0].PointerAt(uv(0) * w, uv(1) * h, 2) / 255); + // } } return pcd; From d6b197046d7c602b2854298770ce86b0600c82b0 Mon Sep 17 00:00:00 2001 From: Nikolaas Steenbergen Date: Fri, 28 Jun 2024 13:38:52 +0200 Subject: [PATCH 2/7] Fixed uv lookup --- cpp/open3d/geometry/TriangleMesh.cpp | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/cpp/open3d/geometry/TriangleMesh.cpp b/cpp/open3d/geometry/TriangleMesh.cpp index a450618f0dc..6c694dfd291 100644 --- a/cpp/open3d/geometry/TriangleMesh.cpp +++ b/cpp/open3d/geometry/TriangleMesh.cpp @@ -441,7 +441,10 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( // sample point cloud bool has_vert_normal = HasVertexNormals(); - // bool has_vert_color = HasVertexColors(); + bool has_vert_color = HasVertexColors(); + bool has_textures_ = HasTextures(); + bool has_triangle_uvs_ = HasTriangleUvs(); + utility::random::UniformRealGenerator uniform_generator(0.0, 1.0); auto pcd = std::make_shared(); pcd->points_.resize(number_of_points); @@ -451,10 +454,9 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( if (use_triangle_normal && !HasTriangleNormals()) { ComputeTriangleNormals(true); } - // if (has_vert_color) { + if (has_vert_color || (has_textures_ && has_triangle_uvs_)) { pcd->colors_.resize(number_of_points); - // } - + } for (size_t point_idx = 0; point_idx < number_of_points; ++point_idx) { double r1 = uniform_generator(); double r2 = uniform_generator(); @@ -474,25 +476,23 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( if (use_triangle_normal) { pcd->normals_[point_idx] = triangle_normals_[tidx]; } - if (has_vert_color) { + if (has_vert_color && !has_textures_ && !has_triangle_uvs_) { pcd->colors_[point_idx] = a * vertex_colors_[triangle(0)] + b * vertex_colors_[triangle(1)] + c * vertex_colors_[triangle(2)]; } - // if (has_triangle_uvs && has_textures) { - Eigen::Vector2d uv = a * triangle_uvs_[triangle(0)] + - b * triangle_uvs_[triangle(1)] + - c * triangle_uvs_[triangle(2)]; - + if (has_textures_ && has_triangle_uvs_) { + Eigen::Vector2d uv = a * triangle_uvs_[3*tidx] + + b * triangle_uvs_[3*tidx+1] + + c * triangle_uvs_[3*tidx+2]; int w = textures_[0].width_; int h = textures_[0].height_; pcd->colors_[point_idx] = Eigen::Vector3d( - (double) *textures_[0].PointerAt(uv(0) * w, uv(1) * h, 0) / 255 , - (double) *textures_[0].PointerAt(uv(0) * w, uv(1) * h, 1) / 255 , - (double) *textures_[0].PointerAt(uv(0) * w, uv(1) * h, 2) / 255); - // } + (double) *(textures_[0].PointerAt(uv(0) * w, uv(1)*h, 0)) / 255 , + (double) *(textures_[0].PointerAt(uv(0) * w, uv(1)*h, 1)) / 255 , + (double) *(textures_[0].PointerAt(uv(0) * w, uv(1)*h, 2)) / 255); + } } - return pcd; } From 2bef72cb8fbf1da41e54aee520a6191720c9a051 Mon Sep 17 00:00:00 2001 From: Nikolaas Steenbergen Date: Fri, 28 Jun 2024 13:42:05 +0200 Subject: [PATCH 3/7] Apply style --- cpp/open3d/geometry/TriangleMesh.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cpp/open3d/geometry/TriangleMesh.cpp b/cpp/open3d/geometry/TriangleMesh.cpp index 6c694dfd291..8cb374ae2c8 100644 --- a/cpp/open3d/geometry/TriangleMesh.cpp +++ b/cpp/open3d/geometry/TriangleMesh.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include "open3d/geometry/BoundingVolume.h" #include "open3d/geometry/IntersectionTest.h" @@ -482,15 +481,21 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( c * vertex_colors_[triangle(2)]; } if (has_textures_ && has_triangle_uvs_) { - Eigen::Vector2d uv = a * triangle_uvs_[3*tidx] + - b * triangle_uvs_[3*tidx+1] + - c * triangle_uvs_[3*tidx+2]; + Eigen::Vector2d uv = a * triangle_uvs_[3 * tidx] + + b * triangle_uvs_[3 * tidx + 1] + + c * triangle_uvs_[3 * tidx + 2]; int w = textures_[0].width_; int h = textures_[0].height_; - pcd->colors_[point_idx] = Eigen::Vector3d( - (double) *(textures_[0].PointerAt(uv(0) * w, uv(1)*h, 0)) / 255 , - (double) *(textures_[0].PointerAt(uv(0) * w, uv(1)*h, 1)) / 255 , - (double) *(textures_[0].PointerAt(uv(0) * w, uv(1)*h, 2)) / 255); + pcd->colors_[point_idx] = + Eigen::Vector3d((double)*(textures_[0].PointerAt( + uv(0) * w, uv(1) * h, 0)) / + 255, + (double)*(textures_[0].PointerAt( + uv(0) * w, uv(1) * h, 1)) / + 255, + (double)*(textures_[0].PointerAt( + uv(0) * w, uv(1) * h, 2)) / + 255); } } return pcd; From 6ece35e34e86d6ded37354e30781afc16de67cc5 Mon Sep 17 00:00:00 2001 From: Nikolaas Steenbergen Date: Fri, 28 Jun 2024 14:11:04 +0200 Subject: [PATCH 4/7] Add poissant sampling --- cpp/open3d/geometry/TriangleMesh.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cpp/open3d/geometry/TriangleMesh.cpp b/cpp/open3d/geometry/TriangleMesh.cpp index 8cb374ae2c8..9cf04898fa1 100644 --- a/cpp/open3d/geometry/TriangleMesh.cpp +++ b/cpp/open3d/geometry/TriangleMesh.cpp @@ -637,6 +637,8 @@ std::shared_ptr TriangleMesh::SamplePointsPoissonDisk( // update pcl bool has_vert_normal = pcl->HasNormals(); bool has_vert_color = pcl->HasColors(); + bool has_textures_ = HasTextures(); + bool has_triangle_uvs_ = HasTriangleUvs(); int next_free = 0; for (size_t idx = 0; idx < pcl->points_.size(); ++idx) { if (!deleted[idx]) { @@ -644,7 +646,7 @@ std::shared_ptr TriangleMesh::SamplePointsPoissonDisk( if (has_vert_normal) { pcl->normals_[next_free] = pcl->normals_[idx]; } - if (has_vert_color) { + if (has_vert_color || (has_textures_ && has_triangle_uvs_)) { pcl->colors_[next_free] = pcl->colors_[idx]; } next_free++; From a69d392388c655b87ec5ae0e83f5430d9598cffe Mon Sep 17 00:00:00 2001 From: Nikolaas Steenbergen Date: Fri, 28 Jun 2024 14:18:44 +0200 Subject: [PATCH 5/7] Add changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e192ce692e5..eea003506ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## Main - +- Fix TriangleMesh::SamplePointsUniformly and TriangleMesh::SamplePointsPoissonDisk now sampling colors from mesh if available (PR #6842) - Fix TriangleMesh::SamplePointsUniformly not sampling triangle meshes uniformly (PR #6653) - Fix tensor based TSDF integration example. - Use GLIBCXX_USE_CXX11_ABI=ON by default From cb5a95e0c614fe554968a49dab65780860aeab06 Mon Sep 17 00:00:00 2001 From: Nikolaas Steenbergen Date: Thu, 8 Aug 2024 16:42:59 +0200 Subject: [PATCH 6/7] Add triangle_material_id --- cpp/open3d/geometry/TriangleMesh.cpp | 35 +++++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cpp/open3d/geometry/TriangleMesh.cpp b/cpp/open3d/geometry/TriangleMesh.cpp index 9cf04898fa1..2bb54937fdc 100644 --- a/cpp/open3d/geometry/TriangleMesh.cpp +++ b/cpp/open3d/geometry/TriangleMesh.cpp @@ -431,6 +431,7 @@ std::shared_ptr TriangleMesh::FilterSmoothTaubin( return mesh; } + std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( size_t number_of_points, const std::vector &triangle_areas, @@ -443,6 +444,7 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( bool has_vert_color = HasVertexColors(); bool has_textures_ = HasTextures(); bool has_triangle_uvs_ = HasTriangleUvs(); + bool has_triangle_material_ids_ = HasTriangleMaterialIds(); utility::random::UniformRealGenerator uniform_generator(0.0, 1.0); auto pcd = std::make_shared(); @@ -475,27 +477,32 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( if (use_triangle_normal) { pcd->normals_[point_idx] = triangle_normals_[tidx]; } + // if there is no texture, sample from vertex color if (has_vert_color && !has_textures_ && !has_triangle_uvs_) { pcd->colors_[point_idx] = a * vertex_colors_[triangle(0)] + b * vertex_colors_[triangle(1)] + c * vertex_colors_[triangle(2)]; } - if (has_textures_ && has_triangle_uvs_) { + // if there is a texture, sample from texture instead + if (has_textures_ && has_triangle_uvs_ && has_triangle_material_ids_){ + Eigen::Vector2d uv = a * triangle_uvs_[3 * tidx] + - b * triangle_uvs_[3 * tidx + 1] + - c * triangle_uvs_[3 * tidx + 2]; - int w = textures_[0].width_; - int h = textures_[0].height_; + b * triangle_uvs_[3 * tidx + 1] + + c * triangle_uvs_[3 * tidx + 2]; + int material_id = triangle_material_ids_[tidx]; + int w = textures_[material_id].width_; + int h = textures_[material_id].height_; + pcd->colors_[point_idx] = - Eigen::Vector3d((double)*(textures_[0].PointerAt( - uv(0) * w, uv(1) * h, 0)) / - 255, - (double)*(textures_[0].PointerAt( - uv(0) * w, uv(1) * h, 1)) / - 255, - (double)*(textures_[0].PointerAt( - uv(0) * w, uv(1) * h, 2)) / - 255); + Eigen::Vector3d((double)*(textures_[material_id].PointerAt( + uv(0) * w, uv(1) * h, 0)) / + 255, + (double)*(textures_[material_id].PointerAt( + uv(0) * w, uv(1) * h, 1)) / + 255, + (double)*(textures_[material_id].PointerAt( + uv(0) * w, uv(1) * h, 2)) / + 255); } } return pcd; From f8b0492106741714d0afddddf5c0280fe00451b2 Mon Sep 17 00:00:00 2001 From: Nikolaas Steenbergen Date: Wed, 14 Aug 2024 17:39:02 +0200 Subject: [PATCH 7/7] apply style --- cpp/open3d/geometry/TriangleMesh.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cpp/open3d/geometry/TriangleMesh.cpp b/cpp/open3d/geometry/TriangleMesh.cpp index 2bb54937fdc..41c80a8e490 100644 --- a/cpp/open3d/geometry/TriangleMesh.cpp +++ b/cpp/open3d/geometry/TriangleMesh.cpp @@ -431,7 +431,6 @@ std::shared_ptr TriangleMesh::FilterSmoothTaubin( return mesh; } - std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( size_t number_of_points, const std::vector &triangle_areas, @@ -484,17 +483,16 @@ std::shared_ptr TriangleMesh::SamplePointsUniformlyImpl( c * vertex_colors_[triangle(2)]; } // if there is a texture, sample from texture instead - if (has_textures_ && has_triangle_uvs_ && has_triangle_material_ids_){ - + if (has_textures_ && has_triangle_uvs_ && has_triangle_material_ids_) { Eigen::Vector2d uv = a * triangle_uvs_[3 * tidx] + - b * triangle_uvs_[3 * tidx + 1] + - c * triangle_uvs_[3 * tidx + 2]; + b * triangle_uvs_[3 * tidx + 1] + + c * triangle_uvs_[3 * tidx + 2]; int material_id = triangle_material_ids_[tidx]; int w = textures_[material_id].width_; int h = textures_[material_id].height_; - pcd->colors_[point_idx] = - Eigen::Vector3d((double)*(textures_[material_id].PointerAt( + pcd->colors_[point_idx] = Eigen::Vector3d( + (double)*(textures_[material_id].PointerAt( uv(0) * w, uv(1) * h, 0)) / 255, (double)*(textures_[material_id].PointerAt(