Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/tests/gtest/PolygonTestUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "PolygonTestUtils.h"
8
9
#include <cmath>
10
11
typedef mozilla::gfx::Polygon MozPolygon;
12
13
namespace mozilla {
14
namespace gfx {
15
16
const float kEpsilon = 0.001f;
17
18
// Compares two points while allowing some numerical inaccuracy.
19
bool FuzzyEquals(const Point4D& lhs, const Point4D& rhs)
20
0
{
21
0
  const auto d = lhs - rhs;
22
0
23
0
  return std::abs(d.x) < kEpsilon &&
24
0
         std::abs(d.y) < kEpsilon &&
25
0
         std::abs(d.z) < kEpsilon &&
26
0
         std::abs(d.w) < kEpsilon;
27
0
}
28
29
bool FuzzyEquals(const Point3D& lhs, const Point3D& rhs)
30
0
{
31
0
  const auto d = lhs - rhs;
32
0
33
0
  return std::abs(d.x) < kEpsilon &&
34
0
         std::abs(d.y) < kEpsilon &&
35
0
         std::abs(d.z) < kEpsilon;
36
0
}
37
38
bool FuzzyEquals(const Point& lhs, const Point& rhs)
39
0
{
40
0
  const auto d = lhs - rhs;
41
0
42
0
  return std::abs(d.x) < kEpsilon &&
43
0
         std::abs(d.y) < kEpsilon;
44
0
}
45
46
bool operator==(const Triangle& lhs, const Triangle& rhs)
47
0
{
48
0
  return FuzzyEquals(lhs.p1, rhs.p1) &&
49
0
         FuzzyEquals(lhs.p2, rhs.p2) &&
50
0
         FuzzyEquals(lhs.p3, rhs.p3);
51
0
}
52
53
// Compares the points of two polygons and ensures
54
// that the points are in the same winding order.
55
bool operator==(const MozPolygon& lhs, const MozPolygon& rhs)
56
0
{
57
0
  const auto& left = lhs.GetPoints();
58
0
  const auto& right = rhs.GetPoints();
59
0
60
0
  // Polygons do not have the same amount of points.
61
0
  if (left.Length() != right.Length()) {
62
0
    return false;
63
0
  }
64
0
65
0
  const size_t pointCount = left.Length();
66
0
67
0
  // Find the first vertex of the first polygon from the second polygon.
68
0
  // This assumes that the polygons do not contain duplicate vertices.
69
0
  int start = -1;
70
0
  for (size_t i = 0; i < pointCount; ++i) {
71
0
    if (FuzzyEquals(left[0], right[i])) {
72
0
      start = i;
73
0
      break;
74
0
    }
75
0
  }
76
0
77
0
  // Found at least one different vertex.
78
0
  if (start == -1) {
79
0
    return false;
80
0
  }
81
0
82
0
  // Verify that the polygons have the same points.
83
0
  for (size_t i = 0; i < pointCount; ++i) {
84
0
    size_t j = (start + i) % pointCount;
85
0
86
0
    if (!FuzzyEquals(left[i], right[j])) {
87
0
      return false;
88
0
    }
89
0
  }
90
0
91
0
  return true;
92
0
}
93
94
} // namespace gfx
95
} // namespace mozilla
96
97
98
TEST(PolygonTestUtils, TestSanity)
99
0
{
100
0
  EXPECT_TRUE(FuzzyEquals(Point4D(0.0f, 0.0f, 0.0f, 1.0f),
101
0
                          Point4D(0.0f, 0.0f, 0.0f, 1.0f)));
102
0
103
0
  EXPECT_TRUE(FuzzyEquals(Point4D(0.0f, 0.0f, 0.0f, 1.0f),
104
0
                          Point4D(0.00001f, 0.00001f, 0.00001f, 1.0f)));
105
0
106
0
  EXPECT_TRUE(FuzzyEquals(Point4D(0.00001f, 0.00001f, 0.00001f, 1.0f),
107
0
                          Point4D(0.0f, 0.0f, 0.0f, 1.0f)));
108
0
109
0
  EXPECT_FALSE(FuzzyEquals(Point4D(0.0f, 0.0f, 0.0f, 1.0f),
110
0
                           Point4D(0.01f, 0.01f, 0.01f, 1.0f)));
111
0
112
0
  EXPECT_FALSE(FuzzyEquals(Point4D(0.01f, 0.01f, 0.01f, 1.0f),
113
0
                           Point4D(0.0f, 0.0f, 0.0f, 1.0f)));
114
0
115
0
  MozPolygon p1 {
116
0
    Point4D(0.0f, 0.0f, 1.0f, 1.0f),
117
0
    Point4D(1.0f, 0.0f, 1.0f, 1.0f),
118
0
    Point4D(1.0f, 1.0f, 1.0f, 1.0f),
119
0
    Point4D(0.0f, 1.0f, 1.0f, 1.0f)
120
0
  };
121
0
122
0
  // Same points as above shifted forward by one position.
123
0
  MozPolygon shifted {
124
0
    Point4D(0.0f, 1.0f, 1.0f, 1.0f),
125
0
    Point4D(0.0f, 0.0f, 1.0f, 1.0f),
126
0
    Point4D(1.0f, 0.0f, 1.0f, 1.0f),
127
0
    Point4D(1.0f, 1.0f, 1.0f, 1.0f)
128
0
  };
129
0
130
0
  MozPolygon p2 {
131
0
    Point4D(0.00001f, 0.00001f, 1.00001f, 1.0f),
132
0
    Point4D(1.00001f, 0.00001f, 1.00001f, 1.0f),
133
0
    Point4D(1.00001f, 1.00001f, 1.00001f, 1.0f),
134
0
    Point4D(0.00001f, 1.00001f, 1.00001f, 1.0f)
135
0
  };
136
0
137
0
  MozPolygon p3 {
138
0
    Point4D(0.01f, 0.01f, 1.01f, 1.0f),
139
0
    Point4D(1.01f, 0.01f, 1.01f, 1.0f),
140
0
    Point4D(1.01f, 1.01f, 1.01f, 1.0f),
141
0
    Point4D(0.01f, 1.01f, 1.01f, 1.0f)
142
0
  };
143
0
144
0
  // Trivial equals
145
0
  EXPECT_TRUE(p1 == p1);
146
0
  EXPECT_TRUE(p2 == p2);
147
0
  EXPECT_TRUE(p3 == p3);
148
0
  EXPECT_TRUE(shifted == shifted);
149
0
150
0
  // Polygons with the same point order
151
0
  EXPECT_TRUE(p1 == p2);
152
0
  EXPECT_TRUE(p1 == shifted);
153
0
154
0
  // Polygons containing different points
155
0
  EXPECT_FALSE(p1 == p3);
156
0
  EXPECT_FALSE(p2 == p3);
157
0
  EXPECT_FALSE(shifted == p3);
158
0
159
0
  const nsTArray<Triangle> t1 {
160
0
    Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(1.0f, 1.0f))
161
0
  };
162
0
163
0
  const nsTArray<Triangle> t2 {
164
0
    Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))
165
0
  };
166
0
167
0
  const nsTArray<Triangle> t3 {
168
0
    Triangle(
169
0
      Point(0.00001f, 0.00001f),
170
0
      Point(0.00001f, 1.00001f),
171
0
      Point(1.00001f, 1.00001f)
172
0
    )
173
0
  };
174
0
175
0
  EXPECT_TRUE(t1[0] == t1[0]);
176
0
  EXPECT_TRUE(t2[0] == t2[0]);
177
0
  EXPECT_TRUE(t3[0] == t1[0]);
178
0
179
0
  EXPECT_FALSE(t1[0] == t2[0]);
180
0
  EXPECT_FALSE(t2[0] == t3[0]);
181
0
182
0
  AssertArrayEQ(t1, t1);
183
0
  AssertArrayEQ(t2, t2);
184
0
}