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" | |
11 #include "Test.h" | 10 #include "Test.h" |
12 | 11 |
13 static const size_t MIN_CHILDREN = 6; | |
14 static const size_t MAX_CHILDREN = 11; | |
15 | |
16 static const int NUM_RECTS = 200; | 12 static const int NUM_RECTS = 200; |
17 static const size_t NUM_ITERATIONS = 100; | 13 static const size_t NUM_ITERATIONS = 100; |
18 static const size_t NUM_QUERIES = 50; | 14 static const size_t NUM_QUERIES = 50; |
19 | 15 |
20 static SkRect random_rect(SkRandom& rand) { | 16 static SkRect random_rect(SkRandom& rand) { |
21 SkRect rect = {0,0,0,0}; | 17 SkRect rect = {0,0,0,0}; |
22 while (rect.isEmpty()) { | 18 while (rect.isEmpty()) { |
23 rect.fLeft = rand.nextRangeF(0, 1000); | 19 rect.fLeft = rand.nextRangeF(0, 1000); |
24 rect.fRight = rand.nextRangeF(0, 1000); | 20 rect.fRight = rand.nextRangeF(0, 1000); |
25 rect.fTop = rand.nextRangeF(0, 1000); | 21 rect.fTop = rand.nextRangeF(0, 1000); |
26 rect.fBottom = rand.nextRangeF(0, 1000); | 22 rect.fBottom = rand.nextRangeF(0, 1000); |
27 rect.sort(); | 23 rect.sort(); |
28 } | 24 } |
29 return rect; | 25 return rect; |
30 } | 26 } |
31 | 27 |
32 static bool verify_query(SkRect query, SkRect rects[], SkTDArray<unsigned>& foun
d) { | 28 static bool verify_query(SkRect query, SkRect rects[], SkTDArray<unsigned>& foun
d) { |
33 // TODO(mtklein): no need to do this after everything's SkRects | |
34 query.roundOut(); | |
35 | |
36 SkTDArray<unsigned> expected; | 29 SkTDArray<unsigned> expected; |
37 | |
38 // manually intersect with every rectangle | 30 // manually intersect with every rectangle |
39 for (int i = 0; i < NUM_RECTS; ++i) { | 31 for (int i = 0; i < NUM_RECTS; ++i) { |
40 if (SkRect::Intersects(query, rects[i])) { | 32 if (SkRect::Intersects(query, rects[i])) { |
41 expected.push(i); | 33 expected.push(i); |
42 } | 34 } |
43 } | 35 } |
44 | 36 |
45 if (expected.count() != found.count()) { | 37 if (expected.count() != found.count()) { |
46 return false; | 38 return false; |
47 } | 39 } |
48 | |
49 if (0 == expected.count()) { | 40 if (0 == expected.count()) { |
50 return true; | 41 return true; |
51 } | 42 } |
52 | |
53 // skia:2834. RTree doesn't always return results in order. | |
54 SkTQSort(expected.begin(), expected.end() -1); | |
55 SkTQSort(found.begin(), found.end() -1); | |
56 return found == expected; | 43 return found == expected; |
57 } | 44 } |
58 | 45 |
59 static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, SkRect rec
ts[], | 46 static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, SkRect rec
ts[], |
60 SkRTree& tree) { | 47 const SkRTree& tree) { |
61 for (size_t i = 0; i < NUM_QUERIES; ++i) { | 48 for (size_t i = 0; i < NUM_QUERIES; ++i) { |
62 SkTDArray<unsigned> hits; | 49 SkTDArray<unsigned> hits; |
63 SkRect query = random_rect(rand); | 50 SkRect query = random_rect(rand); |
64 tree.search(query, &hits); | 51 tree.search(query, &hits); |
65 REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); | 52 REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); |
66 } | 53 } |
67 } | 54 } |
68 | 55 |
69 static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) { | 56 DEF_TEST(RTree, reporter) { |
70 SkASSERT(rtree); | |
71 | |
72 int expectedDepthMin = -1; | 57 int expectedDepthMin = -1; |
73 int expectedDepthMax = -1; | |
74 | |
75 int tmp = NUM_RECTS; | 58 int tmp = NUM_RECTS; |
76 while (tmp > 0) { | 59 while (tmp > 0) { |
77 tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN), | 60 tmp -= static_cast<int>(pow(static_cast<double>(SkRTree::kMaxChildren), |
78 static_cast<double>(expectedDepthMin + 1))); | 61 static_cast<double>(expectedDepthMin + 1))); |
79 ++expectedDepthMin; | 62 ++expectedDepthMin; |
80 } | 63 } |
81 | 64 |
| 65 int expectedDepthMax = -1; |
82 tmp = NUM_RECTS; | 66 tmp = NUM_RECTS; |
83 while (tmp > 0) { | 67 while (tmp > 0) { |
84 tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN), | 68 tmp -= static_cast<int>(pow(static_cast<double>(SkRTree::kMinChildren), |
85 static_cast<double>(expectedDepthMax + 1))); | 69 static_cast<double>(expectedDepthMax + 1))); |
86 ++expectedDepthMax; | 70 ++expectedDepthMax; |
87 } | 71 } |
88 | 72 |
89 SkRandom rand; | 73 SkRandom rand; |
90 SkAutoTMalloc<SkRect> rects(NUM_RECTS); | 74 SkAutoTMalloc<SkRect> rects(NUM_RECTS); |
91 for (size_t i = 0; i < NUM_ITERATIONS; ++i) { | 75 for (size_t i = 0; i < NUM_ITERATIONS; ++i) { |
92 rtree->clear(); | 76 SkRTree rtree; |
93 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); | 77 REPORTER_ASSERT(reporter, 0 == rtree.getCount()); |
94 | 78 |
95 for (int j = 0; j < NUM_RECTS; j++) { | 79 for (int j = 0; j < NUM_RECTS; j++) { |
96 rects[j] = random_rect(rand); | 80 rects[j] = random_rect(rand); |
97 } | 81 } |
98 | 82 |
99 rtree->insert(&rects, NUM_RECTS); | 83 rtree.insert(&rects, NUM_RECTS); |
100 SkASSERT(rects); // SkRTree doesn't take ownership of rects. | 84 SkASSERT(rects); // SkRTree doesn't take ownership of rects. |
101 | 85 |
102 run_queries(reporter, rand, rects, *rtree); | 86 run_queries(reporter, rand, rects, rtree); |
103 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); | 87 REPORTER_ASSERT(reporter, NUM_RECTS == rtree.getCount()); |
104 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && | 88 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree.getDepth() && |
105 expectedDepthMax >= rtree->getDepth()); | 89 expectedDepthMax >= rtree.getDepth()); |
106 } | 90 } |
107 } | 91 } |
108 | |
109 DEF_TEST(RTree, reporter) { | |
110 SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); | |
111 SkAutoUnref au(rtree); | |
112 rtree_test_main(rtree, reporter); | |
113 | |
114 // Rtree that orders input rectangles on deferred insert. | |
115 SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, fals
e); | |
116 SkAutoUnref auo(unsortedRtree); | |
117 rtree_test_main(unsortedRtree, reporter); | |
118 } | |
OLD | NEW |