summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/gpu/LoopBlinnLocalTriangulator.cpp
blob: 1517a672f02c5fb7f69b116089f4ed98299cba2d (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
/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if ENABLE(ACCELERATED_2D_CANVAS)

#include "LoopBlinnLocalTriangulator.h"

#include "LoopBlinnMathUtils.h"
#include <algorithm>

namespace WebCore {

using LoopBlinnMathUtils::approxEqual;
using LoopBlinnMathUtils::linesIntersect;
using LoopBlinnMathUtils::pointInTriangle;

bool LoopBlinnLocalTriangulator::Triangle::contains(LoopBlinnLocalTriangulator::Vertex* v)
{
    return indexForVertex(v) >= 0;
}

LoopBlinnLocalTriangulator::Vertex* LoopBlinnLocalTriangulator::Triangle::nextVertex(LoopBlinnLocalTriangulator::Vertex* current, bool traverseCounterClockwise)
{
    int index = indexForVertex(current);
    ASSERT(index >= 0);
    if (traverseCounterClockwise)
        ++index;
    else
        --index;
    if (index < 0)
        index += 3;
    else
        index = index % 3;
    return m_vertices[index];
}

int LoopBlinnLocalTriangulator::Triangle::indexForVertex(LoopBlinnLocalTriangulator::Vertex* vertex)
{
    for (int i = 0; i < 3; ++i)
        if (m_vertices[i] == vertex)
            return i;
    return -1;
}

void LoopBlinnLocalTriangulator::Triangle::makeCounterClockwise()
{
    // Possibly swaps two vertices so that the triangle's vertices are
    // always specified in counterclockwise order. This orders the
    // vertices canonically when walking the interior edges from the
    // start to the end vertex.
    FloatPoint3D point0(m_vertices[0]->xyCoordinates());
    FloatPoint3D point1(m_vertices[1]->xyCoordinates());
    FloatPoint3D point2(m_vertices[2]->xyCoordinates());
    FloatPoint3D crossProduct = (point1 - point0).cross(point2 - point0);
    if (crossProduct.z() < 0)
        std::swap(m_vertices[1], m_vertices[2]);
}

LoopBlinnLocalTriangulator::LoopBlinnLocalTriangulator()
{
    reset();
}

void LoopBlinnLocalTriangulator::reset()
{
    m_numberOfTriangles = 0;
    m_numberOfInteriorVertices = 0;
    for (int i = 0; i < 4; ++i) {
        m_interiorVertices[i] = 0;
        m_vertices[i].resetFlags();
    }
}

void LoopBlinnLocalTriangulator::triangulate(InsideEdgeComputation computeInsideEdges, LoopBlinnConstants::FillSide sideToFill)
{
    triangulateHelper(sideToFill);

    if (computeInsideEdges == ComputeInsideEdges) {
        // We need to compute which vertices describe the path along the
        // interior portion of the shape, to feed these vertices to the
        // more general tessellation algorithm. It is possible that we
        // could determine this directly while producing triangles above.
        // Here we try to do it generally just by examining the triangles
        // that have already been produced. We walk around them in a
        // specific direction determined by which side of the curve is
        // being filled. We ignore the interior vertex unless it is also
        // the ending vertex, and skip the edges shared between two
        // triangles.
        Vertex* v = &m_vertices[0];
        addInteriorVertex(v);
        int numSteps = 0;
        while (!v->end() && numSteps < 4) {
            // Find the next vertex according to the above rules
            bool gotNext = false;
            for (int i = 0; i < numberOfTriangles() && !gotNext; ++i) {
                Triangle* tri = getTriangle(i);
                if (tri->contains(v)) {
                    Vertex* next = tri->nextVertex(v, sideToFill == LoopBlinnConstants::RightSide);
                    if (!next->marked() && !isSharedEdge(v, next) && (!next->interior() || next->end())) {
                        addInteriorVertex(next);
                        v = next;
                        // Break out of for loop
                        gotNext = true;
                    }
                }
            }
            ++numSteps;
        }
        if (!v->end()) {
            // Something went wrong with the above algorithm; add the last
            // vertex to the interior vertices anyway. (FIXME: should we
            // add an assert here and do more extensive testing?)
            addInteriorVertex(&m_vertices[3]);
        }
    }
}

void LoopBlinnLocalTriangulator::triangulateHelper(LoopBlinnConstants::FillSide sideToFill)
{
    reset();

    m_vertices[3].setEnd(true);

    // First test for degenerate cases.
    for (int i = 0; i < 4; ++i) {
        for (int j = i + 1; j < 4; ++j) {
            if (approxEqual(m_vertices[i].xyCoordinates(), m_vertices[j].xyCoordinates())) {
                // Two of the vertices are coincident, so we can eliminate at
                // least one triangle. We might be able to eliminate the other
                // as well, but this seems sufficient to avoid degenerate
                // triangulations.
                int indices[3] = { 0 };
                int index = 0;
                for (int k = 0; k < 4; ++k)
                    if (k != j)
                        indices[index++] = k;
                addTriangle(&m_vertices[indices[0]],
                            &m_vertices[indices[1]],
                            &m_vertices[indices[2]]);
                return;
            }
        }
    }

    // See whether any of the points are fully contained in the
    // triangle defined by the other three.
    for (int i = 0; i < 4; ++i) {
        int indices[3] = { 0 };
        int index = 0;
        for (int j = 0; j < 4; ++j)
            if (i != j)
                indices[index++] = j;
        if (pointInTriangle(m_vertices[i].xyCoordinates(),
                            m_vertices[indices[0]].xyCoordinates(),
                            m_vertices[indices[1]].xyCoordinates(),
                            m_vertices[indices[2]].xyCoordinates())) {
            // Produce three triangles surrounding this interior vertex.
            for (int j = 0; j < 3; ++j)
                addTriangle(&m_vertices[indices[j % 3]],
                            &m_vertices[indices[(j + 1) % 3]],
                            &m_vertices[i]);
            // Mark the interior vertex so we ignore it if trying to trace
            // the interior edge.
            m_vertices[i].setInterior(true);
            return;
        }
    }

    // There are only a few permutations of the vertices, ignoring
    // rotations, which are irrelevant:
    //
    //  0--3  0--2  0--3  0--1  0--2  0--1
    //  |  |  |  |  |  |  |  |  |  |  |  |
    //  |  |  |  |  |  |  |  |  |  |  |  |
    //  1--2  1--3  2--1  2--3  3--1  3--2
    //
    // Note that three of these are reflections of each other.
    // Therefore there are only three possible triangulations:
    //
    //  0--3  0--2  0--3
    //  |\ |  |\ |  |\ |
    //  | \|  | \|  | \|
    //  1--2  1--3  2--1
    //
    // From which we can choose by seeing which of the potential
    // diagonals intersect. Note that we choose the shortest diagonal
    // to split the quad.
    if (linesIntersect(m_vertices[0].xyCoordinates(),
                       m_vertices[2].xyCoordinates(),
                       m_vertices[1].xyCoordinates(),
                       m_vertices[3].xyCoordinates())) {
        if ((m_vertices[2].xyCoordinates() - m_vertices[0].xyCoordinates()).diagonalLengthSquared() <
            (m_vertices[3].xyCoordinates() - m_vertices[1].xyCoordinates()).diagonalLengthSquared()) {
            addTriangle(&m_vertices[0], &m_vertices[1], &m_vertices[2]);
            addTriangle(&m_vertices[0], &m_vertices[2], &m_vertices[3]);
        } else {
            addTriangle(&m_vertices[0], &m_vertices[1], &m_vertices[3]);
            addTriangle(&m_vertices[1], &m_vertices[2], &m_vertices[3]);
        }
    } else if (linesIntersect(m_vertices[0].xyCoordinates(),
                              m_vertices[3].xyCoordinates(),
                              m_vertices[1].xyCoordinates(),
                              m_vertices[2].xyCoordinates())) {
        if ((m_vertices[3].xyCoordinates() - m_vertices[0].xyCoordinates()).diagonalLengthSquared() <
            (m_vertices[2].xyCoordinates() - m_vertices[1].xyCoordinates()).diagonalLengthSquared()) {
            addTriangle(&m_vertices[0], &m_vertices[1], &m_vertices[3]);
            addTriangle(&m_vertices[0], &m_vertices[3], &m_vertices[2]);
        } else {
            addTriangle(&m_vertices[0], &m_vertices[1], &m_vertices[2]);
            addTriangle(&m_vertices[2], &m_vertices[1], &m_vertices[3]);
        }
    } else {
        // Lines (0->1), (2->3) intersect -- or should, modulo numerical
        // precision issues
        if ((m_vertices[1].xyCoordinates() - m_vertices[0].xyCoordinates()).diagonalLengthSquared() <
            (m_vertices[3].xyCoordinates() - m_vertices[2].xyCoordinates()).diagonalLengthSquared()) {
            addTriangle(&m_vertices[0], &m_vertices[2], &m_vertices[1]);
            addTriangle(&m_vertices[0], &m_vertices[1], &m_vertices[3]);
        } else {
            addTriangle(&m_vertices[0], &m_vertices[2], &m_vertices[3]);
            addTriangle(&m_vertices[3], &m_vertices[2], &m_vertices[1]);
        }
    }
}

void LoopBlinnLocalTriangulator::addTriangle(Vertex* v0, Vertex* v1, Vertex* v2)
{
    ASSERT(m_numberOfTriangles < 3);
    m_triangles[m_numberOfTriangles++].setVertices(v0, v1, v2);
}

void LoopBlinnLocalTriangulator::addInteriorVertex(Vertex* v)
{
    ASSERT(m_numberOfInteriorVertices < 4);
    m_interiorVertices[m_numberOfInteriorVertices++] = v;
    v->setMarked(true);
}

bool LoopBlinnLocalTriangulator::isSharedEdge(Vertex* v0, Vertex* v1)
{
    bool haveEdge01 = false;
    bool haveEdge10 = false;
    for (int i = 0; i < numberOfTriangles(); ++i) {
        Triangle* tri = getTriangle(i);
        if (tri->contains(v0) && tri->nextVertex(v0, true) == v1)
            haveEdge01 = true;
        if (tri->contains(v1) && tri->nextVertex(v1, true) == v0)
            haveEdge10 = true;
    }
    return haveEdge01 && haveEdge10;
}

} // namespace WebCore

#endif