root/OpenSceneGraph/trunk/src/osgPlugins/ive/VolumeTransferFunctionProperty.cpp @ 13041

Revision 13041, 3.7 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include "Exception.h"
15#include "VolumeTransferFunctionProperty.h"
16
17#include <osgDB/ReadFile>
18#include "Object.h"
19
20using namespace ive;
21
22void VolumeTransferFunctionProperty::write(DataOutputStream* out)
23{
24    // Write Layer's identification.
25    out->writeInt(IVEVOLUMETRANSFERFUNCTIONPROPERTY);
26
27    // If the osg class is inherited by any other class we should also write this to file.
28    osg::Object* object = dynamic_cast<osg::Object*>(this);
29    if (object)
30        ((ive::Object*)(object))->write(out);
31    else
32        out_THROW_EXCEPTION("VolumeTransferFunctionProperty::write(): Could not cast this osgVolume::TransferFunctionProperty to an osg::Object.");
33
34
35    osg::TransferFunction1D* tf = dynamic_cast<osg::TransferFunction1D*>(getTransferFunction());
36    if (tf)
37    {
38
39        out->writeUInt(1); // TransferFunction1D
40        out->writeUInt(tf->getNumberImageCells());
41
42        const osg::TransferFunction1D::ColorMap& colourMap = tf->getColorMap();
43
44        // count the number of colour entries in the map so we can write it to the .ive file
45        unsigned int numColours = 0;
46        for(osg::TransferFunction1D::ColorMap::const_iterator itr = colourMap.begin();
47            itr != colourMap.end();
48            ++itr)
49        {
50            ++numColours;
51        }
52
53        // write out the num of colours
54        out->writeUInt(numColours);
55
56        // write out the colour map entires
57        for(osg::TransferFunction1D::ColorMap::const_iterator itr = colourMap.begin();
58            itr != colourMap.end();
59            ++itr)
60        {
61            out->writeFloat(itr->first);
62            out->writeVec4(itr->second);
63        }
64    }
65    else
66    {
67        out->writeUInt(0);
68    }
69}
70
71void VolumeTransferFunctionProperty::read(DataInputStream* in)
72{
73    // Peek on Layer's identification.
74    int id = in->peekInt();
75    if (id != IVEVOLUMETRANSFERFUNCTIONPROPERTY)
76        in_THROW_EXCEPTION("VolumeTransferFunctionProperty::read(): Expected CompositeProperty identification.");
77
78    // Read Layer's identification.
79    id = in->readInt();
80
81    // If the osg class is inherited by any other class we should also read this from file.
82    osg::Object* object = dynamic_cast<osg::Object*>(this);
83    if (object)
84        ((ive::Object*)(object))->read(in);
85    else
86        in_THROW_EXCEPTION("VolumeTransferFunctionProperty::write(): Could not cast this osgVolume::TransferFunctionProperty to an osg::Object.");
87
88    unsigned int numDimensions = in->readUInt();
89    if (numDimensions==1)
90    {
91        osg::TransferFunction1D* tf = new osg::TransferFunction1D;
92        setTransferFunction(tf);
93
94        tf->allocate(in->readUInt());
95
96        osg::TransferFunction1D::ColorMap& colourMap = tf->getColorMap();
97
98        // count the number of colour entries in the map so we can write it to the .ive file
99        unsigned int numColours = in->readUInt();
100        for(unsigned int i=0; i<numColours; ++i)
101        {
102            float value = in->readFloat();
103            osg::Vec4 colour = in->readVec4();
104            colourMap[value] = colour;
105        }
106
107        tf->updateImage();
108    }
109    else
110    {
111    }
112
113}
Note: See TracBrowser for help on using the browser.