|
| 1 | +/******************************************************* |
| 2 | + * Copyright (c) 2015, ArrayFire |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is distributed under 3-clause BSD license. |
| 6 | + * The complete license agreement can be obtained at: |
| 7 | + * http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | + ********************************************************/ |
| 9 | + |
| 10 | +#include <af/dim4.hpp> |
| 11 | +#include <af/defines.h> |
| 12 | +#include <af/vision.h> |
| 13 | +#include <af/image.h> |
| 14 | +#include <af/arith.h> |
| 15 | +#include <af/blas.h> |
| 16 | +#include <af/data.h> |
| 17 | +#include <err_common.hpp> |
| 18 | +#include <backend.hpp> |
| 19 | +#include <handle.hpp> |
| 20 | +#include <convolve.hpp> |
| 21 | +#include <arith.hpp> |
| 22 | + |
| 23 | +using af::dim4; |
| 24 | +using namespace detail; |
| 25 | + |
| 26 | +template<typename T> |
| 27 | +static af_array transform_coordinates(const af_array& tf, const float d0, const float d1) |
| 28 | +{ |
| 29 | + dim_t in_dims[2] = { 4, 3 }; |
| 30 | + T h_in[4*3] = { (T)0, (T)0, (T)d1, (T)d1, |
| 31 | + (T)0, (T)d0, (T)d0, (T)0, |
| 32 | + (T)1, (T)1, (T)1, (T)1 }; |
| 33 | + |
| 34 | + af_array in = 0; |
| 35 | + af_array w = 0; |
| 36 | + af_array tmp = 0; |
| 37 | + af_array xt = 0; |
| 38 | + af_array yt = 0; |
| 39 | + af_array t = 0; |
| 40 | + |
| 41 | + AF_CHECK(af_create_array(&in, h_in, 2, in_dims, (af_dtype) af::dtype_traits<T>::af_type)); |
| 42 | + |
| 43 | + af_array tfIdx = 0; |
| 44 | + af_index_t tfIndexs[2]; |
| 45 | + tfIndexs[0].isSeq = true; |
| 46 | + tfIndexs[1].isSeq = true; |
| 47 | + tfIndexs[0].idx.seq = af_make_seq(0, 2, 1); |
| 48 | + tfIndexs[1].idx.seq = af_make_seq(2, 2, 1); |
| 49 | + AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs)); |
| 50 | + |
| 51 | + AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE)); |
| 52 | + T h_w[4] = { 1, 1, 1, 1 }; |
| 53 | + dim_t w_dims = 4; |
| 54 | + AF_CHECK(af_create_array(&w, h_w, 1, &w_dims, (af_dtype) af::dtype_traits<T>::af_type)); |
| 55 | + AF_CHECK(af_div(&w, w, tmp, false)); |
| 56 | + |
| 57 | + tfIndexs[1].idx.seq = af_make_seq(0, 0, 1); |
| 58 | + AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs)); |
| 59 | + AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE)); |
| 60 | + AF_CHECK(af_mul(&xt, tmp, w, false)); |
| 61 | + |
| 62 | + tfIndexs[1].idx.seq = af_make_seq(1, 1, 1); |
| 63 | + AF_CHECK(af_index_gen(&tfIdx, tf, 2, tfIndexs)); |
| 64 | + AF_CHECK(af_matmul(&tmp, in, tfIdx, AF_MAT_NONE, AF_MAT_NONE)); |
| 65 | + AF_CHECK(af_mul(&yt, tmp, w, false)); |
| 66 | + |
| 67 | + AF_CHECK(af_join(&t, 1, xt, yt)); |
| 68 | + |
| 69 | + AF_CHECK(af_release_array(w)); |
| 70 | + AF_CHECK(af_release_array(tmp)); |
| 71 | + AF_CHECK(af_release_array(xt)); |
| 72 | + AF_CHECK(af_release_array(yt)); |
| 73 | + |
| 74 | + return t; |
| 75 | +} |
| 76 | + |
| 77 | +af_err af_transform_coordinates(af_array *out, const af_array tf, const float d0, const float d1) |
| 78 | +{ |
| 79 | + try { |
| 80 | + ArrayInfo tfInfo = getInfo(tf); |
| 81 | + dim4 tfDims = tfInfo.dims(); |
| 82 | + ARG_ASSERT(1, (tfDims[0]==3 && tfDims[1]==3 && tfDims.ndims()==2)); |
| 83 | + |
| 84 | + af_array output; |
| 85 | + af_dtype type = tfInfo.getType(); |
| 86 | + switch(type) { |
| 87 | + case f32: output = transform_coordinates<float >(tf, d0, d1); break; |
| 88 | + case f64: output = transform_coordinates<double>(tf, d0, d1); break; |
| 89 | + default : TYPE_ERROR(1, type); |
| 90 | + } |
| 91 | + std::swap(*out, output); |
| 92 | + } |
| 93 | + CATCHALL; |
| 94 | + |
| 95 | + return AF_SUCCESS; |
| 96 | +} |
0 commit comments