[cig-commits] r13760 - cs/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Dec 17 02:33:23 PST 2008


Author: luis
Date: 2008-12-17 02:33:23 -0800 (Wed, 17 Dec 2008)
New Revision: 13760

Modified:
   cs/cigma/trunk/src/io_vtk_reader.cpp
   cs/cigma/trunk/src/io_vtk_reader.h
Log:
Conforming VtkReader to the FileReader interface

Modified: cs/cigma/trunk/src/io_vtk_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_vtk_reader.cpp	2008-12-17 10:33:20 UTC (rev 13759)
+++ cs/cigma/trunk/src/io_vtk_reader.cpp	2008-12-17 10:33:23 UTC (rev 13760)
@@ -5,13 +5,13 @@
 #ifdef HAVE_VTK
 
 //
-// This is the implementation of VtkReader when the preprocessor
-// variable HAVE_VTK has been defined.
+// This is the implementation of VtkReader when HAVE_VTK has been defined
 //
 
 #include "Filesystem.h"
 #include "nc_array.h"
 #include "eb_array.h"
+#include "core_array.h"
 
 #include "vtkUnstructuredGridReader.h"
 #include "vtkStructuredGridReader.h"
@@ -73,7 +73,8 @@
 
         if (legacyReader)
         {
-            legacyReader->Update();
+            this->filename = string(filename);
+            this->legacyReader->Update();
             this->dataset = legacyReader->GetOutput();
             this->dataset->Update();
         }
@@ -89,7 +90,8 @@
 
         if (xmlReader)
         {
-            xmlReader->GetPointDataArraySelection()->EnableAllArrays();
+            this->filename = string(filename);
+            this->xmlReader->GetPointDataArraySelection()->EnableAllArrays();
             this->dataset = xmlReader->GetOutputAsDataSet();
             this->dataset->Update();
         }
@@ -130,6 +132,8 @@
         xmlReader = 0;
     }
 
+    filename = "";
+
     return 0;
 }
 
@@ -144,17 +148,69 @@
 int VtkReader::getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd)
 {
     TRI_LOG_STR("VtkReader::getCoordinates()");
-    return vtk_read_coordinates<double>(this->dataset, loc, coordinates, nno, nsd);
+    return vtk_read_coordinates<double>(this->dataset, coordinates, nno, nsd);
 }
 
 
 int VtkReader::getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs)
 {
     TRI_LOG_STR("VtkReader::getConnectivity()");
-    return vtk_read_connectivity<int>(this->dataset, loc, connectivity, nel, ndofs);
+    return vtk_read_connectivity<int>(this->dataset, connectivity, nel, ndofs);
 }
 
 
+void VtkReader::getIntArray(const char *loc, cigma::array<int>& A)
+{
+    TRI_LOG_STR("VtkReader::getIntArray");
+    int status = vtk_read_pointdata<int>(this->dataset, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read array '" << loc << "' from VTK file '" << filename << "'" << std::ends;
+        throw cigma::Exception("VtkReader::getIntArray", stream.str());
+    }
+}
+
+
+void VtkReader::getLongArray(const char *loc, cigma::array<long>& A)
+{
+    TRI_LOG_STR("VtkReader::getLongArray");
+    int status = vtk_read_pointdata<long>(this->dataset, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read array '" << loc << "' from VTK file '" << filename << "'" << std::ends;
+        throw cigma::Exception("VtkReader::getLongArray", stream.str());
+    }
+}
+
+
+void VtkReader::getFloatArray(const char *loc, cigma::array<float>& A)
+{
+    TRI_LOG_STR("VtkReader::getFloatArray");
+    int status = vtk_read_pointdata<float>(this->dataset, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read array '" << loc << "' from VTK file '" << filename << "'" << std::ends;
+        throw cigma::Exception("VtkReader::getFloatArray", stream.str());
+    }
+}
+
+
+void VtkReader::getDoubleArray(const char *loc, cigma::array<double>& A)
+{
+    TRI_LOG_STR("VtkReader::getDoubleArray");
+    int status = vtk_read_pointdata<double>(this->dataset, loc, &(A._data), &(A.npts), &(A.ndim));
+    if (status < 0)
+    {
+        std::ostringstream stream;
+        stream << "Could not read array '" << loc << "' from VTK file '" << filename << "'" << std::ends;
+        throw cigma::Exception("VtkReader::getDoubleArray", stream.str());
+    }
+}
+
+
 Cell::type VtkReader::getCellType(int id) const
 {
     int vtkCellType = VTK_EMPTY_CELL;
@@ -186,82 +242,91 @@
 }
 
 
-shared_ptr<MeshPart> VtkReader::getMesh()
+shared_ptr<NodeCoordinates> VtkReader::getNodeCoordinates(const char *loc)
 {
-    TRI_LOG_STR("VtkReader::getMesh()");
-
-    shared_ptr<MeshPart> mesh;
-    int status;
-
-    int npts, ndim;
-    double *coords = 0;
-    status = this->getCoordinates("", &coords, &npts, &ndim);
+    TRI_LOG_STR("VtkReader::getNodeCoordinates()");
+    shared_ptr<nc_array> nc(new nc_array);
+    int status = this->getCoordinates(0, &(nc->coords), &(nc->nno), &(nc->nsd));
     if (status < 0)
     {
-        if (coords) { delete [] coords; }
-        return mesh;
+        std::ostringstream stream;
+        stream << "Could not read node coordinates from VTK file '"
+               << filename << "'" << std::ends;
+        throw cigma::Exception("VtkReader::getNodeCoordinates", stream.str());
     }
-    shared_ptr<nc_array> nc(new nc_array);
-    nc->nno = npts;
-    nc->nsd = ndim;
-    nc->coords = coords;
+    return nc;
+}
 
-    int nel, ndofs;
-    int *connect = 0;
-    status = this->getConnectivity("", &connect, &nel, &ndofs);
+
+shared_ptr<ElementBlock> VtkReader::getElementBlock(const char *loc)
+{
+    TRI_LOG_STR("VtkReader::getElementBlock()");
+    shared_ptr<eb_array> eb(new eb_array);
+    int status = this->getConnectivity(0, &(eb->connect), &(eb->nel), &(eb->ndofs));
     if (status < 0)
     {
-        if (connect) { delete [] connect; }
-        return mesh;
+        std::ostringstream stream;
+        stream << "Could not read element block from VTK file '"
+               << filename << "'" << std::ends;
+        throw cigma::Exception("VtkReader::getElementBlock", stream.str());
     }
-    shared_ptr<eb_array> eb(new eb_array);
-    eb->nel = nel;
-    eb->ndofs = ndofs;
-    eb->connect = connect;
+    return eb;
+}
 
 
-    mesh = shared_ptr<MeshPart>(new MeshPart);
-    mesh->coords = nc;
-    mesh->connect = eb;
+shared_ptr<MeshPart> VtkReader::getMeshPart(const char *loc)
+{
+    TRI_LOG_STR("VtkReader::getMeshPart()");
+    shared_ptr<MeshPart> mesh(new MeshPart);
+    mesh->coords    = this->getNodeCoordinates(0);
+    mesh->connect   = this->getElementBlock(0);
     mesh->cell_type = this->getCellType(0);
-
     return mesh;
 }
 
 
-shared_ptr<FE> VtkReader::getFE()
+shared_ptr<Quadrature> VtkReader::getQuadrature(const char *loc)
 {
+    TRI_LOG_STR("VtkReader::getQuadrature()");
+    shared_ptr<Quadrature> Q;
+
+    if (loc == 0)
+    {
+        Q = Quadrature::default_rule(this->getCellType(0));
+    }
+    else
+    {
+        string msg("Can't specify a quadrature rule from a VTK file");
+        throw cigma::Exception("VtkReader::getQuadrature", msg);
+    }
+
+    return Q;
+}
+
+
+shared_ptr<FE> VtkReader::getFE(const char *loc)
+{
     TRI_LOG_STR("VtkReader::getFE()");
-    Cell::type celltype = this->getCellType(0);
-    TRI_LOG(Cell::type2string(celltype));
-    shared_ptr<FE> fe(new FE(celltype));
+    shared_ptr<FE> fe(new FE(this->getCellType(0)));
     return fe;
 }
 
 
-shared_ptr<DofHandler> VtkReader::getDofs(const char *loc)
+shared_ptr<DofHandler> VtkReader::getDofHandler(const char *loc)
 {
-    TRI_LOG_STR("VtkReader::getDofs()");
+    TRI_LOG_STR("VtkReader::getDofHandler()");
     TRI_LOG(loc);
 
-    shared_ptr<DofHandler> dofs;
-    int status;
-
-    int nno, rank;
-    double *data = 0;
-    status = this->getDataset(loc, &data, &nno, &rank);
+    shared_ptr<DofHandler> dh;
+    int status = this->getDataset(loc, &(dh->dofs), &(dh->nno), &(dh->rank));
     if (status < 0)
     {
-        if (data) { delete [] data; }
-        return dofs;
+        std::ostringstream stream;
+        stream << "Could not read dataset '" << loc << "' from VTK file '"
+               << filename << "'" << std::ends;
+        throw cigma::Exception("VtkReader::getDofHandler", stream.str());
     }
-
-    dofs = shared_ptr<DofHandler>(new DofHandler);
-    dofs->nno = nno;
-    dofs->rank = rank;
-    dofs->dofs = data;
-
-    return dofs;
+    return dh;
 }
 
 
@@ -339,72 +404,24 @@
 #else
 
 //
-// Default implementation
+// Implementation of VtkReader methods when HAVE_VTK is not defined
 //
 
-
-using namespace boost;
-using namespace cigma;
-
-
-VtkReader::VtkReader()
+cigma::VtkReader::VtkReader()
 {
-    throw cigma::Exception("VtkReader", "Can't create VtkReader. Cigma is not configured with the VTK library");
+    std::string msg("Can't create VtkReader. Cigma is not configured with the VTK library.");
+    throw cigma::Exception("VtkReader", msg);
 }
 
-VtkReader::~VtkReader()
+cigma::VtkReader::~VtkReader()
 {
 }
 
-int VtkReader::open(const char *filename, const char *mode)
+cigma::Cell::type cigma::VtkReader::getCellType(int id) const
 {
-    return -1;
+    return cigma::Cell::NONE;
 }
 
-int VtkReader::close()
-{
-    return -1;
-}
-
-int VtkReader::getDataset(const char *loc, double **data, int *num, int *dim)
-{
-    return -1;
-}
-
-
-int VtkReader::getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd)
-{
-    return -1;
-}
-
-int VtkReader::getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs)
-{
-    return -1;
-}
-
-Cell::type VtkReader::getCellType(int id) const
-{
-    return Cell::NONE;
-}
-
-shared_ptr<MeshPart> VtkReader::getMesh()
-{
-    shared_ptr<MeshPart> mesh;
-    return mesh;
-}
-
-shared_ptr<FE> VtkReader::getFE()
-{
-    shared_ptr<FE> fe;
-    return fe;
-}
-
-shared_ptr<DofHandler> VtkReader::getDofs(const char *loc)
-{
-    shared_ptr<DofHandler> dofs;
-    return dofs;
-}
-
 int vtk_print_contents(const char *filename)
 {
     throw cigma::Exception("vtk_print_contents", "Error: Cigma not configured with VTK library", 1);

Modified: cs/cigma/trunk/src/io_vtk_reader.h
===================================================================
--- cs/cigma/trunk/src/io_vtk_reader.h	2008-12-17 10:33:20 UTC (rev 13759)
+++ cs/cigma/trunk/src/io_vtk_reader.h	2008-12-17 10:33:23 UTC (rev 13760)
@@ -4,29 +4,28 @@
 #include "io_file_reader.h"
 #include "io_vtk.h" // for GridType enum
 
-#include <boost/shared_ptr.hpp>
-#include "Common.h"
-#include "Cell.h"
-#include "MeshPart.h"
-#include "FE.h"
-#include "DofHandler.h"
 
-#ifdef HAVE_VTK
-#include "vtkDataSet.h"
-#include "vtkDataSetReader.h"
-#include "vtkXMLDataReader.h"
-#endif
-
-
 namespace cigma
 {
     class VtkReader;
 }
 
 
+/* XXX: keep here for now */
+int vtk_print_contents(const char *filename);
+
+
+
+#ifdef HAVE_VTK
+
+#include "vtkDataSet.h"
+#include "vtkDataSetReader.h"
+#include "vtkXMLDataReader.h"
+
 class cigma::VtkReader : public cigma::FileReader
 {
 public:
+
     VtkReader();
     ~VtkReader();
 
@@ -39,23 +38,53 @@
     int getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd);
     int getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs);
 
+    void getIntArray(const char *loc, cigma::array<int>& A);
+    void getLongArray(const char *loc, cigma::array<long>& A);
+    void getFloatArray(const char *loc, cigma::array<float>& A);
+    void getDoubleArray(const char *loc, cigma::array<double>& A);
+
+    boost::shared_ptr<NodeCoordinates> getNodeCoordinates(const char *loc);
+    boost::shared_ptr<ElementBlock> getElementBlock(const char *loc);
+    boost::shared_ptr<MeshPart> getMeshPart(const char *loc);
+    boost::shared_ptr<Quadrature> getQuadrature(const char *loc);
+    boost::shared_ptr<FE> getFE(const char *loc);
+    boost::shared_ptr<DofHandler> getDofHandler(const char *loc);
+
     Cell::type getCellType(int id) const;
 
-    boost::shared_ptr<MeshPart>     getMesh();
-    boost::shared_ptr<FE>           getFE();
-    boost::shared_ptr<DofHandler>   getDofs(const char *loc);
 
-#ifdef HAVE_VTK
 public:
+
+    std::string filename;
     vtkGridType gridType;
+
     vtkDataSetReader *legacyReader;
     vtkXMLReader *xmlReader;
     vtkDataSet *dataset;
-#endif
+
 };
 
-/* XXX: keep here for now */
-int vtk_print_contents(const char *filename);
 
 
+#else
+
+
+
+/*
+ * If HAVE_VTK is not defined, we subclass NullReader
+ */
+
+#include "io_null_reader.h"
+class cigma::VtkReader : public cigma::NullReader
+{
+    VtkReader();
+    ~VtkReader();
+    ReaderType getReaderType() { return VTK_FILE_READER; }
+    Cell::type getCellType()   { return Cell::NONE; }
+};
+
+#endif /* HAVE_VTK */
+
+
+
 #endif /* __CIGMA_VTK_READER_H__ */



More information about the CIG-COMMITS mailing list