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

luis at geodynamics.org luis at geodynamics.org
Wed Sep 10 12:50:47 PDT 2008


Author: luis
Date: 2008-09-10 12:50:46 -0700 (Wed, 10 Sep 2008)
New Revision: 12845

Added:
   cs/cigma/trunk/src/DataIO.cpp
   cs/cigma/trunk/src/DataIO.h
   cs/cigma/trunk/src/TextIO.cpp
   cs/cigma/trunk/src/TextIO.h
   cs/cigma/trunk/src/VtkIO.cpp
   cs/cigma/trunk/src/VtkIO.h
Log:
Interface for dataset I/O




Added: cs/cigma/trunk/src/DataIO.cpp
===================================================================
--- cs/cigma/trunk/src/DataIO.cpp	                        (rev 0)
+++ cs/cigma/trunk/src/DataIO.cpp	2008-09-10 19:50:46 UTC (rev 12845)
@@ -0,0 +1,4 @@
+#include "DataIO.h"
+
+cigma::DataIO::~DataIO() {}
+

Added: cs/cigma/trunk/src/DataIO.h
===================================================================
--- cs/cigma/trunk/src/DataIO.h	                        (rev 0)
+++ cs/cigma/trunk/src/DataIO.h	2008-09-10 19:50:46 UTC (rev 12845)
@@ -0,0 +1,37 @@
+#ifndef __DATAIO_H__
+#define __DATAIO_H__
+
+#include <string>
+
+namespace cigma
+{
+    class DataIO;
+}
+
+class cigma::DataIO
+{
+public:
+    DataIO() {}
+    virtual ~DataIO();
+
+public:
+    virtual void open_read(std::string filename);
+    virtual void open_write(std::string filename);
+    virtual void close();
+
+public:
+    virtual bool read_connectivity(int **connect, int *nel, int *ndofs);
+    virtual bool read_coordinates(double **coords, int *nno, int *nsd);
+    virtual bool read_dofs(double **dofs, int *nno, int *ndim);
+    virtual bool read_points(double **points, int *npts, int *ndim);
+
+public:
+    virtual void write_header();
+    virtual void write_points(double *points, int npts, int ndim);
+    virtual void write_cells(int *cells, int nel, int ndofs);
+};
+
+//----------------------------------------------------------------------------
+
+
+#endif

Added: cs/cigma/trunk/src/TextIO.cpp
===================================================================
--- cs/cigma/trunk/src/TextIO.cpp	                        (rev 0)
+++ cs/cigma/trunk/src/TextIO.cpp	2008-09-10 19:50:46 UTC (rev 12845)
@@ -0,0 +1,159 @@
+#include "TextIO.h"
+#include <cassert>
+#include <cstdlib>
+
+//----------------------------------------------------------------------------
+
+cigma::TextIO::TextIO()
+{
+    fp = NULL;
+}
+
+cigma::TextIO::~TextIO()
+{
+    close();
+}
+
+//----------------------------------------------------------------------------
+
+void cigma::TextIO::open_read(std::string filename)
+{
+    fp = (FILE *)fopen(filename.c_str(), "r");
+}
+
+void cigma::TextIO::open_write(std::string filename)
+{
+    fp = (FILE *)fopen(filename.c_str(), "w");
+}
+
+void cigma::TextIO::close()
+{
+    if (fp != NULL)
+    {
+        fclose(fp);
+    }
+}
+
+//----------------------------------------------------------------------------
+
+static bool read_dmat(FILE *fp, double **mat, int *rows, int *cols)
+{
+    int ret;
+
+    assert(fp != NULL);
+
+    ret = fscanf(fp, "%d", rows);
+    assert(ret == 1);
+    assert(*rows > 0);
+    
+    ret = fscanf(fp, "%d", cols);
+    assert(ret == 1);
+    assert(*cols > 0);
+
+    *mat = new double[(*rows) * (*cols)];
+
+    for (int i = 0; i < (*rows); i++)
+    {
+        for (int j = 0; j < (*cols); j++)
+        {
+            int n = (*rows) * i + j;
+            ret = fscanf(fp, "%lf", &((*mat)[n]));
+            assert(ret == 1);
+        }
+    }
+    
+    return true;
+}
+
+static bool write_dmat(FILE *fp, double *mat, int rows, int cols)
+{
+    assert(fp != NULL);
+
+    fprintf(fp, "%d %d\n", rows, cols);
+    for (int i = 0; i < rows; i++)
+    {
+        for (int j = 0; j < cols; j++)
+        {
+            fprintf(fp, " %g", mat[rows*i + j]);
+        }
+        printf("\n");
+    }
+
+    return true;
+}
+
+static bool write_imat(FILE *fp, int *mat, int rows, int cols)
+{
+    assert(fp != NULL);
+    fprintf(fp, "%d %d\n", rows, cols);
+    for (int i = 0; i < rows; i++)
+    {
+        for (int j = 0; j < cols; j++)
+        {
+            fprintf(fp, " %d", mat[rows*i + j]);
+        }
+        fprintf(fp, "\n");
+    }
+
+    return true;
+}
+
+//----------------------------------------------------------------------------
+
+bool cigma::TextIO::read_connectivity(int **connect, int *nel, int *ndofs)
+{
+    int ret;
+
+    ret = fscanf(fp, "%d", nel);
+    assert(ret == 1);
+    assert(*nel > 0);
+
+    ret = fscanf(fp, "%d", ndofs);
+    assert(ret == 1);
+    assert(*ndofs > 0);
+
+    *connect = new int[(*nel) * (*ndofs)];
+
+    for (int i = 0; i < (*nel); i++)
+    {
+        for (int j = 0; j < (*ndofs); j++)
+        {
+            ret = fscanf(fp, "%d", &((*connect)[(*ndofs)*i + j]));
+            assert(ret == 1);
+        }
+    }
+
+    return true;
+}
+
+bool cigma::TextIO::read_coordinates(double **coords, int *nno, int *nsd)
+{
+    return read_dmat(fp, coords, nno, nsd);
+}
+
+bool cigma::TextIO::read_dofs(double **dofs, int *nno, int *ndim)
+{
+    return read_dmat(fp, dofs, nno, ndim);
+}
+
+bool cigma::TextIO::read_points(double **points, int *npts, int *ndim)
+{
+    return read_dmat(fp, points, npts, ndim);
+}
+
+//----------------------------------------------------------------------------
+
+void cigma::TextIO::write_header()
+{
+}
+
+void cigma::TextIO::write_points(double *points, int npts, int ndim)
+{
+    write_dmat(fp, points, npts, ndim);
+}
+
+void cigma::TextIO::write_cells(int *cells, int nel, int ndofs)
+{
+    write_imat(fp, cells, nel, ndofs);
+}
+

Added: cs/cigma/trunk/src/TextIO.h
===================================================================
--- cs/cigma/trunk/src/TextIO.h	                        (rev 0)
+++ cs/cigma/trunk/src/TextIO.h	2008-09-10 19:50:46 UTC (rev 12845)
@@ -0,0 +1,41 @@
+#ifndef __TEXTREADER_H__
+#define __TEXTREADER_H__
+
+#include "DataIO.h"
+#include <cstdio>
+
+namespace cigma
+{
+    class TextIO;
+}
+
+class cigma::TextIO : public cigma::DataIO
+{
+public:
+    TextIO();
+    ~TextIO();
+
+public:
+    void open_read(std::string filename);
+    void open_write(std::string filename);
+    void close();
+
+public:
+    bool read_connectivity(int **connect, int *nel, int *ndofs);
+    bool read_coordinates(double **coords, int *nno, int *nsd);
+    bool read_dofs(double **dofs, int *nno, int *ndim);
+    bool read_points(double **points, int *npts, int *ndim);
+
+public:
+    void write_header();
+    void write_points(double *points, int npts, int ndim);
+    void write_cells(int *cells, int nel, int ndofs);
+
+private:
+    FILE *fp;
+};
+
+//----------------------------------------------------------------------------
+
+
+#endif

Added: cs/cigma/trunk/src/VtkIO.cpp
===================================================================
--- cs/cigma/trunk/src/VtkIO.cpp	                        (rev 0)
+++ cs/cigma/trunk/src/VtkIO.cpp	2008-09-10 19:50:46 UTC (rev 12845)
@@ -0,0 +1,159 @@
+#include "VtkIO.h"
+#include <cstdlib>
+#include <cassert>
+
+//----------------------------------------------------------------------------
+
+cigma::VtkIO::VtkIO()
+{
+    fp = NULL;
+}
+
+cigma::VtkIO::~VtkIO()
+{
+}
+
+//----------------------------------------------------------------------------
+
+void cigma::VtkIO::write_header()
+{
+    fprintf(fp, "# vtk DataFile Version 3.0\n");
+    fprintf(fp, "This line is a comment\n");
+    fprintf(fp, "ASCII\n");
+    fprintf(fp, "DATASET UNSTRUCTURED_GRID\n");
+}
+
+void cigma::VtkIO::write_points(double *points, int nno, int nsd)
+{
+    fprintf(fp, "POINTS %d float\n", nno);
+    for (int i = 0; i < nno; i++)
+    {
+        fprintf(fp, " %g", points[nsd*i + 0]);
+        fprintf(fp, " %g", points[nsd*i + 1]);
+        if (nsd == 3)
+            fprintf(fp, " %g\n", points[nsd*i + 2]);
+        else
+            fprintf(fp, " 0.0\n");
+    }
+}
+
+void cigma::VtkIO::write_point_data(const char *name, double *data, int nno, int ndim)
+{
+    fprintf(fp, "POINT_DATA %d\n", nno);
+    if (ndim == 1)
+    {
+        fprintf(fp, "SCALARS %s float 1\n", name);
+        fprintf(fp, "LOOKUP_TABLE default\n");
+        for (int i = 0; i < nno; i++)
+            fprintf(fp, "%g\n", data[i]);
+    }
+    else if ((ndim == 2) || (ndim == 3))
+    {
+        fprintf(fp, "VECTORS %s float\n", name);
+        for (int i = 0; i < nno; i++)
+        {
+            fprintf(fp, " %g", data[ndim*i + 0]);
+            fprintf(fp, " %g", data[ndim*i + 1]);
+            if (ndim == 3)
+                fprintf(fp, " %g\n", data[ndim*i + 2]);
+            else
+                fprintf(fp, " 0.0\n");
+        }
+    }
+    else if (ndim == 9)
+    {
+        fprintf(fp, "TENSORS %s float\n", name);
+        for (int i = 0; i < nno; i++)
+        {
+            for (int j = 0; j < ndim; j++)
+            {
+                fprintf(fp, " %g", data[ndim*i + j]);
+            }
+            fprintf(fp, "\n");
+        }
+    }
+}
+
+void cigma::VtkIO::write_cells(int *cells, int nel, int ndofs)
+{
+    fprintf(fp, "CELLS %d %d\n", nel, nel*(1 + ndofs));
+    for (int i = 0; i < nel; i++)
+    {
+        fprintf(fp, " %d", ndofs);
+        for (int j = 0; j < ndofs; j++)
+            fprintf(fp, " %d", cells[ndofs*i + j]);
+        fprintf(fp, "\n");
+    }
+}
+
+void cigma::VtkIO::write_cell_types(int nsd, int nel, int ndofs)
+{
+    int vtkType = 0;
+
+    fprintf(fp, "CELL_TYPES %d\n", nel);
+    if (nsd == 3)
+    {
+        switch (ndofs)
+        {
+        case  4: vtkType = 10; break; // VTK_TETRA=10
+        case  8: vtkType = 12; break; // VTK_HEXAHEDRON=12
+        case  6: vtkType = 13; break; // VTK_WEDGE=13
+        case  5: vtkType = 14; break; // VTK_PYRAMID=14
+        case 10: vtkType = 24; break; // VTK_QUADRATIC_TETRA=24
+        case 20: vtkType = 25; break; // VTK_QUADRATIC_HEXAHEDRON=25
+        }
+    }
+    else if (nsd == 2)
+    {
+        switch (ndofs)
+        {
+        case 3: vtkType =  5; break; // VTK_TRIANGLE=5
+        case 4: vtkType =  9; break; // VTK_QUAD=9
+        case 6: vtkType = 22; break; // VTK_QUADRATIC_TRIANGLE=22
+        case 8: vtkType = 23; break; // VTK_QUADRATIC_QUAD=23
+        }
+    }
+
+    assert(vtkType > 0);
+
+    for (int i = 0; i < nel; i++)
+        fprintf(fp, "%d\n", vtkType);
+}
+
+void cigma::VtkIO::write_cell_data(const char *name, double *data, int nel, int ndim)
+{
+    fprintf(fp, "CELL_DATA %d\n", nel);
+    if (ndim == 1)
+    {
+        fprintf(fp, "SCALARS %s float 1\n", name);
+        fprintf(fp, "LOOKUP_TABLE default\n");
+        for (int i = 0; i < nel; i++)
+            fprintf(fp, "%g\n", data[i]);
+    }
+    else if ((ndim == 2) || (ndim == 3))
+    {
+        fprintf(fp, "VECTORS %s float\n", name);
+        for (int i = 0; i < nel; i++)
+        {
+            fprintf(fp, " %g", data[ndim*i + 0]);
+            fprintf(fp, " %g", data[ndim*i + 1]);
+            if (ndim == 3)
+                fprintf(fp, " %g\n", data[ndim*i + 2]);
+            else
+                fprintf(fp, " 0.0\n");
+        }
+    }
+    else if (ndim == 9)
+    {
+        fprintf(fp, "TENSORS %s float\n", name);
+        for (int i = 0; i < nel; i++)
+        {
+            for (int j = 0; j < ndim; j++)
+            {
+                fprintf(fp, " %g", data[ndim*i + j]);
+            }
+            fprintf(fp, "\n");
+        }
+    }
+}
+

Added: cs/cigma/trunk/src/VtkIO.h
===================================================================
--- cs/cigma/trunk/src/VtkIO.h	                        (rev 0)
+++ cs/cigma/trunk/src/VtkIO.h	2008-09-10 19:50:46 UTC (rev 12845)
@@ -0,0 +1,39 @@
+#ifndef __VTKIO_H__
+#define __VTKIO_H__
+
+#include "DataIO.h"
+#include <cstdio>
+
+namespace cigma
+{
+    class VtkIO;
+}
+
+
+class cigma::VtkIO : public cigma::DataIO
+{
+public:
+    VtkIO();
+    ~VtkIO();
+
+public:
+
+public:
+    bool read_connectivity(int **connect, int *nel, int *ndofs);
+    bool read_coordinates(double **coords, int *nno, int *nsd);
+    bool read_dofs(double **dofs, int *nno, int *ndim);
+    bool read_points(double **points, int *npts, int *ndim);
+
+public:
+    void write_header();
+    void write_points(double *points, int npts, int ndim);
+    void write_cells(int *cells, int nel, int ndofs);
+    void write_cell_types(int nsd, int nel, int ndofs);
+    void write_point_data(const char *name, double *data, int nno, int ndim);
+    void write_cell_data(const char *name, double *data, int nel, int ndim);
+
+private:
+    FILE *fp;
+};
+
+#endif



More information about the cig-commits mailing list