21 #ifndef SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_SHAPEWITHID_H_
22 #define SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_SHAPEWITHID_H_
24 #include <boost/foreach.hpp>
25 #include <boost/optional.hpp>
26 #include <comma/base/types.h>
27 #include <comma/visiting/traits.h>
28 #include <snark/math/interval.h>
29 #include <snark/graphics/qt3d/rotation_matrix.h>
30 #include <snark/graphics/qt3d/vertex_buffer.h>
31 #include <Qt3D/qglnamespace.h>
32 #include <Qt3D/qglpainter.h>
34 namespace snark {
namespace graphics {
namespace View {
40 ShapeWithId() : id( 0 ), block( 0 ) {}
51 struct Shapetraits {};
54 struct Shapetraits< Eigen::Vector3d >
56 static const QGL::DrawingMode drawingMode = QGL::Points;
57 static const unsigned int size = 1;
59 static void update(
const Eigen::Vector3d& p,
const Eigen::Vector3d& offset,
const QColor4ub& color,
unsigned int block, qt3d::vertex_buffer& buffer, boost::optional<
snark::math::closed_interval< float, 3 > >& extents )
61 Eigen::Vector3d point = p - offset;
62 buffer.addVertex( QVector3D( point.x(), point.y(), point.z() ), color, block );
65 extents = extents->hull( point.cast<
float >() );
73 static void draw( QGLPainter* painter,
unsigned int size,
unsigned int index )
75 painter->draw( QGL::Points, size, index );
78 static const Eigen::Vector3d& somePoint(
const Eigen::Vector3d& point ) {
return point; }
80 static const Eigen::Vector3d& center(
const Eigen::Vector3d& point ) {
return point; }
84 struct Shapetraits< snark::math::closed_interval< double, 3 > >
86 static const unsigned int size = 8;
89 Eigen::Vector3f min = ( e.
min() - offset ).cast< float >();
90 Eigen::Vector3f max = ( e.
max() - offset ).cast< float >();
91 buffer.addVertex( QVector3D( min.x(), min.y(), min.z() ), color, block );
92 buffer.addVertex( QVector3D( min.x(), min.y(), max.z() ), color, block );
93 buffer.addVertex( QVector3D( min.x(), max.y(), max.z() ), color, block );
94 buffer.addVertex( QVector3D( min.x(), max.y(), min.z() ), color, block );
95 buffer.addVertex( QVector3D( max.x(), min.y(), min.z() ), color, block );
96 buffer.addVertex( QVector3D( max.x(), min.y(), max.z() ), color, block );
97 buffer.addVertex( QVector3D( max.x(), max.y(), max.z() ), color, block );
98 buffer.addVertex( QVector3D( max.x(), max.y(), min.z() ), color, block );
110 static void draw( QGLPainter* painter,
unsigned int size,
unsigned int index )
112 const boost::array< unsigned short, 8 > baseIndices = { { 0, 4, 1, 5, 2, 6, 3, 7 } };
113 for(
unsigned int i = 0; i < size; i += 8 )
116 painter->draw( QGL::LineLoop, 4, index + i );
117 painter->draw( QGL::LineLoop, 4, index + i + 4 );
120 boost::array< unsigned short, 8 > lineIndices = baseIndices;
121 BOOST_FOREACH(
unsigned short& j, lineIndices )
125 painter->draw( QGL::Lines, &lineIndices[0], 8 );
135 struct Shapetraits< std::pair< Eigen::Vector3d, Eigen::Vector3d > >
137 static const unsigned int size = 2;
138 static void update(
const std::pair< Eigen::Vector3d, Eigen::Vector3d >& p,
const Eigen::Vector3d& offset,
const QColor4ub& color,
unsigned int block, qt3d::vertex_buffer& buffer, boost::optional<
snark::math::closed_interval< float, 3 > >& extents )
140 Eigen::Vector3f first = ( p.first - offset ).cast< float >();
141 Eigen::Vector3f second = ( p.second - offset ).cast< float >();
142 buffer.addVertex( QVector3D( first.x(), first.y(), first.z() ), color, block );
143 buffer.addVertex( QVector3D( second.x(), second.y(), second.z() ), color, block );
154 static void draw( QGLPainter* painter,
unsigned int size,
unsigned int index )
156 painter->draw( QGL::Lines, size, index );
159 static const Eigen::Vector3d& somePoint(
const std::pair< Eigen::Vector3d, Eigen::Vector3d >& line ) {
return line.first; }
161 static Eigen::Vector3d center(
const std::pair< Eigen::Vector3d, Eigen::Vector3d >& line ) {
return ( line.first + line.second ) / 2; }
164 template < std::
size_t Size >
167 Eigen::Vector3d center;
168 Eigen::Vector3d orientation;
173 template < std::
size_t Size >
174 struct Shapetraits< Ellipse< Size > >
176 static const unsigned int size = Size;
177 static void update(
const Ellipse< Size >& ellipse,
const Eigen::Vector3d& offset,
const QColor4ub& color,
unsigned int block, qt3d::vertex_buffer& buffer, boost::optional<
snark::math::closed_interval< float, 3 > >& extents )
179 Eigen::Vector3d c = ellipse.center - offset;
181 static const double step = 3.14159265358979323846l * 2 / Size;
184 for( std::size_t i = 0; i < Size; ++i, angle += step )
186 Eigen::Vector3d v = r * Eigen::Vector3d( std::cos( angle ) * ellipse.major, std::sin( angle ) * ellipse.minor, 0 );
187 Eigen::Vector3d p( v.x(), v.y(), v.z() );
188 Eigen::Vector3f point = ( p + c ).cast< float >();
189 buffer.addVertex( QVector3D( point.x(), point.y(), point.z() ), color, block );
192 extents = extents->
hull( point );
201 static void draw( QGLPainter* painter,
unsigned int size,
unsigned int index )
203 for(
unsigned int i = 0; i < size; i += Size )
205 painter->draw( QGL::LineLoop, Size, index + i );
209 static const Eigen::Vector3d& somePoint(
const Ellipse< Size >& ellipse ) {
return ellipse.center; }
211 static Eigen::Vector3d center(
const Ellipse< Size >& ellipse ) {
return ellipse.center; }
216 namespace comma {
namespace visiting {
218 template <>
struct traits< QColor4ub >
220 template <
typename Key,
class Visitor >
221 static void visit( Key, QColor4ub& p, Visitor& v )
223 unsigned char red = 0;
224 unsigned char green = 0;
225 unsigned char blue = 0;
226 unsigned char alpha = 255;
228 v.apply(
"g", green );
229 v.apply(
"b", blue );
230 v.apply(
"a", alpha );
231 p = QColor4ub( red, green, blue, alpha );
234 template <
typename Key,
class Visitor >
235 static void visit( Key,
const QColor4ub& p, Visitor& v )
237 v.apply(
"r", p.red() );
238 v.apply(
"g", p.green() );
239 v.apply(
"b", p.blue() );
240 v.apply(
"a", p.alpha() );
244 template <
typename S >
struct traits< snark::graphics::View::ShapeWithId< S > >
246 template <
typename Key,
class Visitor >
247 static void visit( Key, snark::graphics::View::ShapeWithId< S >& p, Visitor& v )
249 v.apply(
"shape", p.shape );
250 v.apply(
"id", p.id );
251 v.apply(
"block", p.block );
252 v.apply(
"colour", p.color );
253 v.apply(
"label", p.label );
254 v.apply(
"scalar", p.scalar );
257 template <
typename Key,
class Visitor >
258 static void visit( Key,
const snark::graphics::View::ShapeWithId< S >& p, Visitor& v )
260 v.apply(
"shape", p.shape );
261 v.apply(
"id", p.id );
262 v.apply(
"block", p.block );
263 v.apply(
"colour", p.color );
264 v.apply(
"label", p.label );
265 v.apply(
"scalar", p.scalar );
269 template < std::
size_t Size >
struct traits< snark::graphics::View::Ellipse< Size > >
271 template <
typename Key,
class Visitor >
272 static void visit( Key, snark::graphics::View::Ellipse< Size >& p, Visitor& v )
274 v.apply(
"center", p.center );
275 v.apply(
"orientation", p.orientation );
276 v.apply(
"major", p.major );
277 v.apply(
"minor", p.minor );
280 template <
typename Key,
class Visitor >
281 static void visit( Key,
const snark::graphics::View::Ellipse< Size >& p, Visitor& v )
283 v.apply(
"center", p.center );
284 v.apply(
"orientation", p.orientation );
285 v.apply(
"major", p.major );
286 v.apply(
"minor", p.minor );
290 template <
typename T >
struct traits< snark::math::closed_interval< T, 3 > >
292 template <
typename Key,
class Visitor >
295 Eigen::Matrix< T, 3, 1 > min;
296 Eigen::Matrix< T, 3, 1 > max;
297 v.apply(
"min", min );
298 v.apply(
"max", max );
302 template <
typename Key,
class Visitor >
305 v.apply(
"min", p.
min() );
306 v.apply(
"max", p.
max() );