File: rotate_linear.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (211 lines) | stat: -rw-r--r-- 9,603 bytes parent folder | download
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*******************************************************
 * 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 <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>

using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::abs;
using af::cfloat;
using af::cdouble;

template<typename T>
class RotateLinear : public ::testing::Test
{
    public:
        virtual void SetUp() {
            subMat0.push_back(af_make_seq(0, 4, 1));
            subMat0.push_back(af_make_seq(2, 6, 1));
            subMat0.push_back(af_make_seq(0, 2, 1));
        }
        vector<af_seq> subMat0;
};

// create a list of types to be tested
typedef ::testing::Types<float, double, cfloat, cdouble, int, intl, char, short> TestTypes;

// register the type list
TYPED_TEST_CASE(RotateLinear, TestTypes);

#define PI 3.1415926535897931f

template<typename T>
void rotateTest(string pTestFile, const unsigned resultIdx, const float angle, const bool crop, const bool recenter, bool isSubRef = false, const vector<af_seq> * seqv = NULL)
{
    if (noDoubleTests<T>()) return;

    vector<af::dim4> numDims;
    vector<vector<T> >   in;
    vector<vector<T> >   tests;
    readTests<T, T, float>(pTestFile,numDims,in,tests);

    af::dim4 dims = numDims[0];

    af_array inArray = 0;
    af_array outArray = 0;
    af_array tempArray = 0;

    float theta = angle * PI / 180.0f;

    if (isSubRef) {

        ASSERT_EQ(AF_SUCCESS, af_create_array(&tempArray, &(in[0].front()), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<T>::af_type));

        ASSERT_EQ(AF_SUCCESS, af_index(&inArray, tempArray, seqv->size(), &seqv->front()));
    } else {
        ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<T>::af_type));
    }

    ASSERT_EQ(AF_SUCCESS, af_rotate(&outArray, inArray, theta, crop, AF_INTERP_BILINEAR));

    // Get result
    T* outData = new T[tests[resultIdx].size()];
    ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));

    // Compare result
    size_t nElems = tests[resultIdx].size();

    // This is a temporary solution. The reason we need this is because of
    // floating point error in the index computations on CPU/GPU, some
    // elements of GPU(CUDA/OpenCL) versions are different from the CPU version.
    // That is, the input index of CPU/GPU may differ by 1 (rounding error) on
    // x or y, hence a different value is copied.
    // We expect 99.99% values to be same between the CPU/GPU versions and
    // ASSERT_EQ (in comments below) to pass for CUDA & OpenCL backends
    size_t fail_count = 0;
    for(size_t i = 0; i < nElems; i++) {
        if(abs((tests[resultIdx][i] - (T)outData[i])) > 0.001) {
            fail_count++;
        }
    }
    ASSERT_EQ(true, ((fail_count / (float)nElems) < 0.02)) << "where count = " << fail_count << std::endl;

    //for (size_t elIter = 0; elIter < nElems; ++elIter) {
    //    ASSERT_EQ(tests[resultIdx][elIter], outData[elIter]) << "at: " << elIter << std::endl;
    //}


    // Delete
    delete[] outData;

    if(inArray   != 0) af_release_array(inArray);
    if(outArray  != 0) af_release_array(outArray);
    if(tempArray != 0) af_release_array(tempArray);
}

#define ROTATE_INIT(desc, file, resultIdx, angle, crop, recenter)       \
    TYPED_TEST(RotateLinear, desc)                                      \
    {                                                                   \
        rotateTest<TypeParam>(string(TEST_DIR"/rotate/"#file".test"), resultIdx, angle, crop, recenter); \
    }

    ROTATE_INIT(Square180NoCropRecenter     , rotatelinear1,  0, 180, false, true);
    ROTATE_INIT(Square180CropRecenter       , rotatelinear1,  1, 180, true , true);
    ROTATE_INIT(Square90NoCropRecenter      , rotatelinear1,  2, 90 , false, true);
    ROTATE_INIT(Square90CropRecenter        , rotatelinear1,  3, 90 , true , true);
    ROTATE_INIT(Square45NoCropRecenter      , rotatelinear1,  4, 45 , false, true);
    ROTATE_INIT(Square45CropRecenter        , rotatelinear1,  5, 45 , true , true);
    ROTATE_INIT(Squarem45NoCropRecenter     , rotatelinear1,  6,-45 , false, true);
    ROTATE_INIT(Squarem45CropRecenter       , rotatelinear1,  7,-45 , true , true);
    ROTATE_INIT(Square60NoCropRecenter      , rotatelinear1,  8, 60 , false, true);
    ROTATE_INIT(Square60CropRecenter        , rotatelinear1,  9, 60 , true , true);
    ROTATE_INIT(Square30NoCropRecenter      , rotatelinear1, 10, 30 , false, true);
    ROTATE_INIT(Square30CropRecenter        , rotatelinear1, 11, 30 , true , true);
    ROTATE_INIT(Square15NoCropRecenter      , rotatelinear1, 12, 15 , false, true);
    ROTATE_INIT(Square15CropRecenter        , rotatelinear1, 13, 15 , true , true);
    ROTATE_INIT(Square10NoCropRecenter      , rotatelinear1, 14, 10 , false, true);
    ROTATE_INIT(Square10CropRecenter        , rotatelinear1, 15, 10 , true , true);
    ROTATE_INIT(Square01NoCropRecenter      , rotatelinear1, 16,  1 , false, true);
    ROTATE_INIT(Square01CropRecenter        , rotatelinear1, 17,  1 , true , true);
    ROTATE_INIT(Square360NoCropRecenter     , rotatelinear1, 18, 360, false, true);
    ROTATE_INIT(Square360CropRecenter       , rotatelinear1, 19, 360, true , true);
    ROTATE_INIT(Squarem180NoCropRecenter    , rotatelinear1, 20,-180, false, true);
    ROTATE_INIT(Squarem180CropRecenter      , rotatelinear1, 21,-180, false, true);
    ROTATE_INIT(Square00NoCropRecenter      , rotatelinear1, 22,  0 , false, true);
    ROTATE_INIT(Square00CropRecenter        , rotatelinear1, 23,  0 , true , true);

    ROTATE_INIT(Rectangle180NoCropRecenter     , rotatelinear2,  0, 180, false, true);
    ROTATE_INIT(Rectangle180CropRecenter       , rotatelinear2,  1, 180, true , true);
    ROTATE_INIT(Rectangle90NoCropRecenter      , rotatelinear2,  2, 90 , false, true);
    ROTATE_INIT(Rectangle90CropRecenter        , rotatelinear2,  3, 90 , true , true);
    ROTATE_INIT(Rectangle45NoCropRecenter      , rotatelinear2,  4, 45 , false, true);
    ROTATE_INIT(Rectangle45CropRecenter        , rotatelinear2,  5, 45 , true , true);
    ROTATE_INIT(Rectanglem45NoCropRecenter     , rotatelinear2,  6,-45 , false, true);
    ROTATE_INIT(Rectanglem45CropRecenter       , rotatelinear2,  7,-45 , true , true);
    ROTATE_INIT(Rectangle60NoCropRecenter      , rotatelinear2,  8, 60 , false, true);
    ROTATE_INIT(Rectangle60CropRecenter        , rotatelinear2,  9, 60 , true , true);
    ROTATE_INIT(Rectangle30NoCropRecenter      , rotatelinear2, 10, 30 , false, true);
    ROTATE_INIT(Rectangle30CropRecenter        , rotatelinear2, 11, 30 , true , true);
    ROTATE_INIT(Rectangle15NoCropRecenter      , rotatelinear2, 12, 15 , false, true);
    ROTATE_INIT(Rectangle15CropRecenter        , rotatelinear2, 13, 15 , true , true);
    ROTATE_INIT(Rectangle10NoCropRecenter      , rotatelinear2, 14, 10 , false, true);
    ROTATE_INIT(Rectangle10CropRecenter        , rotatelinear2, 15, 10 , true , true);
    ROTATE_INIT(Rectangle01NoCropRecenter      , rotatelinear2, 16,  1 , false, true);
    ROTATE_INIT(Rectangle01CropRecenter        , rotatelinear2, 17,  1 , true , true);
    ROTATE_INIT(Rectangle360NoCropRecenter     , rotatelinear2, 18, 360, false, true);
    ROTATE_INIT(Rectangle360CropRecenter       , rotatelinear2, 19, 360, true , true);
    ROTATE_INIT(Rectanglem180NoCropRecenter    , rotatelinear2, 20,-180, false, true);
    ROTATE_INIT(Rectanglem180CropRecenter      , rotatelinear2, 21,-180, false, true);
    ROTATE_INIT(Rectangle00NoCropRecenter      , rotatelinear2, 22,  0 , false, true);
    ROTATE_INIT(Rectangle00CropRecenter        , rotatelinear2, 23,  0 , true , true);

////////////////////////////////// CPP //////////////////////////////////////

TEST(RotateLinear, CPP)
{
    if (noDoubleTests<float>()) return;

    const unsigned resultIdx = 0;
    const float angle = 180;
    const bool crop = false;

    vector<af::dim4> numDims;
    vector<vector<float> >   in;
    vector<vector<float> >   tests;
    readTests<float, float, float>(string(TEST_DIR"/rotate/rotatelinear1.test"),numDims,in,tests);

    af::dim4 dims = numDims[0];
    float theta = angle * PI / 180.0f;

    af::array input(dims, &(in[0].front()));
    af::array output = af::rotate(input, theta, crop, AF_INTERP_BILINEAR);

    // Get result
    float* outData = new float[tests[resultIdx].size()];
    output.host((void*)outData);

    // Compare result
    size_t nElems = tests[resultIdx].size();

    // This is a temporary solution. The reason we need this is because of
    // floating point error in the index computations on CPU/GPU, some
    // elements of GPU(CUDA/OpenCL) versions are different from the CPU version.
    // That is, the input index of CPU/GPU may differ by 1 (rounding error) on
    // x or y, hence a different value is copied.
    // We expect 99.99% values to be same between the CPU/GPU versions and
    // ASSERT_EQ (in comments below) to pass for CUDA & OpenCL backends
    size_t fail_count = 0;
    for(size_t i = 0; i < nElems; i++) {
        if(fabs(tests[resultIdx][i] - outData[i]) > 0.0001)
            fail_count++;
    }
    ASSERT_EQ(true, ((fail_count / (float)nElems) < 0.01));

    // Delete
    delete[] outData;
}