-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputview.cpp
199 lines (173 loc) · 7.93 KB
/
outputview.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright 2019 Balázs Ludmány
This file is part of contours-viewer.
contours-viewer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
contours-viewer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with contours-viewer. If not, see <https://www.gnu.org/licenses/>.
*/
#include <wx/filename.h>
#include <wx/grid.h>
#include <boost/range/adaptor/indexed.hpp>
#include <gdfontl.h>
#include "outputview.hpp"
#include "model/ratios.hpp"
/*!
* Returns true if a is shorter than b.
*/
constexpr bool is_shorter(const char *a, const char *b) {
if (*a == '\0')
if (*b == '\0')
return false; // same length
else
return true; // a is shorter than b
else if (*b == '\0')
return false; // a is longer than b
else
return is_shorter(a + 1, b + 1);
}
constexpr std::size_t string_length(const char *str, const std::size_t length) {
return (*str == '\0') ? length : string_length(str + 1, length + 1);
}
static constexpr const char *labels[] = {"Name",
"Number of lines",
"Minimum area",
"X offset",
"Y offset",
"Z offset",
"S",
"U",
"Surface area",
"Volume",
"a",
"b",
"c",
"Largest circumference",
"Largest area",
"Xmin",
"Xmax",
"Ymin",
"Ymax",
"Zmin",
"Zmax",
"Reeb code",
"Morse code"};
static constexpr std::size_t label_count =
sizeof(labels) / sizeof(const char *);
static constexpr std::size_t longest_label = string_length(
*std::max_element(std::begin(labels), std::end(labels), is_shorter), 0);
void OutputView::Initialize(const std::string &fileName) {
m_table.resize(label_count + ratio_label_count);
CreateGrid(label_count + ratio_label_count, 1);
EnableEditing(false);
SetRowLabelAlignment(wxALIGN_LEFT, wxALIGN_CENTRE);
HideColLabels();
int row = 0;
for (const auto &label : labels)
SetRowLabelValue(row++, label);
for (const auto &label : ratio_labels)
SetRowLabelValue(row++, label);
SetRowLabelSize(wxGRID_AUTOSIZE);
DisableDragGridSize();
row = 0;
SetCellRenderer(row++, 0, new wxGridCellStringRenderer()); // name
SetCellRenderer(row++, 0, new wxGridCellNumberRenderer()); // number of lines
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // minimum area
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // x offset
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // y offset
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // z offset
SetCellRenderer(row++, 0, new wxGridCellNumberRenderer()); // S
SetCellRenderer(row++, 0, new wxGridCellNumberRenderer()); // U
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // surface area
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // volume
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // a
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // b
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // c
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // largest circ
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // largest area
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // Xmin
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // Xmax
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // Ymin
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // Ymax
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // Zmin
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4)); // Zmax
SetCellRenderer(row++, 0, new wxGridCellStringRenderer()); // Reeb
SetCellRenderer(row++, 0, new wxGridCellStringRenderer()); // Morse
for (; row < label_count + ratio_label_count;)
SetCellRenderer(row++, 0, new wxGridCellFloatRenderer(-1, 4));
wxFileName name(fileName);
m_table[0] = name.GetName();
SetCellValue(0, 0, name.GetName());
AutoSize();
}
void OutputView::UpdateParameters(const Vector &offset, int level_count,
double area_ratio) {
wxCriticalSectionLocker lock(m_critical_section);
int row = 1;
m_table[row++] = wxString::Format("%d", level_count);
m_table[row++] = wxString::Format("%f", area_ratio);
m_table[row++] = wxString::Format("%f", offset.x());
m_table[row++] = wxString::Format("%f", offset.y());
m_table[row++] = wxString::Format("%f", offset.z());
}
void OutputView::UpdateMeshData(const std::array<double, 13> &properties) {
wxCriticalSectionLocker lock(m_critical_section);
for (int i = 0; i < properties.size(); ++i)
m_table[8 + i] = wxString::Format("%f", properties[i]);
}
void OutputView::UpdateSU(int S, int U) {
wxCriticalSectionLocker lock(m_critical_section);
int row = 6;
m_table[row++] = wxString::Format("%d", S);
m_table[row++] = wxString::Format("%d", U);
}
void OutputView::UpdateReeb(const std::string &reeb) {
wxCriticalSectionLocker lock(m_critical_section);
m_table[21] = reeb;
}
void OutputView::UpdateMorse(const std::string &morse) {
wxCriticalSectionLocker lock(m_critical_section);
m_table[22] = morse;
}
void OutputView::UpdateRatios(const std::array<double, 6> &properties) {
wxCriticalSectionLocker lock(m_critical_section);
for (int i = 0; i < properties.size(); ++i)
m_table[23 + i] = wxString::Format("%f", properties[i]);
}
void OutputView::Swap() {
using boost::adaptors::indexed;
wxCriticalSectionLocker lock(m_critical_section);
for (const auto &row : m_table | indexed())
SetCellValue(row.index(), 0, row.value());
AutoSize();
}
#ifdef GD_FOUND
GD::Image OutputView::Screenshot() const {
const int font_width = 8;
const int font_height = 16;
std::size_t longest_value = 0;
for (int row = 0; row < GetNumberRows(); ++row)
longest_value = std::max(GetCellValue(row, 0).Len(), longest_value);
auto font = gdFontGetLarge();
auto color = GD::TrueColor(0, 0, 0).Int();
GD::Image image((longest_label + longest_value) * font_width,
GetNumberRows() * font_height, true);
image.Fill(0, 0, GD::TrueColor(255, 255, 255).Int());
for (std::size_t i = 0; i < label_count; ++i)
image.String(font, 0, i * font_height, labels[i], color);
for (std::size_t i = 0; i < ratio_label_count; ++i)
image.String(font, 0, (i + label_count) * font_height, ratio_labels[i],
color);
const auto second_column = longest_label * font_width;
for (std::size_t i = 0; i < GetNumberRows(); ++i)
image.String(font, second_column, i * font_height,
static_cast<const char *>(GetCellValue(i, 0).c_str()), color);
return image;
}
#endif //GD_FOUND