summaryrefslogtreecommitdiffstats
path: root/gfx/2d/Polygon.h
blob: 3de3d684f9d3ac97762ca2677bcec79c6f1955e0 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MOZILLA_GFX_POLYGON_H
#define MOZILLA_GFX_POLYGON_H

#include <initializer_list>
#include <utility>

#include "Matrix.h"
#include "Point.h"
#include "Triangle.h"
#include "nsTArray.h"

namespace mozilla {
namespace gfx {

/**
 * Calculates the w = 0 intersection point for the edge defined by
 * |aFirst| and |aSecond|.
 */
template <class Units>
Point4DTyped<Units> CalculateEdgeIntersect(const Point4DTyped<Units>& aFirst,
                                           const Point4DTyped<Units>& aSecond) {
  static const float w = 0.00001f;
  const float t = (w - aFirst.w) / (aSecond.w - aFirst.w);
  return aFirst + (aSecond - aFirst) * t;
}

/**
 * Clips the polygon defined by |aPoints| so that there are no points with
 * w <= 0.
 */
template <class Units>
nsTArray<Point4DTyped<Units>> ClipPointsAtInfinity(
    const nsTArray<Point4DTyped<Units>>& aPoints) {
  nsTArray<Point4DTyped<Units>> outPoints(aPoints.Length());

  const size_t pointCount = aPoints.Length();
  for (size_t i = 0; i < pointCount; ++i) {
    const Point4DTyped<Units>& first = aPoints[i];
    const Point4DTyped<Units>& second = aPoints[(i + 1) % pointCount];

    if (!first.w || !second.w) {
      // Skip edges at infinity.
      continue;
    }

    if (first.w > 0.0f) {
      outPoints.AppendElement(first);
    }

    if ((first.w <= 0.0f) ^ (second.w <= 0.0f)) {
      outPoints.AppendElement(CalculateEdgeIntersect(first, second));
    }
  }

  return outPoints;
}

/**
 * Calculates the distances between the points in |aPoints| and the plane
 * defined by |aPlaneNormal| and |aPlanePoint|.
 */
template <class Units>
nsTArray<float> CalculatePointPlaneDistances(
    const nsTArray<Point4DTyped<Units>>& aPoints,
    const Point4DTyped<Units>& aPlaneNormal,
    const Point4DTyped<Units>& aPlanePoint, size_t& aPos, size_t& aNeg) {
  // Point classification might produce incorrect results due to numerical
  // inaccuracies. Using an epsilon value makes the splitting plane "thicker".
  const float epsilon = 0.05f;

  aPos = aNeg = 0;
  nsTArray<float> distances(aPoints.Length());

  for (const Point4DTyped<Units>& point : aPoints) {
    float dot = (point - aPlanePoint).DotProduct(aPlaneNormal);

    if (dot > epsilon) {
      aPos++;
    } else if (dot < -epsilon) {
      aNeg++;
    } else {
      // The point is within the thick plane.
      dot = 0.0f;
    }

    distances.AppendElement(dot);
  }

  return distances;
}

/**
 * Clips the polygon defined by |aPoints|. The clipping uses previously
 * calculated plane to point distances and the plane normal |aNormal|.
 * The result of clipping is stored in |aBackPoints| and |aFrontPoints|.
 */
template <class Units>
void ClipPointsWithPlane(const nsTArray<Point4DTyped<Units>>& aPoints,
                         const Point4DTyped<Units>& aNormal,
                         const nsTArray<float>& aDots,
                         nsTArray<Point4DTyped<Units>>& aBackPoints,
                         nsTArray<Point4DTyped<Units>>& aFrontPoints) {
  static const auto Sign = [](const float& f) {
    if (f > 0.0f) return 1;
    if (f < 0.0f) return -1;
    return 0;
  };

  const size_t pointCount = aPoints.Length();
  for (size_t i = 0; i < pointCount; ++i) {
    size_t j = (i + 1) % pointCount;

    const Point4DTyped<Units>& a = aPoints[i];
    const Point4DTyped<Units>& b = aPoints[j];
    const float dotA = aDots[i];
    const float dotB = aDots[j];

    // The point is in front of or on the plane.
    if (dotA >= 0) {
      aFrontPoints.AppendElement(a);
    }

    // The point is behind or on the plane.
    if (dotA <= 0) {
      aBackPoints.AppendElement(a);
    }

    // If the sign of the dot products changes between two consecutive
    // vertices, then the plane intersects with the polygon edge.
    // The case where the polygon edge is within the plane is handled above.
    if (Sign(dotA) && Sign(dotB) && Sign(dotA) != Sign(dotB)) {
      // Calculate the line segment and plane intersection point.
      const Point4DTyped<Units> ab = b - a;
      const float dotAB = ab.DotProduct(aNormal);
      const float t = -dotA / dotAB;
      const Point4DTyped<Units> p = a + (ab * t);

      // Add the intersection point to both polygons.
      aBackPoints.AppendElement(p);
      aFrontPoints.AppendElement(p);
    }
  }
}

/**
 * PolygonTyped stores the points of a convex planar polygon.
 */
template <class Units>
class PolygonTyped {
  typedef Point3DTyped<Units> Point3DType;
  typedef Point4DTyped<Units> Point4DType;

 public:
  PolygonTyped() = default;

  explicit PolygonTyped(const nsTArray<Point4DType>& aPoints,
                        const Point4DType& aNormal = DefaultNormal())
      : mNormal(aNormal), mPoints(aPoints) {}

  explicit PolygonTyped(nsTArray<Point4DType>&& aPoints,
                        const Point4DType& aNormal = DefaultNormal())
      : mNormal(aNormal), mPoints(std::move(aPoints)) {}

  explicit PolygonTyped(const std::initializer_list<Point4DType>& aPoints,
                        const Point4DType& aNormal = DefaultNormal())
      : mNormal(aNormal), mPoints(aPoints) {
#ifdef DEBUG
    EnsurePlanarPolygon();
#endif
  }

  /**
   * Returns the smallest 2D rectangle that can fully cover the polygon.
   */
  RectTyped<Units> BoundingBox() const {
    if (mPoints.IsEmpty()) {
      return RectTyped<Units>();
    }

    float minX, maxX, minY, maxY;
    minX = maxX = mPoints[0].x;
    minY = maxY = mPoints[0].y;

    for (const Point4DType& point : mPoints) {
      minX = std::min(point.x, minX);
      maxX = std::max(point.x, maxX);

      minY = std::min(point.y, minY);
      maxY = std::max(point.y, maxY);
    }

    return RectTyped<Units>(minX, minY, maxX - minX, maxY - minY);
  }

  /**
   * Clips the polygon against the given 2D rectangle.
   */
  PolygonTyped<Units> ClipPolygon(const RectTyped<Units>& aRect) const {
    if (aRect.IsEmpty()) {
      return PolygonTyped<Units>();
    }

    return ClipPolygon(FromRect(aRect));
  }

  /**
   * Clips this polygon against |aPolygon| in 2D and returns a new polygon.
   */
  PolygonTyped<Units> ClipPolygon(const PolygonTyped<Units>& aPolygon) const {
    const nsTArray<Point4DType>& points = aPolygon.GetPoints();

    if (mPoints.IsEmpty() || points.IsEmpty()) {
      return PolygonTyped<Units>();
    }

    nsTArray<Point4DType> clippedPoints(mPoints.Clone());

    size_t pos, neg;
    nsTArray<Point4DType> backPoints(4), frontPoints(4);

    // Iterate over all the edges of the clipping polygon |aPolygon| and clip
    // this polygon against the edges.
    const size_t pointCount = points.Length();
    for (size_t i = 0; i < pointCount; ++i) {
      const Point4DType p1 = points[(i + 1) % pointCount];
      const Point4DType p2 = points[i];

      // Calculate the normal for the edge defined by |p1| and |p2|.
      const Point4DType normal(p2.y - p1.y, p1.x - p2.x, 0.0f, 0.0f);

      // Calculate the distances between the points of the polygon and the
      // plane defined by |aPolygon|.
      const nsTArray<float> distances =
          CalculatePointPlaneDistances(clippedPoints, normal, p1, pos, neg);

      backPoints.ClearAndRetainStorage();
      frontPoints.ClearAndRetainStorage();

      // Clip the polygon points using the previously calculated distances.
      ClipPointsWithPlane(clippedPoints, normal, distances, backPoints,
                          frontPoints);

      // Only use the points behind the clipping plane.
      clippedPoints = std::move(backPoints);

      if (clippedPoints.Length() < 3) {
        // The clipping created a polygon with no area.
        return PolygonTyped<Units>();
      }
    }

    return PolygonTyped<Units>(std::move(clippedPoints), mNormal);
  }

  /**
   * Returns a new polygon containing the bounds of the given 2D rectangle.
   */
  static PolygonTyped<Units> FromRect(const RectTyped<Units>& aRect) {
    nsTArray<Point4DType> points{
        Point4DType(aRect.X(), aRect.Y(), 0.0f, 1.0f),
        Point4DType(aRect.X(), aRect.YMost(), 0.0f, 1.0f),
        Point4DType(aRect.XMost(), aRect.YMost(), 0.0f, 1.0f),
        Point4DType(aRect.XMost(), aRect.Y(), 0.0f, 1.0f)};

    return PolygonTyped<Units>(std::move(points));
  }

  const Point4DType& GetNormal() const { return mNormal; }

  const nsTArray<Point4DType>& GetPoints() const { return mPoints; }

  bool IsEmpty() const {
    // If the polygon has less than three points, it has no visible area.
    return mPoints.Length() < 3;
  }

  /**
   * Returns a list of triangles covering the polygon.
   */
  nsTArray<TriangleTyped<Units>> ToTriangles() const {
    nsTArray<TriangleTyped<Units>> triangles;

    if (IsEmpty()) {
      return triangles;
    }

    // This fan triangulation method only works for convex polygons.
    for (size_t i = 1; i < mPoints.Length() - 1; ++i) {
      TriangleTyped<Units> triangle(Point(mPoints[0].x, mPoints[0].y),
                                    Point(mPoints[i].x, mPoints[i].y),
                                    Point(mPoints[i + 1].x, mPoints[i + 1].y));
      triangles.AppendElement(std::move(triangle));
    }

    return triangles;
  }

  void TransformToLayerSpace(const Matrix4x4Typed<Units, Units>& aTransform) {
    TransformPoints(aTransform, true);
    mNormal = DefaultNormal();
  }

  void TransformToScreenSpace(
      const Matrix4x4Typed<Units, Units>& aTransform,
      const Matrix4x4Typed<Units, Units>& aInverseTransform) {
    TransformPoints(aTransform, false);

    // Perspective projection transformation might produce points with w <= 0,
    // so we need to clip these points.
    mPoints = ClipPointsAtInfinity(mPoints);

    // Normal vectors should be transformed using inverse transpose.
    mNormal = aInverseTransform.TransposeTransform4D(mNormal);
  }

  void TransformToScreenSpace(const Matrix4x4Typed<Units, Units>& aTransform) {
    MOZ_ASSERT(!aTransform.IsSingular());

    TransformToScreenSpace(aTransform, aTransform.Inverse());
  }

 private:
  static Point4DType DefaultNormal() {
    return Point4DType(0.0f, 0.0f, 1.0f, 0.0f);
  }

#ifdef DEBUG
  void EnsurePlanarPolygon() const {
    if (mPoints.Length() <= 3) {
      // Polygons with three or less points are guaranteed to be planar.
      return;
    }

    // This normal calculation method works only for planar polygons.
    // The resulting normal vector will point towards the viewer when the
    // polygon has a counter-clockwise winding order from the perspective
    // of the viewer.
    Point3DType normal;
    const Point3DType p0 = mPoints[0].As3DPoint();

    for (size_t i = 1; i < mPoints.Length() - 1; ++i) {
      const Point3DType p1 = mPoints[i].As3DPoint();
      const Point3DType p2 = mPoints[i + 1].As3DPoint();

      normal += (p1 - p0).CrossProduct(p2 - p0);
    }

    // Ensure that at least one component is greater than zero.
    // This avoids division by zero when normalizing the vector.
    bool hasNonZeroComponent = std::abs(normal.x) > 0.0f ||
                               std::abs(normal.y) > 0.0f ||
                               std::abs(normal.z) > 0.0f;

    MOZ_ASSERT(hasNonZeroComponent);

    normal.Normalize();

    // Ensure that the polygon is planar.
    // http://mathworld.wolfram.com/Point-PlaneDistance.html
    const float epsilon = 0.01f;
    for (const Point4DType& point : mPoints) {
      const Point3DType p1 = point.As3DPoint();
      const float d = normal.DotProduct(p1 - p0);

      MOZ_ASSERT(std::abs(d) < epsilon);
    }
  }
#endif

  void TransformPoints(const Matrix4x4Typed<Units, Units>& aTransform,
                       const bool aDivideByW) {
    for (Point4DType& point : mPoints) {
      point = aTransform.TransformPoint(point);

      if (aDivideByW && point.w > 0.0f) {
        point = point / point.w;
      }
    }
  }

  Point4DType mNormal;
  CopyableTArray<Point4DType> mPoints;
};

typedef PolygonTyped<UnknownUnits> Polygon;

}  // namespace gfx
}  // namespace mozilla

#endif /* MOZILLA_GFX_POLYGON_H */