00001 #include "Smoother.h" 00002 #include <string> 00003 #include "../framedata/VectorFrameData.h" 00004 #include "../visualizer/SmootherGUI.h" 00005 #include "../util/Error.h" 00006 00007 Smoother::Smoother(size_t windowSize, AbstractModule *successor) : 00008 ProcessModule(successor), m_windowSize(windowSize) 00009 { 00010 setGUI(new SmootherGUI(this)); 00011 } 00012 00013 Smoother::Smoother(ConfigSection *config, AbstractModule *successor) : 00014 ProcessModule(successor) 00015 { 00016 if (config == NULL) { 00017 throw Error("Smoother::Smoother(): Pointer to ConfigSection is NULL"); 00018 } 00019 try { 00020 m_windowSize = config->get<int>("window size"); 00021 } catch (Error e) { // TODO: ErrorNoConfigValue or sth 00022 config->setItem("window size", "10"); 00023 m_windowSize = 10; 00024 } 00025 00026 setGUI(new SmootherGUI(this)); 00027 } 00028 00029 void Smoother::processFrameData(IFrameData *data) 00030 { 00031 if (isSignal(data)) { 00032 passSignal(data); 00033 return; 00034 } 00035 00036 assertFramedataType(data, "Vector3D", "Smoother::processFrameData"); 00037 00038 Vector3d tmp = static_cast<VectorFrameData*>(data)->getData(); 00039 00040 // append datum to window 00041 m_frameHistory.push_back(tmp); 00042 while (m_frameHistory.size() > m_windowSize) { 00043 m_frameHistory.pop_front(); 00044 } 00045 00046 // calculate average 00047 VectorList::reverse_iterator it = m_frameHistory.rbegin(); 00048 ++it; // skip last element as it is tmp 00049 for (; it != m_frameHistory.rend(); ++it) { 00050 tmp += *it; 00051 } 00052 tmp /= static_cast<double>(m_frameHistory.size()); 00053 00054 // gui output 00055 static_cast<SmootherGUI*>(getGUI())->addData(tmp); 00056 00057 // send to next module 00058 static_cast<VectorFrameData*>(data)->setData(tmp); 00059 ProcessModule::processFrameData(data); 00060 } 00061