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