1 //
2 // TestBadData.cpp
3 //
4 
5 // RTree
6 #include <RTree.h>
7 
8 #include <iostream>
9 #include <fstream>
10 #include <string>
11 #include <vector>
12 
13 using namespace std;
14 
15 typedef int ValueType;
16 typedef long long CoordType;
17 
18 struct Rect
19 {
20     Rect() noexcept = default;
21 
RectRect22     constexpr Rect(CoordType a_minX, CoordType a_minY, CoordType a_maxX, CoordType a_maxY) noexcept
23         : fMin{ a_minX, a_minY }
24         , fMax{ a_maxX, a_maxY }
25     {
26     }
27 
28 
29     CoordType fMin[2] = { 0, };
30     CoordType fMax[2] = { 0, };
31 };
32 
33 
MySearchCallback(ValueType id)34 bool MySearchCallback(ValueType id)
35 {
36     cout << "Hit data rect " << id << "\n";
37     return true; // keep going
38 }
39 
40 
main(int argc,char * argv[])41 int main(int argc, char* argv[])
42 {
43     if (argc < 2) {
44         std::cout << "Usage: " << argv[0] << " inFile\n";
45         return -1;
46     }
47 
48     using RectVector = std::vector<Rect>;
49     RectVector rectVector;
50 
51     // read the data
52     {
53         ifstream inFile(argv[1]);
54         if (!inFile.is_open()) {
55             std::cerr << "Can't open input file\n";
56             return -1;
57         }
58         while (!inFile.eof()) {
59             // security and robustness be damned
60             CoordType xmin, ymin, xmax, ymax;
61             string dummy;
62             inFile >> xmin >> ymin >> xmax >> ymax;
63             cout << xmin << " " << ymin << " " << xmax << " " << ymax << "\n";
64             rectVector.emplace_back(xmin, ymin, xmin + xmax, ymin + ymax);
65         }
66     }
67 
68     using MyTree = RTree<ValueType, CoordType, 2, float>;
69     MyTree tree;
70 
71     int i, nhits;
72     cout << "number of rectangles is " << rectVector.size() << "\n";
73 
74     for (i = 0; i < rectVector.size(); i++)
75     {
76         tree.Insert(rectVector[i].fMin, rectVector[i].fMax, i); // Note, all values including zero are fine in this version
77     }
78 
79     Rect search_rect(6, 4, 10, 6);
80     nhits = tree.Search(search_rect.fMin, search_rect.fMax, MySearchCallback);
81 
82     cout << "Search resulted in " << nhits << " hits\n";
83 
84     // Iterator test
85     int itIndex = 0;
86     MyTree::Iterator it;
87     for (tree.GetFirst(it);
88         !tree.IsNull(it);
89         tree.GetNext(it))
90     {
91         int value = tree.GetAt(it);
92 
93         CoordType boundsMin[2] = { 0,0 };
94         CoordType boundsMax[2] = { 0,0 };
95         it.GetBounds(boundsMin, boundsMax);
96         cout << "it[" << itIndex++ << "] " << value << " = (" << boundsMin[0] << "," << boundsMin[1] << "," << boundsMax[0] << "," << boundsMax[1] << ")\n";
97     }
98 
99     // Iterator test, alternate syntax
100     itIndex = 0;
101     tree.GetFirst(it);
102     while (!it.IsNull())
103     {
104         CoordType value = *it;
105         ++it;
106         cout << "it[" << itIndex++ << "] " << value << "\n";
107     }
108 
109     return 0;
110 }
111 
112