Skip to content

Commit

Permalink
[OMEdit] Implement visualization of Vector
Browse files Browse the repository at this point in the history
- Create VectorObject class inheriting AbstractVisualizerObject
- Read VectorObject attributes from MAT/CSV/FMU files
- Visualize VectorObject as a composite of three OSG shapes
  (one cylinder for the shaft, two cones for both heads)
- Select scale/length/radius constants from MSL default parameters
  • Loading branch information
anotheruserofgithub authored and adeas31 committed Jun 19, 2022
1 parent ba864fe commit d2a23be
Show file tree
Hide file tree
Showing 10 changed files with 562 additions and 4 deletions.
95 changes: 95 additions & 0 deletions OMEdit/OMEditLIB/Animation/Vector.cpp
@@ -0,0 +1,95 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#include "Vector.h"

#include <cmath>

std::ostream& operator<<(std::ostream& os, const VectorQuantity quantity)
{
switch (quantity)
{
case VectorQuantity::force:
return os << "force";
case VectorQuantity::torque:
return os << "torque";
case VectorQuantity::velocity:
return os << "velocity";
case VectorQuantity::acceleration:
return os << "acceleration";
case VectorQuantity::angularVelocity:
return os << "angular velocity";
case VectorQuantity::angularAcceleration:
return os << "angular acceleration";
case VectorQuantity::relativePosition:
return os << "relative position";
default:
return os;
}
}

VectorObject::VectorObject()
: AbstractVisualizerObject(VisualizerType::vector),
_quantity(VisualizerAttribute(0.0)),
_headAtOrigin(VisualizerAttribute(0.0)),
_twoHeadedArrow(VisualizerAttribute(0.0))
{
_coords[0] = VisualizerAttribute(1.0);
_coords[1] = VisualizerAttribute(0.0);
_coords[2] = VisualizerAttribute(0.0);
}

void VectorObject::dumpVisualizerAttributes() const
{
AbstractVisualizerObject::dumpVisualizerAttributes();
std::cout << "coords " << _coords[0].getValueString() << " , " << _coords[1].getValueString() << " , " << _coords[2].getValueString() << std::endl;
std::cout << "quantity " << _quantity.getValueString() << " = " << getQuantity() << std::endl;
std::cout << "headAtOrigin " << _headAtOrigin.getValueString() << " = " << (hasHeadAtOrigin() ? "true" : "false") << std::endl;
std::cout << "twoHeadedArrow " << _twoHeadedArrow.getValueString() << " = " << (isTwoHeadedArrow() ? "true" : "false") << std::endl;
}

float VectorObject::getScale() const
{
switch (getQuantity())
{
case VectorQuantity::force:
return kScaleForce;
case VectorQuantity::torque:
return kScaleTorque;
default:
return 1;
}
}

float VectorObject::getLength() const
{
return std::sqrt(std::pow(_coords[0].exp, 2) + std::pow(_coords[1].exp, 2) + std::pow(_coords[2].exp, 2)) / getScale();
}
71 changes: 71 additions & 0 deletions OMEdit/OMEditLIB/Animation/Vector.h
@@ -0,0 +1,71 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#ifndef VECTOR_H
#define VECTOR_H

#include "AbstractVisualizer.h"

/*! Equivalent to Modelica.Mechanics.MultiBody.Types.VectorQuantity */
enum class VectorQuantity {force = 1, torque, velocity, acceleration, angularVelocity, angularAcceleration, relativePosition};

std::ostream& operator<<(std::ostream& os, const VectorQuantity quantity);

class VectorObject : public AbstractVisualizerObject
{
public:
VectorObject();
~VectorObject() = default;
VectorObject(const VectorObject&) = default;
VectorObject& operator=(const VectorObject&) = default;
void dumpVisualizerAttributes() const override;
float getScale() const;
float getLength() const;
float getRadius() const {return kRadius;}
float getHeadLength() const {return kHeadLength;}
float getHeadRadius() const {return kHeadRadius;}
VectorQuantity getQuantity() const {return static_cast<VectorQuantity>(_quantity.exp);}
bool hasHeadAtOrigin() const {return _headAtOrigin.exp;}
bool isTwoHeadedArrow() const {return _twoHeadedArrow.exp;}
private:
const float kRadius = 0.0125; //!< Modelica.Mechanics.MultiBody.World.defaultArrowDiameter / 2 = 1 / 40 / 2 = 0.0125
const float kHeadLength = 0.1000; //!< Modelica.Mechanics.MultiBody.Types.Defaults.ArrowHeadLengthFraction * (2 * kRadius) = 4 * 0.025 = 0.1000
const float kHeadRadius = 0.0375; //!< Modelica.Mechanics.MultiBody.Types.Defaults.ArrowHeadWidthFraction * (2 * kRadius) / 2 = 3 * 0.025 / 2 = 0.0375
const float kScaleForce = 1200; //!< Modelica.Mechanics.MultiBody.Examples.Elementary.ForceAndTorque.world.defaultN_to_m = 1200
const float kScaleTorque = 120; //!< Modelica.Mechanics.MultiBody.Examples.Elementary.ForceAndTorque.world.defaultNm_to_m = 120
public:
VisualizerAttribute _coords[3];
VisualizerAttribute _quantity;
VisualizerAttribute _headAtOrigin;
VisualizerAttribute _twoHeadedArrow;
};

#endif
6 changes: 5 additions & 1 deletion OMEdit/OMEditLIB/Animation/ViewerWidget.cpp
Expand Up @@ -464,11 +464,15 @@ void ViewerWidget::removeTexture()
void ViewerWidget::resetTransparencyAndTextureForAllVisualizers()
{
std::vector<ShapeObject>& shapes = mpAnimationWidget->getVisualization()->getBaseData()->_shapes;
std::vector<VectorObject>& vectors = mpAnimationWidget->getVisualization()->getBaseData()->_vectors;
std::vector<std::reference_wrapper<AbstractVisualizerObject>> visualizers;
visualizers.reserve(shapes.size());
visualizers.reserve(shapes.size() + vectors.size());
for (ShapeObject& shape : shapes) {
visualizers.push_back(shape);
}
for (VectorObject& vector : vectors) {
visualizers.push_back(vector);
}
for (AbstractVisualizerObject& visualizer : visualizers) {
visualizer.setTransparency(0.0);
visualizer.setTextureImagePath("");
Expand Down

0 comments on commit d2a23be

Please sign in to comment.