File: SegmentPointComparatorTest.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 (188 lines) | stat: -rw-r--r-- 4,475 bytes parent folder | download | duplicates (2)
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
//
// Test Suite for geos::noding::SegmentPointComparator class.
//
// Ports tests found in jts/junit/noding/SegmentPointComparatorTest.java
// and jts/junit/noding/SegmentPointComparatorFullTest.java

#include <tut/tut.hpp>
// geos
#include <geos/noding/SegmentNode.h>
#include <geos/noding/SegmentPointComparator.h>
#include <geos/noding/SegmentString.h>
#include <geos/noding/Octant.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/geom/LineSegment.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateSequence.h>
// std
#include <memory>

namespace tut {
//
// Test Group
//

// Common data used by tests
struct test_segmentpointcomparator_data {

    typedef std::unique_ptr<geos::geom::CoordinateSequence>
    CoordinateSequenceAutoPtr;

    typedef std::unique_ptr<geos::noding::SegmentString>
    SegmentStringAutoPtr;

    typedef geos::geom::Coordinate Coordinate;
    typedef geos::geom::LineSegment LineSegment;
    typedef geos::geom::PrecisionModel PrecisionModel;

    PrecisionModel pm;

    test_segmentpointcomparator_data()
        : pm(1.0)
    {}

    void
    checkNodePosition(int octant, double x0, double y0,
                      double x1, double y1, int
                      expectedPositionValue)
    {
        using geos::noding::SegmentPointComparator;

        int posValue = SegmentPointComparator::compare(octant,
                       Coordinate(x0, y0),
                       Coordinate(x1, y1)
                                                      );
        ensure(posValue == expectedPositionValue);
    }

    void
    checkNodePosition(const LineSegment& seg, const Coordinate& p0,
                      const Coordinate& p1, int expectedPositionValue)
    {
        using geos::noding::Octant;
        using geos::noding::SegmentPointComparator;

        int octant = Octant::octant(seg.p0, seg.p1);
        int posValue = SegmentPointComparator::compare(octant, p0, p1);

        ensure(posValue == expectedPositionValue);
    }

    Coordinate
    computePoint(const LineSegment& seg, double dist)
    {
        double dx = seg.p1.x - seg.p0.x;
        double dy = seg.p1.y - seg.p0.y;
        double len = seg.getLength();
        Coordinate pt(dist * dx / len, dist * dy / len);
        pm.makePrecise(pt);
        return pt;
    }

    void
    checkPointsAtDistance(const LineSegment& seg,
                          double dist0, double dist1)
    {
        using geos::geom::Coordinate;
        Coordinate p0 = computePoint(seg, dist0);
        Coordinate p1 = computePoint(seg, dist1);
        if(p0.equals(p1)) {
            checkNodePosition(seg, p0, p1, 0);
        }
        else {
            checkNodePosition(seg, p0, p1, -1);
            checkNodePosition(seg, p1, p0, 1);
        }
    }

    void
    checkSegment(double x, double y)
    {
        Coordinate seg0(0, 0);
        Coordinate seg1(x, y);
        LineSegment seg(seg0, seg1);

        for(int i = 0; i < 4; i++) {
            double dist = i;

            double gridSize = 1 / pm.getScale();

            checkPointsAtDistance(seg, dist, dist + 1.0 * gridSize);
            checkPointsAtDistance(seg, dist, dist + 2.0 * gridSize);
            checkPointsAtDistance(seg, dist, dist + 3.0 * gridSize);
            checkPointsAtDistance(seg, dist, dist + 4.0 * gridSize);
        }
    }


};

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

group test_segmentpointcomparator_group("geos::noding::SegmentPointComparator");

//
// Test Cases
//

// testOctant0 (from simple unit test)
template<>
template<>
void object::test<1>
()
{
    checkNodePosition(0, 1, 1, 2, 2, -1);
    checkNodePosition(0, 1, 0, 1, 1, -1);
}

// testQuadrant0
template<>
template<>
void object::test<2>
()
{
    checkSegment(100, 0);
    checkSegment(100, 50);
    checkSegment(100, 100);
    checkSegment(100, 150);
    checkSegment(0, 100);
}

// testQuadrant4
template<>
template<>
void object::test<3>
()
{
    checkSegment(100, -50);
    checkSegment(100, -100);
    checkSegment(100, -150);
    checkSegment(0, -100);
}

// testQuadrant1
template<>
template<>
void object::test<4>
()
{
    checkSegment(-100, 0);
    checkSegment(-100, 50);
    checkSegment(-100, 100);
    checkSegment(-100, 150);
}

// testQuadrant2
template<>
template<>
void object::test<5>
()
{
    checkSegment(-100, 0);
    checkSegment(-100, -50);
    checkSegment(-100, -100);
    checkSegment(-100, -150);
}

} // namespace tut