File: Area.cpp

package info (click to toggle)
mercator 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,008 kB
  • sloc: sh: 10,433; cpp: 4,482; makefile: 115
file content (296 lines) | stat: -rw-r--r-- 7,705 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2005 Alistair Riddoch

#include <Mercator/Area.h>
#include "Mercator/Segment.h"

#include <wfmath/intersect.h>
#include <iostream>
#include <cmath>

#include <cassert>

namespace Mercator
{

typedef WFMath::Point<2> Point2;
typedef WFMath::Vector<2> Vector2;

bool isZero(double d)
{
    return (fabs(d) < WFMATH_EPSILON);
}

/// \brief Helper to clip points to a given range.
class TopClip
{
public:
    /// Constructor
    ///
    /// @param t top of y range
    TopClip(double t) : topY(t) { }
    
    /// \brief Check a point is outside this clip.
    ///
    /// @param p point to be checked.
    /// @return true if p is outside the clip.
    bool inside(const Point2& p) const
    {
        return p.y() >= topY;
    }

    /// \brief Determine the point where a line crosses this clip.
    ///
    /// @param u one of of a line that crosses this clip
    /// @param v one of of a line that crosses this clip
    /// @return a point where the line cross this clip.
    Point2 clip(const Point2& u, const Point2& v) const
    {
        double dy = v.y() - u.y();
        double dx = v.x() - u.x();
        
        // shouldn't every happen - if dy iz zero, the line is horizontal,
        // so either both points should be inside, or both points should be
        // outside. In either case, we should not call clip()
        assert(!isZero(dy));
        
        double t = (topY - u.y()) / dy;
        return Point2(u.x() + t * dx, topY);
    }
private:
    /// \brief Top of y range.
    double topY;
};

/// \brief Helper to clip points to a given range.
class BottomClip
{
public:
    /// Constructor
    ///
    /// @param t bottom of y range
    BottomClip(double t) : bottomY(t) { }
    
    /// \brief Check a point is outside this clip.
    ///
    /// @param p point to be checked.
    /// @return true if p is outside the clip.
    bool inside(const Point2& p) const
    {
        return p.y() < bottomY;
    }

    /// \brief Determine the point where a line crosses this clip.
    ///
    /// @param u one of of a line that crosses this clip
    /// @param v one of of a line that crosses this clip
    /// @return a point where the line cross this clip.
    Point2 clip(const Point2& u, const Point2& v) const
    {
        double dy = v.y() - u.y();
        double dx = v.x() - u.x();
        assert(!isZero(dy));
        
        double t = (u.y() - bottomY) / -dy;
        return Point2(u.x() + t * dx, bottomY);
    }
private:
    /// \brief Bottom of y range.
    double bottomY;
};

/// \brief Helper to clip points to a given range.
class LeftClip
{
public:
    /// Constructor
    ///
    /// @param t left of x range.
    LeftClip(double t) : leftX(t) { }
    
    /// \brief Check a point is outside this clip.
    ///
    /// @param p point to be checked.
    /// @return true if p is outside the clip.
    bool inside(const Point2& p) const
    {
        return p.x() >= leftX;
    }

    /// \brief Determine the point where a line crosses this clip.
    ///
    /// @param u one of of a line that crosses this clip
    /// @param v one of of a line that crosses this clip
    /// @return a point where the line cross this clip.
    Point2 clip(const Point2& u, const Point2& v) const
    {
        double dy = v.y() - u.y();
        double dx = v.x() - u.x();
        
        // shouldn't every happen
        assert(!isZero(dx));
        
        double t = (leftX - u.x()) / dx;
        return Point2(leftX, u.y() + t * dy);
    }
private:
    /// \brief Left of x range.
    double leftX;
};

/// \brief Helper to clip points to a given range.
class RightClip
{
public:
    /// Constructor
    ///
    /// @param t right of x range.
    RightClip(double t) : rightX(t) { }
    
    /// \brief Check a point is outside this clip.
    ///
    /// @param p point to be checked.
    /// @return true if p is outside the clip.
    bool inside(const Point2& p) const
    {
        return p.x() < rightX;
    }

    /// \brief Determine the point where a line crosses this clip.
    ///
    /// @param u one of of a line that crosses this clip
    /// @param v one of of a line that crosses this clip
    /// @return a point where the line cross this clip.
    Point2 clip(const Point2& u, const Point2& v) const
    {
        double dy = v.y() - u.y();
        double dx = v.x() - u.x();
        
        // shouldn't every happen
        assert(!isZero(dx));
        
        double t = (u.x() - rightX) / -dx;
        return Point2(rightX, u.y() + t * dy);
    }
private:
    /// \brief Right of x range.
    double rightX;
};

template <class Clip>
WFMath::Polygon<2> sutherlandHodgmanKernel(const WFMath::Polygon<2>& inpoly, Clip clipper)
{
    WFMath::Polygon<2> outpoly;
    
    if (!inpoly.isValid()) return inpoly;
    int points = inpoly.numCorners();
    if (points < 3) return outpoly; // i.e an invalid result
    
    Point2 lastPt = inpoly.getCorner(points - 1);
    bool lastInside = clipper.inside(lastPt);
    
    for (int p=0; p < points; ++p) {
    
        Point2 curPt = inpoly.getCorner(p);
        bool inside = clipper.inside(curPt);
        
        if (lastInside) {
            if (inside) {
                // emit curPt
                outpoly.addCorner(outpoly.numCorners(), curPt);
            } else {
                // emit intersection of edge with clip line
                outpoly.addCorner(outpoly.numCorners(), clipper.clip(lastPt, curPt));
            }
        } else {
            if (inside) {
                // emit both
                outpoly.addCorner(outpoly.numCorners(), clipper.clip(lastPt, curPt));
                outpoly.addCorner(outpoly.numCorners(), curPt);
            } else {
                // don't emit anything
            }
        } // last was outside
        
        lastPt = curPt;
        lastInside = inside;
    }
    
    return outpoly;
}

Area::Area(int layer, bool hole) :
    m_layer(layer),
    m_hole(hole),
    m_shader(0)
{
}

void Area::setShape(const WFMath::Polygon<2>& p)
{
    assert(p.isValid());
    m_shape = p;
    m_box = p.boundingBox();
}

void Area::setShader(const Shader * shader) const
{
    m_shader = shader;
}

bool Area::contains(double x, double y) const
{
    if (!WFMath::Contains(m_box, Point2(x,y), false)) return false;
    
    return WFMath::Contains(m_shape, Point2(x,y), false);
}

int Area::addToSegment(Segment & s) const
{
    if (!checkIntersects(s)) {
        return -1;
    }
    return s.addArea(this);
}

void Area::updateToSegment(Segment & s) const
{
    if (!checkIntersects(s)) {
        s.removeArea(this);
        return;
    }
    if (s.updateArea(this) != 0) {
        s.addArea(this);
    }
}

void Area::removeFromSegment(Segment & s) const
{
    if (checkIntersects(s)) {
        s.removeArea(this);
    }
}

WFMath::Polygon<2> Area::clipToSegment(const Segment& s) const
{
    // box reject
    if (!checkIntersects(s)) return WFMath::Polygon<2>();
    
    WFMath::AxisBox<2> segBox(s.getRect());
    WFMath::Polygon<2> clipped = sutherlandHodgmanKernel(m_shape, TopClip(segBox.lowCorner().y()));
    
    clipped = sutherlandHodgmanKernel(clipped, BottomClip(segBox.highCorner().y()));
    clipped = sutherlandHodgmanKernel(clipped, LeftClip(segBox.lowCorner().x()));
    clipped = sutherlandHodgmanKernel(clipped, RightClip(segBox.highCorner().x()));
    
    return clipped;
}

bool Area::checkIntersects(const Segment& s) const
{
    return WFMath::Intersect(m_shape, s.getRect(), false) ||
        WFMath::Contains(s.getRect(), m_shape.getCorner(0), false);
}

} // of namespace