calculate the point cloud color(RGB) sampled from texured meshes

62 views Asked by At

I use the OpenMesh and CGAL calculate the color (RGB) of point clouds sampled from textured mesh, the point clouds coordinate (XYZ) is right, but the point clouds UV is wrong and is always [0, 0], resulting the color (RGB) of point clouds is [0, 0, 0].

//read the texture map 
for(int i=0; i<textures_files.size();i++){
            std::string texturesMap = folderPath + '/' + textures_files[i];
            data[i] = stbi_load(texturesMap.c_str(), &width[i], &height[i], &n[i], 3);
    }

auto textures_index = mesh.face_texture_index_pph();

//define the coordinate(XYZ) and color(RGB)

CGAL::Point_set point_set;

CGAL::Point_set::Property_map<unsigned char> ps_red;
CGAL::Point_set::Property_map<unsigned char> ps_green;
CGAL::Point_set::Property_map<unsigned char> ps_blue;
boost::tie (ps_red, boost::tuples::ignore) = point_set.add_property_map<unsigned char>("red", 0);
boost::tie (ps_green, boost::tuples::ignore) = point_set.add_property_map<unsigned char>("green", 0);
boost::tie (ps_blue, boost::tuples::ignore) = point_set.add_property_map<unsigned char>("blue", 0);

CGAL::Point_set::Property_map<int> ps_u;
CGAL::Point_set::Property_map<int> ps_v;
boost::tie (ps_u, boost::tuples::ignore) = point_set.add_property_map<int>("u", 0);
boost::tie (ps_v, boost::tuples::ignore) = point_set.add_property_map<int>("v", 0);

//sample point clouds from mesh

std::list<CGAL::Point_3<CGAL::Epick>> samples;
CGAL::Polygon_mesh_processing::sample_triangle_mesh(mesh, std::back_inserter(samples), CGAL::Polygon_mesh_processing::parameters::do_sample_vertices(false).do_sample_edges(false).use_monte_carlo_sampling(true).number_of_points_per_area_unit(10/(poissonDiskSampling*poissonDiskSampling)));

CGAL::Kd_tree<CGAL::Search_traits_3<CGAL::Epick>> point_tree(samples.begin(), samples.end());
point_tree.build();
                                                   CGAL::AABB_tree<CGAL::AABB_traits<CGAL::GetGeomTraits<OpenMesh::MyMesh>::type,                 CGAL::AABB_face_graph_triangle_primitive<OpenMesh::MyMesh>> > mesh_tree;
CGAL::Polygon_mesh_processing::build_AABB_tree(mesh, mesh_tree);
    
CGAL::Fuzzy_sphere<CGAL::Search_traits_3<CGAL::Epick>> sampling_spere(CGAL::Point_3<CGAL::Epick>(0,0,0), 10e6);
auto sample = point_tree.search_any_point(sampling_spere);

//calculate the coordinate(XYZ) and color(RGB)
while (sample) {
    auto query_location = CGAL::Polygon_mesh_processing::locate_with_AABB_tree(*sample, mesh_tree, mesh);
    auto face = OpenMesh::make_smart(query_location.first, mesh);

    if (mesh.normal(face).length() > 0) {

    auto lambda = query_location.second;

    CGAL::Point_set_kernel::Vector_2 texCoords[3];
    CGAL::Point_set_kernel::Vector_3 points[3];
    auto arrangement_id = mesh.property(textures_index, face);

    std::size_t i = 0;
    for (auto hh: face.halfedges()) {
        auto texCoord = mesh.texcoord2D(hh);
        texCoords[i] =      CGAL::Point_set_kernel::Vector_2(texCoord[0]*width[arrangement_id], texCoord[1]*height[arrangement_id]);
        auto point = mesh.point(hh.to());
        points[i] = CGAL::Point_set_kernel::Vector_3(point[0], point[1], point[2]);
        i++;
        }
CGAL::Point_set_kernel::Point_2 uv = CGAL::Point_set_kernel::Point_2(0,0) + lambda[0] * texCoords[0] + lambda[1] * texCoords[1] + lambda[2] * texCoords[2];
CGAL::Point_set_kernel::Point_3 point = CGAL::Point_set_kernel::Point_3(0,0,0) + lambda[0] * points[0] + lambda[1] * points[1] + lambda[2] * points[2];
    
int u = uv[0];
int v = uv[1];

auto point_xyz = point_set.insert(point, normal);
                
ps_u[*point_xyz] = u;
ps_v[*point_xyz] = v;

ps_red[*point_xyz] = data[arrangement_id][3*(u + (height[arrangement_id] - v - 1)*width[arrangement_id]) + 0];
ps_green[*point_xyz] = data[arrangement_id][3*(u + (height[arrangement_id] - v - 1)*width[arrangement_id]) + 1];
ps_blue[*point_xyz] = data[arrangement_id][3*(u + (height[arrangement_id] - v - 1)*width[arrangement_id]) + 2];

What is wrong with the following code:

auto texCoord = mesh.texcoord2D(hh);
texCoords[i] = CGAL::Point_set_kernel::Vector_2(texCoord[0]*width[arrangement_id], texCoord[1]*height[arrangement_id]);

why the texxCoord[i] always is [0,0], resulting in the UV and RGB are always [0,0] and [0,0,0]

0

There are 0 answers