Skip to content

Commit

Permalink
refactor align and bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Jan 24, 2025
1 parent db29bfd commit 7635fc7
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 134 deletions.
4 changes: 4 additions & 0 deletions relightlab/alignframe.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "alignframe.h"
#include "alignpicking.h"
#include "imageview.h"
#include "flowlayout.h"
#include "relightapp.h"
Expand All @@ -25,6 +26,9 @@ AlignFrame::AlignFrame(QWidget *parent): QFrame(parent) {
content->addWidget(aligns_frame);
aligns = new QVBoxLayout(aligns_frame);

AlignPicking *picking = new AlignPicking();
content->addWidget(picking);

//content->addStretch();
connect(new_align, SIGNAL(clicked()), this, SLOT(newAlign()));
}
Expand Down
2 changes: 2 additions & 0 deletions relightlab/alignpicking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ AlignPicking::AlignPicking(QWidget *parent): ImageViewer(parent) {
rect = new AlignRect(this, 0, 0, 0, 0);
rect->setPen(QPen(Qt::yellow, 2));
rect->setBrush(Qt::transparent);

view->setCursor(Qt::CrossCursor);
}


Expand Down
1 change: 1 addition & 0 deletions relightlab/imagelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void ImageList::init() {
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
item->setCheckState(img.skip? Qt::Unchecked : Qt::Checked);
addItem(item);
count++;
}

connect(this, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(verifyItem(QListWidgetItem *)));
Expand Down
1 change: 0 additions & 1 deletion relightlab/spherepicking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ SpherePicking::SpherePicking(QWidget *parent): ImageViewer(parent) {
scene().addItem(axis[1]);

connect(view, SIGNAL(clicked(QPoint)), this, SLOT(click(QPoint)));
//TODO: rename something, it conflicts!
view->setCursor(Qt::CrossCursor);

//QApplication::setOverrideCursor(Qt::CrossCursor);
Expand Down
4 changes: 2 additions & 2 deletions relightlab/verifydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ VerifyDialog::VerifyDialog(std::vector<QImage> &_thumbs, std::vector<QPointF> &_
for(size_t i = 0; i < thumbs.size(); i++) {
assert(!thumbs[i].isNull());
if(marker == REFLECTION ) {
ReflectionVerify *thumb = new ReflectionVerify(thumbs[i], 192, positions[i]);
VerifyView *thumb = new VerifyView(thumbs[i], 192, positions[i], VerifyMarker::REFLECTION);
flowlayout->addWidget(thumb);
} else {
AlignVerify *thumb = new AlignVerify(thumbs[i], 192, positions[i]);
VerifyView *thumb = new VerifyView(thumbs[i], 192, positions[i], VerifyMarker::ALIGN);
flowlayout->addWidget(thumb);
}
}
Expand Down
118 changes: 34 additions & 84 deletions relightlab/verifyview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,40 @@
#include <QDebug>
#include <assert.h>

ReflectionPoint::ReflectionPoint(VerifyView *_view, QRectF rect, QGraphicsItem *parent):
QGraphicsEllipseItem(rect, parent), view(_view) {
init();
}

void ReflectionPoint::init() {
VerifyMarker::VerifyMarker(VerifyView *_view, Marker _marker, QGraphicsItem *parent):
QGraphicsItem(parent), view(_view), marker(_marker) {
setCursor(Qt::CrossCursor);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
}


AlignPoint::AlignPoint(VerifyView *_view, QGraphicsItem *parent):
QGraphicsPathItem(parent), view(_view) {
QPainterPath VerifyMarker::shape() const {
QPainterPath path;
path.moveTo(-2, 0);
path.lineTo(2, 0);
path.moveTo(0, -2);
path.lineTo(0, 2);
setPath(path);
init();
if(marker == ALIGN)
path.addRect(-3, -3, 6, 6);
else
path.addRect(-radius-2, -radius-2, 2*(radius+2), 2*(radius+2));
return path;
}

void AlignPoint::init() {
setCursor(Qt::CrossCursor);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
QRectF VerifyMarker::boundingRect() const {
return QRectF(-3, -3, 6, 6);
}

QVariant ReflectionPoint::itemChange(GraphicsItemChange change, const QVariant &value) {
if ((change == ItemPositionChange && scene()) ) {

QPointF newPos = value.toPointF();
QRectF rect = scene()->sceneRect();
if (!rect.contains(newPos)) {
// Keep the item inside the scene rect.
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
}
return newPos;
}

if(change == ItemScenePositionHasChanged) {
view->update();
void VerifyMarker::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) {
QPen pen(active ? Qt::green : Qt::red, marker == ALIGN? 0.2 : 2);
painter->setPen(pen);
if(marker == ALIGN) {
painter->drawLine(QPointF(-2, 0), QPointF(2, 0));
painter->drawLine(QPointF(0, -2), QPointF(0, 2));
} else {
painter->drawEllipse(QPointF(0, 0), radius, radius);
}
return QGraphicsItem::itemChange(change, value);
}

QVariant AlignPoint::itemChange(GraphicsItemChange change, const QVariant &value) {
QVariant VerifyMarker::itemChange(GraphicsItemChange change, const QVariant &value) {
if ((change == ItemPositionChange && scene()) ) {

QPointF newPos = value.toPointF();
Expand All @@ -79,8 +62,9 @@ QVariant AlignPoint::itemChange(GraphicsItemChange change, const QVariant &value
return QGraphicsItem::itemChange(change, value);
}

VerifyView:: VerifyView(QImage &_image, int _height, QPointF &_pos, QWidget *parent):
QGraphicsView(parent), image(_image), pos(_pos) {

VerifyView:: VerifyView(QImage &_image, int _height, QPointF &_pos, VerifyMarker::Marker _marker, QWidget *parent):
QGraphicsView(parent), marker(_marker), image(_image), pos(_pos) {
height = _height;
setScene(&scene);

Expand All @@ -91,68 +75,34 @@ VerifyView:: VerifyView(QImage &_image, int _height, QPointF &_pos, QWidget *par
img_item = scene.addPixmap(pix);
double scale = height/(double)pix.height();
setFixedSize(pix.width()*scale, height);
}

ReflectionVerify::ReflectionVerify(QImage&_image, int _height, QPointF &_pos, QWidget *parent):
VerifyView(_image, _height, _pos, parent) {
double scale = height/double(image.height());
int r = 8*scale;
reflection = new ReflectionPoint(this, QRectF(QPointF(-r, -r), QSize(2*r, 2*r)));
if(pos.isNull()) {
reflection->setPos(image.width()/2, image.height()/2);
reflection->setPen(QPen(Qt::red, 2));

} else {
reflection->setPos(pos);
reflection->setPen(QPen(Qt::green, 2));
}
scene.addItem(reflection);
}

AlignVerify::AlignVerify(QImage&_image, int _height, QPointF &_pos, QWidget *parent):
VerifyView(_image, _height, _pos, parent) {

align = new AlignPoint(this);
marker_item = new VerifyMarker(this, marker);
marker_item->radius = 8*scale;
if(pos.isNull()) {
align->setPos(image.width()/2, image.height()/2);
align->setPen(QPen(Qt::red, 0.2));
marker_item->setPos(image.width()/2, image.height()/2);
marker_item->active = false;

} else {
align->setPos(pos);
align->setPen(QPen(Qt::green, 0.2));
marker_item->setPos(pos);
marker_item->active = true;
}
scene.addItem(align);
scene.addItem(marker_item);
}


void ReflectionVerify::update() {
QPointF p = reflection->pos();
void VerifyView::update() {
QPointF p = marker_item->pos();
if(!img_item->boundingRect().contains(p)) {
reflection->setPos(image.width()/2, image.height()/2);
reflection->setPen(QPen(Qt::red, 2));
marker_item->setPos(image.width()/2, image.height()/2);
marker_item->active = false;
pos = QPointF(0, 0); //order is important: setPos triggers again.

} else {
pos = p;
reflection->setPen(QPen(Qt::green, 2));
marker_item->active = true;
}
}


void AlignVerify::update() {
QPointF p = align->pos();
if(!img_item->boundingRect().contains(p)) {
align->setPos(image.width()/2, image.height()/2);
align->setPen(QPen(Qt::red, 0.2));
pos = QPointF(0, 0); //order is important: setPos triggers again.

} else {
pos = p;
align->setPen(QPen(Qt::green, 0.2));
}
}


void VerifyView::resizeEvent(QResizeEvent *) {
fitInView(img_item->boundingRect()); //.sceneRect()); //img_item);
}
Expand Down
64 changes: 19 additions & 45 deletions relightlab/verifyview.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,44 @@
class Sphere;
class VerifyView;


class ReflectionPoint: public QGraphicsEllipseItem {
class VerifyMarker: public QGraphicsItem {
public:
ReflectionPoint(VerifyView *_view, QRectF rect, QGraphicsItem *parent = Q_NULLPTR);
void init();
virtual ~ReflectionPoint() {}
enum Marker { REFLECTION, ALIGN };
Marker marker;

protected:
VerifyView *view;
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
};
bool active = true;

class AlignPoint: public QGraphicsPathItem {
public:
AlignPoint(VerifyView *_view, QGraphicsItem *parent = Q_NULLPTR);
AlignPoint(VerifyView *_view, qreal x, qreal y, QGraphicsItem *parent = Q_NULLPTR);
void init();
virtual ~AlignPoint() {}
float radius = 0.0f;
QPointF center;

protected:
VerifyView *view;
VerifyView *view = nullptr;

VerifyMarker(VerifyView *_view, Marker _marker, QGraphicsItem *parent = Q_NULLPTR);
QPainterPath shape() const override;
QRectF boundingRect() const override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) override;
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
};


class VerifyView: public QGraphicsView {
Q_OBJECT
public:
VerifyView(QImage &image, int height, QPointF &pos, QWidget *parent = nullptr);
virtual void update() = 0;
VerifyMarker::Marker marker;

VerifyView(QImage &image, int height, QPointF &pos, VerifyMarker::Marker _marker, QWidget *parent = nullptr);
void update();

protected:
void resizeEvent(QResizeEvent *event);

protected:
QGraphicsPixmapItem *img_item;
QGraphicsScene scene;
VerifyMarker *marker_item = nullptr;
QGraphicsPixmapItem *img_item = nullptr;
QGraphicsScene scene = nullptr;
QImage &image;
QPointF &pos;
int id;
int height;
};

class ReflectionVerify: public VerifyView {
Q_OBJECT
public:
double lightSize = 10.0;

ReflectionVerify(QImage &image,int height, QPointF &pos, QWidget *parent = nullptr);
virtual void update();

private:
ReflectionPoint *reflection;

};

class AlignVerify: public VerifyView {
Q_OBJECT
public:
AlignVerify(QImage &image,int height, QPointF &pos, QWidget *parent = nullptr);
virtual void update();

private:
AlignPoint *align;
};

#endif // VERIFYVIEW_H
2 changes: 1 addition & 1 deletion src/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Align {
}

QRect rect;
std::vector<QPointF> offsets; //from the center of the rect
std::vector<QPointF> offsets; //from the center of the rect, [0,0] is not used.
std::vector<QImage> thumbs;

QJsonObject toJson();
Expand Down
18 changes: 18 additions & 0 deletions src/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ void Project::load(QString filename) {
_align->fromJson(align.toObject());
aligns.push_back(_align);
}
computeOffsets();
}
if(obj.contains("whites")) {
for(auto white: obj["whites"].toArray()) {
Expand Down Expand Up @@ -595,6 +596,23 @@ float lineSphereDistance(const Vector3f &origin, const Vector3f &direction, cons
float d = (-b + sqrt(det))/(2.0f*a);
return d;
}
void Project::computeOffsets() {
offsets.resize(images.size(), QPointF(0,0));
std::vector<float> weights(offsets.size(), 0);
for(Align *align: aligns) {
for(size_t i = 0; i < align->offsets.size(); i++) {
offsets[i] += align->offsets[i];
weights[i] += 1.0f;
}
}
for(size_t i = 0; i < offsets.size(); i++) {
if(weights[i] > 0)
offsets[i] /= weights[i];
}
}


/* This is obsolete, used only in legacy relight app */
void Project::computeDirections() {
if(spheres.size() == 0) {
QMessageBox::critical(nullptr, "Missing light directions.", "Light directions can be loaded from a .lp file or processing the spheres.");
Expand Down
5 changes: 4 additions & 1 deletion src/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Project {
std::vector<Align *> aligns;
std::vector<White *> whites;
QRect crop;
std::vector<QPointF> offsets;
float pixelSize = 0; //if computed from measures in mm

QString name;
Expand All @@ -61,7 +62,9 @@ class Project {
void save(QString filename);
void saveLP(QString filename, std::vector<Eigen::Vector3f> &directions);
void loadLP(QString filename);
void computeDirections();

void computeOffsets(); //from aligns
void computeDirections(); //obsolete
void computePixelSize();

Measure *newMeasure();
Expand Down

0 comments on commit 7635fc7

Please sign in to comment.