File: geometry_util.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 (513 lines) | stat: -rw-r--r-- 20,323 bytes parent folder | download | duplicates (4)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "ui/gfx/geometry/test/geometry_util.h"

#include <sstream>
#include <string>

#include "base/strings/stringprintf.h"
#include "ui/gfx/geometry/axis_transform2d.h"
#include "ui/gfx/geometry/box_f.h"
#include "ui/gfx/geometry/decomposed_transform.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/insets_f.h"
#include "ui/gfx/geometry/mask_filter_info.h"
#include "ui/gfx/geometry/outsets.h"
#include "ui/gfx/geometry/outsets_f.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point3_f.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/quad_f.h"
#include "ui/gfx/geometry/quaternion.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/geometry/vector2d_f.h"
#include "ui/gfx/geometry/vector3d_f.h"
#include "ui/gfx/selection_bound.h"

namespace gfx {

namespace {

bool FloatAlmostEqual(float a, float b) {
  // FloatLE is the gtest predicate for less than or almost equal to.
  return ::testing::FloatLE("a", "b", a, b) &&
         ::testing::FloatLE("b", "a", b, a);
}

bool FloatNear(float a, float b, float abs_error) {
  return std::abs(a - b) <= abs_error;
}

template <typename T>
::testing::AssertionResult EqFailure(const char* lhs_expr,
                                     const char* rhs_expr,
                                     const T& lhs,
                                     const T& rhs) {
  return ::testing::AssertionFailure()
         << "Expected equality of these values:\n"
         << lhs_expr << "\n    Which is: " << lhs.ToString() << "\n"
         << rhs_expr << "\n    Which is: " << rhs.ToString();
}

template <typename T>
::testing::AssertionResult NearFailure(const char* lhs_expr,
                                       const char* rhs_expr,
                                       const char* abs_error_expr,
                                       const T& lhs,
                                       const T& rhs,
                                       float abs_error) {
  return ::testing::AssertionFailure()
         << "The difference between these values:\n"
         << lhs_expr << "\n    Which is: " << lhs.ToString() << "\n"
         << rhs_expr << "\n    Which is: " << rhs.ToString() << "\nexceeds "
         << abs_error_expr << "\n    Which is: " << abs_error;
}

struct SkRectToString {
  SkRect r;
  std::string ToString() const {
    return base::StringPrintf("SkRect::MakeLTRB(%g, %g, %g, %g)", r.left(),
                              r.top(), r.right(), r.bottom());
  }
};

}  // namespace

::testing::AssertionResult AssertAxisTransform2dFloatEqual(
    const char* lhs_expr,
    const char* rhs_expr,
    const AxisTransform2d& lhs,
    const AxisTransform2d& rhs) {
  if (FloatAlmostEqual(lhs.scale().x(), rhs.scale().x()) &&
      FloatAlmostEqual(lhs.scale().y(), rhs.scale().y()) &&
      FloatAlmostEqual(lhs.translation().x(), rhs.translation().x()) &&
      FloatAlmostEqual(lhs.translation().y(), rhs.translation().y())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertQuaternionFloatEqual(const char* lhs_expr,
                                                      const char* rhs_expr,
                                                      const Quaternion& lhs,
                                                      const Quaternion& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y()) &&
      FloatAlmostEqual(lhs.z(), rhs.z()) &&
      FloatAlmostEqual(lhs.w(), rhs.w())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertQuaternionFloatNear(const char* lhs_expr,
                                                     const char* rhs_expr,
                                                     const char* abs_error_expr,
                                                     const Quaternion& lhs,
                                                     const Quaternion& rhs,
                                                     float abs_error) {
  if (FloatNear(lhs.x(), rhs.x(), abs_error) &&
      FloatNear(lhs.y(), rhs.y(), abs_error) &&
      FloatNear(lhs.z(), rhs.z(), abs_error) &&
      FloatNear(lhs.w(), rhs.w(), abs_error)) {
    return ::testing::AssertionSuccess();
  }
  return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs, abs_error);
}

::testing::AssertionResult AssertDecomposedTransformFloatEqual(
    const char* lhs_expr,
    const char* rhs_expr,
    const DecomposedTransform& lhs,
    const DecomposedTransform& rhs) {
#define CHECK_ARRAY(array)                                                 \
  do {                                                                     \
    for (size_t i = 0; i < std::size(lhs.array); i++) {                    \
      if (!FloatAlmostEqual(lhs.array[i], rhs.array[i])) {                 \
        return EqFailure(lhs_expr, rhs_expr, lhs, rhs)                     \
               << "First difference is at: " << #array << "[" << i << "]"; \
      }                                                                    \
    }                                                                      \
  } while (false)

  CHECK_ARRAY(translate);
  CHECK_ARRAY(scale);
  CHECK_ARRAY(skew);
  CHECK_ARRAY(perspective);
#undef CHECK_ARRAY

  return AssertQuaternionFloatEqual(lhs_expr, rhs_expr, lhs.quaternion,
                                    rhs.quaternion)
         << " In quaternion";
}

::testing::AssertionResult AssertDecomposedTransformFloatNear(
    const char* lhs_expr,
    const char* rhs_expr,
    const char* abs_error_expr,
    const DecomposedTransform& lhs,
    const DecomposedTransform& rhs,
    float abs_error) {
#define CHECK_ARRAY(array)                                                 \
  do {                                                                     \
    for (size_t i = 0; i < std::size(lhs.array); i++) {                    \
      if (!FloatNear(lhs.array[i], rhs.array[i], abs_error)) {             \
        return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs,   \
                           abs_error)                                      \
               << "First difference is at: " << #array << "[" << i << "]"; \
      }                                                                    \
    }                                                                      \
  } while (false)

  CHECK_ARRAY(translate);
  CHECK_ARRAY(scale);
  CHECK_ARRAY(skew);
  CHECK_ARRAY(perspective);
#undef CHECK_ARRAY

  return AssertQuaternionFloatNear(lhs_expr, rhs_expr, abs_error_expr,
                                   lhs.quaternion, rhs.quaternion, abs_error)
         << " In quaternion";
}

::testing::AssertionResult AssertTransformFloatEqual(const char* lhs_expr,
                                                     const char* rhs_expr,
                                                     const Transform& lhs,
                                                     const Transform& rhs) {
  for (int row = 0; row < 4; ++row) {
    for (int col = 0; col < 4; ++col) {
      if (!FloatAlmostEqual(lhs.rc(row, col), rhs.rc(row, col))) {
        return EqFailure(lhs_expr, rhs_expr, lhs, rhs)
               << "\nFirst difference at row: " << row << " col: " << col;
      }
    }
  }
  return ::testing::AssertionSuccess();
}

::testing::AssertionResult AssertTransformFloatNear(const char* lhs_expr,
                                                    const char* rhs_expr,
                                                    const char* abs_error_expr,
                                                    const Transform& lhs,
                                                    const Transform& rhs,
                                                    float abs_error) {
  for (int row = 0; row < 4; ++row) {
    for (int col = 0; col < 4; ++col) {
      if (!FloatNear(lhs.rc(row, col), rhs.rc(row, col), abs_error)) {
        return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs,
                           abs_error)
               << "\nFirst difference at row: " << row << " col: " << col;
      }
    }
  }
  return ::testing::AssertionSuccess();
}

::testing::AssertionResult AssertBoxFloatEqual(const char* lhs_expr,
                                               const char* rhs_expr,
                                               const BoxF& lhs,
                                               const BoxF& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y()) &&
      FloatAlmostEqual(lhs.z(), rhs.z()) &&
      FloatAlmostEqual(lhs.width(), rhs.width()) &&
      FloatAlmostEqual(lhs.height(), rhs.height()) &&
      FloatAlmostEqual(lhs.depth(), rhs.depth())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertBoxFloatNear(const char* lhs_expr,
                                              const char* rhs_expr,
                                              const char* abs_error_expr,
                                              const BoxF& lhs,
                                              const BoxF& rhs,
                                              float abs_error) {
  if (FloatNear(lhs.x(), rhs.x(), abs_error) &&
      FloatNear(lhs.y(), rhs.y(), abs_error) &&
      FloatNear(lhs.z(), rhs.z(), abs_error) &&
      FloatNear(lhs.width(), rhs.width(), abs_error) &&
      FloatNear(lhs.height(), rhs.height(), abs_error) &&
      FloatNear(lhs.depth(), rhs.depth(), abs_error)) {
    return ::testing::AssertionSuccess();
  }
  return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs, abs_error);
}

::testing::AssertionResult AssertPointFloatEqual(const char* lhs_expr,
                                                 const char* rhs_expr,
                                                 const PointF& lhs,
                                                 const PointF& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertPointFloatNear(const char* lhs_expr,
                                                const char* rhs_expr,
                                                const char* abs_error_expr,
                                                const PointF& lhs,
                                                const PointF& rhs,
                                                float abs_error) {
  if (FloatNear(lhs.x(), rhs.x(), abs_error) &&
      FloatNear(lhs.y(), rhs.y(), abs_error)) {
    return ::testing::AssertionSuccess();
  }
  return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs, abs_error);
}

::testing::AssertionResult AssertPoint3FloatEqual(const char* lhs_expr,
                                                  const char* rhs_expr,
                                                  const Point3F& lhs,
                                                  const Point3F& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y()) &&
      FloatAlmostEqual(lhs.z(), rhs.z())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertPoint3FloatNear(const char* lhs_expr,
                                                 const char* rhs_expr,
                                                 const char* abs_error_expr,
                                                 const Point3F& lhs,
                                                 const Point3F& rhs,
                                                 float abs_error) {
  if (FloatNear(lhs.x(), rhs.x(), abs_error) &&
      FloatNear(lhs.y(), rhs.y(), abs_error) &&
      FloatNear(lhs.z(), rhs.z(), abs_error)) {
    return ::testing::AssertionSuccess();
  }
  return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs, abs_error);
}

::testing::AssertionResult AssertVector2dFloatEqual(const char* lhs_expr,
                                                    const char* rhs_expr,
                                                    const Vector2dF& lhs,
                                                    const Vector2dF& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertVector2dFloatNear(const char* lhs_expr,
                                                   const char* rhs_expr,
                                                   const char* abs_error_expr,
                                                   const Vector2dF& lhs,
                                                   const Vector2dF& rhs,
                                                   float abs_error) {
  if (FloatNear(lhs.x(), rhs.x(), abs_error) &&
      FloatNear(lhs.y(), rhs.y(), abs_error)) {
    return ::testing::AssertionSuccess();
  }
  return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs, abs_error);
}

::testing::AssertionResult AssertVector3dFloatEqual(const char* lhs_expr,
                                                    const char* rhs_expr,
                                                    const Vector3dF& lhs,
                                                    const Vector3dF& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y()) &&
      FloatAlmostEqual(lhs.z(), rhs.z())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertVector3dFloatNear(const char* lhs_expr,
                                                   const char* rhs_expr,
                                                   const char* abs_error_expr,
                                                   const Vector3dF& lhs,
                                                   const Vector3dF& rhs,
                                                   float abs_error) {
  if (FloatNear(lhs.x(), rhs.x(), abs_error) &&
      FloatNear(lhs.y(), rhs.y(), abs_error) &&
      FloatNear(lhs.z(), rhs.z(), abs_error)) {
    return ::testing::AssertionSuccess();
  }
  return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs, abs_error);
}

::testing::AssertionResult AssertRectFloatEqual(const char* lhs_expr,
                                                const char* rhs_expr,
                                                const RectF& lhs,
                                                const RectF& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y()) &&
      FloatAlmostEqual(lhs.width(), rhs.width()) &&
      FloatAlmostEqual(lhs.height(), rhs.height())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertRectFloatNear(const char* lhs_expr,
                                               const char* rhs_expr,
                                               const char* abs_error_expr,
                                               const RectF& lhs,
                                               const RectF& rhs,
                                               float abs_error) {
  if (FloatNear(lhs.x(), rhs.x(), abs_error) &&
      FloatNear(lhs.y(), rhs.y(), abs_error) &&
      FloatNear(lhs.width(), rhs.width(), abs_error) &&
      FloatNear(lhs.height(), rhs.height(), abs_error)) {
    return ::testing::AssertionSuccess();
  }
  return NearFailure(lhs_expr, rhs_expr, abs_error_expr, lhs, rhs, abs_error);
}

::testing::AssertionResult AssertSkRectFloatEqual(const char* lhs_expr,
                                                  const char* rhs_expr,
                                                  const SkRect& lhs,
                                                  const SkRect& rhs) {
  if (FloatAlmostEqual(lhs.x(), rhs.x()) &&
      FloatAlmostEqual(lhs.y(), rhs.y()) &&
      FloatAlmostEqual(lhs.right(), rhs.right()) &&
      FloatAlmostEqual(lhs.bottom(), rhs.bottom())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, SkRectToString{lhs},
                   SkRectToString{rhs});
}

::testing::AssertionResult AssertSizeFloatEqual(const char* lhs_expr,
                                                const char* rhs_expr,
                                                const SizeF& lhs,
                                                const SizeF& rhs) {
  if (FloatAlmostEqual(lhs.width(), rhs.width()) &&
      FloatAlmostEqual(lhs.height(), rhs.height())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

::testing::AssertionResult AssertSkSizeFloatEqual(const char* lhs_expr,
                                                  const char* rhs_expr,
                                                  const SkSize& lhs,
                                                  const SkSize& rhs) {
  return AssertSizeFloatEqual(lhs_expr, rhs_expr, SkSizeToSizeF(lhs),
                              SkSizeToSizeF(rhs));
}

::testing::AssertionResult AssertInsetsFloatEqual(const char* lhs_expr,
                                                  const char* rhs_expr,
                                                  const InsetsF& lhs,
                                                  const InsetsF& rhs) {
  if (FloatAlmostEqual(lhs.top(), rhs.top()) &&
      FloatAlmostEqual(lhs.right(), rhs.right()) &&
      FloatAlmostEqual(lhs.bottom(), rhs.bottom()) &&
      FloatAlmostEqual(lhs.left(), rhs.left())) {
    return ::testing::AssertionSuccess();
  }
  return EqFailure(lhs_expr, rhs_expr, lhs, rhs);
}

void PrintTo(const AxisTransform2d& transform, ::std::ostream* os) {
  *os << transform.ToString();
}

void PrintTo(const BoxF& box, ::std::ostream* os) {
  *os << box.ToString();
}

void PrintTo(const Point& point, ::std::ostream* os) {
  *os << point.ToString();
}

void PrintTo(const Point3F& point, ::std::ostream* os) {
  *os << point.ToString();
}

void PrintTo(const PointF& point, ::std::ostream* os) {
  *os << point.ToString();
}

void PrintTo(const Insets& input, ::std::ostream* os) {
  *os << input.ToString();
}

void PrintTo(const InsetsF& input, ::std::ostream* os) {
  *os << input.ToString();
}

void PrintTo(const Outsets& input, ::std::ostream* os) {
  *os << input.ToString();
}

void PrintTo(const OutsetsF& input, ::std::ostream* os) {
  *os << input.ToString();
}

void PrintTo(const QuadF& quad, ::std::ostream* os) {
  *os << quad.ToString();
}

void PrintTo(const Rect& rect, ::std::ostream* os) {
  *os << rect.ToString();
}

void PrintTo(const RectF& rect, ::std::ostream* os) {
  *os << rect.ToString();
}

void PrintTo(const Size& size, ::std::ostream* os) {
  *os << size.ToString();
}

void PrintTo(const SizeF& size, ::std::ostream* os) {
  *os << size.ToString();
}

void PrintTo(const Transform& transform, ::std::ostream* os) {
  *os << transform.ToString();
}

void PrintTo(const Vector2d& vector, ::std::ostream* os) {
  *os << vector.ToString();
}

void PrintTo(const Vector2dF& vector, ::std::ostream* os) {
  *os << vector.ToString();
}

void PrintTo(const Vector3dF& vector, ::std::ostream* os) {
  *os << vector.ToString();
}

void PrintTo(const MaskFilterInfo& info, ::std::ostream* os) {
  *os << info.ToString();
}

void PrintTo(const SelectionBound& bound, ::std::ostream* os) {
  *os << bound.ToString();
}

void PrintTo(const SkRect& rect, ::std::ostream* os) {
  *os << SkRectToString{rect}.ToString();
}

void PrintTo(const DecomposedTransform& transform, ::std::ostream* os) {
  *os << transform.ToString();
}

void PrintTo(const Quaternion& quaternion, ::std::ostream* os) {
  *os << quaternion.ToString();
}

}  // namespace gfx