Skip to content

Commit

Permalink
Mesh: extend API to access edges of a facet via Python
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Oct 20, 2021
1 parent 3b310cf commit cef86fb
Show file tree
Hide file tree
Showing 14 changed files with 558 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Mod/Mesh/App/AppMesh.cpp
Expand Up @@ -33,6 +33,7 @@
#include "Mesh.h"
#include "MeshPy.h"
#include "MeshPointPy.h"
#include "EdgePy.h"
#include "FacetPy.h"
#include "MeshFeaturePy.h"
#include "FeatureMeshImport.h"
Expand Down Expand Up @@ -66,6 +67,7 @@ PyMOD_INIT_FUNC(Mesh)

// add mesh elements
Base::Interpreter().addType(&Mesh::MeshPointPy ::Type,meshModule,"MeshPoint");
Base::Interpreter().addType(&Mesh::EdgePy ::Type,meshModule,"Edge");
Base::Interpreter().addType(&Mesh::FacetPy ::Type,meshModule,"Facet");
Base::Interpreter().addType(&Mesh::MeshPy ::Type,meshModule,"Mesh");
Base::Interpreter().addType(&Mesh::MeshFeaturePy::Type,meshModule,"Feature");
Expand Down
5 changes: 5 additions & 0 deletions src/Mod/Mesh/App/CMakeLists.txt
Expand Up @@ -28,12 +28,14 @@ if (BUILD_QT5)
)
endif()

generate_from_xml(EdgePy)
generate_from_xml(FacetPy)
generate_from_xml(MeshFeaturePy)
generate_from_xml(MeshPointPy)
generate_from_xml(MeshPy)

SET(Mesh_XML_SRCS
EdgePy.xml
FacetPy.xml
MeshFeaturePy.xml
MeshPointPy.xml
Expand Down Expand Up @@ -331,6 +333,9 @@ SET(Mesh_SRCS
Exporter.h
Importer.cpp
Importer.h
Edge.cpp
Edge.h
EdgePyImp.cpp
Facet.cpp
Facet.h
FacetPyImp.cpp
Expand Down
19 changes: 19 additions & 0 deletions src/Mod/Mesh/App/Core/Elements.cpp
Expand Up @@ -280,6 +280,25 @@ bool MeshGeomEdge::IntersectWithLine (const Base::Vector3f &rclPt,
return dist2 + dist3 <= dist1 + eps;
}

bool MeshGeomEdge::IsParallel(const MeshGeomEdge &edge) const
{
Base::Vector3f r(_aclPoints[1] - _aclPoints[0]);
Base::Vector3f s(edge._aclPoints[1] - edge._aclPoints[0]);
Base::Vector3f n = r.Cross(s);
return n.IsNull();
}

bool MeshGeomEdge::IsCollinear(const MeshGeomEdge &edge) const
{
if (IsParallel(edge)) {
Base::Vector3f r(_aclPoints[1] - _aclPoints[0]);
Base::Vector3f d = edge._aclPoints[0] - _aclPoints[0];
return d.Cross(r).IsNull();
}

return false;
}

bool MeshGeomEdge::IntersectWithEdge (const MeshGeomEdge &edge, Base::Vector3f &res) const
{
const float eps = 1e-06f;
Expand Down
10 changes: 10 additions & 0 deletions src/Mod/Mesh/App/Core/Elements.h
Expand Up @@ -198,6 +198,16 @@ class MeshExport MeshGeomEdge
* Checks if the projection point of \a point lies on the edge.
*/
bool IsProjectionPointOf(const Base::Vector3f& point) const;
/**
* Checks if the two edges are parallel.
* \note Parallel edges could be collinear.
*/
bool IsParallel(const MeshGeomEdge &edge) const;
/**
* Checks if the two edges are collinear.
* \note Collinear edges always are parallel.
*/
bool IsCollinear(const MeshGeomEdge &edge) const;

public:
Base::Vector3f _aclPoints[2]; /**< Corner points */
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Mesh/App/Core/tritritest.h
Expand Up @@ -41,7 +41,7 @@ OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
*/


#include <math.h>
#include <cmath>

#define FABS(x) ((float)fabs(x)) /* implement as is fastest on your machine */

Expand Down
74 changes: 74 additions & 0 deletions src/Mod/Mesh/App/Edge.cpp
@@ -0,0 +1,74 @@
/***************************************************************************
* Copyright (c) 2021 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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., 51 Franklin Street, *
* Fifth Floor, Boston, MA 02110-1301, USA *
* *
***************************************************************************/


#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
#endif

#include "Edge.h"
#include "Mesh.h"

using namespace Mesh;

Edge::Edge()
: Index(-1)
, Mesh(nullptr)
{
for (int i=0; i<2; i++) {
PIndex[i] = MeshCore::POINT_INDEX_MAX;
NIndex[i] = MeshCore::FACET_INDEX_MAX;
}
}

Edge::Edge(const Edge& e)
: MeshCore::MeshGeomEdge(e)
, Index(e.Index)
, Mesh(e.Mesh)
{
for (int i=0; i<2; i++) {
PIndex[i] = e.PIndex[i];
NIndex[i] = e.NIndex[i];
}
}

Edge::~Edge()
{
}

void Edge::operator = (const Edge& e)
{
MeshCore::MeshGeomEdge::operator = (e);
Mesh = e.Mesh;
Index = e.Index;
for (int i=0; i<2; i++) {
PIndex[i] = e.PIndex[i];
NIndex[i] = e.NIndex[i];
}
}

void Edge::unbound()
{
Index = -1;
Mesh = nullptr;
}
63 changes: 63 additions & 0 deletions src/Mod/Mesh/App/Edge.h
@@ -0,0 +1,63 @@
/***************************************************************************
* Copyright (c) 2021 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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., 51 Franklin Street, *
* Fifth Floor, Boston, MA 02110-1301, USA *
* *
***************************************************************************/


#ifndef MESH_EDGE_H
#define MESH_EDGE_H

#include <Base/Matrix.h>
#include <Base/Vector3D.h>
#include <Base/Handle.h>

#include <Mod/Mesh/App/Core/Elements.h>

namespace Mesh
{
// forward declaration
class MeshObject;

/** The Edge helper class
* The Edge class provides an interface for the EdgePy class for
* convenient access to the Mesh data structure. This class should not be used
* for programming algorithms in C++. Use Mesh Core classes instead!
*/
class MeshExport Edge : public MeshCore::MeshGeomEdge
{
public:
Edge();
Edge(const Edge& f);
~Edge();

bool isBound() const {return Index != -1;}
void unbound();
void operator = (const Edge& f);

int Index;
MeshCore::PointIndex PIndex[2];
MeshCore::FacetIndex NIndex[2];
Base::Reference<MeshObject> Mesh;
};

} // namespace Mesh


#endif // MESH_EDGE_H
90 changes: 90 additions & 0 deletions src/Mod/Mesh/App/EdgePy.xml
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="PyObjectBase"
Name="EdgePy"
Twin="Edge"
TwinPointer="Edge"
Include="Mod/Mesh/App/Edge.h"
FatherInclude="Base/PyObjectBase.h"
Namespace="Mesh"
Constructor="true"
Delete="true"
FatherNamespace="Base">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer[at]users.sourceforge.net" />
<DeveloperDocu>Edge in a Mesh</DeveloperDocu>
<UserDocu>Edge in mesh
This is an edge of a facet in a MeshObject. You can get it by e.g. iterating over the facets of a
mesh and calling getEdge(index).
</UserDocu>
</Documentation>
<Methode Name="intersectWithEdge">
<Documentation>
<UserDocu>intersectWithEdge(Edge) -> list
Get a list of intersection points with another edge.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="isParallel">
<Documentation>
<UserDocu>isParallel(Edge) -> bool
Checks if the two edges are parallel.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="isCollinear">
<Documentation>
<UserDocu>isCollinear(Edge) -> bool
Checks if the two edges are collinear.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="unbound">
<Documentation>
<UserDocu>method unbound()
Cut the connection to a MeshObject. The edge becomes
free and is more or less a simple edge.
After calling unbound() no topological operation will
work!
</UserDocu>
</Documentation>
</Methode>
<Attribute Name="Index" ReadOnly="true">
<Documentation>
<UserDocu>The index of this edge of the facet</UserDocu>
</Documentation>
<Parameter Name="Index" Type="Long"/>
</Attribute>
<Attribute Name="Points" ReadOnly="true">
<Documentation>
<UserDocu>A list of points of the edge</UserDocu>
</Documentation>
<Parameter Name="Points" Type="List"/>
</Attribute>
<Attribute Name="PointIndices" ReadOnly="true">
<Documentation>
<UserDocu>The index tuple of point vertices of the mesh this edge is built of</UserDocu>
</Documentation>
<Parameter Name="PointIndices" Type="Tuple"/>
</Attribute>
<Attribute Name="NeighbourIndices" ReadOnly="true">
<Documentation>
<UserDocu>The index tuple of neighbour facets of the mesh this edge is adjacent with</UserDocu>
</Documentation>
<Parameter Name="NeighbourIndices" Type="Tuple"/>
</Attribute>
<Attribute Name="Length" ReadOnly="true">
<Documentation>
<UserDocu>The length of the edge</UserDocu>
</Documentation>
<Parameter Name="Length" Type="Float"/>
</Attribute>
<Attribute Name="Bound" ReadOnly="true">
<Documentation>
<UserDocu>Bound state of the edge</UserDocu>
</Documentation>
<Parameter Name="Bound" Type="Boolean"/>
</Attribute>
</PythonExport>
</GenerateModel>

0 comments on commit cef86fb

Please sign in to comment.