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