3 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version. The Blender
9 * Foundation also sells licenses for use in proprietary software under
10 * the Blender License. See http://www.blender.org/BL/ for information
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL/BL DUAL LICENSE BLOCK *****
32 #ifndef NAN_INCLUDED_LOD_TaggedSetOps_h
33 #define NAN_INCLUDED_LOD_TaggedSetOps_h
40 #include "MEM_NonCopyable.h"
44 * This class contains some utility functions for finding the intersection,
45 * union, and difference of a collection of stl vector of indices into
46 * a set of primitives.
48 * These are mainly used as helper functions in the decimation and bsp
51 * This template class assumes that each value of type IndexType encountered
52 * in the list is a valid index into an array of primitives. This is not
53 * checked at run-time and is left to the user to insure. Prmitives of
54 * type ObjectType must have the following public methods to be used by
55 * this template class:
58 * OpenTag(void) --- return a persistent tag value for the primitive
61 * SetOpenTag(int bla) --- set the persistent tag value for this primitive to bla.
64 * SelectTag() --- return a persistent boolean tag for this primitive
67 * SetSelectTag(bool bla) --- set the persistent boolean tag for this primitive to bla.
69 * Here persistent means that the tag should be associated with the object for the
70 * entire lifetime of the primitive. Again none of this stuff is enforced you have
71 * to make sure that your primitives do the right thing. Often these tags can be
72 * cunningly stowed away inside some of the spare bits in the primitive. See
73 * CTR_TaggedIndex for such a class.
78 <class IndexType, class ObjectType>
79 class CTR_TaggedSetOps : public MEM_NonCopyable {
86 const std::vector< std::vector<IndexType> > &index_list,
87 std::vector<ObjectType> &primitives,
88 std::vector<IndexType> &output,
93 // iterate through vectors in index_list
94 // iterate through individual members of each vector
95 // mark each obejct that the index points to
97 std::vector< std::vector<IndexType> >::const_iterator last_vector = index_list.end();
98 std::vector< std::vector<IndexType> >::const_iterator start_vector = index_list.begin();
100 // FIXME some temporary space
102 std::vector<IndexType> temp_union;
103 temp_union.reserve(64);
107 for (; start_vector != last_vector; ++start_vector) {
109 std::vector<IndexType>::const_iterator last_index = start_vector->end();
110 std::vector<IndexType>::const_iterator start_index = start_vector->begin();
112 for (; start_index != last_index; ++start_index) {
114 ObjectType & prim = primitives[*start_index];
116 if (!prim.OpenTag()) {
118 temp_union.push_back(*start_index);
120 int tag = prim.OpenTag();
121 tag = (tag & mask) >> shift;
123 prim.SetOpenTag((prim.OpenTag() & ~mask)| ((tag << shift) & mask));
129 // now iterate through the union and pull out all those with the right tag
131 std::vector<IndexType>::const_iterator last_index = temp_union.end();
132 std::vector<IndexType>::const_iterator start_index = temp_union.begin();
134 for (; start_index != last_index; ++start_index) {
136 ObjectType & prim = primitives[*start_index];
138 if (prim.OpenTag() == tag_num) {
139 //it's part of the intersection!
141 output.push_back(*start_index);
142 // because we're iterating through the union
143 // it's safe to remove the tag at this point
145 prim.SetOpenTag(prim.OpenTag() & ~mask);
150 // note not a strict set intersection!
151 // if x appears twice in b and is part of the intersection
152 // it will appear twice in the intersection
157 const std::vector<IndexType> &a,
158 const std::vector<IndexType> &b,
159 std::vector<ObjectType> &primitives,
160 std::vector<IndexType> &output
163 std::vector<IndexType>::const_iterator last_index = a.end();
164 std::vector<IndexType>::const_iterator start_index = a.begin();
166 for (; start_index != last_index; ++start_index) {
167 ObjectType & prim = primitives[*start_index];
168 prim.SetSelectTag(true);
170 last_index = b.end();
171 start_index = b.begin();
173 for (; start_index != last_index; ++start_index) {
174 ObjectType & prim = primitives[*start_index];
175 if (prim.SelectTag()) {
176 output.push_back(*start_index);
180 last_index = a.end();
181 start_index = a.begin();
183 for (; start_index != last_index; ++start_index) {
184 ObjectType & prim = primitives[*start_index];
185 prim.SetSelectTag(false);
193 std::vector< std::vector<IndexType> > &index_list,
194 std::vector<ObjectType> &primitives,
195 std::vector<IndexType> &output
198 // iterate through vectors in index_list
199 // iterate through individual members of each vector
200 // mark each obejct that the index points to
202 std::vector< std::vector<IndexType> >::const_iterator last_vector = index_list.end();
203 std::vector< std::vector<IndexType> >::iterator start_vector = index_list.begin();
205 for (; start_vector != last_vector; ++start_vector) {
207 std::vector<IndexType>::const_iterator last_index = start_vector->end();
208 std::vector<IndexType>::iterator start_index = start_vector->begin();
210 for (; start_index != last_index; ++start_index) {
212 ObjectType & prim = primitives[*start_index];
214 if (!prim.SelectTag()) {
216 output.push_back(*start_index);
217 prim.SetSelectTag(true);
222 // now iterate through the union and reset the tags
224 std::vector<IndexType>::const_iterator last_index = output.end();
225 std::vector<IndexType>::iterator start_index = output.begin();
227 for (; start_index != last_index; ++start_index) {
229 ObjectType & prim = primitives[*start_index];
230 prim.SetSelectTag(false);
238 std::vector< IndexType> &a,
239 std::vector< IndexType> &b,
240 std::vector<ObjectType> &primitives,
241 std::vector< IndexType> &output
244 // iterate through b mark all
245 // iterate through a and add to output all unmarked
247 std::vector<IndexType>::const_iterator last_index = b.end();
248 std::vector<IndexType>::iterator start_index = b.begin();
250 for (; start_index != last_index; ++start_index) {
252 ObjectType & prim = primitives[*start_index];
253 prim.SetSelectTag(true);
256 last_index = a.end();
257 start_index = a.begin();
259 for (; start_index != last_index; ++start_index) {
261 ObjectType & prim = primitives[*start_index];
262 if (!prim.SelectTag()) {
263 output.push_back(*start_index);
269 last_index = b.end();
270 start_index = b.begin();
272 for (; start_index != last_index; ++start_index) {
274 ObjectType & prim = primitives[*start_index];
275 prim.SetSelectTag(false);
281 // private constructor - this class is not meant for