Changeset ee088cb in OpenModelica


Ignore:
Timestamp:
2023-03-27T11:28:24+02:00 (13 months ago)
Author:
anotheruserofgithub <96748782+anotheruserofgithub@…>
Children:
e757cda7
Parents:
740a8c7
git-author:
anotheruserofgithub <96748782+anotheruserofgithub@…> (03/09/23 06:52:36)
git-committer:
anotheruserofgithub <96748782+anotheruserofgithub@…> (03/27/23 11:28:24)
Message:

Support OBJ & 3DS files as CAD shapes

Location:
OMEdit/OMEditLIB/Animation
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • OMEdit/OMEditLIB/Animation/AnimationUtil.h

    r6814223c ree088cb  
    140140}
    141141
    142 
    143142inline bool stlFileType(const std::string& typeName)
    144143{
    145144  return typeName.substr(typeName.size()-3) == std::string("stl");
     145}
     146
     147inline bool objFileType(const std::string& typeName)
     148{
     149  return typeName.substr(typeName.size()-3) == std::string("obj");
     150}
     151
     152inline bool tdsFileType(const std::string& typeName)
     153{
     154  return typeName.substr(typeName.size()-3) == std::string("3ds");
    146155}
    147156
  • OMEdit/OMEditLIB/Animation/Visualization.cpp

    r740a8c7 ree088cb  
    4242#include <osg/GL> // for having direct access to glClear()
    4343
     44#include <osg/Array>
    4445#include <osg/Drawable>
    4546#include <osg/Shape>
     
    332333      } else if (stlFileType(shape._fileName)) {
    333334        shape._type = "stl";
     335      } else if (objFileType(shape._fileName)) {
     336        shape._type = "obj";
     337      } else if (tdsFileType(shape._fileName)) {
     338        shape._type = "3ds";
    334339      }
    335340    }
     
    792797        for (ShapeObject& shape : relevantShapes) {
    793798          // Consider OpenSceneGraph shape drawables only
    794           if (shape._type.compare("dxf") == 0 || shape._type.compare("stl") == 0) {
     799          if (shape._type.compare("dxf") == 0 || shape._type.compare("stl") == 0 ||
     800              shape._type.compare("obj") == 0 || shape._type.compare("3ds") == 0) {
    795801            continue;
    796802          }
     
    12821288    transf->setName(shape._id);
    12831289
    1284     if (shape._type.compare("stl") == 0)
    1285     { //cad node
     1290    if (shape._type.compare("obj") == 0 or shape._type.compare("3ds") == 0)
     1291    { //advanced cad node
     1292      //std::cout<<"It's an advanced cad and the filename is "<<shape._fileName<<std::endl;
     1293      osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(shape._fileName);
     1294      if (node.valid())
     1295      {
     1296        osg::ref_ptr<CADFile> cad = new CADFile(node.get());
     1297
     1298        transf->addChild(cad.get());
     1299      }
     1300    }
     1301    else if (shape._type.compare("stl") == 0)
     1302    { //stl node
    12861303      //std::cout<<"It's a stl and the filename is "<<shape._fileName<<std::endl;
    12871304      // Disable mesh optimization because it is too expensive (see OSG commit a082b57)
     
    14131430     {
    14141431      ShapeObject* shape = _visualizer->asShape();
    1415       if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0)
     1432      if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0 or
     1433          shape->_type.compare("obj") == 0 or shape->_type.compare("3ds") == 0)
    14161434      {
    14171435        //it's a cad file so we have to rescale the underlying geometry vertices
     
    15461564    osg::ref_ptr<osg::StateSet> stateSet = nullptr;
    15471565    bool geometryColors = false;
     1566    bool is3DSShape = false;
    15481567
    15491568    if (_visualizer->isShape()) {
    15501569      ShapeObject* shape = _visualizer->asShape();
    1551       if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0) {
     1570      if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0 or
     1571          shape->_type.compare("obj") == 0 or shape->_type.compare("3ds") == 0) {
    15521572        osg::ref_ptr<osg::Transform> transformNode = shape->getTransformNode();
    15531573        if (transformNode.valid() && transformNode->getNumChildren() > 0) {
     
    15561576            stateSet = cad->getOrCreateStateSet();
    15571577            geometryColors = !shape->getVisualProperties()->getColor().custom();
     1578            is3DSShape = shape->_type.compare("3ds") == 0;
    15581579          }
    15591580        }
     
    15771598      changeTransparencyOfMaterial(ss, transparency);
    15781599      if (geometryColors) {
    1579         changeTransparencyOfGeometry(node, transparency);
    1580       }
    1581     }
     1600        if (is3DSShape) {
     1601          changeTransparencyOfGeometry<osg::Vec4ubArray, 255>(node, transparency);
     1602        } else {
     1603          changeTransparencyOfGeometry<osg::Vec4Array, 1>    (node, transparency);
     1604        }
     1605      }
     1606    }
     1607
    15821608    if (changeTexture) {
    15831609      //set texture
     
    17001726 * changes transparency of a geode's geometry
    17011727 */
     1728template<typename Vec4Array, unsigned int scale>
    17021729void UpdateVisitor::changeTransparencyOfGeometry(osg::Geode& geode, const float transparency)
    17031730{
    1704   osg::Vec4::value_type opacity = 1.0 - transparency;
     1731  using Vec4 = typename Vec4Array::ElementDataType;
     1732  using type = typename Vec4::value_type;
     1733  type opacity = (1.0 - transparency) * scale;
    17051734  unsigned int num = geode.getNumDrawables();
    17061735  for (unsigned int i = 0; i < num; i++) {
     
    17091738      osg::Geometry* geometry = drawable->asGeometry();
    17101739      if (geometry) {
    1711         osg::Vec4Array* colors = dynamic_cast<osg::Vec4Array*>(geometry->getColorArray());
     1740        Vec4Array* colors = dynamic_cast<Vec4Array*>(geometry->getColorArray());
    17121741        if (colors) {
    1713           for (osg::Vec4& color : colors->asVector()) {
     1742          for (Vec4& color : colors->asVector()) {
    17141743            color.a() = opacity;
    17151744          }
     
    18621891
    18631892  osg::Matrix3 T0;
    1864   if (type == "stl" || type == "dxf")
     1893  if (type == "dxf" || type == "stl" || type == "obj" || type == "3ds")
    18651894  {
    18661895    T0 = osg::Matrix3(dirs._lDir[0], dirs._lDir[1], dirs._lDir[2],
  • OMEdit/OMEditLIB/Animation/Visualization.h

    r740a8c7 ree088cb  
    100100  void changeColorOfMaterial(osg::StateSet* ss, const osg::Material::ColorMode mode, const QColor color, const float specular);
    101101  void changeTransparencyOfMaterial(osg::StateSet* ss, const float transparency);
     102  template<typename Vec4Array, unsigned int scale>
    102103  void changeTransparencyOfGeometry(osg::Geode& geode, const float transparency);
    103104public:
Note: See TracChangeset for help on using the changeset viewer.