forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface.cpp
More file actions
190 lines (162 loc) · 6.64 KB
/
surface.cpp
File metadata and controls
190 lines (162 loc) · 6.64 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
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
/*******************************************************
* 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 <af/graphics.h>
#include <af/image.h>
#include <backend.hpp>
#include <common/ArrayInfo.hpp>
#include <common/err_common.hpp>
#include <common/graphics_common.hpp>
#include <handle.hpp>
#include <join.hpp>
#include <reduce.hpp>
#include <reorder.hpp>
#include <surface.hpp>
#include <tile.hpp>
using af::dim4;
using namespace detail;
using namespace graphics;
template<typename T>
fg_chart setup_surface(fg_window window, const af_array xVals,
const af_array yVals, const af_array zVals,
const af_cell* const props) {
ForgeModule& _ = graphics::forgePlugin();
Array<T> xIn = getArray<T>(xVals);
Array<T> yIn = getArray<T>(yVals);
Array<T> zIn = getArray<T>(zVals);
const ArrayInfo& Xinfo = getInfo(xVals);
const ArrayInfo& Yinfo = getInfo(yVals);
const ArrayInfo& Zinfo = getInfo(zVals);
af::dim4 X_dims = Xinfo.dims();
af::dim4 Y_dims = Yinfo.dims();
af::dim4 Z_dims = Zinfo.dims();
if (Xinfo.isVector()) {
// Convert xIn is a column vector
xIn = modDims(xIn, xIn.elements());
// Now tile along second dimension
dim4 x_tdims(1, Y_dims[0], 1, 1);
xIn = tile(xIn, x_tdims);
// Convert yIn to a row vector
yIn = modDims(yIn, af::dim4(1, yIn.elements()));
// Now tile along first dimension
dim4 y_tdims(X_dims[0], 1, 1, 1);
yIn = tile(yIn, y_tdims);
}
// Flatten xIn, yIn and zIn into row vectors
dim4 rowDims = dim4(1, zIn.elements());
xIn = modDims(xIn, rowDims);
yIn = modDims(yIn, rowDims);
zIn = modDims(zIn, rowDims);
// Now join along first dimension, skip reorder
std::vector<Array<T>> inputs{xIn, yIn, zIn};
Array<T> Z = join(0, inputs);
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_3D);
else
chart = fgMngr.getChart(window, 0, 0, FG_CHART_3D);
fg_surface surface =
fgMngr.getSurface(chart, Z_dims[0], Z_dims[1], getGLType<T>());
FG_CHECK(_.fg_set_surface_color(surface, 0.0, 1.0, 0.0, 1.0));
// If chart axes limits do not have a manual override
// then compute and set axes limits
if (!fgMngr.getChartAxesOverride(chart)) {
float cmin[3], cmax[3];
T dmin[3], dmax[3];
FG_CHECK(_.fg_get_chart_axes_limits(
&cmin[0], &cmax[0], &cmin[1], &cmax[1], &cmin[2], &cmax[2], chart));
dmin[0] = reduce_all<af_min_t, T, T>(xIn);
dmax[0] = reduce_all<af_max_t, T, T>(xIn);
dmin[1] = reduce_all<af_min_t, T, T>(yIn);
dmax[1] = reduce_all<af_max_t, T, T>(yIn);
dmin[2] = reduce_all<af_min_t, T, T>(zIn);
dmax[2] = reduce_all<af_max_t, T, T>(zIn);
if (cmin[0] == 0 && cmax[0] == 0 && cmin[1] == 0 && cmax[1] == 0 &&
cmin[2] == 0 && cmax[2] == 0) {
// No previous limits. Set without checking
cmin[0] = step_round(dmin[0], false);
cmax[0] = step_round(dmax[0], true);
cmin[1] = step_round(dmin[1], false);
cmax[1] = step_round(dmax[1], true);
cmin[2] = step_round(dmin[2], false);
cmax[2] = step_round(dmax[2], true);
} else {
if (cmin[0] > dmin[0]) cmin[0] = step_round(dmin[0], false);
if (cmax[0] < dmax[0]) cmax[0] = step_round(dmax[0], true);
if (cmin[1] > dmin[1]) cmin[1] = step_round(dmin[1], false);
if (cmax[1] < dmax[1]) cmax[1] = step_round(dmax[1], true);
if (cmin[2] > dmin[2]) cmin[2] = step_round(dmin[2], false);
if (cmax[2] < dmax[2]) cmax[2] = step_round(dmax[2], true);
}
FG_CHECK(_.fg_set_chart_axes_limits(chart, cmin[0], cmax[0], cmin[1],
cmax[1], cmin[2], cmax[2]));
}
copy_surface<T>(Z, surface);
return chart;
}
af_err af_draw_surface(const af_window window, const af_array xVals,
const af_array yVals, const af_array S,
const af_cell* const props) {
try {
if (window == 0) { AF_ERROR("Not a valid window", AF_ERR_INTERNAL); }
const ArrayInfo& Xinfo = getInfo(xVals);
af::dim4 X_dims = Xinfo.dims();
af_dtype Xtype = Xinfo.getType();
const ArrayInfo& Yinfo = getInfo(yVals);
af::dim4 Y_dims = Yinfo.dims();
af_dtype Ytype = Yinfo.getType();
const ArrayInfo& Sinfo = getInfo(S);
af::dim4 S_dims = Sinfo.dims();
af_dtype Stype = Sinfo.getType();
TYPE_ASSERT(Xtype == Ytype);
TYPE_ASSERT(Ytype == Stype);
if (!Yinfo.isVector()) {
DIM_ASSERT(1, X_dims == Y_dims);
DIM_ASSERT(3, Y_dims == S_dims);
} else {
DIM_ASSERT(3, (X_dims[0] * Y_dims[0] == (dim_t)Sinfo.elements()));
}
makeContextCurrent(window);
fg_chart chart = NULL;
switch (Xtype) {
case f32:
chart = setup_surface<float>(window, xVals, yVals, S, props);
break;
case s32:
chart = setup_surface<int>(window, xVals, yVals, S, props);
break;
case u32:
chart = setup_surface<uint>(window, xVals, yVals, S, props);
break;
case s16:
chart = setup_surface<short>(window, xVals, yVals, S, props);
break;
case u16:
chart = setup_surface<ushort>(window, xVals, yVals, S, props);
break;
case u8:
chart = setup_surface<uchar>(window, xVals, yVals, S, props);
break;
default: TYPE_ERROR(1, Xtype);
}
auto gridDims = forgeManager().getWindowGrid(window);
ForgeModule& _ = graphics::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;
}