Skip to content

Commit

Permalink
Added python bindings for voronoi diagram construction and accessing …
Browse files Browse the repository at this point in the history
…the resulting vertices.
  • Loading branch information
mlampert authored and sliptonic committed Sep 28, 2020
1 parent 39cd29c commit 27238c3
Show file tree
Hide file tree
Showing 10 changed files with 734 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Mod/Path/App/AppPath.cpp
Expand Up @@ -46,6 +46,10 @@
#include "FeaturePathShape.h"
#include "AreaPy.h"
#include "FeatureArea.h"
#include "Voronoi.h"
#include "VoronoiPy.h"
#include "VoronoiVertex.h"
#include "VoronoiVertexPy.h"

namespace Path {
extern PyObject* initModule();
Expand All @@ -72,6 +76,8 @@ PyMOD_INIT_FUNC(Path)
Base::Interpreter().addType(&Path::ToolPy ::Type, pathModule, "Tool");
Base::Interpreter().addType(&Path::TooltablePy ::Type, pathModule, "Tooltable");
Base::Interpreter().addType(&Path::AreaPy ::Type, pathModule, "Area");
Base::Interpreter().addType(&Path::VoronoiPy ::Type, pathModule, "Voronoi");
Base::Interpreter().addType(&Path::VoronoiVertexPy ::Type, pathModule, "VoronoiVertex");

// NOTE: To finish the initialization of our own type objects we must
// call PyType_Ready, otherwise we run into a segmentation fault, later on.
Expand All @@ -94,6 +100,8 @@ PyMOD_INIT_FUNC(Path)
Path::FeatureAreaPython ::init();
Path::FeatureAreaView ::init();
Path::FeatureAreaViewPython ::init();
Path::Voronoi ::init();
Path::VoronoiVertex ::init();

PyMOD_Return(pathModule);
}
10 changes: 10 additions & 0 deletions src/Mod/Path/App/CMakeLists.txt
Expand Up @@ -36,6 +36,8 @@ generate_from_xml(TooltablePy)
generate_from_xml(FeaturePathCompoundPy)
generate_from_xml(AreaPy)
generate_from_xml(FeatureAreaPy)
generate_from_xml(VoronoiPy)
generate_from_xml(VoronoiVertexPy)

SET(Python_SRCS
CommandPy.xml
Expand All @@ -52,6 +54,10 @@ SET(Python_SRCS
AreaPyImp.cpp
FeatureAreaPy.xml
FeatureAreaPyImp.cpp
VoronoiPy.xml
VoronoiPyImp.cpp
VoronoiVertexPy.xml
VoronoiVertexPyImp.cpp
)

SET(Mod_SRCS
Expand Down Expand Up @@ -90,6 +96,10 @@ SET(Path_SRCS
FeatureArea.h
PathSegmentWalker.h
PathSegmentWalker.cpp
Voronoi.cpp
Voronoi.h
VoronoiVertex.cpp
VoronoiVertex.h
${Mod_SRCS}
${Python_SRCS}
)
Expand Down
106 changes: 106 additions & 0 deletions src/Mod/Path/App/Voronoi.cpp
@@ -0,0 +1,106 @@
/***************************************************************************
* Copyright (c) sliptonic (shopinthewoods@gmail.com) 2020 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/


#include "PreCompiled.h"

#ifndef _PreComp_
# include <cinttypes>
# include <iomanip>
# include <boost/algorithm/string.hpp>
# include <boost/lexical_cast.hpp>
#endif

#include <Base/Vector3D.h>
#include <Base/Writer.h>
#include <Base/Reader.h>
#include <Base/Exception.h>
#include "Voronoi.h"

using namespace Base;
using namespace Path;

TYPESYSTEM_SOURCE(Path::Voronoi , Base::BaseClass);

// Helpers

#if 0
static const std::size_t EXTERNAL_COLOR = 1;

static void color_exterior(const Voronoi::diagram_type::edge_type *edge) {
if (edge->color() == EXTERNAL_COLOR) {
// end recursion
return;
}
edge->color(EXTERNAL_COLOR);
edge->twin()->color(EXTERNAL_COLOR);
auto v = edge->vertex1();
if (v == NULL || !edge->is_primary()) {
return;
}
v->color(EXTERNAL_COLOR);
auto e = v->incident_edge();
do {
color_exterior(e);
e = e->rot_next();
} while (e != v->incident_edge());
}
#endif

// Constructors & destructors

Voronoi::Voronoi()
:vd(new diagram_type)
{
}

Voronoi::~Voronoi()
{
}


void Voronoi::addPoint(const Voronoi::point_type &p) {
points.push_back(p);
}

void Voronoi::addSegment(const Voronoi::segment_type &s) {
segments.push_back(s);
}


long Voronoi::numCells() const {
return vd->num_cells();
}

long Voronoi::numEdges() const {
return vd->num_edges();
}

long Voronoi::numVertices() const {
return vd->num_vertices();
}

void Voronoi::construct()
{
vd->clear();
construct_voronoi(points.begin(), points.end(), segments.begin(), segments.end(), (voronoi_diagram_type*)vd);
}
76 changes: 76 additions & 0 deletions src/Mod/Path/App/Voronoi.h
@@ -0,0 +1,76 @@
/***************************************************************************
* Copyright (c) sliptonic (shopinthewoods@gmail.com) 2020 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef PATH_VORONOI_H
#define PATH_VORONOI_H

#include <map>
#include <string>
#include <Base/BaseClass.h>
#include <Base/Handle.h>
#include <Base/Vector3D.h>

#include <vector>
#include <boost/polygon/point_concept.hpp>
#include <boost/polygon/polygon.hpp>
#include <boost/polygon/segment_concept.hpp>
#include <boost/polygon/voronoi.hpp>

namespace Path
{
class PathExport Voronoi
: public Base::BaseClass
{
TYPESYSTEM_HEADER();

public:
//constructors
Voronoi();
~Voronoi();

// types
typedef double coordinate_type;
typedef boost::polygon::point_data<coordinate_type> point_type;
typedef boost::polygon::segment_data<coordinate_type> segment_type;
typedef boost::polygon::voronoi_diagram<coordinate_type> voronoi_diagram_type;
class diagram_type
: public voronoi_diagram_type
, public Base::Handled
{ };

// specific methods
void addPoint(const point_type &p);
void addSegment(const segment_type &p);

void construct();
long numCells() const;
long numEdges() const;
long numVertices() const;

// attributes
std::vector<point_type> points;
std::vector<segment_type> segments;
Base::Reference<diagram_type> vd;
};

} //namespace Path

#endif // PATH_VORONOI_H
69 changes: 69 additions & 0 deletions src/Mod/Path/App/VoronoiPy.xml
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="BaseClassPy"
Name="VoronoiPy"
Twin="Voronoi"
TwinPointer="Voronoi"
Include="Mod/Path/App/Voronoi.h"
Namespace="Path"
FatherInclude="Base/BaseClassPy.h"
FatherNamespace="Base"
Constructor="true"
Delete="true">
<Documentation>
<Author Licence="LGPL" Name="sliptonic" EMail="shopinthewoods@gmail.com" />
<UserDocu>Voronoi([segments]): Create voronoi for given collection of line segments</UserDocu>
</Documentation>
<!--
<Attribute Name="Cells" ReadOnly="true">
<Documentation>
<UserDocu>List of all cells of the voronoi diagram</UserDocu>
</Documentation>
<Parameter Name="Cells" Type="List"/>
</Attribute>
<Attribute Name="Edges" ReadOnly="true">
<Documentation>
<UserDocu>List of all edges of the voronoi diagram</UserDocu>
</Documentation>
<Parameter Name="Edges" Type="List"/>
</Attribute>
-->
<Attribute Name="Vertices" ReadOnly="true">
<Documentation>
<UserDocu>List of all vertices of the voronoi diagram</UserDocu>
</Documentation>
<Parameter Name="Vertices" Type="List"/>
</Attribute>
<Methode Name="numCells" Const="true">
<Documentation>
<UserDocu>Return number of cells</UserDocu>
</Documentation>
</Methode>
<Methode Name="numEdges" Const="true">
<Documentation>
<UserDocu>Return number of edges</UserDocu>
</Documentation>
</Methode>
<Methode Name="numVertices" Const="true">
<Documentation>
<UserDocu>Return number of vertices</UserDocu>
</Documentation>
</Methode>
<Methode Name="addPoint">
<Documentation>
<UserDocu>addPoint(vector|vector2d) add given point to input collection</UserDocu>
</Documentation>
</Methode>
<Methode Name="addSegment">
<Documentation>
<UserDocu>addSegment(vector|vector2d, vector|vector2d) add given segment to input collection</UserDocu>
</Documentation>
</Methode>
<Methode Name="construct">
<Documentation>
<UserDocu>constructs the voronoi diagram from the input collections</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

0 comments on commit 27238c3

Please sign in to comment.