This file is indexed.

/usr/share/openscenegraph/examples/osguserstats/osguserstats.cpp is in openscenegraph-examples 3.2.1-7ubuntu4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
 *
 * This application is open source and may be redistributed and/or modified
 * freely and without restriction, both in commercial and non commercial applications,
 * as long as this copyright notice is maintained.
 *
 * This application 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.
*/

#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osgGA/TrackballManipulator>
#include <osgGA/StateSetManipulator>

#include <iostream>


// The idea of user stats is that you record times or values in the viewer's 
// stats, and you also tell the stats handler to watch those values each 
// frame. The stats handler can display the stats in three ways:
//  - A numeric time beside the stat name
//    Requires that an elapsed time be recorded in the viewer's stats for the 
//    "timeTakenName".
//  - A bar in the top bar graph
//    Requires that two times (relative to the viewer's start tick) be 
//    recorded in the viewer's stats for the "beginTimeName" and "endTimeName".
//  - A line in the bottom graph
//    Requires that an elapsed time be recorded in the viewer's stats for the 
//    "timeTakenName".


// Anything you want to time has to use a consistent name in both the stats 
// handler and the viewer stats, so it's a good idea to use constants to make
// sure the names are the same everywhere.
const std::string frameNumberName    = "Custom Frame Number";
const std::string frameTimeName      = "Custom Frame Time";
const std::string customTimeName     = "Custom";
const std::string operation1TimeName = "Operation1";
const std::string operation2TimeName = "Operation2";
const std::string otherThreadTimeName = "Thread";


void initUserStats(osgViewer::StatsHandler* statsHandler)
{
    // This line displays the frame number. It's not averaged, just displayed as is.
    statsHandler->addUserStatsLine("Frame", osg::Vec4(0.7,0.7,0.7,1), osg::Vec4(0.7,0.7,0.7,0.5), 
                                   frameNumberName, 1.0, false, false, "", "", 0.0);

    // This line displays the frame time (from beginning of event to end of draw). No bars.
    statsHandler->addUserStatsLine("MS/frame", osg::Vec4(1,0,1,1), osg::Vec4(1,0,1,0.5), 
                                   frameTimeName, 1000.0, true, false, "", "", 0.02);

    // This line displays the sum of update and main camera cull times.
    statsHandler->addUserStatsLine("Custom", osg::Vec4(1,1,1,1), osg::Vec4(1,1,1,0.5), 
                                   customTimeName + " time taken", 1000.0, true, false, customTimeName + " begin", customTimeName + " end", 0.016);

    // This line displays the time taken by a function below ( doSomethingAndTimeIt() )
    statsHandler->addUserStatsLine("Sleep1", osg::Vec4(1,0,0,1), osg::Vec4(1,0,0,0.5), 
                                   operation1TimeName + " time taken", 1000.0, true, false, operation1TimeName + " begin", operation1TimeName + " end", 0.016);

    // This line displays the time taken by a function below ( doSomethingAndTimeIt() )
    statsHandler->addUserStatsLine("Sleep2", osg::Vec4(1,0.5,0.5,1), osg::Vec4(1,0.5,0.5,0.5), 
                                   operation2TimeName + " time taken", 1000.0, true, false, operation2TimeName + " begin", operation2TimeName + " end", 0.016);

    // This line displays the time taken by a function below ( doSomethingAndTimeIt() )
    statsHandler->addUserStatsLine("Thread", osg::Vec4(0,0.5,0,1), osg::Vec4(0,0.5,0,0.5), 
                                   otherThreadTimeName + " time taken", 1000.0, true, false, otherThreadTimeName + " begin", otherThreadTimeName + " end", 0.016);
}


void updateUserStats(osgViewer::Viewer& viewer)
{
    // Test the custom stats line by just adding up the update and cull 
    // times for the viewer main camera for the previous frame.
    if (viewer.getViewerStats()->collectStats("update") && viewer.getCamera()->getStats()->collectStats("rendering"))
    {
        // First get the frame number. The code below assumes that 
        // updateUserStats() is called after advance(), so the frame number
        // that will be returned is for the frame that has just started and is
        // not rendered yet. The previous frame is framenumber-1, but we can't
        // use that frame's timings because it's probably not finished 
        // rendering yet (in multithreaded viewer modes). So we'll use the
        // timings for framenumber-2 for this demo.
        unsigned int framenumber = viewer.getFrameStamp()->getFrameNumber();

        // Get the update time and the viewer main camera's cull time. We use 
        // getAveragedAttribute() in order to get the actual time elapsed as 
        // calculated by the stats.
        double update = 0.0, cull = 0.0;
        viewer.getViewerStats()->getAveragedAttribute("Update traversal time taken", update);
        viewer.getCamera()->getStats()->getAveragedAttribute("Cull traversal time taken", cull);

        // Get various begin and end times, note these are not elapsed times 
        // in a frame but rather the simulation time at those moments.
        double eventBegin = 0.0, updateBegin = 0.0, cullEnd = 0.0, drawEnd = 0.0;
        viewer.getViewerStats()->getAttribute(framenumber-2, "Event traversal begin time", eventBegin);
        viewer.getViewerStats()->getAttribute(framenumber-2, "Update traversal begin time", updateBegin);
        viewer.getCamera()->getStats()->getAttribute(framenumber-2, "Cull traversal end time", cullEnd);
        viewer.getCamera()->getStats()->getAttribute(framenumber-2, "Draw traversal end time", drawEnd);

        // This line displays the frame number. It's not averaged, just displayed as is.
        viewer.getViewerStats()->setAttribute(framenumber, frameNumberName, framenumber);

        // This line displays the frame time (from beginning of event to end of draw). No bars.
        viewer.getViewerStats()->setAttribute(framenumber-1, frameTimeName, drawEnd - eventBegin);

        // This line displays the sum of update and main camera cull times.
        viewer.getViewerStats()->setAttribute(framenumber-1, customTimeName + " time taken", update+cull);
        // Since we give begin and end times that correspond to the begin of 
        // the update phase and the end of the cull phase, the bar in the 
        // graph will not correspond to the summed times above if something 
        // happened between update and cull (as in this demo). Also, we need 
        // to translate the updateBegin and cullEnd times by one frame since 
        // we're taking the times for framenumber-2 but using them to display
        // in the framenumber-1 graph.
        viewer.getViewerStats()->setAttribute(framenumber-1, customTimeName + " begin", updateBegin + (1.0/60.0));
        viewer.getViewerStats()->setAttribute(framenumber-1, customTimeName + " end", cullEnd + (1.0/60.0));
    }
}


/// Utility function you call before something you want to time, so that the 
/// recorded times will all be consistent using the viewer's time.
void startTiming(osgViewer::Viewer& viewer, const std::string& name)
{
    osg::Timer_t tick = osg::Timer::instance()->tick();
    double currentTime = osg::Timer::instance()->delta_s(viewer.getStartTick(), tick);
    unsigned int framenumber = viewer.getFrameStamp()->getFrameNumber();

    viewer.getViewerStats()->setAttribute(framenumber, name + " begin", currentTime);
}

/// Utility function you call after something you want to time, so that the 
/// recorded times will all be consistent using the viewer's time.
void endTiming(osgViewer::Viewer& viewer, const std::string& name)
{
    osg::Timer_t tick = osg::Timer::instance()->tick();
    double currentTime = osg::Timer::instance()->delta_s(viewer.getStartTick(), tick);
    unsigned int framenumber = viewer.getFrameStamp()->getFrameNumber();

    viewer.getViewerStats()->setAttribute(framenumber, name + " end", currentTime);

    double begin = 0.0;
    double elapsed = 0.0;
    if (viewer.getViewerStats()->getAttribute(framenumber, name + " begin", begin))
    {
        elapsed = currentTime - begin;
    }

    viewer.getViewerStats()->setAttribute(framenumber, name + " time taken", elapsed);
}


/// Will just sleep for the given number of milliseconds in the same thread 
/// as the caller, recording the time taken in the viewer's stats.
void doSomethingAndTimeIt(osgViewer::Viewer& viewer, const std::string& name, double milliseconds)
{
    startTiming(viewer, name);

    //------------------------------------------------------------
    // Your processing goes here.

    // Do nothing for the specified number of  milliseconds, just so we can 
    // see it in the stats.
    osg::Timer_t startTick = osg::Timer::instance()->tick();
    while (osg::Timer::instance()->delta_m(startTick, osg::Timer::instance()->tick()) < milliseconds)
    {
        OpenThreads::Thread::YieldCurrentThread();
    }
    //------------------------------------------------------------

    endTiming(viewer, name);
}


/// Thread that will sleep for the given number of milliseconds, recording 
/// the time taken in the viewer's stats, whenever its process() method is 
/// called.
class UselessThread : public OpenThreads::Thread
{
public:
    UselessThread(osgViewer::Viewer& viewer, double timeToRun)
        : _viewer(viewer)
        , _timeToRun(timeToRun)
        , _done(false)
        , _process(false)
    {
    }

    void run()
    {
        while (!_done)
        {
            if (_process)
            {
                startTiming(_viewer, otherThreadTimeName);

                //------------------------------------------------------------
                // Your processing goes here.

                // Do nothing for the specified number of  milliseconds, just so we can 
                // see it in the stats.
                osg::Timer_t startTick = osg::Timer::instance()->tick();
                while (osg::Timer::instance()->delta_m(startTick, osg::Timer::instance()->tick()) < _timeToRun)
                {
                    OpenThreads::Thread::YieldCurrentThread();
                }
                //------------------------------------------------------------

                endTiming(_viewer, otherThreadTimeName);

                _process = false;
            }
            else
            {
                OpenThreads::Thread::microSleep(50);
            }
        }
    }

    int cancel()
    {
        _done = true;
        return OpenThreads::Thread::cancel();
    }

    void process()
    {
        _process = true;
    }

protected:
    osgViewer::Viewer& _viewer;
    double _timeToRun;
    bool _done;
    bool _process;
};


int main(int argc, char** argv)
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);

    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
    arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","Provide authentication information for http file access.");

    osgViewer::Viewer viewer(arguments);

    unsigned int helpType = 0;
    if ((helpType = arguments.readHelpType()))
    {
        arguments.getApplicationUsage()->write(std::cout, helpType);
        return 1;
    }

    // report any errors if they have occurred when parsing the program arguments.
    if (arguments.errors())
    {
        arguments.writeErrorMessages(std::cout);
        return 1;
    }

    if (arguments.argc()<=1)
    {
        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
        return 1;
    }

    std::string url, username, password;
    while(arguments.read("--login",url, username, password))
    {
        if (!osgDB::Registry::instance()->getAuthenticationMap())
        {
            osgDB::Registry::instance()->setAuthenticationMap(new osgDB::AuthenticationMap);
            osgDB::Registry::instance()->getAuthenticationMap()->addAuthenticationDetails(
                url,
                new osgDB::AuthenticationDetails(username, password)
            );
        }
    }

    viewer.setCameraManipulator(new osgGA::TrackballManipulator);

    // add the state manipulator
    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );

    // add the thread model handler
    viewer.addEventHandler(new osgViewer::ThreadingHandler);

    // add the window size toggle handler
    viewer.addEventHandler(new osgViewer::WindowSizeHandler);

    // add the stats handler
    osgViewer::StatsHandler* statsHandler = new osgViewer::StatsHandler;
    viewer.addEventHandler(statsHandler);

    initUserStats(statsHandler);

    // add the help handler
    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));

    // load the data
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
    if (!loadedModel)
    {
        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
        return 1;
    }

    // any option left unread are converted into errors to write out later.
    arguments.reportRemainingOptionsAsUnrecognized();

    // report any errors if they have occurred when parsing the program arguments.
    if (arguments.errors())
    {
        arguments.writeErrorMessages(std::cout);
        return 1;
    }


    // optimize the scene graph, remove redundant nodes and state etc.
    osgUtil::Optimizer optimizer;
    optimizer.optimize(loadedModel.get());

    viewer.setSceneData( loadedModel.get() );

    viewer.realize();

    // Start up a thread that will just run for a fixed time each frame, in 
    // parallel to the frame loop.
    UselessThread thread(viewer, 6.0);
    thread.start();

    while (!viewer.done())
    {
        viewer.advance();

        updateUserStats(viewer);

        // Eat up some time on the viewer thread before the event phase.
        doSomethingAndTimeIt(viewer, operation1TimeName, 2.0);

        // Start taking some time on the other thread.
        thread.process();

        viewer.eventTraversal();
        viewer.updateTraversal();

        // Eat up some time on the viewer thread between the update and cull 
        // phases.
        doSomethingAndTimeIt(viewer, operation2TimeName, 3.0);

        viewer.renderingTraversals();
    }

    thread.cancel();
    while (thread.isRunning())
    {
        OpenThreads::Thread::YieldCurrentThread();
    }

}