Panda3D

fltTransformRotateAboutPoint.cxx

00001 // Filename: fltTransformRotateAboutPoint.cxx
00002 // Created by:  drose (30Aug00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "fltTransformRotateAboutPoint.h"
00016 #include "fltRecordReader.h"
00017 #include "fltRecordWriter.h"
00018 
00019 TypeHandle FltTransformRotateAboutPoint::_type_handle;
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: FltTransformRotateAboutPoint::Constructor
00023 //       Access: Public
00024 //  Description:
00025 ////////////////////////////////////////////////////////////////////
00026 FltTransformRotateAboutPoint::
00027 FltTransformRotateAboutPoint(FltHeader *header) : FltTransformRecord(header) {
00028   _center.set(0.0, 0.0, 0.0);
00029   _axis.set(1.0, 0.0, 0.0);
00030   _angle = 0.0;
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: FltTransformRotateAboutPoint::set
00035 //       Access: Public
00036 //  Description: Defines the rotation.  The angle is given in degrees,
00037 //               counterclockwise about the axis as seen from point a.
00038 ////////////////////////////////////////////////////////////////////
00039 void FltTransformRotateAboutPoint::
00040 set(const LPoint3d &center, const LVector3 &axis, PN_stdfloat angle) {
00041   _center = center;
00042   _axis = axis;
00043   _angle = angle;
00044 
00045   recompute_matrix();
00046 }
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: FltTransformRotateAboutPoint::get_center
00050 //       Access: Public
00051 //  Description:
00052 ////////////////////////////////////////////////////////////////////
00053 const LPoint3d &FltTransformRotateAboutPoint::
00054 get_center() const {
00055   return _center;
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: FltTransformRotateAboutPoint::get_axis
00060 //       Access: Public
00061 //  Description:
00062 ////////////////////////////////////////////////////////////////////
00063 const LVector3 &FltTransformRotateAboutPoint::
00064 get_axis() const {
00065   return _axis;
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: FltTransformRotateAboutPoint::get_angle
00070 //       Access: Public
00071 //  Description: Returns the angle of rotation, in degrees
00072 //               counterclockwise about the axis.
00073 ////////////////////////////////////////////////////////////////////
00074 PN_stdfloat FltTransformRotateAboutPoint::
00075 get_angle() const {
00076   return _angle;
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: FltTransformRotateAboutPoint::recompute_matrix
00081 //       Access: Private
00082 //  Description:
00083 ////////////////////////////////////////////////////////////////////
00084 void FltTransformRotateAboutPoint::
00085 recompute_matrix() {
00086   if (_axis == LVector3::zero()) {
00087     // Degenerate case.
00088     _matrix = LMatrix4d::ident_mat();
00089   } else {
00090     LVector3d axis = LCAST(double, _axis);
00091 
00092     _matrix =
00093       LMatrix4d::translate_mat(-_center) *
00094       LMatrix4d::rotate_mat(_angle, axis, CS_zup_right) *
00095       LMatrix4d::translate_mat(_center);
00096   }
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: FltTransformRotateAboutPoint::extract_record
00101 //       Access: Protected, Virtual
00102 //  Description: Fills in the information in this record based on the
00103 //               information given in the indicated datagram, whose
00104 //               opcode has already been read.  Returns true on
00105 //               success, false if the datagram is invalid.
00106 ////////////////////////////////////////////////////////////////////
00107 bool FltTransformRotateAboutPoint::
00108 extract_record(FltRecordReader &reader) {
00109   if (!FltTransformRecord::extract_record(reader)) {
00110     return false;
00111   }
00112 
00113   nassertr(reader.get_opcode() == FO_rotate_about_point, false);
00114   DatagramIterator &iterator = reader.get_iterator();
00115 
00116   iterator.skip_bytes(4);   // Undocumented additional padding.
00117 
00118   _center[0] = iterator.get_be_float64();
00119   _center[1] = iterator.get_be_float64();
00120   _center[2] = iterator.get_be_float64();
00121   _axis[0] = iterator.get_be_float32();
00122   _axis[1] = iterator.get_be_float32();
00123   _axis[2] = iterator.get_be_float32();
00124   _angle = iterator.get_be_float32();
00125 
00126   recompute_matrix();
00127 
00128   check_remaining_size(iterator);
00129   return true;
00130 }
00131 
00132 ////////////////////////////////////////////////////////////////////
00133 //     Function: FltTransformRotateAboutPoint::build_record
00134 //       Access: Protected, Virtual
00135 //  Description: Fills up the current record on the FltRecordWriter with
00136 //               data for this record, but does not advance the
00137 //               writer.  Returns true on success, false if there is
00138 //               some error.
00139 ////////////////////////////////////////////////////////////////////
00140 bool FltTransformRotateAboutPoint::
00141 build_record(FltRecordWriter &writer) const {
00142   if (!FltTransformRecord::build_record(writer)) {
00143     return false;
00144   }
00145 
00146   writer.set_opcode(FO_rotate_about_point);
00147   Datagram &datagram = writer.update_datagram();
00148 
00149   datagram.pad_bytes(4);   // Undocumented additional padding.
00150 
00151   datagram.add_be_float64(_center[0]);
00152   datagram.add_be_float64(_center[1]);
00153   datagram.add_be_float64(_center[2]);
00154   datagram.add_be_float32(_axis[0]);
00155   datagram.add_be_float32(_axis[1]);
00156   datagram.add_be_float32(_axis[2]);
00157   datagram.add_be_float32(_angle);
00158 
00159   return true;
00160 }
00161 
 All Classes Functions Variables Enumerations