typeUtils.cpp

00001 /*
00002   型定義
00003   Saotufmi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "typeUtils.h"
00008 
00009 
00010 const VXV::Position& VXV::Position::operator+=(const Position& rhs) {
00011   x += rhs.x;
00012   y += rhs.y;
00013   zt += rhs.zt;
00014   return *this;
00015 }
00016 
00017 
00018 const VXV::Position& VXV::Position::operator-=(const Position& rhs) {
00019   x -= rhs.x;
00020   y -= rhs.y;
00021   zt -= rhs.zt;
00022   return *this;
00023 }
00024 
00025 
00026 int VXV::length(const Grid3D& position) {
00027   return static_cast<int>(sqrt(1.0 * ((position.x * position.x) +
00028                                       (position.y * position.y) +
00029                                       (position.z * position.z))));
00030 }
00031 
00032 
00033 const VXV::Grid& VXV::Grid::operator+=(const Grid& rhs) {
00034   x += rhs.x;
00035   y += rhs.y;
00036   return *this;
00037 }
00038 
00039 
00040 const VXV::Grid& VXV::Grid::operator-=(const Grid& rhs) {
00041   x -= rhs.x;
00042   y -= rhs.y;
00043   return *this;
00044 }
00045 
00046 
00047 const VXV::Grid3D& VXV::Grid3D::operator+=(const Grid3D& rhs) {
00048   x += rhs.x;
00049   y += rhs.y;
00050   z += rhs.z;
00051   return *this;
00052 }
00053 
00054 
00055 const VXV::Grid3D& VXV::Grid3D::operator-=(const Grid3D& rhs) {
00056   x -= rhs.x;
00057   y -= rhs.y;
00058   z -= rhs.z;
00059   return *this;
00060 }
00061 
00062 
00063 const int VXV::Direction::to_deg(void) const {
00064   return (int)rint(360.0 * to_rad() / (2.0 * M_PI));
00065 }
00066 
00067 
00068 const double VXV::Direction::to_rad(void) const {
00069   int times = static_cast<int>(inner_radian / (2.0 * M_PI));
00070   double radian = (inner_radian - (2.0 * M_PI * times));
00071   radian = (radian < 0) ? (2.0 * M_PI + radian) : radian;
00072 
00073   return (radian > M_PI) ? (radian - 2.0 * M_PI) : radian;
00074 }
00075 
00076 
00077 const VXV::Position3D& VXV::Position3D::operator+=(const Position3D& rhs) {
00078   x += rhs.x;
00079   y += rhs.y;
00080   z += rhs.z;
00081   xt += rhs.xt;
00082   yt += rhs.yt;
00083   zt += rhs.zt;
00084   return *this;
00085 }
00086 
00087 
00088 const VXV::Position3D& VXV::Position3D::operator-=(const Position3D& rhs) {
00089   x -= rhs.x;
00090   y -= rhs.y;
00091   z -= rhs.z;
00092   xt -= rhs.xt;
00093   yt -= rhs.yt;
00094   zt -= rhs.zt;
00095   return *this;
00096 }
00097 
00098 
00099 VXV::Matrix4D VXV::createConvertMatrix(const Position3D pos) {
00100   Matrix4D conv;
00101 
00102   conv.a11 = cos(pos.yt.to_rad()) * cos(pos.zt.to_rad());
00103   conv.a21 = -cos(pos.xt.to_rad())*sin(pos.zt.to_rad())
00104     + sin(pos.xt.to_rad())*sin(pos.yt.to_rad())*cos(pos.zt.to_rad());
00105   conv.a31 = cos(pos.xt.to_rad())*sin(pos.yt.to_rad())*cos(pos.zt.to_rad())
00106     + sin(pos.xt.to_rad())*sin(pos.zt.to_rad());
00107   conv.a41 = pos.x;
00108 
00109   conv.a12 = cos(pos.yt.to_rad())*sin(pos.zt.to_rad());
00110   conv.a22 = cos(pos.xt.to_rad())*cos(pos.zt.to_rad())
00111     + sin(pos.xt.to_rad())*sin(pos.yt.to_rad())*sin(pos.zt.to_rad());
00112   conv.a32 = cos(pos.xt.to_rad())*sin(pos.yt.to_rad())*sin(pos.zt.to_rad())
00113     - sin(pos.xt.to_rad())*cos(pos.zt.to_rad());
00114   conv.a42 = pos.y;
00115 
00116   conv.a13 = -sin(pos.yt.to_rad());
00117   conv.a23 = sin(pos.xt.to_rad())*cos(pos.yt.to_rad());
00118   conv.a33 = cos(pos.xt.to_rad())*cos(pos.yt.to_rad());
00119   conv.a43 = pos.z;
00120 
00121   return conv;
00122 }
00123 
00124 
00125 void VXV::convert(Position3D& converted, const Position3D& input,
00126                   const Matrix4D& matrix) {
00127   Position3D pos;
00128   pos.x = (int)(input.x * matrix.a11 +
00129                       input.y * matrix.a21 +
00130                       input.z * matrix.a31) + matrix.a41;
00131   pos.y = (int)(input.x * matrix.a12 +
00132                       input.y * matrix.a22 +
00133                       input.z * matrix.a32) + matrix.a42;
00134   pos.z = (int)(input.x * matrix.a13 +
00135                       input.y * matrix.a23 +
00136                       input.z * matrix.a33) + matrix.a43;
00137   converted.x = pos.x;
00138   converted.y = pos.y;
00139   converted.z = pos.z;
00140 }
00141 
00142 
00143 void VXV::convertWithAngle(Position3D& converted, const Position3D& input,
00144                            const Position3D& forMatrix) {
00145 
00146   Matrix4D convert = createConvertMatrix(forMatrix);
00147   VXV::convert(converted, input, convert);
00148   converted.xt += forMatrix.xt;
00149   converted.yt += forMatrix.yt;
00150   converted.zt += forMatrix.zt;
00151 }
00152 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1