Panda3D
|
00001 // Filename: paletteGroups.cxx 00002 // Created by: drose (30Nov00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "paletteGroups.h" 00016 #include "paletteGroup.h" 00017 00018 #include "indent.h" 00019 #include "datagram.h" 00020 #include "datagramIterator.h" 00021 #include "bamReader.h" 00022 #include "bamWriter.h" 00023 #include "indirectCompareNames.h" 00024 #include "pvector.h" 00025 00026 TypeHandle PaletteGroups::_type_handle; 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: PaletteGroups::Constructor 00030 // Access: Public 00031 // Description: 00032 //////////////////////////////////////////////////////////////////// 00033 PaletteGroups:: 00034 PaletteGroups() { 00035 } 00036 00037 //////////////////////////////////////////////////////////////////// 00038 // Function: PaletteGroups::Copy Constructor 00039 // Access: Public 00040 // Description: 00041 //////////////////////////////////////////////////////////////////// 00042 PaletteGroups:: 00043 PaletteGroups(const PaletteGroups ©) : 00044 _groups(copy._groups) 00045 { 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: PaletteGroups::operator = 00050 // Access: Public 00051 // Description: 00052 //////////////////////////////////////////////////////////////////// 00053 void PaletteGroups:: 00054 operator = (const PaletteGroups ©) { 00055 _groups = copy._groups; 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: PaletteGroups::insert 00060 // Access: Public 00061 // Description: Inserts a new group to the set, if it is not already 00062 // there. 00063 //////////////////////////////////////////////////////////////////// 00064 void PaletteGroups:: 00065 insert(PaletteGroup *group) { 00066 _groups.insert(group); 00067 } 00068 00069 //////////////////////////////////////////////////////////////////// 00070 // Function: PaletteGroups::count 00071 // Access: Public 00072 // Description: Returns the number of times the given group appears 00073 // in the set. This is either 1 if it appears at all, 00074 // or 0 if it does not appear. 00075 //////////////////////////////////////////////////////////////////// 00076 PaletteGroups::size_type PaletteGroups:: 00077 count(PaletteGroup *group) const { 00078 return _groups.count(group); 00079 } 00080 00081 //////////////////////////////////////////////////////////////////// 00082 // Function: PaletteGroups::make_complete 00083 // Access: Public 00084 // Description: Completes the set with the transitive closure of all 00085 // dependencies: for each PaletteGroup already in the 00086 // set a, all of the groups that it depends on are added 00087 // to the set, and so on. The indicated set a may be 00088 // the same as this set. 00089 //////////////////////////////////////////////////////////////////// 00090 void PaletteGroups:: 00091 make_complete(const PaletteGroups &a) { 00092 Groups result; 00093 00094 Groups::const_iterator gi; 00095 for (gi = a._groups.begin(); gi != a._groups.end(); ++gi) { 00096 r_make_complete(result, *gi); 00097 } 00098 00099 _groups.swap(result); 00100 } 00101 00102 //////////////////////////////////////////////////////////////////// 00103 // Function: PaletteGroups::make_union 00104 // Access: Public 00105 // Description: Computes the union of PaletteGroups a and b, and 00106 // stores the result in this object. The result may be 00107 // the same object as either a or b. 00108 //////////////////////////////////////////////////////////////////// 00109 void PaletteGroups:: 00110 make_union(const PaletteGroups &a, const PaletteGroups &b) { 00111 Groups u; 00112 00113 Groups::const_iterator ai, bi; 00114 ai = a._groups.begin(); 00115 bi = b._groups.begin(); 00116 00117 while (ai != a._groups.end() && bi != b._groups.end()) { 00118 if ((*ai) < (*bi)) { 00119 u.insert(u.end(), *ai); 00120 ++ai; 00121 00122 } else if ((*bi) < (*ai)) { 00123 u.insert(u.end(), *bi); 00124 ++bi; 00125 00126 } else { // (*ai) == (*bi) 00127 u.insert(u.end(), *ai); 00128 ++ai; 00129 ++bi; 00130 } 00131 } 00132 00133 while (ai != a._groups.end()) { 00134 u.insert(u.end(), *ai); 00135 ++ai; 00136 } 00137 00138 while (bi != b._groups.end()) { 00139 u.insert(u.end(), *bi); 00140 ++bi; 00141 } 00142 00143 _groups.swap(u); 00144 } 00145 00146 //////////////////////////////////////////////////////////////////// 00147 // Function: PaletteGroups::make_intersection 00148 // Access: Public 00149 // Description: Computes the intersection of PaletteGroups a and b, 00150 // and stores the result in this object. The result may 00151 // be the same object as either a or b. 00152 //////////////////////////////////////////////////////////////////// 00153 void PaletteGroups:: 00154 make_intersection(const PaletteGroups &a, const PaletteGroups &b) { 00155 Groups i; 00156 00157 Groups::const_iterator ai, bi; 00158 ai = a._groups.begin(); 00159 bi = b._groups.begin(); 00160 00161 while (ai != a._groups.end() && bi != b._groups.end()) { 00162 if ((*ai) < (*bi)) { 00163 ++ai; 00164 00165 } else if ((*bi) < (*ai)) { 00166 ++bi; 00167 00168 } else { // (*ai) == (*bi) 00169 i.insert(i.end(), *ai); 00170 ++ai; 00171 ++bi; 00172 } 00173 } 00174 00175 _groups.swap(i); 00176 } 00177 00178 //////////////////////////////////////////////////////////////////// 00179 // Function: PaletteGroups::remove_null 00180 // Access: Public 00181 // Description: Removes the special "null" group from the set. This 00182 // is a special group that egg files may be assigned to, 00183 // but which textures never are; it indicates that the 00184 // egg file should not influence the palette assignment. 00185 //////////////////////////////////////////////////////////////////// 00186 void PaletteGroups:: 00187 remove_null() { 00188 Groups::iterator gi; 00189 for (gi = _groups.begin(); gi != _groups.end(); ++gi) { 00190 if ((*gi)->get_name() == "null") { 00191 _groups.erase(gi); 00192 return; 00193 } 00194 } 00195 } 00196 00197 //////////////////////////////////////////////////////////////////// 00198 // Function: PaletteGroups::clear 00199 // Access: Public 00200 // Description: Empties the set. 00201 //////////////////////////////////////////////////////////////////// 00202 void PaletteGroups:: 00203 clear() { 00204 _groups.clear(); 00205 } 00206 00207 //////////////////////////////////////////////////////////////////// 00208 // Function: PaletteGroups::empty 00209 // Access: Public 00210 // Description: Returns true if the set is empty, false otherwise. 00211 //////////////////////////////////////////////////////////////////// 00212 bool PaletteGroups:: 00213 empty() const { 00214 return _groups.empty(); 00215 } 00216 00217 //////////////////////////////////////////////////////////////////// 00218 // Function: PaletteGroups::size 00219 // Access: Public 00220 // Description: Returns the number of elements in the set. 00221 //////////////////////////////////////////////////////////////////// 00222 PaletteGroups::size_type PaletteGroups:: 00223 size() const { 00224 return _groups.size(); 00225 } 00226 00227 //////////////////////////////////////////////////////////////////// 00228 // Function: PaletteGroups::begin 00229 // Access: Public 00230 // Description: Returns an iterator suitable for traversing the set. 00231 //////////////////////////////////////////////////////////////////// 00232 PaletteGroups::iterator PaletteGroups:: 00233 begin() const { 00234 return _groups.begin(); 00235 } 00236 00237 //////////////////////////////////////////////////////////////////// 00238 // Function: PaletteGroups::end 00239 // Access: Public 00240 // Description: Returns an iterator suitable for traversing the set. 00241 //////////////////////////////////////////////////////////////////// 00242 PaletteGroups::iterator PaletteGroups:: 00243 end() const { 00244 return _groups.end(); 00245 } 00246 00247 //////////////////////////////////////////////////////////////////// 00248 // Function: PaletteGroups::output 00249 // Access: Public 00250 // Description: 00251 //////////////////////////////////////////////////////////////////// 00252 void PaletteGroups:: 00253 output(ostream &out) const { 00254 if (!_groups.empty()) { 00255 // Sort the group names into order by name for output. 00256 pvector<PaletteGroup *> group_vector; 00257 group_vector.reserve(_groups.size()); 00258 Groups::const_iterator gi; 00259 for (gi = _groups.begin(); gi != _groups.end(); ++gi) { 00260 group_vector.push_back(*gi); 00261 } 00262 sort(group_vector.begin(), group_vector.end(), 00263 IndirectCompareNames<PaletteGroup>()); 00264 00265 pvector<PaletteGroup *>::const_iterator gvi = group_vector.begin(); 00266 out << (*gvi)->get_name(); 00267 ++gvi; 00268 while (gvi != group_vector.end()) { 00269 out << " " << (*gvi)->get_name(); 00270 ++gvi; 00271 } 00272 } 00273 } 00274 00275 //////////////////////////////////////////////////////////////////// 00276 // Function: PaletteGroups::write 00277 // Access: Public 00278 // Description: 00279 //////////////////////////////////////////////////////////////////// 00280 void PaletteGroups:: 00281 write(ostream &out, int indent_level) const { 00282 // Sort the group names into order by name for output. 00283 pvector<PaletteGroup *> group_vector; 00284 group_vector.reserve(_groups.size()); 00285 Groups::const_iterator gi; 00286 for (gi = _groups.begin(); gi != _groups.end(); ++gi) { 00287 group_vector.push_back(*gi); 00288 } 00289 sort(group_vector.begin(), group_vector.end(), 00290 IndirectCompareNames<PaletteGroup>()); 00291 00292 pvector<PaletteGroup *>::const_iterator gvi; 00293 for (gvi = group_vector.begin(); gvi != group_vector.end(); ++gvi) { 00294 indent(out, indent_level) << (*gvi)->get_name() << "\n"; 00295 } 00296 } 00297 00298 //////////////////////////////////////////////////////////////////// 00299 // Function: PaletteGroups::r_make_complete 00300 // Access: Private 00301 // Description: The recursive implementation of make_complete(), this 00302 // adds the indicated group and all of its dependencies 00303 // to the set. 00304 //////////////////////////////////////////////////////////////////// 00305 void PaletteGroups:: 00306 r_make_complete(PaletteGroups::Groups &result, PaletteGroup *group) { 00307 bool inserted = result.insert(group).second; 00308 00309 if (inserted) { 00310 Groups::const_iterator gi; 00311 for (gi = group->_dependent._groups.begin(); 00312 gi != group->_dependent._groups.end(); 00313 ++gi) { 00314 r_make_complete(result, *gi); 00315 } 00316 } 00317 } 00318 00319 //////////////////////////////////////////////////////////////////// 00320 // Function: PaletteGroups::register_with_read_factory 00321 // Access: Public, Static 00322 // Description: Registers the current object as something that can be 00323 // read from a Bam file. 00324 //////////////////////////////////////////////////////////////////// 00325 void PaletteGroups:: 00326 register_with_read_factory() { 00327 BamReader::get_factory()-> 00328 register_factory(get_class_type(), make_PaletteGroups); 00329 } 00330 00331 //////////////////////////////////////////////////////////////////// 00332 // Function: PaletteGroups::write_datagram 00333 // Access: Public, Virtual 00334 // Description: Fills the indicated datagram up with a binary 00335 // representation of the current object, in preparation 00336 // for writing to a Bam file. 00337 //////////////////////////////////////////////////////////////////// 00338 void PaletteGroups:: 00339 write_datagram(BamWriter *writer, Datagram &datagram) { 00340 TypedWritable::write_datagram(writer, datagram); 00341 datagram.add_uint32(_groups.size()); 00342 00343 Groups::const_iterator gi; 00344 for (gi = _groups.begin(); gi != _groups.end(); ++gi) { 00345 writer->write_pointer(datagram, *gi); 00346 } 00347 } 00348 00349 //////////////////////////////////////////////////////////////////// 00350 // Function: PaletteGroups::complete_pointers 00351 // Access: Public, Virtual 00352 // Description: Called after the object is otherwise completely read 00353 // from a Bam file, this function's job is to store the 00354 // pointers that were retrieved from the Bam file for 00355 // each pointer object written. The return value is the 00356 // number of pointers processed from the list. 00357 //////////////////////////////////////////////////////////////////// 00358 int PaletteGroups:: 00359 complete_pointers(TypedWritable **p_list, BamReader *manager) { 00360 int pi = TypedWritable::complete_pointers(p_list, manager); 00361 for (int i = 0; i < _num_groups; i++) { 00362 PaletteGroup *group; 00363 DCAST_INTO_R(group, p_list[pi++], i); 00364 _groups.insert(group); 00365 } 00366 return pi; 00367 } 00368 00369 //////////////////////////////////////////////////////////////////// 00370 // Function: PaletteGroups::make_PaletteGroups 00371 // Access: Protected 00372 // Description: This method is called by the BamReader when an object 00373 // of this type is encountered in a Bam file; it should 00374 // allocate and return a new object with all the data 00375 // read. 00376 //////////////////////////////////////////////////////////////////// 00377 TypedWritable* PaletteGroups:: 00378 make_PaletteGroups(const FactoryParams ¶ms) { 00379 PaletteGroups *me = new PaletteGroups; 00380 DatagramIterator scan; 00381 BamReader *manager; 00382 00383 parse_params(params, scan, manager); 00384 me->fillin(scan, manager); 00385 return me; 00386 } 00387 00388 //////////////////////////////////////////////////////////////////// 00389 // Function: PaletteGroups::fillin 00390 // Access: Protected 00391 // Description: Reads the binary data from the given datagram 00392 // iterator, which was written by a previous call to 00393 // write_datagram(). 00394 //////////////////////////////////////////////////////////////////// 00395 void PaletteGroups:: 00396 fillin(DatagramIterator &scan, BamReader *manager) { 00397 TypedWritable::fillin(scan, manager); 00398 _num_groups = scan.get_int32(); 00399 manager->read_pointers(scan, _num_groups); 00400 }