From 2ba54fe4cc727e72ef0ae9585ea882799c4d1dd5 Mon Sep 17 00:00:00 2001 From: Markus Lampert Date: Mon, 30 Sep 2019 16:24:59 -0700 Subject: [PATCH] Add BoundBox property to Path, usable without a gui. --- src/Mod/Path/App/Path.cpp | 73 ++++++++++++++++++++++++++++++++++ src/Mod/Path/App/Path.h | 2 + src/Mod/Path/App/PathPy.xml | 6 +++ src/Mod/Path/App/PathPyImp.cpp | 6 +++ 4 files changed, 87 insertions(+) diff --git a/src/Mod/Path/App/Path.cpp b/src/Mod/Path/App/Path.cpp index b67013e754..a5e51efcf9 100644 --- a/src/Mod/Path/App/Path.cpp +++ b/src/Mod/Path/App/Path.cpp @@ -39,6 +39,7 @@ //#include "Mod/Robot/App/kdl_cp/utilities/error.h" #include "Path.h" +#include using namespace Path; using namespace Base; @@ -145,6 +146,78 @@ double Toolpath::getLength() return l; } +class BoundBoxSegmentVisitor : public PathSegmentVisitor +{ +public: + BoundBoxSegmentVisitor() + { } + + virtual void g0(int id, const Base::Vector3d &last, const Base::Vector3d &next, const std::deque &pts) + { + (void)id; + processPt(last); + processPts(pts); + processPt(next); + } + virtual void g1(int id, const Base::Vector3d &last, const Base::Vector3d &next, const std::deque &pts) + { + (void)id; + processPt(last); + processPts(pts); + processPt(next); + } + virtual void g23(int id, const Base::Vector3d &last, const Base::Vector3d &next, const std::deque &pts, const Base::Vector3d ¢er) + { + (void)id; + (void)center; + processPt(last); + processPts(pts); + processPt(next); + } + virtual void g8x(int id, const Base::Vector3d &last, const Base::Vector3d &next, const std::deque &pts, + const std::deque &p, const std::deque &q) + { + (void)id; + (void)q; // always within the bounds of p + processPt(last); + processPts(pts); + processPts(p); + processPt(next); + } + virtual void g38(int id, const Base::Vector3d &last, const Base::Vector3d &next) + { + (void)id; + processPt(last); + processPt(next); + } + + Base::BoundBox3d bb; + +private: + void processPts(const std::deque &pts) { + for (std::deque::const_iterator it=pts.begin(); pts.end() != it; ++it) { + processPt(*it); + } + } + void processPt(const Base::Vector3d &pt) { + bb.MaxX = std::max(bb.MaxX, pt.x); + bb.MinX = std::min(bb.MinX, pt.x); + bb.MaxY = std::max(bb.MaxY, pt.y); + bb.MinY = std::min(bb.MinY, pt.y); + bb.MaxZ = std::max(bb.MaxZ, pt.z); + bb.MinZ = std::min(bb.MinZ, pt.z); + } +}; + +Base::BoundBox3d Toolpath::getBoundBox() const +{ + BoundBoxSegmentVisitor visitor; + PathSegmentWalker walker(*this); + walker.walk(visitor, Vector3d(0, 0, 0)); + + return visitor.bb; +} + static void bulkAddCommand(const std::string &gcodestr, std::vector &commands, bool &inches) { Command *cmd = new Command(); diff --git a/src/Mod/Path/App/Path.h b/src/Mod/Path/App/Path.h index 16319627b5..24e1023312 100644 --- a/src/Mod/Path/App/Path.h +++ b/src/Mod/Path/App/Path.h @@ -27,6 +27,7 @@ #include "Command.h" //#include "Mod/Robot/App/kdl_cp/path_composite.hpp" //#include "Mod/Robot/App/kdl_cp/frames_io.hpp" +#include #include #include @@ -62,6 +63,7 @@ namespace Path void recalculate(void); // recalculates the points void setFromGCode(const std::string); // sets the path from the contents of the given GCode string std::string toGCode(void) const; // gets a gcode string representation from the Path + Base::BoundBox3d getBoundBox(void) const; // shortcut functions unsigned int getSize(void) const { return vpcCommands.size(); } diff --git a/src/Mod/Path/App/PathPy.xml b/src/Mod/Path/App/PathPy.xml index 22c6722255..4440ea2379 100644 --- a/src/Mod/Path/App/PathPy.xml +++ b/src/Mod/Path/App/PathPy.xml @@ -40,6 +40,12 @@ commands (optional) is a list of Path commands + + + the extent of this path + + + adds a command or a list of commands at the end of the path diff --git a/src/Mod/Path/App/PathPyImp.cpp b/src/Mod/Path/App/PathPyImp.cpp index 2757d96913..5df6b892ba 100644 --- a/src/Mod/Path/App/PathPyImp.cpp +++ b/src/Mod/Path/App/PathPyImp.cpp @@ -29,6 +29,7 @@ #include "PathPy.h" #include "PathPy.cpp" +#include "Base/BoundBoxPy.h" #include "Base/GeometryPyCXX.h" #include "CommandPy.h" @@ -128,6 +129,11 @@ Py::Long PathPy::getSize(void) const return Py::Long((long)getToolpathPtr()->getSize()); } +Py::Object PathPy::getBoundBox(void) const +{ + return Py::BoundingBox(getToolpathPtr()->getBoundBox()); +} + // specific methods PyObject* PathPy::copy(PyObject * args)