2012-09-05 16:10:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkRTree.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "Test.h"
|
2012-09-05 16:10:59 +00:00
|
|
|
|
2012-09-26 13:08:56 +00:00
|
|
|
static const int NUM_RECTS = 200;
|
2012-09-05 16:10:59 +00:00
|
|
|
static const size_t NUM_ITERATIONS = 100;
|
|
|
|
static const size_t NUM_QUERIES = 50;
|
|
|
|
|
2014-08-27 17:39:42 +00:00
|
|
|
static SkRect random_rect(SkRandom& rand) {
|
|
|
|
SkRect rect = {0,0,0,0};
|
2012-09-05 16:10:59 +00:00
|
|
|
while (rect.isEmpty()) {
|
2014-08-27 17:39:42 +00:00
|
|
|
rect.fLeft = rand.nextRangeF(0, 1000);
|
|
|
|
rect.fRight = rand.nextRangeF(0, 1000);
|
|
|
|
rect.fTop = rand.nextRangeF(0, 1000);
|
|
|
|
rect.fBottom = rand.nextRangeF(0, 1000);
|
2012-09-05 16:10:59 +00:00
|
|
|
rect.sort();
|
|
|
|
}
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2014-10-02 14:41:56 +00:00
|
|
|
static bool verify_query(SkRect query, SkRect rects[], SkTDArray<unsigned>& found) {
|
|
|
|
SkTDArray<unsigned> expected;
|
2012-09-05 16:10:59 +00:00
|
|
|
// manually intersect with every rectangle
|
2012-09-26 13:08:56 +00:00
|
|
|
for (int i = 0; i < NUM_RECTS; ++i) {
|
2014-10-02 14:41:56 +00:00
|
|
|
if (SkRect::Intersects(query, rects[i])) {
|
|
|
|
expected.push(i);
|
2012-09-05 16:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 02:01:13 +00:00
|
|
|
if (expected.count() != found.count()) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-05 16:10:59 +00:00
|
|
|
if (0 == expected.count()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return found == expected;
|
|
|
|
}
|
|
|
|
|
2014-10-02 14:41:56 +00:00
|
|
|
static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, SkRect rects[],
|
2014-11-18 17:27:49 +00:00
|
|
|
const SkRTree& tree) {
|
2012-09-13 13:25:30 +00:00
|
|
|
for (size_t i = 0; i < NUM_QUERIES; ++i) {
|
2014-10-02 14:41:56 +00:00
|
|
|
SkTDArray<unsigned> hits;
|
2014-08-27 17:39:42 +00:00
|
|
|
SkRect query = random_rect(rand);
|
2012-09-05 16:10:59 +00:00
|
|
|
tree.search(query, &hits);
|
|
|
|
REPORTER_ASSERT(reporter, verify_query(query, rects, hits));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 17:27:49 +00:00
|
|
|
DEF_TEST(RTree, reporter) {
|
2012-09-05 16:10:59 +00:00
|
|
|
int expectedDepthMin = -1;
|
|
|
|
int tmp = NUM_RECTS;
|
|
|
|
while (tmp > 0) {
|
2014-11-18 17:27:49 +00:00
|
|
|
tmp -= static_cast<int>(pow(static_cast<double>(SkRTree::kMaxChildren),
|
|
|
|
static_cast<double>(expectedDepthMin + 1)));
|
2012-09-05 16:10:59 +00:00
|
|
|
++expectedDepthMin;
|
|
|
|
}
|
|
|
|
|
2014-11-18 17:27:49 +00:00
|
|
|
int expectedDepthMax = -1;
|
2012-09-05 16:10:59 +00:00
|
|
|
tmp = NUM_RECTS;
|
|
|
|
while (tmp > 0) {
|
2014-11-18 17:27:49 +00:00
|
|
|
tmp -= static_cast<int>(pow(static_cast<double>(SkRTree::kMinChildren),
|
|
|
|
static_cast<double>(expectedDepthMax + 1)));
|
2012-09-05 16:10:59 +00:00
|
|
|
++expectedDepthMax;
|
|
|
|
}
|
|
|
|
|
2014-10-27 17:27:10 +00:00
|
|
|
SkRandom rand;
|
|
|
|
SkAutoTMalloc<SkRect> rects(NUM_RECTS);
|
2012-09-13 13:25:30 +00:00
|
|
|
for (size_t i = 0; i < NUM_ITERATIONS; ++i) {
|
2014-11-18 17:27:49 +00:00
|
|
|
SkRTree rtree;
|
|
|
|
REPORTER_ASSERT(reporter, 0 == rtree.getCount());
|
2012-09-05 16:10:59 +00:00
|
|
|
|
2014-10-27 17:27:10 +00:00
|
|
|
for (int j = 0; j < NUM_RECTS; j++) {
|
|
|
|
rects[j] = random_rect(rand);
|
2012-09-05 16:10:59 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 21:44:27 +00:00
|
|
|
rtree.insert(rects.get(), NUM_RECTS);
|
2014-10-27 17:27:10 +00:00
|
|
|
SkASSERT(rects); // SkRTree doesn't take ownership of rects.
|
|
|
|
|
2014-11-18 17:27:49 +00:00
|
|
|
run_queries(reporter, rand, rects, rtree);
|
|
|
|
REPORTER_ASSERT(reporter, NUM_RECTS == rtree.getCount());
|
|
|
|
REPORTER_ASSERT(reporter, expectedDepthMin <= rtree.getDepth() &&
|
|
|
|
expectedDepthMax >= rtree.getDepth());
|
2012-09-05 16:10:59 +00:00
|
|
|
}
|
|
|
|
}
|