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 | |
| 20 | struct DataRect { |
| 21 | SkIRect rect; |
| 22 | void* data; |
| 23 | }; |
| 24 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 25 | static SkIRect random_rect(SkRandom& rand) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 26 | SkIRect rect = {0,0,0,0}; |
| 27 | while (rect.isEmpty()) { |
| 28 | rect.fLeft = rand.nextS() % 1000; |
| 29 | rect.fRight = rand.nextS() % 1000; |
| 30 | rect.fTop = rand.nextS() % 1000; |
| 31 | rect.fBottom = rand.nextS() % 1000; |
| 32 | rect.sort(); |
| 33 | } |
| 34 | return rect; |
| 35 | } |
| 36 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 37 | static void random_data_rects(SkRandom& rand, DataRect out[], int n) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 38 | for (int i = 0; i < n; ++i) { |
| 39 | out[i].rect = random_rect(rand); |
| 40 | out[i].data = reinterpret_cast<void*>(i); |
| 41 | } |
| 42 | } |
| 43 | |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 44 | static bool verify_query(SkIRect query, DataRect rects[], |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 45 | SkTDArray<void*>& found) { |
| 46 | SkTDArray<void*> expected; |
| 47 | // manually intersect with every rectangle |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 48 | for (int i = 0; i < NUM_RECTS; ++i) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 49 | if (SkIRect::IntersectsNoEmptyCheck(query, rects[i].rect)) { |
| 50 | expected.push(rects[i].data); |
| 51 | } |
| 52 | } |
| 53 | |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 54 | if (expected.count() != found.count()) { |
| 55 | return false; |
| 56 | } |
| 57 | |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 58 | if (0 == expected.count()) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | // Just cast to long since sorting by the value of the void*'s was being problematic... |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 63 | SkTQSort(reinterpret_cast<long*>(expected.begin()), |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 64 | reinterpret_cast<long*>(expected.end() - 1)); |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 65 | SkTQSort(reinterpret_cast<long*>(found.begin()), |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 66 | reinterpret_cast<long*>(found.end() - 1)); |
| 67 | return found == expected; |
| 68 | } |
| 69 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 70 | static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, DataRect rects[], |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 71 | SkRTree& tree) { |
robertphillips@google.com | 178a267 | 2012-09-13 13:25:30 +0000 | [diff] [blame] | 72 | for (size_t i = 0; i < NUM_QUERIES; ++i) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 73 | SkTDArray<void*> hits; |
| 74 | SkIRect query = random_rect(rand); |
| 75 | tree.search(query, &hits); |
| 76 | REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); |
| 77 | } |
| 78 | } |
| 79 | |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 80 | static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 81 | DataRect rects[NUM_RECTS]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 82 | SkRandom rand; |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, NULL != rtree); |
| 84 | |
| 85 | int expectedDepthMin = -1; |
| 86 | int expectedDepthMax = -1; |
| 87 | |
| 88 | int tmp = NUM_RECTS; |
| 89 | while (tmp > 0) { |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 90 | tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN), |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 91 | static_cast<double>(expectedDepthMin + 1))); |
| 92 | ++expectedDepthMin; |
| 93 | } |
| 94 | |
| 95 | tmp = NUM_RECTS; |
| 96 | while (tmp > 0) { |
| 97 | tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN), |
| 98 | static_cast<double>(expectedDepthMax + 1))); |
| 99 | ++expectedDepthMax; |
| 100 | } |
| 101 | |
robertphillips@google.com | 178a267 | 2012-09-13 13:25:30 +0000 | [diff] [blame] | 102 | for (size_t i = 0; i < NUM_ITERATIONS; ++i) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 103 | random_data_rects(rand, rects, NUM_RECTS); |
| 104 | |
| 105 | // First try bulk-loaded inserts |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 106 | for (int i = 0; i < NUM_RECTS; ++i) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 107 | rtree->insert(rects[i].data, rects[i].rect, true); |
| 108 | } |
| 109 | rtree->flushDeferredInserts(); |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 110 | run_queries(reporter, rand, rects, *rtree); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
| 112 | REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
| 113 | expectedDepthMax >= rtree->getDepth()); |
| 114 | rtree->clear(); |
| 115 | REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
| 116 | |
| 117 | // Then try immediate inserts |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 118 | for (int i = 0; i < NUM_RECTS; ++i) { |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 119 | rtree->insert(rects[i].data, rects[i].rect); |
| 120 | } |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 121 | run_queries(reporter, rand, rects, *rtree); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
| 123 | REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
| 124 | expectedDepthMax >= rtree->getDepth()); |
| 125 | rtree->clear(); |
| 126 | REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
| 127 | |
| 128 | // And for good measure try immediate inserts, but in reversed order |
| 129 | for (int i = NUM_RECTS - 1; i >= 0; --i) { |
| 130 | rtree->insert(rects[i].data, rects[i].rect); |
| 131 | } |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 132 | run_queries(reporter, rand, rects, *rtree); |
rileya@google.com | 1f45e93 | 2012-09-05 16:10:59 +0000 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
| 134 | REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
| 135 | expectedDepthMax >= rtree->getDepth()); |
| 136 | rtree->clear(); |
| 137 | REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
| 138 | } |
| 139 | } |
| 140 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 141 | DEF_TEST(RTree, reporter) { |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 142 | SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); |
| 143 | SkAutoUnref au(rtree); |
| 144 | rtree_test_main(rtree, reporter); |
| 145 | |
| 146 | // Rtree that orders input rectangles on deferred insert. |
sglez@google.com | ffb71f2 | 2013-08-30 17:43:31 +0000 | [diff] [blame] | 147 | SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, false); |
| 148 | SkAutoUnref auo(unsortedRtree); |
| 149 | rtree_test_main(unsortedRtree, reporter); |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 150 | } |