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

BSP_FragTree.cpp « intern « bsp « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3668db6cf7cb4c48ffd305681944ed87bd5b12d9 (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
/**
 * $Id$
 * ***** BEGIN GPL/BL DUAL 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. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * 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.
 *
 * 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/BL DUAL LICENSE BLOCK *****
 */

#include "BSP_FragTree.h"

#include "BSP_FragNode.h"
#include "BSP_CSGMesh.h"
#include "BSP_MeshFragment.h"
#include "MT_Plane3.h"
#include "BSP_CSGException.h"
#include <vector>
#include "BSP_CSGISplitter.h"

using namespace std;

	MEM_SmartPtr<BSP_FragTree>
BSP_FragTree::
New(
	BSP_CSGMesh *mesh,
	BSP_CSGISplitter & splitter
){
	if (mesh == NULL) return NULL;
	if (mesh->FaceSet().size() == 0) return NULL;

	// This is the external tree construction method
	// (not the internal method!)
	// We need to build a tree root with an initial
	// node based on the mesh rather than a mesh fragment.

	// For now we pick an arbitrary polygon for the initial
	// plane.

	vector<BSP_MVertex> verts = mesh->VertexSet();
	const BSP_MFace & f0 = mesh->FaceSet()[0]; 	

	const MT_Vector3 & p1 = verts[f0.m_verts[0]].m_pos;
	const MT_Vector3 & p2 = verts[f0.m_verts[1]].m_pos;
	const MT_Vector3 & p3 = verts[f0.m_verts[2]].m_pos;
		
	MT_Plane3 plane = f0.m_plane;

	MEM_SmartPtr<BSP_FragTree> output(new BSP_FragTree(mesh));
	MEM_SmartPtr<BSP_FragNode> node(BSP_FragNode::New(plane,mesh));

	if (output == NULL || node == NULL) return NULL;

	// Generate initial mesh fragments for this plane pass into
	// first node.	
	
	BSP_MeshFragment frag_in(mesh,e_classified_in),frag_out(mesh,e_classified_out);
	
	MEM_SmartPtr<BSP_MeshFragment> on_frag = new BSP_MeshFragment(mesh,e_classified_on);

	splitter.Split(*mesh,plane,&frag_in,&frag_out,on_frag,NULL);

	// We are not interested in the on_frag.
	on_frag.Delete();

	// Build the in_tree of the first node(recursive)
	node->InTree().Build(&frag_in,splitter);

	// Build the out tree of the first node(recursive)
	node->OutTree().Build(&frag_out,splitter);

	output->m_node = node;

	return output;
}
				
	void
BSP_FragTree::
Classify(
	BSP_CSGMesh *mesh,
	BSP_MeshFragment *in_frag,
	BSP_MeshFragment *out_frag,
	BSP_MeshFragment *on_frag,
	BSP_CSGISplitter & splitter
){

	if (mesh == NULL) return;
	if (mesh->FaceSet().size() == 0) return;
	if (m_node == NULL) return;

	BSP_MeshFragment frag_in(mesh,e_classified_in);
	BSP_MeshFragment frag_out(mesh,e_classified_out);
	BSP_MeshFragment frag_on(mesh,e_classified_on);
	BSP_MeshFragment frag_spanning(mesh,e_classified_spanning);

	splitter.Split(*mesh,m_node->Plane(),&frag_in,&frag_out,&frag_on,NULL);
	
	if (frag_on.FaceSet().size()) {

		on_frag->FaceSet().insert(
			on_frag->FaceSet().end(),
			frag_on.FaceSet().begin(),
			frag_on.FaceSet().end()
		);

		frag_on.FaceSet().clear();
	}

	// recurse into subtrees.
	m_node->InTree().Classify(&frag_in,in_frag,out_frag,on_frag,e_classified_in,splitter);
	m_node->OutTree().Classify(&frag_out,in_frag,out_frag,on_frag,e_classified_out,splitter);

}	

		



BSP_FragTree::
~BSP_FragTree(
){
	// nothing to do
}

BSP_FragTree::
BSP_FragTree(
	BSP_CSGMesh * mesh
):
	m_mesh(mesh)
{
	//nothing to do
}

BSP_FragTree::
BSP_FragTree(
){
	// nothing to do
}

	void
BSP_FragTree::
Build(
	BSP_MeshFragment * frag,
	BSP_CSGISplitter & splitter
){

	// Node must be NULL because we are building the tree.

	MT_assert(m_node == NULL);

	if (frag->FaceSet().size()) {
		
		// choose a plane for the node. The first index in this
		// mesh fragment will do for now.
		vector<BSP_MVertex> & verts = m_mesh->VertexSet();

		// choose a random splitting plane

		MT_Plane3 plane;
		{
			int rand_index;
#if 1		
			if (frag->FaceSet().size() > 1) {
				rand_index = rand() % frag->FaceSet().size();
			} else {
				rand_index = 0;
			}
#else
			rand_index = 0;
#endif
	
			const BSP_MFace & f0 = m_mesh->FaceSet()[frag->FaceSet()[rand_index]]; 	
			plane = f0.m_plane; 
		}
	
		// build the node.
		m_node = BSP_FragNode::New(plane,frag->Mesh());

		if (m_node == NULL) {
			BSP_CSGException e(e_tree_build_error);
			throw(e);
		}
	
		m_node->Build(frag,splitter);
	}
}


	void
BSP_FragTree::
Push(
	BSP_MeshFragment *in_frag,
	BSP_MeshFragment *output,
	const BSP_Classification keep,
	const BSP_Classification current,
	const bool dominant,
	BSP_CSGISplitter & splitter
){

	if (in_frag->FaceSet().size()) {

		if (m_node == NULL) {

			// we have reached a leaf node.
			// if the current classification matches
			// the classification we want to keep
			// copy the polygons of the current
			// fragment onto the output
			vector<BSP_FaceInd>::const_iterator in_frag_it = in_frag->FaceSet().begin();
			vector<BSP_FaceInd>::const_iterator in_frag_end = in_frag->FaceSet().end();
			vector<BSP_MFace>::iterator faces = in_frag->Mesh()->FaceSet().begin();
		
			if (keep == current || current == e_classified_on) {
				for (;in_frag_it != in_frag_end; ++ in_frag_it) {
					output->FaceSet().push_back(*in_frag_it);
				} 

				in_frag->FaceSet().clear();
			} 
		} else {

			m_node->Push(in_frag,output,keep,dominant,splitter);
		}
	}
}


	void
BSP_FragTree::
Classify(
	BSP_MeshFragment * frag,
	BSP_MeshFragment *in_frag,
	BSP_MeshFragment *out_frag,
	BSP_MeshFragment *on_frag,
	const BSP_Classification current,
	BSP_CSGISplitter & splitter
){

	if (frag->FaceSet().size()) {

		if (m_node == NULL) {

			vector<BSP_FaceInd>::const_iterator frag_it = frag->FaceSet().begin();
			vector<BSP_FaceInd>::const_iterator frag_end = frag->FaceSet().end();
			
			BSP_MeshFragment *output;
			if (current == e_classified_in) {
				output = in_frag;
			} else {
				//must be e_classified_out
				output = out_frag;
			}
			// Copy the selected indices into the correct output fragment.

			for (;frag_it != frag_end; ++ frag_it) {
				output->FaceSet().push_back(*frag_it);
			} 

			frag->FaceSet().clear();
		} else {

			m_node->Classify(frag,in_frag,out_frag,on_frag,splitter);
		}
	}
}