如何用CGAL::Dereference_property_map化简?

How to simplify with CGAL::Dereference_property_map?

我有以下 classes:

#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))

我在手册 here and here 中搜索并找到了模板 CGAL::Dereference_property_map。我假设它使我能够创建一个 class 重载 [] 运算符并可用于简化函数。

可悲的是,我才刚刚开始更认真地编写 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)

但它不起作用,我无法弄清楚...

您要提供的顶点图必须是ReadablePropertyMap的模型。由于您有自定义顶点类型,因此您必须编写一个自定义 属性 地图,该地图将简单地 return 存储在顶点 class.

中的点

类似的东西应该可以工作:

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