11 #include <vtkAssembly.h> 12 #include <vtkCamera.h> 13 #include <vtkMatrix4x4.h> 14 #include <vtkTransform.h> 19 : m_pParentNode(
NULL )
21 m_pNodeAssembly = vtkSmartPointer< vtkAssembly >::New();
23 if( pParentNode !=
nullptr )
35 while( !m_vpChildNodes.empty() )
36 delete m_vpChildNodes.front();
49 assert( m_pNodeAssembly->GetNumberOfPaths() == 0 );
68 return ( m_pParentNode !=
NULL );
73 return ( m_pParentNode ==
NULL );
78 return ( !m_vpChildNodes.empty() );
83 return m_vpChildNodes.empty();
88 assert( ( iIndex > 0 ) && ( iIndex < (
int ) m_vpChildNodes.size() ) );
90 if( ( iIndex > 0 ) && ( iIndex < (
int ) m_vpChildNodes.size() ) )
91 return m_vpChildNodes[ iIndex ];
98 assert( ( iIndex > 0 ) && ( iIndex < (
int ) m_vpChildNodes.size() ) );
100 if( ( iIndex > 0 ) && ( iIndex < (
int ) m_vpChildNodes.size() ) )
101 return m_vpChildNodes[ iIndex ];
108 if( m_vpChildNodes.empty() ) {
113 vpChildren.resize( m_vpChildNodes.size() );
114 for(
size_t i = 0; i < m_vpChildNodes.size(); i++ )
115 vpChildren[ i ] = m_vpChildNodes[ i ];
119 if( m_vpChildNodes.empty() ) {
124 vpChildren.resize( m_vpChildNodes.size() );
125 for(
size_t i = 0; i < m_vpChildNodes.size(); i++ )
126 vpChildren[ i ] = m_vpChildNodes[ i ];
138 assert( pChild !=
NULL );
139 if( pChild ==
NULL )
return false;
142 assert( pChild !=
this );
143 if( pChild ==
this )
return false;
146 assert( pChild->
IsRoot() );
151 m_vpChildNodes.push_back( pChild );
154 pChild->m_pParentNode =
this;
158 m_pNodeAssembly->AddPart( pChild->m_pNodeAssembly );
166 for( std::vector<DAFFViz::SGNode*>::const_iterator cit = vpChildren.begin(); cit != vpChildren.end(); ++cit )
172 for( std::vector<DAFFViz::SGNode*>::iterator it = m_vpChildNodes.begin(); it != m_vpChildNodes.end(); ++it )
173 if( ( *it ) == pChild ) {
176 m_pNodeAssembly->RemovePart( pChild->m_pNodeAssembly );
180 pChild->m_pParentNode =
NULL;
183 m_vpChildNodes.erase( it );
193 for( std::vector<DAFFViz::SGNode*>::const_iterator cit = vpChildren.begin(); cit != vpChildren.end(); ++cit )
200 double* data = m_pNodeAssembly->GetPosition();
210 m_pNodeAssembly->SetPosition( x, y, z );
216 double* data = m_pNodeAssembly->GetOrientation();
217 rotx_deg = data[ 0 ];
218 roty_deg = data[ 1 ];
219 rotz_deg = data[ 2 ];
226 m_pNodeAssembly->SetOrientation( rotx_deg, roty_deg, rotz_deg );
232 double* pos = m_pNodeAssembly->GetPosition();
239 vtkSmartPointer< vtkMatrix4x4 > mOrigin = vtkSmartPointer< vtkMatrix4x4 >::New();
241 mOrigin->SetElement( 0, 3, -pos[ 0 ] );
242 mOrigin->SetElement( 1, 3, -pos[ 1 ] );
243 mOrigin->SetElement( 2, 3, -pos[ 2 ] );
246 vtkSmartPointer< vtkMatrix4x4 > mPosition = vtkSmartPointer< vtkMatrix4x4 >::New();
247 mPosition->Identity();
248 mPosition->SetElement( 0, 3, pos[ 0 ] );
249 mPosition->SetElement( 1, 3, pos[ 1 ] );
250 mPosition->SetElement( 2, 3, pos[ 2 ] );
261 vtkSmartPointer< vtkMatrix4x4 > mRoll = vtkSmartPointer< vtkMatrix4x4 >::New();
263 mRoll->SetElement( 0, 0, cos( r ) );
264 mRoll->SetElement( 0, 1, sin( r ) );
265 mRoll->SetElement( 1, 0, -sin( r ) );
266 mRoll->SetElement( 1, 1, cos( r ) );
276 vtkSmartPointer< vtkMatrix4x4 > mPitch = vtkSmartPointer< vtkMatrix4x4 >::New();
278 mPitch->SetElement( 1, 1, cos( p ) );
279 mPitch->SetElement( 1, 2, -sin( p ) );
280 mPitch->SetElement( 2, 1, sin( p ) );
281 mPitch->SetElement( 2, 2, cos( p ) );
291 vtkSmartPointer< vtkMatrix4x4 > mYaw = vtkSmartPointer< vtkMatrix4x4 >::New();
293 mYaw->SetElement( 0, 0, cos( y ) );
294 mYaw->SetElement( 0, 2, sin( y ) );
295 mYaw->SetElement( 2, 0, -sin( y ) );
296 mYaw->SetElement( 2, 2, cos( y ) );
301 vtkSmartPointer< vtkTransform > transform = vtkSmartPointer< vtkTransform >::New();
302 transform->Concatenate( mPosition );
303 transform->Concatenate( mYaw );
304 transform->Concatenate( mPitch );
305 transform->Concatenate( mRoll );
306 transform->Concatenate( mOrigin );
310 m_pNodeAssembly->SetUserTransform( transform );
317 double ux,
double uy,
double uz )
319 double* pos = m_pNodeAssembly->GetPosition();
322 vtkSmartPointer< vtkMatrix4x4 > mOrigin = vtkSmartPointer< vtkMatrix4x4 >::New();
324 mOrigin->SetElement( 0, 3, -pos[ 0 ] );
325 mOrigin->SetElement( 1, 3, -pos[ 1 ] );
326 mOrigin->SetElement( 2, 3, -pos[ 2 ] );
329 vtkSmartPointer< vtkMatrix4x4 > mPosition = vtkSmartPointer< vtkMatrix4x4 >::New();
330 mPosition->Identity();
331 mPosition->SetElement( 0, 3, pos[ 0 ] );
332 mPosition->SetElement( 1, 3, pos[ 1 ] );
333 mPosition->SetElement( 2, 3, pos[ 2 ] );
336 double zx = uz*vy - uy*vz;
337 double zy = ux*vz - uz*vx;
338 double zz = uy*vx - ux*vy;
340 vtkSmartPointer< vtkMatrix4x4 > mViewUp = vtkSmartPointer< vtkMatrix4x4 >::New();
342 mViewUp->SetElement( 0, 0, zx );
343 mViewUp->SetElement( 1, 0, zy );
344 mViewUp->SetElement( 2, 0, zz );
345 mViewUp->SetElement( 0, 1, ux );
346 mViewUp->SetElement( 1, 1, uy );
347 mViewUp->SetElement( 2, 1, uz );
348 mViewUp->SetElement( 0, 2, -vx );
349 mViewUp->SetElement( 1, 2, -vy );
350 mViewUp->SetElement( 2, 2, -vz );
354 vtkSmartPointer< vtkTransform > transform = vtkSmartPointer< vtkTransform >::New();
355 transform->Concatenate( mPosition );
356 transform->Concatenate( mViewUp );
357 transform->Concatenate( mOrigin );
361 m_pNodeAssembly->SetUserTransform( transform );
369 vtkSmartPointer< vtkMatrix4x4 > pM = m_pNodeAssembly->GetUserMatrix();
371 std::vector< double > vdView( 4 );
372 std::vector< double > vdDefaultView( 4 );
373 vdDefaultView[ 2 ] = -1.0f;
374 vdDefaultView[ 3 ] = 1.0f;
377 vdView[0] = *pM->MultiplyDoublePoint( &vdDefaultView[0] );
384 for(
size_t i = 0; i < 4; i++ )
385 vdView[ i ] = vdDefaultView[ i ];
388 std::vector< double > vdUp( 4 );
389 std::vector< double > vdDefaultUp( 4 );
390 vdDefaultUp[ 1 ] = vdDefaultUp[ 3 ] = 1.0f;
393 vdUp[0] = *pM->MultiplyDoublePoint( &vdDefaultUp[0] );
400 for(
size_t i = 0; i < 4; i++ )
401 vdUp[ i ] = vdDefaultUp[ i ];
407 double dEps = DAFF::EPSILON_D;
408 double dPi = DAFF::PI_D;
410 double vx, vy, vz, ux, uy, uz, yaw, pitch, roll;
413 if( vy >= ( 1 - dEps ) )
415 yaw = atan2( ux, uz );
426 if( vy <= -( 1 - dEps ) )
428 yaw = atan2( -ux, -uz );
439 yaw = atan2( -vx, -vz );
442 double zy = vz*ux - vx*uz;
444 if( ( uy <= dEps ) && ( uy >= -dEps ) ) {
446 double zy = vz*ux - vx*uz;
447 roll = ( zy <= 0 ? dPi: -dPi );
457 double cp = sqrt( 1 - vy*vy );
458 double uy_by_cp = uy / cp;
459 if( std::fabs( uy_by_cp - 1 ) < dEps )
462 roll = ( zy <= 0 ? acos( uy_by_cp ) : -acos( uy_by_cp ) );
473 double* data = m_pNodeAssembly->GetScale();
482 m_pNodeAssembly->SetScale( sx, sy, sz );
488 return m_pNodeAssembly->GetVisibility() > 0 ?
true :
false;
494 for( std::vector<SGNode*>::const_iterator cit = m_vpChildNodes.begin(); cit != m_vpChildNodes.end(); ++cit )
495 ( *cit )->SetVisible( bVisible );
499 m_pNodeAssembly->VisibilityOn();
501 m_pNodeAssembly->VisibilityOff();
507 assert( pActor !=
NULL );
510 m_pNodeAssembly->AddPart( pActor );
516 assert( pActor !=
NULL );
519 m_pNodeAssembly->RemovePart( pActor );
526 assert( pAssembly !=
NULL );
529 m_pNodeAssembly->AddPart( pAssembly );
535 assert( pAssembly !=
NULL );
538 m_pNodeAssembly->RemovePart( pAssembly );
545 for( std::vector<SGNode*>::const_iterator cit = m_vpChildNodes.begin(); cit != m_vpChildNodes.end(); ++cit )
546 ( *cit )->OnSetFollowerCamera( pCamera );
549 vtkSmartPointer< vtkAssembly > SGNode::GetNodeAssembly()
551 return m_pNodeAssembly;
void GetChildNodes(std::vector< const DAFFViz::SGNode * > &vpChildren) const
Returns all the pointers to the child nodes (for const correctness, clears vector if no childs availa...
void RemoveAssembly(vtkSmartPointer< vtkAssembly > pAssembly)
Release an assembly from the assembly of the node.
void AddActor(vtkSmartPointer< vtkActor > pActor)
Add a VTK actor to the node.
void SetPosition(double x, double y, double z)
Position getter (unit is meter)
bool AddChildNode(DAFFViz::SGNode *pChild)
Appends a single child node.
void SetScale(double sx, double sy, double sz)
Scale setter.
void SetOrientation(double dRotXDeg, double dRotYDeg, double dRotZDeg)
Sets the orientation of the node assembly based on world coordinate axis (angles in [°]) ...
void SetOrientationYPR(double dYawDeg, double dPitchDeg, double dRollDeg)
Sets the orientation of the node assembly based on yaw, pitch and roll angles around own coordinate a...
void RemoveActor(vtkSmartPointer< vtkActor > pActor)
Remove a VTK actor from the assembly of the node.
void SetOrientationVU(double vx, double vy, double vz, double ux, double uy, double uz)
Sets the orientation of the node assembly based on view- and up-vectors.
void AddAssembly(vtkSmartPointer< vtkAssembly > pAssembly)
Add a VTK assembly to the node.
virtual void OnSetFollowerCamera(vtkSmartPointer< vtkCamera > pCamera)
Set active camera for followers.
bool RemoveChildNodes(const std::vector< DAFFViz::SGNode * > &vpChildren)
Removes a vector of child nodes.
DAFFViz::SGNode * GetRootNode() const
Traverses up to the root (recursively) and returns the node pointer.
bool HasParentNode() const
Returns true if the node is appended to a parent node.
bool HasChildNodes() const
Returns true if the node combines one or more child nodes.
virtual void SetVisible(bool bVisible)
Set visibility.
void GetPosition(double &x, double &y, double &z) const
Position setter (unit is meter)
Base class for scene graph nodes.
bool RemoveChildNode(DAFFViz::SGNode *pChild)
Removes a single child node.
void GetOrientation(double &dRotXDeg, double &dRotYDeg, double &dRotZDeg) const
Orientation getter (angles in [°])
void GetOrientationYPR(double &dYawDeg, double &dPitchDeg, double &dRollDeg)
Orientation getter of the node assembly based on yaw, pitch and roll angles around own coordinate axi...
const DAFFViz::SGNode * GetChildNode(int iIndex) const
Returns the pointer to the child node of given index (for const correctness)
void GetOrientationVU(double &vx, double &vy, double &vz, double &ux, double &uy, double &uz)
Returns the orientation of the node assembly based on view- and up-vectors.
static double rad2grad(double phi)
DAFFViz::SGNode * GetParentNode() const
Returns the parent node pointer.
bool AddChildNodes(const std::vector< DAFFViz::SGNode * > &vpChildren)
Appends a vector child nodes.
#define DAFFVIZ_UNLOCK_VTK
void GetScale(double &sx, double &sy, double &sz) const
Scale getter.
static double grad2rad(double phi)
SGNode(DAFFViz::SGNode *pParentNode=NULL)
Constructor with optional linking to a parent node.
bool IsLeaf() const
Returns true if the node appears as a leaf node (... has no child nodes)
virtual ~SGNode()
Destructor.
bool IsRoot() const
Returns true if the node is a root node (... has no parent node)
virtual bool IsVisible() const
Returns true if the node is visible.