Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SG_Tree.cpp « SceneGraph « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bef246533a6bb78895e7276e3d22d009fce709b0 (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
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
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 * Bounding Box
 */

/** \file gameengine/SceneGraph/SG_Tree.cpp
 *  \ingroup bgesg
 */


#include <math.h>
 
#include "SG_BBox.h"
#include "SG_Tree.h"
#include "SG_Node.h"

SG_Tree::SG_Tree() :
		m_left(NULL),
		m_right(NULL),
		m_parent(NULL),
		m_radius(0.0),
		m_client_object(NULL)
{
}

SG_Tree::SG_Tree(SG_Tree* left, SG_Tree* right) :
		m_left(left),
		m_right(right),
		m_parent(NULL),
		m_client_object(NULL)
{
	if (m_left)
	{
		m_bbox = m_left->m_bbox;
		m_left->m_parent = this;
	}
	if (m_right)
	{
		m_bbox += m_right->m_bbox;
		m_right->m_parent = this;
	}
	m_center = (m_bbox.m_min + m_bbox.m_max)/2.0;
	m_radius = (m_bbox.m_max - m_bbox.m_min).length();
}
	
SG_Tree::SG_Tree(SG_Node* client) :
		m_left(NULL),
		m_right(NULL),
		m_parent(NULL),
		m_client_object(client)
{
	m_bbox = SG_BBox(client->BBox(), client->GetWorldTransform());
	m_center = (m_bbox.m_min + m_bbox.m_max)/2.0;
	m_radius = (m_bbox.m_max - m_bbox.m_min).length();
}

SG_Tree::~SG_Tree() 
{
}
	
MT_Scalar SG_Tree::volume() const
{
	return m_bbox.volume();
}
	
void SG_Tree::dump() const
{
	if (m_left)
		m_left->dump();
	if (m_client_object)
		std::cout << m_client_object << std::endl;
	else
		std::cout << this << " ";
	if (m_right)
		m_right->dump();
}

SG_Tree* SG_Tree::Left() const
{
	return m_left;
}

SG_Tree* SG_Tree::Right() const
{
	return m_right;
}

SG_Node* SG_Tree::Client() const
{
	return m_client_object;
}

SG_Tree* SG_Tree::Find(SG_Node *node)
{
	if (m_client_object == node)
		return this;
	
	SG_Tree *left = m_left, *right = m_right;
	
	if (left && right)
	{
		if (right->m_bbox.intersects(node->BBox()))
			std::swap(left, right);
	}
		
	if (left)
	{
		SG_Tree* ret = left->Find(node);
		if (ret) return ret;
	}
	
	if (right)
	{
		SG_Tree* ret = right->Find(node);
		if (ret) return ret;
	}
	
	return NULL;
}

void SG_Tree::get(MT_Point3 *box) const
{
	MT_Transform identity;
	identity.setIdentity();
	m_bbox.get(box, identity);
}

bool SG_Tree::inside(const MT_Point3 &point) const
{
	return m_bbox.inside(point);
}

const SG_BBox& SG_Tree::BBox() const
{
	return m_bbox;
}

void SG_Tree::SetLeft(SG_Tree *left)
{
	m_left = left;
	m_bbox += left->m_bbox;
	m_center = (m_bbox.m_min + m_bbox.m_max)/2.0;
	m_radius = (m_bbox.m_max - m_bbox.m_min).length();
}

void SG_Tree::SetRight(SG_Tree *right)
{
	m_right = right;
	m_bbox += right->m_bbox;
	m_center = (m_bbox.m_min + m_bbox.m_max)/2.0;
	m_radius = (m_bbox.m_max - m_bbox.m_min).length();
}

/**
 * A Half array is a square 2d array where cell(x, y) is undefined
 * if x < y.
 */
template<typename T>
class HalfArray
{
	std::vector<std::vector<T> > m_array;
public:
	HalfArray() {}
	~HalfArray() {}
	
	void resize(unsigned int size)
	{
		m_array.resize(size);
		for ( unsigned int i = 0; i < size; i++)
		{
			m_array[i].resize(size - i);
		}
	}
	
	T& operator() (unsigned int x, unsigned int y)
	{
		assert(x >= y);
		return m_array[y][x - y];
	}
	
	void erase_column (unsigned int x)
	{
		for (unsigned int y = 0; y <= x; y++)
			m_array[y].erase(m_array[y].begin() + x - y);
	}

	void delete_column (unsigned int x)
	{
		for (unsigned int y = 0; y < x; y++)
		{
			delete m_array[y][x - y];
			m_array[y].erase(m_array[y].begin() + x - y);
		}
	}
	
	void erase_row (unsigned int y)
	{
		m_array.erase(m_array.begin() + y);
	}
};

SG_TreeFactory::SG_TreeFactory()
{
}

SG_TreeFactory::~SG_TreeFactory()
{
}
	
void SG_TreeFactory::Add(SG_Node* client)
{
	if (client)
		m_objects.insert(new SG_Tree(client));
}

void SG_TreeFactory::Add(SG_Tree* tree)
{
	m_objects.insert(tree);
}

SG_Tree* SG_TreeFactory::MakeTreeDown(SG_BBox &bbox)
{
	if (m_objects.empty())
		return NULL;
	if (m_objects.size() == 1)
		return *m_objects.begin();
		
	TreeSet::iterator it = m_objects.begin();
	SG_Tree *root = *it;
	if (m_objects.size() == 2)
	{
		root->SetRight(*(++it));
		return root;
	}
		
	if (m_objects.size() == 3)
	{
		root->SetLeft(*(++it));
		root->SetRight(*(++it));
		return root;
	}

	if (bbox.volume() < 1.0)
		return MakeTreeUp();
		
	SG_TreeFactory lefttree;
	SG_TreeFactory righttree;
	
	SG_BBox left, right;
	int hasleft = 0, hasright = 0;
	bbox.split(left, right);
	
	if (left.test(root->BBox()) == SG_BBox::INSIDE)
	{
		lefttree.Add(root);
		root = NULL;
	}
	
	if (root && right.test(root->BBox()) == SG_BBox::INSIDE)
	{
		righttree.Add(root);
		root = NULL;
	}
	
	for (++it; it != m_objects.end(); ++it)
	{
		switch (left.test((*it)->BBox()))
		{
			case SG_BBox::INSIDE:
				// Object is inside left tree;
				lefttree.Add(*it);
				hasleft++;
				break;
			case SG_BBox::OUTSIDE:
				righttree.Add(*it);
				hasright++;
				break;
			case SG_BBox::INTERSECT:
				if (left.inside((*it)->Client()->GetWorldPosition()))
				{
					lefttree.Add(*it);
					hasleft++;
				}
				else {
					righttree.Add(*it);
					hasright++;
				}
				break;
		}
	}
	std::cout << "Left: " << hasleft << " Right: " << hasright << " Count: " << m_objects.size() << std::endl;
	
	SG_Tree *leftnode = NULL;
	if (hasleft)
		leftnode = lefttree.MakeTreeDown(left);
	
	SG_Tree *rightnode = NULL;
	if (hasright)
		rightnode = righttree.MakeTreeDown(right);
		
	if (!root)
		root = new SG_Tree(leftnode, rightnode);
	else
	{
		if (leftnode)
			root->SetLeft(leftnode);
		if (rightnode)
			root->SetRight(rightnode);
	}

	return root;
}

SG_Tree* SG_TreeFactory::MakeTree()
{
	if (m_objects.size() < 8)
		return MakeTreeUp();

	TreeSet::iterator it = m_objects.begin();
	SG_BBox bbox((*it)->BBox());
	for (++it; it != m_objects.end(); ++it)
		bbox += (*it)->BBox();
	
	return MakeTreeDown(bbox);
}

SG_Tree* SG_TreeFactory::MakeTreeUp()
{
	unsigned int num_objects = m_objects.size();
	
	if (num_objects < 1)
		return NULL;
	if (num_objects < 2)
		return *m_objects.begin();

	HalfArray<SG_Tree*> sizes;
	sizes.resize(num_objects);
	
	unsigned int x, y;
	TreeSet::iterator xit, yit;
	for ( y = 0, yit = m_objects.begin(); y < num_objects; y++, ++yit)
	{
		sizes(y, y) = *yit;
		xit = yit;
		for ( x = y+1, ++xit; x < num_objects; x++, ++xit)
		{
			sizes(x, y) = new SG_Tree(*xit, *yit);
			
		}
	}
	while (num_objects > 2)
	{
		/* Find the pair of bboxes that produce the smallest combined bbox. */
		unsigned int minx = UINT_MAX, miny = UINT_MAX;
		MT_Scalar min_volume = FLT_MAX;
		SG_Tree *min = NULL;
		//char temp[16];
		for ( y = 0; y < num_objects; y++)
		{
			for ( x = y+1; x < num_objects; x++)
			{
				if (sizes(x, y)->volume() < min_volume)
				{
					min = sizes(x, y);
					minx = x;
					miny = y;
					min_volume = sizes(x, y)->volume();
				}
			}
		}
		
		/* Remove other bboxes that contain the two bboxes */
		sizes.delete_column(miny);
		
		for ( x = miny + 1; x < num_objects; x++)
		{
			if (x == minx)
				continue;
			delete sizes(x, miny);
		}
		sizes.erase_row(miny);
		
		num_objects--;
		minx--;
		sizes(minx, minx) = min;
		for ( x = minx + 1; x < num_objects; x++)
		{
			delete sizes(x, minx);
			sizes(x, minx) = new SG_Tree(min, sizes(x, x));
		}
		for ( y = 0; y < minx; y++)
		{
			delete sizes(minx, y);
			sizes(minx, y) = new SG_Tree(sizes(y, y), min);
		}
	}
	return sizes(1, 0);
}