Skip to content

Commit 89a7472

Browse files
author
JJPPeters
committed
MAJOR update
Well I fucked the commits by merging the gh-pages branch like an utter tit. So here are all the commits I made into one big, unreadable mess. Added: Axes widget that rotates with basis angle (uses the axes resource and svg file) Ability to export colour bar as an RGB image. Size is hardcoded. Labels on to distorition images. Fixed: Memory leak when updating images due to stupid pointers. MAJOR bug where eigen matrices where not initialised to zero (this only happened for smaller images?) Small tweaks to usability, right click options are grayed out until they are valid. Finding the g vector has a fail case. Errors when opening a small image (widht or height < 3) Tiff images now open the right way up. probably a ton more...
1 parent 4567da5 commit 89a7472

12 files changed

+476
-17
lines changed

Plotting/colorbarplot.h

+48
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class ColorBarPlot : public QCustomPlot
1414
public:
1515
ColorBarPlot(QWidget *parent = 0) : QCustomPlot(parent)
1616
{
17+
setContextMenuPolicy(Qt::CustomContextMenu);
18+
setFocusPolicy(Qt::StrongFocus);
19+
1720
ColorBar = new QCPColorScale(this);
1821

1922
// remove the crap we don't want
@@ -24,6 +27,8 @@ class ColorBarPlot : public QCustomPlot
2427
//ColorBar->axis()->setLabel("Strain");
2528

2629
CreateColorMaps();
30+
31+
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
2732
}
2833

2934
void SetColorMap(const QString &Map)
@@ -97,6 +102,49 @@ class ColorBarPlot : public QCustomPlot
97102
JetLike_Map.setColorStopAt(1.0, QColor(255, 0, 0));
98103
}
99104

105+
private slots:
106+
void contextMenuRequest(QPoint pos)
107+
{
108+
QMenu* menu = new QMenu(this);
109+
110+
menu->addAction("Export RGB", this, SLOT(ExportImage()));
111+
112+
menu->popup(mapToGlobal(pos));
113+
}
114+
115+
public slots:
116+
void ExportImage()
117+
{
118+
QSettings settings;
119+
120+
// get path
121+
QString filepath = QFileDialog::getSaveFileName(this, "Save image", settings.value("dialog/currentSavePath").toString(), "TIFF (*.tif)");
122+
123+
if (filepath.isEmpty())
124+
return;
125+
126+
QFileInfo temp_file(filepath);
127+
settings.setValue("dialog/currentSavePath", temp_file.path());
128+
129+
ExportImage(filepath);
130+
}
131+
132+
void ExportImage(QString directory, QString filename)
133+
{
134+
QString filepath = QDir(directory).filePath(filename);
135+
filepath += ".tif";
136+
137+
ExportImage(filepath);
138+
}
139+
140+
void ExportImage(QString filepath)
141+
{
142+
int w = width();
143+
144+
std::string format = "TIFF";
145+
saveRastered(filepath, w, 512, 1.0, format.c_str());
146+
}
147+
100148
};
101149

102150
#endif // COLORBARPLOT_H

Plotting/imageplot.h

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#define EIGEN_DEFAULT_TO_ROW_MAJOR
66
#endif
77

8+
#ifndef EIGEN_INITIALIZE_MATRICES_BY_ZERO
9+
#define EIGEN_INITIALIZE_MATRICES_BY_ZERO
10+
#endif
11+
812
#include <complex>
913
#include <memory>
1014
#include <cmath>
@@ -35,6 +39,7 @@ class ImagePlot : public QCustomPlot
3539
{
3640
setInteractions(QCP::iRangeZoom);
3741
setContextMenuPolicy(Qt::CustomContextMenu);
42+
setFocusPolicy(Qt::StrongFocus);
3843

3944
// this will remove all the crud but keep the grid lines
4045
xAxis->setSubTickPen(Qt::NoPen);
@@ -85,6 +90,8 @@ class ImagePlot : public QCustomPlot
8590
if (sx*sy != (int)image.size())
8691
throw sizeError;
8792

93+
clearImage();
94+
8895
AspectRatio = (double)sx/(double)sy;
8996

9097
rescaleAxes();
@@ -121,8 +128,13 @@ class ImagePlot : public QCustomPlot
121128
if (sx*sy != (int)image.size())
122129
throw sizeError;
123130

131+
clearImage();
132+
124133
AspectRatio = (double)sx/(double)sy;
125134

135+
rescaleAxes();
136+
setImageRatio();
137+
126138
ImageObject = new QCPColorMap(xAxis, yAxis);
127139
addPlottable(ImageObject);
128140
ImageObject->setGradient(QCPColorGradient::gpGrayscale); // default
@@ -340,9 +352,12 @@ private slots:
340352

341353
QMenu* saveMenu = new QMenu("Export...", this);
342354

343-
saveMenu->addAction("RGB image", this, SLOT(ExportImage()));
344-
saveMenu->addAction("Data image", this, SLOT(ExportData()));
345-
saveMenu->addAction("Binary", this, SLOT(ExportBinary()));
355+
QAction* expIm = saveMenu->addAction("RGB image", this, SLOT(ExportImage()));
356+
expIm->setEnabled(haveImage);
357+
QAction* expDat = saveMenu->addAction("Data image", this, SLOT(ExportData()));
358+
expDat->setEnabled(haveImage);
359+
QAction* expBin = saveMenu->addAction("Binary", this, SLOT(ExportBinary()));
360+
expBin->setEnabled(haveImage);
346361

347362
menu->addMenu(saveMenu);
348363

Strain/gpa.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ int GPA::getGVectors()
9898
averages.push_back(av);
9999
}
100100

101+
if(averages.size() < 1)
102+
return std::min(xs, ys) / 2;
103+
101104
// normalise
102105
auto minIterator = std::min_element(averages.begin(), averages.end());
103106
double min = *minIterator;

Strain/gpa.h

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const double PI = 3.14159265358979323846;
1010
#define EIGEN_DEFAULT_TO_ROW_MAJOR
1111
#endif
1212

13+
#ifndef EIGEN_INITIALIZE_MATRICES_BY_ZERO
14+
#define EIGEN_INITIALIZE_MATRICES_BY_ZERO
15+
#endif
16+
1317
#include <memory>
1418
#include <complex>
1519
#include <algorithm>

Strain/phase.h

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#define EIGEN_DEFAULT_TO_ROW_MAJOR
66
#endif
77

8+
#ifndef EIGEN_INITIALIZE_MATRICES_BY_ZERO
9+
#define EIGEN_INITIALIZE_MATRICES_BY_ZERO
10+
#endif
11+
812
#ifndef PI_H
913
#define PI_H
1014
const double PI = 3.14159265358979323846;

Strainpp.pro

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
RC_FILE = Strainpp.rc
88
CONFIG += c++14
99

10-
QT += core gui
10+
QT += core gui svg
1111
QMAKE_CXXFLAGS_RELEASE += -O3
1212
QMAKE_CXXFLAGS += -fopenmp
1313
LIBS += -fopenmp
@@ -64,3 +64,6 @@ DEPENDPATH += $$PWD/../../../Programming/Cpp/msys64/usr/local/include
6464

6565
win32:!win32-g++: PRE_TARGETDEPS += $$PWD/../../../Programming/Cpp/msys64/usr/local/lib/tiff.lib
6666
else:win32-g++: PRE_TARGETDEPS += $$PWD/../../../Programming/Cpp/msys64/usr/local/lib/libtiff.a
67+
68+
RESOURCES += \
69+
axesresource.qrc

Utils/utils.h

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#define EIGEN_DEFAULT_TO_ROW_MAJOR
66
#endif
77

8+
#ifndef EIGEN_INITIALIZE_MATRICES_BY_ZERO
9+
#define EIGEN_INITIALIZE_MATRICES_BY_ZERO
10+
#endif
11+
812
#include <memory>
913
#include <cmath>
1014
#include "fftw3.h"

axes.svg

+26
Loading

axesresource.qrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/Images">
3+
<file>axes.svg</file>
4+
</qresource>
5+
</RCC>

mainwindow.cpp

+120
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#include "dmreader.h"
55

6+
#include <QPainter>
7+
#include <QtSvg/QSvgRenderer>
8+
#include <QPoint>
9+
610
MainWindow::MainWindow(QWidget *parent) :
711
QMainWindow(parent),
812
ui(new Ui::MainWindow)
@@ -37,6 +41,7 @@ MainWindow::MainWindow(QWidget *parent) :
3741

3842
statusBar()->addWidget(statusLabel);
3943

44+
on_angleSpin_editingFinished(); // this is just to update the axes image...
4045

4146
// connect al the slots to update the strain images
4247
connect(ui->colorBar, SIGNAL(limitsChanged(double)), ui->exxPlot, SLOT(SetColorLimits(double)));
@@ -111,6 +116,12 @@ void MainWindow::openDM(std::string filename)
111116
int ny = dmFile.getY();
112117
int nz = dmFile.getZ();
113118

119+
if (nx < 3 || ny < 3)
120+
{
121+
QMessageBox::information(this, tr("File open"), tr("Image too small."), QMessageBox::Ok);
122+
return;
123+
}
124+
114125
std::vector<double> image;
115126
if ( nz > 1)
116127
image = dmFile.getImage(0, nx*ny);
@@ -562,6 +573,7 @@ void MainWindow::doRefinement(double top, double left, double bottom, double rig
562573
}
563574
}
564575

576+
// probably poorly named since it does so much more
565577
void MainWindow::getStrains()
566578
{
567579
updateStatusBar("GPA completed!");
@@ -750,6 +762,9 @@ void MainWindow::ExportAll(int choice)
750762
ui->exxPlot->ExportSelector(fileDir, "Dilitation", choice);
751763
}
752764

765+
if (choice == 0)
766+
ui->colorBar->ExportImage(fileDir, "ColourBar");
767+
753768
// need to loop through and show images before exporting them from same plot
754769
for(int i = 0; i < ui->leftCombo->count(); ++i)
755770
{
@@ -806,6 +821,9 @@ void MainWindow::ExportStrains(int choice)
806821
{
807822
ui->exxPlot->ExportSelector(fileDir, "Dilitation", choice);
808823
}
824+
825+
if (choice == 0)
826+
ui->colorBar->ExportImage(fileDir, "ColourBar");
809827
}
810828

811829
void MainWindow::DisconnectAll()
@@ -830,6 +848,71 @@ void MainWindow::ClearImages()
830848

831849
void MainWindow::on_angleSpin_editingFinished()
832850
{
851+
// OK....
852+
// This first part is very messy. But bear with me.
853+
// The axes is actually an SVG file kept int the resource file "axesresource.qrc"
854+
855+
// simple, get the angle!
856+
double angle = ui->angleSpin->value();
857+
858+
if (lastAngle == angle)
859+
return;
860+
861+
lastAngle = angle;
862+
863+
// These are to account for the size of the text
864+
// there might be a better way using rects and setting the alignment to centre
865+
// but I can't be botthered to work it out
866+
QPoint ysz(-3, 4.5);
867+
QPoint xsz(-3, 3);
868+
// This is the size of our area to draw in
869+
QSize size(100, 100);
870+
// This is the pixmap that will eventually be shown, we start with it transparent
871+
QPixmap newmap(size);
872+
newmap.fill(Qt::transparent);
873+
// this is the midpoint of our image (so we can rotate around it)
874+
QPoint mid(size.height()/2, size.width()/2);
875+
// this is the position of our labels
876+
QPoint xp(65, 65);
877+
QPoint yp(35, 35);
878+
879+
// here we rotate these positions about the mid point
880+
xp = xp - mid;
881+
yp = yp - mid;
882+
QTransform temp;
883+
QTransform tform = temp.rotate(-angle);
884+
885+
xp = tform.map(xp);
886+
yp = tform.map(yp);
887+
xp = xp + mid;
888+
yp = yp + mid;
889+
890+
// set the font and colours
891+
QFont tFont("Arial", 12, QFont::Normal);
892+
QPen xPen(QColor("#E30513"));
893+
QPen yPen(QColor("#008D36"));
894+
895+
// here we load the svg from the resource file
896+
QSvgRenderer renderer(QString(":/Images/axes.svg"));
897+
QPainter* p = new QPainter(&newmap);
898+
//draw the texts (we have rotated the POSITION before)
899+
p->setFont(tFont);
900+
p->setPen(xPen);
901+
p->drawText(xp + xsz, "x");
902+
p->setPen(yPen);
903+
p->drawText(yp + ysz, "y");
904+
// now rotate the svg about hte mid point and draw it
905+
p->translate(size.height()/2,size.height()/2);
906+
p->rotate(-angle);
907+
p->translate(-size.height()/2,-size.height()/2);
908+
renderer.render(p);
909+
// done, phew...
910+
p->end();
911+
912+
// finally set it
913+
ui->lblAxes->setPixmap(newmap);
914+
915+
// now do the calculation if we need to
833916
if(!haveStrains)
834917
return;
835918

@@ -838,6 +921,43 @@ void MainWindow::on_angleSpin_editingFinished()
838921

839922
void MainWindow::on_resultModeBox_currentIndexChanged(const QString &mode)
840923
{
924+
// set the labels on the image
925+
if (mode == "Distortion")
926+
{
927+
ui->exxLabel->setText("e<sub>xx</sub>");
928+
ui->exyLabel->setText("e<sub>xy</sub>");
929+
ui->eyxLabel->setText("e<sub>yx</sub>");
930+
ui->eyyLabel->setText("e<sub>yy</sub>");
931+
ui->exyLabel->setVisible(true);
932+
ui->eyxLabel->setVisible(true);
933+
ui->eyyLabel->setVisible(true);
934+
}
935+
else if(mode == "Strain")
936+
{
937+
ui->exxLabel->setText("ε<sub>xx</sub>");
938+
ui->exyLabel->setText("ε<sub>xy</sub>");
939+
ui->eyxLabel->setText("ε<sub>yx</sub>");
940+
ui->eyyLabel->setText("ε<sub>yy</sub>");
941+
ui->exyLabel->setVisible(true);
942+
ui->eyxLabel->setVisible(true);
943+
ui->eyyLabel->setVisible(true);
944+
}
945+
else if(mode == "Rotation")
946+
{
947+
ui->exxLabel->setText("ω<sub>xx</sub>");
948+
ui->eyyLabel->setText("ω<sub>yy</sub>");
949+
ui->exyLabel->setVisible(false);
950+
ui->eyxLabel->setVisible(false);
951+
ui->eyyLabel->setVisible(true);
952+
}
953+
else if(mode == "Dilitation")
954+
{
955+
ui->exxLabel->setText("Δ");
956+
ui->exyLabel->setVisible(false);
957+
ui->eyxLabel->setVisible(false);
958+
ui->eyyLabel->setVisible(false);
959+
}
960+
841961
if(!haveStrains)
842962
return;
843963

0 commit comments

Comments
 (0)