Added support for voronoi cells

This commit is contained in:
Markus Lampert
2020-09-03 18:51:33 -07:00
committed by sliptonic
parent 13742cca41
commit bbd2c049a9
11 changed files with 413 additions and 18 deletions

View File

@@ -47,6 +47,8 @@
#include "AreaPy.h"
#include "FeatureArea.h"
#include "Voronoi.h"
#include "VoronoiCell.h"
#include "VoronoiCellPy.h"
#include "VoronoiEdge.h"
#include "VoronoiEdgePy.h"
#include "VoronoiPy.h"
@@ -79,6 +81,7 @@ PyMOD_INIT_FUNC(Path)
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::VoronoiCellPy ::Type, pathModule, "VoronoiCell");
Base::Interpreter().addType(&Path::VoronoiEdgePy ::Type, pathModule, "VoronoiEdge");
Base::Interpreter().addType(&Path::VoronoiVertexPy ::Type, pathModule, "VoronoiVertex");
@@ -104,6 +107,7 @@ PyMOD_INIT_FUNC(Path)
Path::FeatureAreaView ::init();
Path::FeatureAreaViewPython ::init();
Path::Voronoi ::init();
Path::VoronoiCell ::init();
Path::VoronoiEdge ::init();
Path::VoronoiVertex ::init();

View File

@@ -37,6 +37,7 @@ generate_from_xml(FeaturePathCompoundPy)
generate_from_xml(AreaPy)
generate_from_xml(FeatureAreaPy)
generate_from_xml(VoronoiPy)
generate_from_xml(VoronoiCellPy)
generate_from_xml(VoronoiEdgePy)
generate_from_xml(VoronoiVertexPy)
@@ -57,6 +58,8 @@ SET(Python_SRCS
FeatureAreaPyImp.cpp
VoronoiPy.xml
VoronoiPyImp.cpp
VoronoiCellPy.xml
VoronoiCellPyImp.cpp
VoronoiEdgePy.xml
VoronoiEdgePyImp.cpp
VoronoiVertexPy.xml
@@ -101,6 +104,8 @@ SET(Path_SRCS
PathSegmentWalker.cpp
Voronoi.cpp
Voronoi.h
VoronoiCell.cpp
VoronoiCell.h
VoronoiEdge.cpp
VoronoiEdge.h
VoronoiVertex.cpp

View File

@@ -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 *
* *
***************************************************************************/
#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"
#include "VoronoiCell.h"
using namespace Base;
using namespace Path;
TYPESYSTEM_SOURCE(Path::VoronoiCell , Base::Persistence)
VoronoiCell::VoronoiCell(Voronoi::diagram_type *d, long index)
: dia(d)
, index(index)
, ptr(0)
{
if (dia && long(dia->num_cells()) > index) {
ptr = &(dia->cells()[index]);
}
}
VoronoiCell::VoronoiCell(Voronoi::diagram_type *d, const Voronoi::diagram_type::cell_type *e)
: dia(d)
, index(Voronoi::InvalidIndex)
, ptr(e)
{
if (d && e) {
index = dia->index(e);
}
}
VoronoiCell::~VoronoiCell() {
}
bool VoronoiCell::isBound(void) const {
if (ptr != 0 && dia.isValid() && index != Voronoi::InvalidIndex) {
if (&(dia->cells()[index]) == ptr) {
return true;
}
}
ptr = 0;
return false;
}

View File

@@ -0,0 +1,54 @@
/***************************************************************************
* 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_VORONOICELL_H
#define PATH_VORONOICELL_H
#include <Base/Handle.h>
#include <Base/BaseClass.h>
#include <Base/Vector3D.h>
#include <Base/VectorPy.h>
#include "Voronoi.h"
namespace Path
{
class Voronoi;
class PathExport VoronoiCell
: public Base::BaseClass
{
TYPESYSTEM_HEADER();
public:
VoronoiCell(Voronoi::diagram_type *dia = 0, long index = Voronoi::InvalidIndex);
VoronoiCell(Voronoi::diagram_type *dia, const Voronoi::diagram_type::cell_type *cell);
~VoronoiCell();
bool isBound(void) const;
Base::Reference<Voronoi::diagram_type> dia;
long index;
mutable const Voronoi::diagram_type::cell_type *ptr;
};
}
#endif

View File

@@ -0,0 +1,59 @@
<?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="VoronoiCellPy"
Twin="VoronoiCell"
TwinPointer="VoronoiCell"
Include="Mod/Path/App/Voronoi.h"
FatherInclude="Base/BaseClassPy.h"
Namespace="Path"
FatherNamespace="Base"
Constructor="true"
RichCompare="true"
Delete="true">
<Documentation>
<Author Licence="LGPL" Name="sliptonic" EMail="shopinthewoods@gmail.com" />
<UserDocu>Cell of a Voronoi diagram</UserDocu>
</Documentation>
<Attribute Name="Color" ReadOnly="false">
<Documentation>
<UserDocu>Assigned color of the receiver.</UserDocu>
</Documentation>
<Parameter Name="Color" Type="Int"/>
</Attribute>
<Attribute Name="SourceIndex" ReadOnly="true">
<Documentation>
<UserDocu>Returns the index of the cell's source</UserDocu>
</Documentation>
<Parameter Name="SourceIndex" Type="Int"/>
</Attribute>
<Attribute Name="SourceCategory" ReadOnly="true">
<Documentation>
<UserDocu>Returns the index of the cell's source</UserDocu>
</Documentation>
<Parameter Name="SourceCategory" Type="Int"/>
</Attribute>
<Attribute Name="IncidentEdge" ReadOnly="true">
<Documentation>
<UserDocu>Incident edge of the cell - if exists</UserDocu>
</Documentation>
<Parameter Name="IncidentEdge" Type="Object"/>
</Attribute>
<Methode Name="containsPoint" Const="true">
<Documentation>
<UserDocu>Returns true if the cell contains a point site</UserDocu>
</Documentation>
</Methode>
<Methode Name="containsSegment" Const="true">
<Documentation>
<UserDocu>Returns true if the cell contains a segment site</UserDocu>
</Documentation>
</Methode>
<Methode Name="isDegenerate" Const="true">
<Documentation>
<UserDocu>Returns true if the cell doesn't have an incident edge</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@@ -0,0 +1,181 @@
/***************************************************************************
* 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 <boost/algorithm/string.hpp>
#endif
#include "Mod/Path/App/Voronoi.h"
#include "Mod/Path/App/Voronoi.h"
#include "Mod/Path/App/VoronoiCell.h"
#include "Mod/Path/App/VoronoiCellPy.h"
#include "Mod/Path/App/VoronoiEdge.h"
#include "Mod/Path/App/VoronoiEdgePy.h"
#include <Base/Exception.h>
#include <Base/GeometryPyCXX.h>
#include <Base/PlacementPy.h>
#include <Base/Vector3D.h>
#include <Base/VectorPy.h>
// files generated out of VoronoiCellPy.xml
#include "VoronoiCellPy.cpp"
using namespace Path;
// returns a string which represents the object e.g. when printed in python
std::string VoronoiCellPy::representation(void) const
{
std::stringstream ss;
ss.precision(5);
ss << "VoronoiCell(";
VoronoiCell *c = getVoronoiCellPtr();
if (c->isBound()) {
ss << c->ptr->source_category() << ":" << c->ptr->source_index();
}
ss << ")";
return ss.str();
}
PyObject *VoronoiCellPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
{
// create a new instance of VoronoiCellPy and the Twin object
return new VoronoiCellPy(new VoronoiCell);
}
// constructor method
int VoronoiCellPy::PyInit(PyObject* args, PyObject* /*kwd*/)
{
if (!PyArg_ParseTuple(args, "")) {
PyErr_SetString(PyExc_RuntimeError, "no arguments accepted");
return -1;
}
return 0;
}
PyObject* VoronoiCellPy::richCompare(PyObject *lhs, PyObject *rhs, int op) {
PyObject *cmp = Py_False;
if ( PyObject_TypeCheck(lhs, &VoronoiCellPy::Type)
&& PyObject_TypeCheck(rhs, &VoronoiCellPy::Type)
&& op == Py_EQ) {
const VoronoiCell *vl = static_cast<VoronoiCellPy*>(lhs)->getVoronoiCellPtr();
const VoronoiCell *vr = static_cast<VoronoiCellPy*>(rhs)->getVoronoiCellPtr();
if (vl->index == vr->index && vl->dia == vr->dia) {
cmp = Py_True;
}
}
Py_INCREF(cmp);
return cmp;
}
const Voronoi::voronoi_diagram_type::cell_type* getCellFromPy(VoronoiCellPy *c, bool throwIfNotBound = true) {
auto self = c->getVoronoiCellPtr();
if (self->isBound()) {
return self->ptr;
}
if (throwIfNotBound) {
throw Py::TypeError("Cell not bound to voronoi diagram");
}
return 0;
}
VoronoiCell* getVoronoiCellFromPy(const VoronoiCellPy *c, PyObject *args = 0) {
VoronoiCell *self = c->getVoronoiCellPtr();
if (!self->isBound()) {
throw Py::TypeError("Cell not bound to voronoi diagram");
}
if (args && !PyArg_ParseTuple(args, "")) {
throw Py::RuntimeError("No arguments accepted");
}
return self;
}
Py::Int VoronoiCellPy::getColor(void) const {
VoronoiCell *c = getVoronoiCellPtr();
if (c->isBound()) {
return Py::Int(c->dia->cells()[c->index].color());
}
return Py::Int(0);
}
void VoronoiCellPy::setColor(Py::Int color) {
getCellFromPy(this)->color(int(color) & 0x0FFFFFFF);
}
Py::Int VoronoiCellPy::getSourceIndex(void) const
{
VoronoiCell *c = getVoronoiCellFromPy(this);
return Py::Int(c->ptr->source_index());
}
Py::Int VoronoiCellPy::getSourceCategory(void) const
{
VoronoiCell *c = getVoronoiCellFromPy(this);
return Py::Int(c->ptr->source_category());
}
Py::Object VoronoiCellPy::getIncidentEdge(void) const
{
VoronoiCell *c = getVoronoiCellFromPy(this);
return Py::asObject(new VoronoiEdgePy(new VoronoiEdge(c->dia, c->ptr->incident_edge())));
}
PyObject* VoronoiCellPy::containsPoint(PyObject *args)
{
VoronoiCell *c = getVoronoiCellFromPy(this, args);
PyObject *chk = c->ptr->contains_point() ? Py_True : Py_False;
Py_INCREF(chk);
return chk;
}
PyObject* VoronoiCellPy::containsSegment(PyObject *args)
{
VoronoiCell *c = getVoronoiCellFromPy(this, args);
PyObject *chk = c->ptr->contains_segment() ? Py_True : Py_False;
Py_INCREF(chk);
return chk;
}
PyObject* VoronoiCellPy::isDegenerate(PyObject *args)
{
VoronoiCell *c = getVoronoiCellFromPy(this, args);
PyObject *chk = c->ptr->is_degenerate() ? Py_True : Py_False;
Py_INCREF(chk);
return chk;
}
// custom attributes get/set
PyObject* VoronoiCellPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int VoronoiCellPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}

View File

@@ -22,14 +22,12 @@
</Documentation>
<Parameter Name="Color" Type="Int"/>
</Attribute>
<!--
<Attribute Name="Cell" ReadOnly="true">
<Documentation>
<UserDocu>cell the edge belongs to</UserDocu>
</Documentation>
<Parameter Name="Cell" Type="Object"/>
</Attribute>
-->
<Attribute Name="Vertices" ReadOnly="true">
<Documentation>
<UserDocu>Begin and End voronoi vertex</UserDocu>

View File

@@ -29,6 +29,8 @@
#include "Mod/Path/App/Voronoi.h"
#include "Mod/Path/App/Voronoi.h"
#include "Mod/Path/App/VoronoiCell.h"
#include "Mod/Path/App/VoronoiCellPy.h"
#include "Mod/Path/App/VoronoiEdge.h"
#include "Mod/Path/App/VoronoiEdgePy.h"
#include "Mod/Path/App/VoronoiVertex.h"
@@ -191,6 +193,13 @@ Py::Object VoronoiEdgePy::getRotatedPrev(void) const
return Py::asObject(new VoronoiEdgePy(new VoronoiEdge(e->dia, e->ptr->rot_prev())));
}
Py::Object VoronoiEdgePy::getCell(void) const
{
VoronoiEdge *e = getVoronoiEdgeFromPy(this);
return Py::asObject(new VoronoiCellPy(new VoronoiCell(e->dia, e->ptr->cell())));
}
PyObject* VoronoiEdgePy::isFinite(PyObject *args)
{
VoronoiEdge *e = getVoronoiEdgeFromPy(this, args);

View File

@@ -15,14 +15,12 @@
<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>

View File

@@ -28,6 +28,7 @@
#endif
#include "Mod/Path/App/Voronoi.h"
#include "Mod/Path/App/VoronoiCell.h"
#include "Mod/Path/App/VoronoiEdge.h"
#include "Mod/Path/App/VoronoiVertex.h"
#include <Base/Exception.h>
@@ -38,6 +39,7 @@
// files generated out of VoronoiPy.xml
#include "VoronoiPy.h"
#include "VoronoiPy.cpp"
#include "VoronoiCellPy.h"
#include "VoronoiEdgePy.h"
#include "VoronoiVertexPy.h"
@@ -159,6 +161,14 @@ Py::List VoronoiPy::getEdges(void) const {
return list;
}
Py::List VoronoiPy::getCells(void) const {
Py::List list;
for (int i=0; i<getVoronoiPtr()->numCells(); ++i) {
list.append(Py::asObject(new VoronoiCellPy(new VoronoiCell(getVoronoiPtr()->vd, i))));
}
return list;
}
// custom attributes get/set
PyObject *VoronoiPy::getCustomAttributes(const char* /*attr*/) const

View File

@@ -101,6 +101,18 @@ const Voronoi::voronoi_diagram_type::vertex_type* getVertexFromPy(VoronoiVertexP
return 0;
}
VoronoiVertex* getVoronoiVertexFromPy(const VoronoiVertexPy *v, PyObject *args = 0) {
VoronoiVertex *self = v->getVoronoiVertexPtr();
if (!self->isBound()) {
throw Py::TypeError("Vertex not bound to voronoi diagram");
}
if (args && !PyArg_ParseTuple(args, "")) {
throw Py::RuntimeError("No arguments accepted");
}
return self;
}
Py::Int VoronoiVertexPy::getColor(void) const {
VoronoiVertex *v = getVoronoiVertexPtr();
if (v->isBound()) {
@@ -115,27 +127,16 @@ void VoronoiVertexPy::setColor(Py::Int color) {
Py::Float VoronoiVertexPy::getX(void) const
{
VoronoiVertex *v = getVoronoiVertexPtr();
if (!v->isBound()) {
throw Py::FloatingPointError("Cannot get coordinates of unbound voronoi vertex");
}
return Py::Float(v->ptr->x());
return Py::Float(getVoronoiVertexFromPy(this)->ptr->x());
}
Py::Float VoronoiVertexPy::getY(void) const
{
VoronoiVertex *v = getVoronoiVertexPtr();
if (!v->isBound()) {
throw Py::FloatingPointError("Cannot get coordinates of unbound voronoi vertex");
}
return Py::Float(v->ptr->y());
return Py::Float(getVoronoiVertexFromPy(this)->ptr->y());
}
Py::Object VoronoiVertexPy::getIncidentEdge() const {
VoronoiVertex *v = getVoronoiVertexPtr();
if (!v->isBound()) {
throw Py::TypeError("Vertex not bound to voronoi diagram");
}
VoronoiVertex *v = getVoronoiVertexFromPy(this);
return Py::asObject(new VoronoiEdgePy(new VoronoiEdge(v->dia, v->ptr->incident_edge())));
}