Panda3D
fltTransformPut.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 fltTransformPut.cxx
10  * @author drose
11  * @date 2000-08-29
12  */
13 
14 #include "fltTransformPut.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 
18 #include "look_at.h"
19 
20 TypeHandle FltTransformPut::_type_handle;
21 
22 /**
23  *
24  */
25 FltTransformPut::
26 FltTransformPut(FltHeader *header) : FltTransformRecord(header) {
27  _from_origin.set(0.0, 0.0, 0.0);
28  _from_align.set(1.0, 0.0, 0.0);
29  _from_track.set(1.0, 0.0, 0.0);
30  _to_origin.set(0.0, 0.0, 0.0);
31  _to_align.set(1.0, 0.0, 0.0);
32  _to_track.set(1.0, 0.0, 0.0);
33 }
34 
35 /**
36  * Defines the put explicitly. The transformation will map the three "from"
37  * points to the corresponding three "to" points.
38  */
40 set(const LPoint3d &from_origin, const LPoint3d &from_align,
41  const LPoint3d &from_track,
42  const LPoint3d &to_origin, const LPoint3d &to_align,
43  const LPoint3d &to_track) {
44  _from_origin = from_origin;
45  _from_align = from_align;
46  _from_track = from_track;
47  _to_origin = to_origin;
48  _to_align = to_align;
49  _to_track = to_track;
50 
51  recompute_matrix();
52 }
53 
54 /**
55  *
56  */
57 const LPoint3d &FltTransformPut::
58 get_from_origin() const {
59  return _from_origin;
60 }
61 
62 /**
63  *
64  */
65 const LPoint3d &FltTransformPut::
66 get_from_align() const {
67  return _from_align;
68 }
69 
70 /**
71  *
72  */
73 const LPoint3d &FltTransformPut::
74 get_from_track() const {
75  return _from_track;
76 }
77 
78 /**
79  *
80  */
81 const LPoint3d &FltTransformPut::
82 get_to_origin() const {
83  return _to_origin;
84 }
85 
86 /**
87  *
88  */
89 const LPoint3d &FltTransformPut::
90 get_to_align() const {
91  return _to_align;
92 }
93 
94 /**
95  *
96  */
97 const LPoint3d &FltTransformPut::
98 get_to_track() const {
99  return _to_track;
100 }
101 
102 /**
103  *
104  */
105 void FltTransformPut::
106 recompute_matrix() {
107  LMatrix4d r1, r2;
108  look_at(r1, _from_align - _from_origin, _from_track - _from_origin, CS_zup_right);
109  look_at(r2, _to_align - _to_origin, _to_track - _to_origin, CS_zup_right);
110 
111  _matrix =
112  LMatrix4d::translate_mat(-_from_origin) *
113  invert(r1) *
114  r2 *
115  LMatrix4d::translate_mat(_to_origin);
116 }
117 
118 /**
119  * Fills in the information in this record based on the information given in
120  * the indicated datagram, whose opcode has already been read. Returns true
121  * on success, false if the datagram is invalid.
122  */
123 bool FltTransformPut::
124 extract_record(FltRecordReader &reader) {
125  if (!FltTransformRecord::extract_record(reader)) {
126  return false;
127  }
128 
129  nassertr(reader.get_opcode() == FO_put, false);
130  DatagramIterator &iterator = reader.get_iterator();
131 
132  iterator.skip_bytes(4); // Undocumented additional padding.
133 
134  _from_origin[0] = iterator.get_be_float64();
135  _from_origin[1] = iterator.get_be_float64();
136  _from_origin[2] = iterator.get_be_float64();
137  _from_align[0] = iterator.get_be_float64();
138  _from_align[1] = iterator.get_be_float64();
139  _from_align[2] = iterator.get_be_float64();
140  _from_track[0] = iterator.get_be_float64();
141  _from_track[1] = iterator.get_be_float64();
142  _from_track[2] = iterator.get_be_float64();
143  _to_origin[0] = iterator.get_be_float64();
144  _to_origin[1] = iterator.get_be_float64();
145  _to_origin[2] = iterator.get_be_float64();
146  _to_align[0] = iterator.get_be_float64();
147  _to_align[1] = iterator.get_be_float64();
148  _to_align[2] = iterator.get_be_float64();
149  _to_track[0] = iterator.get_be_float64();
150  _to_track[1] = iterator.get_be_float64();
151  _to_track[2] = iterator.get_be_float64();
152 
153  recompute_matrix();
154 
155  check_remaining_size(iterator);
156  return true;
157 }
158 
159 /**
160  * Fills up the current record on the FltRecordWriter with data for this
161  * record, but does not advance the writer. Returns true on success, false if
162  * there is some error.
163  */
164 bool FltTransformPut::
165 build_record(FltRecordWriter &writer) const {
166  if (!FltTransformRecord::build_record(writer)) {
167  return false;
168  }
169 
170  writer.set_opcode(FO_put);
171  Datagram &datagram = writer.update_datagram();
172 
173  datagram.pad_bytes(4); // Undocumented additional padding.
174 
175  datagram.add_be_float64(_from_origin[0]);
176  datagram.add_be_float64(_from_origin[1]);
177  datagram.add_be_float64(_from_origin[2]);
178  datagram.add_be_float64(_from_align[0]);
179  datagram.add_be_float64(_from_align[1]);
180  datagram.add_be_float64(_from_align[2]);
181  datagram.add_be_float64(_from_track[0]);
182  datagram.add_be_float64(_from_track[1]);
183  datagram.add_be_float64(_from_track[2]);
184  datagram.add_be_float64(_to_origin[0]);
185  datagram.add_be_float64(_to_origin[1]);
186  datagram.add_be_float64(_to_origin[2]);
187  datagram.add_be_float64(_to_align[0]);
188  datagram.add_be_float64(_to_align[1]);
189  datagram.add_be_float64(_to_align[2]);
190  datagram.add_be_float64(_to_track[0]);
191  datagram.add_be_float64(_to_track[1]);
192  datagram.add_be_float64(_to_track[2]);
193 
194  return true;
195 }
A class to retrieve the individual data elements previously stored in a Datagram.
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
PN_float64 get_be_float64()
Extracts a 64-bit big-endian floating-point number.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the datagram.
Definition: datagram.I:209
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:99
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
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.
void check_remaining_size(const DatagramIterator &di, const std::string &name=std::string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record.
Definition: fltRecord.cxx:254
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.
A base class for a number of types of ancillary records that follow beads and indicate some kind of a...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.