From 1859ec25597f3e133af7d07b5b3f12274dd8a2f0 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 11 Aug 2012 15:09:08 +0200 Subject: [PATCH] Add utility class to better debug algorithms --- src/Base/CMakeLists.txt | 3 ++ src/Base/Debugger.cpp | 80 +++++++++++++++++++++++++++++ src/Base/Debugger.h | 76 +++++++++++++++++++++++++++ src/Base/PreCompiled.h | 2 + src/Mod/Mesh/App/Core/Algorithm.cpp | 11 ++++ src/Mod/Mesh/App/Core/Algorithm.h | 4 ++ 6 files changed, 176 insertions(+) create mode 100644 src/Base/Debugger.cpp create mode 100644 src/Base/Debugger.h diff --git a/src/Base/CMakeLists.txt b/src/Base/CMakeLists.txt index 3cab43354..4b5a06170 100644 --- a/src/Base/CMakeLists.txt +++ b/src/Base/CMakeLists.txt @@ -152,6 +152,7 @@ SET(FreeCADBase_XML_SRCS SOURCE_GROUP("XML" FILES ${FreeCADBase_XML_SRCS}) set(FreeCADBase_MOC_HDRS + Debugger.h FutureWatcherProgress.h ) fc_wrap_cpp(FreeCADBase_MOC_SRCS ${FreeCADBase_MOC_HDRS}) @@ -182,6 +183,7 @@ SET(FreeCADBase_CPP_SRCS BoundBoxPyImp.cpp Builder3D.cpp Console.cpp + Debugger.cpp Exception.cpp Factory.cpp FileInfo.cpp @@ -234,6 +236,7 @@ SET(FreeCADBase_HPP_SRCS BoundBox.h Builder3D.h Console.h + Debugger.h Exception.h Factory.h FileInfo.h diff --git a/src/Base/Debugger.cpp b/src/Base/Debugger.cpp new file mode 100644 index 000000000..2b08f0aa4 --- /dev/null +++ b/src/Base/Debugger.cpp @@ -0,0 +1,80 @@ +/*************************************************************************** + * Copyright (c) 2012 Werner Mayer * + * * + * 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 +# include +#endif + +#include "Debugger.h" +#include "Console.h" + +using namespace Base; + +Debugger::Debugger(QObject* parent) + : QObject(parent), isAttached(false) +{ +} + +Debugger::~Debugger() +{ +} + +void Debugger::attach() +{ + QCoreApplication::instance()->installEventFilter(this); + isAttached = true; +} + +void Debugger::detach() +{ + QCoreApplication::instance()->removeEventFilter(this); + isAttached = false; +} + +bool Debugger::eventFilter(QObject*, QEvent* event) +{ + if (event->type() == QEvent::KeyPress) { + if (loop.isRunning()) { + loop.quit(); + return true; + } + } + + return false; +} + +int Debugger::exec() +{ + if (isAttached) + Base::Console().Message("TO CONTINUE PRESS ANY KEY...\n"); + return loop.exec(); +} + +void Debugger::quit() +{ + loop.quit(); +} + +#include "moc_Debugger.cpp" diff --git a/src/Base/Debugger.h b/src/Base/Debugger.h new file mode 100644 index 000000000..114b7e38b --- /dev/null +++ b/src/Base/Debugger.h @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (c) 2012 Werner Mayer * + * * + * 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 BASE_DEBUGGER_H +#define BASE_DEBUGGER_H + +#include +#include + +namespace Base { +/** + This is a utility class to break the application at a point to inspect e.g. the result of + an algorithm. + You usually use it like this + \code + ... + Base::Debugger dbg; + dbg.attach(); + dbg.exec(); + ... + \endcode + Or you can connect it with a button and let the user click it in order to continue. + \code + QPushButton* btn = new QPushButton(); + btn->setText("Continue"); + btn->show(); + Base::Debugger dbg; + connect(btn, SIGNAL(clicked()), &dbg, SLOT(quit())); + dbg.exec(); + \endcode + \author Werner Mayer + */ +class BaseExport Debugger : public QObject +{ + Q_OBJECT + +public: + Debugger(QObject* parent=0); + ~Debugger(); + + void attach(); + void detach(); + bool eventFilter(QObject*, QEvent*); + int exec(); + +public Q_SLOTS: + void quit(); + +private: + bool isAttached; + QEventLoop loop; +}; + +} + +#endif // BASE_DEBUGGER_H diff --git a/src/Base/PreCompiled.h b/src/Base/PreCompiled.h index f3b7dba3b..8d9663454 100644 --- a/src/Base/PreCompiled.h +++ b/src/Base/PreCompiled.h @@ -105,6 +105,8 @@ // QtCore #include #include +#include +#include #include #include #include diff --git a/src/Mod/Mesh/App/Core/Algorithm.cpp b/src/Mod/Mesh/App/Core/Algorithm.cpp index f72bba760..828a6675a 100644 --- a/src/Mod/Mesh/App/Core/Algorithm.cpp +++ b/src/Mod/Mesh/App/Core/Algorithm.cpp @@ -255,6 +255,17 @@ float MeshAlgorithm::GetAverageEdgeLength() const return fLen; } +Base::Vector3f MeshAlgorithm::GetGravityPoint() const +{ + Base::Vector3f center; + MeshPointIterator cP(_rclMesh); + for (cP.Init(); cP.More(); cP.Next()) { + center += *cP; + } + + return center / (float)_rclMesh.CountPoints(); +} + void MeshAlgorithm::GetMeshBorders (std::list > &rclBorders) const { std::vector aulAllFacets(_rclMesh.CountFacets()); diff --git a/src/Mod/Mesh/App/Core/Algorithm.h b/src/Mod/Mesh/App/Core/Algorithm.h index 78a4caf5b..7d55d6e94 100644 --- a/src/Mod/Mesh/App/Core/Algorithm.h +++ b/src/Mod/Mesh/App/Core/Algorithm.h @@ -115,6 +115,10 @@ public: * Calculates the average length of edges. */ float GetAverageEdgeLength() const; + /** + * Calculates the gravity point of the mesh. + */ + Base::Vector3f GetGravityPoint() const; /** * Returns all boundaries of the mesh. */