summaryrefslogtreecommitdiff
path: root/cad/src/experimental/oleksandr/boxtree.cpp
blob: 9ccf000a10e2c5203a28f211fb9739cfb5a25079 (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
// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
/*
  Name: boxtree.cpp
  Author: Oleksandr Shevchenko
  Description: class for boxtree representation  
*/

#include "boxtree.h"

//----------------------------------------------------------------------------
// Constructor

BoxTree::BoxTree():
	mLeft(								// first leaf for tree
		0),
	mRight(								// second leaf for tree
		0)
{

}

//----------------------------------------------------------------------------
// Destructor

BoxTree::~BoxTree()
{

}

//----------------------------------------------------------------------------
// Add()
//
// add box into container
//
void BoxTree::Add(
	Box * box)
{
	mBoxes.Add(box);
	mBox.Enclose(box->Min());
	mBox.Enclose(box->Max());
}

//----------------------------------------------------------------------------
// BestAxe()
//
// best split axe
//
int BoxTree::BestAxe()
{
	int lx = 0;
	int rx = 0;
	int ly = 0;
	int ry = 0;
	int lz = 0;
	int rz = 0;
	int n = Size();
	for (int i=0; i<n; i++)
	{
		Triple center = mBoxes[i]->Center() - mBox.Center();
		if (center.X()<0) lx++; else rx++;
		if (center.Y()<0) ly++; else ry++;
		if (center.Z()<0) lz++; else rz++;
	}
	int x_break = (lx != 0 && rx != 0);
	int y_break = (ly != 0 && ry != 0);
	int z_break = (lz != 0 && rz != 0);
	Triple d = mBox.Extent();
	if (x_break)
	{
		if (y_break)
		{
			if (z_break)
			{
				if (d.X()>d.Y())
				{
					if (d.X()>d.Z())
						return (3);
					else
						return (1);
				}
				else
				{
					if (d.Y()>d.Z())
						return (2);
					else
						return (1);
				}
			}
			else
			{
				if (d.X()>d.Y())
					return (3);
				else
					return (2);
			}
		}
		else
		{
			if (z_break)
			{
				if (d.X()>d.Z())
					return (3);
				else
					return (1);
			}
			else
			{
				return (3);
			}
		}
	}
	else
	{
		if (y_break)
		{
			if (z_break)
			{
				if (d.Y()>d.Z())
					return (2);
				else
					return (1);
			}
			else
			{
				return (2);
			}
		}
		else
		{
			if (z_break)
			{
				return (1);
			}
			else
			{
				return (0);
			}
		}
	}
}

//----------------------------------------------------------------------------
// BuildTree()
//
// create bounding volume box tree
//
int BoxTree::BuildTree()
{
	int i;
	if (Empty()) i = Split();
	if (!i) return 0;
	if (Empty()) return 1;
	if (Left()) i = Left()->BuildTree();
	if (!i) return 0;
	if (Right()) i = Right()->BuildTree();
	if (!i) return 0;
	return 1;
}

//----------------------------------------------------------------------------
// Delete()
//
// recursive delete
//
void BoxTree::Delete(
	BoxTree * tree)
{
	if (tree)
	{
		Delete(tree->Left());
		Delete(tree->Right());
		tree->DeleteLeaf();
	}
}

//----------------------------------------------------------------------------
// Duplicate()
//
// delete duplicate entities
//
void BoxTree::Duplicate(
	Box * base,
	int * ia)
{
	Duplicate(base,ia,this);
}

//----------------------------------------------------------------------------
// Duplicate()
//
// delete duplicate entities
//
void BoxTree::Duplicate(
	Box * base, 
	int * ia,
	BoxTree * bt)
{
	if (bt)
	{
		if (!(bt->Left() && bt->Right()))
		{
			for (int il=0; il<bt->mBoxes.Size(); il++)
			{
				Box * bi = bt->mBoxes[il];
				int i = int(bi - base);
				for (int jl=il+1; jl<bt->mBoxes.Size(); jl++)
				{
					Box * bj = bt->mBoxes[jl];
					int j = int(bj - base);
					if (ia[j]>0)
					{
						ia[j] = -ia[i];
					}
				}
			}
		}
		Duplicate(base,ia,bt->Left());
		Duplicate(base,ia,bt->Right());
	}
}

//----------------------------------------------------------------------------
// Initialize()
//
// formation of main box
//
int BoxTree::Initialize(
	const Container<Box *> & boxes)
{
	int n = boxes.Size();
	for (int i=0; i<n; i++)
	{
		Add(boxes[i]);
	}
	return (BuildTree());
}

//----------------------------------------------------------------------------
// Split()
//
// split box
//
int BoxTree::Split()
{
	int const Number = 1;
	int i;
	int n = Size();
	if (n<=Number) return 1;
	//  best split axe
	int variant = BestAxe();
	//  now we can subdivide
	if(!AllocateTree()) return 0;
	//  put entities into boxes
	for (i=0; i<n; i++)
	{
		switch (WhichBox(variant,i))
		{
		case 0:
			Left()->Add(mBoxes[i]);
			break;
		case 1:
			Right()->Add(mBoxes[i]);
			break;
		}
	}
	//  calculate new subboxes
	if (Left()->Size() && Right()->Size())
	{
		mBoxes.Empty();
	}
	else
	{
		FreeTree();
	}
	return 1;
}