File: AngleTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (204 lines) | stat: -rw-r--r-- 6,090 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
//
// Test Suite for geos::algorithm::Angle
// Ported from JTS junit/algorithm/AngleTest.java r378

#include <tut/tut.hpp>
// geos
#include <geos/geom/Coordinate.h>
#include <geos/algorithm/Angle.h>
// std
#include <string>

namespace tut {
//
// Test Group
//

// dummy data, not used
struct test_angle_data {
    typedef geos::geom::Coordinate Coordinate;
    typedef geos::algorithm::Angle Angle;

    double TOL;
    double PI;

    test_angle_data()
        :
        TOL(1e-5),
        PI(3.14159265358979323846)
    {}

};

typedef test_group<test_angle_data> group;
typedef group::object object;

group test_angle_group("geos::algorithm::Angle");

//
// Test Cases
//

// testAngle()
template<>
template<>
void object::test<1>
()
{
    ensure_equals("1", Angle::angle(Coordinate(10, 0)), 0.0, TOL);
    ensure_equals("2", Angle::angle(Coordinate(10, 10)), PI / 4, TOL);
    ensure_equals("3", Angle::angle(Coordinate(0, 10)), PI / 2, TOL);
    ensure_equals("4", Angle::angle(Coordinate(-10, 10)), 0.75 * PI, TOL);
    ensure_equals("5", Angle::angle(Coordinate(-10, 0)), PI, TOL);
    ensure_equals("6", Angle::angle(Coordinate(-10, -0.1)), -3.131592987, TOL);
    ensure_equals("7", Angle::angle(Coordinate(-10, -10)), -0.75 * PI, TOL);
}

// testIsAcute()
template<>
template<>
void object::test<2>
()
{
    ensure(Angle::isAcute(
               Coordinate(10, 0), Coordinate(0, 0), Coordinate(5, 10)));
    ensure(Angle::isAcute(
               Coordinate(10, 0), Coordinate(0, 0), Coordinate(5, -10)));
    // angle of 0
    ensure(Angle::isAcute(
               Coordinate(10, 0), Coordinate(0, 0), Coordinate(10, 0)));
    ensure_not(Angle::isAcute(
                   Coordinate(10, 0), Coordinate(0, 0), Coordinate(-5, 10)));
    ensure_not(Angle::isAcute(
                   Coordinate(10, 0), Coordinate(0, 0), Coordinate(-5, -10)));
}

// testNormalizePositive()
template<>
template<>
void object::test<3>
()
{
    ensure_equals("", Angle::normalizePositive(0.0), 0.0, TOL);

    ensure_equals("", Angle::normalizePositive(-0.5 * PI), 1.5 * PI, TOL);
    ensure_equals("", Angle::normalizePositive(-PI), PI, TOL);
    ensure_equals("", Angle::normalizePositive(-1.5 * PI), .5 * PI, TOL);
    ensure_equals("", Angle::normalizePositive(-2 * PI), 0.0, TOL);
    ensure_equals("", Angle::normalizePositive(-2.5 * PI), 1.5 * PI, TOL);
    ensure_equals("", Angle::normalizePositive(-3 * PI), PI, TOL);
    ensure_equals("", Angle::normalizePositive(-4 * PI), 0.0, TOL);

    ensure_equals("", Angle::normalizePositive(0.5 * PI), 0.5 * PI, TOL);
    ensure_equals("", Angle::normalizePositive(PI), PI, TOL);
    ensure_equals("", Angle::normalizePositive(1.5 * PI), 1.5 * PI, TOL);
    ensure_equals("", Angle::normalizePositive(2 * PI), 0.0, TOL);
    ensure_equals("", Angle::normalizePositive(2.5 * PI), 0.5 * PI, TOL);
    ensure_equals("", Angle::normalizePositive(3 * PI), PI, TOL);
    ensure_equals("", Angle::normalizePositive(4 * PI), 0.0, TOL);
}

// testNormalize()
template<>
template<>
void object::test<4>
()
{
    ensure_equals("1", Angle::normalize(0.0), 0.0, TOL);

    ensure_equals("2", Angle::normalize(-0.5 * PI), -0.5 * PI, TOL);
    ensure_equals("3", Angle::normalize(-PI), PI, TOL);
    ensure_equals("4", Angle::normalize(-1.5 * PI), .5 * PI, TOL);
    ensure_equals("5", Angle::normalize(-2 * PI), 0.0, TOL);
    ensure_equals("6", Angle::normalize(-2.5 * PI), -0.5 * PI, TOL);
    ensure_equals("7", Angle::normalize(-3 * PI), PI, TOL);
    ensure_equals("8", Angle::normalize(-4 * PI), 0.0, TOL);

    ensure_equals("9", Angle::normalize(0.5 * PI), 0.5 * PI, TOL);
    ensure_equals("10", Angle::normalize(PI), PI, TOL);
    ensure_equals("11", Angle::normalize(1.5 * PI), -0.5 * PI, TOL);
    ensure_equals("12", Angle::normalize(2 * PI), 0.0, TOL);
    ensure_equals("13", Angle::normalize(2.5 * PI), 0.5 * PI, TOL);
    ensure_equals("14", Angle::normalize(3 * PI), PI, TOL);
    ensure_equals("15", Angle::normalize(4 * PI), 0.0, TOL);
}

// testInteriorAngle()
template<>
template<>
void object::test<5>
()
{
    Coordinate p1(1, 2);
    Coordinate p2(3, 2);
    Coordinate p3(2, 1);
    // Tests all interior angles of a triangle "POLYGON ((1 2, 3 2, 2 1, 1 2))"
    ensure_equals("45",
        45.0,
        Angle::toDegrees(Angle::interiorAngle(p1, p2, p3)),
        TOL);
    ensure_equals("90",
        90.0,
        Angle::toDegrees(Angle::interiorAngle(p2, p3, p1)),
        TOL);
    ensure_equals("45",
        45.0,
        Angle::toDegrees(Angle::interiorAngle(p3, p1, p2)),
        TOL);
    // Tests interior angles greater than 180 degrees
    ensure_equals("315",
        315.0,
        Angle::toDegrees(Angle::interiorAngle(p3, p2, p1)),
        TOL);
    ensure_equals("270",
        270.0,
        Angle::toDegrees(Angle::interiorAngle(p1, p3, p2)),
        TOL);
    ensure_equals("315",
        315.0,
        Angle::toDegrees(Angle::interiorAngle(p2, p1, p3)),
        TOL);
}

// testSinCosSnap()
template<>
template<>
void object::test<6>
()
{
    double rSin, rCos;

    // -720 to 720 degrees with 1 degree increments
    for (int angdeg = -720; angdeg <= 720; angdeg++) {
        const double ang = Angle::toRadians(angdeg);

        Angle::sinCosSnap(ang, rSin, rCos);

        double cSin = std::sin(ang);
        double cCos = std::cos(ang);
        if ( (angdeg % 90) == 0 ) {
            // not always the same for multiples of 90 degrees
            ensure(std::to_string(angdeg), std::fabs(rSin - cSin) < 1e-15);
            ensure(std::to_string(angdeg), std::fabs(rCos - cCos) < 1e-15);
        } else {
            ensure_equals(std::to_string(angdeg), rSin, cSin);
            ensure_equals(std::to_string(angdeg), rCos, cCos);
        }

    }

    // use radian increments that don't snap to exact degrees or zero
    for (double angrad = -6.3; angrad < 6.3; angrad += 0.013) {

        Angle::sinCosSnap(angrad, rSin, rCos);

        ensure_equals(std::to_string(angrad), rSin, std::sin(angrad));
        ensure_equals(std::to_string(angrad), rCos, std::cos(angrad));

    }

}


} // namespace tut