tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PolygonTestUtils.cpp (4932B)


      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 #include "Point.h"
     12 #include "Triangle.h"
     13 
     14 typedef mozilla::gfx::Polygon MozPolygon;
     15 
     16 using mozilla::gfx::Point;
     17 using mozilla::gfx::Point4D;
     18 using mozilla::gfx::Triangle;
     19 
     20 namespace mozilla {
     21 namespace gfx {
     22 
     23 const float kEpsilon = 0.001f;
     24 
     25 // Compares two points while allowing some numerical inaccuracy.
     26 bool FuzzyEquals(const Point4D& lhs, const Point4D& rhs) {
     27  const auto d = lhs - rhs;
     28 
     29  return std::abs(d.x) < kEpsilon && std::abs(d.y) < kEpsilon &&
     30         std::abs(d.z) < kEpsilon && std::abs(d.w) < kEpsilon;
     31 }
     32 
     33 bool FuzzyEquals(const Point3D& lhs, const Point3D& rhs) {
     34  const auto d = lhs - rhs;
     35 
     36  return std::abs(d.x) < kEpsilon && std::abs(d.y) < kEpsilon &&
     37         std::abs(d.z) < kEpsilon;
     38 }
     39 
     40 bool FuzzyEquals(const Point& lhs, const Point& rhs) {
     41  const auto d = lhs - rhs;
     42 
     43  return std::abs(d.x) < kEpsilon && std::abs(d.y) < kEpsilon;
     44 }
     45 
     46 bool operator==(const Triangle& lhs, const Triangle& rhs) {
     47  return FuzzyEquals(lhs.p1, rhs.p1) && FuzzyEquals(lhs.p2, rhs.p2) &&
     48         FuzzyEquals(lhs.p3, rhs.p3);
     49 }
     50 
     51 // Compares the points of two polygons and ensures
     52 // that the points are in the same winding order.
     53 bool operator==(const MozPolygon& lhs, const MozPolygon& rhs) {
     54  const auto& left = lhs.GetPoints();
     55  const auto& right = rhs.GetPoints();
     56 
     57  // Polygons do not have the same amount of points.
     58  if (left.Length() != right.Length()) {
     59    return false;
     60  }
     61 
     62  const size_t pointCount = left.Length();
     63 
     64  // Find the first vertex of the first polygon from the second polygon.
     65  // This assumes that the polygons do not contain duplicate vertices.
     66  int start = -1;
     67  for (size_t i = 0; i < pointCount; ++i) {
     68    if (FuzzyEquals(left[0], right[i])) {
     69      start = i;
     70      break;
     71    }
     72  }
     73 
     74  // Found at least one different vertex.
     75  if (start == -1) {
     76    return false;
     77  }
     78 
     79  // Verify that the polygons have the same points.
     80  for (size_t i = 0; i < pointCount; ++i) {
     81    size_t j = (start + i) % pointCount;
     82 
     83    if (!FuzzyEquals(left[i], right[j])) {
     84      return false;
     85    }
     86  }
     87 
     88  return true;
     89 }
     90 
     91 }  // namespace gfx
     92 }  // namespace mozilla
     93 
     94 TEST(PolygonTestUtils, TestSanity)
     95 {
     96  EXPECT_TRUE(FuzzyEquals(Point4D(0.0f, 0.0f, 0.0f, 1.0f),
     97                          Point4D(0.0f, 0.0f, 0.0f, 1.0f)));
     98 
     99  EXPECT_TRUE(FuzzyEquals(Point4D(0.0f, 0.0f, 0.0f, 1.0f),
    100                          Point4D(0.00001f, 0.00001f, 0.00001f, 1.0f)));
    101 
    102  EXPECT_TRUE(FuzzyEquals(Point4D(0.00001f, 0.00001f, 0.00001f, 1.0f),
    103                          Point4D(0.0f, 0.0f, 0.0f, 1.0f)));
    104 
    105  EXPECT_FALSE(FuzzyEquals(Point4D(0.0f, 0.0f, 0.0f, 1.0f),
    106                           Point4D(0.01f, 0.01f, 0.01f, 1.0f)));
    107 
    108  EXPECT_FALSE(FuzzyEquals(Point4D(0.01f, 0.01f, 0.01f, 1.0f),
    109                           Point4D(0.0f, 0.0f, 0.0f, 1.0f)));
    110 
    111  MozPolygon p1{
    112      Point4D(0.0f, 0.0f, 1.0f, 1.0f), Point4D(1.0f, 0.0f, 1.0f, 1.0f),
    113      Point4D(1.0f, 1.0f, 1.0f, 1.0f), Point4D(0.0f, 1.0f, 1.0f, 1.0f)};
    114 
    115  // Same points as above shifted forward by one position.
    116  MozPolygon shifted{
    117      Point4D(0.0f, 1.0f, 1.0f, 1.0f), Point4D(0.0f, 0.0f, 1.0f, 1.0f),
    118      Point4D(1.0f, 0.0f, 1.0f, 1.0f), Point4D(1.0f, 1.0f, 1.0f, 1.0f)};
    119 
    120  MozPolygon p2{Point4D(0.00001f, 0.00001f, 1.00001f, 1.0f),
    121                Point4D(1.00001f, 0.00001f, 1.00001f, 1.0f),
    122                Point4D(1.00001f, 1.00001f, 1.00001f, 1.0f),
    123                Point4D(0.00001f, 1.00001f, 1.00001f, 1.0f)};
    124 
    125  MozPolygon p3{
    126      Point4D(0.01f, 0.01f, 1.01f, 1.0f), Point4D(1.01f, 0.01f, 1.01f, 1.0f),
    127      Point4D(1.01f, 1.01f, 1.01f, 1.0f), Point4D(0.01f, 1.01f, 1.01f, 1.0f)};
    128 
    129  // Trivial equals
    130  EXPECT_TRUE(p1 == p1);
    131  EXPECT_TRUE(p2 == p2);
    132  EXPECT_TRUE(p3 == p3);
    133  EXPECT_TRUE(shifted == shifted);
    134 
    135  // Polygons with the same point order
    136  EXPECT_TRUE(p1 == p2);
    137  EXPECT_TRUE(p1 == shifted);
    138 
    139  // Polygons containing different points
    140  EXPECT_FALSE(p1 == p3);
    141  EXPECT_FALSE(p2 == p3);
    142  EXPECT_FALSE(shifted == p3);
    143 
    144  const nsTArray<Triangle> t1{
    145      Triangle(Point(0.0f, 0.0f), Point(0.0f, 1.0f), Point(1.0f, 1.0f))};
    146 
    147  const nsTArray<Triangle> t2{
    148      Triangle(Point(0.0f, 0.0f), Point(1.0f, 1.0f), Point(1.0f, 0.0f))};
    149 
    150  const nsTArray<Triangle> t3{Triangle(Point(0.00001f, 0.00001f),
    151                                       Point(0.00001f, 1.00001f),
    152                                       Point(1.00001f, 1.00001f))};
    153 
    154  EXPECT_TRUE(t1[0] == t1[0]);
    155  EXPECT_TRUE(t2[0] == t2[0]);
    156  EXPECT_TRUE(t3[0] == t1[0]);
    157 
    158  EXPECT_FALSE(t1[0] == t2[0]);
    159  EXPECT_FALSE(t2[0] == t3[0]);
    160 
    161  AssertArrayEQ(t1, t1);
    162  AssertArrayEQ(t2, t2);
    163 }