Skip to content

Commit

Permalink
- separate VisualizerMAT class
Browse files Browse the repository at this point in the history
  • Loading branch information
vwaurich authored and adeas31 committed Sep 30, 2016
1 parent a751c1a commit 394cc91
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 194 deletions.
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Animation/AnimationWindowContainer.cpp
Expand Up @@ -29,7 +29,7 @@
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
* @author Volker Waurich <volker.waurich@tu-dresden.de>
*/

#include "AnimationWindowContainer.h"
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Animation/AnimationWindowContainer.h
Expand Up @@ -38,6 +38,7 @@
#include "AnimationUtil.h"
#include "MainWindow.h"
#include "Visualizer.h"
#include "VisualizerMAT.h"

#include <iostream>
#include <string>
Expand Down
171 changes: 0 additions & 171 deletions OMEdit/OMEditGUI/Animation/Visualizer.cpp
Expand Up @@ -311,177 +311,6 @@ void VisualizerAbstract::pauseVisualization()
}


///--------------------------------------------------///
///MAT VISUALIZER CLASS------------------------------///
///--------------------------------------------------///


VisualizerMAT::VisualizerMAT(const std::string& modelFile, const std::string& path)
: VisualizerAbstract(modelFile, path, VisType::MAT),
_matReader()
{
}

void VisualizerMAT::initData()
{
VisualizerAbstract::initData();
readMat(_baseData->getModelFile(), _baseData->getPath());
_timeManager->setStartTime(omc_matlab4_startTime(&_matReader));
_timeManager->setEndTime(omc_matlab4_stopTime(&_matReader));
}

void VisualizerMAT::initializeVisAttributes(const double time)
{
if (0.0 > time)
std::cout<<"Cannot load visualization attributes for time point < 0.0."<<std::endl;
updateVisAttributes(time);
}

void VisualizerMAT::readMat(const std::string& modelFile, const std::string& path)
{
std::string resFileName = path + modelFile; // + "_res.mat";

// Check if the MAT file exists.
if (!fileExists(resFileName))
{
std::string msg = "Could not find MAT file" + resFileName + ".";
std::cout<<msg<<std::endl;
}
else
{
// Read mat file.
auto ret = omc_new_matlab4_reader(resFileName.c_str(), &_matReader);
// Check return value.
if (0 != ret)
{
std::string msg(ret);
std::cout<<msg<<std::endl;
}
}

/*
FILE * fileA = fopen("allVArs.txt", "w+");
omc_matlab4_print_all_vars(fileA, &matReader);
fclose(fileA);
*/
}

void VisualizerMAT::updateVisAttributes(const double time)
{
//std::cout<<"updateVisAttributes at "<<time <<std::endl;
// Update all shapes.
unsigned int shapeIdx = 0;
rAndT rT;
osg::ref_ptr<osg::Node> child = nullptr;
ModelicaMatReader* tmpReaderPtr = &_matReader;
try
{
for (auto& shape : _baseData->_shapes)
{
//std::cout<<"shape "<<shape._id <<std::endl;

// Get the values for the scene graph objects
updateObjectAttributeMAT(&shape._length, time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._width, time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._height, time, tmpReaderPtr);

updateObjectAttributeMAT(&shape._lDir[0], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._lDir[1], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._lDir[2], time, tmpReaderPtr);

updateObjectAttributeMAT(&shape._wDir[0], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._wDir[1], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._wDir[2], time, tmpReaderPtr);

updateObjectAttributeMAT(&shape._r[0], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._r[1], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._r[2], time, tmpReaderPtr);

updateObjectAttributeMAT(&shape._rShape[0], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._rShape[1], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._rShape[2], time, tmpReaderPtr);

updateObjectAttributeMAT(&shape._T[0], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[1], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[2], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[3], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[4], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[5], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[6], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[7], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._T[8], time, tmpReaderPtr);

updateObjectAttributeMAT(&shape._color[0], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._color[1], time, tmpReaderPtr);
updateObjectAttributeMAT(&shape._color[2], time, tmpReaderPtr);

updateObjectAttributeMAT(&shape._specCoeff, time, tmpReaderPtr);

rT = rotateModelica2OSG(osg::Vec3f(shape._r[0].exp, shape._r[1].exp, shape._r[2].exp),
osg::Vec3f(shape._rShape[0].exp, shape._rShape[1].exp, shape._rShape[2].exp),
osg::Matrix3(shape._T[0].exp, shape._T[1].exp, shape._T[2].exp,
shape._T[3].exp, shape._T[4].exp, shape._T[5].exp,
shape._T[6].exp, shape._T[7].exp, shape._T[8].exp),
osg::Vec3f(shape._lDir[0].exp, shape._lDir[1].exp, shape._lDir[2].exp),
osg::Vec3f(shape._wDir[0].exp, shape._wDir[1].exp, shape._wDir[2].exp),
shape._length.exp,/* shape._width.exp, shape._height.exp,*/ shape._type);

assemblePokeMatrix(shape._mat, rT._T, rT._r);
// Update the shapes.
_nodeUpdater->_shape = shape;
//shape.dumpVisAttributes();
// Get the scene graph nodes and stuff.
child = _viewerStuff->getScene().getRootNode()->getChild(shapeIdx); // the transformation
child->accept(*_nodeUpdater);
++shapeIdx;
}
}
catch (std::exception& ex)
{
std::string msg = "Error in VisualizerMAT::updateVisAttributes at time point " + std::to_string(time)
+ "\n" + std::string(ex.what());
throw(msg);
}
}

void VisualizerMAT::updateScene(const double time)
{
if (0.0 > time)

_timeManager->updateTick(); //for real-time measurement
double visTime = _timeManager->getRealTime();

updateVisAttributes(time);

_timeManager->updateTick(); //for real-time measurement
visTime = _timeManager->getRealTime() - visTime;
_timeManager->setRealTimeFactor(_timeManager->getHVisual() / visTime);
}

void VisualizerMAT::updateObjectAttributeMAT(ShapeObjectAttribute* attr, double time, ModelicaMatReader* reader)
{
if (!attr->isConst)
attr->exp = omcGetVarValue(reader, attr->cref.c_str(), time);
}

double VisualizerMAT::omcGetVarValue(ModelicaMatReader* reader, const char* varName, double time)
{
double val = 0.0;
ModelicaMatVariable_t* var = nullptr;
var = omc_matlab4_find_var(reader, varName);
if (var == nullptr)
std::cout<<"Did not get variable from result file. Variable name is "<<std::string(varName)<<std::endl;
else
omc_matlab4_val(&val, reader, var, time);

return val;
}

void VisualizerMAT::setSimulationSettings(const UserSimSettingsMAT& simSetMAT)
{
auto newVal = simSetMAT.speedup * _timeManager->getHVisual();
_timeManager->setHVisual(newVal);
}


OMVisScene::OMVisScene()
Expand Down
22 changes: 0 additions & 22 deletions OMEdit/OMEditGUI/Animation/Visualizer.h
Expand Up @@ -180,28 +180,6 @@ class VisualizerAbstract
TimeManager* _timeManager;
};

class VisualizerMAT : public VisualizerAbstract
{
public:
VisualizerMAT() = delete;
VisualizerMAT(const std::string& fileName, const std::string& path);
virtual ~VisualizerMAT() = default;
VisualizerMAT(const VisualizerMAT& omvm) = delete;
VisualizerMAT& operator=(const VisualizerMAT& omvm) = delete;
void initData();
void initializeVisAttributes(const double time = -1.0);
void readMat(const std::string& modelFile, const std::string& path);
void setSimulationSettings(const UserSimSettingsMAT& simSetMAT);
//void simulate(TimeManager& omvm){ };
void updateVisAttributes(const double time) override;
void updateScene(const double time);
void updateObjectAttributeMAT(ShapeObjectAttribute* attr, double time, ModelicaMatReader* reader);
double omcGetVarValue(ModelicaMatReader* reader, const char* varName, double time);
private:
ModelicaMatReader _matReader;
};


osg::Vec3f Mat3mulV3(osg::Matrix3 M, osg::Vec3f V);
osg::Vec3f V3mulMat3(osg::Vec3f V, osg::Matrix3 M);
osg::Matrix3 Mat3mulMat3(osg::Matrix3 M1, osg::Matrix3 M2);
Expand Down

0 comments on commit 394cc91

Please sign in to comment.