summaryrefslogtreecommitdiff
path: root/src/base/triangulate/Shapes.h
blob: 0417d7c773f58c059b643a59cb341bb409b88fd1 (plain)
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
//
//  libavg - Media Playback Engine.
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

//
// Based on Poly2Tri algorithm.
// Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
// http://code.google.com/p/poly2tri/
//

#ifndef SHAPES_H
#define SHAPES_H

#include <vector>
#include <cstddef>
#include <assert.h>
#include <cmath>

namespace avg {

struct Edge;

struct Point
{
    double m_X, m_Y;
    int m_Index;

    /// The edges this point constitutes an upper ending point
    std::vector<Edge*> m_EdgeList;

    /// Default constructor does nothing (for performance).
    Point() {
        m_X = 0.0;
        m_Y = 0.0;
        m_Index = 0;
    }

    Point(double x, double y, int index) :
            m_X(x), m_Y(y), m_Index(index) {}

    void set_zero()
    {
        m_X = 0.0;
        m_Y = 0.0;
    }

    void set(double x_, double y_)
    {
        m_X = x_;
        m_Y = y_;
    }

    Point operator -() const
    {
        Point v;
        v.set(-m_X, -m_Y);
        return v;
    }

    void operator +=(const Point& v)
    {
        m_X += v.m_X;
        m_Y += v.m_Y;
    }

    void operator -=(const Point& v)
    {
        m_X -= v.m_X;
        m_Y -= v.m_Y;
    }

    void operator *=(double a)
    {
        m_X *= a;
        m_Y *= a;
    }

    double length() const
    {
        return sqrt(m_X * m_X + m_Y * m_Y);
    }

    /// Convert this point into a unit point. Returns the Length.
    double normalize()
    {
        double len = length();
        m_X /= len;
        m_Y /= len;
        return len;
    }

};


// Represents a simple polygon's edge
struct Edge
{
    Point* m_P, *m_Q;

    Edge(Point& p1, Point& p2) :m_P(&p1), m_Q(&p2)
    {
        if (p1.m_Y > p2.m_Y) {
            m_Q = &p1;
            m_P = &p2;
        } else if (p1.m_Y == p2.m_Y) {
            if (p1.m_X > p2.m_X) {
                m_Q = &p1;
                m_P = &p2;
            } else if (p1.m_X == p2.m_X) {
                // Repeat points
                assert(false);
            }
        }
        m_Q->m_EdgeList.push_back(this);
    }
};


class TriangulationTriangle
{

public:

    TriangulationTriangle(Point& a, Point& b, Point& c);

/// Flags to determine if an edge is a Constrained edge
    bool m_ConstrainedEdge[3];
/// Flags to determine if an edge is a Delauney edge
    bool m_DelaunayEdge[3];

    Point* getPoint(const int& index);
    Point* pointCW(Point& point);
    Point* pointCCW(Point& point);
    Point* oppositePoint(TriangulationTriangle& t, Point& p);

    TriangulationTriangle* getNeighbor(const int& index);
    void markNeighbor(Point* p1, Point* p2, TriangulationTriangle* t);
    void markNeighbor(TriangulationTriangle& t);

    void markConstrainedEdge(const int index);
    void markConstrainedEdge(Edge& edge);
    void markConstrainedEdge(Point* p, Point* q);

    unsigned int index(const Point* p);
    unsigned int edgeIndex(const Point* p1, const Point* p2);

    TriangulationTriangle* neighborCW(Point& point);
    TriangulationTriangle* neighborCCW(Point& point);
    bool getConstrainedEdgeCCW(Point& p);
    bool getConstrainedEdgeCW(Point& p);
    void setConstrainedEdgeCCW(Point& p, bool ce);
    void setConstrainedEdgeCW(Point& p, bool ce);
    bool getDelunayEdgeCCW(Point& p);
    bool getDelunayEdgeCW(Point& p);
    void setDelunayEdgeCCW(Point& p, bool e);
    void setDelunayEdgeCW(Point& p, bool e);

    bool contains(Point* p);
    bool contains(const Edge& e);
    bool contains(Point* p, Point* q);
    void legalize(Point& point);
    void legalize(Point& opoint, Point& npoint);

    void clear();
    void clearNeighbor(TriangulationTriangle *triangle);
    void clearNeighbors();
    void clearDelunayEdges();

    inline bool isInterior();
    inline void isInterior(bool b);

    TriangulationTriangle& neighborAcross(Point& opoint);

private:

    Point* m_Points[3];

    TriangulationTriangle* m_Neighbors[3];

    bool m_Interior;
};

inline bool cmp(const Point* a, const Point* b)
{
    if (a->m_Y < b->m_Y) {
        return true;
    } else if (a->m_Y == b->m_Y) {
        // Make sure q is point with greater x value
        if (a->m_X < b->m_X) {
            return true;
        }
    }
    return false;
}
/*
 inline Point operator +(const Point& a, const Point& b)
 {
 return Point(a.x + b.x, a.y + b.y);
 }

 inline Point operator -(const Point& a, const Point& b)
 {
 return Point(a.x - b.x, a.y - b.y);
 }

 inline Point operator *(double s, const Point& a)
 {
 return Point(s * a.x, s * a.y, a.index);
 } */

inline bool operator ==(const Point& a, const Point& b)
{
    return a.m_X == b.m_X && a.m_Y == b.m_Y;
}

inline bool operator !=(const Point& a, const Point& b)
{
    return a.m_X != b.m_X && a.m_Y != b.m_Y;
}

inline double dot(const Point& a, const Point& b)
{
    return a.m_X * b.m_X + a.m_Y * b.m_Y;
}

inline double cross(const Point& a, const Point& b)
{
    return a.m_X * b.m_Y - a.m_Y * b.m_X;
}

inline Point cross(const Point& a, double s)
{
    return Point(s * a.m_Y, -s * a.m_X, a.m_Index);
}

inline Point cross(const double s, const Point& a)
{
    return Point(-s * a.m_Y, s * a.m_X, a.m_Index);
}

inline Point* TriangulationTriangle::getPoint(const int& index)
{
    return m_Points[index];
}

inline TriangulationTriangle* TriangulationTriangle::getNeighbor(
        const int& index)
{
    return m_Neighbors[index];
}

inline bool TriangulationTriangle::contains(Point* p)
{
    return p == m_Points[0] || p == m_Points[1] || p == m_Points[2];
}

inline bool TriangulationTriangle::contains(const Edge& e)
{
    return contains(e.m_P) && contains(e.m_Q);
}

inline bool TriangulationTriangle::contains(Point* p, Point* q)
{
    return contains(p) && contains(q);
}

inline bool TriangulationTriangle::isInterior()
{
    return m_Interior;
}

inline void TriangulationTriangle::isInterior(bool b)
{
    m_Interior = b;
}

}

#endif