Panda3D
paletteGroups.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file paletteGroups.cxx
10  * @author drose
11  * @date 2000-11-30
12  */
13 
14 #include "paletteGroups.h"
15 #include "paletteGroup.h"
16 
17 #include "indent.h"
18 #include "datagram.h"
19 #include "datagramIterator.h"
20 #include "bamReader.h"
21 #include "bamWriter.h"
22 #include "indirectCompareNames.h"
23 #include "pvector.h"
24 
25 TypeHandle PaletteGroups::_type_handle;
26 
27 /**
28  *
29  */
30 PaletteGroups::
31 PaletteGroups() {
32 }
33 
34 /**
35  *
36  */
37 PaletteGroups::
38 PaletteGroups(const PaletteGroups &copy) :
39  _groups(copy._groups)
40 {
41 }
42 
43 /**
44  *
45  */
46 void PaletteGroups::
47 operator = (const PaletteGroups &copy) {
48  _groups = copy._groups;
49 }
50 
51 /**
52  * Inserts a new group to the set, if it is not already there.
53  */
54 void PaletteGroups::
56  _groups.insert(group);
57 }
58 
59 /**
60  * Returns the number of times the given group appears in the set. This is
61  * either 1 if it appears at all, or 0 if it does not appear.
62  */
63 PaletteGroups::size_type PaletteGroups::
64 count(PaletteGroup *group) const {
65  return _groups.count(group);
66 }
67 
68 /**
69  * Completes the set with the transitive closure of all dependencies: for each
70  * PaletteGroup already in the set a, all of the groups that it depends on are
71  * added to the set, and so on. The indicated set a may be the same as this
72  * set.
73  */
74 void PaletteGroups::
76  Groups result;
77 
78  Groups::const_iterator gi;
79  for (gi = a._groups.begin(); gi != a._groups.end(); ++gi) {
80  r_make_complete(result, *gi);
81  }
82 
83  _groups.swap(result);
84 }
85 
86 /**
87  * Computes the union of PaletteGroups a and b, and stores the result in this
88  * object. The result may be the same object as either a or b.
89  */
90 void PaletteGroups::
91 make_union(const PaletteGroups &a, const PaletteGroups &b) {
92  Groups u;
93 
94  Groups::const_iterator ai, bi;
95  ai = a._groups.begin();
96  bi = b._groups.begin();
97 
98  while (ai != a._groups.end() && bi != b._groups.end()) {
99  if ((*ai) < (*bi)) {
100  u.insert(u.end(), *ai);
101  ++ai;
102 
103  } else if ((*bi) < (*ai)) {
104  u.insert(u.end(), *bi);
105  ++bi;
106 
107  } else { // (*ai) == (*bi)
108  u.insert(u.end(), *ai);
109  ++ai;
110  ++bi;
111  }
112  }
113 
114  while (ai != a._groups.end()) {
115  u.insert(u.end(), *ai);
116  ++ai;
117  }
118 
119  while (bi != b._groups.end()) {
120  u.insert(u.end(), *bi);
121  ++bi;
122  }
123 
124  _groups.swap(u);
125 }
126 
127 /**
128  * Computes the intersection of PaletteGroups a and b, and stores the result
129  * in this object. The result may be the same object as either a or b.
130  */
131 void PaletteGroups::
133  Groups i;
134 
135  Groups::const_iterator ai, bi;
136  ai = a._groups.begin();
137  bi = b._groups.begin();
138 
139  while (ai != a._groups.end() && bi != b._groups.end()) {
140  if ((*ai) < (*bi)) {
141  ++ai;
142 
143  } else if ((*bi) < (*ai)) {
144  ++bi;
145 
146  } else { // (*ai) == (*bi)
147  i.insert(i.end(), *ai);
148  ++ai;
149  ++bi;
150  }
151  }
152 
153  _groups.swap(i);
154 }
155 
156 /**
157  * Removes the special "null" group from the set. This is a special group
158  * that egg files may be assigned to, but which textures never are; it
159  * indicates that the egg file should not influence the palette assignment.
160  */
161 void PaletteGroups::
163  Groups::iterator gi;
164  for (gi = _groups.begin(); gi != _groups.end(); ++gi) {
165  if ((*gi)->get_name() == "null") {
166  _groups.erase(gi);
167  return;
168  }
169  }
170 }
171 
172 /**
173  * Empties the set.
174  */
175 void PaletteGroups::
176 clear() {
177  _groups.clear();
178 }
179 
180 /**
181  * Returns true if the set is empty, false otherwise.
182  */
183 bool PaletteGroups::
184 empty() const {
185  return _groups.empty();
186 }
187 
188 /**
189  * Returns the number of elements in the set.
190  */
191 PaletteGroups::size_type PaletteGroups::
192 size() const {
193  return _groups.size();
194 }
195 
196 /**
197  * Returns an iterator suitable for traversing the set.
198  */
199 PaletteGroups::iterator PaletteGroups::
200 begin() const {
201  return _groups.begin();
202 }
203 
204 /**
205  * Returns an iterator suitable for traversing the set.
206  */
207 PaletteGroups::iterator PaletteGroups::
208 end() const {
209  return _groups.end();
210 }
211 
212 /**
213  *
214  */
215 void PaletteGroups::
216 output(std::ostream &out) const {
217  if (!_groups.empty()) {
218  // Sort the group names into order by name for output.
219  pvector<PaletteGroup *> group_vector;
220  group_vector.reserve(_groups.size());
221  Groups::const_iterator gi;
222  for (gi = _groups.begin(); gi != _groups.end(); ++gi) {
223  group_vector.push_back(*gi);
224  }
225  sort(group_vector.begin(), group_vector.end(),
227 
228  pvector<PaletteGroup *>::const_iterator gvi = group_vector.begin();
229  out << (*gvi)->get_name();
230  ++gvi;
231  while (gvi != group_vector.end()) {
232  out << " " << (*gvi)->get_name();
233  ++gvi;
234  }
235  }
236 }
237 
238 /**
239  *
240  */
241 void PaletteGroups::
242 write(std::ostream &out, int indent_level) const {
243  // Sort the group names into order by name for output.
244  pvector<PaletteGroup *> group_vector;
245  group_vector.reserve(_groups.size());
246  Groups::const_iterator gi;
247  for (gi = _groups.begin(); gi != _groups.end(); ++gi) {
248  group_vector.push_back(*gi);
249  }
250  sort(group_vector.begin(), group_vector.end(),
252 
254  for (gvi = group_vector.begin(); gvi != group_vector.end(); ++gvi) {
255  indent(out, indent_level) << (*gvi)->get_name() << "\n";
256  }
257 }
258 
259 /**
260  * The recursive implementation of make_complete(), this adds the indicated
261  * group and all of its dependencies to the set.
262  */
263 void PaletteGroups::
264 r_make_complete(PaletteGroups::Groups &result, PaletteGroup *group) {
265  bool inserted = result.insert(group).second;
266 
267  if (inserted) {
268  Groups::const_iterator gi;
269  for (gi = group->_dependent._groups.begin();
270  gi != group->_dependent._groups.end();
271  ++gi) {
272  r_make_complete(result, *gi);
273  }
274  }
275 }
276 
277 /**
278  * Registers the current object as something that can be read from a Bam file.
279  */
280 void PaletteGroups::
283  register_factory(get_class_type(), make_PaletteGroups);
284 }
285 
286 /**
287  * Fills the indicated datagram up with a binary representation of the current
288  * object, in preparation for writing to a Bam file.
289  */
290 void PaletteGroups::
291 write_datagram(BamWriter *writer, Datagram &datagram) {
292  TypedWritable::write_datagram(writer, datagram);
293  datagram.add_uint32(_groups.size());
294 
295  Groups::const_iterator gi;
296  for (gi = _groups.begin(); gi != _groups.end(); ++gi) {
297  writer->write_pointer(datagram, *gi);
298  }
299 }
300 
301 /**
302  * Called after the object is otherwise completely read from a Bam file, this
303  * function's job is to store the pointers that were retrieved from the Bam
304  * file for each pointer object written. The return value is the number of
305  * pointers processed from the list.
306  */
307 int PaletteGroups::
309  int pi = TypedWritable::complete_pointers(p_list, manager);
310  for (int i = 0; i < _num_groups; i++) {
311  PaletteGroup *group;
312  DCAST_INTO_R(group, p_list[pi++], i);
313  _groups.insert(group);
314  }
315  return pi;
316 }
317 
318 /**
319  * This method is called by the BamReader when an object of this type is
320  * encountered in a Bam file; it should allocate and return a new object with
321  * all the data read.
322  */
323 TypedWritable* PaletteGroups::
324 make_PaletteGroups(const FactoryParams &params) {
325  PaletteGroups *me = new PaletteGroups;
326  DatagramIterator scan;
327  BamReader *manager;
328 
329  parse_params(params, scan, manager);
330  me->fillin(scan, manager);
331  return me;
332 }
333 
334 /**
335  * Reads the binary data from the given datagram iterator, which was written
336  * by a previous call to write_datagram().
337  */
338 void PaletteGroups::
340  TypedWritable::fillin(scan, manager);
341  _num_groups = scan.get_int32();
342  manager->read_pointers(scan, _num_groups);
343 }
void make_intersection(const PaletteGroups &a, const PaletteGroups &b)
Computes the intersection of PaletteGroups a and b, and stores the result in this object.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Called after the object is otherwise completely read from a Bam file, this function's job is to store...
iterator end() const
Returns an iterator suitable for traversing the set.
void remove_null()
Removes the special "null" group from the set.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
virtual void write_datagram(BamWriter *writer, Datagram &datagram)
Fills the indicated datagram up with a binary representation of the current object,...
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
iterator begin() const
Returns an iterator suitable for traversing the set.
size_type count(PaletteGroup *group) const
Returns the number of times the given group appears in the set.
This is the highest level of grouping for TextureImages.
Definition: paletteGroup.h:43
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
size_type size() const
Returns the number of elements in the set.
void fillin(DatagramIterator &scan, BamReader *manager)
Reads the binary data from the given datagram iterator, which was written by a previous call to write...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void clear()
Empties the set.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
int32_t get_int32()
Extracts a signed 32-bit integer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition: datagram.I:94
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
void read_pointers(DatagramIterator &scan, int count)
A convenience function to read a contiguous list of pointers.
Definition: bamReader.cxx:653
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool empty() const
Returns true if the set is empty, false otherwise.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void make_complete(const PaletteGroups &a)
Completes the set with the transitive closure of all dependencies: for each PaletteGroup already in t...
void make_union(const PaletteGroups &a, const PaletteGroups &b)
Computes the union of PaletteGroups a and b, and stores the result in this object.
void insert(PaletteGroup *group)
Inserts a new group to the set, if it is not already there.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
A set of PaletteGroups.
Definition: paletteGroups.h:28
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:317