如何使用CGAL::D ereference_property_map进行简化?
How to simplify with CGAL::Dereference_property_map?
我有以下类:
#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))
。
我在这里和这里搜索了手册,找到了模板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)
但它不起作用,我无法弄清楚...
您必须提供的顶点映射必须是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
。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 使用字符数组作为 Map 中的键
- C++如何创建 std::map
- C++ equivalent to Java Map getOrDefault?
- 从其他容器中移动构造"std::map"
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射