Visulalization Voronoi in OpenSceneGraph

eryar@163.com

Abstract. In mathematics a Voronoi diagram is a way of dividing space into a number of regions. A set of points, called seeds, sites, or generators is specified beforehand and for each seed there will be a correspoinding region consisting of all points closer to that seed than to any other. The regions are called Voronoi cells. It is dual to the Delaunay triangulation. It is named after Georgy Voronoy, and is also called a Voronoi tessellation, a Voronoi decomposition, a Voronoi partition, or a Dirichlet tessellation. Voronoi diagrams can be found in a large number of fields in science and technology, even in art, and they have found numerous practical and theoretical applications. The paper use OpenSceneGraph to visualize the Voronoi diagram.

Key words. Voronoi, C++, OpenSceneGraph, Visualization

1. Introduction

计算几何(Computational Geometry)作为一门学科,起源于20世纪70年代,经过近四十多年的发展,其研究内容不断扩大,涉及Voronoi图、三角剖分、凸包、直线与多边形求交、可见性、路径规划、多边形剖分等内容。据相关统计,在数以千计的相关文章中,约有15%是关于Voronoi图及其对偶(dual)图Delaunay三角剖分(Delaunay Triangulation)的研究。由于Voronoi图具有最近性、邻接性等众多性质和比较系统的理论体系,如今已经在计算机图形学、机械工程、地理信息系统、机器人、图像处理、大数据分析与处理、生物计算及无线传感网络等领域得到了广泛应用,同时也是解决碰撞检测、路径规划、可见性计算、骨架计算以及凸包计算等计算几何所涉及的其他问题的有效工具。

Voronoi图的起源最早可以追溯到17世纪。1644年,Descartes用类似Voronoi图的结构显示太阳系中物质的分布。数学家G.L. Dirichlet和M.G.Voronoi分别于1850年和1908年在他们的论文中讨论了Voronoi图的概念,所以Voronoi图又叫Dirichlet tessellation。在其他领域,这个概念也曾独立地出现,如生物学和生理学中称之为中轴变换(Medial Axis Transform)或骨架(Skeleton)。化学与物理学中称之为Wigner-Seitz Zones,气象学与地理学中称之为Thiessen多边形。Voronoi图最早由Thiessen应用于气象观测站中随机分布的研究。由于M.G. Voronoi从更通用的n维情况对其进行研究和定义,所以Voronoi图这个名称为大多数人所使用。

在路径规划、机械加工、模式识别、虚拟现实、生物计算等领域,将站点从离散点扩展到线段圆弧等生成Voronoi图的方式也是非常常见的。

目前可用于生成Voronoi图的库有一些,很多是开源库。像CGAL库、boost中也提供了生成Voronoi图的算法。本文根据Shane O Sullivans1封装的Voronoi库,并用OpenSceneGraph显示出剖分结果。

2. Implementation

用Shane O Sullivans封装的VoronoiDiagramGenerator可以生成点集的Voronoi图,得到剖分的线段。程序小巧,易于使用。结合OpenSceneGraph将剖分得到的线段显示出来。程序代码如下所示:

/*
* Copyright (c) 2014 eryar All Rights Reserved.
*
* File : Main.cpp
* Author : eryar@163.com
* Date : 2014-04-30 18:28
* Version : V1.0
*
* Description : VoronoiViewer for voronoi library visulization.
*
*/ #include "VoronoiDiagramGenerator.h" // OpenSceneGraph library.
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers> #pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib") osg::Node* BuildVoronoi(void)
{
osg::ref_ptr<osg::Geode> theGeode = new osg::Geode();
osg::ref_ptr<osg::Geometry> theLines = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> theVertices = new osg::Vec3Array(); const long thePointCount = ;
float *xValues = new float[thePointCount] ();
float *yValues = new float[thePointCount] (); float theMin = 0.0;
float theMax = 100.0; float x1 = 0.0;
float y1 = 0.0;
float x2 = 0.0;
float y2 = 0.0; // Draw the boundary box.
theVertices->push_back(osg::Vec3(theMin, 0.0, theMin));
theVertices->push_back(osg::Vec3(theMin, 0.0, theMax)); theVertices->push_back(osg::Vec3(theMin, 0.0, theMin));
theVertices->push_back(osg::Vec3(theMax, 0.0, theMin)); theVertices->push_back(osg::Vec3(theMin, 0.0, theMax));
theVertices->push_back(osg::Vec3(theMax, 0.0, theMax)); theVertices->push_back(osg::Vec3(theMax, 0.0, theMin));
theVertices->push_back(osg::Vec3(theMax, 0.0, theMax)); // initialize random seed:
srand(time(NULL)); // Sites of the Voronoi.
for (int i = ; i < thePointCount; ++i)
{
xValues[i] = rand() % ;
yValues[i] = rand() % ; // Draw the site.
theVertices->push_back(osg::Vec3(xValues[i] - 1.0, 0.0, yValues[i]));
theVertices->push_back(osg::Vec3(xValues[i] + 1.0, 0.0, yValues[i])); theVertices->push_back(osg::Vec3(xValues[i], 0.0, yValues[i] - 1.0));
theVertices->push_back(osg::Vec3(xValues[i], 0.0, yValues[i] + 1.0));
} // Generate Voronoi Diagram.
VoronoiDiagramGenerator vdg;
vdg.generateVoronoi(xValues, yValues, thePointCount, theMin, theMax, theMin, theMax);
vdg.resetIterator(); while (vdg.getNext(x1, y1, x2, y2))
{
theVertices->push_back(osg::Vec3(x1, 0.0, y1));
theVertices->push_back(osg::Vec3(x2, 0.0, y2));
} theLines->setVertexArray(theVertices); // Set the colors.
osg::ref_ptr<osg::Vec4Array> theColors = new osg::Vec4Array();
theColors->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f)); theLines->setColorArray(theColors);
theLines->setColorBinding(osg::Geometry::BIND_OVERALL); // Set the normal.
osg::ref_ptr<osg::Vec3Array> theNormals = new osg::Vec3Array();
theNormals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f)); theLines->setNormalArray(theNormals);
theLines->setNormalBinding(osg::Geometry::BIND_OVERALL); theLines->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, , theVertices->size())); theGeode->addDrawable(theLines); // Free the meomry.
delete [] xValues;
delete [] yValues; return theGeode.release();
} int main(int argc, char* argv[])
{
osgViewer::Viewer myViewer; myViewer.setSceneData(BuildVoronoi()); myViewer.addEventHandler(new osgGA::StateSetManipulator(myViewer.getCamera()->getOrCreateStateSet()));
myViewer.addEventHandler(new osgViewer::StatsHandler);
myViewer.addEventHandler(new osgViewer::WindowSizeHandler); return myViewer.run();
}

上述程序生成结果如下所示:

Figure 2.1 Voronoi Diagram in OpenSceneGraph

修改站点的数量,生成的Voronoi图如下所示:

修改范围时也要修改生成范围中点的随机函数的取余操作,避免生成点超出范围。

Figure 2.2 Less Sites of the Voronoi Diagram

Figure 2.3 More Sites of the Voronoi Diagram

3. Conclusion

Shane O Sullivans封装的库小巧,使用方便,速度还很快。也有些不足,如不能取得一个站点对应的多边形,即某个点属于哪个区域。不能得到带权点集的Voronoi剖分。

源程序小巧,借助程序代码来对Voronoi的概念进行理解还是不错的。

4. References

1. Shane O Sullivans, http://www.skynet.ie/~sos/mapviewer/voronoi.php

2. http://ect.bell-labs.com/who/sjf/

3. 汪嘉业, 王文平, 屠长河, 杨承磊. 计算几何及应用. 科学出版社. 2011

4. 杨承磊, 吕琳, 杨义军, 孟祥旭. Voronoi图及其应用. 清华大学出版社. 2013

PDF Version and Source code: Visualization Voronoi in OpenSceneGraph

Visulalization Voronoi in OpenSceneGraph的更多相关文章

  1. Visulalize Boost Voronoi in OpenSceneGraph

    Visulalize Boost Voronoi in OpenSceneGraph eryar@163.com Abstract. One of the important features of ...

  2. BOOST Voronoi Visualizer

    BOOST Voronoi Visualizer eryar@163.com Abstract. The Voronoi extension of the Boost.Polygon library ...

  3. OpenSceneGraph in ActiveX by ActiveQt

    OpenSceneGraph in ActiveX by ActiveQt eryar@163.com Abstract. Qt’s ActiveX and COM support allows Qt ...

  4. OpenSceneGraph 编译 error LNK2019:unresolved external symbol 错误

    在编译 OpenSceneGraph 的一个简单示例时, #include <osgViewer/Viewer> #include <osgDB/ReadFile> void ...

  5. OpenSceneGraph 笔记--如何导出三角形数据

    OpenSceneGraph 笔记--如何导出三角形数据 转载:http://blog.csdn.net/pizi0475/article/details/5384389 在OpenSceneGrap ...

  6. OpenSceneGraph控制模型

    OpenSceneGraph控制模型 转自:http://www.cppblog.com/eryar/archive/2012/05/28/176538.html 一.简介 对模型的控制就是修改模型的 ...

  7. [OSG]OpenSceneGraph FAQ 以及OSG资源

    1.地球背面的一个点,计算它在屏幕上的坐标,能得到吗? 不是被挡住了吗? 答:计算一个空间点的屏幕坐标,使用osgAPEx::GetScreenPosition函数.当空间点处于相机视空间内(不管它是 ...

  8. Implementation Model Editor of AVEVA in OpenSceneGraph

    Implementation Model Editor of AVEVA in OpenSceneGraph eryar@163.com 摘要Abstract:本文主要对工厂和海工设计软件AVEVA的 ...

  9. Render OpenCascade Geometry Surfaces in OpenSceneGraph

    在OpenSceneGraph中绘制OpenCascade的曲面 Render OpenCascade Geometry Surfaces in OpenSceneGraph eryar@163.co ...

随机推荐

  1. IT

    http://www.cnblogs.com/TomXu/archive/2011/12/19/2291448.html " 经常从Recruiter那里得到抱怨:“汤姆,为什么面试者每次回 ...

  2. make工作时的执行步骤

    GNU的make工作时的执行步骤 (1)读入所有的Makefile (2)读入被include的其它Makefile (3)初始化文件中的变量 (4)推导隐晦规则,并分析所有的规则 (5)为所有的目标 ...

  3. SVN出现Invalid authz configuration解决方案

    思路: 1.检查是否为不存在的用户组或用户设置了权限(大部分为此问题,调整用户权限或删除账号后,但忘了去掉某个文件夹的权限) 2.检查authz文件的编码: 3.更改权限后是否未重启svn. 4.按照 ...

  4. Spring中Aop的扩展及剖析

    AOP简介: 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范 ...

  5. VIPM 发布功能总结

    前言 上一篇中,我们分析介绍了LabVIEW自带的安装发布功能,今天总结一下VIPM的发布功能.   VIPM 提到LabVIEW,不能不提VI Package Manager (VIPM)这个工具包 ...

  6. 书中的银行,我们一起奋斗的C#,只因乐在其中~

          梦回C#,只因心中还留有那么一点执着,相信大家应该也有这些感触吧!!所以呢?我想给大伙分享我的一些学习,也希望大家能给我多点建议,让我们一起进步,共同成长!!! 那么我们就来看一下该怎么运 ...

  7. backbone新手填坑教程资源

    backbone 入门第二版 http://www.kancloud.cn/kancloud/backbonejs-learning-note/49379 backbone 入门讲解 http://w ...

  8. 借助cookie实现子网页修改父网页内容遇到的问题:同一个浏览器访问相同页面,会互相影响。 (已解决)

    问题是这样的,  我把左侧菜单做成了网页, 然后点击左侧菜单选项会改变右侧内容, 也就是子网页访问并修改父网页的内容. 为了兼容性更好, 我没有使用farther,或者opener等方法,  而是用了 ...

  9. HTML+CSS中的一些小知识

    今天分享一些HTML.CSS的小知识,希望能够对大家有所帮助! 1.解决网页乱码的问题:最重要的是要保证各个环节的字符编码一致! (1)编辑器的编辑环境的字符集(默认字符集):Crtl+U 常见的编码 ...

  10. Ajax全面基础学习(一)

    快捷方法: $.get(url,[data],[callback],[type])get方法的[data]将被链在url后面[callback]是请求成功后的回调,可以得到响应数据,如果请求失败,看不 ...