RTFX  0.3
Real time special effects collaborative visualization and production library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
RTFXObject.h
Go to the documentation of this file.
1 #ifndef __RTFX_OBJECT__
2 #define __RTFX_OBJECT__
3 
20 #include "RTFXBoostIncludes.h"
21 #include "RTFXMath.h"
22 #include "RTFXRecorder.h"
23 #include "RTFXHelpers.h"
24 #include "RTFXPropertyIncludes.h"
25 #include "RTFX_API.h"
26 
27 using namespace std;
28 
29 namespace RTFX
30 {
31 
38  class RTFXAPI RTFXObject
39  {
40  public:
41 
45  {
46  NONE = 0,
47  CAMERA = 1,
48  LIGHT = 2,
49  GEOMETRY = 3,
50  POINT = 4,
51  FACE = 5,
52  BONE = 6,
53  SKELETON = 7,
54  PARTICLESYSTEM = 8,
55  RAW = 9,
56  TRANSFORM = 10,
57  SCRIPT = 11,
58  CUSTOM = 254,
59  };
60 
63  RTFXObject() : hasChanged( true ), shouldBeDeleted( false ), shouldBeResent( false ), isPartial( false )
64  {
65  // Set default OWNER and ID
66  id = boost::uuids::nil_uuid();
67  owner = boost::uuids::nil_uuid();
68 
69  // Initialize the base property ID
70  nextPropertyID = 0;
71 
72  isNew = true;
73 
74  // Create the name property
75  RTFXPropertyString * nameProperty = new RTFXPropertyString();
76  nameProperty->SetAll( "name", "name of property", true, GetANewPropertyID() );
77  nameProperty->SetDataType( RTFXProperty::DATASTRING );
78  nameProperty->SetValue( "object" );
79  nameProperty->SetBasis( false );
80 
81  propertyMap[ nameProperty->GetID() ] = nameProperty;
82  propertyNameLookup[ nameProperty->GetName() ] = nameProperty->GetID();
83  nameProperty->SetOwner( id );
84  name = "object";
85  }
86 
88  virtual ~RTFXObject()
89  {
90  debug( ostringstream().flush() << "RTFXObject::Destructor: " << name << endl );
91  // Erase the elements of the propertyNameLookups
92  DeleteAllProperties();
93 
94  nextPropertyID = 0;
95  };
96 
97 
99  RTFXObject( const RTFXObject &_o )
100  {
101  debug( ostringstream().flush() << "RTFXObject::Copy Constructor: copying data - " << _o.name << endl );
102  // Set the RTFXObject base properties
103  SetType( _o.type );
104 
105  SetID( _o.id );
106 
107  SetChanged( true );
108  SetDeleteMe( _o.shouldBeDeleted );
109  SetResend( _o.shouldBeResent );
110  SetPartial( _o.isPartial );
111  SetName( _o.name );
112  SetDescription( _o.description );
113  SetNew( _o.isNew );
114 
115  CopyProperties( &_o );
116 
117  SetIsInstance( _o.isInstance );
118  SetInstanceOfID( _o.instanceOfID );
119  }
120 
122  RTFXObjectType GetType()
123  {
124  // boost::lock_guard<boost::mutex> lock( *dataLock );
125  return type;
126  }
127 
129  void SetType( RTFXObjectType _type )
130  {
131  // boost::lock_guard<boost::mutex> lock( *dataLock );
132  type = _type;
133  }
134 
136  boost::uuids::uuid GetID()
137  {
138  // boost::lock_guard<boost::mutex> lock( *dataLock );
139  return id;
140  }
141 
143  void SetID( boost::uuids::uuid _id )
144  {
145  // boost::lock_guard<boost::mutex> lock( *dataLock );
146  id = _id;
147  RTFXPropertyString * ptr_ = ( RTFXPropertyString * )GetProperty( "name" );
148  ptr_->SetOwner( id );
149  }
150 
152  boost::uuids::uuid GetOwner()
153  {
154  // boost::lock_guard<boost::mutex> lock( *dataLock );
155  return owner;
156  }
157 
159  void SetOwner( boost::uuids::uuid _owner )
160  {
161  // boost::lock_guard<boost::mutex> lock( *dataLock );
162  owner = _owner;
163  }
164 
166  bool GetChanged()
167  {
168  // boost::lock_guard<boost::mutex> lock( *dataLock );
169  return hasChanged;
170  }
171 
173  void SetChanged( bool _changed )
174  {
175  // boost::lock_guard<boost::mutex> lock( *dataLock );
176  hasChanged = _changed;
177  }
178 
180  bool GetDeleteMe() { return shouldBeDeleted; }
181 
183  void SetDeleteMe( bool _deleteme ) { shouldBeDeleted = _deleteme; }
184 
186  bool GetResend() { return shouldBeResent; }
187 
189  void SetResend( bool _resend ) { shouldBeResent = _resend; }
190 
192  bool GetPartial()
193  {
194  // boost::lock_guard<boost::mutex> lock( *dataLock );
195  return isPartial;
196  }
197 
199  void SetPartial( bool _partial )
200  {
201  // boost::lock_guard<boost::mutex> lock( *dataLock );
202  isPartial = _partial;
203  }
204 
206  std::string GetName()
207  {
208  // boost::lock_guard<boost::mutex> lock( *dataLock );
209  RTFXPropertyString * ptr_ = ( RTFXPropertyString * )GetProperty( "name" );
210  name = ptr_->GetValue();
211  return name;
212  }
213 
215  void SetName( std::string _name )
216  {
217  // boost::lock_guard<boost::mutex> lock( *dataLock );
218  name = _name;
219  RTFXPropertyString * tmp_ = (RTFXPropertyString *)GetProperty( "name" );
220  tmp_->SetValue( _name );
221  }
222 
224  std::string GetDescription()
225  {
226  // boost::lock_guard<boost::mutex> lock( *dataLock );
227  return description;
228  }
229 
231  void SetDescription( std::string _description )
232  {
233  // boost::lock_guard<boost::mutex> lock( *dataLock );
234  description = _description;
235  }
236 
240  void AddProperty( RTFXProperty * _property )
241  {
242  // boost::lock_guard<boost::mutex> lock( *dataLock );
243  if ( PropertyExists( _property->GetName() ) )
244  return;
245 
246  if ( _property->GetID() == 0 && nextPropertyID != 0 )
247  {
248  _property->SetID( nextPropertyID );
249  nextPropertyID ++;
250  }
251 
252  propertyMap[ _property->GetID() ] = _property;
253  propertyNameLookup[ _property->GetName() ] = _property->GetID();
254  _property->SetOwner( id );
255  }
256 
258  bool PropertyExists( unsigned int _id )
259  {
260  // boost::lock_guard<boost::mutex> lock( *dataLock );
261  if ( propertyMap.find( _id ) == propertyMap.end() )
262  return false;
263  return true;
264  }
265 
268  bool PropertyExists( std::string _propertyName )
269  {
270  // boost::lock_guard<boost::mutex> lock( *dataLock );
271  if ( propertyNameLookup.find( _propertyName ) == propertyNameLookup.end() )
272  return false;
273  return true;
274  }
275 
277  unsigned int GetANewPropertyID()
278  {
279  // boost::lock_guard<boost::mutex> lock( *dataLock );
280  unsigned int returnValue_ = nextPropertyID;
281  nextPropertyID ++;
282 
283  return returnValue_;
284  }
285 
288  RTFXProperty * GetProperty( std::string _propertyName )
289  {
290  // Lookup the id of the property
291  // boost::lock_guard<boost::mutex> lock( *dataLock );
292  unsigned int propertyID_ = propertyNameLookup[ _propertyName ];
293  return propertyMap[ propertyID_ ];
294  }
295 
298  RTFXProperty * GetProperty( unsigned int _id )
299  {
300  // boost::lock_guard<boost::mutex> lock( *dataLock );
301  return propertyMap[ _id ];
302  }
303 
305  void DeleteProperty( unsigned int _id )
306  {
307  // boost::lock_guard<boost::mutex> lock( *dataLock );
308 
309  RTFXProperty * ptr_ = GetProperty( _id );
310  propertyMap.erase( _id );
311  propertyNameLookup.erase( ptr_->GetName() );
312 
313  delete ptr_;
314  }
315 
319  void DeletePropertyByName( std::string _propertyName )
320  {
321  // boost::lock_guard<boost::mutex> lock( *dataLock );
322 
323  RTFXProperty * ptr_ = GetProperty( _propertyName );
324  propertyMap.erase( ptr_->GetID() );
325  propertyNameLookup.erase( _propertyName );
326 
327  delete ptr_;
328  }
329 
331  virtual void Update( RTFXObject * _object ) = 0;
332 
337  virtual bool EqualData( RTFXObject * _object )
338  {
339  // boost::lock_guard<boost::mutex> lock( *dataLock );
340 
341  // Assume the objects are equal to start with
342  bool equal_ = true;
343 
344  if ( name.compare( _object->name ) != 0 )
345  return false;
346 
347  if ( description.compare( _object->description ) != 0 )
348  return false;
349 
350  if ( isInstance != _object->isInstance )
351  return false;
352 
353  if ( isInstance && ( instanceOfID != _object->instanceOfID ) )
354  return false;
355 
356  // Compare properties
357  if ( propertyMap.size() != _object->propertyMap.size() )
358  return false;
359 
360  // Compare individual properties
361  std::map<std::string, unsigned int>::iterator iter_ = propertyNameLookup.begin();
362  while ( iter_ != propertyNameLookup.end() )
363  {
364  RTFXProperty * ptr1_ = GetProperty( (*iter_).first );
365  RTFXProperty * ptr2_ = _object->GetProperty( (*iter_).first );
366 
367  if ( ptr1_ == NULL || ptr2_ == NULL )
368  return false;
369  else
370  equal_ &= ptr1_->EqualTo( ptr2_ );
371  }
372 
373  return equal_;
374  }
375 
377  virtual void ToNative( AXISFRAME _frame ) = 0;
378 
380  virtual void FromNative( AXISFRAME _frame ) = 0;
381 
383  virtual void ChangeFrame( AXISFRAME _from, AXISFRAME _to ) = 0;
384 
387  virtual std::vector<RTFXProperty *> GetChangedProperties()
388  {
389  // boost::lock_guard<boost::mutex> lock( *dataLock );
390  std::vector<RTFXProperty *> changedProperties_;
391 
392  std::map<unsigned int, RTFXProperty *>::iterator iter_ = propertyMap.begin();
393 
394  while ( iter_ != propertyMap.end() )
395  {
396  RTFXProperty * ptr_ = (*iter_).second;
397  if ( ptr_->GetChanged() )
398  {
399  changedProperties_.push_back( ptr_ );
400  }
401  iter_ ++;
402  }
403 
404  return changedProperties_;
405  }
406 
408  virtual void SetAllPropertyChanged( bool _value )
409  {
410  // boost::lock_guard<boost::mutex> lock( *dataLock );
411  std::map<unsigned int, RTFXProperty *>::iterator iter_ = propertyMap.begin();
412 
413  while ( iter_ != propertyMap.end() )
414  {
415  verbose( ostringstream().flush() << "RTFXObject::SetAllPropertyChanged: property - " << (*iter_).first << endl );
416  RTFXProperty * ptr_ = (*iter_).second;
417  ptr_->SetChanged( _value );
418 
419  iter_ ++;
420  }
421  }
422 
424  virtual bool ExistChangedProperties()
425  {
426  // boost::lock_guard<boost::mutex> lock( *dataLock );
427  std::map<unsigned int, RTFXProperty *>::iterator iter_ = propertyMap.begin();
428  while ( iter_ != propertyMap.end() )
429  {
430  RTFXProperty * ptr_ = (*iter_).second;
431  if ( ptr_->GetChanged() )
432  return true;
433  }
434  return false;
435  }
436 
438  void DeleteAllProperties()
439  {
440  // boost::lock_guard<boost::mutex> lock( *dataLock );
441  map<unsigned int, RTFXProperty *>::iterator iter_ = propertyMap.begin();
442  while ( iter_ != propertyMap.end() )
443  {
444  RTFXProperty * ptr_ = ( *iter_ ).second;
445  ++ iter_;
446 
447  DeleteProperty( ptr_->GetID() );
448  }
449 
450  // Delete the memory from the map
451  {
452  map<unsigned int, RTFXProperty *> tmp_;
453  tmp_.swap( propertyMap );
454 
455  map<string, unsigned int> tmp2_;
456  tmp2_.swap( propertyNameLookup );
457  }
458  }
459 
461  void SetFrame( AXISFRAME _frame ) { frame = _frame; }
462 
464  AXISFRAME GetFrame() { return frame; }
465 
467  void CopyProperties( const RTFXObject * _obj )
468  {
469  std::map<unsigned int, RTFXProperty *>::const_iterator iter_ = _obj->propertyMap.begin();
470  while ( iter_ != _obj->propertyMap.end() )
471  {
472  RTFXProperty * ptr_ = (*iter_).second;
473 
474  // If property exists, udpate it
475  RTFXProperty * localProperty_ = NULL;
476  if ( PropertyExists( ptr_->GetName() ) )
477  localProperty_ = GetProperty( ptr_->GetName() );
478 
479  if ( localProperty_ != NULL )
480  localProperty_->Copy( ptr_ );
481 
482  // Otherwise, add new property
483  else
484  {
485  RTFXProperty * newPtr_;
486  RTFXProperty::RTFXPropertyData dataType_ = ptr_->GetDataType();
487 
488  switch ( ptr_->GetType() )
489  {
490  case RTFXProperty::BASICARRAY:
491  {
492  if ( dataType_ == RTFXProperty::CHAR )
493  newPtr_ = new RTFXPropertyArray<char>();
494  else if ( dataType_ == RTFXProperty::BOOL )
495  newPtr_ = new RTFXPropertyArray<bool>();
496  else if ( dataType_ == RTFXProperty::DOUBLE || dataType_ == RTFXProperty::UNKNOWN )
497  newPtr_ = new RTFXPropertyArray<double>();
498  else if ( dataType_ == RTFXProperty::FLOAT )
499  newPtr_ = new RTFXPropertyArray<float>();
500  else if ( dataType_ == RTFXProperty::INT )
501  newPtr_ = new RTFXPropertyArray<int>();
502  else if ( dataType_ == RTFXProperty::UNSIGNEDINT )
503  newPtr_ = new RTFXPropertyArray<unsigned int>();
504  break;
505  }
506  case RTFXProperty::BASICTYPE:
507  {
508  if ( dataType_ == RTFXProperty::CHAR )
509  newPtr_ = new RTFXPropertyBasic<char>();
510  else if ( dataType_ == RTFXProperty::BOOL )
511  newPtr_ = new RTFXPropertyBasic<bool>();
512  else if ( dataType_ == RTFXProperty::DOUBLE || dataType_ == RTFXProperty::UNKNOWN )
513  newPtr_ = new RTFXPropertyBasic<double>();
514  else if ( dataType_ == RTFXProperty::FLOAT )
515  newPtr_ = new RTFXPropertyBasic<float>();
516  else if ( dataType_ == RTFXProperty::INT )
517  newPtr_ = new RTFXPropertyBasic<int>();
518  else if ( dataType_ == RTFXProperty::UNSIGNEDINT )
519  newPtr_ = new RTFXPropertyBasic<unsigned int>();
520  break;
521  }
522  case RTFXProperty::CHARARRAY:
523  {
524  newPtr_ = new RTFXPropertyCharArray();
525  break;
526  }
527  case RTFXProperty::COLOUR:
528  {
529  newPtr_ = new RTFXPropertyColour();
530  break;
531  }
532  case RTFXProperty::MATRIX4:
533  {
534  newPtr_ = new RTFXPropertyMatrix4x4();
535  break;
536  }
537  case RTFXProperty::POINT3:
538  {
539  newPtr_ = new RTFXPropertyPoint3D();
540  break;
541  }
542  case RTFXProperty::RAWDATA:
543  {
544  newPtr_ = new RTFXPropertyRawData();
545  break;
546  }
547  case RTFXProperty::STRING:
548  {
549  newPtr_ = new RTFXPropertyString();
550  break;
551  }
552  case RTFXProperty::VECTOR3:
553  {
554  newPtr_ = new RTFXPropertyVector3D();
555  break;
556  }
557  case RTFXProperty::VECTOROF:
558  {
559  if ( dataType_ == RTFXProperty::CHAR )
560  newPtr_ = new RTFXPropertyVectorOf<char>();
561  else if ( dataType_ == RTFXProperty::BOOL )
562  newPtr_ = new RTFXPropertyVectorOf<bool>();
563  else if ( dataType_ == RTFXProperty::DATASTRING )
564  newPtr_ = new RTFXPropertyVectorOf<string>();
565  else if ( dataType_ == RTFXProperty::DOUBLE || dataType_ == RTFXProperty::UNKNOWN )
566  newPtr_ = new RTFXPropertyVectorOf<double>();
567  else if ( dataType_ == RTFXProperty::FLOAT )
568  newPtr_ = new RTFXPropertyVectorOf<float>();
569  else if ( dataType_ == RTFXProperty::INT )
570  newPtr_ = new RTFXPropertyVectorOf<int>();
571  else if ( dataType_ == RTFXProperty::UNSIGNEDINT )
572  newPtr_ = new RTFXPropertyVectorOf<unsigned int>();
573  break;
574  }
575  case RTFXProperty::DELETEOBJECT:
576  {
577  newPtr_ = new RTFXPropertyDeleteObject();
578  break;
579  }
580  default:
581  {
582  break;
583  }
584  }
585 
586  if ( newPtr_ != NULL )
587  {
588  newPtr_->Copy( ptr_ );
589  AddProperty( newPtr_ );
590  }
591  }
592 
593  iter_ ++;
594  }
595  }
596 
598  void SetIsInstance( bool _isInstance ) { isInstance = _isInstance; }
599 
601  bool GetIsInstance() { return isInstance; }
602 
604  void SetInstanceOfID( boost::uuids::uuid _instanceOfID ) { instanceOfID = _instanceOfID; }
605 
607  boost::uuids::uuid GetInstanceOfID() { return instanceOfID; }
608 
610  void SetNew( bool _isnew ) { isNew = _isnew; }
611 
613  bool GetIsNew() { return isNew; }
614 
616  void RemovePropertyReference( string _name )
617  {
618  RTFXProperty * ptr_ = NULL;
619  ptr_ = GetProperty( _name );
620  if ( ptr_ == NULL ) return;
621 
622  // Now we have a pointer to the property, must remove it from both maps
623  propertyMap.erase( ptr_->GetID() );
624  propertyNameLookup.erase( _name );
625  }
626 
627  protected:
629  unsigned int GetPropertyCount() { return nextPropertyID; }
630 
632  std::map<unsigned int, RTFXProperty *> * GetPropertyMapPointer() { return &propertyMap; }
633 
635  boost::mutex * dataLock;
636 
638  size_t ComputeTotalSize() { return sizeOfObject; }
639 
641  size_t GetSize() { return sizeOfObject; }
642 
643  private:
644 
646  size_t sizeOfObject;
647 
649  bool isNew;
650 
653 
655  boost::uuids::uuid id;
656 
658  boost::uuids::uuid owner;
659 
664 
668 
671 
674  bool isPartial;
675 
677  AXISFRAME frame;
678 
680  std::string name;
681 
683  std::string description;
684 
686  unsigned int nextPropertyID;
687 
690  std::map< unsigned int, RTFXProperty * > propertyMap;
691 
693  std::map< std::string, unsigned int > propertyNameLookup;
694 
698 
700  boost::uuids::uuid instanceOfID;
701 
702  friend class boost::serialization::access;
703 
704  template<class Archive>
705  void serialize( Archive &_ar, const unsigned int _version )
706  {
707  // debug( ostringstream().flush() << "RTFXObject::serialize" << endl );
708  _ar & type & id & owner & hasChanged & shouldBeDeleted & shouldBeResent & isPartial & isInstance & instanceOfID & name & description;
709  // debug( ostringstream().flush() << "RTFXObject::serialize: properties" << endl );
710  _ar & nextPropertyID & propertyMap & propertyNameLookup;
711  // debug( ostringstream().flush() << "RTFXObject::serialize: done." << endl );
712  };
713  };
714 }
715 
716 BOOST_SERIALIZATION_ASSUME_ABSTRACT( RTFX::RTFXObject )
717 
718 #endif