Line data Source code
1 :
2 : /* Copyright (c) 2009-2013, Stefan Eilemann <eile@equalizergraphics.com>
3 : * 2011, Daniel Nachbaur <danielnachbaur@gmail.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify it under
6 : * the terms of the GNU Lesser General Public License version 2.1 as published
7 : * by the Free Software Foundation.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 : * details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public License
15 : * along with this library; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #include "observer.h"
20 :
21 : #include "leafVisitor.h"
22 : #include "log.h"
23 : #include "paths.h"
24 :
25 : #include <co/dataIStream.h>
26 : #include <co/dataOStream.h>
27 :
28 : namespace eq
29 : {
30 : namespace fabric
31 : {
32 :
33 : template< typename C, typename O >
34 227 : Observer< C, O >::Observer( C* config )
35 227 : : _config( config )
36 : {
37 227 : LBASSERT( config );
38 227 : config->_addObserver( static_cast< O* >( this ));
39 :
40 227 : const float eyeBase_2 = config->getFAttribute( C::FATTR_EYE_BASE ) * .5f;
41 227 : setEyePosition( EYE_LEFT, Vector3f( -eyeBase_2, 0.f, 0.f ));
42 227 : setEyePosition( EYE_CYCLOP, Vector3f::ZERO );
43 227 : setEyePosition( EYE_RIGHT, Vector3f( eyeBase_2, 0.f, 0.f ));
44 227 : LBLOG( LOG_INIT ) << "New " << lunchbox::className( this ) << std::endl;
45 227 : }
46 :
47 : template< typename C, typename O >
48 226 : Observer< C, O >::~Observer()
49 : {
50 226 : LBLOG( LOG_INIT ) << "Delete " << lunchbox::className( this ) << std::endl;
51 226 : _config->_removeObserver( static_cast< O* >( this ));
52 452 : }
53 :
54 : template< typename C, typename O >
55 454 : Observer< C, O >::BackupData::BackupData()
56 : : headMatrix( Matrix4f::IDENTITY )
57 : , focusDistance( 1.f )
58 : , focusMode( FOCUSMODE_FIXED )
59 454 : , camera( OFF )
60 : {
61 1816 : for( size_t i = 0; i < NUM_EYES; ++i )
62 1362 : eyePosition[ i ] = Vector3f::ZERO;
63 454 : eyePosition[ EYE_LEFT_BIT ].x() = -.05f;
64 454 : eyePosition[ EYE_RIGHT_BIT ].x() = .05f;
65 454 : }
66 :
67 : template< typename C, typename O >
68 10 : void Observer< C, O >::backup()
69 : {
70 10 : Object::backup();
71 10 : _backup = _data;
72 10 : }
73 :
74 : template< typename C, typename O >
75 2 : void Observer< C, O >::restore()
76 : {
77 2 : _data = _backup;
78 2 : Object::restore();
79 2 : setDirty( DIRTY_EYE_POSITION | DIRTY_HEAD | DIRTY_FOCUS );
80 2 : }
81 :
82 : template< typename C, typename O >
83 36 : void Observer< C, O >::serialize( co::DataOStream& os,
84 : const uint64_t dirtyBits )
85 : {
86 36 : Object::serialize( os, dirtyBits );
87 :
88 36 : if( dirtyBits & DIRTY_HEAD )
89 20 : os << _data.headMatrix;
90 36 : if( dirtyBits & DIRTY_EYE_POSITION )
91 144 : for( size_t i = 0; i < NUM_EYES; ++i )
92 108 : os << _data.eyePosition[i];
93 36 : if( dirtyBits & DIRTY_FOCUS )
94 20 : os << _data.focusDistance << _data.focusMode;
95 36 : if( dirtyBits & DIRTY_TRACKER )
96 20 : os << _data.camera << _data.vrpnTracker;
97 36 : }
98 :
99 : template< typename C, typename O >
100 26 : void Observer< C, O >::deserialize( co::DataIStream& is,
101 : const uint64_t dirtyBits )
102 : {
103 26 : Object::deserialize( is, dirtyBits );
104 :
105 26 : if( dirtyBits & DIRTY_HEAD )
106 10 : is >> _data.headMatrix;
107 26 : if( dirtyBits & DIRTY_EYE_POSITION )
108 104 : for( size_t i = 0; i < NUM_EYES; ++i )
109 78 : is >> _data.eyePosition[i];
110 26 : if( dirtyBits & DIRTY_FOCUS )
111 10 : is >> _data.focusDistance >> _data.focusMode;
112 26 : if( dirtyBits & DIRTY_TRACKER )
113 10 : is >> _data.camera >> _data.vrpnTracker;
114 26 : }
115 :
116 : template< typename C, typename O >
117 472 : void Observer< C, O >::setDirty( const uint64_t dirtyBits )
118 : {
119 472 : Object::setDirty( dirtyBits );
120 472 : _config->setDirty( C::DIRTY_OBSERVERS );
121 472 : }
122 :
123 : template< typename C, typename O >
124 3727 : VisitorResult Observer< C, O >::accept( Visitor& visitor )
125 : {
126 3727 : return visitor.visit( static_cast< O* >( this ));
127 : }
128 :
129 : template< typename C, typename O >
130 0 : VisitorResult Observer< C, O >::accept( Visitor& visitor ) const
131 : {
132 0 : return visitor.visit( static_cast< const O* >( this ));
133 : }
134 :
135 : template< typename C, typename O >
136 0 : ObserverPath Observer< C, O >::getPath() const
137 : {
138 0 : const std::vector< O* >& observers = _config->getObservers();
139 : typename std::vector< O* >::const_iterator i = std::find( observers.begin(),
140 : observers.end(),
141 0 : this );
142 0 : LBASSERT( i != observers.end( ));
143 :
144 0 : ObserverPath path;
145 0 : path.observerIndex = std::distance( observers.begin(), i );
146 0 : return path;
147 : }
148 :
149 : #ifdef EQ_1_0_API
150 : template< typename C, typename O >
151 0 : void Observer< C, O >::setEyeBase( const float eyeBase )
152 : {
153 0 : const float eyeBase_2 = eyeBase * .5f;
154 0 : setEyePosition( EYE_LEFT, Vector3f( -eyeBase_2, 0.f, 0.f ));
155 0 : setEyePosition( EYE_CYCLOP, Vector3f::ZERO );
156 0 : setEyePosition( EYE_RIGHT, Vector3f( eyeBase_2, 0.f, 0.f ));
157 0 : }
158 :
159 : template< typename C, typename O >
160 0 : float Observer< C, O >::getEyeBase() const
161 : {
162 0 : return (getEyePosition( EYE_LEFT ) - getEyePosition( EYE_RIGHT )).length();
163 : }
164 : #endif
165 :
166 : template< typename C, typename O >
167 990 : void Observer< C, O >::setEyePosition( const Eye eye, const Vector3f& pos )
168 : {
169 990 : LBASSERT( lunchbox::getIndexOfLastBit( eye ) <= EYE_LAST );
170 990 : Vector3f& position = _data.eyePosition[ lunchbox::getIndexOfLastBit( eye )];
171 990 : if( position == pos )
172 1526 : return;
173 :
174 454 : position = pos;
175 454 : setDirty( DIRTY_EYE_POSITION );
176 : }
177 :
178 : template< typename C, typename O >
179 1062 : const Vector3f& Observer< C, O >::getEyePosition( const Eye eye ) const
180 : {
181 1062 : LBASSERT( lunchbox::getIndexOfLastBit( eye ) <= EYE_LAST );
182 1062 : return _data.eyePosition[ lunchbox::getIndexOfLastBit( eye )];
183 : }
184 :
185 : template< typename C, typename O >
186 104 : void Observer< C, O >::setFocusDistance( const float focusDistance )
187 : {
188 104 : if( _data.focusDistance == focusDistance )
189 208 : return;
190 :
191 0 : _data.focusDistance = focusDistance;
192 0 : setDirty( DIRTY_FOCUS );
193 : }
194 :
195 : template< typename C, typename O >
196 104 : void Observer< C, O >::setFocusMode( const FocusMode focusMode )
197 : {
198 104 : if( _data.focusMode == focusMode )
199 206 : return;
200 :
201 2 : _data.focusMode = focusMode;
202 2 : setDirty( DIRTY_FOCUS );
203 : }
204 :
205 : template< typename C, typename O >
206 103 : void Observer< C, O >::setOpenCVCamera( const int32_t camera )
207 : {
208 103 : if( _data.camera == camera )
209 206 : return;
210 :
211 0 : _data.camera = camera;
212 0 : setDirty( DIRTY_TRACKER );
213 : }
214 :
215 : template< typename C, typename O >
216 0 : void Observer< C, O >::setVRPNTracker( const std::string& tracker )
217 : {
218 0 : if( _data.vrpnTracker == tracker )
219 0 : return;
220 :
221 0 : _data.vrpnTracker = tracker;
222 0 : setDirty( DIRTY_TRACKER );
223 : }
224 :
225 : template< typename C, typename O >
226 0 : bool Observer< C, O >::setHeadMatrix( const Matrix4f& matrix )
227 : {
228 0 : if( _data.headMatrix == matrix )
229 0 : return false;
230 :
231 0 : _data.headMatrix = matrix;
232 0 : setDirty( DIRTY_HEAD );
233 0 : return true;
234 : }
235 :
236 : template< typename C, typename O >
237 121 : std::ostream& operator << ( std::ostream& os, const Observer< C, O >& observer )
238 : {
239 121 : os << lunchbox::disableFlush << lunchbox::disableHeader << "observer"
240 : << std::endl;
241 121 : os << "{" << std::endl << lunchbox::indent;
242 :
243 121 : const std::string& name = observer.getName();
244 121 : if( !name.empty( ))
245 2 : os << "name \"" << name << "\"" << std::endl;
246 :
247 242 : os << "eye_left " << observer.getEyePosition( EYE_LEFT ) << std::endl
248 242 : << "eye_cyclop " << observer.getEyePosition( EYE_CYCLOP ) <<std::endl
249 242 : << "eye_right " << observer.getEyePosition( EYE_RIGHT ) << std::endl
250 242 : << "focus_distance " << observer.getFocusDistance() << std::endl
251 363 : << "focus_mode " << observer.getFocusMode() << std::endl
252 242 : << "opencv_camera " << IAttribute( observer.getOpenCVCamera( ))
253 : << std::endl;
254 121 : if( !observer.getVRPNTracker().empty( ))
255 0 : os << "vrpn_tracker \"" << observer.getVRPNTracker() << "\""
256 : << std::endl;
257 121 : os << lunchbox::exdent << "}" << std::endl << lunchbox::enableHeader
258 : << lunchbox::enableFlush;
259 121 : return os;
260 : }
261 :
262 : }
263 : }
|