Panda3D
fltTransformRotateAboutEdge.cxx
1 // Filename: fltTransformRotateAboutEdge.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 "fltTransformRotateAboutEdge.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 
19 TypeHandle FltTransformRotateAboutEdge::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: FltTransformRotateAboutEdge::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 FltTransformRotateAboutEdge::
27 FltTransformRotateAboutEdge(FltHeader *header) : FltTransformRecord(header) {
28  _point_a.set(0.0, 0.0, 0.0);
29  _point_b.set(1.0, 0.0, 0.0);
30  _angle = 0.0;
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: FltTransformRotateAboutEdge::set
35 // Access: Public
36 // Description: Defines the rotation. The angle is given in degrees,
37 // counterclockwise about the axis as seen from point a.
38 ////////////////////////////////////////////////////////////////////
40 set(const LPoint3d &point_a, const LPoint3d &point_b, PN_stdfloat angle) {
41  _point_a = point_a;
42  _point_b = point_b;
43  _angle = angle;
44 
45  recompute_matrix();
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: FltTransformRotateAboutEdge::get_point_a
50 // Access: Public
51 // Description:
52 ////////////////////////////////////////////////////////////////////
53 const LPoint3d &FltTransformRotateAboutEdge::
54 get_point_a() const {
55  return _point_a;
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: FltTransformRotateAboutEdge::get_point_b
60 // Access: Public
61 // Description:
62 ////////////////////////////////////////////////////////////////////
63 const LPoint3d &FltTransformRotateAboutEdge::
64 get_point_b() const {
65  return _point_b;
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: FltTransformRotateAboutEdge::get_angle
70 // Access: Public
71 // Description: Returns the angle of rotation, in degrees
72 // counterclockwise about the axis as seen from point a.
73 ////////////////////////////////////////////////////////////////////
75 get_angle() const {
76  return _angle;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: FltTransformRotateAboutEdge::recompute_matrix
81 // Access: Private
82 // Description:
83 ////////////////////////////////////////////////////////////////////
84 void FltTransformRotateAboutEdge::
85 recompute_matrix() {
86  if (_point_a == _point_b) {
87  // Degenerate case.
88  _matrix = LMatrix4d::ident_mat();
89  } else {
90  LVector3d axis = _point_b - _point_a;
91  _matrix =
92  LMatrix4d::translate_mat(-_point_a) *
93  LMatrix4d::rotate_mat(_angle, normalize(axis), CS_zup_right) *
94  LMatrix4d::translate_mat(_point_a);
95  }
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: FltTransformRotateAboutEdge::extract_record
100 // Access: Protected, Virtual
101 // Description: Fills in the information in this record based on the
102 // information given in the indicated datagram, whose
103 // opcode has already been read. Returns true on
104 // success, false if the datagram is invalid.
105 ////////////////////////////////////////////////////////////////////
106 bool FltTransformRotateAboutEdge::
107 extract_record(FltRecordReader &reader) {
108  if (!FltTransformRecord::extract_record(reader)) {
109  return false;
110  }
111 
112  nassertr(reader.get_opcode() == FO_rotate_about_edge, false);
113  DatagramIterator &iterator = reader.get_iterator();
114 
115  iterator.skip_bytes(4); // Undocumented additional padding.
116 
117  _point_a[0] = iterator.get_be_float64();
118  _point_a[1] = iterator.get_be_float64();
119  _point_a[2] = iterator.get_be_float64();
120  _point_b[0] = iterator.get_be_float64();
121  _point_b[1] = iterator.get_be_float64();
122  _point_b[2] = iterator.get_be_float64();
123  _angle = iterator.get_be_float32();
124 
125  iterator.skip_bytes(4); // Undocumented additional padding.
126 
127  recompute_matrix();
128 
129  check_remaining_size(iterator);
130  return true;
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: FltTransformRotateAboutEdge::build_record
135 // Access: Protected, Virtual
136 // Description: Fills up the current record on the FltRecordWriter with
137 // data for this record, but does not advance the
138 // writer. Returns true on success, false if there is
139 // some error.
140 ////////////////////////////////////////////////////////////////////
141 bool FltTransformRotateAboutEdge::
142 build_record(FltRecordWriter &writer) const {
143  if (!FltTransformRecord::build_record(writer)) {
144  return false;
145  }
146 
147  writer.set_opcode(FO_rotate_about_edge);
148  Datagram &datagram = writer.update_datagram();
149 
150  datagram.pad_bytes(4); // Undocumented additional padding.
151 
152  datagram.add_be_float64(_point_a[0]);
153  datagram.add_be_float64(_point_a[1]);
154  datagram.add_be_float64(_point_a[2]);
155  datagram.add_be_float64(_point_b[0]);
156  datagram.add_be_float64(_point_b[1]);
157  datagram.add_be_float64(_point_b[2]);
158  datagram.add_be_float32(_angle);
159 
160  datagram.pad_bytes(4); // Undocumented additional padding.
161 
162  return true;
163 }
164 
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 rotate_mat(double angle, const LVecBase3d &axis, CoordinateSystem cs=CS_default)
Returns a matrix that rotates by the given angle in degrees counterclockwise about the indicated vect...
Definition: lmatrix.h:6690
void set(const LPoint3d &point_a, const LPoint3d &point_b, PN_stdfloat angle)
Defines the rotation.
PN_stdfloat get_angle() const
Returns the angle of rotation, in degrees counterclockwise about the axis as seen from point a...
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
static const LMatrix4d & ident_mat()
Returns an identity matrix.
Definition: lmatrix.h:5168
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:760
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.
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