Как упростить с помощью CGAL :: Dereference_property_map - PullRequest
0 голосов
/ 05 января 2020

У меня есть следующие классы:

#pragma once

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point;
typedef Kernel::Vector_3 Vector;

class Vertex {
private:
    Point position;
    Vector normal;
public:
    Vertex(
        double positionX, double positionY, double positionZ,
        double normalX, double normalY, double normalZ) :
        position{ positionX, positionY, positionZ },
        normal{ normalX, normalY, normalZ } {};
    Point& getPosition() { return position; };
    Vector& getNormal() { return normal; };
};
#include <vector>
#include "Vertex.h"

class VertexCloud {
private:
    std::vector<Vertex> vertices;
public:
    void addVertex(Vertex vertex) { vertices.push_back(vertex); };
    void addVertices(std::vector<Vertex> vertices) { this->vertices.insert(this->vertices.end(), vertices.begin(), vertices.end()); };
    std::vector<Vertex>& getVertices() { return vertices; }
};

И если я хочу выполнить упрощение для своего облака точек, мне нужно будет сделать что-то подобное:

std::unique_ptr<VertexCloud> CgalSimplification::gridSimplification(VertexCloud& vertexCloud, double epsilon) {
    std::vector<Point> points;
    std::vector<Vector> normals;

    // This is eating up some performance. Need to improve.
    for (Vertex vertex : vertexCloud.getVertices()) {
        points.push_back(vertex.getPosition());
        normals.push_back(vertex.getNormal());
    }

    std::vector<std::size_t> indices(points.size());
    for (std::size_t i = 0; i < points.size(); ++i) {
        indices[i] = i;
    }

    // Simplification by clustering using erase-remove idiom.
    double cell_size{epsilon};
    std::vector<std::size_t>::iterator end;
    end = CGAL::grid_simplify_point_set(
        indices,
        cell_size,
        CGAL::parameters::point_map(CGAL::make_property_map(points))
    );

    std::size_t k = end - indices.begin();
    {
        std::vector<Point> tmp_points(k);
        std::vector<Vector> tmp_normals(k);
        for (std::size_t i = 0; i < k; ++i) {
            tmp_points[i] = points[indices[i]];
            tmp_normals[i] = normals[indices[i]];
        }
        points.swap(tmp_points);
        normals.swap(tmp_normals);
    }

    auto simplifiedVertexCloud = std::make_unique<VertexCloud>();
    for (int i = 0; i < points.size(); i++) {
        simplifiedVertexCloud->addVertex(
            Vertex(
                points[i].x(),
                points[i].y(),
                points[i].z(),
                normals[i].x(),
                normals[i].y(),
                normals[i].z()
            )
        );
    }

    return simplifiedVertexCloud;
}

То, что я хочу сделать, - это обойти передачу данных из моего объекта vertexCloud в вектор объектов Point, чтобы потом передать их в CGAL::parameters::point_map(CGAL::make_property_map(points)) в функции упрощения. Я искал в руководстве https://doc.cgal.org/latest/Property_map/index.html#Chapter_CGAL_and_Boost_Property_Maps и https://doc.cgal.org/latest/Point_set_processing_3/index.html#Chapter_Point_Set_Processing и нашел шаблон CGAL::Dereference_property_map, который, как я полагаю, дает мне возможность создать класс, который перегружает оператор [] и может использоваться для функции упрощения. К сожалению, я только начинаю более серьезно программировать на C ++ и борюсь с документацией на это. Может кто-нибудь привести пример использования CGAL::Dereference_property_map?

Я пробовал что-то вроде:

#include <vector>
#include "Vertex.h"
#include <CGAL/property_map.h>

class VertexCloud : CGAL::Dereference_property_map<VertexCloud> {
private:
    std::vector<Vertex> vertices;
public:
    void addVertex(Vertex vertex) { vertices.push_back(vertex); };
    void addVertices(std::vector<Vertex> vertices) { this->vertices.insert(this->vertices.end(), vertices.begin(), vertices.end()); };
    std::vector<Vertex>& getVertices() { return vertices; }
    Point& operator[](int i) { return vertices[i].getPosition(); };
};

И:

CGAL::parameters::point_map(vertexCloud)

Но это не работает и Я не могу понять ...

Спасибо ...

1 Ответ

1 голос
/ 06 января 2020

Карта точек вершины, которую вы должны предоставить, должна быть моделью ReadablePropertyMap. Поскольку у вас есть собственный тип вершины, вы должны написать собственную карту свойств, которая будет просто возвращать точку, хранящуюся в классе Vertex.

Что-то подобное должно работать:

struct My_vertex_point_map{
  typedef Vertex key_type;
  typedef Point value_type;
  typedef const value_type& reference;
  typedef boost::readable_property_map_tag category;

  friend reference get(const My_vertex_point_map&, const key_type& v) {
    return v.position;
  }
};

затем передать экземпляр этой точечной карты вершин в качестве параметра vertex_point_map.

...