2016-10-04 02:35:52 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "PolygonTestUtils.h"
|
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "Point.h"
|
|
|
|
#include "Polygon.h"
|
|
|
|
#include "Triangle.h"
|
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
2017-02-05 20:50:00 +03:00
|
|
|
typedef mozilla::gfx::Polygon MozPolygon;
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
TEST(MozPolygon, TriangulateRectangle)
|
2016-10-04 02:35:52 +03:00
|
|
|
{
|
2017-02-05 20:50:00 +03:00
|
|
|
const MozPolygon p {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.0f, 0.0f, 1.0f, 1.0f),
|
|
|
|
Point4D(0.0f, 1.0f, 1.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 1.0f, 1.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 0.0f, 1.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
|
|
|
|
2016-10-07 20:58:13 +03:00
|
|
|
const nsTArray<Triangle> triangles = p.ToTriangles();
|
2016-10-04 02:35:52 +03:00
|
|
|
const nsTArray<Triangle> expected = {
|
|
|
|
Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(1.0f, 1.0f)),
|
|
|
|
Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))
|
|
|
|
};
|
|
|
|
|
|
|
|
AssertArrayEQ(triangles, expected);
|
|
|
|
}
|
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
TEST(MozPolygon, TriangulatePentagon)
|
2016-10-04 02:35:52 +03:00
|
|
|
{
|
2017-02-05 20:50:00 +03:00
|
|
|
const MozPolygon p {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.0f, 0.0f, 1.0f, 1.0f),
|
|
|
|
Point4D(0.0f, 1.0f, 1.0f, 1.0f),
|
|
|
|
Point4D(0.5f, 1.5f, 1.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 1.0f, 1.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 0.0f, 1.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
|
|
|
|
2016-10-07 20:58:13 +03:00
|
|
|
const nsTArray<Triangle> triangles = p.ToTriangles();
|
2016-10-04 02:35:52 +03:00
|
|
|
const nsTArray<Triangle> expected = {
|
|
|
|
Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(0.5f, 1.5f)),
|
|
|
|
Triangle(Point(0.0f, 0.0f), Point(0.5f, 1.5f), Point(1.0f, 1.0f)),
|
|
|
|
Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))
|
|
|
|
};
|
|
|
|
|
|
|
|
AssertArrayEQ(triangles, expected);
|
|
|
|
}
|
|
|
|
|
2016-12-04 19:49:32 +03:00
|
|
|
void
|
2017-02-05 20:50:00 +03:00
|
|
|
TestClipRect(const MozPolygon& aPolygon,
|
|
|
|
const MozPolygon& aExpected,
|
2016-12-04 19:49:32 +03:00
|
|
|
const Rect& aRect)
|
2016-10-04 02:35:52 +03:00
|
|
|
{
|
2017-02-05 20:50:00 +03:00
|
|
|
const MozPolygon res = aPolygon.ClipPolygon(MozPolygon::FromRect(aRect));
|
2016-12-04 19:49:32 +03:00
|
|
|
EXPECT_TRUE(res == aExpected);
|
|
|
|
}
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
TEST(MozPolygon, ClipRectangle)
|
2016-12-04 19:49:32 +03:00
|
|
|
{
|
2017-02-05 20:50:00 +03:00
|
|
|
MozPolygon polygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.0f, 0.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.0f, 1.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 1.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 0.0f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, polygon, Rect(0.0f, 0.0f, 1.0f, 1.0f));
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
MozPolygon expected = MozPolygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.0f, 0.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.0f, 0.8f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.8f, 0.8f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.8f, 0.0f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 0.8f, 0.8f));
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
expected = MozPolygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.2f, 0.2f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.2f, 1.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 1.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 0.2f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.8f, 0.8f));
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
expected = MozPolygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.2f, 0.2f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.2f, 0.8f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.8f, 0.8f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.8f, 0.2f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.6f, 0.6f));
|
2016-10-04 02:35:52 +03:00
|
|
|
}
|
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
TEST(MozPolygon, ClipTriangle)
|
2016-10-04 02:35:52 +03:00
|
|
|
{
|
2017-02-05 20:50:00 +03:00
|
|
|
MozPolygon clipped, expected;
|
|
|
|
const MozPolygon polygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.0f, 0.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.0f, 1.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 1.0f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
expected = MozPolygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.0f, 0.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.0f, 1.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 1.0f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 1.0f, 1.0f));
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
expected = MozPolygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.0f, 0.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.0f, 0.8f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.8f, 0.8f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, expected, Rect(0.0f, 0.0f, 0.8f, 0.8f));
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
expected = MozPolygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.2f, 0.2f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.2f, 1.0f, 0.0f, 1.0f),
|
|
|
|
Point4D(1.0f, 1.0f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.8f, 0.8f));
|
2016-10-04 02:35:52 +03:00
|
|
|
|
2017-02-05 20:50:00 +03:00
|
|
|
expected = MozPolygon {
|
2017-04-04 04:15:48 +03:00
|
|
|
Point4D(0.2f, 0.2f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.2f, 0.8f, 0.0f, 1.0f),
|
|
|
|
Point4D(0.8f, 0.8f, 0.0f, 1.0f)
|
2016-10-04 02:35:52 +03:00
|
|
|
};
|
2016-12-04 19:49:32 +03:00
|
|
|
TestClipRect(polygon, expected, Rect(0.2f, 0.2f, 0.6f, 0.6f));
|
2017-02-13 19:51:00 +03:00
|
|
|
}
|