File: float_polygon_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (205 lines) | stat: -rw-r--r-- 7,590 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/platform/geometry/float_polygon.h"

#include <memory>

#include "base/containers/span.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"

namespace blink {

class FloatPolygonTestValue {
  STACK_ALLOCATED();

 public:
  explicit FloatPolygonTestValue(base::span<const float> coordinates) {
    DCHECK_EQ(coordinates.size() % 2, 0u);
    Vector<gfx::PointF> vertices(coordinates.size() / 2);
    for (size_t i = 0; i < coordinates.size(); i += 2) {
      vertices[i / 2] = gfx::PointF(coordinates[i], coordinates[i + 1]);
    }
    polygon_ = std::make_unique<FloatPolygon>(std::move(vertices));
  }

  const FloatPolygon& Polygon() const { return *polygon_; }

 private:
  std::unique_ptr<FloatPolygon> polygon_;
};

namespace {

bool CompareEdgeIndex(const FloatPolygonEdge* edge1,
                      const FloatPolygonEdge* edge2) {
  return edge1->EdgeIndex() < edge2->EdgeIndex();
}

Vector<const FloatPolygonEdge*>
SortedOverlappingEdges(const FloatPolygon& polygon, float min_y, float max_y) {
  Vector<const FloatPolygonEdge*> result;
  polygon.OverlappingEdges(min_y, max_y, result);
  std::sort(result.begin(), result.end(), CompareEdgeIndex);
  return result;
}

}  // anonymous namespace

#define SIZEOF_ARRAY(p) (sizeof(p) / sizeof(p[0]))

/**
 * Checks a right triangle. This test covers all of the trivial FloatPolygon
 * accessors.
 *
 *                        200,100
 *                          /|
 *                         / |
 *                        /  |
 *                       -----
 *                 100,200   200,200
 */
TEST(FloatPolygonTest, basics) {
  const float kTriangleCoordinates[] = {200, 100, 200, 200, 100, 200};
  FloatPolygonTestValue triangle_test_value(kTriangleCoordinates);
  const FloatPolygon& triangle = triangle_test_value.Polygon();

  EXPECT_FALSE(triangle.IsEmpty());

  EXPECT_EQ(3u, triangle.NumberOfVertices());
  EXPECT_EQ(gfx::PointF(200, 100), triangle.VertexAt(0));
  EXPECT_EQ(gfx::PointF(200, 200), triangle.VertexAt(1));
  EXPECT_EQ(gfx::PointF(100, 200), triangle.VertexAt(2));

  EXPECT_EQ(3u, triangle.NumberOfEdges());
  EXPECT_EQ(gfx::PointF(200, 100), triangle.EdgeAt(0).Vertex1());
  EXPECT_EQ(gfx::PointF(200, 200), triangle.EdgeAt(0).Vertex2());
  EXPECT_EQ(gfx::PointF(200, 200), triangle.EdgeAt(1).Vertex1());
  EXPECT_EQ(gfx::PointF(100, 200), triangle.EdgeAt(1).Vertex2());
  EXPECT_EQ(gfx::PointF(100, 200), triangle.EdgeAt(2).Vertex1());
  EXPECT_EQ(gfx::PointF(200, 100), triangle.EdgeAt(2).Vertex2());

  EXPECT_EQ(0u, triangle.EdgeAt(0).VertexIndex1());
  EXPECT_EQ(1u, triangle.EdgeAt(0).VertexIndex2());
  EXPECT_EQ(1u, triangle.EdgeAt(1).VertexIndex1());
  EXPECT_EQ(2u, triangle.EdgeAt(1).VertexIndex2());
  EXPECT_EQ(2u, triangle.EdgeAt(2).VertexIndex1());
  EXPECT_EQ(0u, triangle.EdgeAt(2).VertexIndex2());

  EXPECT_EQ(200, triangle.EdgeAt(0).MinX());
  EXPECT_EQ(200, triangle.EdgeAt(0).MaxX());
  EXPECT_EQ(100, triangle.EdgeAt(1).MinX());
  EXPECT_EQ(200, triangle.EdgeAt(1).MaxX());
  EXPECT_EQ(100, triangle.EdgeAt(2).MinX());
  EXPECT_EQ(200, triangle.EdgeAt(2).MaxX());

  EXPECT_EQ(100, triangle.EdgeAt(0).MinY());
  EXPECT_EQ(200, triangle.EdgeAt(0).MaxY());
  EXPECT_EQ(200, triangle.EdgeAt(1).MinY());
  EXPECT_EQ(200, triangle.EdgeAt(1).MaxY());
  EXPECT_EQ(100, triangle.EdgeAt(2).MinY());
  EXPECT_EQ(200, triangle.EdgeAt(2).MaxY());

  EXPECT_EQ(0u, triangle.EdgeAt(0).EdgeIndex());
  EXPECT_EQ(1u, triangle.EdgeAt(1).EdgeIndex());
  EXPECT_EQ(2u, triangle.EdgeAt(2).EdgeIndex());

  EXPECT_EQ(2u, triangle.EdgeAt(0).PreviousEdge().EdgeIndex());
  EXPECT_EQ(1u, triangle.EdgeAt(0).NextEdge().EdgeIndex());
  EXPECT_EQ(0u, triangle.EdgeAt(1).PreviousEdge().EdgeIndex());
  EXPECT_EQ(2u, triangle.EdgeAt(1).NextEdge().EdgeIndex());
  EXPECT_EQ(1u, triangle.EdgeAt(2).PreviousEdge().EdgeIndex());
  EXPECT_EQ(0u, triangle.EdgeAt(2).NextEdge().EdgeIndex());

  EXPECT_EQ(gfx::RectF(100, 100, 100, 100), triangle.BoundingBox());

  Vector<const FloatPolygonEdge*> result_a =
      SortedOverlappingEdges(triangle, 100, 200);
  EXPECT_EQ(3u, result_a.size());
  if (result_a.size() == 3) {
    EXPECT_EQ(0u, result_a[0]->EdgeIndex());
    EXPECT_EQ(1u, result_a[1]->EdgeIndex());
    EXPECT_EQ(2u, result_a[2]->EdgeIndex());
  }

  Vector<const FloatPolygonEdge*> result_b =
      SortedOverlappingEdges(triangle, 200, 200);
  EXPECT_EQ(3u, result_b.size());
  if (result_b.size() == 3) {
    EXPECT_EQ(0u, result_b[0]->EdgeIndex());
    EXPECT_EQ(1u, result_b[1]->EdgeIndex());
    EXPECT_EQ(2u, result_b[2]->EdgeIndex());
  }

  Vector<const FloatPolygonEdge*> result_c =
      SortedOverlappingEdges(triangle, 100, 150);
  EXPECT_EQ(2u, result_c.size());
  if (result_c.size() == 2) {
    EXPECT_EQ(0u, result_c[0]->EdgeIndex());
    EXPECT_EQ(2u, result_c[1]->EdgeIndex());
  }

  Vector<const FloatPolygonEdge*> result_d =
      SortedOverlappingEdges(triangle, 201, 300);
  EXPECT_EQ(0u, result_d.size());

  Vector<const FloatPolygonEdge*> result_e =
      SortedOverlappingEdges(triangle, 98, 99);
  EXPECT_EQ(0u, result_e.size());
}

#define TEST_EMPTY(coordinates)                                             \
  {                                                                         \
    FloatPolygonTestValue empty_polygon_test_value(coordinates);            \
    const FloatPolygon& empty_polygon = empty_polygon_test_value.Polygon(); \
    EXPECT_TRUE(empty_polygon.IsEmpty());                                   \
  }

TEST(FloatPolygonTest, emptyPolygons) {
  const float kEmptyCoordinates1[] = {0, 0};
  TEST_EMPTY(kEmptyCoordinates1);

  const float kEmptyCoordinates2[] = {0, 0, 1, 1};
  TEST_EMPTY(kEmptyCoordinates2);

  const float kEmptyCoordinates3[] = {0, 0, 1, 1, 2, 2, 3, 3};
  TEST_EMPTY(kEmptyCoordinates3);

  const float kEmptyCoordinates4[] = {0, 0, 1, 1, 2, 2, 3, 3, 1, 1};
  TEST_EMPTY(kEmptyCoordinates4);

  const float kEmptyCoordinates5[] = {0, 0, 0, 1, 0, 2, 0, 3, 0, 1};
  TEST_EMPTY(kEmptyCoordinates5);

  const float kEmptyCoordinates6[] = {0, 0, 1, 0, 2, 0, 3, 0, 1, 0};
  TEST_EMPTY(kEmptyCoordinates6);
}

}  // namespace blink