Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/base/simple_enclosed_region.h" | 5 #include "cc/base/simple_enclosed_region.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 // Anything Union contained rect = Anything. | 330 // Anything Union contained rect = Anything. |
| 331 r.Union(gfx::Rect(5, 6, 4, 5)); | 331 r.Union(gfx::Rect(5, 6, 4, 5)); |
| 332 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(4, 5, 6, 7), r)); | 332 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(4, 5, 6, 7), r)); |
| 333 | 333 |
| 334 // Anything Union containing rect = containing rect. | 334 // Anything Union containing rect = containing rect. |
| 335 r.Union(gfx::Rect(2, 3, 8, 9)); | 335 r.Union(gfx::Rect(2, 3, 8, 9)); |
| 336 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 3, 8, 9), r)); | 336 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 3, 8, 9), r)); |
| 337 r.Union(gfx::Rect(2, 3, 9, 10)); | 337 r.Union(gfx::Rect(2, 3, 9, 10)); |
| 338 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 3, 9, 10), r)); | 338 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 3, 9, 10), r)); |
| 339 | 339 |
| 340 // Union with a smaller disjoint rect is ignored. | 340 // Union with a second disjoint rect with area larger than half of the first |
| 341 r.Union(gfx::Rect(20, 21, 9, 9)); | 341 // one. |
| 342 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 3, 9, 10), r)); | 342 // +---+ +--+ |
| 343 // | | | | | |
| 344 // +---+ +--+ | |
| 345 r.Union(gfx::Rect(20, 20, 6, 10)); | |
| 346 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(20, 20, 6, 10), r)); | |
| 343 | 347 |
| 344 // Union with a smaller overlapping rect is ignored. | 348 // Union with a second disjoint rect with area smaller than half of the first |
| 345 r.Union(gfx::Rect(3, 4, 9, 9)); | 349 // one. |
| 346 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 3, 9, 10), r)); | 350 // +----+ +--+ |
| 351 // | | +--+ | |
| 352 // +----+ | |
| 353 r.Union(gfx::Rect(2, 3, 3, 3)); | |
| 354 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(20, 20, 6, 10), r)); | |
| 347 | 355 |
| 348 // Union with an equal sized rect can be either one. | 356 // Union with a second disjoint rect with area larger than the first one. |
| 349 r.Union(gfx::Rect(4, 4, 9, 10)); | 357 // +---+ +-------+ |
| 350 EXPECT_EQ(1u, r.GetRegionComplexity()); | 358 // | | | | |
| 351 EXPECT_TRUE(r.bounds() == gfx::Rect(2, 3, 9, 10) || | 359 // +---+ +-------+ |
| 352 r.bounds() == gfx::Rect(4, 4, 9, 10)); | 360 r.Union(gfx::Rect(2, 3, 15, 15)); |
| 361 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 3, 15, 15), r)); | |
| 353 | 362 |
| 354 // Union with a larger disjoint rect is taken. | 363 // Union with rect which extends from the first one: |
| 355 r.Union(gfx::Rect(20, 21, 12, 13)); | 364 // +----------+ |
| 356 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(20, 21, 12, 13), r)); | 365 // | 1 | 2 | |
| 357 | 366 // +-----+----+ |
| 358 // Union with a larger overlapping rect is taken. | |
| 359 r.Union(gfx::Rect(19, 19, 12, 14)); | |
| 360 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(19, 19, 12, 14), r)); | |
| 361 | |
| 362 // True also when the rect covers one edge of the existing region. | |
| 363 r = gfx::Rect(10, 10, 10, 10); | 367 r = gfx::Rect(10, 10, 10, 10); |
| 364 r.Union(gfx::Rect(12, 7, 9, 16)); | 368 r.Union(gfx::Rect(20, 10, 5, 10)); |
| 365 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(12, 7, 9, 16), r)); | 369 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 15, 10), r)); |
| 366 | 370 |
| 367 r = gfx::Rect(10, 10, 10, 10); | 371 r = gfx::Rect(10, 10, 10, 10); |
| 368 r.Union(gfx::Rect(9, 7, 9, 16)); | 372 r.Union(gfx::Rect(10, 5, 10, 5)); |
| 369 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(9, 7, 9, 16), r)); | 373 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 5, 10, 15), r)); |
| 370 | 374 |
| 371 r = gfx::Rect(10, 10, 10, 10); | 375 r = gfx::Rect(10, 10, 10, 10); |
| 372 r.Union(gfx::Rect(7, 12, 16, 9)); | 376 r.Union(gfx::Rect(5, 10, 5, 10)); |
| 373 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(7, 12, 16, 9), r)); | 377 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(5, 10, 15, 10), r)); |
| 374 | 378 |
| 375 r = gfx::Rect(10, 10, 10, 10); | 379 r = gfx::Rect(10, 10, 10, 10); |
| 376 r.Union(gfx::Rect(7, 9, 16, 9)); | 380 r.Union(gfx::Rect(10, 20, 10, 5)); |
| 377 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(7, 9, 16, 9), r)); | 381 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 15), r)); |
| 378 | 382 |
| 379 // But if the existing region can be expanded to make a larger rect, then it | 383 // Union with rect which overlaps and extends from the first one: |
| 380 // will. Union area is 9*12 = 108. By merging, we make a rect with an area of | 384 // +----+--+---+ |
| 381 // 10*11 = 110. The resulting rect is expanded as far as possible while | 385 // | 1 | |2 | |
| 382 // remaining enclosed in the Union. | 386 // | | | | |
| 387 // +----+--+---+ | |
| 383 r = gfx::Rect(10, 10, 10, 10); | 388 r = gfx::Rect(10, 10, 10, 10); |
| 384 r.Union(gfx::Rect(12, 9, 9, 12)); | 389 r.Union(gfx::Rect(10, 2, 10, 10)); |
| 385 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 11, 10), r)); | 390 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 2, 10, 18), r)); |
| 386 | 391 |
| 387 r = gfx::Rect(10, 10, 10, 10); | 392 r = gfx::Rect(10, 10, 10, 10); |
| 388 r.Union(gfx::Rect(9, 9, 9, 12)); | 393 r.Union(gfx::Rect(2, 10, 10, 10)); |
| 389 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(9, 10, 11, 10), r)); | 394 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(2, 10, 18, 10), r)); |
| 390 | 395 |
| 391 r = gfx::Rect(10, 10, 10, 10); | 396 r = gfx::Rect(10, 10, 10, 10); |
| 392 r.Union(gfx::Rect(9, 12, 12, 9)); | 397 r.Union(gfx::Rect(10, 18, 10, 10)); |
| 393 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 11), r)); | 398 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 18), r)); |
| 394 | 399 |
| 395 r = gfx::Rect(10, 10, 10, 10); | 400 r = gfx::Rect(10, 10, 10, 10); |
| 396 r.Union(gfx::Rect(9, 9, 12, 9)); | 401 r.Union(gfx::Rect(18, 10, 10, 10)); |
| 397 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 9, 10, 11), r)); | 402 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 18, 10), r)); |
| 398 | 403 |
| 399 r = gfx::Rect(12, 9, 9, 12); | 404 // Union with a second rect which overlaps with the first one and |
| 400 r.Union(gfx::Rect(10, 10, 10, 10)); | 405 // area(rect 1) + area(overlap) > area(rect 2)*2. |
| 401 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 11, 10), r)); | 406 // +---+ |
| 407 // +---|+ 2| | |
| 408 // | +---+ | |
| 409 // | 1 | | |
| 410 // +----+ (same figure for next test case.) | |
| 411 r = gfx::Rect(10, 10, 10, 10); | |
| 412 r.Union(gfx::Rect(8, 8, 4, 4)); | |
| 413 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 10), r)); | |
| 402 | 414 |
| 403 r = gfx::Rect(9, 9, 9, 12); | 415 r = gfx::Rect(10, 10, 10, 10); |
| 404 r.Union(gfx::Rect(10, 10, 10, 10)); | 416 r.Union(gfx::Rect(18, 8, 4, 4)); |
|
danakj
2017/05/15 21:47:42
Can you use cases where the area of area(rect 2)*2
yiyix
2017/05/17 03:00:32
This would be a good test! I have this one for the
| |
| 405 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(9, 10, 11, 10), r)); | 417 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 10), r)); |
| 406 | 418 |
| 407 r = gfx::Rect(9, 12, 12, 9); | 419 r = gfx::Rect(10, 10, 10, 10); |
| 408 r.Union(gfx::Rect(10, 10, 10, 10)); | 420 r.Union(gfx::Rect(8, 18, 4, 4)); |
| 409 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 11), r)); | 421 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 10), r)); |
| 410 | 422 |
| 411 r = gfx::Rect(9, 9, 12, 9); | 423 r = gfx::Rect(10, 10, 10, 10); |
| 412 r.Union(gfx::Rect(10, 10, 10, 10)); | 424 r.Union(gfx::Rect(18, 18, 4, 4)); |
| 413 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 9, 10, 11), r)); | 425 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 10), r)); |
| 426 | |
| 427 // Union with a second rect which overlaps with the first one and | |
| 428 // area(rect 1) + area(overlap) < area(rect 2)*2 and | |
| 429 // area(rect 1) > area(rect 2). | |
| 430 r = gfx::Rect(10, 10, 5, 5); | |
| 431 r.Union(gfx::Rect(7, 7, 4, 4)); | |
| 432 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(7, 7, 4, 4), r)); | |
| 433 | |
| 434 r = gfx::Rect(10, 10, 5, 5); | |
| 435 r.Union(gfx::Rect(14, 7, 4, 4)); | |
| 436 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(14, 7, 4, 4), r)); | |
| 437 | |
| 438 r = gfx::Rect(10, 10, 5, 5); | |
| 439 r.Union(gfx::Rect(7, 14, 4, 4)); | |
| 440 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(7, 14, 4, 4), r)); | |
| 441 | |
| 442 r = gfx::Rect(10, 10, 5, 5); | |
| 443 r.Union(gfx::Rect(14, 14, 4, 4)); | |
| 444 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(14, 14, 4, 4), r)); | |
| 445 | |
| 446 // Union with a second rect which overlaps with the first one and the new | |
| 447 // unioned rect should combine both rect. | |
| 448 // +---+-+-----------+ | |
| 449 // | 1| | 2 | | |
| 450 // | +-|-----------+ | |
| 451 // +-----+ | |
| 452 r = gfx::Rect(10, 10, 5, 5); | |
| 453 r.Union(gfx::Rect(5, 11, 7, 4)); | |
| 454 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(5, 11, 10, 4), r)); | |
| 455 | |
| 456 r = gfx::Rect(10, 10, 5, 5); | |
| 457 r.Union(gfx::Rect(13, 10, 7, 4)); | |
| 458 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 4), r)); | |
| 459 | |
| 460 r = gfx::Rect(10, 10, 5, 5); | |
| 461 r.Union(gfx::Rect(10, 12, 4, 7)); | |
| 462 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 4, 9), r)); | |
| 463 | |
| 464 r = gfx::Rect(10, 10, 5, 5); | |
| 465 r.Union(gfx::Rect(11, 11, 4, 7)); | |
| 466 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(11, 10, 4, 8), r)); | |
| 414 } | 467 } |
| 415 | 468 |
| 416 TEST(SimpleEnclosedRegionTest, Subtract) { | 469 TEST(SimpleEnclosedRegionTest, Subtract) { |
| 417 SimpleEnclosedRegion r; | 470 SimpleEnclosedRegion r; |
| 418 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(), r)); | 471 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(), r)); |
| 419 | 472 |
| 420 // Empty Subtract anything = empty. | 473 // Empty Subtract anything = empty. |
| 421 r.Subtract(gfx::Rect(4, 5, 6, 7)); | 474 r.Subtract(gfx::Rect(4, 5, 6, 7)); |
| 422 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(), r)); | 475 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(), r)); |
| 423 | 476 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 630 r.Subtract(gfx::Rect(14, 13, 3, 7)); | 683 r.Subtract(gfx::Rect(14, 13, 3, 7)); |
| 631 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 4, 10), r)); | 684 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 4, 10), r)); |
| 632 | 685 |
| 633 r = gfx::Rect(10, 10, 10, 10); | 686 r = gfx::Rect(10, 10, 10, 10); |
| 634 r.Subtract(gfx::Rect(14, 15, 3, 5)); | 687 r.Subtract(gfx::Rect(14, 15, 3, 5)); |
| 635 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 5), r)); | 688 EXPECT_TRUE(ExpectRegionEq(gfx::Rect(10, 10, 10, 5), r)); |
| 636 } | 689 } |
| 637 | 690 |
| 638 } // namespace | 691 } // namespace |
| 639 } // namespace cc | 692 } // namespace cc |
| OLD | NEW |