Changeset 370


Ignore:
Timestamp:
Mar 21, 2006 11:00:11 PM (18 years ago)
Author:
mmc
Message:

Added support for triangular and prismatic meshes.

Serialization sort of works. Needs better treatment of pointers for
classes derived from Rappture::Serializable.

Location:
trunk/src2/core
Files:
8 added
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/src2/core/Makefile

    r364 r370  
     1CFLAGS = -g
    12all: viz
    23
    3 viz: viz.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshRect3D.o RpFieldRect3D.o
    4         g++ -g -o viz viz.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshRect3D.o RpFieldRect3D.o
     4viz: viz.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshTri2D.o RpMeshRect3D.o RpFieldRect3D.o RpMeshPrism3D.o RpFieldPrism3D.o RpSerialBuffer.o RpSerializer.o RpSerializable.o
     5        g++ $(CFLAGS) -o viz viz.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshTri2D.o RpMeshRect3D.o RpFieldRect3D.o RpMeshPrism3D.o RpFieldPrism3D.o RpSerialBuffer.o RpSerializer.o RpSerializable.o
    56
    67viz.o: viz.cc
    7         g++ -c -g -o viz.o viz.cc
     8        g++ $(CFLAGS) -c -o viz.o viz.cc
    89
    9 test: test.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshRect3D.o RpFieldRect3D.o RpSerialBuffer.o
    10         g++ -g -o test test.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshRect3D.o RpFieldRect3D.o RpSerialBuffer.o
     10test: test.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshTri2D.o RpFieldTri2D.o RpMeshRect3D.o RpFieldRect3D.o RpMeshPrism3D.o RpFieldPrism3D.o RpSerialBuffer.o RpSerializer.o RpSerializable.o
     11        g++ $(CFLAGS) -o test test.o RpOutcome.o RpMesh1D.o RpField1D.o RpMeshTri2D.o RpFieldTri2D.o RpMeshRect3D.o RpFieldRect3D.o RpMeshPrism3D.o RpFieldPrism3D.o RpSerialBuffer.o RpSerializer.o RpSerializable.o
    1112
    1213test.o: test.cc RpPtr.h
    13         g++ -c -g -o test.o test.cc
     14        g++ $(CFLAGS) -c -o test.o test.cc
    1415
    1516RpOutcome.o: RpOutcome.cc RpPtr.h
    16         g++ -c -g -o RpOutcome.o RpOutcome.cc
     17        g++ $(CFLAGS) -c -o RpOutcome.o RpOutcome.cc
    1718
    1819RpMesh1D.o: RpMesh1D.cc RpMesh1D.h RpNode.h
    19         g++ -c -g -o RpMesh1D.o RpMesh1D.cc
     20        g++ $(CFLAGS) -c -o RpMesh1D.o RpMesh1D.cc
    2021
    2122RpField1D.o: RpField1D.cc RpField1D.h RpMesh1D.h RpNode.h
    22         g++ -c -g -o RpField1D.o RpField1D.cc
     23        g++ $(CFLAGS) -c -o RpField1D.o RpField1D.cc
     24
     25RpMeshTri2D.o: RpMeshTri2D.cc RpMeshTri2D.h RpNode.h
     26        g++ $(CFLAGS) -c -o RpMeshTri2D.o RpMeshTri2D.cc
     27
     28RpFieldTri2D.o: RpFieldTri2D.cc RpFieldTri2D.h RpNode.h
     29        g++ $(CFLAGS) -c -o RpFieldTri2D.o RpFieldTri2D.cc
    2330
    2431RpMeshRect3D.o: RpMeshRect3D.cc RpMeshRect3D.h RpNode.h
    25         g++ -c -g -o RpMeshRect3D.o RpMeshRect3D.cc
     32        g++ $(CFLAGS) -c -o RpMeshRect3D.o RpMeshRect3D.cc
    2633
    2734RpFieldRect3D.o: RpFieldRect3D.cc RpFieldRect3D.h RpNode.h
    28         g++ -c -g -o RpFieldRect3D.o RpFieldRect3D.cc
     35        g++ $(CFLAGS) -c -o RpFieldRect3D.o RpFieldRect3D.cc
     36
     37RpMeshPrism3D.o: RpMeshPrism3D.cc RpMeshPrism3D.h RpNode.h
     38        g++ $(CFLAGS) -c -o RpMeshPrism3D.o RpMeshPrism3D.cc
     39
     40RpFieldPrism3D.o: RpFieldPrism3D.cc RpFieldPrism3D.h RpNode.h
     41        g++ $(CFLAGS) -c -o RpFieldPrism3D.o RpFieldPrism3D.cc
    2942
    3043RpSerialBuffer.o: RpSerialBuffer.cc RpSerialBuffer.h
    31         g++ -c -g -o RpSerialBuffer.o RpSerialBuffer.cc
     44        g++ $(CFLAGS) -c -o RpSerialBuffer.o RpSerialBuffer.cc
     45
     46RpSerializer.o: RpSerializer.cc RpSerializer.h
     47        g++ $(CFLAGS) -c -o RpSerializer.o RpSerializer.cc
    3248
    3349RpSerializable.o: RpSerializable.cc RpSerializable.h
    34         g++ -c -g -o RpSerializable.o RpSerializable.cc
     50        g++ $(CFLAGS) -c -o RpSerializable.o RpSerializable.cc
    3551
    3652clean:
  • trunk/src2/core/RpField1D.cc

    r364 r370  
    8888
    8989Node1D&
    90 Field1D::nodeAt(int pos)
     90Field1D::atNode(int pos)
    9191{
    9292    return _meshPtr->at(pos);
  • trunk/src2/core/RpField1D.h

    r364 r370  
    3636
    3737    virtual int size() const;
    38     virtual Node1D& nodeAt(int pos);
     38    virtual Node1D& atNode(int pos);
    3939    virtual double rangeMin() const;
    4040    virtual double rangeMax() const;
  • trunk/src2/core/RpFieldRect3D.cc

    r364 r370  
    3131{
    3232    _meshPtr = Ptr<MeshRect3D>( new MeshRect3D(xg,yg,zg) );
     33    int npts = xg.size()*yg.size()*zg.size();
     34    _valuelist.reserve(npts);
    3335}
    3436
     
    4648    _meshPtr = field._meshPtr;
    4749    _counter = field._counter;
     50    return *this;
    4851}
    4952
     
    5558FieldRect3D::size(Axis which) const
    5659{
    57     if (!_meshPtr.isnull()) {
     60    if (!_meshPtr.isNull()) {
    5861        return _meshPtr->size(which);
    5962    }
     
    6265
    6366Node1D&
    64 FieldRect3D::nodeAt(Axis which, int pos)
     67FieldRect3D::atNode(Axis which, int pos)
    6568{
    6669    static Node1D null(0.0);
    6770
    68     if (!_meshPtr.isnull()) {
     71    if (!_meshPtr.isNull()) {
    6972        return _meshPtr->at(which, pos);
    7073    }
     
    7578FieldRect3D::rangeMin(Axis which) const
    7679{
    77     if (!_meshPtr.isnull()) {
     80    if (!_meshPtr.isNull()) {
    7881        return _meshPtr->rangeMin(which);
    7982    }
     
    8487FieldRect3D::rangeMax(Axis which) const
    8588{
    86     if (!_meshPtr.isnull()) {
     89    if (!_meshPtr.isNull()) {
    8790        return _meshPtr->rangeMax(which);
    8891    }
     
    9396FieldRect3D::define(int nodeId, double f)
    9497{
    95     while (_valuelist.size() <= nodeId) {
    96         _valuelist.push_back(0.0);
    97     }
    9898    _valuelist[nodeId] = f;
    9999    return *this;
     
    101101
    102102double
    103 FieldRect3D::value(double x, double y, double z) const
     103FieldRect3D::value(double x, double y, double z, double outside) const
    104104{
    105105    double f0, f1, fy0, fy1, fz0, fz1;
    106106
    107     if (!_meshPtr.isnull()) {
     107    if (!_meshPtr.isNull()) {
    108108        CellRect3D cell = _meshPtr->locate(Node3D(x,y,z));
     109
     110        // outside the defined data? then return the outside value
     111        if (cell.isOutside()) {
     112            return outside;
     113        }
    109114
    110115        // yuck! brute force...
     
    138143        return _interpolate(cell.z(0),fz0, cell.z(4),fz1, z);
    139144    }
    140     return 0.0;
     145    return outside;
    141146}
    142147
  • trunk/src2/core/RpFieldRect3D.h

    r364 r370  
    1717#define RAPPTURE_FIELDRECT3D_H
    1818
     19#include <math.h>
     20#include <vector>
    1921#include "RpPtr.h"
    2022#include "RpMeshRect3D.h"
     
    3133
    3234    virtual int size(Axis which) const;
    33     virtual Node1D& nodeAt(Axis which, int pos);
     35    virtual Node1D& atNode(Axis which, int pos);
    3436    virtual double rangeMin(Axis which) const;
    3537    virtual double rangeMax(Axis which) const;
    3638
    3739    virtual FieldRect3D& define(int nodeId, double f);
    38     virtual double value(double x, double y, double z) const;
     40    virtual double value(double x, double y, double z,
     41        double outside=NAN) const;
    3942
    4043protected:
     
    4346
    4447private:
    45     std::deque<double> _valuelist; // list of all values, in nodeId order
     48    std::vector<double> _valuelist; // list of all values, in nodeId order
    4649    Ptr<MeshRect3D> _meshPtr;       // mesh for all (x,y,z) points
    4750    int _counter;                   // counter for generating node IDs
  • trunk/src2/core/RpMesh1D.cc

    r364 r370  
    2222
    2323// serialization version 'A' for Mesh1D...
    24 ////SerialConverter Mesh1D::versionA("Mesh1D", 'A',
    25 ////    Mesh1D::serialize_A, Mesh1D::create, Mesh1D::deserialize_A);
     24SerialConversion Mesh1D::versionA("Mesh1D", 'A',
     25    (Serializable::serializeObjectMethod)&Mesh1D::serialize_A,
     26    &Mesh1D::create,
     27    (Serializable::deserializeObjectMethod)&Mesh1D::deserialize_A);
    2628
    2729
     
    5254    assert(n >= 0 && n < 2);
    5355    return _x[n];
     56}
     57
     58int
     59Cell1D::isOutside() const
     60{
     61    return (_nodeIds[0] < 0 || _nodeIds[1] < 0);
    5462}
    5563
     
    317325}
    318326
    319 ////void
    320 ////Mesh1D::serialize_A(SerialBuffer& buffer) const
    321 ////{
    322 ////}
    323 
    324 ////Ptr<Serializable>
    325 ////Mesh1D::create()
    326 ////{
    327 ////    return Ptr<Serializable>( new Mesh1D() );
    328 ////}
    329 
    330 ////Outcome
    331 ////Mesh1D::deserialize_A(SerialBuffer& buffer)
    332 ////{
    333 ////}
     327Ptr<Serializable>
     328Mesh1D::create()
     329{
     330    return Ptr<Serializable>( (Serializable*) new Mesh1D() );
     331}
     332
     333void
     334Mesh1D::serialize_A(SerialBuffer& buffer) const
     335{
     336    Mesh1D* nonconst = (Mesh1D*)this;
     337    buffer.writeInt(_nodelist.size());
     338
     339    std::deque<Node1D>::iterator iter = nonconst->_nodelist.begin();
     340    while (iter != _nodelist.end()) {
     341        buffer.writeInt( (*iter).id() );
     342        buffer.writeDouble( (*iter).x() );
     343        ++iter;
     344    }
     345    buffer.writeInt(_counter);
     346}
     347
     348Outcome
     349Mesh1D::deserialize_A(SerialBuffer& buffer)
     350{
     351    Outcome status;
     352    Node1D newnode(0.0);
     353
     354    clear();
     355    int npts = buffer.readInt();
     356
     357    for (int n=0; n < npts; n++) {
     358        newnode.id( buffer.readInt() );
     359        newnode.x( buffer.readDouble() );
     360        _nodelist.push_back(newnode);
     361    }
     362    _counter = buffer.readInt();
     363
     364    return status;
     365}
  • trunk/src2/core/RpMesh1D.h

    r364 r370  
    2121#include <deque>
    2222#include "RpNode.h"
    23 ////#include "RpSerializable.h"
     23#include "RpSerializable.h"
    2424
    2525namespace Rappture {
     
    3232    double& x(int n);
    3333
     34    int isOutside() const;
     35
    3436private:
    3537    int _nodeIds[2];
     
    3739};
    3840
    39 class Mesh1D {
     41class Mesh1D : public Serializable {
    4042public:
    4143    Mesh1D();
     
    6163    char serializerVersion() const { return 'A'; }
    6264
    63 ////    void serialize_A(SerialBuffer& buffer) const;
    64 ////    static Ptr<Serializable> create();
    65 ////    Outcome deserialize_A(SerialBuffer& buffer);
     65    void serialize_A(SerialBuffer& buffer) const;
     66    static Ptr<Serializable> create();
     67    Outcome deserialize_A(SerialBuffer& buffer);
    6668
    6769protected:
     
    7779
    7880    // methods for serializing/deserializing version 'A'
    79 ////    static SerialConversion versionA;
     81    static SerialConversion versionA;
    8082};
    8183
  • trunk/src2/core/RpMeshRect3D.cc

    r364 r370  
    7878}
    7979
     80int
     81CellRect3D::isOutside() const
     82{
     83    for (int i=0; i < 8; i++) {
     84        if (_nodeIds[i] < 0) {
     85            return 1;
     86        }
     87    }
     88    return 0;
     89}
     90
    8091
    8192MeshRect3D::MeshRect3D()
     
    175186    coord.x(node.z());
    176187    cells[2] = _axis[2].locate(coord);
    177 
    178     // any of these cells on the edge? then set both sides to edge point
    179     for (int c=0; c < 3; c++) {
    180         if (cells[c].nodeId(0) < 0) {
    181             cells[c].nodeId(0) = cells[c].nodeId(1);
    182             cells[c].x(0) = cells[c].x(1);
    183         }
    184         else if (cells[c].nodeId(1) < 0) {
    185             cells[c].nodeId(1) = cells[c].nodeId(0);
    186             cells[c].x(1) = cells[c].x(0);
    187         }
    188     }
    189188
    190189    int n = 0;
     
    195194                int ny = cells[1].nodeId(iy);
    196195                int nz = cells[2].nodeId(iz);
    197                 result.nodeId(n) = nz*(_axis[0].size()*_axis[1].size())
    198                                    + ny*(_axis[0].size())
    199                                    + nx;
    200 
    201                 result.x(n) = cells[0].x(ix);
    202                 result.y(n) = cells[1].x(iy);
    203                 result.z(n) = cells[2].x(iz);
     196                if (nx >= 0 && ny >= 0 && nz >= 0) {
     197                    result.nodeId(n) = nz*(_axis[0].size()*_axis[1].size())
     198                                       + ny*(_axis[0].size())
     199                                       + nx;
     200                    result.x(n) = cells[0].x(ix);
     201                    result.y(n) = cells[1].x(iy);
     202                    result.z(n) = cells[2].x(iz);
     203                }
    204204                n++;
    205205            }
  • trunk/src2/core/RpMeshRect3D.h

    r364 r370  
    2121namespace Rappture {
    2222
    23 enum Axis {xaxis=0, yaxis, zaxis};
    24 
    2523class CellRect3D {
    2624public:
     
    3230    double& y(int n);
    3331    double& z(int n);
     32
     33    int isOutside() const;
    3434
    3535private:
  • trunk/src2/core/RpNode.h

    r364 r370  
    2020
    2121namespace Rappture {
     22
     23enum Axis {xaxis=0, yaxis, zaxis};
    2224
    2325class Node {
  • trunk/src2/core/RpOutcome.cc

    r364 r370  
    7676{
    7777    _status &= oc._status;
    78     if (!oc._contextPtr.isnull()) {
     78    if (!oc._contextPtr.isNull()) {
    7979        _remarkPtr = oc._remarkPtr;
    8080        _contextPtr = oc._contextPtr;
     
    8585Outcome::remark() const
    8686{
    87     if (!_remarkPtr.isnull()) {
     87    if (!_remarkPtr.isNull()) {
    8888        return _remarkPtr->data();
    8989    }
     
    9494Outcome::addContext(const char *rem)
    9595{
    96     if (_contextPtr.isnull()) {
     96    if (_contextPtr.isNull()) {
    9797        _contextPtr = new std::string();
    9898    }
     
    105105Outcome::context() const
    106106{
    107     if (!_contextPtr.isnull()) {
     107    if (!_contextPtr.isNull()) {
    108108        return _contextPtr->data();
    109109    }
  • trunk/src2/core/RpPtr.h

    r364 r370  
    8787class Ptr {
    8888public:
    89     explicit Ptr(Type* ptr);
     89    explicit Ptr(Type* ptr=NULL);
    9090    Ptr(const Ptr& ptr);
    9191    Ptr& operator=(Type* ptr);
     
    9393    ~Ptr();
    9494
    95     int isnull() const;
     95    int isNull() const;
    9696    Type* operator->() const;
    9797    Type* pointer() const;
     98    Type& operator*() const;
    9899    void clear();
    99100
     
    154155template <class Type>
    155156int
    156 Ptr<Type>::isnull() const
     157Ptr<Type>::isNull() const
    157158{
    158159    if (_pc) {
     
    183184
    184185template <class Type>
     186Type&
     187Ptr<Type>::operator*() const
     188{
     189    assert(_pc != NULL);
     190    return *(Type*)_pc->pointer();
     191}
     192
     193template <class Type>
    185194void
    186195Ptr<Type>::clear()
  • trunk/src2/core/RpSerializable.cc

    r364 r370  
    2121
    2222// initialize the global lists of registered serialization helper funcs
    23 Serializable::_name2createFunc = NULL;
    24 Serializable::_name2desMethod = NULL;
     23Serializable::Name2ConvFuncsMap* Serializable::_name2convFuncs = NULL;
    2524
    2625Serializable::Serializable()
     
    4241    cname += serializerVersion();
    4342
    44     ConversionFuncs& cfuncs = (*_name2convFuncs)[cname];
    45     this->(cfuncs.serialMethod)(buffer);
     43    Serializable::ConversionFuncs& cfuncs = (*_name2convFuncs)[cname];
     44    (this->*(cfuncs.serialMethod))(buffer);
    4645}
    4746
    4847Outcome
    49 Serializable::deserialize(SerialBuffer& buffer, Ptr<Serializable>& objPtr)
     48Serializable::deserialize(SerialBuffer& buffer, Ptr<Serializable>* objPtrPtr)
    5049{
    5150    Outcome result;
     
    6665
    6766    ConversionFuncs& cfuncs = (*_name2convFuncs)[type];
    68     objPtr = (cfuncs.createFunc)();
    69     return objPtr->(cfuncs.deserialMethod)(buffer);
     67    *objPtrPtr = (cfuncs.createFunc)();
     68
     69    Serializable *objPtr = objPtrPtr->pointer();
     70    return (objPtr->*(cfuncs.deserialMethod))(buffer);
    7071}
    7172
    72 SerialConversion::SerialConversion(const char *class, char version,
    73     Serializable::serializeObjectMethod serMethod)
     73SerialConversion::SerialConversion(const char *className, char version,
     74    Serializable::serializeObjectMethod serMethod,
    7475    Serializable::createObjectFunc createFunc,
    7576    Serializable::deserializeObjectMethod desMethod)
    7677{
    77     if (Serializer::_name2convFuncs == NULL) {
    78         Serializer::_name2convFuncs = new Name2ConvFuncsMap;
     78    if (Serializable::_name2convFuncs == NULL) {
     79        Serializable::_name2convFuncs = new Serializable::Name2ConvFuncsMap();
    7980    }
    8081
    8182    // register the conversion functions for this version
    82     std::string vname(class);
     83    std::string vname(className);
    8384    vname += ":";
    8485    vname += version;
    8586
    86     ConversionFuncs& funcs = (*_name2convFuncs)[vname];
     87    Serializable::ConversionFuncs& funcs
     88        = (*Serializable::_name2convFuncs)[vname];
    8789
    8890    funcs.version = version;
     
    9294
    9395    // if this is the latest version, register it as "current"
    94     std::string cname(class);
     96    std::string cname(className);
    9597    cname += ":current";
    9698
    97     Name2ConvFuncsMap::iterator iter = _name2convFuncs->find(cname);
    98     if (iter == _name2convFuncs->end()) {
     99    Serializable::Name2ConvFuncsMap::iterator iter
     100        = Serializable::_name2convFuncs->find(cname);
     101
     102    if (iter == Serializable::_name2convFuncs->end()) {
    99103        // this is the first -- register it as "current"
    100         ConversionFuncs& cfuncs = (*_name2convFuncs)[cname];
     104        Serializable::ConversionFuncs& cfuncs
     105            = (*Serializable::_name2convFuncs)[cname];
    101106        cfuncs.version = version;
    102107        cfuncs.serialMethod = serMethod;
     
    104109        cfuncs.deserialMethod = desMethod;
    105110    } else {
    106         ConversionFuncs& cfuncs = (*_name2convFuncs)[cname];
     111        Serializable::ConversionFuncs& cfuncs
     112            = (*Serializable::_name2convFuncs)[cname];
     113
    107114        if (cfuncs.version < version) {
    108115            // this is the latest -- register it as "current"
  • trunk/src2/core/RpSerializable.h

    r364 r370  
    2121#include <string>
    2222#include <map>
     23#include "RpPtr.h"
     24#include "RpOutcome.h"
    2325#include "RpSerialBuffer.h"
    2426
     
    3234    virtual ~Serializable();
    3335
    34     virtual const char* serializerType() = 0;
    35     virtual char serializerVersion() = 0;
     36    virtual const char* serializerType() const = 0;
     37    virtual char serializerVersion() const = 0;
    3638
    3739    virtual void serialize(SerialBuffer& bufferPtr) const;
    38     static Outcome deserialize(SerialBuffer& buffer, Ptr<Serializable>& objPtr);
     40    static Outcome deserialize(SerialBuffer& buffer, Ptr<Serializable>* objPtrPtr);
     41
     42    typedef void (Serializable::*serializeObjectMethod)(SerialBuffer& buffer) const;
     43    typedef Ptr<Serializable> (*createObjectFunc)();
     44    typedef Outcome (Serializable::*deserializeObjectMethod)(SerialBuffer& buffer);
    3945
    4046private:
    4147    friend class SerialConversion;
    4248
    43     typedef void serializeObjectMethod(SerialBuffer& buffer) const;
    44     typedef static Ptr<Serializable> createObjectFunc();
    45     typedef Outcome deserializeObjectMethod(SerialBuffer& buffer);
    46 
    47     struct ConversionFuncs {
     49    class ConversionFuncs {
     50    public:
    4851        char version;
    4952        serializeObjectMethod serialMethod;
     
    5255    };
    5356
    54     typedef map<std::string, ConversionFuncs> Name2ConvFuncsMap;
     57    typedef std::map<std::string, ConversionFuncs> Name2ConvFuncsMap;
    5558    static Name2ConvFuncsMap *_name2convFuncs;
    5659};
     
    5861class SerialConversion {
    5962public:
    60     SerialConversion(const char *class, char version,
     63    SerialConversion(const char *className, char version,
    6164        Serializable::serializeObjectMethod,
    6265        Serializable::createObjectFunc,
  • trunk/src2/core/RpSerializer.cc

    r364 r370  
    7373
    7474        // if there was an error, then bail out
    75         if (!result) {
     75        if (result != 0) {
    7676            std::ostringstream context;
    7777            context << "while deserializing object #" << i+1
    7878              << " of " << nobj;
    79             return result.addContext(context.str());
     79            return result.addContext(context.str().data());
    8080        }
    8181
     
    106106        bufferPtr->writeString("RpObj:");
    107107        bufferPtr->writeString(id);
    108         objPtr->serialize(bufferPtr);
     108        objPtr->serialize(*bufferPtr.pointer());
    109109    }
    110110    return bufferPtr;
     
    120120Serializer::get(int pos) const
    121121{
     122    Serializer *nonconst = (Serializer*)this;
    122123    assert(pos >= 0 && pos < _idlist.size());
    123124
    124125    std::string id(_idlist[pos]);
    125     return _id2obj[id];
     126    return nonconst->_id2obj[id];
    126127}
    127128
     
    134135
    135136const char*
    136 Serializer::register(Ptr<Serializable> objPtr)
     137Serializer::add(Serializable* objPtr)
    137138{
    138139    const char* key = NULL;
     
    140141    // build a unique string that represents this object
    141142    std::ostringstream idbuffer;
    142     idbuffer << (void*)objPtr.pointer();
     143    idbuffer << (void*)objPtr;
    143144    std::string id(idbuffer.str());
    144145
     
    147148    if (iter == _id2obj.end()) {
    148149        // if not, then add to map and also to list of all objects
    149         _id2obj[id] = objPtr;
     150        _id2obj[id] = Ptr<Serializable>(objPtr);
    150151        iter = _id2obj.find(id);
    151152        key = (*iter).first.data();
     
    154155        // if so, then update map
    155156        key = (*iter).first.data();
    156         _id2obj[id] = objPtr;
     157        _id2obj[id] = Ptr<Serializable>(objPtr);
    157158    }
    158159    return key;
  • trunk/src2/core/RpSerializer.h

    r364 r370  
    2121#include <map>
    2222#include "RpPtr.h"
     23#include "RpOutcome.h"
    2324#include "RpSerialBuffer.h"
     25#include "RpSerializable.h"
    2426
    2527namespace Rappture {
     
    3032    virtual ~Serializer();
    3133
     34    virtual Ptr<SerialBuffer> serialize();
    3235    virtual Outcome deserialize(const char* bytes, int nbytes);
    33     virtual Ptr<SerialBuffer> serialize();
    3436
    3537    virtual int size() const;
     
    3739
    3840    virtual Serializer& clear();
    39     virtual const char* register(Ptr<Serializable> objPtr);
     41    virtual const char* add(Serializable* objPtr);
    4042
    4143private:
     
    4850
    4951    // map each id in _idlist to the corresponding object
    50     typedef std::map<std::string, Ptr<Serializable>> SerializerId2Obj;
     52    typedef std::map<std::string, Ptr<Serializable> > SerializerId2Obj;
    5153    SerializerId2Obj _id2obj;
    5254};
  • trunk/src2/core/test.cc

    r364 r370  
    66#include "RpNode.h"
    77#include "RpField1D.h"
     8#include "RpMeshTri2D.h"
     9#include "RpFieldTri2D.h"
    810#include "RpMeshRect3D.h"
    911#include "RpFieldRect3D.h"
     12#include "RpMeshPrism3D.h"
     13#include "RpFieldPrism3D.h"
    1014#include "RpSerialBuffer.h"
    11 ////#include "RpSerializable.h"
     15#include "RpSerializer.h"
    1216
    1317class Foo {
     
    6771    std::cout << "@ 8.8 = " << f.value(8.8) << std::endl;
    6872
    69     Rappture::SerialBuffer buffer;
    70     buffer.writeChar('a');
    71     buffer.writeInt(1051);
    72     buffer.writeDouble(2.51e12);
    73     buffer.writeString("xyz");
    74     buffer.writeString("pdq");
    75 
    76     buffer.rewind();
    77     std::cout << "buffer: ";
    78     std::cout << buffer.readChar() << " ";
    79     std::cout << buffer.readInt() << " ";
    80     std::cout << buffer.readDouble() << " ";
    81     std::cout << buffer.readString() << " ";
    82     std::cout << buffer.readString() << std::endl;
    83 
    84     Rappture::Mesh1D uni(1.0,5.0,16);
    85     showmesh(uni);
     73    Rappture::Ptr<Rappture::Mesh1D> uni(
     74        new Rappture::Mesh1D(1.0,5.0,16)
     75    );
     76    showmesh(*uni);
    8677
    8778    Rappture::Mesh1D uniy(0.0,2.0,10);
    8879    Rappture::Mesh1D uniz(-1.0,1.0,20);
    8980    Rappture::Ptr<Rappture::MeshRect3D> m3dPtr(
    90         new Rappture::MeshRect3D(uni, uniy, uniz)
     81        new Rappture::MeshRect3D(*uni, uniy, uniz)
    9182    );
    92     Rappture::FieldRect3D f3d(m3dPtr);
     83    Rappture::Ptr<Rappture::FieldRect3D> f3d(
     84        new Rappture::FieldRect3D(*uni, uniy, uniz)
     85    );
    9386
    9487    int n=0;
    95     for (int iz=0; iz < f3d.size(Rappture::zaxis); iz++) {
    96         double zval = f3d.nodeAt(Rappture::zaxis, iz).x();
    97         for (int iy=0; iy < f3d.size(Rappture::yaxis); iy++) {
    98             double yval = f3d.nodeAt(Rappture::yaxis, iy).x();
    99             for (int ix=0; ix < f3d.size(Rappture::xaxis); ix++) {
    100                 double xval = f3d.nodeAt(Rappture::xaxis, ix).x();
    101                 f3d.define(n++, xval*yval*zval);
     88    for (int iz=0; iz < f3d->size(Rappture::zaxis); iz++) {
     89        double zval = f3d->atNode(Rappture::zaxis, iz).x();
     90        for (int iy=0; iy < f3d->size(Rappture::yaxis); iy++) {
     91            double yval = f3d->atNode(Rappture::yaxis, iy).x();
     92            for (int ix=0; ix < f3d->size(Rappture::xaxis); ix++) {
     93                double xval = f3d->atNode(Rappture::xaxis, ix).x();
     94                f3d->define(n++, xval*yval*zval);
    10295            }
    10396        }
    10497    }
    10598
    106     std::cout << "at (1,0,-0.5) " << f3d.value(1.0,0.0,-0.5) << std::endl;
    107     std::cout << "at (2,1,-0.5) " << f3d.value(2.0,1.0,-0.5) << std::endl;
    108     std::cout << "at (3,0.7,-0.2) " << f3d.value(3.0,0.7,-0.2) << std::endl;
    109     std::cout << "at (5,2,1) " << f3d.value(5.0,2.0,1.0) << std::endl;
     99    std::cout << "at (1,0,-0.5) " << f3d->value(1.0,0.0,-0.5) << std::endl;
     100    std::cout << "at (2,1,-0.5) " << f3d->value(2.0,1.0,-0.5) << std::endl;
     101    std::cout << "at (3,0.7,-0.2) " << f3d->value(3.0,0.7,-0.2) << std::endl;
     102    std::cout << "at (5,2,1) " << f3d->value(5.0,2.0,1.0) << std::endl;
    110103
    111     Rappture::Ptr<Foo> ptr3(new Foo("q"));
    112     std::ostringstream ss;
    113     ss << (void*)ptr3.pointer();
    114     std::cout << ss << std::endl;
    115104
    116     Rappture::Outcome status = foo(1);
    117     if (status) {
    118         status.addContext("while in main program");
    119         std::cout << status.remark() << std::endl;
    120         std::cout << status.context() << std::endl;
     105    Rappture::MeshTri2D m2d;
     106    m2d.addNode( Rappture::Node2D(0.0,0.0) );
     107    m2d.addNode( Rappture::Node2D(1.0,0.0) );
     108    m2d.addNode( Rappture::Node2D(0.0,1.0) );
     109    m2d.addNode( Rappture::Node2D(1.0,1.0) );
     110    m2d.addNode( Rappture::Node2D(2.0,0.0) );
     111    m2d.addNode( Rappture::Node2D(2.0,1.0) );
     112    m2d.addCell(0,1,2);
     113    m2d.addCell(1,2,3);
     114    m2d.addCell(1,3,4);
     115    m2d.addCell(3,4,5);
     116
     117    Rappture::FieldTri2D f2d(m2d);
     118    f2d.define(0, 0.0);
     119    f2d.define(1, 1.0);
     120    f2d.define(2, 1.0);
     121    f2d.define(3, 4.0);
     122    f2d.define(4, 2.0);
     123    f2d.define(5, 2.0);
     124    std::cout << "value @(0.1,0.1): " << f2d.value(0.1,0.1) << std::endl;
     125    std::cout << "value @(0.99,0.99): " << f2d.value(0.99,0.99) << std::endl;
     126    std::cout << "value @(1.99,0.01): " << f2d.value(1.99,0.01) << std::endl;
     127    std::cout << "value @(2.01,0.02): " << f2d.value(2.01,1.02) << std::endl;
     128
     129    Rappture::FieldPrism3D fp3d(m2d, uniy);
     130    for (int i=0; i < 10; i++) {
     131        fp3d.define(i*6+0, (i+1)*0.0);
     132        fp3d.define(i*6+1, (i+1)*1.0);
     133        fp3d.define(i*6+2, (i+1)*1.0);
     134        fp3d.define(i*6+3, (i+1)*4.0);
     135        fp3d.define(i*6+4, (i+1)*2.0);
     136        fp3d.define(i*6+5, (i+1)*2.0);
     137    }
     138    std::cout << "value @(0.1,0.1,0.01): " << fp3d.value(0.1,0.1,0.01) << std::endl;
     139    std::cout << "value @(0.1,0.1,0.21): " << fp3d.value(0.1,0.1,0.21) << std::endl;
     140    std::cout << "value @(0.1,0.1,0.41): " << fp3d.value(0.1,0.1,0.41) << std::endl;
     141    std::cout << "value @(0.99,0.99,0.01): " << fp3d.value(0.99,0.99,0.01) << std::endl;
     142    std::cout << "value @(0.99,0.99,0.21): " << fp3d.value(0.99,0.99,0.21) << std::endl;
     143    std::cout << "value @(0.99,0.99,0.41): " << fp3d.value(0.99,0.99,0.41) << std::endl;
     144
     145
     146    std::cout << "original mesh:" << std::endl;
     147    showmesh( *uni );
     148    Rappture::Serializer objs;
     149    objs.add( uni.pointer() );
     150    Rappture::Ptr<Rappture::SerialBuffer> buffer = objs.serialize();
     151
     152    Rappture::Serializer objs2;
     153    Rappture::Outcome err = objs2.deserialize(buffer->bytes(), buffer->size());
     154
     155    if (err) {
     156        err.addContext("while in main program");
     157        std::cout << err.remark() << std::endl;
     158        std::cout << err.context() << std::endl;
    121159        exit(1);
    122160    }
     161
     162    Rappture::Ptr<Rappture::Serializable> uni2ptr = objs2.get(0);
     163    Rappture::Ptr<Rappture::Mesh1D> uni2( (Rappture::Mesh1D*)uni2ptr.pointer() );
     164    std::cout << "reconstituted mesh:" << std::endl;
     165    showmesh( *uni2 );
     166
    123167    exit(0);
    124168}
  • trunk/src2/core/viz.cc

    r364 r370  
    55#include <string>
    66#include "RpFieldRect3D.h"
     7#include "RpFieldPrism3D.h"
    78
    89/* Load a 3D volume from a dx-format file
     
    1011void
    1112load_volume_file(int index, char *fname) {
    12     int dummy, nx, ny, nz, npts;
     13    Rappture::MeshTri2D xymesh;
     14    int dummy, nx, ny, nz, nxy, npts;
    1315    double x0, y0, z0, dx, dy, dz, ddx, ddy, ddz;
    14     char line[128], *start;
     16    char line[128], type[128], *start;
    1517    std::ifstream fin(fname);
     18
     19    int isrect = 1;
    1620
    1721    do {
     
    2327            if (sscanf(start, "object %d class gridpositions counts %d %d %d", &dummy, &nx, &ny, &nz) == 4) {
    2428                // found grid size
     29                isrect = 1;
     30            }
     31            else if (sscanf(start, "object %d class array type float rank 1 shape 3 items %d data follows", &dummy, &nxy) == 2) {
     32                std::ofstream ftmp("tmppts");
     33                double xx, yy, zz;
     34                isrect = 0;
     35                for (int i=0; i < nxy; i++) {
     36                    fin.getline(line,sizeof(line)-1);
     37                    if (sscanf(line, "%lg %lg %lg", &xx, &yy, &zz) == 3) {
     38                        xymesh.addNode( Rappture::Node2D(xx,yy) );
     39                        ftmp << xx << " " << yy << std::endl;
     40                    }
     41                }
     42                ftmp.close();
     43
     44                if (system("voronoi -t < tmppts > tmpcells") == 0) {
     45                    int cx, cy, cz;
     46                    std::ifstream ftri("tmpcells");
     47                    while (!ftri.eof()) {
     48                        ftri.getline(line,sizeof(line)-1);
     49                        if (sscanf(line, "%d %d %d", &cx, &cy, &cz) == 3) {
     50                            xymesh.addCell(cx, cy, cz);
     51                        }
     52                    }
     53                    ftri.close();
     54                } else {
     55                    std::cerr << "WARNING: triangularization failed" << std::endl;
     56                }
     57            }
     58            else if (sscanf(start, "object %d class regulararray count %d", &dummy, &nz) == 2) {
     59                // found z-grid
    2560            }
    2661            else if (sscanf(start, "origin %lg %lg %lg", &x0, &y0, &z0) == 3) {
     
    3368                else if (ddz != 0.0) { dz = ddz; }
    3469            }
    35             else if (sscanf(start, "object %d class array type double rank 0 items %d data follows", &dummy, &npts) == 2) {
    36                 if (npts != nx*ny*nz) {
     70            else if (sscanf(start, "object %d class array type %s rank 0 items %d data follows", &dummy, type, &npts) == 3) {
     71                if (isrect && (npts != nx*ny*nz)) {
    3772                    std::cerr << "inconsistent data: expected " << nx*ny*nz << " points but found " << npts << " points" << std::endl;
    3873                    return;
    3974                }
     75                else if (!isrect && (npts != nxy*nz)) {
     76                    std::cerr << "inconsistent data: expected " << nxy*nz << " points but found " << npts << " points" << std::endl;
     77                    return;
     78                }
    4079                break;
    41             }
    42             else if (sscanf(start, "object %d class gridconnections", &dummy) == 1) {
    43                 // ignore this
    4480            }
    4581        }
     
    4884    // read data points
    4985    if (!fin.eof()) {
    50         Rappture::Mesh1D xgrid(x0, x0+nx*dx, nx);
    51         Rappture::Mesh1D ygrid(y0, y0+ny*dy, ny);
    52         Rappture::Mesh1D zgrid(z0, z0+nz*dz, nz);
    53         Rappture::FieldRect3D field(xgrid, ygrid, zgrid);
     86        if (isrect) {
     87            Rappture::Mesh1D xgrid(x0, x0+nx*dx, nx);
     88            Rappture::Mesh1D ygrid(y0, y0+ny*dy, ny);
     89            Rappture::Mesh1D zgrid(z0, z0+nz*dz, nz);
     90            Rappture::FieldRect3D field(xgrid, ygrid, zgrid);
    5491
    55         double dval;
    56         int nread = 0;
    57         while (!fin.eof()) {
    58             if (!(fin >> dval).fail()) {
    59                 field.define(nread++, dval);
     92            double dval;
     93            int nread = 0;
     94            while (!fin.eof() && nread < npts) {
     95                if (!(fin >> dval).fail()) {
     96                    field.define(nread++, dval);
     97                }
    6098            }
    61         }
    6299
    63         // make sure that we read all of the expected points
    64         if (nread != nx*ny*nz) {
    65             std::cerr << "inconsistent data: expected " << nx*ny*nz << " points but found " << nread << " points" << std::endl;
    66             return;
    67         }
     100            // make sure that we read all of the expected points
     101            if (nread != nx*ny*nz) {
     102                std::cerr << "inconsistent data: expected " << nx*ny*nz << " points but found " << nread << " points" << std::endl;
     103                return;
     104            }
    68105
    69         // figure out a good mesh spacing
    70         int nsample = 100;
    71         dx = field.rangeMax(Rappture::xaxis) - field.rangeMin(Rappture::xaxis);
    72         dy = field.rangeMax(Rappture::yaxis) - field.rangeMin(Rappture::yaxis);
    73         dz = field.rangeMax(Rappture::zaxis) - field.rangeMin(Rappture::zaxis);
    74         double dmin = pow((dx*dy*dz)/(nsample*nsample*nsample), 0.333);
     106            // figure out a good mesh spacing
     107            int nsample = 100;
     108            dx = field.rangeMax(Rappture::xaxis) - field.rangeMin(Rappture::xaxis);
     109            dy = field.rangeMax(Rappture::yaxis) - field.rangeMin(Rappture::yaxis);
     110            dz = field.rangeMax(Rappture::zaxis) - field.rangeMin(Rappture::zaxis);
     111            double dmin = pow((dx*dy*dz)/(nsample*nsample*nsample), 0.333);
    75112
    76         nx = (int)ceil(dx/dmin);
    77         ny = (int)ceil(dy/dmin);
    78         nz = (int)ceil(dz/dmin);
    79         float *data = new float[nx*ny*nz];
    80         std::cout << "generating " << nx << "x" << ny << "x" << nz << " = " << nx*ny*nz << " points" << std::endl;
     113            nx = (int)ceil(dx/dmin);
     114            ny = (int)ceil(dy/dmin);
     115            nz = (int)ceil(dz/dmin);
     116            float *data = new float[nx*ny*nz];
     117            std::cout << "generating " << nx << "x" << ny << "x" << nz << " = " << nx*ny*nz << " points" << std::endl;
    81118
    82         // generate the uniformly sampled data that we need for a volume
    83         int ngen = 0;
    84         for (int iz=0; iz < nz; iz++) {
    85             double zval = z0 + iz*dmin;
    86             for (int iy=0; iy < ny; iy++) {
    87                 double yval = y0 + iy*dmin;
    88                 for (int ix=0; ix < nx; ix++) {
    89                     double xval = x0 + ix*dmin;
    90                     data[ngen++] = field.value(xval,yval,zval);
     119            // generate the uniformly sampled data that we need for a volume
     120            int ngen = 0;
     121            for (int iz=0; iz < nz; iz++) {
     122                double zval = z0 + iz*dmin;
     123                for (int iy=0; iy < ny; iy++) {
     124                    double yval = y0 + iy*dmin;
     125                    for (int ix=0; ix < nx; ix++) {
     126                        double xval = x0 + ix*dmin;
     127                        data[ngen++] = field.value(xval,yval,zval);
     128                    }
     129                }
     130            }
     131        } else {
     132            Rappture::Mesh1D zgrid(z0, z0+nz*dz, nz);
     133            Rappture::FieldPrism3D field(xymesh, zgrid);
     134
     135            double dval;
     136            int nread = 0;
     137            while (!fin.eof() && nread < npts) {
     138                if (!(fin >> dval).fail()) {
     139                    field.define(nread++, dval);
     140                }
     141            }
     142
     143            // make sure that we read all of the expected points
     144            if (nread != nxy*nz) {
     145                std::cerr << "inconsistent data: expected " << nxy*nz << " points but found " << nread << " points" << std::endl;
     146                return;
     147            }
     148
     149            // figure out a good mesh spacing
     150            int nsample = 100;
     151            dx = field.rangeMax(Rappture::xaxis) - field.rangeMin(Rappture::xaxis);
     152            dy = field.rangeMax(Rappture::yaxis) - field.rangeMin(Rappture::yaxis);
     153            dz = field.rangeMax(Rappture::zaxis) - field.rangeMin(Rappture::zaxis);
     154            double dmin = pow((dx*dy*dz)/(nsample*nsample*nsample), 0.333);
     155
     156            nx = (int)ceil(dx/dmin);
     157            ny = (int)ceil(dy/dmin);
     158            nz = (int)ceil(dz/dmin);
     159            float *data = new float[nx*ny*nz];
     160            std::cout << "generating " << nx << "x" << ny << "x" << nz << " = " << nx*ny*nz << " points" << std::endl;
     161
     162            // generate the uniformly sampled data that we need for a volume
     163            int ngen = 0;
     164            for (int iz=0; iz < nz; iz++) {
     165                double zval = z0 + iz*dmin;
     166                for (int iy=0; iy < ny; iy++) {
     167                    double yval = y0 + iy*dmin;
     168                    for (int ix=0; ix < nx; ix++) {
     169                        double xval = x0 + ix*dmin;
     170                        data[ngen++] = field.value(xval,yval,zval);
     171                    }
    91172                }
    92173            }
Note: See TracChangeset for help on using the changeset viewer.