File: ProjectionsSaver.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (174 lines) | stat: -rw-r--r-- 5,716 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/IO/ProjectionsSaver.cpp
//! @brief     Implements class ProjectionsSaver.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/IO/ProjectionsSaver.h"
#include "Base/Axis/Scale.h"
#include "Base/Py/PyFmt.h"
#include "Device/Data/Datafield.h"
#include "GUI/Model/Data/Data2DItem.h"
#include "GUI/Model/Mask/MasksSet.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/View/Widget/AppConfig.h"
#include "GUI/View/Widget/FileDialog.h"
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QVector>
#include <QWidget>
#include <memory>

namespace {

const int bin_centers_colwidth = 12;
const int bin_values_colwidth = 20;

QString to_scientific_str(double value)
{
    auto str = Py::Fmt::printScientificDouble(value);
    return QString("%1").arg(QString::fromStdString(str), -bin_values_colwidth);
}

QString to_double_str(double value)
{
    auto str = Py::Fmt::printDouble(value);
    return QString("%1").arg(QString::fromStdString(str), -bin_centers_colwidth);
}

//! Returns horizontal or vertical cuts, sorted by position.
QVector<const LineItem*> projectionItems(Qt::Orientation ori, const Data2DItem* data_item)
{
    const auto* pset = data_item->prjns();
    if (!pset)
        return {};
    QVector<const LineItem*> result;
    for (const MaskItem* m : *pset)
        if (const auto* t = dynamic_cast<const LineItem*>(m))
            if (t->orientation() == ori)
                result.push_back(t);
    std::sort(result.begin(), result.end(), [](const LineItem* t1, const LineItem* t2) {
        return t1->pos().dVal() < t2->pos().dVal();
    });
    return result;
}

struct Projection {
    double axis_value; //!< value on axis where projection has been made
    QVector<double> bin_values;
};

struct ProjectionsData {
    bool is_horizontal;
    QVector<double> bin_centers;
    QVector<Projection> projections;
};

//! Returns projections header, e.g.
//! "# x         z at y=6.0194            z at y=33.5922"
QString projectionFileHeader(ProjectionsData& projs)
{
    const char label1 = projs.is_horizontal ? 'x' : 'y';
    const char label2 = projs.is_horizontal ? 'y' : 'x';

    QString result = QString("# %1").arg(label1, -bin_centers_colwidth + 1);

    for (auto& data : projs.projections)
        result.append(QString("z @ %1=%2")
                          .arg(label2)
                          .arg(data.axis_value, -(bin_values_colwidth - 6), 'f', 4));
    result.append("\n");

    return result;
}

//! Returns projections data for all projections of given type (horizontal, vertical).
ProjectionsData projectionsData(Qt::Orientation projectionsType, const Data2DItem* data_item)
{
    ProjectionsData result;
    result.is_horizontal = (projectionsType == Qt::Horizontal);

    for (const auto* item : projectionItems(projectionsType, data_item)) {
        std::unique_ptr<Datafield> field;
        Projection data;

        if (const auto* horLine = dynamic_cast<const HorizontalLineItem*>(item)) {
            data.axis_value = horLine->pos().dVal();
            field.reset(data_item->c_field()->xProjection(data.axis_value));
        } else if (const auto* verLine = dynamic_cast<const VerticalLineItem*>(item)) {
            data.axis_value = verLine->pos().dVal();
            field.reset(data_item->c_field()->yProjection(data.axis_value));
        } else
            ASSERT_NEVER;

        auto values = field->flatVector();
        auto centers = field->axis(0).binCenters();
        data.bin_values = QVector<double>(values.begin(), values.end());
        if (result.bin_centers.isEmpty())
            result.bin_centers = QVector<double>(centers.begin(), centers.end());
        result.projections.push_back(data);
    }
    return result;
}

//! Generates multi-line string with projections data of given type (horizontal, vertical).
QString projectionsToString(Qt::Orientation projectionsType, const Data2DItem* data_item)
{
    QString result;
    QTextStream out(&result);

    auto projData = projectionsData(projectionsType, data_item);

    if (projData.projections.isEmpty())
        return result;

    out << projectionFileHeader(projData);

    auto bin_centers = projData.bin_centers;

    for (int i_point = 0; i_point < bin_centers.size(); ++i_point) {
        out << to_double_str(bin_centers[i_point]) << " ";
        for (auto& data : projData.projections)
            out << to_scientific_str(data.bin_values[i_point]);
        out << "\n";
    }
    return result;
}

} // namespace


void IO::saveProjections(const Data2DItem* data_item)
{
    if (!data_item)
        return;

    QString fname = GUI::FileDialog::w1_1f("Save projections data", gApp->artifact_export_dir, "");
    if (fname.isEmpty())
        return;

    QFile file(fname);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        throw std::runtime_error("Cannot create file for saving projections");

    QTextStream out(&file);

    out << "# Projections along x-axis (horizontal projections) \n";
    out << projectionsToString(Qt::Horizontal, data_item);
    out << "\n";

    out << "# Projections along y-axis (vertical projections) \n";
    out << projectionsToString(Qt::Vertical, data_item);
    out << "\n";

    file.close();
}