-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathhist.cpp
More file actions
161 lines (142 loc) · 5.46 KB
/
hist.cpp
File metadata and controls
161 lines (142 loc) · 5.46 KB
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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <backend.hpp>
#include <common/ArrayInfo.hpp>
#include <common/cast.hpp>
#include <common/err_common.hpp>
#include <common/graphics_common.hpp>
#include <copy.hpp>
#include <handle.hpp>
#include <hist_graphics.hpp>
#include <platform.hpp>
#include <reduce.hpp>
#include <af/graphics.h>
using arrayfire::common::ForgeManager;
using arrayfire::common::ForgeModule;
using arrayfire::common::forgePlugin;
using arrayfire::common::getGLType;
using arrayfire::common::makeContextCurrent;
using arrayfire::common::step_round;
using detail::Array;
using detail::copy_histogram;
using detail::forgeManager;
using detail::getScalar;
using detail::schar;
using detail::uchar;
using detail::uint;
using detail::ushort;
template<typename T>
fg_chart setup_histogram(fg_window const window, const af_array in,
const double minval, const double maxval,
const af_cell* const props) {
ForgeModule& _ = forgePlugin();
const Array<T> histogramInput = getArray<T>(in);
dim_t nBins = histogramInput.elements();
// Retrieve Forge Histogram with nBins and array type
ForgeManager& fgMngr = forgeManager();
// Get the chart for the current grid position (if any)
fg_chart chart = NULL;
if (props->col > -1 && props->row > -1) {
chart = fgMngr.getChart(window, props->row, props->col, FG_CHART_2D);
} else {
chart = fgMngr.getChart(window, 0, 0, FG_CHART_2D);
}
// Create a histogram for the chart
fg_histogram hist = fgMngr.getHistogram(chart, nBins, getGLType<T>());
// Set histogram bar colors to ArrayFire's orange
FG_CHECK(_.fg_set_histogram_color(hist, 0.929f, 0.486f, 0.2745f, 1.0f));
// If chart axes limits do not have a manual override
// then compute and set axes limits
if (!fgMngr.getChartAxesOverride(chart)) {
float xMin, xMax, yMin, yMax, zMin, zMax;
FG_CHECK(_.fg_get_chart_axes_limits(&xMin, &xMax, &yMin, &yMax, &zMin,
&zMax, chart));
T freqMax =
getScalar<T>(detail::reduce_all<af_max_t, T, T>(histogramInput));
// For histogram, xMin and xMax should always be the first
// and last bin respectively and should not be rounded
if (xMin == 0 && xMax == 0 && yMin == 0 && yMax == 0) {
// No previous limits. Set without checking
xMin = static_cast<float>(minval);
xMax = static_cast<float>(maxval);
yMax = static_cast<float>(step_round(freqMax, true));
// For histogram, always set yMin to 0.
yMin = 0;
} else {
if (xMin > minval) {
xMin = static_cast<float>(minval);
}
if (xMax < maxval) {
xMax = static_cast<float>(maxval);
}
if (yMax < freqMax) {
yMax = static_cast<float>(step_round(freqMax, true));
}
// For histogram, always set yMin to 0.
yMin = 0;
}
FG_CHECK(_.fg_set_chart_axes_limits(chart, xMin, xMax, yMin, yMax, zMin,
zMax));
}
copy_histogram<T>(histogramInput, hist);
return chart;
}
af_err af_draw_hist(const af_window window, const af_array X,
const double minval, const double maxval,
const af_cell* const props) {
try {
if (window == 0) { AF_ERROR("Not a valid window", AF_ERR_INTERNAL); }
const ArrayInfo& Xinfo = getInfo(X);
af_dtype Xtype = Xinfo.getType();
ARG_ASSERT(0, Xinfo.isVector());
makeContextCurrent(window);
fg_chart chart = NULL;
switch (Xtype) {
case f32:
chart =
setup_histogram<float>(window, X, minval, maxval, props);
break;
case s32:
chart = setup_histogram<int>(window, X, minval, maxval, props);
break;
case u32:
chart = setup_histogram<uint>(window, X, minval, maxval, props);
break;
case s16:
chart =
setup_histogram<short>(window, X, minval, maxval, props);
break;
case u16:
chart =
setup_histogram<ushort>(window, X, minval, maxval, props);
break;
case s8:
chart =
setup_histogram<schar>(window, X, minval, maxval, props);
break;
case u8:
chart =
setup_histogram<uchar>(window, X, minval, maxval, props);
break;
default: TYPE_ERROR(1, Xtype);
}
auto gridDims = forgeManager().getWindowGrid(window);
ForgeModule& _ = forgePlugin();
if (props->col > -1 && props->row > -1) {
FG_CHECK(_.fg_draw_chart_to_cell(
window, gridDims.first, gridDims.second,
props->row * gridDims.second + props->col, chart,
props->title));
} else {
FG_CHECK(_.fg_draw_chart(window, chart));
}
}
CATCHALL;
return AF_SUCCESS;
}