rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 8 | #include "SkRTree.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "SkRandom.h" |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 10 | #include "SkTSort.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 12 | |
| 13 | static const size_t MIN_CHILDREN = 6; |
| 14 | static const size_t MAX_CHILDREN = 11; |
| 15 | |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 16 | static const int NUM_RECTS = 200; |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 17 | static const size_t NUM_ITERATIONS = 100; |
| 18 | static const size_t NUM_QUERIES = 50; |
| 19 | |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 20 | static SkRect random_rect(SkRandom& rand) { |
| 21 | SkRect rect = {0,0,0,0}; |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 22 | while (rect.isEmpty()) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 23 | rect.fLeft = rand.nextRangeF(0, 1000); |
| 24 | rect.fRight = rand.nextRangeF(0, 1000); |
| 25 | rect.fTop = rand.nextRangeF(0, 1000); |
| 26 | rect.fBottom = rand.nextRangeF(0, 1000); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 27 | rect.sort(); |
| 28 | } |
| 29 | return rect; |
| 30 | } |
| 31 | |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 32 | static void random_data_rects(SkRandom& rand, SkRect out[], int n) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 33 | for (int i = 0; i < n; ++i) { |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 34 | out[i] = random_rect(rand); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 38 | static bool verify_query(SkRect query, SkRect rects[], SkTDArray<unsigned>& found) { |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 39 | // TODO(mtklein): no need to do this after everything's SkRects |
| 40 | query.roundOut(); |
| 41 | |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 42 | SkTDArray<unsigned> expected; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 43 | |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 44 | // manually intersect with every rectangle |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 45 | for (int i = 0; i < NUM_RECTS; ++i) { |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 46 | if (SkRect::Intersects(query, rects[i])) { |
| 47 | expected.push(i); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 51 | if (expected.count() != found.count()) { |
| 52 | return false; |
| 53 | } |
| 54 | |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 55 | if (0 == expected.count()) { |
| 56 | return true; |
| 57 | } |
| 58 | |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 59 | // skia:2834. RTree doesn't always return results in order. |
| 60 | SkTQSort(expected.begin(), expected.end() -1); |
| 61 | SkTQSort(found.begin(), found.end() -1); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 62 | return found == expected; |
| 63 | } |
| 64 | |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 65 | static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, SkRect rects[], |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 66 | SkRTree& tree) { |
robertphillips@google.com | 178a267 | 2012-09-13 13:25:30 +0000 | [diff] [blame] | 67 | for (size_t i = 0; i < NUM_QUERIES; ++i) { |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 68 | SkTDArray<unsigned> hits; |
mtklein | 533eb78 | 2014-08-27 10:39:42 -0700 | [diff] [blame] | 69 | SkRect query = random_rect(rand); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 70 | tree.search(query, &hits); |
| 71 | REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); |
| 72 | } |
| 73 | } |
| 74 | |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 75 | static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) { |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 76 | SkRect rects[NUM_RECTS]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 77 | SkRandom rand; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, rtree); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 79 | |
| 80 | int expectedDepthMin = -1; |
| 81 | int expectedDepthMax = -1; |
| 82 | |
| 83 | int tmp = NUM_RECTS; |
| 84 | while (tmp > 0) { |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 85 | tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN), |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 86 | static_cast<double>(expectedDepthMin + 1))); |
| 87 | ++expectedDepthMin; |
| 88 | } |
| 89 | |
| 90 | tmp = NUM_RECTS; |
| 91 | while (tmp > 0) { |
| 92 | tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN), |
| 93 | static_cast<double>(expectedDepthMax + 1))); |
| 94 | ++expectedDepthMax; |
| 95 | } |
| 96 | |
robertphillips@google.com | 178a267 | 2012-09-13 13:25:30 +0000 | [diff] [blame] | 97 | for (size_t i = 0; i < NUM_ITERATIONS; ++i) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 98 | random_data_rects(rand, rects, NUM_RECTS); |
| 99 | |
| 100 | // First try bulk-loaded inserts |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 101 | for (int i = 0; i < NUM_RECTS; ++i) { |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 102 | rtree->insert(i, rects[i], true); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 103 | } |
| 104 | rtree->flushDeferredInserts(); |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 105 | run_queries(reporter, rand, rects, *rtree); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
| 107 | REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
| 108 | expectedDepthMax >= rtree->getDepth()); |
| 109 | rtree->clear(); |
| 110 | REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
| 111 | |
| 112 | // Then try immediate inserts |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 113 | for (int i = 0; i < NUM_RECTS; ++i) { |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 114 | rtree->insert(i, rects[i]); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 115 | } |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 116 | run_queries(reporter, rand, rects, *rtree); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
| 118 | REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
| 119 | expectedDepthMax >= rtree->getDepth()); |
| 120 | rtree->clear(); |
| 121 | REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
| 122 | |
| 123 | // And for good measure try immediate inserts, but in reversed order |
| 124 | for (int i = NUM_RECTS - 1; i >= 0; --i) { |
mtklein | 6bd4196 | 2014-10-02 07:41:56 -0700 | [diff] [blame] | 125 | rtree->insert(i, rects[i]); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 126 | } |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 127 | run_queries(reporter, rand, rects, *rtree); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 128 | REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
| 129 | REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
| 130 | expectedDepthMax >= rtree->getDepth()); |
| 131 | rtree->clear(); |
| 132 | REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
| 133 | } |
| 134 | } |
| 135 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 136 | DEF_TEST(RTree, reporter) { |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 137 | SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); |
| 138 | SkAutoUnref au(rtree); |
| 139 | rtree_test_main(rtree, reporter); |
| 140 | |
| 141 | // Rtree that orders input rectangles on deferred insert. |
sglez@google.com | ffb71f2 | 2013-08-30 17:43:31 +0000 | [diff] [blame] | 142 | SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, false); |
| 143 | SkAutoUnref auo(unsortedRtree); |
| 144 | rtree_test_main(unsortedRtree, reporter); |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 145 | } |