Skip to content

Commit 7635fc7

Browse files
committed
refactor align and bugfix
1 parent db29bfd commit 7635fc7

10 files changed

+85
-134
lines changed

relightlab/alignframe.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "alignframe.h"
2+
#include "alignpicking.h"
23
#include "imageview.h"
34
#include "flowlayout.h"
45
#include "relightapp.h"
@@ -25,6 +26,9 @@ AlignFrame::AlignFrame(QWidget *parent): QFrame(parent) {
2526
content->addWidget(aligns_frame);
2627
aligns = new QVBoxLayout(aligns_frame);
2728

29+
AlignPicking *picking = new AlignPicking();
30+
content->addWidget(picking);
31+
2832
//content->addStretch();
2933
connect(new_align, SIGNAL(clicked()), this, SLOT(newAlign()));
3034
}

relightlab/alignpicking.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ AlignPicking::AlignPicking(QWidget *parent): ImageViewer(parent) {
2222
rect = new AlignRect(this, 0, 0, 0, 0);
2323
rect->setPen(QPen(Qt::yellow, 2));
2424
rect->setBrush(Qt::transparent);
25+
26+
view->setCursor(Qt::CrossCursor);
2527
}
2628

2729

relightlab/imagelist.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void ImageList::init() {
1515
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
1616
item->setCheckState(img.skip? Qt::Unchecked : Qt::Checked);
1717
addItem(item);
18+
count++;
1819
}
1920

2021
connect(this, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(verifyItem(QListWidgetItem *)));

relightlab/spherepicking.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ SpherePicking::SpherePicking(QWidget *parent): ImageViewer(parent) {
5050
scene().addItem(axis[1]);
5151

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

5655
//QApplication::setOverrideCursor(Qt::CrossCursor);

relightlab/verifydialog.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ VerifyDialog::VerifyDialog(std::vector<QImage> &_thumbs, std::vector<QPointF> &_
3434
for(size_t i = 0; i < thumbs.size(); i++) {
3535
assert(!thumbs[i].isNull());
3636
if(marker == REFLECTION ) {
37-
ReflectionVerify *thumb = new ReflectionVerify(thumbs[i], 192, positions[i]);
37+
VerifyView *thumb = new VerifyView(thumbs[i], 192, positions[i], VerifyMarker::REFLECTION);
3838
flowlayout->addWidget(thumb);
3939
} else {
40-
AlignVerify *thumb = new AlignVerify(thumbs[i], 192, positions[i]);
40+
VerifyView *thumb = new VerifyView(thumbs[i], 192, positions[i], VerifyMarker::ALIGN);
4141
flowlayout->addWidget(thumb);
4242
}
4343
}

relightlab/verifyview.cpp

+34-84
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,40 @@
1010
#include <QDebug>
1111
#include <assert.h>
1212

13-
ReflectionPoint::ReflectionPoint(VerifyView *_view, QRectF rect, QGraphicsItem *parent):
14-
QGraphicsEllipseItem(rect, parent), view(_view) {
15-
init();
16-
}
1713

18-
void ReflectionPoint::init() {
14+
VerifyMarker::VerifyMarker(VerifyView *_view, Marker _marker, QGraphicsItem *parent):
15+
QGraphicsItem(parent), view(_view), marker(_marker) {
1916
setCursor(Qt::CrossCursor);
2017
setFlag(QGraphicsItem::ItemIsMovable);
2118
setFlag(QGraphicsItem::ItemIsSelectable);
2219
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
2320
}
2421

25-
26-
AlignPoint::AlignPoint(VerifyView *_view, QGraphicsItem *parent):
27-
QGraphicsPathItem(parent), view(_view) {
22+
QPainterPath VerifyMarker::shape() const {
2823
QPainterPath path;
29-
path.moveTo(-2, 0);
30-
path.lineTo(2, 0);
31-
path.moveTo(0, -2);
32-
path.lineTo(0, 2);
33-
setPath(path);
34-
init();
24+
if(marker == ALIGN)
25+
path.addRect(-3, -3, 6, 6);
26+
else
27+
path.addRect(-radius-2, -radius-2, 2*(radius+2), 2*(radius+2));
28+
return path;
3529
}
3630

37-
void AlignPoint::init() {
38-
setCursor(Qt::CrossCursor);
39-
setFlag(QGraphicsItem::ItemIsMovable);
40-
setFlag(QGraphicsItem::ItemIsSelectable);
41-
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
31+
QRectF VerifyMarker::boundingRect() const {
32+
return QRectF(-3, -3, 6, 6);
4233
}
4334

44-
QVariant ReflectionPoint::itemChange(GraphicsItemChange change, const QVariant &value) {
45-
if ((change == ItemPositionChange && scene()) ) {
46-
47-
QPointF newPos = value.toPointF();
48-
QRectF rect = scene()->sceneRect();
49-
if (!rect.contains(newPos)) {
50-
// Keep the item inside the scene rect.
51-
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
52-
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
53-
}
54-
return newPos;
55-
}
56-
57-
if(change == ItemScenePositionHasChanged) {
58-
view->update();
35+
void VerifyMarker::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) {
36+
QPen pen(active ? Qt::green : Qt::red, marker == ALIGN? 0.2 : 2);
37+
painter->setPen(pen);
38+
if(marker == ALIGN) {
39+
painter->drawLine(QPointF(-2, 0), QPointF(2, 0));
40+
painter->drawLine(QPointF(0, -2), QPointF(0, 2));
41+
} else {
42+
painter->drawEllipse(QPointF(0, 0), radius, radius);
5943
}
60-
return QGraphicsItem::itemChange(change, value);
6144
}
6245

63-
QVariant AlignPoint::itemChange(GraphicsItemChange change, const QVariant &value) {
46+
QVariant VerifyMarker::itemChange(GraphicsItemChange change, const QVariant &value) {
6447
if ((change == ItemPositionChange && scene()) ) {
6548

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

82-
VerifyView:: VerifyView(QImage &_image, int _height, QPointF &_pos, QWidget *parent):
83-
QGraphicsView(parent), image(_image), pos(_pos) {
65+
66+
VerifyView:: VerifyView(QImage &_image, int _height, QPointF &_pos, VerifyMarker::Marker _marker, QWidget *parent):
67+
QGraphicsView(parent), marker(_marker), image(_image), pos(_pos) {
8468
height = _height;
8569
setScene(&scene);
8670

@@ -91,68 +75,34 @@ VerifyView:: VerifyView(QImage &_image, int _height, QPointF &_pos, QWidget *par
9175
img_item = scene.addPixmap(pix);
9276
double scale = height/(double)pix.height();
9377
setFixedSize(pix.width()*scale, height);
94-
}
95-
96-
ReflectionVerify::ReflectionVerify(QImage&_image, int _height, QPointF &_pos, QWidget *parent):
97-
VerifyView(_image, _height, _pos, parent) {
98-
double scale = height/double(image.height());
99-
int r = 8*scale;
100-
reflection = new ReflectionPoint(this, QRectF(QPointF(-r, -r), QSize(2*r, 2*r)));
101-
if(pos.isNull()) {
102-
reflection->setPos(image.width()/2, image.height()/2);
103-
reflection->setPen(QPen(Qt::red, 2));
104-
105-
} else {
106-
reflection->setPos(pos);
107-
reflection->setPen(QPen(Qt::green, 2));
108-
}
109-
scene.addItem(reflection);
110-
}
111-
112-
AlignVerify::AlignVerify(QImage&_image, int _height, QPointF &_pos, QWidget *parent):
113-
VerifyView(_image, _height, _pos, parent) {
11478

115-
align = new AlignPoint(this);
79+
marker_item = new VerifyMarker(this, marker);
80+
marker_item->radius = 8*scale;
11681
if(pos.isNull()) {
117-
align->setPos(image.width()/2, image.height()/2);
118-
align->setPen(QPen(Qt::red, 0.2));
82+
marker_item->setPos(image.width()/2, image.height()/2);
83+
marker_item->active = false;
11984

12085
} else {
121-
align->setPos(pos);
122-
align->setPen(QPen(Qt::green, 0.2));
86+
marker_item->setPos(pos);
87+
marker_item->active = true;
12388
}
124-
scene.addItem(align);
89+
scene.addItem(marker_item);
12590
}
12691

12792

128-
void ReflectionVerify::update() {
129-
QPointF p = reflection->pos();
93+
void VerifyView::update() {
94+
QPointF p = marker_item->pos();
13095
if(!img_item->boundingRect().contains(p)) {
131-
reflection->setPos(image.width()/2, image.height()/2);
132-
reflection->setPen(QPen(Qt::red, 2));
96+
marker_item->setPos(image.width()/2, image.height()/2);
97+
marker_item->active = false;
13398
pos = QPointF(0, 0); //order is important: setPos triggers again.
13499

135100
} else {
136101
pos = p;
137-
reflection->setPen(QPen(Qt::green, 2));
102+
marker_item->active = true;
138103
}
139104
}
140105

141-
142-
void AlignVerify::update() {
143-
QPointF p = align->pos();
144-
if(!img_item->boundingRect().contains(p)) {
145-
align->setPos(image.width()/2, image.height()/2);
146-
align->setPen(QPen(Qt::red, 0.2));
147-
pos = QPointF(0, 0); //order is important: setPos triggers again.
148-
149-
} else {
150-
pos = p;
151-
align->setPen(QPen(Qt::green, 0.2));
152-
}
153-
}
154-
155-
156106
void VerifyView::resizeEvent(QResizeEvent *) {
157107
fitInView(img_item->boundingRect()); //.sceneRect()); //img_item);
158108
}

relightlab/verifyview.h

+19-45
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,44 @@
88
class Sphere;
99
class VerifyView;
1010

11-
12-
class ReflectionPoint: public QGraphicsEllipseItem {
11+
class VerifyMarker: public QGraphicsItem {
1312
public:
14-
ReflectionPoint(VerifyView *_view, QRectF rect, QGraphicsItem *parent = Q_NULLPTR);
15-
void init();
16-
virtual ~ReflectionPoint() {}
13+
enum Marker { REFLECTION, ALIGN };
14+
Marker marker;
1715

18-
protected:
19-
VerifyView *view;
20-
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
21-
};
16+
bool active = true;
2217

23-
class AlignPoint: public QGraphicsPathItem {
24-
public:
25-
AlignPoint(VerifyView *_view, QGraphicsItem *parent = Q_NULLPTR);
26-
AlignPoint(VerifyView *_view, qreal x, qreal y, QGraphicsItem *parent = Q_NULLPTR);
27-
void init();
28-
virtual ~AlignPoint() {}
18+
float radius = 0.0f;
19+
QPointF center;
2920

30-
protected:
31-
VerifyView *view;
21+
VerifyView *view = nullptr;
22+
23+
VerifyMarker(VerifyView *_view, Marker _marker, QGraphicsItem *parent = Q_NULLPTR);
24+
QPainterPath shape() const override;
25+
QRectF boundingRect() const override;
26+
void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) override;
3227
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
3328
};
3429

35-
3630
class VerifyView: public QGraphicsView {
3731
Q_OBJECT
3832
public:
39-
VerifyView(QImage &image, int height, QPointF &pos, QWidget *parent = nullptr);
40-
virtual void update() = 0;
33+
VerifyMarker::Marker marker;
34+
35+
VerifyView(QImage &image, int height, QPointF &pos, VerifyMarker::Marker _marker, QWidget *parent = nullptr);
36+
void update();
4137

4238
protected:
4339
void resizeEvent(QResizeEvent *event);
4440

4541
protected:
46-
QGraphicsPixmapItem *img_item;
47-
QGraphicsScene scene;
42+
VerifyMarker *marker_item = nullptr;
43+
QGraphicsPixmapItem *img_item = nullptr;
44+
QGraphicsScene scene = nullptr;
4845
QImage &image;
4946
QPointF &pos;
5047
int id;
5148
int height;
5249
};
5350

54-
class ReflectionVerify: public VerifyView {
55-
Q_OBJECT
56-
public:
57-
double lightSize = 10.0;
58-
59-
ReflectionVerify(QImage &image,int height, QPointF &pos, QWidget *parent = nullptr);
60-
virtual void update();
61-
62-
private:
63-
ReflectionPoint *reflection;
64-
65-
};
66-
67-
class AlignVerify: public VerifyView {
68-
Q_OBJECT
69-
public:
70-
AlignVerify(QImage &image,int height, QPointF &pos, QWidget *parent = nullptr);
71-
virtual void update();
72-
73-
private:
74-
AlignPoint *align;
75-
};
76-
7751
#endif // VERIFYVIEW_H

src/align.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Align {
1616
}
1717

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

2222
QJsonObject toJson();

src/project.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ void Project::load(QString filename) {
347347
_align->fromJson(align.toObject());
348348
aligns.push_back(_align);
349349
}
350+
computeOffsets();
350351
}
351352
if(obj.contains("whites")) {
352353
for(auto white: obj["whites"].toArray()) {
@@ -595,6 +596,23 @@ float lineSphereDistance(const Vector3f &origin, const Vector3f &direction, cons
595596
float d = (-b + sqrt(det))/(2.0f*a);
596597
return d;
597598
}
599+
void Project::computeOffsets() {
600+
offsets.resize(images.size(), QPointF(0,0));
601+
std::vector<float> weights(offsets.size(), 0);
602+
for(Align *align: aligns) {
603+
for(size_t i = 0; i < align->offsets.size(); i++) {
604+
offsets[i] += align->offsets[i];
605+
weights[i] += 1.0f;
606+
}
607+
}
608+
for(size_t i = 0; i < offsets.size(); i++) {
609+
if(weights[i] > 0)
610+
offsets[i] /= weights[i];
611+
}
612+
}
613+
614+
615+
/* This is obsolete, used only in legacy relight app */
598616
void Project::computeDirections() {
599617
if(spheres.size() == 0) {
600618
QMessageBox::critical(nullptr, "Missing light directions.", "Light directions can be loaded from a .lp file or processing the spheres.");

src/project.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Project {
3737
std::vector<Align *> aligns;
3838
std::vector<White *> whites;
3939
QRect crop;
40+
std::vector<QPointF> offsets;
4041
float pixelSize = 0; //if computed from measures in mm
4142

4243
QString name;
@@ -61,7 +62,9 @@ class Project {
6162
void save(QString filename);
6263
void saveLP(QString filename, std::vector<Eigen::Vector3f> &directions);
6364
void loadLP(QString filename);
64-
void computeDirections();
65+
66+
void computeOffsets(); //from aligns
67+
void computeDirections(); //obsolete
6568
void computePixelSize();
6669

6770
Measure *newMeasure();

0 commit comments

Comments
 (0)