1 /*
2  * Copyright (c) 2012-2016, The University of Oxford
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  * 3. Neither the name of the University of Oxford nor the names of its
13  *    contributors may be used to endorse or promote products derived from this
14  *    software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include "math/oskar_fit_ellipse.h"
32 #include "utility/oskar_get_error_string.h"
33 
34 #include <cmath>
35 #include <cstdlib>
36 #include <cstdio>
37 #include <vector>
38 
TEST(fit_ellipse,test1)39 TEST(fit_ellipse, test1)
40 {
41     int num_points = 7, status = 0;
42     double maj_d = 0.0, min_d = 0.0, pa_d = 0.0;
43     float maj_f = 0.0, min_f = 0.0, pa_f = 0.0;
44 
45     // Test double precision.
46     {
47         std::vector<double> x(num_points), y(num_points);
48         std::vector<double> work1(5 * num_points), work2(5 * num_points);
49         x[0] = -0.1686;
50         x[1] = -0.0921;
51         x[2] =  0.0765;
52         x[3] =  0.1686;
53         x[4] =  0.0921;
54         x[5] = -0.0765;
55         x[6] = -0.1686;
56 
57         y[0] =  0.7282;
58         y[1] =  0.6994;
59         y[2] =  0.6675;
60         y[3] =  0.6643;
61         y[4] =  0.7088;
62         y[5] =  0.7407;
63         y[6] =  0.7282;
64 
65         oskar_fit_ellipse_d(&maj_d, &min_d, &pa_d, num_points, &x[0], &y[0],
66                 &work1[0], &work2[0], &status);
67         EXPECT_EQ(0, status) << oskar_get_error_string(status);
68     }
69 
70     // Test single precision.
71     {
72         std::vector<float> x(num_points), y(num_points);
73         std::vector<float> work1(5 * num_points), work2(5 * num_points);
74         x[0] = -0.1686f;
75         x[1] = -0.0921f;
76         x[2] =  0.0765f;
77         x[3] =  0.1686f;
78         x[4] =  0.0921f;
79         x[5] = -0.0765f;
80         x[6] = -0.1686f;
81 
82         y[0] =  0.7282f;
83         y[1] =  0.6994f;
84         y[2] =  0.6675f;
85         y[3] =  0.6643f;
86         y[4] =  0.7088f;
87         y[5] =  0.7407f;
88         y[6] =  0.7282f;
89 
90         oskar_fit_ellipse_f(&maj_f, &min_f, &pa_f, num_points, &x[0], &y[0],
91                 &work1[0], &work2[0], &status);
92         EXPECT_EQ(0, status) << oskar_get_error_string(status);
93     }
94 
95     // Compare results.
96     EXPECT_NEAR(maj_d, 0.3608619735, 1e-9);
97     EXPECT_NEAR(min_d, 0.0494223702, 1e-9);
98     EXPECT_NEAR(pa_d, -1.3865537748, 1e-9);
99     EXPECT_NEAR(maj_d, maj_f, 1e-5);
100     EXPECT_NEAR(min_d, min_f, 1e-5);
101     EXPECT_NEAR(pa_d, pa_f, 1e-5);
102 }
103