VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages

metricCollector.h

#pragma once
#include <iostream>
#include <fstream>
class DtCgf;
class DtScenario;
// Class which collects metrics on particular objects.
{
public:
virtual ~DtMetricCollector();
// A tick callback is installed with the main VRF system to get called each
// simulation tick and check the metrics.
static void tickCallback(void* usr);
virtual void tick();
// Callbacks to manage scenario start/end conditions.
// The metrics output file is restarted each time a scenario is started.
static void postLoadCallback(const DtScenario& scenario, void* usr);
virtual void processLoad(const DtScenario& scenario);
static void preCloseCallback(void* usr);
virtual void processClose();
static void postRollbackCallback(void* usr);
virtual void processRollback();
virtual void printLocationAndContacts(const DtLocalObject* entity);
virtual void printContact(const DtRwSensorContactInfo* contact);
protected:
std::ofstream myOutputFile;
};

metricCollector.cxx

#include "vrfcgf/cgf.h"
#include <iostream>
#include <fstream>
const double theMetricPeriod = 0.5;
: myCgf(cgf)
, myLastMetricTime(0)
{
// Register callbacks for notification of scenario load and close.
// Note: New scenario is also invokes the scenario load callback.
cgf->addPostLoadScenarioCallback(postLoadCallback, this);
cgf->addPreCloseScenarioCallback(preCloseCallback, this);
cgf->addPostScenarioRollbackCallback(postRollbackCallback, this);
}
{
}
void DtMetricCollector::postLoadCallback(const DtScenario& scenario, void* usr)
{
static_cast<DtMetricCollector*>(usr)->processLoad(scenario);
}
{
DtString filename("metrics.txt");
if (!myOutputFile.is_open()) // prevent reopening on rewind/rollback
{
myOutputFile.open(filename);
if (!myOutputFile.good())
{
DtWarn << "Could not open metrics file for output " << filename << std::endl;
}
}
}
{
static_cast<DtMetricCollector*>(usr)->processClose();
}
{
myOutputFile.close();
}
{
static_cast<DtMetricCollector*>(usr)->processRollback();
}
{
}
{
static_cast<DtMetricCollector*>(usr)->tick();
}
{
DtPROFILE("DtMetricCollector::tick");
// Check for the paused state, and don't do any metrics if paused.
if (myCgf->simulationServices()->dT() > 0)
{
// Only record metrics if enough time has passed since the last time the metrics were recorded.
if (myCgf->simulationServices()->simTime() >= myLastMetricTime + theMetricPeriod)
{
myOutputFile << "Time: " << myLastMetricTime << "\n";
// Only collect metrics on particular objects.
// In this case, just check for an entity named "R 1"
if (obj)
{
}
}
}
}
{
// Output basic information on the object such as name and location.
myOutputFile << entity->objectName() << "\n";
DtGeodeticCoord geodLoc;
geodLoc.setGeocentric(entity->worldPosition());
myOutputFile << "\tLocation: " << geodLoc.string() << "\n";
// If the entity has any sensor contacts, show those as well.
DtListItem* item = entity->internalState()->objectContactList().first();
if (item)
{
myOutputFile << "\tContacts: ";
for (; item; item = item->next())
{
const DtRwSensorContactInfo* contactInfo = static_cast<const DtRwSensorContactInfo*>(item->data());
printContact(contactInfo);
}
myOutputFile << "\n";
}
}
{
// For each contact, find the object name.
if (obj.isValid())
{
myOutputFile << obj->objectName() << " ";
}
}

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)