forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface.cpp
More file actions
135 lines (113 loc) · 4.12 KB
/
surface.cpp
File metadata and controls
135 lines (113 loc) · 4.12 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
/*******************************************************
* 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 <ArrayInfo.hpp>
#include <graphics_common.hpp>
#include <err_common.hpp>
#include <backend.hpp>
#include <surface.hpp>
#include <reduce.hpp>
#include <join.hpp>
#include <tile.hpp>
#include <reorder.hpp>
#include <handle.hpp>
using af::dim4;
using namespace detail;
#if defined(WITH_GRAPHICS)
using namespace graphics;
template<typename T>
fg::Surface* setup_surface(const af_array xVals, const af_array yVals, const af_array zVals)
{
Array<T> xIn = getArray<T>(xVals);
Array<T> yIn = getArray<T>(yVals);
Array<T> zIn = getArray<T>(zVals);
T xmax = reduce_all<af_max_t, T, T>(xIn);
T xmin = reduce_all<af_min_t, T, T>(xIn);
T ymax = reduce_all<af_max_t, T, T>(yIn);
T ymin = reduce_all<af_min_t, T, T>(yIn);
T zmax = reduce_all<af_max_t, T, T>(zIn);
T zmin = reduce_all<af_min_t, T, T>(zIn);
ArrayInfo Xinfo = getInfo(xVals);
ArrayInfo Yinfo = getInfo(yVals);
ArrayInfo Zinfo = getInfo(zVals);
af::dim4 X_dims = Xinfo.dims();
af::dim4 Y_dims = Yinfo.dims();
af::dim4 Z_dims = Zinfo.dims();
dim4 rdims(1, 0, 2, 3);
dim4 x_tdims(1, Y_dims[0], 1, 1);
dim4 y_tdims(1, X_dims[0], 1, 1);
if(Xinfo.isVector()){
xIn = tile(xIn, x_tdims);
yIn = tile(yIn, y_tdims);
yIn = reorder(yIn, rdims);
}
xIn.modDims(xIn.elements());
yIn.modDims(yIn.elements());
zIn.modDims(zIn.elements());
Array<T> Z = join(1, join(1, xIn, yIn), zIn);
Z = reorder(Z, rdims);
Z.modDims(Z.elements());
ForgeManager& fgMngr = ForgeManager::getInstance();
fg::Surface* surface = fgMngr.getSurface(Z_dims[0], Z_dims[1], getGLType<T>());
surface->setColor(1.0, 0.0, 0.0);
surface->setAxesLimits(xmax, xmin, ymax, ymin, zmax, zmin);
surface->setAxesTitles("X Axis", "Y Axis", "Z Axis");
copy_surface<T>(Z, surface);
return surface;
}
#endif
af_err af_draw_surface(const af_window wind, const af_array xVals, const af_array yVals, const af_array S, const af_cell* const props)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
ArrayInfo Xinfo = getInfo(xVals);
af::dim4 X_dims = Xinfo.dims();
af_dtype Xtype = Xinfo.getType();
ArrayInfo Yinfo = getInfo(yVals);
af::dim4 Y_dims = Yinfo.dims();
af_dtype Ytype = Yinfo.getType();
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()));
}
fg::Window* window = reinterpret_cast<fg::Window*>(wind);
window->makeCurrent();
fg::Surface* surface = NULL;
switch(Xtype) {
case f32: surface = setup_surface<float >(xVals, yVals , S); break;
case s32: surface = setup_surface<int >(xVals, yVals , S); break;
case u32: surface = setup_surface<uint >(xVals, yVals , S); break;
case s16: surface = setup_surface<short >(xVals, yVals , S); break;
case u16: surface = setup_surface<ushort >(xVals, yVals , S); break;
case u8 : surface = setup_surface<uchar >(xVals, yVals , S); break;
default: TYPE_ERROR(1, Xtype);
}
if (props->col>-1 && props->row>-1)
window->draw(props->col, props->row, *surface, props->title);
else
window->draw(*surface);
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}