Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visprep #21

Merged
merged 5 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cliTest/cliTest.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ SOURCES += main.cpp \
testperformancewriter.cpp \
testselfcleaningfile.cpp \
../depthmapXcli/runmethods.cpp \
../depthmapXcli/modeparserregistry.cpp
../depthmapXcli/modeparserregistry.cpp \
testvisprepparser.cpp \
../depthmapXcli/visprepparser.cpp


HEADERS += \
../depthmapXcli/commandlineparser.h \
Expand Down
186 changes: 186 additions & 0 deletions cliTest/testvisprepparser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Copyright (C) 2017 Christian Sailer

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program 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 General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <catch.hpp>
#include "depthmapXcli/visprepparser.h"
#include "argumentholder.h"
#include "selfcleaningfile.h"

TEST_CASE("VisPrepParserFail", "Error cases")
{
SECTION("Missing argument to pg")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pg", "-pp", "1.2,1.3"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-pg requires an argument"));
}
SECTION("Missing argument to pp")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pp"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-pp requires an argument"));
}
SECTION("Missing argument to pf")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pf", "-pg", "1.2"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-pf requires an argument"));
}

SECTION("Non-numeric input to -pg")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pg", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-pg must be a number >0, got foo"));
}

SECTION("rubbish input to -pp")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pp", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Invalid fill point provided (foo). Should only contain digits dots and commas"));
}

SECTION("Non-existing file provide")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pg", "0.1", "-pf", "foo.csv"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Failed to load file foo.csv, error"));
}

SECTION("Neiter points nor point file provided")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pg", "0.1"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Either -pp or -pf must be given"));
}

SECTION("No grid")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pp", "0.1,1"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Use -pg to define grid"));
}

SECTION("Points and pointfile provided")
{
VisPrepParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\t2\n" << std::flush;
}
ArgumentHolder ah{"prog", "-pg", "0.1", "-pp", "0.1,5.2", "-pf", "testpoints.csv"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-pf cannot be used together with -pp"));
}

SECTION("Pointfile and points provided")
{
VisPrepParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\t2\n" << std::flush;
}
ArgumentHolder ah{"prog", "-pg", "0.1", "-pf", "testpoints.csv", "-pp", "0.1,5.2"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("-pp cannot be used together with -pf"));
}

SECTION("Malformed pointfile")
{
VisPrepParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\n" << std::flush;
}
ArgumentHolder ah{"prog", "-pg", "0.1", "-pf", "testpoints.csv"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Error parsing line: 1"));
}

SECTION("Malformed point arg")
{
VisPrepParser parser;
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f("testpoints.csv");
f << "x\ty\n1\n" << std::flush;
}
ArgumentHolder ah{"prog", "-pg", "0.1", "-pp", "0.1"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Error parsing line: 0.1"));
}

SECTION("Nonsensical visiblity restriction")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pr", "foo"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Restricted visibilyt of 'foo' makes no sense, use a positive number or -1 for unrestricted"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: visibilyt -> visibility

}

SECTION("Nonsensical visiblity restriction")
{
VisPrepParser parser;
ArgumentHolder ah{"prog", "-pr", "0.0"};
REQUIRE_THROWS_WITH(parser.parse(ah.argc(), ah.argv()), Catch::Contains("Restricted visibilyt of '0.0' makes no sense, use a positive number or -1 for unrestricted"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: visibilyt -> visibility

}

}

TEST_CASE("VisprepParserSuccess", "Read successfully")
{
VisPrepParser parser;
double x1 = 1.0;
double y1 = 2.0;
double x2 = 1.1;
double y2 = 1.2;
double grid = 0.5;
std::stringstream gstring;
gstring << grid << std::flush;

SECTION("Read from commandline")
{
std::stringstream p1;
p1 << x1 << "," << y1 << std::flush;
std::stringstream p2;
p2 << x2 << "," << y2 << std::flush;

ArgumentHolder ah{"prog", "-pg", gstring.str(), "-pp", p1.str(), "-pp", p2.str(), "-pb", "-pr", "2.1"};
parser.parse(ah.argc(), ah.argv());
REQUIRE(parser.getBoundaryGraph());
REQUIRE(parser.getMaxVisibility() == Approx(2.1));
}

SECTION("Read from file")
{
SelfCleaningFile scf("testpoints.csv");
{
std::ofstream f(scf.Filename().c_str());
f << "x\ty\n" << x1 << "\t" << y1 << "\n"
<< x2 << "\t" << y2 << "\n" << std::flush;
}
ArgumentHolder ah{"prog", "-pg", gstring.str(), "-pf", scf.Filename()};
parser.parse(ah.argc(), ah.argv() );
REQUIRE_FALSE(parser.getBoundaryGraph());
REQUIRE(parser.getMaxVisibility() == Approx(-1.0));
}

REQUIRE(parser.getGrid() == Approx(grid));
auto points = parser.getFillPoints();
REQUIRE(points.size() == 2);
REQUIRE(points[0].x == Approx(x1));
REQUIRE(points[0].y == Approx(y1));
REQUIRE(points[1].x == Approx(x2));
REQUIRE(points[1].y == Approx(y2));
}
5 changes: 2 additions & 3 deletions depthmapX/GraphDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,15 +669,14 @@ void QGraphDoc::OnEditGrid()
if ( QMessageBox::Yes != QMessageBox::question(this, tr("depthmapX"), tr("This will clear existing points. Do you want to continue?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::No) )
return;
}
CGridDialog dlg;
QtRegion r = m_meta_graph->SuperSpacePixel::getRegion();
dlg.m_maxdimension = __max(r.width(), r.height());
CGridDialog dlg(__max(r.width(), r.height()));
if (QDialog::Accepted == dlg.exec())
{
if (newmap) {
m_meta_graph->PointMaps::addNewMap();
}
m_meta_graph->setGrid( dlg.m_spacing, Point2f(0.0f, 0.0f) );
m_meta_graph->setGrid( dlg.getSpacing(), Point2f(0.0f, 0.0f) );
m_meta_graph->m_showgrid = true;
SetUpdateFlag(NEW_TABLE);
SetRedrawFlag(VIEW_ALL,REDRAW_GRAPH, NEW_DATA);
Expand Down
69 changes: 7 additions & 62 deletions depthmapX/GridDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2011-2012, Tasos Varoudis
// Copyright (C) 2017 Christian Sailer

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand All @@ -15,30 +16,21 @@

#include "GridDialog.h"
#include <QMessageBox>
#include <salalib/gridproperties.h>

CGridDialog::CGridDialog(QWidget *parent)
: QDialog(parent)
CGridDialog::CGridDialog(double maxDimension, QWidget *parent)
: QDialog(parent), m_maxdimension(maxDimension)
{
setupUi(this);
m_spacing = 0.01;
m_maxdimension = 1.0;
}

void CGridDialog::showEvent(QShowEvent * event)
{
m_maxexponent = (int) floor(log10(m_maxdimension)) - 1;
m_minexponent = m_maxexponent - 2;
m_basemantissa = (int) floor(m_maxdimension / pow(10.0,double(m_maxexponent+1)));
GridProperties gp(m_maxdimension);
m_spacing = gp.getDefault();

// current:
m_mantissa = m_basemantissa;
m_exponent = m_maxexponent - 1;

m_spacing = (double) m_mantissa * pow(10.0, double(m_exponent));

double truemax = (double) 2 * m_mantissa * pow(10.0, double(m_maxexponent));
double truemin = (double) m_mantissa * pow(10.0, double(m_minexponent));
c_spacing_ctrl->setRange(truemin, truemax);
c_spacing_ctrl->setRange(gp.getMin(), gp.getMax());

UpdateData(false);
}
Expand All @@ -58,48 +50,6 @@ void CGridDialog::OnDeltaposSpinSpacing(double iDelta)
void CGridDialog::OnOK()
{
UpdateData(true);

double truemax = (double) 2 * m_mantissa * pow(10.0, double(m_maxexponent));
double truemin = (double) m_mantissa * pow(10.0, double(m_minexponent));
if (m_spacing > truemax || m_spacing < truemin) {
QString formatabsmin, formatmin, formatmax;
if (m_minexponent < 0) {
formatmin.sprintf("%%.%df", abs(m_minexponent));
}
else {
formatmin = tr("%.0f");
}
if (m_minexponent-1 < 0) {
formatabsmin.sprintf("%%.%df", abs(m_minexponent-1));
}
else {
formatabsmin = tr("%.0f");
}
if (m_maxexponent < 0) {
formatmax.sprintf("%%.%df" ,abs(m_maxexponent));
}
else {
formatmax = tr("%.0f");
}
QString absminstr, minstr, maxstr;
absminstr.sprintf(formatabsmin.toLatin1(), truemin/10);
minstr.sprintf(formatmin.toLatin1(), truemin);
maxstr.sprintf(formatmax.toLatin1(), truemax);
if (m_spacing >= truemin / 10 && m_spacing < truemin) {
QString msg;
msg = tr("You are below the suggested minimum grid spacing of ") + minstr + tr(". If you use this grid spacing, it may cause processing problems.\nAre you sure you want to proceed with this grid spacing?");
if (QMessageBox::No == QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No)) {
return;
}
}
else {
QString msg;
msg = tr("Please enter a spacing between ") + absminstr + tr(" (at the absolute minimum) and ") + maxstr;
QMessageBox::warning(this, "Notice", msg, QMessageBox::Ok, QMessageBox::Ok);
return;
}
}

accept();
}

Expand All @@ -119,8 +69,3 @@ void CGridDialog::UpdateData(bool value)
c_spacing_ctrl->setValue(m_spacing);
}
}

void CGridDialog::userValue(double value)
{
m_spacing = c_spacing_ctrl->value();
}
21 changes: 7 additions & 14 deletions depthmapX/GridDialog.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2011-2012, Tasos Varoudis
// Copyright (C) 2017 Christian Sailer

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand All @@ -14,30 +15,22 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "ui_GridDialog.h"
#include <salalib/mgraph.h>
#include <salalib/attributes.h>
#include <salalib/shapemap.h>
#include <salalib/axialmap.h>


class CGridDialog : public QDialog, public Ui::CGridDialog
{
Q_OBJECT
public:
CGridDialog(QWidget *parent = 0);
double m_spacing;
double m_maxdimension;
int m_minexponent;
int m_maxexponent;
int m_basemantissa;
int m_mantissa;
int m_exponent;
CGridDialog(double maxDimension, QWidget *parent = 0);
void UpdateData(bool value);
void showEvent(QShowEvent * event);
double getSpacing() const { return m_spacing; }

private:
double m_spacing;
double m_maxdimension;

private slots:
void OnDeltaposSpinSpacing(double);
void OnOK();
void OnCancel();
void userValue(double value);
};
Loading