首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何简化CGAL::Dereference_property_map?

如何简化CGAL::Dereference_property_map?
EN

Stack Overflow用户
提问于 2020-01-05 17:39:00
回答 1查看 132关注 0票数 0

我有以下课程:

代码语言:javascript
复制
#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; };
};
代码语言:javascript
复制
#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; }
};

如果我想在我的点云上进行简化,我需要这样做:

代码语言:javascript
复制
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))

我在手册这里这里中搜索了一下,并找到了模板CGAL::Dereference_property_map。我假设它使我能够创建一个重载[]操作符并可用于简化函数的类。

遗憾的是,我刚刚开始更认真地编写C++程序,并且在文档方面做了很多工作。有人能提供一个关于CGAL::Dereference_property_map使用的例子吗?

我试过

代码语言:javascript
复制
#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(); };
};

代码语言:javascript
复制
CGAL::parameters::point_map(vertexCloud)

但这不管用,我也搞不懂.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-06 06:21:07

您必须提供的顶点点映射必须是ReadablePropertyMap的模型。因为您有一个自定义顶点类型,所以您必须编写一个自定义属性映射,它只返回存储在顶点类中的点。

像这样的东西应该能起作用:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59602502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档