File: myx_gc_bsp.cpp

package info (click to toggle)
mysql-query-browser 1.2.5beta-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 63,792 kB
  • ctags: 46,485
  • sloc: pascal: 249,299; ansic: 80,111; cpp: 72,467; sh: 25,271; objc: 20,015; yacc: 10,755; java: 9,917; xml: 4,580; php: 2,806; python: 1,566; sql: 1,563; makefile: 1,452; perl: 3
file content (410 lines) | stat: -rw-r--r-- 12,768 bytes parent folder | download | duplicates (4)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @file myx_gc_bsp.cpp 
 * @brief Implementation of a special BSP (binary space partitioning) tree.
 * 
 */

#include "myx_gc_bsp.h"

//----------------- CBspTree -------------------------------------------------------------------------------------------

/**
 * Constructor of the tree class.
 *
 * @param width The width of the area we have to cover.
 * @param height The height of the area we have to cover.
 */
CBspTree::CBspTree(float width, float height)
{
  // The min size is our recursion stopper. No BSP entry is created with a width or height
  // smaller than the minimum size.
  FMinSize = 32;

  // The first split is vertical because usually a scene has a larger width than height.
  FRoot.isVertical = true;
  FRoot.bounds.lower.x = width;
  FRoot.bounds.lower.y = height;
  FRoot.splitPoint = width / 2;
}

//----------------------------------------------------------------------------------------------------------------------

CBspTree::~CBspTree(void)
{
  clear();
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Classifies the element's bounding box against the given BSP entry, that is, determines whether the bounds of the element
 * lie entirely on the left or on the right side of splitting line or on both sides.
 * Note: left/right can actually be above/below, depending on the splitter orientation, but for classification it does not matter.
 *
 * @param entry The entry of the BSP tree against which we classify.
 * @param element The element to classify.
 */
CBspTree::TClassification CBspTree::classify(TBspEntry* entry, CGraphicElement* element)
{
  TClassification result = IS_UNKNOWN;

  const TBoundingBox& bounds = element->bounds();
  if (entry->isVertical)
  {
    int leftCount = 0;
    int rightCount = 0;
    if (bounds.upper.x <= entry->splitPoint)
      ++leftCount;
    else
      ++rightCount;
    if (bounds.lower.x <= entry->splitPoint)
      ++leftCount;
    else
      ++rightCount;
    if (leftCount == 0)
      result = IS_RIGHT;
    else
      if (rightCount == 0)
        result = IS_LEFT;
      else
        result = IS_BOTH;
  }
  else
  {
    int aboveCount = 0;
    int belowCount = 0;
    if (bounds.upper.y <= entry->splitPoint)
      ++aboveCount;
    else
      ++belowCount;
    if (bounds.lower.y <= entry->splitPoint)
      ++aboveCount;
    else
      ++belowCount;
    if (aboveCount == 0)
      result = IS_RIGHT;
    else
      if (belowCount == 0)
        result = IS_LEFT;
      else
        result = IS_BOTH;
  };

  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Removes the given entry and all its child entries from the tree.
 *
 * @param entry The entry to be removed.
 */
void CBspTree::deleteEntry(TBspEntry* entry)
{
  if (entry->left != NULL)
    deleteEntry(entry->left);
  if (entry->right != NULL)
    deleteEntry(entry->right);

  delete entry;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Recurses down the subtree given by entry to find an element that occupies the given position.
 *
 * @param entry The top element of the subtree to search through.
 * @param point The position to look for.
 * @param singleHit True if only one hit is requested.
 */
CGraphicElement* CBspTree::findElement(TBspEntry* entry, TVertex point)
{
  CGraphicElement* result = NULL;

  if (entry != NULL)
  {
    if (entry->left != NULL || entry->right != NULL)
    {
      if (entry->isVertical)
      {
        if (point.x <= entry->splitPoint)
          result = findElement(entry->left, point);
        else
          result = findElement(entry->right, point);
      }
      else
      {
        if (point.y <= entry->splitPoint)
          result = findElement(entry->left, point);
        else
          result = findElement(entry->right, point);
      };
    }
    else
    {
      // Found a leaf. Return the last entry that fits as it is the top most.
      for (CGraphicElementList::reverse_iterator iterator = entry->elements.rbegin(); iterator != entry->elements.rend(); ++iterator)
      {
        CGraphicElement* element = *iterator;
        if (element->visible() && element->containsPoint(point.x, point.y))
        {
          result = element;
          break;
        };
      };
    };
  };
  return result;
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Recurses down the subtree given by entry to find elements that occupy the given position.
 *
 * @param entry The top element of the subtree to search through.
 * @param point The position to look for.
 * @param singleHit True if only one hit is requested.
 */
void CBspTree::findElements(TBspEntry* entry, TVertex point, CHitResults* hits)
{
  if (entry != NULL)
  {
    if (entry->left != NULL || entry->right != NULL)
    {
      if (entry->isVertical)
      {
        if (point.x <= entry->splitPoint)
          findElements(entry->left, point, hits);
        else
          findElements(entry->right, point, hits);
      }
      else
      {
        if (point.y <= entry->splitPoint)
          findElements(entry->left, point, hits);
        else
          findElements(entry->right, point, hits);
      };
    }
    else
    {
      // Found a leaf. Check its elements. Go backwards through the list to get the latest element
      // (which is rendered as top most) first.
      for (CGraphicElementList::reverse_iterator iterator = entry->elements.rbegin(); iterator != entry->elements.rend(); ++iterator)
      {
        CGraphicElement* element = *iterator;
        if (element->visible() && element->containsPoint(point.x, point.y))
          hits->addHit(*iterator);
      };
    };
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Main method to add an element to the tree. It calls itself recursively.
 *
 * @param entry The current entry used for classification.
 * @param element The element to instert.
 */
void CBspTree::insertElement(TBspEntry* entry, CGraphicElement* element)
{
  // Check if we have reached the smallest splitting size. If so add the element to the given entry,
  // otherwise recurse down.
  float width = entry->bounds.lower.x - entry->bounds.upper.x;
  float height = entry->bounds.lower.y - entry->bounds.upper.y;
  if (width <= FMinSize || height <= FMinSize)
    entry->elements.push_back(element);
  else
  {
    TClassification classification = classify(entry, element);

    if (classification == IS_LEFT || classification == IS_BOTH)
    {
      // Go down the left sub tree. Create it if not yet done.
      if (entry->left == NULL)
      {
        TBspEntry* newEntry = new TBspEntry();
        entry->left = newEntry;
        newEntry->isVertical = !entry->isVertical;
        newEntry->bounds = entry->bounds;
        if (entry->isVertical)
        {
          newEntry->bounds.lower.x = (newEntry->bounds.lower.x + newEntry->bounds.upper.x) / 2;
          newEntry->splitPoint = (newEntry->bounds.lower.y + newEntry->bounds.upper.y) / 2;
        }
        else
        {
          newEntry->bounds.lower.y = (newEntry->bounds.lower.y + newEntry->bounds.upper.y) / 2;
          newEntry->splitPoint = (newEntry->bounds.lower.x + newEntry->bounds.upper.x) / 2;
        };
      };
      insertElement(entry->left, element);
    };

    if (classification == IS_RIGHT || classification == IS_BOTH)
    {
      // Go down the right sub tree. Create it if not yet done.
      if (entry->right == NULL)
      {
        TBspEntry* newEntry = new TBspEntry();
        entry->right = newEntry;
        newEntry->isVertical = !entry->isVertical;
        newEntry->bounds = entry->bounds;
        if (entry->isVertical)
        {
          newEntry->bounds.upper.x = (newEntry->bounds.lower.x + newEntry->bounds.upper.x) / 2;
          newEntry->splitPoint = (newEntry->bounds.lower.y + newEntry->bounds.upper.y) / 2;
        }
        else
        {
          newEntry->bounds.upper.y = (newEntry->bounds.lower.y + newEntry->bounds.upper.y) / 2;
          newEntry->splitPoint = (newEntry->bounds.lower.x + newEntry->bounds.upper.x) / 2;
        };
      };

      insertElement(entry->right, element);
    };
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Used as debugging aid. Renders the given entry via OpenGL.
 *
 * @param entry The subtree to render.
 */
void CBspTree::renderEntry(TBspEntry* entry)
{
  static GLubyte colorVerticalLeft[4] = {255, 0, 0, 10};
  static GLubyte colorVerticalRight[4] = {0, 255, 0, 10};
  static GLubyte colorHorizontalLeft[4] = {0, 0, 255, 10};
  static GLubyte colorHorizontalRight[4] = {255, 255, 0, 10};

  if (entry->left != NULL)
  {
    if (entry->isVertical)
      glColor4ubv(colorVerticalLeft);
    else
      glColor4ubv(colorHorizontalLeft);

    glBegin(GL_POLYGON);
      glVertex2f(entry->bounds.upper.x, entry->bounds.upper.y);
      glVertex2f(entry->bounds.lower.x, entry->bounds.upper.y);
      glVertex2f(entry->bounds.lower.x, entry->bounds.lower.y);
      glVertex2f(entry->bounds.upper.x, entry->bounds.lower.y);
    glEnd();

    renderEntry(entry->left);
  };

  if (entry->right != NULL)
  {
    if (entry->isVertical)
      glColor4ubv(colorVerticalRight);
    else
      glColor4ubv(colorHorizontalRight);

    glBegin(GL_POLYGON);
      glVertex2f(entry->bounds.upper.x, entry->bounds.upper.y);
      glVertex2f(entry->bounds.lower.x, entry->bounds.upper.y);
      glVertex2f(entry->bounds.lower.x, entry->bounds.lower.y);
      glVertex2f(entry->bounds.upper.x, entry->bounds.lower.y);
    glEnd();

    renderEntry(entry->right);
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Adds the given element to the tree according to its bounds.
 *
 * @param element The element to add.
 */
void CBspTree::addElement(CGraphicElement* element)
{
  insertElement(&FRoot, element);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Removes all entries from the tree.
 */
void CBspTree::clear(void)
{
  if (FRoot.left != NULL)
  {
    deleteEntry(FRoot.left);
    FRoot.left = NULL;
  };
  if (FRoot.right != NULL)
  {
    deleteEntry(FRoot.right);
    FRoot.right = NULL;
  };
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Returns the first (top most) element in the cache at the given position or NULL if there is none.
 *
 * @param point The position to look at (view space).
 * @return NULL or the top most element at that position.
 */
CGraphicElement* CBspTree::findElement(TVertex point)
{
  return findElement(&FRoot, point);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Searches the tree for a given position and returns the elements located there.
 *
 * @param point The point at which we look for elements.
 * @param singleHit True if only hit is required.
 * @param hits A list that takes all found elements.
 */
void CBspTree::findElements(TVertex point, CHitResults* hits)
{
  findElements(&FRoot, point, hits);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 * Renders the areas defined currently in the tree as an debug aid using normal OpenGL calls.
 * A rendering context must be active and setup correctly for the output.
 */
void CBspTree::render(void)
{
  renderEntry(&FRoot);
}

//----------------------------------------------------------------------------------------------------------------------