[CGAL]带岛多边形三角化

news/2025/1/19 13:45:10/

[CGAL]带岛多边形三角化
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 % 2 == 1;}
};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 != -1){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 == -1){fh->info().nesting_level = index;for (int i = 0; i < 3; i++){CDT::Edge e(fh, i);CDT::Face_handle n = fh->neighbor(i);if (n->info().nesting_level == -1){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 = -1;}std::list<CDT::Edge> border;mark_domains(cdt, cdt.infinite_face(), 0, 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 == -1){mark_domains(cdt, n, e.first->info().nesting_level + 1, border);}}
}int _tmain(int argc, _TCHAR* argv[])
{//读取shpconst char * pszShapeFile = "data\\walltest.shp";SHPHandle hShp = SHPOpen(pszShapeFile, "r");int nShapeType, nVertices;int nEntities = 0;double* minB = new double[4];double* maxB = new double[4];SHPGetInfo(hShp, &nEntities, &nShapeType, minB, maxB);printf("ShapeType:%d\n", nShapeType);printf("Entities:%d\n", nEntities);CDT cdt;for (int i = 0; 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 = 0; k < parts; k++){Polygon_2 polygon1;int fromIdx = partStart[k];int toIdx = fromIdx;if (k<parts-1){toIdx = partStart[k + 1];}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 polygonmark_domains(cdt);FILE *ply = fopen("data\\floorpeint.ply", "w");int idx = 0;for (CDT::Vertex_iterator v = cdt.vertices_begin(); v != cdt.vertices_end(); ++v){Vertex_handle vv = v->handle();vv->id() = idx;idx++;}int count = 0;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 = 0; i < 3; 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(0);Vertex_handle vertId1 = fit->vertex(1);Vertex_handle vertId2 = fit->vertex(2);int id0 = vertId0->id();int id1 = vertId1->id();int id2 = vertId2->id();fprintf(ply, "%d %d %d %d\n", 3, id0, id1, id2);}}}std::cout << "There are " << count << " facets in the domain." << std::endl;system("pause");return 0;
}

效果图


http://www.ppmy.cn/news/679032.html

相关文章

【前端】案例1 轮播图【HTML/CSS/JS】+JQ

引入JQ <script src"https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>CSS代码 /* 轮播图部分 */.two_content {width: 100%;height: 490px;position: relative;overflow: hidden;cursor: pointer;z-index: 1;}.bigimages {width: 10…

微信小程序无法显示网络图片,但是浏览器能够正常打开该图片

检查图片url是否正确是否已在微信公众平台后台添加了相应的服务地址如果以上两种情况都没有问题&#xff0c;那么可能是图片服务器做了防盗设置 当浏览器可以打开某个图片url&#xff0c;但是微信小程序无法打开&#xff0c;可能是资源服务器做了防盗链&#xff0c;设置了不带…

java后台生成微信二维码,打不开图片

访问微信接口生成二维码图片后&#xff0c;打开二维码图片显示不支持此格式。我这边问题解决1.参数"scene"必传且不能修改成其他字段。2.路径中“\”替换成“/”。

关于微信获取不到相册图片问题

本人亲测&#xff01;&#xff01;&#xff01; 最近做一个项目&#xff0c;分享二维码&#xff0c;用户获取分享的H5页面填写资料的&#xff0c;遇到了非常头疼的问题&#xff0c;在上传问题上&#xff0c;大多数浏览器都是正常的&#xff0c;唯有微信内置浏览器&#xff0c;在…

Win10系统打不开图片

最近电脑出了好多的问题&#xff0c;都是一些很让人纠结的问题&#xff0c;而且都不怎么好解决&#xff0c;造成了很多的困扰了&#xff0c;自己弄来弄去&#xff0c;都没全部弄好&#xff0c;下面就是一个很让我纠结的问题了。那就是重装了系统后&#xff0c;电脑的图片无法打…

微信小程序,线上打不开图片或文件,但是测试环境可以

最近发现一个问题&#xff0c;就是小程序展示一个网络图片&#xff0c;在本地测试什么问题没有&#xff0c;但是到了线上就显示不出来。 打开console&#xff0c;可以发现红色报错如“downloadFile:fail url not in domain list”等&#xff0c;其实就是获取文件或者图片路径的…

微信GIF图保存到本地的方法

最近想在公众号上放微信聊天里的GIF图&#xff0c;但是因为下载或者收藏都只能是图片/视频/文件。 度娘了一波&#xff0c;基本都是说去微信的文件保存位置找红色圈住的文件夹。 然后下面是很多没有后缀的文件&#xff0c;那些就是GIF图。可是不知道是不是因为微信升级还是什么…

关于微信小程序不能显示图片

本人场景&#xff1a;wxml <image> 标签设置 src"../../images/中文名.png" 时出现 IOS 端显示没问题&#xff0c;而安卓端不显示图片的问题。 上网一查才看到微信小程序在安卓和 IOS 系统间存在着许多的坑。 1.安卓手机访问不到图片&#xff0c;无法显示图片…