Panda3D
 All Classes Functions Variables Enumerations
fltTransformPut.cxx
1 // Filename: fltTransformPut.cxx
2 // Created by: drose (29Aug00)
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 "fltTransformPut.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 
19 #include "look_at.h"
20 
21 TypeHandle FltTransformPut::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: FltTransformPut::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 FltTransformPut::
29 FltTransformPut(FltHeader *header) : FltTransformRecord(header) {
30  _from_origin.set(0.0, 0.0, 0.0);
31  _from_align.set(1.0, 0.0, 0.0);
32  _from_track.set(1.0, 0.0, 0.0);
33  _to_origin.set(0.0, 0.0, 0.0);
34  _to_align.set(1.0, 0.0, 0.0);
35  _to_track.set(1.0, 0.0, 0.0);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: FltTransformPut::set
40 // Access: Public
41 // Description: Defines the put explicitly. The transformation will
42 // map the three "from" points to the corresponding
43 // three "to" points.
44 ////////////////////////////////////////////////////////////////////
46 set(const LPoint3d &from_origin, const LPoint3d &from_align,
47  const LPoint3d &from_track,
48  const LPoint3d &to_origin, const LPoint3d &to_align,
49  const LPoint3d &to_track) {
50  _from_origin = from_origin;
51  _from_align = from_align;
52  _from_track = from_track;
53  _to_origin = to_origin;
54  _to_align = to_align;
55  _to_track = to_track;
56 
57  recompute_matrix();
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: FltTransformPut::get_from_origin
62 // Access: Public
63 // Description:
64 ////////////////////////////////////////////////////////////////////
65 const LPoint3d &FltTransformPut::
66 get_from_origin() const {
67  return _from_origin;
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: FltTransformPut::get_from_align
72 // Access: Public
73 // Description:
74 ////////////////////////////////////////////////////////////////////
75 const LPoint3d &FltTransformPut::
76 get_from_align() const {
77  return _from_align;
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: FltTransformPut::get_from_track
82 // Access: Public
83 // Description:
84 ////////////////////////////////////////////////////////////////////
85 const LPoint3d &FltTransformPut::
86 get_from_track() const {
87  return _from_track;
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: FltTransformPut::get_to_origin
92 // Access: Public
93 // Description:
94 ////////////////////////////////////////////////////////////////////
95 const LPoint3d &FltTransformPut::
96 get_to_origin() const {
97  return _to_origin;
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function: FltTransformPut::get_to_align
102 // Access: Public
103 // Description:
104 ////////////////////////////////////////////////////////////////////
105 const LPoint3d &FltTransformPut::
106 get_to_align() const {
107  return _to_align;
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: FltTransformPut::get_to_track
112 // Access: Public
113 // Description:
114 ////////////////////////////////////////////////////////////////////
115 const LPoint3d &FltTransformPut::
116 get_to_track() const {
117  return _to_track;
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: FltTransformPut::recompute_matrix
122 // Access: Private
123 // Description:
124 ////////////////////////////////////////////////////////////////////
125 void FltTransformPut::
126 recompute_matrix() {
127  LMatrix4d r1, r2;
128  look_at(r1, _from_align - _from_origin, _from_track - _from_origin, CS_zup_right);
129  look_at(r2, _to_align - _to_origin, _to_track - _to_origin, CS_zup_right);
130 
131  _matrix =
132  LMatrix4d::translate_mat(-_from_origin) *
133  invert(r1) *
134  r2 *
135  LMatrix4d::translate_mat(_to_origin);
136 }
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function: FltTransformPut::extract_record
140 // Access: Protected, Virtual
141 // Description: Fills in the information in this record based on the
142 // information given in the indicated datagram, whose
143 // opcode has already been read. Returns true on
144 // success, false if the datagram is invalid.
145 ////////////////////////////////////////////////////////////////////
146 bool FltTransformPut::
147 extract_record(FltRecordReader &reader) {
148  if (!FltTransformRecord::extract_record(reader)) {
149  return false;
150  }
151 
152  nassertr(reader.get_opcode() == FO_put, false);
153  DatagramIterator &iterator = reader.get_iterator();
154 
155  iterator.skip_bytes(4); // Undocumented additional padding.
156 
157  _from_origin[0] = iterator.get_be_float64();
158  _from_origin[1] = iterator.get_be_float64();
159  _from_origin[2] = iterator.get_be_float64();
160  _from_align[0] = iterator.get_be_float64();
161  _from_align[1] = iterator.get_be_float64();
162  _from_align[2] = iterator.get_be_float64();
163  _from_track[0] = iterator.get_be_float64();
164  _from_track[1] = iterator.get_be_float64();
165  _from_track[2] = iterator.get_be_float64();
166  _to_origin[0] = iterator.get_be_float64();
167  _to_origin[1] = iterator.get_be_float64();
168  _to_origin[2] = iterator.get_be_float64();
169  _to_align[0] = iterator.get_be_float64();
170  _to_align[1] = iterator.get_be_float64();
171  _to_align[2] = iterator.get_be_float64();
172  _to_track[0] = iterator.get_be_float64();
173  _to_track[1] = iterator.get_be_float64();
174  _to_track[2] = iterator.get_be_float64();
175 
176  recompute_matrix();
177 
178  check_remaining_size(iterator);
179  return true;
180 }
181 
182 ////////////////////////////////////////////////////////////////////
183 // Function: FltTransformPut::build_record
184 // Access: Protected, Virtual
185 // Description: Fills up the current record on the FltRecordWriter with
186 // data for this record, but does not advance the
187 // writer. Returns true on success, false if there is
188 // some error.
189 ////////////////////////////////////////////////////////////////////
190 bool FltTransformPut::
191 build_record(FltRecordWriter &writer) const {
192  if (!FltTransformRecord::build_record(writer)) {
193  return false;
194  }
195 
196  writer.set_opcode(FO_put);
197  Datagram &datagram = writer.update_datagram();
198 
199  datagram.pad_bytes(4); // Undocumented additional padding.
200 
201  datagram.add_be_float64(_from_origin[0]);
202  datagram.add_be_float64(_from_origin[1]);
203  datagram.add_be_float64(_from_origin[2]);
204  datagram.add_be_float64(_from_align[0]);
205  datagram.add_be_float64(_from_align[1]);
206  datagram.add_be_float64(_from_align[2]);
207  datagram.add_be_float64(_from_track[0]);
208  datagram.add_be_float64(_from_track[1]);
209  datagram.add_be_float64(_from_track[2]);
210  datagram.add_be_float64(_to_origin[0]);
211  datagram.add_be_float64(_to_origin[1]);
212  datagram.add_be_float64(_to_origin[2]);
213  datagram.add_be_float64(_to_align[0]);
214  datagram.add_be_float64(_to_align[1]);
215  datagram.add_be_float64(_to_align[2]);
216  datagram.add_be_float64(_to_track[0]);
217  datagram.add_be_float64(_to_track[1]);
218  datagram.add_be_float64(_to_track[2]);
219 
220  return true;
221 }
222 
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly...
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
void set(const LPoint3d &from_origin, const LPoint3d &from_align, const LPoint3d &from_track, const LPoint3d &to_origin, const LPoint3d &to_align, const LPoint3d &to_track)
Defines the put explicitly.
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 skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
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
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:531
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
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