OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRTree.h" | 8 #include "SkRTree.h" |
9 #include "SkRandom.h" | 9 #include "SkRandom.h" |
10 #include "SkTSort.h" | 10 #include "SkTSort.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 while (rect.isEmpty()) { | 22 while (rect.isEmpty()) { |
23 rect.fLeft = rand.nextRangeF(0, 1000); | 23 rect.fLeft = rand.nextRangeF(0, 1000); |
24 rect.fRight = rand.nextRangeF(0, 1000); | 24 rect.fRight = rand.nextRangeF(0, 1000); |
25 rect.fTop = rand.nextRangeF(0, 1000); | 25 rect.fTop = rand.nextRangeF(0, 1000); |
26 rect.fBottom = rand.nextRangeF(0, 1000); | 26 rect.fBottom = rand.nextRangeF(0, 1000); |
27 rect.sort(); | 27 rect.sort(); |
28 } | 28 } |
29 return rect; | 29 return rect; |
30 } | 30 } |
31 | 31 |
32 static void random_data_rects(SkRandom& rand, SkRect out[], int n) { | |
33 for (int i = 0; i < n; ++i) { | |
34 out[i] = random_rect(rand); | |
35 } | |
36 } | |
37 | |
38 static bool verify_query(SkRect query, SkRect rects[], SkTDArray<unsigned>& foun
d) { | 32 static bool verify_query(SkRect query, SkRect rects[], SkTDArray<unsigned>& foun
d) { |
39 // TODO(mtklein): no need to do this after everything's SkRects | 33 // TODO(mtklein): no need to do this after everything's SkRects |
40 query.roundOut(); | 34 query.roundOut(); |
41 | 35 |
42 SkTDArray<unsigned> expected; | 36 SkTDArray<unsigned> expected; |
43 | 37 |
44 // manually intersect with every rectangle | 38 // manually intersect with every rectangle |
45 for (int i = 0; i < NUM_RECTS; ++i) { | 39 for (int i = 0; i < NUM_RECTS; ++i) { |
46 if (SkRect::Intersects(query, rects[i])) { | 40 if (SkRect::Intersects(query, rects[i])) { |
47 expected.push(i); | 41 expected.push(i); |
(...skipping 18 matching lines...) Expand all Loading... |
66 SkRTree& tree) { | 60 SkRTree& tree) { |
67 for (size_t i = 0; i < NUM_QUERIES; ++i) { | 61 for (size_t i = 0; i < NUM_QUERIES; ++i) { |
68 SkTDArray<unsigned> hits; | 62 SkTDArray<unsigned> hits; |
69 SkRect query = random_rect(rand); | 63 SkRect query = random_rect(rand); |
70 tree.search(query, &hits); | 64 tree.search(query, &hits); |
71 REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); | 65 REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); |
72 } | 66 } |
73 } | 67 } |
74 | 68 |
75 static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) { | 69 static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) { |
76 SkRect rects[NUM_RECTS]; | 70 SkASSERT(rtree); |
77 SkRandom rand; | |
78 REPORTER_ASSERT(reporter, rtree); | |
79 | 71 |
80 int expectedDepthMin = -1; | 72 int expectedDepthMin = -1; |
81 int expectedDepthMax = -1; | 73 int expectedDepthMax = -1; |
82 | 74 |
83 int tmp = NUM_RECTS; | 75 int tmp = NUM_RECTS; |
84 while (tmp > 0) { | 76 while (tmp > 0) { |
85 tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN), | 77 tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN), |
86 static_cast<double>(expectedDepthMin + 1))); | 78 static_cast<double>(expectedDepthMin + 1))); |
87 ++expectedDepthMin; | 79 ++expectedDepthMin; |
88 } | 80 } |
89 | 81 |
90 tmp = NUM_RECTS; | 82 tmp = NUM_RECTS; |
91 while (tmp > 0) { | 83 while (tmp > 0) { |
92 tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN), | 84 tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN), |
93 static_cast<double>(expectedDepthMax + 1))); | 85 static_cast<double>(expectedDepthMax + 1))); |
94 ++expectedDepthMax; | 86 ++expectedDepthMax; |
95 } | 87 } |
96 | 88 |
| 89 SkRandom rand; |
| 90 SkAutoTMalloc<SkRect> rects(NUM_RECTS); |
97 for (size_t i = 0; i < NUM_ITERATIONS; ++i) { | 91 for (size_t i = 0; i < NUM_ITERATIONS; ++i) { |
98 random_data_rects(rand, rects, NUM_RECTS); | 92 rtree->clear(); |
| 93 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
99 | 94 |
100 // First try bulk-loaded inserts | 95 for (int j = 0; j < NUM_RECTS; j++) { |
101 for (int i = 0; i < NUM_RECTS; ++i) { | 96 rects[j] = random_rect(rand); |
102 rtree->insert(i, rects[i], true); | |
103 } | 97 } |
104 rtree->flushDeferredInserts(); | 98 |
| 99 rtree->insert(&rects, NUM_RECTS); |
| 100 SkASSERT(rects); // SkRTree doesn't take ownership of rects. |
| 101 |
105 run_queries(reporter, rand, rects, *rtree); | 102 run_queries(reporter, rand, rects, *rtree); |
106 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); | 103 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
107 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && | 104 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
108 expectedDepthMax >= rtree->getDepth()); | 105 expectedDepthMax >= rtree->getDepth()); |
109 rtree->clear(); | |
110 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); | |
111 | |
112 // Then try immediate inserts | |
113 for (int i = 0; i < NUM_RECTS; ++i) { | |
114 rtree->insert(i, rects[i]); | |
115 } | |
116 run_queries(reporter, rand, rects, *rtree); | |
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) { | |
125 rtree->insert(i, rects[i]); | |
126 } | |
127 run_queries(reporter, rand, rects, *rtree); | |
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 } | 106 } |
134 } | 107 } |
135 | 108 |
136 DEF_TEST(RTree, reporter) { | 109 DEF_TEST(RTree, reporter) { |
137 SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); | 110 SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); |
138 SkAutoUnref au(rtree); | 111 SkAutoUnref au(rtree); |
139 rtree_test_main(rtree, reporter); | 112 rtree_test_main(rtree, reporter); |
140 | 113 |
141 // Rtree that orders input rectangles on deferred insert. | 114 // Rtree that orders input rectangles on deferred insert. |
142 SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, fals
e); | 115 SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, fals
e); |
143 SkAutoUnref auo(unsortedRtree); | 116 SkAutoUnref auo(unsortedRtree); |
144 rtree_test_main(unsortedRtree, reporter); | 117 rtree_test_main(unsortedRtree, reporter); |
145 } | 118 } |
OLD | NEW |