CGAL带岛多边形三角化,并输出(*.ply)格式的模型

模型输出的关键是节点和索引

#include <CGAL/Triangulation_vertex_base_with_id_2.h>
#include <CGAL/Triangulation_face_base_with_info_2.h>

因此注意这两个泛型,对比不带信息的

#include <CGAL/Triangulation_vertex_base_2.h>
#include <CGAL/Triangulation_face_base_2.h>,这两个增加了部分信息作为拓展。

这样Vertex_handle就可以读取这部分拓展的信息。

心得:CGAL的泛型机制真的很强大,拓展性很好。

// AxModelDelaunay.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "shapefil.h" #include "CGAL/exceptions.h"
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_id_2.h>
#include <CGAL/Triangulation_face_base_with_info_2.h>
#include <CGAL/Polygon_2.h>
#include <iostream>
struct FaceInfo2
{
FaceInfo2(){}
int nesting_level;
bool in_domain()
{
return nesting_level % == ;
}
}; typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Triangulation_vertex_base_with_id_2<K> Vb;
typedef CGAL::Triangulation_face_base_with_info_2<FaceInfo2, K> Fbb;
typedef CGAL::Constrained_triangulation_face_base_2<K, Fbb> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb, Fb> TDS;
typedef CGAL::Exact_predicates_tag Itag;
typedef CGAL::Constrained_Delaunay_triangulation_2<K, TDS, Itag> CDT;
typedef CDT::Point Point;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CDT::Vertex_handle Vertex_handle; void mark_domains(CDT& ct, CDT::Face_handle start, int index, std::list<CDT::Edge>& border)
{
if (start->info().nesting_level != -){
return;
}
std::list<CDT::Face_handle> queue;
queue.push_back(start);
while (!queue.empty()){
CDT::Face_handle fh = queue.front();
queue.pop_front();
if (fh->info().nesting_level == -){
fh->info().nesting_level = index;
for (int i = ; i < ; i++){
CDT::Edge e(fh, i);
CDT::Face_handle n = fh->neighbor(i);
if (n->info().nesting_level == -){
if (ct.is_constrained(e)) border.push_back(e);
else queue.push_back(n);
}
}
}
}
}
//explore set of facets connected with non constrained edges,
//and attribute to each such set a nesting level.
//We start from facets incident to the infinite vertex, with a nesting
//level of 0. Then we recursively consider the non-explored facets incident
//to constrained edges bounding the former set and increase the nesting level by 1.
//Facets in the domain are those with an odd nesting level.
void mark_domains(CDT& cdt)
{
for (CDT::All_faces_iterator it = cdt.all_faces_begin(); it != cdt.all_faces_end(); ++it){
it->info().nesting_level = -;
}
std::list<CDT::Edge> border;
mark_domains(cdt, cdt.infinite_face(), , border);
while (!border.empty()){
CDT::Edge e = border.front();
border.pop_front();
CDT::Face_handle n = e.first->neighbor(e.second);
if (n->info().nesting_level == -){
mark_domains(cdt, n, e.first->info().nesting_level + , border);
}
}
} int _tmain(int argc, _TCHAR* argv[])
{
//读取shp
const char * pszShapeFile = "data\\walltest.shp";
SHPHandle hShp = SHPOpen(pszShapeFile, "r");
int nShapeType, nVertices;
int nEntities = ;
double* minB = new double[];
double* maxB = new double[];
SHPGetInfo(hShp, &nEntities, &nShapeType, minB, maxB);
printf("ShapeType:%d\n", nShapeType);
printf("Entities:%d\n", nEntities);
CDT cdt;
for (int i = ; i < nEntities; i++)
{
int iShape = i;
SHPObject *obj = SHPReadObject(hShp, iShape);
printf("--------------Feature:%d------------\n", iShape);
int parts = obj->nParts;
int* partStart = obj->panPartStart;
int verts = obj->nVertices;
printf("nParts:%d\n", parts);
printf("nVertices:%d\n", verts);
for (int k = ; k < parts; k++)
{
Polygon_2 polygon1;
int fromIdx = partStart[k];
int toIdx = fromIdx;
if (k<parts-)
{
toIdx = partStart[k + ];
}
else
{
toIdx = verts;
} for (size_t j = fromIdx; j < toIdx; j++)
{
double x = obj->padfX[j];
double y = obj->padfY[j];
//Point_2 pt(x, y);
printf("%f,%f;", x, y);
polygon1.push_back(Point(x, y)); }
cdt.insert_constraint(polygon1.vertices_begin(), polygon1.vertices_end(), true);
}
printf("\n");
} //construct two non-intersecting nested polygons
//Polygon_2 polygon1;
//polygon1.push_back(Point(0, 0));
//polygon1.push_back(Point(2, 0));
//polygon1.push_back(Point(2, 2));
//polygon1.push_back(Point(0, 2));
//Polygon_2 polygon2;
//polygon2.push_back(Point(0.5, 0.5));
//polygon2.push_back(Point(1.5, 0.5));
//polygon2.push_back(Point(1.5, 1.5));
//polygon2.push_back(Point(0.5, 1.5)); ////Insert the polygons into a constrained triangulation
//CDT cdt;
//cdt.insert_constraint(polygon1.vertices_begin(), polygon1.vertices_end(), true);
//cdt.insert_constraint(polygon2.vertices_begin(), polygon2.vertices_end(), true); //Mark facets that are inside the domain bounded by the polygon
mark_domains(cdt);
FILE *ply = fopen("data\\floorpeint.ply", "w"); int idx = ;
for (CDT::Vertex_iterator v = cdt.vertices_begin(); v != cdt.vertices_end(); ++v)
{
Vertex_handle vv = v->handle();
vv->id() = idx;
idx++;
} int count = ;
for (CDT::Finite_faces_iterator fit = cdt.finite_faces_begin();
fit != cdt.finite_faces_end(); ++fit)
{
if (fit->info().in_domain())
{
++count;
for (int i = ; i < ; i++)
{
Vertex_handle vert = fit->vertex(i);
int x=vert->id();
std::cout << "The Id is " << x << std::endl;
CDT::Edge ed(fit, i);
ed.second;
}
} }
if (ply)
{
fprintf(ply, "ply\nformat %s 1.0\n", "ascii");
fprintf(ply, "element vertex %d\n",idx );
fprintf(ply, "property float x\n");
fprintf(ply, "property float y\n");
fprintf(ply, "property float z\n");
fprintf(ply, "element face %d\n", count);
fprintf(ply, "property list uint8 int32 vertex_indices\n");
fprintf(ply, "end_header\n"); for (CDT::Vertex_iterator v = cdt.vertices_begin(); v != cdt.vertices_end(); ++v)
{
Vertex_handle vv = v->handle();
double x = vv->point().x();
double y = vv->point().y();
fprintf(ply, "%f %f %f\n", x, y, 0.0);
}
for (CDT::Finite_faces_iterator fit = cdt.finite_faces_begin(); fit != cdt.finite_faces_end(); ++fit)
{
if (fit->info().in_domain())
{ Vertex_handle vertId0 = fit->vertex();
Vertex_handle vertId1 = fit->vertex();
Vertex_handle vertId2 = fit->vertex();
int id0 = vertId0->id();
int id1 = vertId1->id();
int id2 = vertId2->id();
fprintf(ply, "%d %d %d %d\n", , id0, id1, id2);
} }
} std::cout << "There are " << count << " facets in the domain." << std::endl;
system("pause");
return ;
}

效果图

[CGAL]带岛多边形三角化的更多相关文章

  1. 用canvas 实现个图片三角化(LOW POLY)效果

    之前无意中看到Ovilia 用threejs做了个LOW POLY,也就是图片平面三角化的效果,觉得很惊艳,然后就自己花了点时间尝试了一下. 我是没怎么用过threejs,所以就直接用canvas的2 ...

  2. 三角化---深度滤波器---单目稠密重建(高翔slam---十三讲)

    一.三角化 [1]三角化得到空间点的三维信息(深度值) (1)三角化的提出 三角化最早由高斯提出,并应用于测量学中.简单来讲就是:在不同的位置观测同一个三维点P(x, y, z),已知在不同位置处观察 ...

  3. 多视几何——三角化求解3D空间点坐标

    VINS-Mono / VINS-Fusion中triangulatePoint()函数通过三角化求解空间点坐标,代码所体现的数学描述不是很直观,查找资料,发现参考文献[1]对这个问题进行详细解释,记 ...

  4. Schur 三角化定理的推论

    将学习到什么 从 Schur 的酉三角化定理可以收获一批结果,在这一部分介绍重要的几个.   迹与行列式 相似矩阵具有相同的特征多项式, 从特征多项式一节中, 我们又知道,相似矩阵的迹以及行列式都是相 ...

  5. PCL贪婪投影三角化算法

    贪婪投影三角化算法是一种对原始点云进行快速三角化的算法,该算法假设曲面光滑,点云密度变化均匀,不能在三角化的同时对曲面进行平滑和孔洞修复. 方法: (1)将三维点通过法线投影到某一平面 (2)对投影得 ...

  6. el-amap 遮罩(带洞多边形)

    el-amap 遮罩(带洞多边形) 遮罩(带洞多边形) 效果图 代码 <template> <div> <el-amap vid="amapDemo" ...

  7. CoordinatorLayout 自定义Behavior并不难,由简到难手把手带你撸三款!

    先来看看最终的效果~~ 本文同步至博主的私人博客wing的地方酒馆 嗯..一个是头像上移的 另一个是模仿UC浏览器的. (PД`q.)你不是说!有三款的吗,怎么只有两款!!!! 不要急嘛... 说了从 ...

  8. Solr教程--官方自带数据的三个练习及讨论翻译版本

    Solr教程 在你开始之前 解压Solr 练习1:索引Techproducts示例数据 在SolrCloud模式下启动Solr 索引技术产品数据 基本搜索 练习1总结 练习2:修改架构和索引影片数据 ...

  9. VBS中解决路径带空格的三种方法

    vbs中,如果需要运行的程序中带有空格,按照通常的方式往往会提示错误,其实有两种形式不同的解决方法: 在应用程序前后分别加三个双引号,代码如下: Set wshell=CreateObject(&qu ...

随机推荐

  1. opencv error: insufficient memory错误解决办法

    用opencv合成图像时出现的错误,大概4000多张会报错,在网上查阅一些博客时才知道原因.之前编译的时候用的是x86,切换到x64下可解决问题,具体: 1.项目->属性->配置管理器-& ...

  2. 并发之AQS原理(三) 如何保证并发

    并发之AQS原理(三) 如何保证并发 1. 如何保证并发 AbstractQueuedSynchronizer 维护了一个state(代表了共享资源)和一个FIFO线程等待队列(多线程竞争资源被阻塞时 ...

  3. php中静态方法和静态属性的介绍

    静态分为两个部分:静态属性和静态方法 静态的东西都是给类用的(包括类常量),非静态的都是给对象用的 静态属性 在定义属性的时候,使用关键字static修饰的属性称之为静态属性. 静态方法 使用stat ...

  4. 性能测试三十四:jvm内存结构(栈、堆、永久代)

    Java内存管理机制 Java采用了自动管理内存的方式Java程序是运行在Jvm之中的Java的跨平台的基于Jvm的跨平台特性内存的分配和对象的创建是在Jvm中用户可以通过一系列参数来配置Jvm Jv ...

  5. POJ 2184 Cow Exhibition (带负值的01背包)

    题意:给你N(N<=100)只牛,每只牛有一个智慧值Si和一个活泼值Fi,现在要从中找出一些来,使得这些牛智慧值总和S与活泼值总和F之和最大,且F和S均为正.Si和Fi范围在-1000到1000 ...

  6. PDA智能程序访问WebService,报告“未能建立与网络的连接”

    其实就是你没又连接上网络.首先下个第三方软件关于vs模拟器连接的.然后根据以下说明操作就可以连接了在确保主机已连上互联网的情况下,按以下步骤设置: 1.打开ActiveSync ,点击“文件”——&g ...

  7. HTML编码规范 - (WEB前端命名规范)

    HTML编码规范 (一)命名规则: 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wr ...

  8. Unity 之 场景切换

      Application.LoadLevel();//场景名称或索引,删除掉原场景的所有东西 Application.LoadLevelAdditive()//添加并加载场景,不删除当前场景的物体, ...

  9. Linux —— 文件处理指令

  10. Mongodb - 切片搭建

    0.概述 mongodb分片搭建,版本号4.0.2,以下除了创建opt文件夹以外,所有操作均在mongodb用户下面执行 准备三台机器:192.168.56.81192.168.56.82192.16 ...