/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include #include using namespace osg; Stats::Stats(const std::string& name): _name(name) { allocate(25); } Stats::Stats(const std::string& name, unsigned int numberOfFrames): _name(name) { allocate(numberOfFrames); } void Stats::allocate(unsigned int numberOfFrames) { OpenThreads::ScopedLock lock(_mutex); _baseFrameNumber = 0; _latestFrameNumber = 0; _attributeMapList.clear(); _attributeMapList.resize(numberOfFrames); } bool Stats::setAttribute(int frameNumber, const std::string& attributeName, double value) { if (frameNumber lock(_mutex); if (frameNumber>_latestFrameNumber) { // need to advance // first clear the entries up to and including the new frameNumber for(int i = _latestFrameNumber+1; i<= frameNumber; ++i) { int index = (i - _baseFrameNumber) % _attributeMapList.size(); _attributeMapList[index].clear(); } if ( (frameNumber-_baseFrameNumber) >= static_cast(_attributeMapList.size())) { _baseFrameNumber = (frameNumber/_attributeMapList.size())*_attributeMapList.size(); } _latestFrameNumber = frameNumber; } int index = getIndex(frameNumber); if (index<0) { osg::notify(osg::NOTICE)<<"Failed to assing valid index for Stats::setAttribute("<second; return true; } bool Stats::getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace) const { return getAveragedAttribute(getEarliestFrameNumber(), getLatestFrameNumber(), attributeName, value, averageInInverseSpace); } bool Stats::getAveragedAttribute(int startFrameNumber, int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace) const { if (endFrameNumber lock(_mutex); double total = 0.0; double numValidSamples = 0.0; for(int i = startFrameNumber; i<=endFrameNumber; ++i) { double v = 0.0; if (getAttributeNoMutex(i,attributeName,v)) { if (averageInInverseSpace) total += 1.0/v; else total += v; numValidSamples += 1.0; } } if (numValidSamples>0.0) { if (averageInInverseSpace) value = numValidSamples/total; else value = total/numValidSamples; return true; } else return false; } Stats::AttributeMap& Stats::getAttributeMapNoMutex(int frameNumber) { int index = getIndex(frameNumber); if (index<0) return _invalidAttributeMap; return _attributeMapList[index]; } const Stats::AttributeMap& Stats::getAttributeMapNoMutex(int frameNumber) const { int index = getIndex(frameNumber); if (index<0) return _invalidAttributeMap; return _attributeMapList[index]; } void Stats::report(std::ostream& out, const char* indent) const { OpenThreads::ScopedLock lock(_mutex); if (indent) out<first<<"\t"<second< lock(_mutex); if (indent) out<first<<"\t"<second<