Panda3D
fltTransformScale.cxx
1 // Filename: fltTransformScale.cxx
2 // Created by: drose (30Aug00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "fltTransformScale.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 
19 TypeHandle FltTransformScale::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: FltTransformScale::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 FltTransformScale::
27 FltTransformScale(FltHeader *header) : FltTransformRecord(header) {
28  _center.set(0.0, 0.0, 0.0);
29  _scale.set(1.0, 1.0, 1.0);
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: FltTransformScale::set
34 // Access: Public
35 // Description: Defines the scale.
36 ////////////////////////////////////////////////////////////////////
38 set(const LPoint3d &center, const LVecBase3 &scale) {
39  _center = center;
40  _scale = scale;
41 
42  recompute_matrix();
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: FltTransformScale::has_center
47 // Access: Public
48 // Description: Returns true if the center is specified, false if it
49 // is not. For some reason, MultiGen stores large
50 // negative numbers in for the center if it is not
51 // specified. It is unclear what the purpose of this
52 // is.
53 ////////////////////////////////////////////////////////////////////
55 has_center() const {
56  return
57  _center[0] > -1e+08 &&
58  _center[1] > -1e+08 &&
59  _center[2] > -1e+08;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: FltTransformScale::get_center
64 // Access: Public
65 // Description:
66 ////////////////////////////////////////////////////////////////////
67 const LPoint3d &FltTransformScale::
68 get_center() const {
69  return _center;
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: FltTransformScale::get_scale
74 // Access: Public
75 // Description:
76 ////////////////////////////////////////////////////////////////////
77 const LVecBase3 &FltTransformScale::
78 get_scale() const {
79  return _scale;
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: FltTransformScale::recompute_matrix
84 // Access: Private
85 // Description:
86 ////////////////////////////////////////////////////////////////////
87 void FltTransformScale::
88 recompute_matrix() {
89  if (has_center()) {
90  _matrix =
91  LMatrix4d::translate_mat(-_center) *
92  LMatrix4d::scale_mat(LCAST(double, _scale)) *
93  LMatrix4d::translate_mat(_center);
94  } else {
95  _matrix =
96  LMatrix4d::scale_mat(LCAST(double, _scale));
97  }
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function: FltTransformScale::extract_record
102 // Access: Protected, Virtual
103 // Description: Fills in the information in this record based on the
104 // information given in the indicated datagram, whose
105 // opcode has already been read. Returns true on
106 // success, false if the datagram is invalid.
107 ////////////////////////////////////////////////////////////////////
108 bool FltTransformScale::
109 extract_record(FltRecordReader &reader) {
110  if (!FltTransformRecord::extract_record(reader)) {
111  return false;
112  }
113 
114  nassertr(reader.get_opcode() == FO_scale, false);
115  DatagramIterator &iterator = reader.get_iterator();
116 
117  iterator.skip_bytes(4);
118 
119  _center[0] = iterator.get_be_float64();
120  _center[1] = iterator.get_be_float64();
121  _center[2] = iterator.get_be_float64();
122  _scale[0] = iterator.get_be_float32();
123  _scale[1] = iterator.get_be_float32();
124  _scale[2] = iterator.get_be_float32();
125 
126  iterator.skip_bytes(4); // Undocumented additional padding.
127 
128  recompute_matrix();
129 
130  check_remaining_size(iterator);
131  return true;
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function: FltTransformScale::build_record
136 // Access: Protected, Virtual
137 // Description: Fills up the current record on the FltRecordWriter with
138 // data for this record, but does not advance the
139 // writer. Returns true on success, false if there is
140 // some error.
141 ////////////////////////////////////////////////////////////////////
142 bool FltTransformScale::
143 build_record(FltRecordWriter &writer) const {
144  if (!FltTransformRecord::build_record(writer)) {
145  return false;
146  }
147 
148  writer.set_opcode(FO_scale);
149  Datagram &datagram = writer.update_datagram();
150 
151  datagram.pad_bytes(4); // Undocumented additional padding.
152 
153  datagram.add_be_float64(_center[0]);
154  datagram.add_be_float64(_center[1]);
155  datagram.add_be_float64(_center[2]);
156  datagram.add_be_float32(_scale[0]);
157  datagram.add_be_float32(_scale[1]);
158  datagram.add_be_float32(_scale[2]);
159 
160  datagram.pad_bytes(4); // Undocumented additional padding.
161 
162  return true;
163 }
164 
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly...
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
static LMatrix4d scale_mat(const LVecBase3d &scale)
Returns a matrix that applies the indicated scale in each of the three axes.
Definition: lmatrix.h:6721
void check_remaining_size(const DatagramIterator &di, const string &name=string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record...
Definition: fltRecord.cxx:313
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
static LMatrix4d translate_mat(const LVecBase3d &trans)
Returns a matrix that applies the indicated translation.
Definition: lmatrix.h:6662
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:111
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the datagram.
Definition: datagram.I:339
void add_be_float32(PN_float32 value)
Adds a 32-bit single-precision big-endian floating-point number to the datagram.
Definition: datagram.I:327
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
void set(const LPoint3d &center, const LVecBase3 &scale)
Defines the scale.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:544
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
bool has_center() const
Returns true if the center is specified, false if it is not.
A base class for a number of types of ancillary records that follow beads and indicate some kind of a...
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:85
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43