Browse Source

Merge pull request #434 from vovythevov/AddLocationToVTKModel

Add location to ctkVTKDataSetModel
Julien Finet 11 years ago
parent
commit
c30b6641a0

+ 61 - 19
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKDataSetModelTest1.cpp

@@ -21,6 +21,7 @@
 // Qt includes
 #include <QApplication>
 #include <QComboBox>
+#include <QMap>
 #include <QTimer>
 #include <QDebug>
 
@@ -28,6 +29,7 @@
 #include "ctkVTKDataSetModel.h"
 
 // VTK includes
+#include <vtkAssignAttribute.h>
 #include <vtkCellData.h>
 #include <vtkFloatArray.h>
 #include <vtkIntArray.h>
@@ -43,7 +45,10 @@
 namespace
 {
 //-----------------------------------------------------------------------------
-bool checkItems(int line, const QList<vtkAbstractArray*>& expectedAttributeArrays, ctkVTKDataSetModel* dataSetModel)
+bool checkItems(int line,
+                const QList<vtkAbstractArray*>& expectedAttributeArrays,
+                ctkVTKDataSetModel* dataSetModel,
+                QMap<vtkAbstractArray*, int> locations)
 {
   foreach(vtkAbstractArray* expectedDataArray, expectedAttributeArrays)
     {
@@ -64,22 +69,35 @@ bool checkItems(int line, const QList<vtkAbstractArray*>& expectedAttributeArray
                    "\tCurrent: " << currentDataArray << "\n";
       return false;
       }
+
+    int loc = dataSetModel->locationFromItem(items.at(0));
+    if (locations[expectedDataArray] != loc)
+      {
+      std::cerr << "Line " << line << " - Problem with model - Incorrect location associated with item name:" << expectedDataArray->GetName() << "\n"
+                   "\tExpected: " << locations[expectedDataArray] << "\n"
+                   "\tCurrent: " << loc << "\n";
+      return false;
+      }
     }
   return true;
 }
-}
+
+} // end namespace
 
 //-----------------------------------------------------------------------------
 int ctkVTKDataSetModelTest1(int argc, char * argv [] )
 {
   QApplication app(argc, argv);
 
+  QMap<vtkAbstractArray*, int> locations;
+
   vtkNew<vtkPolyData> dataSet;
 
   vtkNew<vtkIntArray> ints;
   {
     ints->SetName("Ints");
     int added = dataSet->GetPointData()->AddArray(ints.GetPointer());
+    locations[ints.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Ints array";
@@ -91,6 +109,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
   {
     floats->SetName("Floats");
     int added = dataSet->GetCellData()->AddArray(floats.GetPointer());
+    locations[floats.GetPointer()] = vtkAssignAttribute::CELL_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Floats array";
@@ -102,6 +121,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
   {
     strings->SetName("Strings");
     int added = dataSet->GetCellData()->AddArray(strings.GetPointer());
+    locations[strings.GetPointer()] = vtkAssignAttribute::CELL_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Strings array";
@@ -114,6 +134,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
     scalars->SetName("Scalars");
     scalars->SetNumberOfComponents(0);
     int added = dataSet->GetPointData()->SetScalars(scalars.GetPointer());
+    locations[scalars.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Scalars array";
@@ -126,6 +147,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
     vectors->SetNumberOfComponents(3);
     vectors->SetName("Vectors");
     int added = dataSet->GetPointData()->SetVectors(vectors.GetPointer());
+    locations[vectors.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Vectors array";
@@ -138,6 +160,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
     normals->SetNumberOfComponents(3);
     normals->SetName("Normals");
     int added = dataSet->GetPointData()->SetNormals(normals.GetPointer());
+    locations[normals.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Normals array";
@@ -150,6 +173,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
     tcoords->SetNumberOfComponents(3);
     tcoords->SetName("Tcoords");
     int added = dataSet->GetPointData()->SetTCoords(tcoords.GetPointer());
+    locations[tcoords.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Tcoords array";
@@ -162,6 +186,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
     tensors->SetNumberOfComponents(9);
     tensors->SetName("Tensors");
     int added = dataSet->GetPointData()->SetTensors(tensors.GetPointer());
+    locations[tensors.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add Tensors array";
@@ -174,6 +199,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
     globalids->SetNumberOfComponents(1);
     globalids->SetName("GlobalIDs");
     int added = dataSet->GetPointData()->SetGlobalIds(globalids.GetPointer());
+    locations[globalids.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add GlobalIDs array";
@@ -186,6 +212,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
     pedigreeids->SetNumberOfComponents(1);
     pedigreeids->SetName("PedigreeIDs");
     int added = dataSet->GetPointData()->SetPedigreeIds(pedigreeids.GetPointer());
+    locations[pedigreeids.GetPointer()] = vtkAssignAttribute::POINT_DATA;
     if (added == -1)
       {
       std::cerr << "Line " << __LINE__ << " - Failed to add PedigreeIDs array";
@@ -199,97 +226,112 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
   QList<vtkAbstractArray*> notAttributeArrays;
   notAttributeArrays << ints.GetPointer() << floats.GetPointer();
 
-  if (!checkItems(__LINE__, notAttributeArrays, &dataSetModel))
+  if (!checkItems(__LINE__, notAttributeArrays, &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::ScalarsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << scalars.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << scalars.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::ScalarsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << scalars.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << scalars.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::VectorsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << vectors.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << vectors.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::VectorsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << vectors.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << vectors.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::VectorsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << vectors.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << vectors.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NormalsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << normals.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << normals.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::NormalsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << normals.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << normals.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::TCoordsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << tcoords.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << tcoords.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::TCoordsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << tcoords.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << tcoords.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::TensorsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << tensors.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << tensors.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::TensorsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << tensors.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << tensors.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::GlobalIDsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << globalids.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << globalids.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::GlobalIDsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << globalids.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << globalids.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::PedigreeIDsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << pedigreeids.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << pedigreeids.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
 
   dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::PedigreeIDsAttribute);
-  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << pedigreeids.GetPointer(), &dataSetModel))
+  if (!checkItems(__LINE__, QList<vtkAbstractArray*>() << notAttributeArrays << pedigreeids.GetPointer(),
+                  &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }
@@ -299,7 +341,7 @@ int ctkVTKDataSetModelTest1(int argc, char * argv [] )
                   << scalars.GetPointer() << vectors.GetPointer()
                   << normals.GetPointer() << tcoords.GetPointer()
                   << tensors.GetPointer() << globalids.GetPointer()
-                  << pedigreeids.GetPointer(), &dataSetModel))
+                  << pedigreeids.GetPointer(), &dataSetModel, locations))
     {
     return EXIT_FAILURE;
     }

+ 15 - 0
Libs/Visualization/VTK/Widgets/ctkVTKDataSetArrayComboBox.cpp

@@ -42,6 +42,7 @@ public:
   int indexFromArray(vtkAbstractArray* dataArray)const;
   vtkAbstractArray* arrayFromIndex(int index)const;
   QString arrayNameFromIndex(int index)const;
+  int locationFromIndex(int index)const;
 };
 
 // --------------------------------------------------------------------------
@@ -97,6 +98,13 @@ QString ctkVTKDataSetArrayComboBoxPrivate::arrayNameFromIndex(int index)const
 }
 
 // --------------------------------------------------------------------------
+int ctkVTKDataSetArrayComboBoxPrivate::locationFromIndex(int index)const
+{
+  return this->dataSetModel()->locationFromIndex(
+    this->dataSetModel()->index(index, 0));
+}
+
+// --------------------------------------------------------------------------
 // ctkVTKDataSetArrayComboBox methods
 
 // --------------------------------------------------------------------------
@@ -128,6 +136,13 @@ QString ctkVTKDataSetArrayComboBox::currentArrayName()const
 }
 
 // --------------------------------------------------------------------------
+int ctkVTKDataSetArrayComboBox::currentArrayLocation()const
+{
+  Q_D(const ctkVTKDataSetArrayComboBox);
+  return d->locationFromIndex(this->currentIndex());
+}
+
+// --------------------------------------------------------------------------
 void ctkVTKDataSetArrayComboBox::setDataSet(vtkDataSet* dataSet)
 {
   Q_D(ctkVTKDataSetArrayComboBox);

+ 6 - 0
Libs/Visualization/VTK/Widgets/ctkVTKDataSetArrayComboBox.h

@@ -53,6 +53,11 @@ public:
   QString currentArrayName()const;
   vtkDataSet* dataSet()const;
 
+  /// Return the current array location, i.e. whether it's a point data
+  /// array or a cell data array.
+  /// \sa currentArrayChanged(int), ctkVTKDataSetModel::locationFromItem()
+  int currentArrayLocation()const;
+
   ctkVTKDataSetModel::AttributeTypes attributeTypes()const;
   void setAttributeTypes(const ctkVTKDataSetModel::AttributeTypes& attributeTypes);
 
@@ -72,6 +77,7 @@ public Q_SLOTS:
 Q_SIGNALS:
   void currentArrayChanged(vtkAbstractArray*);
   void currentArrayChanged(const QString& name);
+
 protected Q_SLOTS:
   void onCurrentIndexChanged(int);
 protected:

+ 32 - 11
Libs/Visualization/VTK/Widgets/ctkVTKDataSetModel.cpp

@@ -26,6 +26,7 @@
 
 // VTK includes
 #include <vtkAbstractArray.h>
+#include <vtkAssignAttribute.h>
 #include <vtkCellData.h>
 #include <vtkDataSet.h>
 #include <vtkPointData.h>
@@ -212,6 +213,16 @@ vtkAbstractArray* ctkVTKDataSetModel::arrayFromItem(QStandardItem* arrayItem)con
 }
 
 //------------------------------------------------------------------------------
+int ctkVTKDataSetModel::locationFromItem(QStandardItem* arrayItem)const
+{
+  if (arrayItem == 0 || arrayItem == this->invisibleRootItem())
+    {
+    return -1;
+    }
+  return arrayItem->data(ctkVTK::LocationRole).toInt();
+}
+
+//------------------------------------------------------------------------------
 QStandardItem* ctkVTKDataSetModel::itemFromArray(vtkAbstractArray* array, int column)const
 {
   if (array == 0)
@@ -283,23 +294,28 @@ void ctkVTKDataSetModel::populateDataSet()
   Q_D(ctkVTKDataSetModel);
   Q_ASSERT(d->DataSet);
 
-  QList<vtkAbstractArray*> attributeArrays;
-  attributeArrays << ctkVTKDataSetModelPrivate::attributeArrayToInsert(d->AttributeType, d->DataSet->GetPointData());
-  attributeArrays << ctkVTKDataSetModelPrivate::attributeArrayToInsert(d->AttributeType, d->DataSet->GetCellData());
-  foreach(vtkAbstractArray* attributeArray, attributeArrays)
+  foreach(vtkAbstractArray* attributeArray,
+    ctkVTKDataSetModelPrivate::attributeArrayToInsert(d->AttributeType, d->DataSet->GetPointData()))
+    {
+    this->insertArray(attributeArray, vtkAssignAttribute::POINT_DATA);
+    }
+
+  foreach(vtkAbstractArray* attributeArray,
+    ctkVTKDataSetModelPrivate::attributeArrayToInsert(d->AttributeType, d->DataSet->GetCellData()))
     {
-    this->insertArray(attributeArray);
+    this->insertArray(attributeArray, vtkAssignAttribute::CELL_DATA);
     }
 }
 
 //------------------------------------------------------------------------------
-void ctkVTKDataSetModel::insertArray(vtkAbstractArray* array)
+void ctkVTKDataSetModel::insertArray(vtkAbstractArray* array, int location)
 {
-  this->insertArray(array, this->rowCount());
+  this->insertArray(array, location, this->rowCount());
 }
 
 //------------------------------------------------------------------------------
-void ctkVTKDataSetModel::insertArray(vtkAbstractArray* array, int row)
+void ctkVTKDataSetModel
+::insertArray(vtkAbstractArray* array, int location, int row)
 {
   Q_D(ctkVTKDataSetModel);
   Q_ASSERT(vtkAbstractArray::SafeDownCast(array));
@@ -308,7 +324,7 @@ void ctkVTKDataSetModel::insertArray(vtkAbstractArray* array, int row)
   for (int i= 0; i < this->columnCount(); ++i)
     {
     QStandardItem* newArrayItem = new QStandardItem();
-    this->updateItemFromArray(newArrayItem, array, i);
+    this->updateItemFromArray(newArrayItem, array, location, i);
     items.append(newArrayItem);
     }
   this->insertRow(row,items);
@@ -321,9 +337,13 @@ void ctkVTKDataSetModel::insertArray(vtkAbstractArray* array, int row)
 }
 
 //------------------------------------------------------------------------------
-void ctkVTKDataSetModel::updateItemFromArray(QStandardItem* item, vtkAbstractArray* array, int column)
+void ctkVTKDataSetModel::updateItemFromArray(QStandardItem* item,
+                                             vtkAbstractArray* array,
+                                             int location,
+                                             int column)
 {
   item->setData(QVariant::fromValue(reinterpret_cast<long long>(array)), ctkVTK::PointerRole);
+  item->setData(location, ctkVTK::LocationRole);
   switch (column)
     {
     case 0:
@@ -394,7 +414,8 @@ void ctkVTKDataSetModel::onArrayModified(vtkObject* modifiedArray)
   foreach (QModelIndex index, arrayIndexes)
     {
     QStandardItem* item = this->itemFromIndex(index);
-    this->updateItemFromArray(item, array, item->column());
+    this->updateItemFromArray(
+      item, array, item->data(ctkVTK::LocationRole).toInt(), item->column());
     }
 }
 

+ 23 - 4
Libs/Visualization/VTK/Widgets/ctkVTKDataSetModel.h

@@ -37,7 +37,8 @@ class vtkAbstractArray;
 namespace ctkVTK
 {
  enum ItemDataRole {
-   PointerRole = Qt::UserRole + 1
+   PointerRole = Qt::UserRole + 1,
+   LocationRole,
  };
 };
 
@@ -87,7 +88,19 @@ public:
   /// Return the vtkAbstractArray associated to the index.
   /// 0 if the index doesn't contain a vtkAbstractArray
   inline vtkAbstractArray* arrayFromIndex(const QModelIndex& arrayIndex)const;
+
+  /// Return the location from a given item. Fails and returns -1 if either
+  /// the given index points to a null item or an invisible item.
+  /// \sa locationFromItem()
+  inline int locationFromIndex(const QModelIndex& arrayIndex)const;
+
   vtkAbstractArray* arrayFromItem(QStandardItem* nodeItem)const;
+
+  /// Return the location from a given item. Fails and returns -1 if either
+  /// the given item is null or should be invisible).
+  /// \sa locationFromIndex(), invisibleRootItem()
+  int locationFromItem(QStandardItem* nodeItem)const;
+
   inline QModelIndex indexFromArray(vtkAbstractArray* array, int column = 0)const;
   QStandardItem* itemFromArray(vtkAbstractArray* array, int column = 0)const;
   QModelIndexList indexes(vtkAbstractArray* array)const;
@@ -101,9 +114,9 @@ protected:
 
   ctkVTKDataSetModel(ctkVTKDataSetModelPrivate* pimpl, QObject *parent=0);
 
-  virtual void insertArray(vtkAbstractArray* array);
-  virtual void insertArray(vtkAbstractArray* array, int row);
-  virtual void updateItemFromArray(QStandardItem* item, vtkAbstractArray* array, int column);
+  virtual void insertArray(vtkAbstractArray* array, int location);
+  virtual void insertArray(vtkAbstractArray* array, int location, int row);
+  virtual void updateItemFromArray(QStandardItem* item, vtkAbstractArray* array, int location, int column);
   virtual void updateArrayFromItem(vtkAbstractArray* array, QStandardItem* item);
   virtual void updateDataSet();
   virtual void populateDataSet();
@@ -124,6 +137,12 @@ vtkAbstractArray* ctkVTKDataSetModel::arrayFromIndex(const QModelIndex &nodeInde
 }
 
 // -----------------------------------------------------------------------------
+int ctkVTKDataSetModel::locationFromIndex(const QModelIndex &nodeIndex)const
+{
+  return this->locationFromItem(this->itemFromIndex(nodeIndex));
+}
+
+// -----------------------------------------------------------------------------
 QModelIndex ctkVTKDataSetModel::indexFromArray(vtkAbstractArray* array, int column)const
 {
   QStandardItem* item = this->itemFromArray(array, column);