aboutsummaryrefslogtreecommitdiff
path: root/tool_src/BspLib/BspObjectList.cpp
blob: 604682bcb9c5554b7bde94d984f991172e4d8671 (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
//-----------------------------------------------------------------------------
//	BSPLIB MODULE: BspObjectList.cpp
//
//  Copyright (c) 1997-1998 by Markus Hadwiger
//  All Rights Reserved.
//-----------------------------------------------------------------------------

// bsplib header files
#include "BspObjectList.h"


BSPLIB_NAMESPACE_BEGIN


// create new (default constructed) BspObject and prepend it to list ----------
//
BspObject *BspObjectListRep::CreateNewObject()
{
	BspObject *temp	= new BspObject;
	temp->next		= list;
	list			= temp;
	return list;
}


// insert existing BspObject at head of list ----------------------------------
//
BspObject *BspObjectListRep::InsertObject( BspObject *obj )
{
	if ( obj != NULL ) {
		obj->next = list;
		list      = obj;
	}
	return obj;
}


// build a list of bounding boxes containing all objects of list --------------
//
BoundingBox *BspObjectListRep::BuildBoundingBoxList()
{
	return ( list ? list->BuildBoundingBoxList() : NULL );
}


// count number of objects in list --------------------------------------------
//
int BspObjectListRep::CountListObjects()
{
	int count = 0;
	for ( BspObject *scan = list; scan; scan = scan->getNext() )
		count++;
	return count;
}


// prepare object bsp tree as secondary data structure ------------------------
//
int BspObjectListRep::PrepareObjectBSPTree( ObjectBSPTree& objbsptree )
{
	if ( list != NULL ) {
		// create list of bounding boxes with contained objects
		BoundingBox *bboxlist = BuildBoundingBoxList();
		// partition space containing bounding boxes (list becomes invalid!)
		objbsptree.InitTree( bboxlist->PartitionSpace() );
		// create new object list, object bsp tree becomes secondary data structure
		list = objbsptree->CreateObjectList();
	}
	return ( list != NULL );
}


// merge object bsp tree into a single object and attached bsp tree -----------
//
int BspObjectListRep::MergeObjectBSPTree( ObjectBSPTree& objbsptree )
{
	//NOTE:
	// it is imperative that the passed in ObjectBSPTree really
	// contains all BspObjects of this list! if this is not the
	// case the list will simply be overwritten with a single-element
	// list containing the aggregate object of all nodes of the
	// passed in ObjectBSPTree. the original list nodes will be lost!

	// create list consisting only of the merged object
	list = objbsptree->CreateMergedBSPTree();

	// delete all BspObjects contained in tree since they are now
	// unnecessary. (their data is contained in the merged object.)
	objbsptree->DeleteNodeBspObjects();
	objbsptree.KillTree();

	return ( list != NULL );
}


// collapse entire list of BspObjects into head of list -----------------------
//
int BspObjectListRep::CollapseObjectList()
{
	if ( list != NULL )
		list->CollapseObjectList();
	return ( list != NULL );
}


// process all objects contained in list --------------------------------------
//
int BspObjectListRep::ProcessObjects( int flags )
{
	// scan entire list
	for ( BspObject *bspobject = list; bspobject; bspobject = bspobject->getNext() ) {

		// apply transformations directly to vertices
		if ( flags & BspObjectList::APPLY_TRANSFORMATIONS ) {
			bspobject->ApplyTransformation();
		}

		// calculate plane normals for planes of all polygons
		if ( flags & BspObjectList::CALC_PLANE_NORMALS )
			bspobject->CalcPlaneNormals();

		// build linked bsp tree from flat representation
		if ( flags & BspObjectList::BUILD_FROM_FLAT ) {
			if ( bspobject->BuildBSPTreeFromFlat() )
				bspobject->bsptree->NumberBSPNodes( bspobject->numbsppolygons );
			continue;	// no other processing allowed
		}

		//TODO:
		// 1. vertices zusammenlegen: MergeVertices()
		// 2. ueberfluessige edges entfernen: CullNullEdges()
		// 3. t vertices entfernen: EliminateTVertices()
		// 4. faces zusammenlegen: MergeFaces()

		// MergeFaces(): nur fuer 3d-studio tri-mesh!!
		//   fuer jedes triangle werden alle anderen gescannt, ob sie eine
		//   edge teilen. falls ja, wird fuer den dritten punkt geprueft, ob er
		//   in der selben ebene liegt. ja --> triangle to quadrilateral merge.
		//   der umlaufsinn der vertex numerierung wird dahingehend geprueft, ob
		//   er konsistent mit dem ergebnis-quad ist.
		//   nein: warnmeldung und kein mergen!!

		if ( flags & BspObjectList::MERGE_VERTICES ) {
		}

		if ( flags & BspObjectList::CULL_NULL_EDGES ) {
		}

		if ( flags & BspObjectList::ELIMINATE_T_VERTICES ) {
		}

		if ( flags & BspObjectList::MERGE_FACES ) {
		}

		// split (possibly hand edited) faces with vertices not in same plane
		if ( flags & BspObjectList::CHECK_PLANES )
			bspobject->CheckPolygonPlanes();

		// compile bsp tree and number nodes
		if ( flags & BspObjectList::BUILD_BSP )
			if ( bspobject->BuildBSPTree() )
				bspobject->bsptree->NumberBSPNodes( bspobject->numbsppolygons );

		// check for vertices with exact same coordinates
		if ( flags & BspObjectList::CHECK_VERTICES )
			bspobject->CheckVertices( TRUE );

		// calc bounding boxes for all nodes
		// operates on bsp tree only, so one has to be present!
		if ( flags & BspObjectList::CALC_BOUNDING_BOXES )
			bspobject->CalcBoundingBoxes();

		// calc explicit separator planes for all nodes
		// operates on bsp tree only, so one has to be present!
		if ( flags & BspObjectList::CALC_SEPARATOR_PLANES )
			bspobject->CalcSeparatorPlanes();

		// check edges for t-vertices and eliminate them (insert trace vertices)
		// operates on bsp tree only, so one has to be present!
		if ( flags & BspObjectList::CHECK_EDGES )
			bspobject->CheckEdges();

		// display object and compilation statistics
		if ( flags & BspObjectList::DISPLAY_STATS )
			bspobject->DisplayStatistics();

		// update attribute numbers, even if no statistics desired
		bspobject->UpdateAttributeNumbers();
	}

	return TRUE;
}


BSPLIB_NAMESPACE_END

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