ticksPosition.cpp

00001 /*
00002   指定時刻の位置を補間して返す
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "ticksPosition.h"
00008 #include <algorithm>
00009 
00010 using namespace VXV;
00011 
00012 
00013 TicksPosition::TicksPosition(void) {
00014 }
00015 
00016 
00017 TicksPosition::~TicksPosition(void) {
00018 }
00019 
00020 
00021 double TicksPosition::calcAngle(const VXV::Direction& a,
00022                                 const VXV::Direction& b, double k) const {
00023   double a_rad = a.to_rad();
00024   double b_rad = b.to_rad();
00025   double diff = b_rad - a_rad;
00026   if (diff > M_PI) {
00027     diff -= 2.0 * M_PI;
00028   } else if (diff < -M_PI) {
00029     diff += 2.0 * M_PI;
00030   }
00031   return a_rad + diff * k;
00032 }
00033 
00034 
00035 VXV::Position3D TicksPosition::getPosition(unsigned long ticks) const {
00036   if (pos_list.empty()) {
00037     return Position3D();
00038   }
00039 
00040   // 範囲外の時刻については、一番近いときの位置を返す
00041   std::list<ticksPos_t>::const_iterator pre = pos_list.begin();
00042   if (pre->ticks > ticks) {
00043     return pos_list.front().pos;
00044   }
00045   for (std::list<ticksPos_t>::const_iterator it = pos_list.begin();
00046        it != pos_list.end(); ++it) {
00047     if (it->ticks == ticks) {
00048       // 指定時刻印と一致した位置を返す
00049       return it->pos;
00050     }
00051     if (it->ticks > ticks) {
00052       // 補間した位置を返す
00053       double k = (ticks - pre->ticks) / (0.0 + it->ticks - pre->ticks);
00054       int x = static_cast<int>(rint(pre->pos.x + (it->pos.x - pre->pos.x) *k));
00055       int y = static_cast<int>(rint(pre->pos.y + (it->pos.y - pre->pos.y) *k));
00056       int z = static_cast<int>(rint(pre->pos.z + (it->pos.z - pre->pos.z) *k));
00057       return Position3D(x, y, z,
00058                         Direction::rad(calcAngle(pre->pos.xt, it->pos.xt, k)),
00059                         Direction::rad(calcAngle(pre->pos.yt, it->pos.yt, k)),
00060                         Direction::rad(calcAngle(pre->pos.zt, it->pos.zt, k)));
00061     }
00062     pre = it;
00063   }
00064   return pos_list.back().pos;
00065 }
00066 
00067 
00068 void TicksPosition::add(const VXV::Position3D& position, unsigned long ticks) {
00069 
00070   std::list<ticksPos_t> add_list(1);
00071   add_list.front().pos = position;
00072   add_list.front().ticks = ticks;
00073 
00074   pos_list.merge(add_list, ticks_less());
00075 }
00076 
00077 
00078 void TicksPosition::clear(void) {
00079   pos_list.clear();
00080 }
00081 
00082 
00083 void TicksPosition::del_olderThan(unsigned long ticks) {
00084 
00085   // ticks 以降を削除
00086   ticksPos_t cmp;
00087   cmp.ticks = ticks;
00088   std::list<ticksPos_t>::iterator
00089     it = upper_bound(pos_list.begin(), pos_list.end(), cmp, ticks_less());
00090   if (it != pos_list.end()) {
00091     pos_list.erase(pos_list.begin(), it);
00092   }
00093 }
00094 

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