Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00019 * All rights reserved. 00020 * 00021 * The Original Code is: all of this file. 00022 * 00023 * Contributor(s): none yet. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 */ 00027 00033 #ifndef NAN_INCLUDED_LOD_TaggedSetOps_h 00034 #define NAN_INCLUDED_LOD_TaggedSetOps_h 00035 00036 #include "MEM_NonCopyable.h" 00037 #include <vector> 00038 00073 template 00074 <class IndexType, class ObjectType> 00075 class CTR_TaggedSetOps : public MEM_NonCopyable { 00076 00077 public : 00078 00079 static 00080 void 00081 Intersect( 00082 const std::vector< std::vector<IndexType> > &index_list, 00083 std::vector<ObjectType> &primitives, 00084 std::vector<IndexType> &output, 00085 unsigned int mask, 00086 unsigned int shift 00087 ) { 00088 00089 // iterate through vectors in index_list 00090 // iterate through individual members of each vector 00091 // mark each obejct that the index points to 00092 00093 typename std::vector< std::vector<IndexType> >::const_iterator 00094 last_vector = index_list.end(); 00095 typename std::vector< std::vector<IndexType> >::const_iterator 00096 start_vector = index_list.begin(); 00097 00098 // FIXME some temporary space 00099 00100 std::vector<IndexType> temp_union; 00101 temp_union.reserve(64); 00102 00103 int tag_num = 0; 00104 00105 for (; start_vector != last_vector; ++start_vector) { 00106 00107 typename std::vector<IndexType>::const_iterator 00108 last_index = start_vector->end(); 00109 typename std::vector<IndexType>::const_iterator 00110 start_index = start_vector->begin(); 00111 00112 for (; start_index != last_index; ++start_index) { 00113 00114 ObjectType & prim = primitives[*start_index]; 00115 00116 if (!prim.OpenTag()) { 00117 // compute the union 00118 temp_union.push_back(*start_index); 00119 } 00120 int tag = prim.OpenTag(); 00121 tag = (tag & mask) >> shift; 00122 tag += 1; 00123 prim.SetOpenTag((prim.OpenTag() & ~mask)| ((tag << shift) & mask)); 00124 } 00125 00126 ++tag_num; 00127 } 00128 00129 // now iterate through the union and pull out all those with the right tag 00130 00131 typename std::vector<IndexType>::const_iterator last_index = 00132 temp_union.end(); 00133 typename std::vector<IndexType>::const_iterator start_index = 00134 temp_union.begin(); 00135 00136 for (; start_index != last_index; ++start_index) { 00137 00138 ObjectType & prim = primitives[*start_index]; 00139 00140 if (prim.OpenTag() == tag_num) { 00141 //it's part of the intersection! 00142 00143 output.push_back(*start_index); 00144 // because we're iterating through the union 00145 // it's safe to remove the tag at this point 00146 00147 prim.SetOpenTag(prim.OpenTag() & ~mask); 00148 } 00149 } 00150 }; 00151 00152 // note not a strict set intersection! 00153 // if x appears twice in b and is part of the intersection 00154 // it will appear twice in the intersection 00155 00156 static 00157 void 00158 IntersectPair( 00159 const std::vector<IndexType> &a, 00160 const std::vector<IndexType> &b, 00161 std::vector<ObjectType> &primitives, 00162 std::vector<IndexType> &output 00163 ) { 00164 00165 typename std::vector<IndexType>::const_iterator last_index = 00166 a.end(); 00167 typename std::vector<IndexType>::const_iterator start_index = 00168 a.begin(); 00169 00170 for (; start_index != last_index; ++start_index) { 00171 ObjectType & prim = primitives[*start_index]; 00172 prim.SetSelectTag(true); 00173 } 00174 last_index = b.end(); 00175 start_index = b.begin(); 00176 00177 for (; start_index != last_index; ++start_index) { 00178 ObjectType & prim = primitives[*start_index]; 00179 if (prim.SelectTag()) { 00180 output.push_back(*start_index); 00181 } 00182 } 00183 // deselect 00184 last_index = a.end(); 00185 start_index = a.begin(); 00186 00187 for (; start_index != last_index; ++start_index) { 00188 ObjectType & prim = primitives[*start_index]; 00189 prim.SetSelectTag(false); 00190 } 00191 }; 00192 00193 00194 static 00195 void 00196 Union( 00197 std::vector< std::vector<IndexType> > &index_list, 00198 std::vector<ObjectType> &primitives, 00199 std::vector<IndexType> &output 00200 ) { 00201 00202 // iterate through vectors in index_list 00203 // iterate through individual members of each vector 00204 // mark each obejct that the index points to 00205 00206 typename std::vector< std::vector<IndexType> >::const_iterator 00207 last_vector = index_list.end(); 00208 typename std::vector< std::vector<IndexType> >::iterator 00209 start_vector = index_list.begin(); 00210 00211 for (; start_vector != last_vector; ++start_vector) { 00212 00213 typename std::vector<IndexType>::const_iterator 00214 last_index = start_vector->end(); 00215 typename std::vector<IndexType>::iterator 00216 start_index = start_vector->begin(); 00217 00218 for (; start_index != last_index; ++start_index) { 00219 00220 ObjectType & prim = primitives[*start_index]; 00221 00222 if (!prim.SelectTag()) { 00223 // compute the union 00224 output.push_back(*start_index); 00225 prim.SetSelectTag(true); 00226 } 00227 } 00228 } 00229 00230 // now iterate through the union and reset the tags 00231 00232 typename std::vector<IndexType>::const_iterator last_index = 00233 output.end(); 00234 typename std::vector<IndexType>::iterator start_index = 00235 output.begin(); 00236 00237 for (; start_index != last_index; ++start_index) { 00238 00239 ObjectType & prim = primitives[*start_index]; 00240 prim.SetSelectTag(false); 00241 } 00242 } 00243 00244 00245 static 00246 void 00247 Difference( 00248 std::vector< IndexType> &a, 00249 std::vector< IndexType> &b, 00250 std::vector<ObjectType> &primitives, 00251 std::vector< IndexType> &output 00252 ) { 00253 00254 // iterate through b mark all 00255 // iterate through a and add to output all unmarked 00256 00257 typename std::vector<IndexType>::const_iterator last_index = 00258 b.end(); 00259 typename std::vector<IndexType>::iterator start_index = 00260 b.begin(); 00261 00262 for (; start_index != last_index; ++start_index) { 00263 00264 ObjectType & prim = primitives[*start_index]; 00265 prim.SetSelectTag(true); 00266 } 00267 00268 last_index = a.end(); 00269 start_index = a.begin(); 00270 00271 for (; start_index != last_index; ++start_index) { 00272 00273 ObjectType & prim = primitives[*start_index]; 00274 if (!prim.SelectTag()) { 00275 output.push_back(*start_index); 00276 } 00277 } 00278 00279 // clean up the tags 00280 00281 last_index = b.end(); 00282 start_index = b.begin(); 00283 00284 for (; start_index != last_index; ++start_index) { 00285 00286 ObjectType & prim = primitives[*start_index]; 00287 prim.SetSelectTag(false); 00288 } 00289 }; 00290 00291 private : 00292 00293 // private constructor - this class is not meant for 00294 // instantiation 00295 00296 CTR_TaggedSetOps(); 00297 00298 }; 00299 00300 #endif 00301