Salome HOME
To make reusable some C++ idioms that are implemented in MEDWrapper of MED module
[modules/visu.git] / src / CONVERTOR / VISU_Convertor.hxx
index 5338d17ae54d3ca10441027ab1bceff6ee710fd3..bebb26f096e3f5f67ab41d21ed26f03d0741a983 100644 (file)
 
 #include <stdio.h>
 
-#include <algorithm>
-#include <iterator>
 #include <list>
 #include <map>
-#include <numeric>
 #include <set>
 #include <utility>
 #include <vector>
 #include <string>
 #include <stdexcept>
 
-#include <vtkPoints.h>
-#include <vtkSystemIncludes.h>
-#include <vtkUnstructuredGrid.h>
-#include <vtkMergeDataObjectFilter.h>
-#include <vtkFieldDataToAttributeDataFilter.h>
+#include <boost/shared_ptr.hpp>
 
-#include "VISU_ExtractUnstructuredGrid.hxx"
-
-#include <vtkSmartPointer.h>
+class vtkUnstructuredGrid;
 
 namespace VISU{
+
+  template<class T> class shared_ptr: public boost::shared_ptr<T>
+  {
+  public:
+    shared_ptr() {}
+
+    template<class Y>
+    explicit shared_ptr(Y * p)
+    {
+      reset(p);
+    }
+
+    template<class Y>
+    shared_ptr(shared_ptr<Y> const & r):
+      boost::shared_ptr<T>(r,boost::detail::dynamic_cast_tag())
+    {}
+
+    template<class Y>
+    shared_ptr & operator=(shared_ptr<Y> const & r)
+    {
+      shared_ptr<T>(r).swap(*this);
+      return *this;
+    }
+
+    template<class Y> shared_ptr& operator()(Y * p) // Y must be complete
+    {
+      if(T* pt = dynamic_cast<T*>(p))
+       boost::shared_ptr<T>::reset(pt);
+      else
+       boost::throw_exception(std::bad_cast());
+      return *this;
+    }
+
+  };
+
+
   enum  TEntity {NODE_ENTITY, EDGE_ENTITY, FACE_ENTITY, CELL_ENTITY};
 
-  typedef vtkSmartPointer<vtkPoints> TVTKPoints;
-  typedef vtkSmartPointer<vtkUnstructuredGrid> TVTKSource;
-  typedef vtkSmartPointer<vtkMergeDataObjectFilter> TVTKMergetFilter;
-  typedef vtkSmartPointer<VISU_ExtractUnstructuredGrid> TVTKExtractFilter;
-  typedef vtkSmartPointer<vtkFieldDataToAttributeDataFilter> TVTKAttribyteFilter;
+  typedef std::pair<std::string,TEntity> TFamilyAndEntity;
+  typedef std::set<TFamilyAndEntity> TFamilyAndEntitySet;
   typedef std::set<std::string> TBindGroups;
 
-  struct TFamily{
-    TVTKSource myStorage;
-    vtkIdType myId;
-    std::string myName;
-    TEntity myEntity;
-    TBindGroups myGroups;
-    vtkIdType myNbCells, myCellsSize;
-    typedef std::set<vtkIdType> TSubMeshOnCellType;
-    typedef std::map<vtkIdType,TSubMeshOnCellType> TSubMesh;
-    TSubMesh mySubMesh;
-    TFamily() : myNbCells(0), myCellsSize(0) {}
+  typedef std::pair<double,std::string> TTime;
+
+  struct TMesh;
+  typedef shared_ptr<TMesh> PMesh;
+  typedef std::map<std::string,PMesh> TMeshMap;
+
+  struct TMeshOnEntity;
+  typedef shared_ptr<TMeshOnEntity> PMeshOnEntity;
+  typedef std::map<TEntity,PMeshOnEntity> TMeshOnEntityMap;
+
+  struct TFamily;
+  typedef shared_ptr<TFamily> PFamily;
+  typedef std::map<std::string,PFamily> TFamilyMap;
+
+  struct TGroup;
+  typedef shared_ptr<TGroup> PGroup;
+  typedef std::map<std::string,PGroup> TGroupMap;
+
+  struct TField;
+  typedef shared_ptr<TField> PField;
+  typedef std::map<std::string,PField> TFieldMap;
+
+  struct TValForTime;
+  typedef shared_ptr<TValForTime> PValForTime;
+  typedef std::map<int,PValForTime> TValField;
+
+  struct TBaseStructure{
+    virtual ~TBaseStructure(){}
   };
-  typedef std::map<std::string,TFamily> TFamilyMap;
 
-  struct TField{
-    TVTKExtractFilter myExtractFilter;
-    vtkIdType myId;
+  typedef std::vector<std::string> TNames;
+
+  struct TMesh: TBaseStructure{
+    int myDim, myNbPoints;
     std::string myName;
-    TEntity myEntity;
-    std::string myMeshName;
-    vtkIdType myNbComp, myNbValField, myDataSize, myIsTrimmed;
-    typedef std::vector<float> TValForCellsWithType;
-    typedef std::map<vtkIdType,TValForCellsWithType> TValForCells;
-    typedef std::pair<double,std::string> TTime;
-    typedef std::vector<std::string> TCompNames;
-    typedef std::vector<std::string> TUnitNames;
-    struct TValForTime{
-      TVTKAttribyteFilter myAttribyteFilter;
-      TVTKMergetFilter myMergeFilter;
-      TVTKSource myStorage;
-      vtkIdType myId;
-      std::string myMeshName;
-      TEntity myEntity;
-      std::string myFieldName;
-      vtkIdType myNbComp;
-      TTime myTime;
-      TValForCells myValForCells;
-      TValForTime() : myNbComp(0) {}
-      ~TValForTime() { 
-       if(myMergeFilter.GetPointer())
-         myMergeFilter->UnRegisterAllOutputs();
-       if(myAttribyteFilter.GetPointer())
-         myAttribyteFilter->UnRegisterAllOutputs();
-      }
-    };
-    typedef std::map<vtkIdType,TValForTime> TValField;
-    TValField myValField;
-    TCompNames myCompNames;
-    TUnitNames myUnitNames;
-    TField() : myNbComp(0), myNbValField(0), myDataSize(0), myIsTrimmed(0) {}
-    void ShallowCopy(const TField& aField);
-    ~TField() { 
-      if(myExtractFilter.GetPointer())
-       myExtractFilter->UnRegisterAllOutputs();
-    }
+
+    TMeshOnEntityMap myMeshOnEntityMap;
+    TGroupMap myGroupMap;
+
+    TMesh() : myDim(0), myNbPoints(0) {}
+    const PField GetField(const std::string& theFieldName) const;
   };
-  typedef std::map<std::string,TField> TFieldMap;
-  struct TMeshOnEntity{
-    TVTKSource myStorage;
+
+  struct TMeshOnEntity: TBaseStructure{
     std::string myMeshName;
     TEntity myEntity;
-    vtkIdType myNbCells, myCellsSize;
-    typedef std::vector<vtkIdType> TConnect;
-    typedef std::vector<TConnect> TConnForCellType;
-    typedef std::map<vtkIdType,TConnForCellType> TCellsConn;
-    TCellsConn myCellsConn;
+    int myNbCells, myCellsSize;
     TFamilyMap myFamilyMap;
     TFieldMap myFieldMap;
     TMeshOnEntity() : myNbCells(0), myCellsSize(0) {}
-    std::pair<vtkIdType,vtkIdType> GetCellsDims(const std::string& theFamilyName = "") const;
-      
+
+    std::pair<int,int> 
+    GetCellsDims(const std::string& theFamilyName = "") const;
   };
-  typedef std::map<TEntity,TMeshOnEntity> TMeshOnEntityMap;
-  const TFamily* GetFamily(const VISU::TMeshOnEntity& theMeshOnEntity, 
-                    const std::string& theFamilyName);
 
-  TFamily* GetFamily(VISU::TMeshOnEntity& theMeshOnEntity, 
-                    const std::string& theFamilyName);
+  struct TFamily: TBaseStructure{
+    int myId;
+    std::string myName;
+    TEntity myEntity;
+    TBindGroups myGroups;
+    int myNbCells, myCellsSize;
+    TFamily() : myNbCells(0), myCellsSize(0) {}
+  };
 
-  typedef std::pair<std::string,TEntity> TFamilyAndEntity;
-  typedef std::set<TFamilyAndEntity> TFamilyAndEntitySet;
-  struct TGroup{
-    TVTKSource myStorage;
+  struct TGroup: TBaseStructure{
     std::string myName;
     std::string myMeshName;
-    vtkIdType myNbCells, myCellsSize;
-    TGroup() : myNbCells(0), myCellsSize(0) {}
+    int myNbCells, myCellsSize;
     TFamilyAndEntitySet myFamilyAndEntitySet;
+    TGroup() : myNbCells(0), myCellsSize(0) {}
   };
-  typedef std::map<std::string,TGroup> TGroupMap;
 
-  struct TMesh{
-    TVTKPoints myPoints;
-    vtkIdType myDim, myNbPoints;
+  struct TField: TBaseStructure{
+    int myId;
+    TEntity myEntity;
+    bool myIsTrimmed;
     std::string myName;
-    typedef float TCoord;
-    typedef std::vector<TCoord> TPointsCoord;
-    TPointsCoord myPointsCoord;
-    typedef std::vector<std::string> TPointsDim;
-    TPointsDim myPointsDim;
-    TMeshOnEntityMap myMeshOnEntityMap;
-    TGroupMap myGroupMap;
-    TMesh() : myDim(0), myNbPoints(0) {}
-    const TField* GetField(const std::string& theFieldName) const;
+    std::string myMeshName;
+    int myNbComp, myDataSize;
+    TValField myValField;
+    TNames myCompNames;
+    TNames myUnitNames;
+    TField() : myNbComp(0), myDataSize(0), myIsTrimmed(0) {}
+  };
+  struct TValForTime: TBaseStructure{
+    int myId;
+    TEntity myEntity;
+    std::string myMeshName;
+    std::string myFieldName;
+    int myNbComp;
+    TTime myTime;
+
+    TValForTime() : myNbComp(0) {}
   };
-  typedef std::map<std::string,TMesh> TMeshMap;
-  void WriteToFile(vtkUnstructuredGrid* theDataSet, const std::string& theFileName);
+
+  PFamily FindFamily(VISU::PMesh theMesh, 
+                    const std::string& theFamilyName);
+
+  PFamily GetFamily(VISU::PMeshOnEntity theMeshOnEntity, 
+                   const std::string& theFamilyName);
+
+  void WriteToFile(vtkUnstructuredGrid* theDataSet, 
+                  const std::string& theFileName);
+
 };
 
 class VISU_Convertor{
@@ -213,23 +234,20 @@ public:
                                   const VISU::TEntity& theEntity,
                                   const std::string& theFieldName) = 0;
  
-  virtual const VISU::TField& GetField(const std::string& theMeshName, 
-                                      VISU::TEntity theEntity, 
-                                      const std::string& theFieldName) = 0;
-
-  virtual const VISU::TField::TValForTime& GetTimeStamp(const std::string& theMeshName, 
-                                                       const VISU::TEntity& theEntity,
-                                                       const std::string& theFieldName,
-                                                       int theStampsNum) = 0;
+  virtual const VISU::PField GetField(const std::string& theMeshName, 
+                                     VISU::TEntity theEntity, 
+                                     const std::string& theFieldName) = 0;
+
+  virtual const VISU::PValForTime GetTimeStamp(const std::string& theMeshName, 
+                                              const VISU::TEntity& theEntity,
+                                              const std::string& theFieldName,
+                                              int theStampsNum) = 0;
     
-  static std::string GenerateName(const VISU::TField::TTime& aTime);
+  static std::string GenerateName(const VISU::TTime& aTime);
   static std::string GenerateName(const std::string& theName, unsigned int theTimeId);
-  static void WriteToFile(vtkUnstructuredGrid* theDataSet, const std::string& theFileName);
 };
 
 extern "C"{
-  VISU_Convertor* CreateMedConvertor(const std::string& theFileName) ;
-  VISU_Convertor* CreateDatConvertor(const std::string& theFileName) ;
   VISU_Convertor* CreateConvertor(const std::string& theFileName) ;
 };