Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkExtractGroup.cxx
index 8f41041b9bc7ec93a10783395554ba892433a149..c39dc03da3830800eeb39497967976ffdebc219b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2020  CEA/DEN, EDF R&D
+// Copyright (C) 2010-2022  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 
 #include "vtkExtractGroup.h"
 #include "MEDFileFieldRepresentationTree.hxx"
+#include "ExtractGroupHelper.h"
 #include "vtkMEDReader.h"
 #include "VTKMEDTraits.hxx"
 
 #include "vtkAdjacentVertexIterator.h"
-#include "vtkDataArrayTemplate.h"
+#include "vtkAOSDataArrayTemplate.h"
 #include "vtkIntArray.h"
 #include "vtkLongArray.h"
 #ifdef WIN32
 #include <map>
 #include <deque>
 
-vtkStandardNewMacro(vtkExtractGroup);
+vtkStandardNewMacro(vtkExtractGroup)
 
-///////////////////
-
-class ExtractGroupStatus
-{
-public:
-  ExtractGroupStatus():_status(false) { }
-  ExtractGroupStatus(const char *name);
-  bool getStatus() const { return _status; }
-  void setStatus(bool status) const { _status=status; }
-  void cpyStatusFrom(const ExtractGroupStatus& other) { _status=other._status; }
-  std::string getName() const { return _name; }
-  void resetStatus() const { _status=false; }
-  const char *getKeyOfEntry() const { return _ze_key_name.c_str(); }
-  virtual void printMySelf(std::ostream& os) const;
-  virtual bool isSameAs(const ExtractGroupStatus& other) const;
-protected:
-mutable bool _status;
-std::string _name;
-std::string _ze_key_name;
-};
-
-class ExtractGroupGrp : public ExtractGroupStatus
-{
-public:
-  ExtractGroupGrp(const char *name):ExtractGroupStatus(name) { std::ostringstream oss; oss << START << name; _ze_key_name=oss.str(); }
-  void setFamilies(const std::vector<std::string>& fams) { _fams=fams; }
-  const std::vector<std::string>& getFamiliesLyingOn() const { return _fams; }
-  bool isSameAs(const ExtractGroupGrp& other) const;
-public:
-  static const char START[];
-  std::vector<std::string> _fams;
-};
-
-class ExtractGroupFam : public ExtractGroupStatus
+class vtkExtractGroup::vtkExtractGroupInternal : public ExtractGroupInternal
 {
-public:
-  ExtractGroupFam(const char *name);
-  void printMySelf(std::ostream& os) const;
-  void fillIdsToKeep(std::set<int>& s) const;
-  int getId() const { return _id; }
-  bool isSameAs(const ExtractGroupFam& other) const;
-public:
-  static const char START[];
-private:
-  int _id;
 };
 
-class vtkExtractGroup::vtkExtractGroupInternal
-{
-public:
-  void loadFrom(vtkMutableDirectedGraph *sil);
-  int getNumberOfEntries() const;
-  const char *getMeshName() const;
-  const char *getKeyOfEntry(int i) const;
-  bool getStatusOfEntryStr(const char *entry) const;
-  void setStatusOfEntryStr(const char *entry, bool status);
-  void printMySelf(std::ostream& os) const;
-  std::set<int> getIdsToKeep() const;
-  void clearSelection() const;
-  int getIdOfFamily(const std::string& famName) const;
-  static bool IsInformationOK(vtkInformation *info);
-private:
-  std::map<std::string,int> computeFamStrIdMap() const;
-  const ExtractGroupStatus& getEntry(const char *entry) const;
-  ExtractGroupStatus& getEntry(const char *entry);
-private:
-  std::vector<ExtractGroupGrp> _groups;
-  std::vector<ExtractGroupFam> _fams;
-  mutable std::vector< std::pair<std::string,bool> > _selection;
-  std::string _mesh_name;
-};
-
-const char ExtractGroupGrp::START[]="GRP_";
-
-const char ExtractGroupFam::START[]="FAM_";
-
-ExtractGroupStatus::ExtractGroupStatus(const char *name):_status(false),_name(name)
-{
-}
-
-void ExtractGroupStatus::printMySelf(std::ostream& os) const
-{
-  os << "      -" << _ze_key_name << "(";
-  if(_status)
-    os << "X";
-  else
-    os << " ";
-  os << ")" << std::endl;
-}
-
-bool ExtractGroupStatus::isSameAs(const ExtractGroupStatus& other) const
-{
-  return _name==other._name && _ze_key_name==other._ze_key_name;
-}
-
-bool ExtractGroupGrp::isSameAs(const ExtractGroupGrp& other) const
-{
-  bool ret(ExtractGroupStatus::isSameAs(other));
-  if(ret)
-    return _fams==other._fams;
-  else
-    return false;
-}
-
-bool vtkExtractGroup::vtkExtractGroupInternal::IsInformationOK(vtkInformation *info)
-{
-  // Check the information contain meta data key
-  if(!info->Has(vtkMEDReader::META_DATA()))
-    return false;
-
-  // Recover Meta Data
-  vtkMutableDirectedGraph *sil(vtkMutableDirectedGraph::SafeDownCast(info->Get(vtkMEDReader::META_DATA())));
-  if(!sil)
-    return false;
-  int idNames(0);
-  vtkAbstractArray *verticesNames(sil->GetVertexData()->GetAbstractArray("Names",idNames));
-  vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
-  if(!verticesNames2)
-    return false;
-  for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
-    {
-      vtkStdString &st(verticesNames2->GetValue(i));
-      if(st=="MeshesFamsGrps")
-        return true;
-    }
-  return false;
-}
-
-const char* vtkExtractGroup::GetGrpStart()
-{
-  return ExtractGroupGrp::START;
-}
-
-const char* vtkExtractGroup::GetFamStart()
-{
-  return ExtractGroupFam::START;
-}
-
-const char *vtkExtractGroup::vtkExtractGroupInternal::getMeshName() const
-{
-  return this->_mesh_name.c_str();
-}
-
-void vtkExtractGroup::vtkExtractGroupInternal::loadFrom(vtkMutableDirectedGraph *sil)
-{
-  std::vector<ExtractGroupGrp> oldGrps(_groups); _groups.clear();
-  std::vector<ExtractGroupFam> oldFams(_fams); _fams.clear();
-  int idNames(0);
-  vtkAbstractArray *verticesNames(sil->GetVertexData()->GetAbstractArray("Names",idNames));
-  vtkStringArray *verticesNames2(vtkStringArray::SafeDownCast(verticesNames));
-  vtkIdType id0;
-  bool found(false);
-  for(int i=0;i<verticesNames2->GetNumberOfValues();i++)
-    {
-      vtkStdString &st(verticesNames2->GetValue(i));
-      if(st=="MeshesFamsGrps")
-        {
-          id0=i;
-          found=true;
-        }
-    }
-  if(!found)
-    throw INTERP_KERNEL::Exception("There is an internal error ! The tree on server side has not the expected look !");
-  vtkAdjacentVertexIterator *it0(vtkAdjacentVertexIterator::New());
-  sil->GetAdjacentVertices(id0,it0);
-  int kk(0),ll(0);
-  while(it0->HasNext())
-    {
-      vtkIdType id1(it0->Next());
-      std::string meshName((const char *)verticesNames2->GetValue(id1));
-      this->_mesh_name=meshName;
-      vtkAdjacentVertexIterator *it1(vtkAdjacentVertexIterator::New());
-      sil->GetAdjacentVertices(id1,it1);
-      vtkIdType idZeGrps(it1->Next());//zeGroups
-      vtkAdjacentVertexIterator *itGrps(vtkAdjacentVertexIterator::New());
-      sil->GetAdjacentVertices(idZeGrps,itGrps);
-      while(itGrps->HasNext())
-        {
-          vtkIdType idg(itGrps->Next());
-          ExtractGroupGrp grp((const char *)verticesNames2->GetValue(idg));
-          vtkAdjacentVertexIterator *itGrps2(vtkAdjacentVertexIterator::New());
-          sil->GetAdjacentVertices(idg,itGrps2);
-          std::vector<std::string> famsOnGroup;
-          while(itGrps2->HasNext())
-            {
-              vtkIdType idgf(itGrps2->Next());
-              famsOnGroup.push_back(std::string((const char *)verticesNames2->GetValue(idgf)));
-            }
-          grp.setFamilies(famsOnGroup);
-          itGrps2->Delete();
-          _groups.push_back(grp);
-        }
-      itGrps->Delete();
-      vtkIdType idZeFams(it1->Next());//zeFams
-      it1->Delete();
-      vtkAdjacentVertexIterator *itFams(vtkAdjacentVertexIterator::New());
-      sil->GetAdjacentVertices(idZeFams,itFams);
-      while(itFams->HasNext())
-        {
-          vtkIdType idf(itFams->Next());
-          ExtractGroupFam fam((const char *)verticesNames2->GetValue(idf));
-          _fams.push_back(fam);
-        }
-      itFams->Delete();
-    }
-  it0->Delete();
-  //
-  std::size_t szg(_groups.size()),szf(_fams.size());
-  if(szg==oldGrps.size() && szf==oldFams.size())
-    {
-      bool isSame(true);
-      for(std::size_t i=0;i<szg && isSame;i++)
-        isSame=_groups[i].isSameAs(oldGrps[i]);
-      for(std::size_t i=0;i<szf && isSame;i++)
-        isSame=_fams[i].isSameAs(oldFams[i]);
-      if(isSame)
-        {
-          for(std::size_t i=0;i<szg;i++)
-            _groups[i].cpyStatusFrom(oldGrps[i]);
-          for(std::size_t i=0;i<szf;i++)
-            _fams[i].cpyStatusFrom(oldFams[i]);
-        }
-    }
-}
-
-int vtkExtractGroup::vtkExtractGroupInternal::getNumberOfEntries() const
-{
-  std::size_t sz0(_groups.size()),sz1(_fams.size());
-  return (int)(sz0+sz1);
-}
-
-const char *vtkExtractGroup::vtkExtractGroupInternal::getKeyOfEntry(int i) const
-{
-  int sz0((int)_groups.size());
-  if(i>=0 && i<sz0)
-    return _groups[i].getKeyOfEntry();
-  else
-    return _fams[i-sz0].getKeyOfEntry();
-}
-
-bool vtkExtractGroup::vtkExtractGroupInternal::getStatusOfEntryStr(const char *entry) const
-{
-  const ExtractGroupStatus& elt(getEntry(entry));
-  return elt.getStatus();
-}
-
-void vtkExtractGroup::vtkExtractGroupInternal::setStatusOfEntryStr(const char *entry, bool status)
-{
-  _selection.emplace_back(entry,status);
-}
-
-const ExtractGroupStatus& vtkExtractGroup::vtkExtractGroupInternal::getEntry(const char *entry) const
-{
-  std::string entryCpp(entry);
-  for(std::vector<ExtractGroupGrp>::const_iterator it0=_groups.begin();it0!=_groups.end();it0++)
-    if(entryCpp==(*it0).getKeyOfEntry())
-      return *it0;
-  for(std::vector<ExtractGroupFam>::const_iterator it0=_fams.begin();it0!=_fams.end();it0++)
-    if(entryCpp==(*it0).getKeyOfEntry())
-      return *it0;
-  std::ostringstream oss; oss << "vtkExtractGroupInternal::getEntry : no such entry \"" << entry << "\"!";
-  throw INTERP_KERNEL::Exception(oss.str().c_str());
-}
-
-ExtractGroupStatus& vtkExtractGroup::vtkExtractGroupInternal::getEntry(const char *entry)
-{
-  std::string entryCpp(entry);
-  for(std::vector<ExtractGroupGrp>::iterator it0=_groups.begin();it0!=_groups.end();it0++)
-    if(entryCpp==(*it0).getKeyOfEntry())
-      return *it0;
-  for(std::vector<ExtractGroupFam>::iterator it0=_fams.begin();it0!=_fams.end();it0++)
-    if(entryCpp==(*it0).getKeyOfEntry())
-      return *it0;
-  std::ostringstream oss; oss << "vtkExtractGroupInternal::getEntry : no such entry \"" << entry << "\"!";
-  throw INTERP_KERNEL::Exception(oss.str().c_str());
-}
-
-void vtkExtractGroup::vtkExtractGroupInternal::printMySelf(std::ostream& os) const
-{
-  os << "Groups :" << std::endl;
-  for(std::vector<ExtractGroupGrp>::const_iterator it0=_groups.begin();it0!=_groups.end();it0++)
-    (*it0).printMySelf(os);
-  os << "Families :" << std::endl;
-  for(std::vector<ExtractGroupFam>::const_iterator it0=_fams.begin();it0!=_fams.end();it0++)
-    (*it0).printMySelf(os);
-}
-
-int vtkExtractGroup::vtkExtractGroupInternal::getIdOfFamily(const std::string& famName) const
-{
-  for(std::vector<ExtractGroupFam>::const_iterator it=_fams.begin();it!=_fams.end();it++)
-    {
-      if((*it).getName()==famName)
-        return (*it).getId();
-    }
-  return std::numeric_limits<int>::max();
-}
-
-ExtractGroupFam::ExtractGroupFam(const char *name):ExtractGroupStatus(name),_id(0)
-{
-  std::size_t pos(_name.find(MEDFileFieldRepresentationLeavesArrays::ZE_SEP));
-  std::string name0(_name.substr(0,pos)),name1(_name.substr(pos+strlen(MEDFileFieldRepresentationLeavesArrays::ZE_SEP)));
-  std::istringstream iss(name1);
-  iss >> _id;
-  std::ostringstream oss; oss << START << name; _ze_key_name=oss.str(); _name=name0;
-}
-
-bool ExtractGroupFam::isSameAs(const ExtractGroupFam& other) const
-{
-  bool ret(ExtractGroupStatus::isSameAs(other));
-  if(ret)
-    return _id==other._id;
-  else
-    return false;
-}
-
-void ExtractGroupFam::printMySelf(std::ostream& os) const
-{
-  os << "      -" << _ze_key_name << " famName : \"" << _name << "\" id : " << _id << " (";
-  if(_status)
-    os << "X";
-  else
-    os << " ";
-  os << ")" << std::endl;
-}
-
-void ExtractGroupFam::fillIdsToKeep(std::set<int>& s) const
-{
-  s.insert(_id);
-}
-
-std::set<int> vtkExtractGroup::vtkExtractGroupInternal::getIdsToKeep() const
-{
-  for(auto it: _selection)
-    {
-      const ExtractGroupStatus& elt(getEntry(it.first.c_str()));
-      elt.setStatus(it.second);
-    }
-  std::map<std::string,int> m(this->computeFamStrIdMap());
-  std::set<int> s;
-  for(std::vector<ExtractGroupGrp>::const_iterator it0=_groups.begin();it0!=_groups.end();it0++)
-    {
-      if((*it0).getStatus())
-        {
-          const std::vector<std::string>& fams((*it0).getFamiliesLyingOn());
-          for(std::vector<std::string>::const_iterator it1=fams.begin();it1!=fams.end();it1++)
-            {
-              std::map<std::string,int>::iterator it2(m.find((*it1)));
-              if(it2!=m.end())
-                s.insert((*it2).second);
-            }
-        }
-     }
-  for(std::vector<ExtractGroupFam>::const_iterator it0=_fams.begin();it0!=_fams.end();it0++)
-    if((*it0).getStatus())
-      (*it0).fillIdsToKeep(s);
-  return s;
-}
-
-void vtkExtractGroup::vtkExtractGroupInternal::clearSelection() const
-{
-  _selection.clear();
-  for(auto it : _groups)
-    it.resetStatus();
-  for(auto it : _fams)
-    it.resetStatus();
-}
-
-std::map<std::string,int> vtkExtractGroup::vtkExtractGroupInternal::computeFamStrIdMap() const
-{
-  std::map<std::string,int> ret;
-  for(std::vector<ExtractGroupFam>::const_iterator it0=_fams.begin();it0!=_fams.end();it0++)
-    ret[(*it0).getName()]=(*it0).getId();
-  return ret;
-}
-
 ////////////////////
 
 vtkExtractGroup::vtkExtractGroup():SIL(NULL),Internal(new vtkExtractGroupInternal),InsideOut(0)
@@ -463,16 +93,16 @@ void vtkExtractGroup::SetInsideOut(int val)
     }
 }
 
-int vtkExtractGroup::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
+int vtkExtractGroup::RequestInformation(vtkInformation * /*request*/, vtkInformationVector **inputVector, vtkInformationVector */*outputVector*/)
 {
 //  vtkUnstructuredGridAlgorithm::RequestInformation(request,inputVector,outputVector);
   try
     {
 //      std::cerr << "########################################## vtkExtractGroup::RequestInformation ##########################################" << std::endl;
 //      request->Print(cout);
-      vtkInformation *outInfo(outputVector->GetInformationObject(0));
+      //vtkInformation *outInfo(outputVector->GetInformationObject(0)); // todo: unused
       vtkInformation *inputInfo(inputVector[0]->GetInformationObject(0));
-      if(!vtkExtractGroup::vtkExtractGroupInternal::IsInformationOK(inputInfo))
+      if(!ExtractGroupInternal::IndependantIsInformationOK(vtkMEDReader::META_DATA(),inputInfo))
         {
         vtkErrorMacro("No SIL Data available ! The source of this filter must be MEDReader !");
         return 0;
@@ -505,13 +135,13 @@ vtkDataSet *FilterFamilies(vtkSmartPointer<vtkThreshold>& thres,
                            vtkDataSet *input, const std::set<int>& idsToKeep, bool insideOut, const char *arrNameOfFamilyField,
                            const char *associationForThreshold, bool& catchAll, bool& catchSmth)
 {
-  const int VTK_DATA_ARRAY_DELETE=vtkDataArrayTemplate<double>::VTK_DATA_ARRAY_DELETE;
+  const int VTK_DATA_ARRAY_DELETE=vtkAOSDataArrayTemplate<double>::VTK_DATA_ARRAY_DELETE;
   const char ZE_SELECTION_ARR_NAME[]="@@ZeSelection@@";
   vtkDataSet *output(input->NewInstance());
   output->ShallowCopy(input);
   thres->SetInputData(output);
-  vtkDataSetAttributes *dscIn(input->GetCellData()),*dscIn2(input->GetPointData());
-  vtkDataSetAttributes *dscOut(output->GetCellData()),*dscOut2(output->GetPointData());
+  //vtkDataSetAttributes *dscIn(input->GetCellData()),*dscIn2(input->GetPointData()); // todo: unused
+  //vtkDataSetAttributes *dscOut(output->GetCellData()),*dscOut2(output->GetPointData()); // todo: unused
   //
   double vMin(insideOut==0?1.:0.),vMax(insideOut==0?2.:1.);
   thres->ThresholdBetween(vMin,vMax);
@@ -522,7 +152,7 @@ vtkDataSet *FilterFamilies(vtkSmartPointer<vtkThreshold>& thres,
   if(!da)
     return 0;
   std::string daName(da->GetName());
-  typedef MEDFileVTKTraits<mcIdType>::VtkType vtkMCIdTypeArray;
+  typedef MEDFileVTKTraits<MEDCoupling::mcIdType>::VtkType vtkMCIdTypeArray;
   vtkMCIdTypeArray *dai(vtkMCIdTypeArray::SafeDownCast(da));
   if(daName!=arrNameOfFamilyField || !dai)
     return 0;
@@ -533,7 +163,7 @@ vtkDataSet *FilterFamilies(vtkSmartPointer<vtkThreshold>& thres,
   zeSelection->SetNumberOfComponents(1);
   char *pt(new char[nbOfTuples]);
   zeSelection->SetArray(pt,nbOfTuples,0,VTK_DATA_ARRAY_DELETE);
-  const mcIdType *inPtr(dai->GetPointer(0));
+  const MEDCoupling::mcIdType *inPtr(dai->GetPointer(0));
   std::fill(pt,pt+nbOfTuples,0);
   catchAll=true; catchSmth=false;
   std::vector<bool> pt2(nbOfTuples,false);
@@ -584,7 +214,7 @@ public:
 private:
   vtkDataSet *_ds;
 };
-int vtkExtractGroup::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
+int vtkExtractGroup::RequestData(vtkInformation * /*request*/, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
 {
   try
     {
@@ -603,7 +233,7 @@ int vtkExtractGroup::RequestData(vtkInformation *request, vtkInformationVector *
           return 0;
         }
       vtkDataSet *input(vtkDataSet::SafeDownCast(inputMB->GetBlock(0)));
-      vtkInformation *info(input->GetInformation());
+      //vtkInformation *info(input->GetInformation()); // todo: unused
       vtkInformation *outInfo(outputVector->GetInformationObject(0));
       vtkMultiBlockDataSet *output(vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
       std::set<int> idsToKeep(this->Internal->getIdsToKeep());
@@ -699,6 +329,16 @@ int vtkExtractGroup::GetSILUpdateStamp()
   return (int)this->SILTime;
 }
 
+const char* vtkExtractGroup::GetGrpStart()
+{
+  return ExtractGroupGrp::START;
+}
+
+const char* vtkExtractGroup::GetFamStart()
+{
+  return ExtractGroupFam::START;
+}
+
 void vtkExtractGroup::PrintSelf(ostream& os, vtkIndent indent)
 {
   this->Superclass::PrintSelf(os, indent);