Panda3D
transformBlendTable.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 transformBlendTable.cxx
10  * @author drose
11  * @date 2005-03-24
12  */
13 
14 #include "transformBlendTable.h"
15 #include "indent.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 
19 TypeHandle TransformBlendTable::_type_handle;
20 
21 /**
22  * Required to implement CopyOnWriteObject.
23  */
24 PT(CopyOnWriteObject) TransformBlendTable::
25 make_cow_copy() {
26  return new TransformBlendTable(*this);
27 }
28 
29 /**
30  *
31  */
32 TransformBlendTable::
33 TransformBlendTable() {
34 }
35 
36 /**
37  *
38  */
39 TransformBlendTable::
40 TransformBlendTable(const TransformBlendTable &copy) :
41  _blends(copy._blends),
42  _rows(copy._rows)
43 {
44 }
45 
46 /**
47  *
48  */
49 void TransformBlendTable::
50 operator = (const TransformBlendTable &copy) {
51  _blends = copy._blends;
52  _rows = copy._rows;
53  clear_index();
54 }
55 
56 /**
57  *
58  */
59 TransformBlendTable::
60 ~TransformBlendTable() {
61 }
62 
63 /**
64  * Replaces the blend at the nth position with the indicated value.
65  */
67 set_blend(size_t n, const TransformBlend &blend) {
68  nassertv(n < _blends.size());
69  _blends[n] = blend;
70 }
71 
72 /**
73  * Removes the blend at the nth position.
74  */
76 remove_blend(size_t n) {
77  nassertv(n < _blends.size());
78  _blends.erase(_blends.begin() + n);
79 }
80 
81 /**
82  * Adds a new blend to the table, and returns its index number. If there is
83  * already an identical blend in the table, simply returns that number
84  * instead.
85  */
87 add_blend(const TransformBlend &blend) {
88  consider_rebuild_index();
89 
90  BlendIndex::iterator bi;
91  bi = _blend_index.find(&blend);
92  if (bi != _blend_index.end()) {
93  // Already had it.
94  return (*bi).second;
95  }
96 
97  bool needs_realloc = (_blends.size() >= _blends.capacity());
98  size_t new_position = _blends.size();
99  _blends.push_back(blend);
100 
101  if (needs_realloc) {
102  // We just reallocated the blends vector, so we must rebuild the index.
103  clear_index();
104 
105  } else {
106  // Since we didn't realloc the blends vector, just update it with the
107  // latest.
108  const TransformBlend &added_blend = _blends[new_position];
109  _blend_index[&added_blend] = new_position;
110  _max_simultaneous_transforms = std::max(_max_simultaneous_transforms,
111  (int)blend.get_num_transforms());
112 
113  // We can't compute this one as we go, so set it to a special value to
114  // indicate it needs to be recomputed.
115  _num_transforms = -1;
116  }
117 
118  return new_position;
119 }
120 
121 /**
122  *
123  */
124 void TransformBlendTable::
125 write(std::ostream &out, int indent_level) const {
126  for (size_t i = 0; i < _blends.size(); ++i) {
127  indent(out, indent_level)
128  << i << ". " << _blends[i] << "\n";
129  }
130 }
131 
132 /**
133  * Resets the index so that it will be rebuilt next time it is needed.
134  */
135 void TransformBlendTable::
136 clear_index() {
137  _blend_index.clear();
138 }
139 
140 /**
141  * Rebuilds the index so that we can easily determine what blend combinations
142  * are already present in the table.
143  */
144 void TransformBlendTable::
145 rebuild_index() {
146  _blend_index.clear();
147 
148  // We'll also count up these two statistics while we rebuild the index.
149  _num_transforms = 0;
150  _max_simultaneous_transforms = 0;
151 
153 
154  for (size_t i = 0; i < _blends.size(); ++i) {
155  const TransformBlend &blend = _blends[i];
156  _blend_index[&blend] = i;
157 
158  for (size_t ti = 0; ti < blend.get_num_transforms(); ++ti) {
159  transforms.insert(blend.get_transform(ti));
160  }
161  _max_simultaneous_transforms = std::max((size_t)_max_simultaneous_transforms,
162  blend.get_num_transforms());
163  }
164 
165  _num_transforms = transforms.size();
166 }
167 
168 /**
169  * Recomputes the modified stamp from the various TransformBlend objects, if
170  * necessary.
171  */
172 void TransformBlendTable::
173 recompute_modified(TransformBlendTable::CData *cdata, Thread *current_thread) {
174  // Update the global_modified sequence number first, to prevent race
175  // conditions.
176  cdata->_global_modified = VertexTransform::get_global_modified(current_thread);
177 
178  // Now get the local modified number.
179  UpdateSeq seq;
180  Blends::const_iterator bi;
181  for (bi = _blends.begin(); bi != _blends.end(); ++bi) {
182  seq = std::max(seq, (*bi).get_modified(current_thread));
183  }
184 
185  cdata->_modified = seq;
186 }
187 
188 /**
189  * Clears the modified stamp to force it to be recomputed.
190  */
191 void TransformBlendTable::
192 clear_modified(Thread *current_thread) {
193  CDWriter cdata(_cycler, true, current_thread);
194  cdata->_global_modified = UpdateSeq();
195  cdata->_modified = UpdateSeq();
196 }
197 
198 /**
199  * Tells the BamReader how to create objects of type TransformBlendTable.
200  */
203  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
204 }
205 
206 /**
207  * Writes the contents of this object to the datagram for shipping out to a
208  * Bam file.
209  */
212  TypedWritable::write_datagram(manager, dg);
213 
214  dg.add_uint16(_blends.size());
215  Blends::const_iterator bi;
216  for (bi = _blends.begin(); bi != _blends.end(); ++bi) {
217  (*bi).write_datagram(manager, dg);
218  }
219 
220  _rows.write_datagram(manager, dg);
221 
222  manager->write_cdata(dg, _cycler);
223 }
224 
225 /**
226  * Receives an array of pointers, one for each time manager->read_pointer()
227  * was called in fillin(). Returns the number of pointers processed.
228  */
231  int pi = TypedWritable::complete_pointers(p_list, manager);
232 
233  Blends::iterator bi;
234  for (bi = _blends.begin(); bi != _blends.end(); ++bi) {
235  pi += (*bi).complete_pointers(p_list + pi, manager);
236  }
237 
238  return pi;
239 }
240 
241 /**
242  * This function is called by the BamReader's factory when a new object of
243  * type TransformBlendTable is encountered in the Bam file. It should create
244  * the TransformBlendTable and extract its information from the file.
245  */
246 TypedWritable *TransformBlendTable::
247 make_from_bam(const FactoryParams &params) {
249  DatagramIterator scan;
250  BamReader *manager;
251 
252  parse_params(params, scan, manager);
253  object->fillin(scan, manager);
254 
255  return object;
256 }
257 
258 /**
259  * This internal function is called by make_from_bam to read in all of the
260  * relevant data from the BamFile for the new TransformBlendTable.
261  */
262 void TransformBlendTable::
263 fillin(DatagramIterator &scan, BamReader *manager) {
264  TypedWritable::fillin(scan, manager);
265 
266  size_t num_blends = scan.get_uint16();
267  _blends.reserve(num_blends);
268  size_t i;
269  for (i = 0; i < num_blends; ++i) {
270  TransformBlend blend;
271  blend.fillin(scan, manager);
272  _blends.push_back(blend);
273  }
274 
275  if (manager->get_file_minor_ver() >= 7) {
276  _rows.read_datagram(scan, manager);
277  } else {
278  // In this case, for bam files prior to 6.7, we must define the
279  // SparseArray with the full number of vertices. This is done in
280  // GeomVertexData::complete_pointers().
281  }
282 
283  manager->read_cdata(scan, _cycler);
284 }
285 
286 /**
287  *
288  */
289 CycleData *TransformBlendTable::CData::
290 make_copy() const {
291  return new CData(*this);
292 }
293 
294 /**
295  * Writes the contents of this object to the datagram for shipping out to a
296  * Bam file.
297  */
298 void TransformBlendTable::CData::
299 write_datagram(BamWriter *manager, Datagram &dg) const {
300 }
301 
302 /**
303  * This internal function is called by make_from_bam to read in all of the
304  * relevant data from the BamFile for the new TransformBlendTable.
305  */
306 void TransformBlendTable::CData::
307 fillin(DatagramIterator &scan, BamReader *manager) {
308  Thread *current_thread = Thread::get_current_thread();
309  _modified = VertexTransform::get_next_modified(current_thread);
310  _global_modified = VertexTransform::get_global_modified(current_thread);
311 }
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is called by make_from_bam to read in all of the relevant data from the BamFil...
get_num_transforms
Returns the number of transforms stored in the blend object.
remove_blend
Removes the blend at the nth position.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
PT(CopyOnWriteObject) TransformBlendTable
Required to implement CopyOnWriteObject.
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition: bamReader.cxx:695
void write_datagram(BamWriter *manager, Datagram &dg) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:47
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:425
set_blend
Replaces the blend at the nth position with the indicated value.
static UpdateSeq get_next_modified(Thread *current_thread)
Returns a monotonically increasing sequence.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
get_transform
Returns the nth transform stored in the blend object.
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
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 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
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition: thread.h:109
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().
static UpdateSeq get_global_modified(Thread *current_thread)
Returns the currently highest VertexTransform::get_modified() value in the world.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
static void register_with_read_factory()
Tells the BamReader how to create objects of type TransformBlendTable.
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines a single entry in a TransformBlendTable.
size_t add_blend(const TransformBlend &blend)
Adds a new blend to the table, and returns its index number.
This base class provides basic reference counting, but also can be used with a CopyOnWritePointer to ...
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
A thread; that is, a lightweight process.
Definition: thread.h:46
This structure collects together the different combinations of transforms and blend amounts used by a...
This is our own Panda specialization on the default STL set.
Definition: pset.h:49
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
This is a sequence number that increments monotonically.
Definition: updateSeq.h:37
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void read_datagram(DatagramIterator &scan, BamReader *manager)
Reads the object that was previously written to a Bam file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.