WooHoo me again ;)
[blender-staging.git] / intern / container / CTR_TaggedSetOps.h
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
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
11  * about this.
12  *
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.
17  *
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.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #ifndef NAN_INCLUDED_LOD_TaggedSetOps_h
33 #define NAN_INCLUDED_LOD_TaggedSetOps_h
34
35 #include "MEM_NonCopyable.h"
36 #include <vector>
37
38 /**
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.
42  *
43  * These are mainly used as helper functions in the decimation and bsp
44  * libraries.
45  *
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:
51  *
52  *      int
53  * OpenTag(void) --- return a persistent tag value for the primitive
54  *
55  *      void
56  * SetOpenTag(int bla) --- set the persistent tag value for this primitive to bla.
57  *
58  *      bool
59  * SelectTag() --- return a persistent boolean tag for this primitive
60  *
61  *      void
62  * SetSelectTag(bool bla) --- set the persistent boolean tag for this primitive to bla.
63  *
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.
69  *
70  */
71
72 template 
73 <class IndexType, class ObjectType>
74 class CTR_TaggedSetOps : public MEM_NonCopyable {
75
76 public :
77
78         static
79                 void
80         Intersect(
81                 const std::vector< std::vector<IndexType> > &index_list,
82                 std::vector<ObjectType> &primitives,
83                 std::vector<IndexType> &output,
84                 unsigned int mask,
85                 unsigned int shift
86         ) {
87
88                 // iterate through vectors in index_list
89                 // iterate through individual members of each vector
90                 // mark each obejct that the index points to 
91
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();
94
95                 // FIXME some temporary space
96
97                 std::vector<IndexType> temp_union;
98                 temp_union.reserve(64);
99
100                 int tag_num = 0;
101
102                 for (; start_vector != last_vector; ++start_vector) {
103
104                         std::vector<IndexType>::const_iterator last_index = start_vector->end();
105                         std::vector<IndexType>::const_iterator start_index = start_vector->begin();
106
107                         for (; start_index != last_index; ++start_index) {
108
109                                 ObjectType & prim = primitives[*start_index];
110
111                                 if (!prim.OpenTag()) {
112                                         // compute the union
113                                         temp_union.push_back(*start_index);
114                                 }
115                                 int tag = prim.OpenTag();
116                                 tag = (tag & mask) >> shift;
117                                 tag += 1;
118                                 prim.SetOpenTag((prim.OpenTag() & ~mask)| ((tag << shift) & mask));
119                         }
120
121                         ++tag_num;
122                 }
123                                 
124                 // now iterate through the union and pull out all those with the right tag
125                 
126                 std::vector<IndexType>::const_iterator last_index = temp_union.end();
127                 std::vector<IndexType>::const_iterator start_index = temp_union.begin();
128
129                 for (; start_index != last_index; ++start_index) {
130
131                         ObjectType & prim = primitives[*start_index];
132
133                         if (prim.OpenTag() == tag_num) {
134                                 //it's part of the intersection!
135
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
139
140                                 prim.SetOpenTag(prim.OpenTag() & ~mask);
141                         }
142                 }
143         };
144                 
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
148
149         static
150                 void
151         IntersectPair(
152                 const std::vector<IndexType> &a,
153                 const std::vector<IndexType> &b,
154                 std::vector<ObjectType> &primitives,
155                 std::vector<IndexType> &output
156         ) {
157                 
158                 std::vector<IndexType>::const_iterator last_index = a.end();
159                 std::vector<IndexType>::const_iterator start_index = a.begin();
160
161                 for (; start_index != last_index; ++start_index) {
162                         ObjectType & prim = primitives[*start_index];
163                         prim.SetSelectTag(true);
164                 }
165                 last_index = b.end();
166                 start_index = b.begin();
167
168                 for (; start_index != last_index; ++start_index) {
169                         ObjectType & prim = primitives[*start_index];
170                         if (prim.SelectTag()) {
171                                 output.push_back(*start_index);
172                         }
173                 }
174                 // deselect
175                 last_index = a.end();
176                 start_index = a.begin();
177
178                 for (; start_index != last_index; ++start_index) {
179                         ObjectType & prim = primitives[*start_index];
180                         prim.SetSelectTag(false);
181                 }
182         };
183
184
185         static  
186                 void
187         Union(
188                 std::vector< std::vector<IndexType> > &index_list,
189                 std::vector<ObjectType> &primitives,
190                 std::vector<IndexType> &output
191         ) {
192         
193                 // iterate through vectors in index_list
194                 // iterate through individual members of each vector
195                 // mark each obejct that the index points to 
196
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();
199
200                 for (; start_vector != last_vector; ++start_vector) {
201
202                         std::vector<IndexType>::const_iterator last_index = start_vector->end();
203                         std::vector<IndexType>::iterator start_index = start_vector->begin();
204
205                         for (; start_index != last_index; ++start_index) {
206
207                                 ObjectType & prim = primitives[*start_index];
208
209                                 if (!prim.SelectTag()) {
210                                         // compute the union
211                                         output.push_back(*start_index);
212                                         prim.SetSelectTag(true);
213                                 }
214                         }
215                 }
216                                 
217                 // now iterate through the union and reset the tags
218                 
219                 std::vector<IndexType>::const_iterator last_index = output.end();
220                 std::vector<IndexType>::iterator start_index = output.begin();
221
222                 for (; start_index != last_index; ++start_index) {
223
224                         ObjectType & prim = primitives[*start_index];
225                         prim.SetSelectTag(false);
226                 }                       
227         }
228
229
230         static
231                 void
232         Difference(
233                 std::vector< IndexType> &a,
234                 std::vector< IndexType> &b,
235                 std::vector<ObjectType> &primitives,
236                 std::vector< IndexType> &output
237         ) {
238
239                 // iterate through b mark all
240                 // iterate through a and add to output all unmarked 
241
242                 std::vector<IndexType>::const_iterator last_index = b.end();
243                 std::vector<IndexType>::iterator start_index = b.begin();
244
245                 for (; start_index != last_index; ++start_index) {
246
247                         ObjectType & prim = primitives[*start_index];
248                         prim.SetSelectTag(true);
249                 }
250                         
251                 last_index = a.end();
252                 start_index = a.begin();
253
254                 for (; start_index != last_index; ++start_index) {
255
256                         ObjectType & prim = primitives[*start_index];
257                         if (!prim.SelectTag()) {
258                                 output.push_back(*start_index);
259                         }
260                 }
261
262                 // clean up the tags
263         
264                 last_index = b.end();
265                 start_index = b.begin();
266
267                 for (; start_index != last_index; ++start_index) {
268
269                         ObjectType & prim = primitives[*start_index];
270                         prim.SetSelectTag(false);
271                 }
272         };
273
274 private :
275
276         // private constructor - this class is not meant for
277         // instantiation
278
279         CTR_TaggedSetOps();
280
281 };
282
283 #endif
284