root/OpenSceneGraph/trunk/src/osg/Stats.cpp @ 13041

Revision 13041, 5.5 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osg/Stats>
15#include <osg/Notify>
16
17using namespace osg;
18
19Stats::Stats(const std::string& name):
20    _name(name)
21{
22    allocate(25);
23}
24
25
26Stats::Stats(const std::string& name, unsigned int numberOfFrames):
27    _name(name)
28{
29    allocate(numberOfFrames);
30}
31
32void Stats::allocate(unsigned int numberOfFrames)
33{
34    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
35
36    _baseFrameNumber = 0;
37    _latestFrameNumber  = 0;
38    _attributeMapList.clear();
39    _attributeMapList.resize(numberOfFrames);
40}
41
42
43bool Stats::setAttribute(unsigned int frameNumber, const std::string& attributeName, double value)
44{
45    if (frameNumber<getEarliestFrameNumber()) return false;
46
47    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
48
49    if (frameNumber>_latestFrameNumber)
50    {
51        // need to advance
52
53        // first clear the entries up to and including the new frameNumber
54        for(unsigned int i = _latestFrameNumber+1; i<= frameNumber; ++i)
55        {
56            unsigned int index = (i - _baseFrameNumber) % _attributeMapList.size();
57            _attributeMapList[index].clear();
58        }
59
60        if ( (frameNumber-_baseFrameNumber) >= static_cast<unsigned int>(_attributeMapList.size()))
61        {
62            _baseFrameNumber = (frameNumber/_attributeMapList.size())*_attributeMapList.size();
63        }
64
65        _latestFrameNumber = frameNumber;
66
67    }
68
69    int index = getIndex(frameNumber);
70    if (index<0)
71    {
72        OSG_NOTICE<<"Failed to assing valid index for Stats::setAttribute("<<frameNumber<<","<<attributeName<<","<<value<<")"<<std::endl;
73        return false;
74    }
75
76    AttributeMap& attributeMap = _attributeMapList[index];
77    attributeMap[attributeName] = value;
78
79    return true;
80}
81
82bool Stats::getAttributeNoMutex(unsigned int frameNumber, const std::string& attributeName, double& value) const
83{
84    int index = getIndex(frameNumber);
85    if (index<0) return false;
86
87    const AttributeMap& attributeMap = _attributeMapList[index];
88    AttributeMap::const_iterator itr = attributeMap.find(attributeName);
89    if (itr == attributeMap.end()) return false;
90
91    value = itr->second;
92    return true;
93}
94
95bool Stats::getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace) const
96{
97    return getAveragedAttribute(getEarliestFrameNumber(), getLatestFrameNumber(), attributeName, value, averageInInverseSpace);
98}
99
100bool Stats::getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace) const
101{
102    if (endFrameNumber<startFrameNumber)
103    {
104        std::swap(endFrameNumber, startFrameNumber);
105    }
106
107    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
108
109    double total = 0.0;
110    double numValidSamples = 0.0;
111    for(unsigned int i = startFrameNumber; i<=endFrameNumber; ++i)
112    {
113        double v = 0.0;
114        if (getAttributeNoMutex(i,attributeName,v))
115        {
116            if (averageInInverseSpace) total += 1.0/v;
117            else total += v;
118            numValidSamples += 1.0;
119        }
120    }
121    if (numValidSamples>0.0)
122    {
123        if (averageInInverseSpace) value = numValidSamples/total;
124        else value = total/numValidSamples;
125        return true;
126    }
127    else return false;
128}
129
130Stats::AttributeMap& Stats::getAttributeMapNoMutex(unsigned int frameNumber)
131{
132    int index = getIndex(frameNumber);
133    if (index<0) return _invalidAttributeMap;
134
135    return _attributeMapList[index];
136}
137
138const Stats::AttributeMap& Stats::getAttributeMapNoMutex(unsigned int frameNumber) const
139{
140    int index = getIndex(frameNumber);
141    if (index<0) return _invalidAttributeMap;
142
143    return _attributeMapList[index];
144}
145
146void Stats::report(std::ostream& out, const char* indent) const
147{
148    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
149
150    if (indent) out<<indent;
151    out<<"Stats "<<_name<<std::endl;
152    for(unsigned int i = getEarliestFrameNumber(); i<= getLatestFrameNumber(); ++i)
153    {
154        out<<" FrameNumber "<<i<<std::endl;
155        const osg::Stats::AttributeMap& attributes = getAttributeMapNoMutex(i);
156        for(osg::Stats::AttributeMap::const_iterator itr = attributes.begin();
157            itr != attributes.end();
158            ++itr)
159        {
160            if (indent) out<<indent;
161            out<<"    "<<itr->first<<"\t"<<itr->second<<std::endl;
162        }
163
164    }
165}
166
167void Stats::report(std::ostream& out, unsigned int frameNumber, const char* indent) const
168{
169    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
170
171    if (indent) out<<indent;
172    out<<"Stats "<<_name<<" FrameNumber "<<frameNumber<<std::endl;
173    const osg::Stats::AttributeMap& attributes = getAttributeMapNoMutex(frameNumber);
174    for(osg::Stats::AttributeMap::const_iterator itr = attributes.begin();
175        itr != attributes.end();
176        ++itr)
177    {
178        if (indent) out<<indent;
179        out<<"    "<<itr->first<<"\t"<<itr->second<<std::endl;
180    }
181}
Note: See TracBrowser for help on using the browser.