Commit 769eae7b authored by Sreekanth Arikatla's avatar Sreekanth Arikatla
Browse files

ENH: Changes the time recording

Changes the time recording to use core/Timer.h
Adds a routine to Timer class to get the present time.
Changes the data recording to have markers for easy parsing
parent 3d358bc3
......@@ -28,23 +28,43 @@ namespace core {
Timer::Timer()
{
this->start();
this->timeBegan = false;
}
//---------------------------------------------------------------------------
void Timer::start()
{
this->begin = ClockType::now();
this->timeBegan = true;
}
//---------------------------------------------------------------------------
char* Timer::getTimeDateTimeFormat()
{
std::chrono::time_point<std::chrono::system_clock> now
= std::chrono::system_clock::now();
std::time_t nowTime = std::chrono::system_clock::to_time_t(now);
return std::ctime(&nowTime);
}
//---------------------------------------------------------------------------
long double Timer::elapsed()
{
long double deltaSec;
TimePointType now = ClockType::now();
DurationType delta = now - this->begin;
deltaSec = ((static_cast<long double>(delta.count()) * PeriodType::num) / PeriodType::den);
return deltaSec;
if (this->timeBegan)
{
long double deltaSec;
TimePointType now = ClockType::now();
DurationType delta = now - this->begin;
deltaSec = ((static_cast<long double>(delta.count()) * PeriodType::num) / PeriodType::den);
return deltaSec;
}
else
{
std::cout << "Error: Time recording hasn't began! Timer::start() hasn't been called before\n";
return 0;
}
}
}
......@@ -29,7 +29,9 @@
#endif //_WIN32
// STL includes
#include <iostream>
#include <chrono>
#include <ctime>
#ifdef _WIN32
#pragma warning(pop)
......@@ -56,6 +58,11 @@ public:
///
void start();
///
/// \brief Return the present time in [DayOfWeek month day hh:mm:ss yy] format
///
char* getTimeDateTimeFormat();
///
/// \brief Gets the time passed between start() and this call
///
......@@ -64,7 +71,9 @@ public:
long double elapsed();
private:
TimePointType begin; //!< Begining of time point
TimePointType begin; //!< Beginning of time point
bool timeBegan; //!< Tells if the beginning is recorded
};
} //core
......
......@@ -11,7 +11,6 @@
#include <memory>
#include <string>
#include <cmath>
#include <time.h>
#ifdef _WIN32
#pragma warning(pop)
......@@ -30,6 +29,7 @@
#include "IO/InitIO.h"
#include "VTKRendering/InitVTKRendering.h"
#include "VTKRendering/VTKViewer.h"
#include "core/Timer.h"
#ifdef _WIN32
#pragma warning(push, 0)
......@@ -37,13 +37,15 @@
// VTK includes
#include <vtkNew.h>
// Screenshot
// Screen shot
#include <vtkObjectFactory.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
// 2d overlay
#include <vtkPNGReader.h>
#include <vtkImageResize.h>
......@@ -72,18 +74,22 @@ const int numTargets = 6;
const double rTarget = 2.0;// radius of the target
const double rOverlay = 1.0;// radius of the overlay
const double maxPosDeviation = 1.0;// set this to right amount
const double maxPosDeviation = 10.0;//> set this to right amount
core::Vec3d targetNormals[numTargets];// normal to the target surface
core::Vec3d targetPositions[numTargets];// position of the center point of target
core::Vec3d targetNormals[numTargets];//> normal to the target surface
core::Vec3d targetPositions[numTargets];//> position of the center point of target
core::Vec3d targetCamPositions[numTargets];//> position of the center point of target
const std::string metricsFileName("metricsFile.txt");
// camera properties
const double viewAngle = 80.0;
const double camAngleOfView = 80.0; // angle of view of the camera in degrees
// compute the cumulative error metric
void computeErrorMetric(core::Vec3d& camPos, core::Vec3d& camUpVec, core::Vec3d& camFocusPt)
void computeErrorMetric(
core::Vec3d& camPos,
core::Vec3d& camUpVec,
core::Vec3d& camFocusPt)
{
int targetInFocus = -1;
double posDeviation;
......@@ -96,7 +102,8 @@ void computeErrorMetric(core::Vec3d& camPos, core::Vec3d& camUpVec, core::Vec3d&
camFocus = (camFocusPt - camPos);
camFocus.normalize();
// find the closest target and compute the angular deviation of the focus
// find the closest target in terms of angular deviation
// and compute the angular deviation of the focus
for (auto i = 0; i < numTargets; ++i)
{
targetDir = (targetPositions[i] - camPos);
......@@ -104,6 +111,8 @@ void computeErrorMetric(core::Vec3d& camPos, core::Vec3d& camUpVec, core::Vec3d&
angularDeviation = std::acos(targetDir.dot(camFocus));
//std::cout << i << "->" << angularDeviation << std::endl;
if (lowestDeviation > angularDeviation)
{
lowestDeviation = angularDeviation;
......@@ -113,7 +122,7 @@ void computeErrorMetric(core::Vec3d& camPos, core::Vec3d& camUpVec, core::Vec3d&
// check if the lowest deviation even passes the cut-off
angularScore = 0.0;
if (angularDeviation > 22.0/14) //> 90 degrees
if (angularDeviation > camAngleOfView*11.0/(7.0*90.0)) // angleofview/2
{
std::cout << "Too bad! The camera is not focusing on any target" << std::endl;
......@@ -124,11 +133,11 @@ void computeErrorMetric(core::Vec3d& camPos, core::Vec3d& camUpVec, core::Vec3d&
else
{
angularScore = 1.0 - 14.0*angularDeviation / 22.0;
std::cout << "The target in focus is: " << targetInFocus << std::endl;
std::cout << "The target in focus: " << targetInFocus << std::endl;
std::cout << "Angular deviation score in focus: " << angularScore << std::endl;
// compute the positional deviation
posDeviation = (targetPositions[targetInFocus] - camPos).norm();
posDeviation = (targetCamPositions[targetInFocus] - camPos).norm();
positionalScore = 0.0;
if (posDeviation < maxPosDeviation)
......@@ -136,14 +145,15 @@ void computeErrorMetric(core::Vec3d& camPos, core::Vec3d& camUpVec, core::Vec3d&
positionalScore = 1.0 - posDeviation / maxPosDeviation;
}
std::cout << "Positional deviation w.r.t target in focus score: "
std::cout << "Positional deviation focus w.r.t target in score: "
<< positionalScore << std::endl;
// compute the angular deviation in roll
}
}
// Define custom interaction style to make a screen capture and record camera configuration
///
/// \brief Define custom interaction style to make a screen
/// capture and record camera configuration
///
class CamNavigationDataGatherer : public vtkInteractorStyleTrackballCamera
{
public:
......@@ -216,24 +226,60 @@ public:
// record the camera configuration
if (this->camController != nullptr)
{
time(&timerNow);
core::Vec3d camPos = *this->camController->getCameraPoition();
core::Vec3d camFocus = *this->camController->getCameraPoition();
core::Vec3d camUpVec = *this->camController->getCameraPoition();
long double elapsedTime = timeKeeper->elapsed();
double* camPos = this->camController->getCameraPoition();
double* camFocusPoint = this->camController->getCameraFocus();
double* camUpVec = this->camController->getCameraUpVector();
if (metricsFile.is_open())
{
metricsFile << difftime(timerNow, timeAtStart) << std::endl;
metricsFile << camPos[0] << camPos[1] << camPos[2] << std::endl;
metricsFile << camFocus[0] << camFocus[1] << camFocus[2] << std::endl;
metricsFile << camUpVec[0] << camUpVec[1] << camUpVec[2] << std::endl;
metricsFile.close();
metricsFile
<< "# "
<< this->screenCaptureData->screenShotNumber
<< ":\n";
metricsFile << elapsedTime << std::endl;
metricsFile
<< camPos[0] << "\t"
<< camPos[1]<< "\t"
<< camPos[2] << std::endl;
metricsFile
<< camFocusPoint[0] << "\t"
<< camFocusPoint[1] << "\t"
<< camFocusPoint[2] << std::endl;
metricsFile
<< camUpVec[0] << "\t"
<< camUpVec[1] << "\t"
<< camUpVec[2] << std::endl;
metricsFile << "\n";
}
std::cout << difftime(timerNow, timeAtStart) << std::endl;
std::cout << camPos[0] << camPos[1] << camPos[2] << std::endl;
std::cout << camFocus[0] << camFocus[1] << camFocus[2] << std::endl;
std::cout << camUpVec[0] << camUpVec[1] << camUpVec[2] << std::endl;
std::cout << elapsedTime << " sec" << std::endl;
std::cout
<< camPos[0] << "\t"
<< camPos[1] << "\t"
<< camPos[2] << std::endl;
std::cout
<< camFocusPoint[0]
<< "\t" << camFocusPoint[1]
<< "\t" << camFocusPoint[2] << std::endl;
std::cout
<< camUpVec[0] << "\t"
<< camUpVec[1] << "\t"
<< camUpVec[2] << std::endl;
core::Vec3d sPos(camPos[0], camPos[1], camPos[2]);
core::Vec3d sFoc(camFocusPoint[0], camFocusPoint[1], camFocusPoint[2]);
core::Vec3d sUV(camUpVec[0], camUpVec[1], camUpVec[2]);
computeErrorMetric(sPos, sUV, sFoc);
}
}
......@@ -248,25 +294,32 @@ public:
void initialize(vtkWindow* rw)
{
timeKeeper = std::make_shared<core::Timer>();
metricsFile = std::ofstream(metricsFileName);
metricsFile << "* DAY AND TIME OF TRIAL: \n"
<< timeKeeper->getTimeDateTimeFormat() << "\n";
metricsFile << "* POSITIONS OF TARGETS:" << std::endl;
for (auto i = 0; i < numTargets; ++i)
{
metricsFile << "Target " << i << " : " << std::endl;
metricsFile << targetPositions[i].x() << "\t"
<< targetPositions[i].y() << "\t"
<< targetPositions[i].z() << "\n";
}
metricsFile << "---------" << std::endl;
time_t rawtime;
time(&rawtime);
struct tm * timeinfo = localtime(&rawtime);
metricsFile << "Time and date of trial: " << asctime(timeinfo) << "\n\n";
metricsFile << "\n* SCREEN SHOT DATA: "
<< "Time, Camera pos, Camera focus point, Camera Up vector\n\n";
this->screenCaptureData = std::make_shared<screenShotData>();
this->screenCaptureData->windowToImageFilter->SetInput(rw);
time(&timeAtStart);
timeKeeper->start();
}
private:
time_t timerNow, timeAtStart;
std::shared_ptr<core::Timer> timeKeeper;
ofstream metricsFile;
std::shared_ptr<screenShotData> screenCaptureData;
......@@ -327,10 +380,12 @@ bool createCameraNavigationScene(
Color meshColor(0.32, 0.32, 0.32, 1.0);
std::shared_ptr<RenderDetail> targetRenderDetail = std::make_shared<RenderDetail>(SIMMEDTK_RENDER_TEXTURE);
std::shared_ptr<RenderDetail> targetRenderDetail
= std::make_shared<RenderDetail>(SIMMEDTK_RENDER_TEXTURE);
targetRenderDetail->addTexture("target", targetFileName, "", "");
std::shared_ptr<RenderDetail> blockRenderDetail = std::make_shared<RenderDetail>(SIMMEDTK_RENDER_NORMALS);
std::shared_ptr<RenderDetail> blockRenderDetail = std::make_shared<RenderDetail>();
blockRenderDetail->setAmbientColor(meshColor);
blockRenderDetail->setDiffuseColor(meshColor);
blockRenderDetail->setSpecularColor(meshColor);
......@@ -347,11 +402,15 @@ bool createCameraNavigationScene(
// BLOCKS
// surface mesh
std::vector<core::Vec3d> blockPts = {core::Vec3d( X/2, 0, -Z/2), core::Vec3d( X/2, 0, Z/2),
core::Vec3d( -X/2, 0, Z/2), core::Vec3d( -X/2, 0, -Z/2),
core::Vec3d( -X/2, Y, -Z/2), core::Vec3d( X/2, Y, -Z/2),
core::Vec3d( -X/2, Y, Z*(pZ-0.5)), core::Vec3d( X/2, Y, Z*(pZ-0.5)),
core::Vec3d( -X/2, Y*pY, Z/2), core::Vec3d( X/2, Y*pY, Z/2)};
std::vector<core::Vec3d> blockPts =
{
core::Vec3d(X / 2, 0, -Z / 2), core::Vec3d(X / 2, 0, Z / 2),
core::Vec3d(-X / 2, 0, Z / 2), core::Vec3d(-X / 2, 0, -Z / 2),
core::Vec3d(-X / 2, Y, -Z / 2),core::Vec3d(X / 2, Y, -Z / 2),
core::Vec3d(-X / 2, Y, Z*(pZ - 0.5)), core::Vec3d(X / 2, Y, Z*(pZ - 0.5)),
core::Vec3d(-X / 2, Y*pY, Z / 2), core::Vec3d(X / 2, Y*pY, Z / 2)
};
std::vector<std::array<size_t,3>> blockTriangles = {{{ 0, 1, 2}}, {{ 0, 2, 3}},
{{ 0, 3, 4}}, {{ 5, 0, 4}},
{{ 5, 4, 6}}, {{ 7, 5, 6}},
......@@ -360,12 +419,15 @@ bool createCameraNavigationScene(
{{ 3, 6, 4}}, {{ 3, 8, 6}},
{{ 3, 2, 8}}, {{ 5, 7, 0}},
{{ 7, 9, 0}}, {{ 9, 1, 0}}};
std::shared_ptr<SurfaceMesh> blockMesh = std::make_shared<SurfaceMesh>();
blockMesh->setVertices(blockPts);
blockMesh->setTriangles(blockTriangles);
// model
std::shared_ptr<MeshCollisionModel> blockModel = std::make_shared<MeshCollisionModel>();
std::shared_ptr<MeshCollisionModel> blockModel =
std::make_shared<MeshCollisionModel>();
blockModel->setMesh(blockMesh);
blockModel->setRenderDetail(blockRenderDetail);
blockModel->getMesh()->scale(s);
......@@ -373,7 +435,9 @@ bool createCameraNavigationScene(
blockModel->getMesh()->rotate(q);
// object
std::shared_ptr<StaticSceneObject> blockObject = std::make_shared<StaticSceneObject>();
std::shared_ptr<StaticSceneObject> blockObject
= std::make_shared<StaticSceneObject>();
blockObject->setModel(blockModel);
scene->addSceneObject(blockObject);
......@@ -383,7 +447,15 @@ bool createCameraNavigationScene(
core::Vec3d topRightEdge(X/2, Y, Z*(pZ-0.5));
core::Vec3d bottomLeftEdge(-X/2, Y*pY, Z/2);
core::Vec3d bottomRightEdge(X/2, Y*pY, Z/2);
std::vector<core::Vec3d> points = {topLeftEdge, topRightEdge, bottomLeftEdge, bottomRightEdge};
std::vector<core::Vec3d> points =
{
topLeftEdge,
topRightEdge,
bottomLeftEdge,
bottomRightEdge
};
std::array<size_t, 3> tri1 = {{ 0, 1, 2}};
std::array<size_t, 3> tri2 = {{ 1, 2, 3}};
std::vector<std::array<size_t,3>> triArray = {tri1, tri2};
......@@ -414,7 +486,9 @@ bool createCameraNavigationScene(
surfaceMesh->addTextureCoordinate(-dx,1.0+dy);
// model
std::shared_ptr<MeshCollisionModel> targetModel = std::make_shared<MeshCollisionModel>();
std::shared_ptr<MeshCollisionModel> targetModel
= std::make_shared<MeshCollisionModel>();
targetModel->setMesh(surfaceMesh);
targetModel->setRenderDetail(targetRenderDetail);
targetModel->getMesh()->scale(s);
......@@ -422,16 +496,24 @@ bool createCameraNavigationScene(
targetModel->getMesh()->rotate(q);
// object
std::shared_ptr<StaticSceneObject> targetObject = std::make_shared<StaticSceneObject>();
std::shared_ptr<StaticSceneObject> targetObject =
std::make_shared<StaticSceneObject>();
targetObject->setModel(targetModel);
scene->addSceneObject(targetObject);
// Compute the target normals and positions
targetNormals[i] = (surfaceMesh->getVertex(0) - surfaceMesh->getVertex(1)).cross(
surfaceMesh->getVertex(0) - surfaceMesh->getVertex(2));
targetNormals[i].normalize();
targetPositions[i] = (surfaceMesh->getVertex(1) + surfaceMesh->getVertex(0)) / 2.0;
double distanceAlongNormal = (rTarget - rOverlay) /
std::tan(camAngleOfView*11.0 / 180 * 7);
targetCamPositions[i] = targetPositions[i] + targetNormals[i] * distanceAlongNormal;
}
return true;
}
......@@ -472,7 +554,9 @@ void add2DOverlay(std::shared_ptr<VTKViewer> vtkViewer,
imageActor->SetPosition(0.5, 0.5);
// Renderer
vtkRenderer* rendererVtk = vtkViewer->getRenderWindow()->GetRenderers()->GetFirstRenderer();
vtkRenderer* rendererVtk
= vtkViewer->getRenderWindow()->GetRenderers()->GetFirstRenderer();
rendererVtk->AddActor2D(imageActor.GetPointer());
}
......@@ -511,7 +595,7 @@ std::shared_ptr<LaparoscopicCameraController> addCameraController(std::shared_pt
auto viewer = sdk->getViewerInstance();
std::shared_ptr<VTKViewer> vtkViewer = std::static_pointer_cast<VTKViewer>(viewer);
vtkCamera* cam = vtkViewer->getVtkCamera();
cam->SetViewAngle(viewAngle);
cam->SetViewAngle(camAngleOfView);
// Set Camera Controller
auto camController = std::make_shared<LaparoscopicCameraController>(camClient);
......
......@@ -94,11 +94,13 @@ vtkCamera* LaparoscopicCameraController::getcamera() const
return this->camera;
}
core::Vec3d* LaparoscopicCameraController::getCameraPoition() const
double* LaparoscopicCameraController::getCameraPoition() const
{
double* pos = this->camera->GetPosition();
return this->camera->GetPosition();
return &core::Vec3d(pos[0], pos[1], pos[2]);
//std::cout << pos[0] << "\t" << pos[1] << "\t" << pos[2] << std::endl;
//return core::Vec3d(pos[0], pos[1], pos[2]);
}
void LaparoscopicCameraController::setCameraPoition(core::Vec3d& p)
......@@ -106,11 +108,9 @@ void LaparoscopicCameraController::setCameraPoition(core::Vec3d& p)
this->camera->SetPosition(p[0], p[1], p[2]);
}
core::Vec3d* LaparoscopicCameraController::getCameraUpVector() const
double* LaparoscopicCameraController::getCameraUpVector() const
{
double* viewUp = this->camera->GetViewUp();
return &core::Vec3d(viewUp[0], viewUp[1], viewUp[2]);
return this->camera->GetViewUp();
}
void LaparoscopicCameraController::setCameraUpVector(core::Vec3d& p)
......@@ -118,11 +118,9 @@ void LaparoscopicCameraController::setCameraUpVector(core::Vec3d& p)
this->camera->SetViewUp(p[0], p[1], p[2]);
}
core::Vec3d* LaparoscopicCameraController::getCameraFocus() const
double* LaparoscopicCameraController::getCameraFocus() const
{
double* focus = this->camera->GetFocalPoint();
return &core::Vec3d(focus[0], focus[1], focus[2]);
return this->camera->GetFocalPoint();
}
void LaparoscopicCameraController::setCameraFocus(core::Vec3d& p)
......
......@@ -106,19 +106,19 @@ public:
///
/// \brief Set/Get position the controlled camera
///
core::Vec3d* getCameraPoition() const;
double* getCameraPoition() const;
void setCameraPoition(core::Vec3d&);
///
/// \brief Set/Get up vector the controlled camera
///
core::Vec3d* getCameraUpVector() const;
double* getCameraUpVector() const;
void setCameraUpVector(core::Vec3d& p);
///
/// \brief Set/Get focus the controlled camera
///
core::Vec3d* getCameraFocus() const;
double* getCameraFocus() const;
void setCameraFocus(core::Vec3d& p);
///
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment