21 #ifndef SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_SHAPE_READER_H_
22 #define SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_SHAPE_READER_H_
31 #include "./ShapeWithId.h"
33 namespace snark {
namespace graphics {
namespace View {
35 template<
typename S >
36 class ShapeReader :
public Reader
39 ShapeReader( QGLView& viewer, comma::csv::options& options, std::size_t size, coloured* c,
unsigned int pointSize,
const std::string& label );
42 void update(
const Eigen::Vector3d& offset );
43 const Eigen::Vector3d& somePoint()
const;
45 void render( QGLPainter *painter = NULL );
49 typedef std::deque< ShapeWithId< S > > DequeType;
51 mutable boost::mutex m_mutex;
52 boost::scoped_ptr< comma::csv::input_stream< ShapeWithId< S > > > m_stream;
53 qt3d::vertex_buffer m_buffer;
54 std::vector< std::pair< QVector3D, std::string > > m_labels;
55 unsigned int m_labelIndex;
56 unsigned int m_labelSize;
60 template<
typename S >
61 ShapeReader< S >::ShapeReader( QGLView& viewer, comma::csv::options& options, std::size_t size, coloured* c,
unsigned int pointSize,
const std::string& label ):
62 Reader( viewer, options, size, c, pointSize, label ),
63 m_buffer( size * Shapetraits< S >::size ),
70 template<
typename S >
71 inline void ShapeReader< S >::start()
73 m_thread.reset(
new boost::thread( boost::bind( &Reader::read, boost::ref( *
this ) ) ) );
76 template<
typename S >
77 inline void ShapeReader< S >::update(
const Eigen::Vector3d& offset )
79 boost::mutex::scoped_lock lock( m_mutex );
80 for(
typename DequeType::iterator it = m_deque.begin(); it != m_deque.end(); ++it )
82 Shapetraits< S >::update( it->shape, offset, it->color, it->block, m_buffer, m_extents );
85 updatePoint( offset );
88 template<
typename S >
89 inline bool ShapeReader< S >::empty()
const
91 boost::mutex::scoped_lock lock( m_mutex );
92 return m_deque.empty();
95 template<
typename S >
96 inline const Eigen::Vector3d& ShapeReader< S >::somePoint()
const
98 boost::mutex::scoped_lock lock( m_mutex );
99 return Shapetraits< S >::somePoint( m_deque.front().shape );
102 template<
typename S >
103 inline void ShapeReader< S >::render( QGLPainter* painter )
105 painter->setStandardEffect(QGL::FlatPerVertexColor);
106 painter->clearAttributes();
107 painter->setVertexAttribute(QGL::Position, m_buffer.points() );
108 painter->setVertexAttribute(QGL::Color, m_buffer.color() );
110 Shapetraits< S >::draw( painter, m_buffer.size(), m_buffer.index() );
111 for(
unsigned int i = 0; i < m_labelSize; i++ )
113 drawLabel( painter, m_labels[ i ].first, m_labels[ i ].second );
115 if( !m_label.empty() )
117 drawLabel( painter, m_translation );
121 template<
typename S >
122 inline bool ShapeReader< S >::readOnce()
136 m_stream.reset(
new comma::csv::input_stream< ShapeWithId< S > >( *m_istream(), options ) );
138 const ShapeWithId< S >* p = m_stream->read();
144 ShapeWithId< S > v = *p;
145 Eigen::Vector3d center = Shapetraits< S >::center( v.shape );
146 if( !v.label.empty() )
148 m_labels[ m_labelIndex ] = std::make_pair( QVector3D( center.x(), center.y(), center.z() ) , v.label );
150 if( m_labelSize < m_labels.size() )
154 if( m_labelIndex > m_labels.size() )
159 v.color = m_colored->color( center, p->id, p->scalar, p->color );
160 boost::mutex::scoped_lock lock( m_mutex );
161 m_deque.push_back( v );
162 m_point = Shapetraits< S >::somePoint( v.shape );
166 catch( std::exception& ex ) { std::cerr <<
"view-points: " << ex.what() << std::endl; }
167 catch( ... ) { std::cerr <<
"view-points: unknown exception" << std::endl; }
173 #endif // SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_SHAPE_READER_H_