Add utility class to better debug algorithms

This commit is contained in:
wmayer 2012-08-11 15:09:08 +02:00
parent 0d351e98e2
commit 1859ec2559
6 changed files with 176 additions and 0 deletions

View File

@ -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

80
src/Base/Debugger.cpp Normal file
View File

@ -0,0 +1,80 @@
/***************************************************************************
* Copyright (c) 2012 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 <QCoreApplication>
# include <QEvent>
#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"

76
src/Base/Debugger.h Normal file
View File

@ -0,0 +1,76 @@
/***************************************************************************
* Copyright (c) 2012 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 BASE_DEBUGGER_H
#define BASE_DEBUGGER_H
#include <QObject>
#include <QEventLoop>
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

View File

@ -105,6 +105,8 @@
// QtCore
#include <QBuffer>
#include <QByteArray>
#include <QCoreApplication>
#include <QEvent>
#include <QIODevice>
#include <QDataStream>
#include <QWriteLocker>

View File

@ -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<std::vector<Base::Vector3f> > &rclBorders) const
{
std::vector<unsigned long> aulAllFacets(_rclMesh.CountFacets());

View File

@ -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.
*/