source: Swollen/swollen/state.cpp @ 865

Last change on this file since 865 was 125, checked in by darran, 19 years ago
  • adding -movie ability ...
File size: 2.3 KB
Line 
1/*
2    State class
3
4    An OpenSceneGraph viewer for pyVolution .sww files.
5    copyright (C) 2004-2005 Geoscience Australia
6*/
7
8
9#define MAX_LINE_LENGTH 200
10
11#include <state.h>
12
13
14// constructor
15State::State()
16{
17
18}
19
20
21State::~State()
22{
23}
24
25
26// serialize to named stream
27void State::write(std::ostream& s)
28{
29    s << _timestep << " ";
30    s << _position.x() << " " << _position.y() << " " << _position.z() << " ";
31    s << _orientation.x() << " " << _orientation.y() << " " << _orientation.z() << " " << _orientation.w() << " ";
32    s << _culling << " ";
33    s << _wireframe << std::endl;
34}
35
36
37// reconstitute self from named stream
38bool State::read(std::istream& s)
39{
40   s >> _timestep
41     >>  _position.x() >> _position.y() >> _position.z()
42     >>  _orientation.x() >> _orientation.y() >> _orientation.z() >> _orientation.w()
43     >> _culling
44     >> _wireframe;
45
46   return s.fail() ? false : true;
47}
48
49
50void State::setMatrix(osg::Matrix m)
51{
52   m.get(_orientation);
53   _position = m.getTrans();
54
55}
56
57osg::Matrix State::getMatrix()
58{
59   osg::Matrix m;
60   m.set(_orientation);
61   m.setTrans(_position);
62   return m;
63}
64
65
66
67
68StateList::StateList() : std::vector<State>()
69{
70}
71
72
73bool StateList::write(std::ostream& s)
74{
75   if( this->size() == 0 )
76      return false;
77
78   s << "# SWM DATA" << std::endl;
79   StateList::iterator iter = this->begin();
80   for( ; iter < this->end(); iter++ )
81      (*iter).write( s );
82
83   return true;
84}
85
86
87
88
89
90bool StateList::read(std::string filename)
91{
92
93   // attempt to open the macro file ...
94   std::fstream f;
95   f.open( filename.c_str(), std::fstream::in );
96   if( f.is_open() )
97   {
98      char str[MAX_LINE_LENGTH];
99      char *p;
100      int index = 0;
101
102      // search for data section
103      // FIXME: not very safe, what if data section missing?
104      do 
105      {
106         f.getline(str, MAX_LINE_LENGTH);
107         p = strstr(str, "# SWM DATA");
108         index++;
109
110      } while( p != str );
111
112
113      bool valid;
114      index = 0;
115      do
116      {
117         State* s = new State();
118         valid = s->read(f);
119         if( valid )
120         {
121            this->push_back( *s );
122            index++;
123         }
124      } while( valid );
125
126      std::cout << "Read " << index << " frames of animation for playback ..." << std::endl;
127
128      f.close();
129
130      // success only on non-empty animation
131      if( index )
132         return true;
133   }
134
135   return false;
136
137}
Note: See TracBrowser for help on using the repository browser.