File: frustum_cull.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,060 kB
  • ctags: 33,549
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (277 lines) | stat: -rw-r--r-- 9,526 bytes parent folder | download | duplicates (5)
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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* 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 (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

/****************************************************************************
History

$Log: not supported by cvs2svn $
Revision 1.8  2005/11/30 09:57:13  m_di_benedetto
Added methods to flag visibility.

Revision 1.7  2005/10/26 11:42:03  m_di_benedetto
Added PASS_THROUGH flags.

Revision 1.6  2005/10/15 19:14:35  m_di_benedetto
Modified objapplyfunctor to nodeapplyfunctor.

Revision 1.5  2005/10/05 01:59:56  m_di_benedetto
First Commit, new version.

Revision 1.3  2005/09/29 22:20:49  m_di_benedetto
Removed '&' in FrustumCull() method.

Revision 1.2  2005/09/28 19:57:18  m_di_benedetto
#included aabb tree base.

Revision 1.1  2005/09/26 18:33:16  m_di_benedetto
First Commit.


****************************************************************************/

#ifndef __VCGLIB_AABBBINARYTREE_FRUSTUMCULL_H
#define __VCGLIB_AABBBINARYTREE_FRUSTUMCULL_H

// std headers
/* EMPTY */

// vcg headers
#include <vcg/space/point3.h>
#include <vcg/space/plane3.h>
#include <vcg/space/index/aabb_binary_tree/base.h>

/***************************************************************************/

namespace vcg {

template <class TREETYPE>
class AABBBinaryTreeFrustumCull {
public:
	typedef AABBBinaryTreeFrustumCull<TREETYPE> ClassType;
	typedef TREETYPE TreeType;
	typedef typename TreeType::ScalarType ScalarType;
	typedef typename TreeType::CoordType CoordType;
	typedef typename TreeType::NodeType NodeType;
	typedef typename TreeType::ObjPtr ObjPtr;

protected:
	class VFrustumPlane {
	public:
		CoordType normal;
		ScalarType offset;
		unsigned int pVertexIndex[3];
	};

	class VFrustum {
	public:
		VFrustumPlane planes[6];
	};

public:
	enum {
		FC_FIRST_PLANE_BIT					= 0,
		FC_PARTIALLY_VISIBLE_BIT		= (1 << (FC_FIRST_PLANE_BIT + 3)),
		FC_FULLY_VISIBLE_BIT				= (1 << (FC_FIRST_PLANE_BIT + 4)),
		FC_PASS_THROUGH_FIRST_BIT		= (FC_FIRST_PLANE_BIT + 5)
	};

	static inline bool IsPartiallyVisible(const NodeType * node) {
		return ((node->Flags() & FC_PARTIALLY_VISIBLE_BIT) != 0);
	}

	static inline bool IsFullyVisible(const NodeType * node) {
		return ((node->Flags() & FC_FULLY_VISIBLE_BIT) != 0);
	}

	static inline void SetFullyVisible(NodeType * node) {
		node->Flags() |= FC_FULLY_VISIBLE_BIT;
	}

	static inline void SetInvisible(NodeType * node) {
		node->Flags() &= ~(FC_FULLY_VISIBLE_BIT | FC_PARTIALLY_VISIBLE_BIT);
	}

	static inline bool IsVisible(const NodeType * node) {
		return ((node->Flags() & (FC_PARTIALLY_VISIBLE_BIT | FC_FULLY_VISIBLE_BIT)) != 0);
	}

	static inline unsigned int PassThrough(const NodeType * node) {
		return ((node->Flags() >> FC_PASS_THROUGH_FIRST_BIT) & 0x03);
	}

	static inline void Initialize(TreeType & tree) {
		NodeType * pRoot = tree.pRoot;
		if (pRoot == 0) {
			return;
		}
		ClassType::InitializeNodeFlagsRec(pRoot);
	}

	template <class NODEAPPLYFUNCTOR>
	static inline void FrustumCull(TreeType & tree, const Point3<ScalarType> & viewerPosition, const Plane3<ScalarType> frustumPlanes[6], const unsigned int minNodeObjectsCount, NODEAPPLYFUNCTOR & nodeApply) {
		NodeType * pRoot = tree.pRoot;
		if (pRoot == 0) {
			return;
		}

		VFrustum frustum;
		for (int i=0; i<6; ++i) {
			frustum.planes[i].normal = frustumPlanes[i].Direction();
			frustum.planes[i].offset = frustumPlanes[i].Offset();
			frustum.planes[i].pVertexIndex[0] = (frustum.planes[i].normal[0] >= ((ScalarType)0)) ? (1) : (0);
			frustum.planes[i].pVertexIndex[1] = (frustum.planes[i].normal[1] >= ((ScalarType)0)) ? (1) : (0);
			frustum.planes[i].pVertexIndex[2] = (frustum.planes[i].normal[2] >= ((ScalarType)0)) ? (1) : (0);
		}

		const unsigned char inMask = 0x3F;
		ClassType::NodeVsFrustum(tree.pRoot, viewerPosition, frustum, inMask, minNodeObjectsCount, nodeApply);
	}

protected:

	static inline void InitializeNodeFlagsRec(NodeType * node) {
		//node->Flags() &= ~(0x7F);
		node->Flags() = 0;
		if (node->children[0] != 0) {
			ClassType::InitializeNodeFlagsRec(node->children[0]);
		}
		if (node->children[1] != 0) {
			ClassType::InitializeNodeFlagsRec(node->children[1]);
		}
	}

	template <class NODEAPPLYFUNCTOR>
	static inline void NodeVsFrustum(NodeType * node, const Point3<ScalarType> & viewerPosition, const VFrustum & f, unsigned char inMask, unsigned int minNodeObjectsCount, NODEAPPLYFUNCTOR & nodeApply) {
		if (node == 0) {
			return;
		}

		const CoordType bminmax[2] = {
			node->boxCenter - node->boxHalfDims,
			node->boxCenter + node->boxHalfDims,
		};
		const unsigned int firstFail = (unsigned int)((node->Flags() >> FC_FIRST_PLANE_BIT) & 0x7);
		const VFrustumPlane * fp = f.planes + firstFail;
		unsigned char k = 1 << firstFail;
		unsigned char newMask = 0x0;
		bool fullInside = true;

		node->Flags() &= ~(1 << 25);
		node->Flags() &= ~(FC_PARTIALLY_VISIBLE_BIT | FC_FULLY_VISIBLE_BIT | (0x03 << FC_PASS_THROUGH_FIRST_BIT));

		if ((k & inMask) != 0) {
			if (
				((fp->normal[0] * bminmax[fp->pVertexIndex[0]][0]) +
				(fp->normal[1] * bminmax[fp->pVertexIndex[1]][1]) +
				(fp->normal[2] * bminmax[fp->pVertexIndex[2]][2]) +
				(fp->offset)) < ((ScalarType)0)
			) {
				return;
			}

			if (
				((fp->normal[0] * bminmax[1 - fp->pVertexIndex[0]][0]) +
				(fp->normal[1] * bminmax[1 - fp->pVertexIndex[1]][1]) +
				(fp->normal[2] * bminmax[1 - fp->pVertexIndex[2]][2]) +
				(fp->offset)) < ((ScalarType)0)
			) {
				newMask |=  k;
				fullInside = false;
			}
		}

		k = 1;
		for (unsigned int i=0; k<=inMask; ++i, k<<=1) {
			if ((i != firstFail) && ((k & inMask) != 0)) {
				fp = f.planes + i;
				if (
					((fp->normal[0] * bminmax[fp->pVertexIndex[0]][0]) +
					(fp->normal[1] * bminmax[fp->pVertexIndex[1]][1]) +
					(fp->normal[2] * bminmax[fp->pVertexIndex[2]][2]) +
					(fp->offset)) < ((ScalarType)0)
				) {
					node->Flags() = (node->Flags() & ((~0x0) & (0x7 << ClassType::FC_FIRST_PLANE_BIT))) | (i << ClassType::FC_FIRST_PLANE_BIT);
					return;
				}

				if (
					((fp->normal[0] * bminmax[1 - fp->pVertexIndex[0]][0]) +
					(fp->normal[1] * bminmax[1 - fp->pVertexIndex[1]][1]) +
					(fp->normal[2] * bminmax[1 - fp->pVertexIndex[2]][2]) +
					(fp->offset)) < ((ScalarType)0)
				) {
					newMask |=  k;
					fullInside = false;
				}
			}
		}

		if (fullInside || (node->IsLeaf()) || (node->ObjectsCount() <= minNodeObjectsCount)) {
			node->Flags() |= FC_FULLY_VISIBLE_BIT;
			nodeApply(*node);
			return;
		}

		node->Flags() |= FC_PARTIALLY_VISIBLE_BIT;

		//ClassType::NodeVsFrustum(node->children[0], viewerPosition, f, newMask, minNodeObjectsCount, nodeApply);
		//ClassType::NodeVsFrustum(node->children[1], viewerPosition, f, newMask, minNodeObjectsCount, nodeApply);
		ScalarType dt;
		if (node->splitAxis == 0) {
			dt = viewerPosition[0] - node->boxCenter[0];
		}
		else if (node->splitAxis == 1) {
			dt = viewerPosition[1] - node->boxCenter[1];
		}
		else {
			dt = viewerPosition[2] - node->boxCenter[2];
		}

		if (dt <= (ScalarType)0) {
			ClassType::NodeVsFrustum(node->children[0], viewerPosition, f, newMask, minNodeObjectsCount, nodeApply);
			ClassType::NodeVsFrustum(node->children[1], viewerPosition, f, newMask, minNodeObjectsCount, nodeApply);
		}
		else {
			ClassType::NodeVsFrustum(node->children[1], viewerPosition, f, newMask, minNodeObjectsCount, nodeApply);
			ClassType::NodeVsFrustum(node->children[0], viewerPosition, f, newMask, minNodeObjectsCount, nodeApply);
		}

		const bool c0 = (node->children[0] != 0) && ClassType::IsVisible(node->children[0]);
		const bool c1 = (node->children[1] != 0) && ClassType::IsVisible(node->children[1]);

		if (c0 != c1) {
				node->Flags() &= ~(0x03 << FC_PASS_THROUGH_FIRST_BIT);
			if (c0) {
				node->Flags() |= (0x01 << FC_PASS_THROUGH_FIRST_BIT);
			}
			else {
				node->Flags() |= (0x02 << FC_PASS_THROUGH_FIRST_BIT);
			}
		}
	}

};

}	// end namespace vcg

#endif // #ifndef __VCGLIB_AABBBINARYTREE_FRUSTUMCULL_H