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
35 #include "MEM_NonCopyable.h"
39 * This class contains some utility functions for finding the intersection,
40 * union, and difference of a collection of stl vector of indices into
41 * a set of primitives.
43 * These are mainly used as helper functions in the decimation and bsp
46 * This template class assumes that each value of type IndexType encountered
47 * in the list is a valid index into an array of primitives. This is not
48 * checked at run-time and is left to the user to insure. Prmitives of
49 * type ObjectType must have the following public methods to be used by
50 * this template class:
53 * OpenTag(void) --- return a persistent tag value for the primitive
56 * SetOpenTag(int bla) --- set the persistent tag value for this primitive to bla.
59 * SelectTag() --- return a persistent boolean tag for this primitive
62 * SetSelectTag(bool bla) --- set the persistent boolean tag for this primitive to bla.
64 * Here persistent means that the tag should be associated with the object for the
65 * entire lifetime of the primitive. Again none of this stuff is enforced you have
66 * to make sure that your primitives do the right thing. Often these tags can be
67 * cunningly stowed away inside some of the spare bits in the primitive. See
68 * CTR_TaggedIndex for such a class.
73 <class IndexType, class ObjectType>
74 class CTR_TaggedSetOps : public MEM_NonCopyable {
81 const std::vector< std::vector<IndexType> > &index_list,
82 std::vector<ObjectType> &primitives,
83 std::vector<IndexType> &output,
88 // iterate through vectors in index_list
89 // iterate through individual members of each vector
90 // mark each obejct that the index points to
92 std::vector< std::vector<IndexType> >::const_iterator last_vector = index_list.end();
93 std::vector< std::vector<IndexType> >::const_iterator start_vector = index_list.begin();
95 // FIXME some temporary space
97 std::vector<IndexType> temp_union;
98 temp_union.reserve(64);
102 for (; start_vector != last_vector; ++start_vector) {
104 std::vector<IndexType>::const_iterator last_index = start_vector->end();
105 std::vector<IndexType>::const_iterator start_index = start_vector->begin();
107 for (; start_index != last_index; ++start_index) {
109 ObjectType & prim = primitives[*start_index];
111 if (!prim.OpenTag()) {
113 temp_union.push_back(*start_index);
115 int tag = prim.OpenTag();
116 tag = (tag & mask) >> shift;
118 prim.SetOpenTag((prim.OpenTag() & ~mask)| ((tag << shift) & mask));
124 // now iterate through the union and pull out all those with the right tag
126 std::vector<IndexType>::const_iterator last_index = temp_union.end();
127 std::vector<IndexType>::const_iterator start_index = temp_union.begin();
129 for (; start_index != last_index; ++start_index) {
131 ObjectType & prim = primitives[*start_index];
133 if (prim.OpenTag() == tag_num) {
134 //it's part of the intersection!
136 output.push_back(*start_index);
137 // because we're iterating through the union
138 // it's safe to remove the tag at this point
140 prim.SetOpenTag(prim.OpenTag() & ~mask);
145 // note not a strict set intersection!
146 // if x appears twice in b and is part of the intersection
147 // it will appear twice in the intersection
152 const std::vector<IndexType> &a,
153 const std::vector<IndexType> &b,
154 std::vector<ObjectType> &primitives,
155 std::vector<IndexType> &output
158 std::vector<IndexType>::const_iterator last_index = a.end();
159 std::vector<IndexType>::const_iterator start_index = a.begin();
161 for (; start_index != last_index; ++start_index) {
162 ObjectType & prim = primitives[*start_index];
163 prim.SetSelectTag(true);
165 last_index = b.end();
166 start_index = b.begin();
168 for (; start_index != last_index; ++start_index) {
169 ObjectType & prim = primitives[*start_index];
170 if (prim.SelectTag()) {
171 output.push_back(*start_index);
175 last_index = a.end();
176 start_index = a.begin();
178 for (; start_index != last_index; ++start_index) {
179 ObjectType & prim = primitives[*start_index];
180 prim.SetSelectTag(false);
188 std::vector< std::vector<IndexType> > &index_list,
189 std::vector<ObjectType> &primitives,
190 std::vector<IndexType> &output
193 // iterate through vectors in index_list
194 // iterate through individual members of each vector
195 // mark each obejct that the index points to
197 std::vector< std::vector<IndexType> >::const_iterator last_vector = index_list.end();
198 std::vector< std::vector<IndexType> >::iterator start_vector = index_list.begin();
200 for (; start_vector != last_vector; ++start_vector) {
202 std::vector<IndexType>::const_iterator last_index = start_vector->end();
203 std::vector<IndexType>::iterator start_index = start_vector->begin();
205 for (; start_index != last_index; ++start_index) {
207 ObjectType & prim = primitives[*start_index];
209 if (!prim.SelectTag()) {
211 output.push_back(*start_index);
212 prim.SetSelectTag(true);
217 // now iterate through the union and reset the tags
219 std::vector<IndexType>::const_iterator last_index = output.end();
220 std::vector<IndexType>::iterator start_index = output.begin();
222 for (; start_index != last_index; ++start_index) {
224 ObjectType & prim = primitives[*start_index];
225 prim.SetSelectTag(false);
233 std::vector< IndexType> &a,
234 std::vector< IndexType> &b,
235 std::vector<ObjectType> &primitives,
236 std::vector< IndexType> &output
239 // iterate through b mark all
240 // iterate through a and add to output all unmarked
242 std::vector<IndexType>::const_iterator last_index = b.end();
243 std::vector<IndexType>::iterator start_index = b.begin();
245 for (; start_index != last_index; ++start_index) {
247 ObjectType & prim = primitives[*start_index];
248 prim.SetSelectTag(true);
251 last_index = a.end();
252 start_index = a.begin();
254 for (; start_index != last_index; ++start_index) {
256 ObjectType & prim = primitives[*start_index];
257 if (!prim.SelectTag()) {
258 output.push_back(*start_index);
264 last_index = b.end();
265 start_index = b.begin();
267 for (; start_index != last_index; ++start_index) {
269 ObjectType & prim = primitives[*start_index];
270 prim.SetSelectTag(false);
276 // private constructor - this class is not meant for