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

BSP_MeshFragment.cpp « intern « bsp « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 408a13d6cd96fc6f38f60b2958757dbc7d1e5f42 (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
/**
 * $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_MeshFragment.h"

#include "BSP_CSGMesh.h"
#include "MT_Plane3.h"
#include <math.h>

using namespace std;


BSP_MeshFragment::
BSP_MeshFragment(
	BSP_CSGMesh *mesh,
	BSP_Classification classification
):
	m_mesh(mesh),
	m_classification(classification)
{
	MT_assert(m_mesh != NULL);
	//nothing to do
}

const 
	vector<BSP_FaceInd> &
BSP_MeshFragment::
FaceSet(
) const {
	return m_faces;
}

	vector<BSP_FaceInd> &
BSP_MeshFragment::
FaceSet(
) {
	return m_faces;
}

	BSP_CSGMesh *
BSP_MeshFragment::
Mesh(
){
	return m_mesh;
}

	BSP_CSGMesh *
BSP_MeshFragment::
Mesh(
) const {
	return m_mesh;
}

	BSP_Classification 
BSP_MeshFragment::
ClassifyPolygon(
	const MT_Plane3 &plane,
	const BSP_MFace & face,
	std::vector<BSP_MVertex>::const_iterator verts,
	vector<BSP_VertexInd> & visited_verts
){

	vector<BSP_VertexInd>::const_iterator f_vi_end = face.m_verts.end();
	vector<BSP_VertexInd>::const_iterator f_vi_it = face.m_verts.begin();

	BSP_Classification p_class = e_unclassified;

	int on_count = 0;

	for (;f_vi_it != f_vi_end; ++f_vi_it) {

		BSP_MVertex & vert = const_cast<BSP_MVertex &>(verts[int(*f_vi_it)]);	

		if (BSP_Classification(vert.OpenTag()) == e_unclassified)
		{
			MT_Scalar sdistance = plane.signedDistance(vert.m_pos);
			MT_Scalar fsdistance = fabs(sdistance);

			if (fabs(sdistance) <= BSP_SPLIT_EPSILON) {
				// this vertex is on
				vert.SetOpenTag(e_classified_on);	
			} else 
			if (sdistance > MT_Scalar(0)) {
				vert.SetOpenTag(e_classified_out);
			} else {
				vert.SetOpenTag(e_classified_in);
			}
			visited_verts.push_back(*f_vi_it);
		}
		BSP_Classification v_class = BSP_Classification(vert.OpenTag());

		if (v_class == e_classified_on) on_count++;


		if (p_class == e_unclassified || p_class == e_classified_on) {
			p_class = v_class;
		} else 
		if (p_class == e_classified_spanning) {
		} else 
		if (p_class == e_classified_in) {
			if (v_class == e_classified_out) {	
				p_class = e_classified_spanning;
			}
		} else {
			if (v_class == e_classified_in) {
				p_class = e_classified_spanning;
			}
		}
	}

	if (on_count > 2) p_class = e_classified_on;

	return p_class;
}


// Classify this mesh fragment with respect
// to plane. The index sets of this fragment
// are consumed by this action. Vertices
// are tagged with a classification enum.

	void
BSP_MeshFragment::
Classify(
	const MT_Plane3 & plane,
	BSP_MeshFragment * in_frag,
 	BSP_MeshFragment * out_frag,
	BSP_MeshFragment * on_frag,
	vector<BSP_FaceInd> & spanning_faces,
	vector<BSP_VertexInd> & visited_verts
){

	vector<BSP_MVertex> & vertex_set = m_mesh->VertexSet();
	vector<BSP_MFace> & face_set = m_mesh->FaceSet();
	
	// Now iterate through the polygons and classify.

	vector<BSP_FaceInd>::const_iterator fi_end = m_faces.end();
	vector<BSP_FaceInd>::iterator fi_it = m_faces.begin();
			
	vector<BSP_FaceInd> & face_in_set = in_frag->FaceSet();
	vector<BSP_FaceInd> & face_out_set = out_frag->FaceSet();
	vector<BSP_FaceInd> & face_on_set = on_frag->FaceSet();
	
	for (;fi_it != fi_end; ++fi_it) {
			
		BSP_Classification p_class = ClassifyPolygon(
			plane,
			face_set[*fi_it],
			vertex_set.begin(),
			visited_verts
		);
		// p_class now holds the classification for this polygon.
		// assign to the appropriate bucket.

		if (p_class == e_classified_in) {
			face_in_set.push_back(*fi_it);
		} else 
		if (p_class == e_classified_out) {
			face_out_set.push_back(*fi_it);
		} else
		if (p_class == e_classified_on) {
			face_on_set.push_back(*fi_it);
		} else {
			spanning_faces.push_back(*fi_it);
			// This is assigned later when we split the polygons in two.
		}
	}

	m_faces.clear();

}		

	void
BSP_MeshFragment::
Classify(
	BSP_CSGMesh & mesh,
	const MT_Plane3 & plane,
	BSP_MeshFragment * in_frag,
 	BSP_MeshFragment * out_frag,
	BSP_MeshFragment * on_frag,
	vector<BSP_FaceInd> & spanning_faces,
	vector<BSP_VertexInd> & visited_verts
){

	vector<BSP_MVertex> & vertex_set = mesh.VertexSet();
	vector<BSP_MFace> & face_set = mesh.FaceSet();
	
	// Now iterate through the polygons and classify.

	int fi_end = mesh.FaceSet().size();
	int fi_it = 0;
			
	vector<BSP_FaceInd> & face_in_set = in_frag->FaceSet();
	vector<BSP_FaceInd> & face_out_set = out_frag->FaceSet();
	vector<BSP_FaceInd> & face_on_set = on_frag->FaceSet();
	
	for (;fi_it != fi_end; ++fi_it) {
			
		BSP_Classification p_class = ClassifyPolygon(
			plane,
			face_set[fi_it],
			vertex_set.begin(),
			visited_verts
		);
		// p_class now holds the classification for this polygon.
		// assign to the appropriate bucket.

		if (p_class == e_classified_in) {
			face_in_set.push_back(fi_it);
		} else 
		if (p_class == e_classified_out) {
			face_out_set.push_back(fi_it);
		} else
		if (p_class == e_classified_on) {
			face_on_set.push_back(fi_it);
		} else {
			spanning_faces.push_back(fi_it);
		}
	}

}
	void
BSP_MeshFragment::
ClassifyOnFragments(
	const MT_Plane3 &plane,
	BSP_MeshFragment *pos_frag,
	BSP_MeshFragment *neg_frag
){

	vector<BSP_MFace> & face_set = m_mesh->FaceSet();
	vector<BSP_FaceInd>::const_iterator fi_end = m_faces.end();
	vector<BSP_FaceInd>::iterator fi_it = m_faces.begin();

	MT_Scalar d = plane.Scalar();

	for (;fi_it != fi_end; ++fi_it) {
		if (fabs(d + face_set[*fi_it].m_plane.Scalar()) > BSP_SPLIT_EPSILON) {
			pos_frag->FaceSet().push_back(*fi_it);
		} else {
			neg_frag->FaceSet().push_back(*fi_it);
		}
	}	
}

BSP_MeshFragment::
~BSP_MeshFragment(
){
}