1 /**********************************************************************
2  *
3  *    FILE:            Node.cpp
4  *
5  *    DESCRIPTION:    Read/Write osg::Node in binary format to disk.
6  *
7  *    CREATED BY:        Rune Schmidt Jensen
8  *
9  *    HISTORY:        Created 10.03.2003
10  *
11  *    Copyright 2003 VR-C
12  **********************************************************************/
13 
14 #include "Exception.h"
15 #include "Node.h"
16 #include "MatrixTransform.h"
17 #include "Group.h"
18 #include "Object.h"
19 #include "StateSet.h"
20 #include "AnimationPathCallback.h"
21 #include "ClusterCullingCallback.h"
22 #include "VolumePropertyAdjustmentCallback.h"
23 
24 using namespace ive;
25 
26 
write(DataOutputStream * out)27 void Node::write(DataOutputStream* out){
28 
29     // Write node identification.
30     out->writeInt(IVENODE);
31 
32     // Write out any inherited classes.
33     osg::Object*  obj = dynamic_cast<osg::Object*>(this);
34     if(obj){
35         ((ive::Object*)(obj))->write(out);
36     }
37     else
38         out_THROW_EXCEPTION("Node::write(): Could not cast this osg::Node to an osg::Object.");
39 
40 
41     // Write osg::node properties.
42     if ( out->getVersion() < VERSION_0012 )
43     {
44         // Write Name
45         out->writeString(getName());
46     }
47     // Write culling active
48     out->writeBool( getCullingActive());
49     // Write Descriptions
50     int nDesc =  getDescriptions().size();
51     out->writeInt(nDesc);
52     if(nDesc!=0){
53         std::vector<std::string> desc =  getDescriptions();
54         for(int i=0;i<nDesc;i++)
55             out->writeString(desc[i]);
56     }
57     // Write Stateset if any
58     out->writeBool( getStateSet()!=0);
59     if(getStateSet())
60         out->writeStateSet(getStateSet());
61 
62     // Write UpdateCallback if any
63     osg::AnimationPathCallback* nc = dynamic_cast<osg::AnimationPathCallback*>(getUpdateCallback());
64     out->writeBool(nc!=0);
65     if(nc)
66         {
67         ((ive::AnimationPathCallback*)(nc))->write(out);
68     }
69 
70     if (out->getVersion() >= VERSION_0006)
71     {
72         osg::ClusterCullingCallback* ccc = dynamic_cast<osg::ClusterCullingCallback*>(getCullCallback());
73         out->writeBool(ccc!=0);
74         if(ccc)
75         {
76             ((ive::ClusterCullingCallback*)(ccc))->write(out);
77         }
78     }
79 
80 
81     if (out->getVersion() >= VERSION_0039)
82     {
83         osgVolume::PropertyAdjustmentCallback* pac = dynamic_cast<osgVolume::PropertyAdjustmentCallback*>(getEventCallback());
84         out->writeBool(pac!=0);
85         if(pac)
86         {
87             ((ive::VolumePropertyAdjustmentCallback*)pac)->write(out);
88         }
89     }
90 
91     if (out->getVersion() >= VERSION_0010)
92     {
93         const osg::BoundingSphere& bs = getInitialBound();
94         out->writeBool(bs.valid());
95         if (bs.valid())
96         {
97             out->writeVec3(bs.center());
98             out->writeFloat(bs.radius());
99         }
100     }
101 
102     // Write NodeMask
103     out->writeUInt(getNodeMask());
104 }
105 
106 
read(DataInputStream * in)107 void Node::read(DataInputStream* in){
108     // Peak on the identification id.
109     int id = in->peekInt();
110 
111     if(id == IVENODE){
112         id = in->readInt();
113         osg::Object*  obj = dynamic_cast<osg::Object*>(this);
114         if(obj){
115             ((ive::Object*)(obj))->read(in);
116         }
117         else
118             in_THROW_EXCEPTION("Node::read(): Could not cast this osg::Node to an osg::Object.");
119 
120         if ( in->getVersion() < VERSION_0012 )
121         {
122             // Read name
123             setName(in->readString());
124         }
125         // Read Culling active
126         setCullingActive(in->readBool());
127         // Read descriptions
128         int nDesc = in->readInt();
129         if(nDesc!=0){
130             for(int i=0;i<nDesc;i++)
131                  addDescription(in->readString());
132         }
133         // Read StateSet if any
134         if(in->readBool())
135         {
136             setStateSet(in->readStateSet());
137         }
138 
139         // Read UpdateCallback if any
140         if(in->readBool())
141         {
142             osg::AnimationPathCallback* nc = new osg::AnimationPathCallback();
143             ((ive::AnimationPathCallback*)(nc))->read(in);
144             setUpdateCallback(nc);
145         }
146 
147         if (in->getVersion() >= VERSION_0006)
148         {
149             if(in->readBool())
150             {
151                 osg::ClusterCullingCallback* ccc = new osg::ClusterCullingCallback();
152                 ((ive::ClusterCullingCallback*)(ccc))->read(in);
153                 setCullCallback(ccc);
154             }
155         }
156 
157         if (in->getVersion() >= VERSION_0039)
158         {
159             if(in->readBool())
160             {
161                 int pacID = in->peekInt();
162                 if (pacID==IVEVOLUMEPROPERTYADJUSTMENTCALLBACK)
163                 {
164                     osgVolume::PropertyAdjustmentCallback* pac = new osgVolume::PropertyAdjustmentCallback();
165                     ((ive::VolumePropertyAdjustmentCallback*)(pac))->read(in);
166                     setEventCallback(pac);
167                 }
168                 else
169                 {
170                     in_THROW_EXCEPTION("Unknown event callback identification in Node::read()");
171                 }
172 
173             }
174         }
175 
176         if (in->getVersion() >= VERSION_0010)
177         {
178             if (in->readBool())
179             {
180                 osg::BoundingSphere bs;
181                 bs.center() = in->readVec3();
182                 bs.radius() = in->readFloat();
183                 setInitialBound(bs);
184             }
185         }
186 
187         // Read NodeMask
188         setNodeMask(in->readUInt());
189     }
190     else{
191         in_THROW_EXCEPTION("Node::read(): Expected Node identification");
192     }
193 }
194