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"
|
2012-09-05 16:10:59 +00:00
|
|
|
#include "SkTSort.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2012-09-05 16:10:59 +00:00
|
|
|
|
|
|
|
static const size_t MIN_CHILDREN = 6;
|
|
|
|
static const size_t MAX_CHILDREN = 11;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
struct DataRect {
|
2014-08-27 17:39:42 +00:00
|
|
|
SkRect rect;
|
2012-09-05 16:10:59 +00:00
|
|
|
void* data;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static void random_data_rects(SkRandom& rand, DataRect out[], int n) {
|
2012-09-05 16:10:59 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
out[i].rect = random_rect(rand);
|
|
|
|
out[i].data = reinterpret_cast<void*>(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 17:39:42 +00:00
|
|
|
static bool verify_query(SkRect query, DataRect rects[],
|
2012-09-05 16:10:59 +00:00
|
|
|
SkTDArray<void*>& found) {
|
2014-08-27 17:39:42 +00:00
|
|
|
// TODO(mtklein): no need to do this after everything's SkRects
|
|
|
|
query.roundOut();
|
|
|
|
|
2012-09-05 16:10:59 +00:00
|
|
|
SkTDArray<void*> expected;
|
2014-08-27 17:39:42 +00:00
|
|
|
|
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-08-27 17:39:42 +00:00
|
|
|
if (SkRect::Intersects(query, rects[i].rect)) {
|
2012-09-05 16:10:59 +00:00
|
|
|
expected.push(rects[i].data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just cast to long since sorting by the value of the void*'s was being problematic...
|
2012-09-06 02:01:13 +00:00
|
|
|
SkTQSort(reinterpret_cast<long*>(expected.begin()),
|
2012-09-05 16:10:59 +00:00
|
|
|
reinterpret_cast<long*>(expected.end() - 1));
|
2012-09-06 02:01:13 +00:00
|
|
|
SkTQSort(reinterpret_cast<long*>(found.begin()),
|
2012-09-05 16:10:59 +00:00
|
|
|
reinterpret_cast<long*>(found.end() - 1));
|
|
|
|
return found == expected;
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, DataRect rects[],
|
2012-09-05 16:10:59 +00:00
|
|
|
SkRTree& tree) {
|
2012-09-13 13:25:30 +00:00
|
|
|
for (size_t i = 0; i < NUM_QUERIES; ++i) {
|
2012-09-05 16:10:59 +00:00
|
|
|
SkTDArray<void*> 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 17:27:47 +00:00
|
|
|
static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) {
|
2012-09-05 16:10:59 +00:00
|
|
|
DataRect rects[NUM_RECTS];
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, rtree);
|
2012-09-05 16:10:59 +00:00
|
|
|
|
|
|
|
int expectedDepthMin = -1;
|
|
|
|
int expectedDepthMax = -1;
|
|
|
|
|
|
|
|
int tmp = NUM_RECTS;
|
|
|
|
while (tmp > 0) {
|
2012-09-06 02:01:13 +00:00
|
|
|
tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN),
|
2012-09-05 16:10:59 +00:00
|
|
|
static_cast<double>(expectedDepthMin + 1)));
|
|
|
|
++expectedDepthMin;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = NUM_RECTS;
|
|
|
|
while (tmp > 0) {
|
|
|
|
tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN),
|
|
|
|
static_cast<double>(expectedDepthMax + 1)));
|
|
|
|
++expectedDepthMax;
|
|
|
|
}
|
|
|
|
|
2012-09-13 13:25:30 +00:00
|
|
|
for (size_t i = 0; i < NUM_ITERATIONS; ++i) {
|
2012-09-05 16:10:59 +00:00
|
|
|
random_data_rects(rand, rects, NUM_RECTS);
|
|
|
|
|
|
|
|
// First try bulk-loaded inserts
|
2012-09-26 13:08:56 +00:00
|
|
|
for (int i = 0; i < NUM_RECTS; ++i) {
|
2012-09-05 16:10:59 +00:00
|
|
|
rtree->insert(rects[i].data, rects[i].rect, true);
|
|
|
|
}
|
|
|
|
rtree->flushDeferredInserts();
|
2013-08-30 17:27:47 +00:00
|
|
|
run_queries(reporter, rand, rects, *rtree);
|
2012-09-05 16:10:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount());
|
|
|
|
REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() &&
|
|
|
|
expectedDepthMax >= rtree->getDepth());
|
|
|
|
rtree->clear();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == rtree->getCount());
|
|
|
|
|
|
|
|
// Then try immediate inserts
|
2012-09-26 13:08:56 +00:00
|
|
|
for (int i = 0; i < NUM_RECTS; ++i) {
|
2012-09-05 16:10:59 +00:00
|
|
|
rtree->insert(rects[i].data, rects[i].rect);
|
|
|
|
}
|
2013-08-30 17:27:47 +00:00
|
|
|
run_queries(reporter, rand, rects, *rtree);
|
2012-09-05 16:10:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount());
|
|
|
|
REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() &&
|
|
|
|
expectedDepthMax >= rtree->getDepth());
|
|
|
|
rtree->clear();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == rtree->getCount());
|
|
|
|
|
|
|
|
// And for good measure try immediate inserts, but in reversed order
|
|
|
|
for (int i = NUM_RECTS - 1; i >= 0; --i) {
|
|
|
|
rtree->insert(rects[i].data, rects[i].rect);
|
|
|
|
}
|
2013-08-30 17:27:47 +00:00
|
|
|
run_queries(reporter, rand, rects, *rtree);
|
2012-09-05 16:10:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount());
|
|
|
|
REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() &&
|
|
|
|
expectedDepthMax >= rtree->getDepth());
|
|
|
|
rtree->clear();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == rtree->getCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(RTree, reporter) {
|
2013-08-30 17:27:47 +00:00
|
|
|
SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN);
|
|
|
|
SkAutoUnref au(rtree);
|
|
|
|
rtree_test_main(rtree, reporter);
|
|
|
|
|
|
|
|
// Rtree that orders input rectangles on deferred insert.
|
2013-08-30 17:43:31 +00:00
|
|
|
SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, false);
|
|
|
|
SkAutoUnref auo(unsortedRtree);
|
|
|
|
rtree_test_main(unsortedRtree, reporter);
|
2013-08-30 17:27:47 +00:00
|
|
|
}
|