2017-10-28 02:10:06 +03:00
|
|
|
/* -*- 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
|
2016-08-09 21:24:18 +03:00
|
|
|
* 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
|
|
|
|
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <initializer_list>
|
2020-01-20 19:18:20 +03:00
|
|
|
#include <utility>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
|
|
|
|
#include "Matrix.h"
|
2016-08-09 21:24:18 +03:00
|
|
|
#include "Point.h"
|
2016-10-04 02:35:52 +03:00
|
|
|
#include "Triangle.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "nsTArray.h"
|
2016-10-19 07:33:14 +03:00
|
|
|
|
2016-08-09 21:24:18 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Calculates the w = 0 intersection point for the edge defined by
|
|
|
|
* |aFirst| and |aSecond|.
|
|
|
|
*/
|
2016-08-09 21:24:18 +03:00
|
|
|
template <class Units>
|
2016-12-04 19:49:32 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
/**
|
|
|
|
* Clips the polygon defined by |aPoints| so that there are no points with
|
|
|
|
* w <= 0.
|
|
|
|
*/
|
2016-12-04 19:49:32 +03:00
|
|
|
template <class Units>
|
2017-04-04 05:51:29 +03:00
|
|
|
nsTArray<Point4DTyped<Units>> ClipPointsAtInfinity(
|
|
|
|
const nsTArray<Point4DTyped<Units>>& aPoints) {
|
|
|
|
nsTArray<Point4DTyped<Units>> outPoints(aPoints.Length());
|
2016-12-04 19:49:32 +03:00
|
|
|
|
|
|
|
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];
|
|
|
|
|
2017-02-23 22:42:10 +03:00
|
|
|
if (!first.w || !second.w) {
|
|
|
|
// Skip edges at infinity.
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-04 19:49:32 +03:00
|
|
|
|
|
|
|
if (first.w > 0.0f) {
|
|
|
|
outPoints.AppendElement(first);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((first.w <= 0.0f) ^ (second.w <= 0.0f)) {
|
|
|
|
outPoints.AppendElement(CalculateEdgeIntersect(first, second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outPoints;
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Calculates the distances between the points in |aPoints| and the plane
|
|
|
|
* defined by |aPlaneNormal| and |aPlanePoint|.
|
|
|
|
*/
|
2016-12-04 19:49:32 +03:00
|
|
|
template <class Units>
|
2017-04-04 05:51:29 +03:00
|
|
|
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;
|
|
|
|
}
|
2016-12-04 19:49:32 +03:00
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
distances.AppendElement(dot);
|
2016-12-04 19:49:32 +03:00
|
|
|
}
|
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-12-04 19:49:32 +03:00
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* PolygonTyped stores the points of a convex planar polygon.
|
|
|
|
*/
|
2016-12-04 19:49:32 +03:00
|
|
|
template <class Units>
|
|
|
|
class PolygonTyped {
|
|
|
|
typedef Point3DTyped<Units> Point3DType;
|
|
|
|
typedef Point4DTyped<Units> Point4DType;
|
|
|
|
|
2016-08-09 21:24:18 +03:00
|
|
|
public:
|
2019-04-11 15:36:51 +03:00
|
|
|
PolygonTyped() = default;
|
2016-09-17 02:03:32 +03:00
|
|
|
|
2017-04-04 04:15:48 +03:00
|
|
|
explicit PolygonTyped(const nsTArray<Point4DType>& aPoints,
|
|
|
|
const Point4DType& aNormal = DefaultNormal())
|
|
|
|
: mNormal(aNormal), mPoints(aPoints) {}
|
2017-04-04 04:15:48 +03:00
|
|
|
|
2017-04-04 04:15:48 +03:00
|
|
|
explicit PolygonTyped(nsTArray<Point4DType>&& aPoints,
|
|
|
|
const Point4DType& aNormal = DefaultNormal())
|
2018-05-30 22:15:35 +03:00
|
|
|
: mNormal(aNormal), mPoints(std::move(aPoints)) {}
|
2017-04-04 04:15:48 +03:00
|
|
|
|
|
|
|
explicit PolygonTyped(const std::initializer_list<Point4DType>& aPoints,
|
|
|
|
const Point4DType& aNormal = DefaultNormal())
|
|
|
|
: mNormal(aNormal), mPoints(aPoints) {
|
2017-04-04 05:51:29 +03:00
|
|
|
#ifdef DEBUG
|
2016-10-04 02:35:52 +03:00
|
|
|
EnsurePlanarPolygon();
|
2017-04-04 05:51:29 +03:00
|
|
|
#endif
|
2016-10-04 02:35:52 +03:00
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Returns the smallest 2D rectangle that can fully cover the polygon.
|
|
|
|
*/
|
2016-10-04 02:35:52 +03:00
|
|
|
RectTyped<Units> BoundingBox() const {
|
2016-12-04 19:49:32 +03:00
|
|
|
if (mPoints.IsEmpty()) {
|
|
|
|
return RectTyped<Units>();
|
|
|
|
}
|
|
|
|
|
2016-10-04 02:35:52 +03:00
|
|
|
float minX, maxX, minY, maxY;
|
|
|
|
minX = maxX = mPoints[0].x;
|
|
|
|
minY = maxY = mPoints[0].y;
|
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
for (const Point4DType& point : mPoints) {
|
2016-10-04 02:35:52 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Clips the polygon against the given 2D rectangle.
|
|
|
|
*/
|
2016-12-04 19:49:32 +03:00
|
|
|
PolygonTyped<Units> ClipPolygon(const RectTyped<Units>& aRect) const {
|
|
|
|
if (aRect.IsEmpty()) {
|
|
|
|
return PolygonTyped<Units>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ClipPolygon(FromRect(aRect));
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Clips this polygon against |aPolygon| in 2D and returns a new polygon.
|
|
|
|
*/
|
2016-12-04 19:49:32 +03:00
|
|
|
PolygonTyped<Units> ClipPolygon(const PolygonTyped<Units>& aPolygon) const {
|
|
|
|
const nsTArray<Point4DType>& points = aPolygon.GetPoints();
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
if (mPoints.IsEmpty() || points.IsEmpty()) {
|
|
|
|
return PolygonTyped<Units>();
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:55:27 +03:00
|
|
|
nsTArray<Point4DType> clippedPoints(mPoints.Clone());
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
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.
|
2016-12-04 19:49:32 +03:00
|
|
|
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];
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
// Calculate the normal for the edge defined by |p1| and |p2|.
|
2016-12-04 19:49:32 +03:00
|
|
|
const Point4DType normal(p2.y - p1.y, p1.x - p2.x, 0.0f, 0.0f);
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
// 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);
|
2016-12-04 19:49:32 +03:00
|
|
|
|
2017-04-04 05:51:29 +03:00
|
|
|
// Only use the points behind the clipping plane.
|
2018-05-30 22:15:35 +03:00
|
|
|
clippedPoints = std::move(backPoints);
|
2017-04-04 05:51:29 +03:00
|
|
|
|
|
|
|
if (clippedPoints.Length() < 3) {
|
2017-01-18 14:10:12 +03:00
|
|
|
// The clipping created a polygon with no area.
|
|
|
|
return PolygonTyped<Units>();
|
|
|
|
}
|
2016-12-04 19:49:32 +03:00
|
|
|
}
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
return PolygonTyped<Units>(std::move(clippedPoints), mNormal);
|
2016-10-04 02:35:52 +03:00
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Returns a new polygon containing the bounds of the given 2D rectangle.
|
|
|
|
*/
|
2016-12-04 19:49:32 +03:00
|
|
|
static PolygonTyped<Units> FromRect(const RectTyped<Units>& aRect) {
|
2017-04-04 04:15:48 +03:00
|
|
|
nsTArray<Point4DType> points{
|
2017-12-19 23:48:39 +03:00
|
|
|
Point4DType(aRect.X(), aRect.Y(), 0.0f, 1.0f),
|
|
|
|
Point4DType(aRect.X(), aRect.YMost(), 0.0f, 1.0f),
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4DType(aRect.XMost(), aRect.YMost(), 0.0f, 1.0f),
|
2017-12-19 23:48:39 +03:00
|
|
|
Point4DType(aRect.XMost(), aRect.Y(), 0.0f, 1.0f)};
|
2017-04-04 04:15:48 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
return PolygonTyped<Units>(std::move(points));
|
2016-12-04 19:49:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const Point4DType& GetNormal() const { return mNormal; }
|
2016-08-09 21:24:18 +03:00
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
const nsTArray<Point4DType>& GetPoints() const { return mPoints; }
|
2016-08-09 21:24:18 +03:00
|
|
|
|
2017-01-18 14:10:12 +03:00
|
|
|
bool IsEmpty() const {
|
|
|
|
// If the polygon has less than three points, it has no visible area.
|
|
|
|
return mPoints.Length() < 3;
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
/**
|
|
|
|
* Returns a list of triangles covering the polygon.
|
|
|
|
*/
|
2016-10-07 20:58:13 +03:00
|
|
|
nsTArray<TriangleTyped<Units>> ToTriangles() const {
|
2016-10-04 02:35:52 +03:00
|
|
|
nsTArray<TriangleTyped<Units>> triangles;
|
|
|
|
|
2017-01-18 14:10:12 +03:00
|
|
|
if (IsEmpty()) {
|
2016-10-04 02:35:52 +03:00
|
|
|
return triangles;
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:59:31 +03:00
|
|
|
// This fan triangulation method only works for convex polygons.
|
2016-10-04 02:35:52 +03:00
|
|
|
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),
|
2017-04-04 04:59:31 +03:00
|
|
|
Point(mPoints[i + 1].x, mPoints[i + 1].y));
|
2018-05-30 22:15:35 +03:00
|
|
|
triangles.AppendElement(std::move(triangle));
|
2016-10-04 02:35:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return triangles;
|
|
|
|
}
|
|
|
|
|
2016-10-07 20:58:13 +03:00
|
|
|
void TransformToLayerSpace(const Matrix4x4Typed<Units, Units>& aTransform) {
|
2016-12-04 19:49:32 +03:00
|
|
|
TransformPoints(aTransform, true);
|
|
|
|
mNormal = DefaultNormal();
|
2016-10-07 20:58:13 +03:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:52:42 +03:00
|
|
|
void TransformToScreenSpace(
|
|
|
|
const Matrix4x4Typed<Units, Units>& aTransform,
|
|
|
|
const Matrix4x4Typed<Units, Units>& aInverseTransform) {
|
2016-12-04 19:49:32 +03:00
|
|
|
TransformPoints(aTransform, false);
|
2017-04-04 05:51:29 +03:00
|
|
|
|
|
|
|
// Perspective projection transformation might produce points with w <= 0,
|
|
|
|
// so we need to clip these points.
|
|
|
|
mPoints = ClipPointsAtInfinity(mPoints);
|
2016-10-07 20:58:13 +03:00
|
|
|
|
|
|
|
// Normal vectors should be transformed using inverse transpose.
|
2018-07-09 22:52:42 +03:00
|
|
|
mNormal = aInverseTransform.TransposeTransform4D(mNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformToScreenSpace(const Matrix4x4Typed<Units, Units>& aTransform) {
|
|
|
|
MOZ_ASSERT(!aTransform.IsSingular());
|
|
|
|
|
|
|
|
TransformToScreenSpace(aTransform, aTransform.Inverse());
|
2016-10-07 20:58:13 +03:00
|
|
|
}
|
|
|
|
|
2016-08-09 21:24:18 +03:00
|
|
|
private:
|
2016-12-04 19:49:32 +03:00
|
|
|
static Point4DType DefaultNormal() {
|
|
|
|
return Point4DType(0.0f, 0.0f, 1.0f, 0.0f);
|
2016-10-04 02:35:52 +03:00
|
|
|
}
|
2016-09-17 02:03:32 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void EnsurePlanarPolygon() const {
|
2016-10-04 02:35:52 +03:00
|
|
|
if (mPoints.Length() <= 3) {
|
|
|
|
// Polygons with three or less points are guaranteed to be planar.
|
|
|
|
return;
|
|
|
|
}
|
2016-08-09 21:24:18 +03:00
|
|
|
|
|
|
|
// This normal calculation method works only for planar polygons.
|
2016-10-04 02:35:52 +03:00
|
|
|
// The resulting normal vector will point towards the viewer when the
|
|
|
|
// polygon has a counter-clockwise winding order from the perspective
|
|
|
|
// of the viewer.
|
2016-12-04 19:49:32 +03:00
|
|
|
Point3DType normal;
|
|
|
|
const Point3DType p0 = mPoints[0].As3DPoint();
|
2016-09-17 02:03:32 +03:00
|
|
|
|
|
|
|
for (size_t i = 1; i < mPoints.Length() - 1; ++i) {
|
2016-12-04 19:49:32 +03:00
|
|
|
const Point3DType p1 = mPoints[i].As3DPoint();
|
|
|
|
const Point3DType p2 = mPoints[i + 1].As3DPoint();
|
|
|
|
|
|
|
|
normal += (p1 - p0).CrossProduct(p2 - p0);
|
2016-08-09 21:24:18 +03:00
|
|
|
}
|
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
// 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;
|
2016-12-04 19:49:32 +03:00
|
|
|
|
2016-09-17 02:03:32 +03:00
|
|
|
MOZ_ASSERT(hasNonZeroComponent);
|
|
|
|
|
|
|
|
normal.Normalize();
|
2016-08-09 21:24:18 +03:00
|
|
|
|
|
|
|
// Ensure that the polygon is planar.
|
|
|
|
// http://mathworld.wolfram.com/Point-PlaneDistance.html
|
2016-09-17 02:03:32 +03:00
|
|
|
const float epsilon = 0.01f;
|
2016-12-04 19:49:32 +03:00
|
|
|
for (const Point4DType& point : mPoints) {
|
|
|
|
const Point3DType p1 = point.As3DPoint();
|
|
|
|
const float d = normal.DotProduct(p1 - p0);
|
|
|
|
|
2016-08-09 21:24:18 +03:00
|
|
|
MOZ_ASSERT(std::abs(d) < epsilon);
|
|
|
|
}
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
|
|
|
#endif
|
2016-12-04 19:49:32 +03:00
|
|
|
|
|
|
|
void TransformPoints(const Matrix4x4Typed<Units, Units>& aTransform,
|
|
|
|
const bool aDivideByW) {
|
|
|
|
for (Point4DType& point : mPoints) {
|
2016-09-17 02:03:32 +03:00
|
|
|
point = aTransform.TransformPoint(point);
|
2016-12-04 19:49:32 +03:00
|
|
|
|
|
|
|
if (aDivideByW && point.w > 0.0f) {
|
|
|
|
point = point / point.w;
|
|
|
|
}
|
2016-09-17 02:03:32 +03:00
|
|
|
}
|
2016-08-09 21:24:18 +03:00
|
|
|
}
|
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
Point4DType mNormal;
|
2020-05-05 15:55:27 +03:00
|
|
|
CopyableTArray<Point4DType> mPoints;
|
2016-08-09 21:24:18 +03:00
|
|
|
};
|
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
typedef PolygonTyped<UnknownUnits> Polygon;
|
2016-08-09 21:24:18 +03:00
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-08-16 21:01:06 +03:00
|
|
|
#endif /* MOZILLA_GFX_POLYGON_H */
|