forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_coordinates.cpp
More file actions
96 lines (81 loc) · 2.89 KB
/
transform_coordinates.cpp
File metadata and controls
96 lines (81 loc) · 2.89 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
/*******************************************************
* Copyright (c) 2015, 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/dim4.hpp>
#include <af/defines.h>
#include <af/vision.h>
#include <af/image.h>
#include <af/arith.h>
#include <af/blas.h>
#include <af/data.h>
#include <err_common.hpp>
#include <backend.hpp>
#include <handle.hpp>
#include <convolve.hpp>
#include <arith.hpp>
using af::dim4;
using namespace detail;
template<typename T>
static af_array transform_coordinates(const af_array& tf, const float d0, const float d1)
{
dim_t in_dims[2] = { 4, 3 };
T h_in[4*3] = { (T)0, (T)0, (T)d1, (T)d1,
(T)0, (T)d0, (T)d0, (T)0,
(T)1, (T)1, (T)1, (T)1 };
af_array in = 0;
af_array w = 0;
af_array tmp = 0;
af_array xt = 0;
af_array yt = 0;
af_array t = 0;
AF_CHECK(af_create_array(&in, h_in, 2, in_dims, (af_dtype) af::dtype_traits<T>::af_type));
af_array tfIdx = 0;
af_index_t tfIndexs[2];
tfIndexs[0].isSeq = true;
tfIndexs[1].isSeq = true;
tfIndexs[0].idx.seq = af_make_seq(0, 2, 1);
tfIndexs[1].idx.seq = af_make_seq(2, 2, 1);
AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs));
AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE));
T h_w[4] = { 1, 1, 1, 1 };
dim_t w_dims = 4;
AF_CHECK(af_create_array(&w, h_w, 1, &w_dims, (af_dtype) af::dtype_traits<T>::af_type));
AF_CHECK(af_div(&w, w, tmp, false));
tfIndexs[1].idx.seq = af_make_seq(0, 0, 1);
AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs));
AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE));
AF_CHECK(af_mul(&xt, tmp, w, false));
tfIndexs[1].idx.seq = af_make_seq(1, 1, 1);
AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs));
AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE));
AF_CHECK(af_mul(&yt, tmp, w, false));
AF_CHECK(af_join(&t, 1, xt, yt));
AF_CHECK(af_release_array(w));
AF_CHECK(af_release_array(tmp));
AF_CHECK(af_release_array(xt));
AF_CHECK(af_release_array(yt));
return t;
}
af_err af_transform_coordinates(af_array *out, const af_array tf, const float d0, const float d1)
{
try {
ArrayInfo tfInfo = getInfo(tf);
dim4 tfDims = tfInfo.dims();
ARG_ASSERT(1, (tfDims[0]==3 && tfDims[1]==3 && tfDims.ndims()==2));
af_array output;
af_dtype type = tfInfo.getType();
switch(type) {
case f32: output = transform_coordinates<float >(tf, d0, d1); break;
case f64: output = transform_coordinates<double>(tf, d0, d1); break;
default : TYPE_ERROR(1, type);
}
std::swap(*out, output);
}
CATCHALL;
return AF_SUCCESS;
}