summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/tests/rtree-performance-test.cpp
blob: cf4bcd76e56821f36152f196d01a2b7b40d81607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright 2010  Evangelos Katsikaros <vkatsikaros at yahoo dot gr>
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */

#include <2geom/toys/toy-framework-2.h>

#include <sstream>
#include <getopt.h>

#include <SpatialIndex.h>
#include <glib.h>
//#include <glib/gtypes.h>

using namespace Geom;

// cmd argument stuff
char* arg_area_limit = NULL;
bool arg_area_limit_set = false;
bool arg_debug = false;

int limit = 0;

// spatial index ID management
SpatialIndex::id_type indexID;

// list of rectangles
GList *items = NULL;

// tree of rectangles
SpatialIndex::ISpatialIndex *tree;

SpatialIndex::id_type test_indexID;

void add_rectangle( int x, int y );

/* Simple Visitor used to search the tree. When Data is encountered
 * we are supposed to call the render function of the Data
 * */
class SearchVisitor : public SpatialIndex::IVisitor {
public:
        
    void visitNode(const SpatialIndex::INode& n){
    }
            
    void visitData(const SpatialIndex::IData& d){
        /* this prototype: do nothing
         * otherwise, render on buffer
         * */
    }
        
    void visitData(std::vector<const SpatialIndex::IData*>& v) {
    }
};


/* we use the this visitor after each insertion in the tree
 * The purpose is to validate that everything was stored properly
 * and test the GList pointer storage. It has no other functional
 * purpose.
 * */
class TestSearchVisitor : public SpatialIndex::IVisitor {
public:
        
    void visitNode(const SpatialIndex::INode& n) {
    }
        
    void visitData(const SpatialIndex::IData& d){
        if( test_indexID == d.getIdentifier() ){
            byte* pData = 0;
            uint32_t cLen = sizeof(GList*);
            d.getData(cLen, &pData);
            //do something...
            GList* gl = reinterpret_cast<GList*>(pData);
            Geom::Rect *member_data = (Geom::Rect *)gl->data;
            double lala = member_data->bottom();            
            std::cout << "   Tree: " << lala << std::endl;
            
            delete[] pData;           
        }
    }
        
    void visitData(std::vector<const SpatialIndex::IData*>& v) {
    }
};


int main(int argc, char **argv) {
   
    int c;

    //--------------------------------------------------------------------------
    // read cmd options
    while (1) {
        static struct option long_options[] =
            {
                /* These options set a flag. */
                /* These options don't set a flag.
                   We distinguish them by their indices. */
                {"area-limit",	required_argument,	0, 'l'},
                {"help",		no_argument,		0, 'h'},
                {"debug",	no_argument,	0, 'd'},
                {0, 0, 0, 0}
            };
        /* getopt_long stores the option index here. */
        int option_index = 0;
    
        c = getopt_long (argc, argv, "l:h:d",
                         long_options, &option_index);

        /* Detect the end of the options. */
        if (c == -1){
            break;
        }
    
        switch (c)
        {
            case 'l':
                arg_area_limit = optarg;
                arg_area_limit_set = true;
                break;
            case 'h':
                std::cerr << "Usage:  " << argv[0] << " options\n" << std::endl ;
                std::cerr << 
                    "  -l  --area-limit=NUMBER  minimum number in node.\n" <<
                    "  -d  --debug              Enable debug info (list/tree related).\n" <<
                    "  -h  --help               Print this help.\n" << std::endl;
                exit(1);
                break;
            case 'd':
                arg_debug = true;
                break;                
            case '?':
                /* getopt_long already printed an error message. */
                break;
        
            default:
                abort ();
        }
    }

    // use some of the cmd options
    if(  arg_area_limit_set  ) {
        std::stringstream s1( arg_area_limit );
        s1 >> limit;
    }
    else {
        limit = 100;
    }
    // end cmd options
    //--------------------------------------------------------------------------


    double plow[2], phigh[2];
    // spatial index memory storage manager
    SpatialIndex::IStorageManager *mem_mngr;
    // initialize spatial indexing stuff
    mem_mngr = SpatialIndex::StorageManager::createNewMemoryStorageManager();
    // fillFactor, indexCapacity, leafCapacity, dimensionality=2, variant=R*, indexIdentifier
    tree = SpatialIndex::RTree::createNewRTree(*mem_mngr, 0.7, 25, 25, 2, SpatialIndex::RTree::RV_RSTAR, indexID);    

    //-------------------------------------------
    /* generate items. add_rectangle() stores them in both list and tree
     * add rect every (20, 20).
     * In area ((0,0), (1000, 1000)) add every (100,100)
     * */
    for( int x_coord = -limit; x_coord <= limit; x_coord += 20 ) {
        for( int y_coord = -limit; y_coord <= limit; y_coord += 20 ) {
            if( x_coord >= 0 && x_coord <= 1000 &&
                y_coord >= 0 && y_coord <= 1000 )
            {
                if( x_coord % 100 == 0 && y_coord % 100 == 0) {
                    add_rectangle( x_coord, y_coord );
                }
                else{
                    add_rectangle( x_coord, y_coord );
                }
            }
            else{
                add_rectangle( x_coord, y_coord );
            }
        
        }
    }
    std::cout << "Area of objects: ( -" << limit
              << ", -" << limit
              << " ), ( " << limit
              << ", " << limit
              << " )" << std::endl;
    std::cout << "Number of Objects (indexID): " << indexID << std::endl;
    // std::cout << "GListElements: " << g_list_length << std::endl;
   
    //-------------------------------------------
    // Traverse list
    Geom::Point sa_start = Point( 0, 0 );
    Geom::Point sa_end = Point( 1000, 1000 );
    Geom::Rect search_area = Rect( sa_start, sa_end );

    Timer list_timer;
    list_timer.ask_for_timeslice();
    list_timer.start();
    
    for (GList *list = items; list; list = list->next) {
        Geom::Rect *child = (Geom::Rect *)list->data;
        if ( search_area.intersects( *child ) )
        {
            /* this prototype: do nothing
             * otherwise, render on buffer
             * */
        }          
    }  
    Timer::Time the_list_time = list_timer.lap();
    
    std::cout << std::endl;
    std::cout << "GList (full scan): " << the_list_time << std::endl;
    
    //-------------------------------------------
    // Search tree - good case
    Timer tree_timer;
    tree_timer.ask_for_timeslice();
    tree_timer.start();

    /* We search only the (0,0), (1000, 1000) where the items are less dense.
     * We expect a good performance versus the list
     * */
    // TODO IMPORTANT !!! check the plow, phigh
    // plow[0] = x1; plow[1] = y1;
    // phigh[0] = x2; phigh[1] = y2;

    plow[0] = 0;
    plow[1] = 0;
    phigh[0] = 1000;
    phigh[1] = 1000;
    
    SpatialIndex::Region search_region = SpatialIndex::Region(plow, phigh, 2);
    SearchVisitor vis = SearchVisitor();
    tree->intersectsWithQuery( search_region, vis );
    
    Timer::Time the_tree_time = tree_timer.lap();
    std::cout << "Rtree (good): " << the_tree_time << std::endl;


    //-------------------------------------------
    // Search tree - worst case
    Timer tree_timer_2;
    tree_timer_2.ask_for_timeslice();
    tree_timer_2.start();

    /* search the whole area, so all items are returned */
    plow[0] = -limit - 100;
    plow[1] = -limit - 100;
    phigh[0] = limit + 100;
    phigh[1] = limit + 100;
    
    SpatialIndex::Region search_region_2 = SpatialIndex::Region(plow, phigh, 2);
    SearchVisitor vis_2 = SearchVisitor();
    tree->intersectsWithQuery( search_region_2, vis_2 );
    
    Timer::Time the_tree_time_2 = tree_timer_2.lap();
    std::cout << "Rtree (full scan): " << the_tree_time_2 << std::endl;
    
    return 0;
}



/* Adds rectangles in a GList and a SpatialIndex rtree
 * */
void add_rectangle( int x, int y ) {
    
    Geom::Point starting_point = Point( x, y );
    Geom::Point ending_point = Point( x + 10, y + 10 );
    Geom::Rect rect_to_add = Rect( starting_point, ending_point );
    items = g_list_append( items, &rect_to_add );

    if( arg_debug ) {
        // fetch the last rect from the list
        Geom::Rect *member_data = (Geom::Rect *)( g_list_last( items ) )->data;
        double lala = member_data->bottom();
        std::cout << "List (" << indexID << "): "  << lala;
    }

    /* Create a SpatialIndex region
     * plow = left-bottom corner
     * phigh = top-right corner
     * [0] = dimension X
     * [1] = dimension Y
     * */    
    double plow[2], phigh[2];    

    plow[0] = rect_to_add.left() ;
    plow[1] = rect_to_add.bottom();    
    phigh[0] = rect_to_add.right();
    phigh[1] = rect_to_add.top();
    
    SpatialIndex::Region r = SpatialIndex::Region(plow, phigh, 2);
    /* Store Glist pointer size and GList pointer as the associated data
     * In inkscape this can be used to directly call render hooked functions
     * from SPCanvasItems
     * */
    tree->insertData( sizeof(GList*), reinterpret_cast<const byte*>( g_list_last( items ) ), r, indexID);

    // tree->insertData(0, 0, r, indexID);
    /* not used. Store zero size and a null pointer as the associated data.
     * indexId is used to retrieve from a mapping each rect
     * (example a hash map, or the indexID is also vector index)
     * */

    if( arg_debug ) {
        test_indexID = indexID;
        /* every time we add a rect, search all the tree to find the last
         * inserted ID. This is not performance-wise good (rtree only good for
         * spatial queries) this is just used for debugging reasons
         * */
        plow[0] = -limit - 100;
        plow[1] = -limit - 100;
        phigh[0] = limit + 100;
        phigh[1] = limit + 100;
    
        SpatialIndex::Region test_search_region = SpatialIndex::Region(plow, phigh, 2);
        TestSearchVisitor test_vis = TestSearchVisitor();
        // search the tree for the region. Visitor implements the search function hooks
        tree->intersectsWithQuery( test_search_region, test_vis );
    }

    indexID++;
}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)(c-basic-offset . 4))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :