Panda3D

fltTransformRotateAboutEdge.cxx

00001 // Filename: fltTransformRotateAboutEdge.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 "fltTransformRotateAboutEdge.h"
00016 #include "fltRecordReader.h"
00017 #include "fltRecordWriter.h"
00018 
00019 TypeHandle FltTransformRotateAboutEdge::_type_handle;
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: FltTransformRotateAboutEdge::Constructor
00023 //       Access: Public
00024 //  Description:
00025 ////////////////////////////////////////////////////////////////////
00026 FltTransformRotateAboutEdge::
00027 FltTransformRotateAboutEdge(FltHeader *header) : FltTransformRecord(header) {
00028   _point_a.set(0.0, 0.0, 0.0);
00029   _point_b.set(1.0, 0.0, 0.0);
00030   _angle = 0.0;
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: FltTransformRotateAboutEdge::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 FltTransformRotateAboutEdge::
00040 set(const LPoint3d &point_a, const LPoint3d &point_b, PN_stdfloat angle) {
00041   _point_a = point_a;
00042   _point_b = point_b;
00043   _angle = angle;
00044 
00045   recompute_matrix();
00046 }
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: FltTransformRotateAboutEdge::get_point_a
00050 //       Access: Public
00051 //  Description:
00052 ////////////////////////////////////////////////////////////////////
00053 const LPoint3d &FltTransformRotateAboutEdge::
00054 get_point_a() const {
00055   return _point_a;
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: FltTransformRotateAboutEdge::get_point_b
00060 //       Access: Public
00061 //  Description:
00062 ////////////////////////////////////////////////////////////////////
00063 const LPoint3d &FltTransformRotateAboutEdge::
00064 get_point_b() const {
00065   return _point_b;
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: FltTransformRotateAboutEdge::get_angle
00070 //       Access: Public
00071 //  Description: Returns the angle of rotation, in degrees
00072 //               counterclockwise about the axis as seen from point a.
00073 ////////////////////////////////////////////////////////////////////
00074 PN_stdfloat FltTransformRotateAboutEdge::
00075 get_angle() const {
00076   return _angle;
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: FltTransformRotateAboutEdge::recompute_matrix
00081 //       Access: Private
00082 //  Description:
00083 ////////////////////////////////////////////////////////////////////
00084 void FltTransformRotateAboutEdge::
00085 recompute_matrix() {
00086   if (_point_a == _point_b) {
00087     // Degenerate case.
00088     _matrix = LMatrix4d::ident_mat();
00089   } else {
00090     LVector3d axis = _point_b - _point_a;
00091     _matrix =
00092       LMatrix4d::translate_mat(-_point_a) *
00093       LMatrix4d::rotate_mat(_angle, normalize(axis), CS_zup_right) *
00094       LMatrix4d::translate_mat(_point_a);
00095   }
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: FltTransformRotateAboutEdge::extract_record
00100 //       Access: Protected, Virtual
00101 //  Description: Fills in the information in this record based on the
00102 //               information given in the indicated datagram, whose
00103 //               opcode has already been read.  Returns true on
00104 //               success, false if the datagram is invalid.
00105 ////////////////////////////////////////////////////////////////////
00106 bool FltTransformRotateAboutEdge::
00107 extract_record(FltRecordReader &reader) {
00108   if (!FltTransformRecord::extract_record(reader)) {
00109     return false;
00110   }
00111 
00112   nassertr(reader.get_opcode() == FO_rotate_about_edge, false);
00113   DatagramIterator &iterator = reader.get_iterator();
00114 
00115   iterator.skip_bytes(4);   // Undocumented additional padding.
00116 
00117   _point_a[0] = iterator.get_be_float64();
00118   _point_a[1] = iterator.get_be_float64();
00119   _point_a[2] = iterator.get_be_float64();
00120   _point_b[0] = iterator.get_be_float64();
00121   _point_b[1] = iterator.get_be_float64();
00122   _point_b[2] = iterator.get_be_float64();
00123   _angle = iterator.get_be_float32();
00124 
00125   iterator.skip_bytes(4);   // Undocumented additional padding.
00126 
00127   recompute_matrix();
00128 
00129   check_remaining_size(iterator);
00130   return true;
00131 }
00132 
00133 ////////////////////////////////////////////////////////////////////
00134 //     Function: FltTransformRotateAboutEdge::build_record
00135 //       Access: Protected, Virtual
00136 //  Description: Fills up the current record on the FltRecordWriter with
00137 //               data for this record, but does not advance the
00138 //               writer.  Returns true on success, false if there is
00139 //               some error.
00140 ////////////////////////////////////////////////////////////////////
00141 bool FltTransformRotateAboutEdge::
00142 build_record(FltRecordWriter &writer) const {
00143   if (!FltTransformRecord::build_record(writer)) {
00144     return false;
00145   }
00146 
00147   writer.set_opcode(FO_rotate_about_edge);
00148   Datagram &datagram = writer.update_datagram();
00149 
00150   datagram.pad_bytes(4);   // Undocumented additional padding.
00151 
00152   datagram.add_be_float64(_point_a[0]);
00153   datagram.add_be_float64(_point_a[1]);
00154   datagram.add_be_float64(_point_a[2]);
00155   datagram.add_be_float64(_point_b[0]);
00156   datagram.add_be_float64(_point_b[1]);
00157   datagram.add_be_float64(_point_b[2]);
00158   datagram.add_be_float32(_angle);
00159 
00160   datagram.pad_bytes(4);   // Undocumented additional padding.
00161 
00162   return true;
00163 }
00164 
 All Classes Functions Variables Enumerations