Skip to content

Commit

Permalink
added measurement panel.
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Nov 12, 2024
1 parent 98af839 commit 2298ba1
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 3 deletions.
2 changes: 1 addition & 1 deletion relightlab/imageview.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public slots:
void one() { view->one(); }
void next() { view->next(); }
void prev() { view->prev(); }
protected:

protected:
QToolBar *toolbar;
};

Expand Down
5 changes: 5 additions & 0 deletions relightlab/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "homeframe.h"
#include "imageframe.h"
#include "alignframe.h"
#include "scaleframe.h"
#include "lightsframe.h"
#include "cropframe.h"
#include "rtiframe.h"
Expand All @@ -32,12 +33,14 @@ MainWindow::MainWindow() {
tabs->addTab(home_frame = new HomeFrame, "Home");
tabs->addTab(image_frame = new ImageFrame, "Images");
tabs->addTab(align_frame = new AlignFrame, "Align");
tabs->addTab(scale_frame = new ScaleFrame, "Scale");
tabs->addTab(lights_frame = new LightsFrame, "Lights");
tabs->addTab(crop_frame = new CropFrame, "Crop");
tabs->addTab(rti_frame = new RtiFrame, "RTI");
tabs->addTab(normals_frame = new NormalsFrame, "Normals");
tabs->addTab(queue_frame = new QueueFrame, "Queue");


setCentralWidget(tabs);
}

Expand Down Expand Up @@ -101,12 +104,14 @@ void MainWindow::openRecentProject() {

void MainWindow::clear() {
image_frame->clear();
scale_frame->clear();
lights_frame->clear();
crop_frame->clear();
}

void MainWindow::init() {
image_frame->init();
scale_frame->init();
lights_frame->init();
crop_frame->init();
}
Expand Down
2 changes: 2 additions & 0 deletions relightlab/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TabWidget;
class HomeFrame;
class ImageFrame;
class AlignFrame;
class ScaleFrame;
class LightsFrame;
class CropFrame;
class RtiFrame;
Expand All @@ -34,6 +35,7 @@ class MainWindow: public QMainWindow {
HomeFrame *home_frame = nullptr;
ImageFrame *image_frame = nullptr;
AlignFrame *align_frame = nullptr;
ScaleFrame *scale_frame = nullptr;
LightsFrame *lights_frame = nullptr;
CropFrame *crop_frame = nullptr;
RtiFrame *rti_frame = nullptr;
Expand Down
6 changes: 4 additions & 2 deletions relightlab/relightlab.pro
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ SOURCES += main.cpp \
../relight/httpserver.cpp \
normalsframe.cpp \
normalstask.cpp \
../src/bni_normal_integration.cpp
../src/bni_normal_integration.cpp \
scaleframe.cpp

RESOURCES += \
res.qrc
Expand Down Expand Up @@ -172,7 +173,8 @@ HEADERS += \
../relight/httplib.h \
normalsframe.h \
normalstask.h \
../src/bni_normal_integration.h
../src/bni_normal_integration.h \
scaleframe.h

FORMS +=

Expand Down
205 changes: 205 additions & 0 deletions relightlab/scaleframe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#include "scaleframe.h"
#include "imageview.h"
#include "relightapp.h"
#include "../src/measure.h"

#include <QVBoxLayout>
#include <QPushButton>
#include <QDoubleSpinBox>
#include <QLabel>
#include <QGraphicsPathItem>
#include <QGraphicsLineItem>
#include <QGraphicsTextItem>

#include <assert.h>

ScaleFrame::ScaleFrame(QWidget *parent): QFrame(parent) {

QVBoxLayout *content = new QVBoxLayout(this);

QHBoxLayout *controls = new QHBoxLayout;
content->addLayout(controls);

QPushButton *take = new QPushButton("Take a new measurement...");
controls->addWidget(take);

scale = new QDoubleSpinBox;
controls->addWidget(scale);

pixelSize = new QLabel;
controls->addWidget(pixelSize);

QPushButton *remove = new QPushButton(QIcon::fromTheme("trash-2"), "");
controls->addWidget(remove);

viewer = new ImageViewer;
viewer->showImage(0);
content->addWidget(viewer);

QPainterPath path;
path.moveTo(-8, -8);
path.lineTo(-3, -3);
path.moveTo( 8, -8);
path.lineTo( 3, -3);
path.moveTo( 8, 8);
path.lineTo( 3, 3);
path.moveTo(-8, 8);
path.lineTo(-3, 3);

first = new QGraphicsPathItem(path);
second = new QGraphicsPathItem(path);
line = new QGraphicsLineItem();
text = new QGraphicsTextItem();
QPen pen;
pen.setColor(Qt::yellow);
pen.setWidth(1);

first->setPen(pen);
second->setPen(pen);
line->setPen(pen);
text->setDefaultTextColor(Qt::yellow);
text->setFont(QFont("Arial", 16));


connect(take, SIGNAL(clicked()), this, SLOT(startPicking()));
connect(viewer->view, SIGNAL(clicked(QPoint)), this, SLOT(click(QPoint)));
connect(scale, SIGNAL(valueChanged(double)), this, SLOT(setLength(double)));
connect(remove, SIGNAL(clicked()), this, SLOT(removeScale()));
}

ScaleFrame::~ScaleFrame() {
auto &scene = viewer->scene();

clear();

delete first;
delete second;
delete line;
delete text;
}

void ScaleFrame::clear() {
auto &scene = viewer->scene();

if(first->scene())
scene.removeItem(first);
if(second->scene())
scene.removeItem(second);
if(line->scene())
scene.removeItem(line);
if(text->scene())
scene.removeItem(text);
scale->setValue(0.0);
status = NOTHING;
}

void ScaleFrame::init() {
auto &scene = viewer->scene();

viewer->showImage(0);

Project &project = qRelightApp->project();
if(project.measures.size()) {
Measure *measure = project.measures[0];
setFirst(measure->first);
setSecond(measure->second);
setLengthLabel(measure->length);
status = DONE;
}
}

void ScaleFrame::removeScale() {
bool proceed = QMessageBox::question(this, "Removing measurement", "You are removing a measurement. Proceed?");
if(!proceed)
return;

Project &project = qRelightApp->project();
for(Measure *m: project.measures)
delete m;
project.measures.clear();

clear();
}

void ScaleFrame::startPicking() {
if(status == DONE) {
bool proceed = QMessageBox::question(this, "Taking a new measurement", "You are removing a measurement and taking a new one. Proceed?");
if(!proceed)
return;
}
clear();
status = FIRST_POINT;
QApplication::setOverrideCursor(Qt::CrossCursor);
}

void ScaleFrame::cancelPicking() {
clear();
QApplication::restoreOverrideCursor();
}

void ScaleFrame::click(QPoint q) {
QPointF p = viewer->view->mapToScene(q);

if(status == FIRST_POINT) {
setFirst(p);
status = SECOND_POINT;
} else if(status == SECOND_POINT) {
setSecond(p);
status = DONE;
QApplication::restoreOverrideCursor();
}

}

void ScaleFrame::setFirst(QPointF p) {
auto &scene = viewer->scene();

first->setPos(p);
scene.addItem(first);
}

void ScaleFrame::setSecond(QPointF p) {
auto &scene = viewer->scene();

second->setPos(p);
line->setLine(QLineF(first->pos(), p));
text->setPos((first->pos() + second->pos())/2);

scene.addItem(second);
scene.addItem(line);
}

void ScaleFrame::setLengthLabel(double length) {
auto &scene = viewer->scene();

text->setPlainText(QString::number(length) + "mm");
text->setPos((first->pos() + second->pos())/2);
if(!text->scene())
scene.addItem(text);

scale->setValue(length);
pixelSize->setText(QString("Pixel size in mm: %1").arg(qRelightApp->project().pixelSize));
}

void ScaleFrame::setLength(double length) {
auto &scene = viewer->scene();

text->setPlainText(QString::number(length) + "mm");
if(!text->scene())
scene.addItem(text);

Project &project = qRelightApp->project();
for(Measure *m: project.measures)
delete m;
project.measures.clear();

Measure *measure = new Measure;
measure->first = first->pos();
measure->second = second->pos();
measure->length = scale->value();
project.measures.push_back(measure);
project.computePixelSize();
pixelSize->setText(QString("Pixel size in mm: %1").arg(qRelightApp->project().pixelSize));
}


51 changes: 51 additions & 0 deletions relightlab/scaleframe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef SCALEFRAME_H
#define SCALEFRAME_H

#include <QFrame>

class ImageViewer;
class QDoubleSpinBox;
class QLabel;
class QGraphicsPathItem;
class QGraphicsLineItem;
class QGraphicsTextItem;


/* mark a segment and specify a distance */


class ScaleFrame: public QFrame {
Q_OBJECT
public:
ScaleFrame(QWidget *parent = nullptr);
~ScaleFrame();
void clear();
void init();
void setFirst(QPointF p);
void setSecond(QPointF p);
void setLengthLabel(double length);

public slots:
void click(QPoint p);
void startPicking();
void cancelPicking();
void removeScale();
void setLength(double length); //called when user modified the value in the spinbox.



protected:
enum Measuring { NOTHING = 0, FIRST_POINT = 1, SECOND_POINT = 2, DONE = 3 };
Measuring status;

QDoubleSpinBox *scale = nullptr;
QLabel *pixelSize = nullptr;
ImageViewer *viewer = nullptr;

QGraphicsPathItem *first = nullptr;
QGraphicsPathItem *second = nullptr;
QGraphicsLineItem *line = nullptr;
QGraphicsTextItem *text = nullptr;
};

#endif // SCALEFRAME_H

0 comments on commit 2298ba1

Please sign in to comment.