2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "selection.h"
24 #include "debugging/debugging.h"
30 #include "windowobserver.h"
34 #include "renderable.h"
35 #include "selectable.h"
38 #include "math/frustum.h"
39 #include "signal/signal.h"
40 #include "generic/object.h"
41 #include "selectionlib.h"
45 #include "stream/stringstream.h"
46 #include "eclasslib.h"
47 #include "generic/bitfield.h"
48 #include "generic/static.h"
51 #include "container/container.h"
55 TextOutputStream& ostream_write( TextOutputStream& t, const Vector4& v ){
56 return t << "[ " << v.x() << " " << v.y() << " " << v.z() << " " << v.w() << " ]";
59 TextOutputStream& ostream_write( TextOutputStream& t, const Matrix4& m ){
60 return t << "[ " << m.x() << " " << m.y() << " " << m.z() << " " << m.t() << " ]";
66 Matrix4 m_viewpointSpace;
67 Matrix4 m_viewplaneSpace;
68 Vector3 m_axis_screen;
70 void update( const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport ){
71 Pivot2World_worldSpace( m_worldSpace, pivot2world, modelview, projection, viewport );
72 Pivot2World_viewpointSpace( m_viewpointSpace, m_axis_screen, pivot2world, modelview, projection, viewport );
73 Pivot2World_viewplaneSpace( m_viewplaneSpace, pivot2world, modelview, projection, viewport );
78 void point_for_device_point( Vector3& point, const Matrix4& device2object, const float x, const float y, const float z ){
79 // transform from normalised device coords to object coords
80 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, z, 1 ) ) );
83 void ray_for_device_point( Ray& ray, const Matrix4& device2object, const float x, const float y ){
84 // point at x, y, zNear
85 point_for_device_point( ray.origin, device2object, x, y, -1 );
87 // point at x, y, zFar
88 point_for_device_point( ray.direction, device2object, x, y, 1 );
91 vector3_subtract( ray.direction, ray.origin );
92 vector3_normalise( ray.direction );
95 bool sphere_intersect_ray( const Vector3& origin, float radius, const Ray& ray, Vector3& intersection ){
96 intersection = vector3_subtracted( origin, ray.origin );
97 const double a = vector3_dot( intersection, ray.direction );
98 const double d = radius * radius - ( vector3_dot( intersection, intersection ) - a * a );
101 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a - sqrt( d ) ) );
106 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a ) );
111 void ray_intersect_ray( const Ray& ray, const Ray& other, Vector3& intersection ){
112 intersection = vector3_subtracted( ray.origin, other.origin );
113 //float a = 1;//vector3_dot(ray.direction, ray.direction); // always >= 0
114 double dot = vector3_dot( ray.direction, other.direction );
115 //float c = 1;//vector3_dot(other.direction, other.direction); // always >= 0
116 double d = vector3_dot( ray.direction, intersection );
117 double e = vector3_dot( other.direction, intersection );
118 double D = 1 - dot * dot; //a*c - dot*dot; // always >= 0
120 if ( D < 0.000001 ) {
121 // the lines are almost parallel
122 intersection = vector3_added( other.origin, vector3_scaled( other.direction, e ) );
126 intersection = vector3_added( other.origin, vector3_scaled( other.direction, ( e - dot * d ) / D ) );
130 const Vector3 g_origin( 0, 0, 0 );
131 const float g_radius = 64;
133 void point_on_sphere( Vector3& point, const Matrix4& device2object, const float x, const float y ){
135 ray_for_device_point( ray, device2object, x, y );
136 sphere_intersect_ray( g_origin, g_radius, ray, point );
139 void point_on_axis( Vector3& point, const Vector3& axis, const Matrix4& device2object, const float x, const float y ){
141 ray_for_device_point( ray, device2object, x, y );
142 ray_intersect_ray( ray, Ray( Vector3( 0, 0, 0 ), axis ), point );
145 void point_on_plane( Vector3& point, const Matrix4& device2object, const float x, const float y ){
146 Matrix4 object2device( matrix4_full_inverse( device2object ) );
147 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, object2device[14] / object2device[15], 1 ) ) );
150 //! a and b are unit vectors .. returns angle in radians
151 inline float angle_between( const Vector3& a, const Vector3& b ){
152 return static_cast<float>( 2.0 * atan2(
153 vector3_length( vector3_subtracted( a, b ) ),
154 vector3_length( vector3_added( a, b ) )
159 #if defined( _DEBUG )
163 test_quat( const Vector3& from, const Vector3& to ){
164 Vector4 quaternion( quaternion_for_unit_vectors( from, to ) );
165 Matrix4 matrix( matrix4_rotation_for_quaternion( quaternion_multiplied_by_quaternion( quaternion, c_quaternion_identity ) ) );
170 static test_quat bleh( g_vector3_axis_x, g_vector3_axis_y );
173 //! axis is a unit vector
174 inline void constrain_to_axis( Vector3& vec, const Vector3& axis ){
175 vec = vector3_normalised( vector3_added( vec, vector3_scaled( axis, -vector3_dot( vec, axis ) ) ) );
178 //! a and b are unit vectors .. a and b must be orthogonal to axis .. returns angle in radians
179 float angle_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
180 if ( vector3_dot( axis, vector3_cross( a, b ) ) > 0.0 ) {
181 return angle_between( a, b );
184 return -angle_between( a, b );
188 float distance_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
189 return static_cast<float>( vector3_dot( b, axis ) - vector3_dot( a, axis ) );
195 virtual void Construct( const Matrix4& device2manip, const float x, const float y ) = 0;
196 virtual void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ) = 0;
199 void transform_local2object( Matrix4& object, const Matrix4& local, const Matrix4& local2object ){
200 object = matrix4_multiplied_by_matrix4(
201 matrix4_multiplied_by_matrix4( local2object, local ),
202 matrix4_full_inverse( local2object )
209 virtual ~Rotatable() = default;
210 virtual void rotate( const Quaternion& rotation ) = 0;
213 class RotateFree : public Manipulatable
216 Rotatable& m_rotatable;
218 RotateFree( Rotatable& rotatable )
219 : m_rotatable( rotatable ){
221 void Construct( const Matrix4& device2manip, const float x, const float y ){
222 point_on_sphere( m_start, device2manip, x, y );
223 vector3_normalise( m_start );
225 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
228 point_on_sphere( current, device2manip, x, y );
229 vector3_normalise( current );
231 m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
235 class RotateAxis : public Manipulatable
239 Rotatable& m_rotatable;
241 RotateAxis( Rotatable& rotatable )
242 : m_rotatable( rotatable ){
244 void Construct( const Matrix4& device2manip, const float x, const float y ){
245 point_on_sphere( m_start, device2manip, x, y );
246 constrain_to_axis( m_start, m_axis );
248 /// \brief Converts current position to a normalised vector orthogonal to axis.
249 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
251 point_on_sphere( current, device2manip, x, y );
252 constrain_to_axis( current, m_axis );
254 m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
257 void SetAxis( const Vector3& axis ){
262 void translation_local2object( Vector3& object, const Vector3& local, const Matrix4& local2object ){
263 object = matrix4_get_translation_vec3(
264 matrix4_multiplied_by_matrix4(
265 matrix4_translated_by_vec3( local2object, local ),
266 matrix4_full_inverse( local2object )
274 virtual ~Translatable() = default;
275 virtual void translate( const Vector3& translation ) = 0;
278 class TranslateAxis : public Manipulatable
282 Translatable& m_translatable;
284 TranslateAxis( Translatable& translatable )
285 : m_translatable( translatable ){
287 void Construct( const Matrix4& device2manip, const float x, const float y ){
288 point_on_axis( m_start, m_axis, device2manip, x, y );
290 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
292 point_on_axis( current, m_axis, device2manip, x, y );
293 current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
295 translation_local2object( current, current, manip2object );
296 vector3_snap( current, GetSnapGridSize() );
298 m_translatable.translate( current );
301 void SetAxis( const Vector3& axis ){
306 class TranslateFree : public Manipulatable
310 Translatable& m_translatable;
312 TranslateFree( Translatable& translatable )
313 : m_translatable( translatable ){
315 void Construct( const Matrix4& device2manip, const float x, const float y ){
316 point_on_plane( m_start, device2manip, x, y );
318 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
320 point_on_plane( current, device2manip, x, y );
321 current = vector3_subtracted( current, m_start );
323 translation_local2object( current, current, manip2object );
324 vector3_snap( current, GetSnapGridSize() );
326 m_translatable.translate( current );
334 virtual ~Scalable() = default;
335 virtual void scale( const Vector3& scaling ) = 0;
339 class ScaleAxis : public Manipulatable
344 Scalable& m_scalable;
346 ScaleAxis( Scalable& scalable )
347 : m_scalable( scalable ){
349 void Construct( const Matrix4& device2manip, const float x, const float y ){
350 point_on_axis( m_start, m_axis, device2manip, x, y );
352 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
354 point_on_axis( current, m_axis, device2manip, x, y );
355 Vector3 delta = vector3_subtracted( current, m_start );
357 translation_local2object( delta, delta, manip2object );
358 vector3_snap( delta, GetSnapGridSize() );
360 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
362 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
363 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
364 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
366 m_scalable.scale( scale );
369 void SetAxis( const Vector3& axis ){
374 class ScaleFree : public Manipulatable
378 Scalable& m_scalable;
380 ScaleFree( Scalable& scalable )
381 : m_scalable( scalable ){
383 void Construct( const Matrix4& device2manip, const float x, const float y ){
384 point_on_plane( m_start, device2manip, x, y );
386 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
388 point_on_plane( current, device2manip, x, y );
389 Vector3 delta = vector3_subtracted( current, m_start );
391 translation_local2object( delta, delta, manip2object );
392 vector3_snap( delta, GetSnapGridSize() );
394 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
396 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
397 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
398 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
400 m_scalable.scale( scale );
413 class RenderableClippedPrimitive : public OpenGLRenderable
417 PointVertex m_points[9];
421 std::vector<primitive_t> m_primitives;
425 void render( RenderStateFlags state ) const {
426 for ( std::size_t i = 0; i < m_primitives.size(); ++i )
428 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
429 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
430 switch ( m_primitives[i].m_count )
433 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
434 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
439 void construct( const Matrix4& world2device ){
440 m_inverse = matrix4_full_inverse( world2device );
441 m_world = g_matrix4_identity;
444 void insert( const Vector4 clipped[9], std::size_t count ){
447 m_primitives.back().m_count = count;
448 for ( std::size_t i = 0; i < count; ++i )
450 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
451 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
456 m_primitives.clear();
460 m_primitives.push_back( primitive_t() );
462 const Colour4b colour_clipped( 255, 127, 0, 255 );
464 for ( std::size_t i = 0; i < 9; ++i )
465 m_primitives.back().m_points[i].colour = colour_clipped;
469 #if defined( _DEBUG )
470 #define DEBUG_SELECTION
473 #if defined( DEBUG_SELECTION )
474 Shader* g_state_clipped;
475 RenderableClippedPrimitive g_render_clipped;
480 // dist_Point_to_Line(): get the distance of a point to a line.
481 // Input: a Point P and a Line L (in any dimension)
482 // Return: the shortest distance from P to L
484 dist_Point_to_Line( Point P, Line L ){
485 Vector v = L.P1 - L.P0;
488 double c1 = dot( w,v );
489 double c2 = dot( v,v );
492 Point Pb = L.P0 + b * v;
499 typedef Vector3 point_type;
501 Segment3D( const point_type& _p0, const point_type& _p1 )
502 : p0( _p0 ), p1( _p1 ){
508 typedef Vector3 Point3D;
510 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
511 return vector3_length_squared( b - a );
514 // get the distance of a point to a segment.
515 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
516 Vector3 v = segment.p1 - segment.p0;
517 Vector3 w = point - segment.p0;
519 double c1 = vector3_dot( w,v );
524 double c2 = vector3_dot( v,v );
529 return Point3D( segment.p0 + v * ( c1 / c2 ) );
532 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
533 return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
536 typedef Vector3 point_t;
537 typedef const Vector3* point_iterator_t;
539 // crossing number test for a point in a polygon
540 // This code is patterned after [Franklin, 2000]
541 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
542 std::size_t crossings = 0;
544 // loop through all edges of the polygon
545 for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
546 { // edge from (*prev) to (*cur)
547 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
548 || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
549 // compute the actual edge-ray intersect x-coordinate
550 float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
551 if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
552 ++crossings; // a valid crossing of y=P[1] right of P[0]
556 return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
559 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
560 return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
571 inline SelectionIntersection select_point_from_clipped( Vector4& clipped ){
572 return SelectionIntersection( clipped[2] / clipped[3], static_cast<float>( vector3_length_squared( Vector3( clipped[0] / clipped[3], clipped[1] / clipped[3], 0 ) ) ) );
575 void BestPoint( std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull ){
576 Vector3 normalised[9];
579 for ( std::size_t i = 0; i < count; ++i )
581 normalised[i][0] = clipped[i][0] / clipped[i][3];
582 normalised[i][1] = clipped[i][1] / clipped[i][3];
583 normalised[i][2] = clipped[i][2] / clipped[i][3];
587 if ( cull != eClipCullNone && count > 2 ) {
588 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
590 if ( ( cull == eClipCullCW && signed_area > 0 )
591 || ( cull == eClipCullCCW && signed_area < 0 ) ) {
597 Segment3D segment( normalised[0], normalised[1] );
598 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
599 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
601 else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
602 point_iterator_t end = normalised + count;
603 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
605 Segment3D segment( *previous, *current );
606 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
607 float depth = point.z();
609 float distance = static_cast<float>( vector3_length_squared( point ) );
611 assign_if_closer( best, SelectionIntersection( depth, distance ) );
614 else if ( count > 2 ) {
617 SelectionIntersection(
618 static_cast<float>( ray_distance_to_plane(
619 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
620 plane3_for_points( normalised[0], normalised[1], normalised[2] )
627 #if defined( DEBUG_SELECTION )
629 g_render_clipped.insert( clipped, count );
634 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
636 for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
638 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
639 BestPoint( count, clipped, best, eClipCullNone );
643 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
645 for ( std::size_t i = 0; i < size; ++i )
647 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
648 BestPoint( count, clipped, best, eClipCullNone );
652 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
654 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
655 BestPoint( count, clipped, best, eClipCullNone );
658 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
660 for ( std::size_t i = 0; i < size; ++i )
662 const std::size_t count = matrix4_clip_triangle( local2view, g_vector3_identity, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
663 BestPoint( count, clipped, best, cull );
667 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
670 const std::size_t count = matrix4_clip_triangle( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), vertex3f_to_vector3( vertices[3].vertex ), clipped );
671 BestPoint( count, clipped, best, cull );
674 const std::size_t count = matrix4_clip_triangle( local2view, vertex3f_to_vector3( vertices[1].vertex ), vertex3f_to_vector3( vertices[2].vertex ), vertex3f_to_vector3( vertices[3].vertex ), clipped );
675 BestPoint( count, clipped, best, cull );
679 struct FlatShadedVertex
690 typedef FlatShadedVertex* FlatShadedVertexIterator;
691 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
692 for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
696 matrix4_clip_triangle(
698 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
699 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
700 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
711 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
713 class SelectionPool : public Selector
715 SelectableSortedSet m_pool;
716 SelectionIntersection m_intersection;
717 Selectable* m_selectable;
720 void pushSelectable( Selectable& selectable ){
721 m_intersection = SelectionIntersection();
722 m_selectable = &selectable;
724 void popSelectable(){
725 addSelectable( m_intersection, m_selectable );
726 m_intersection = SelectionIntersection();
728 void addIntersection( const SelectionIntersection& intersection ){
729 assign_if_closer( m_intersection, intersection );
731 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
732 if ( intersection.valid() ) {
733 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
737 typedef SelectableSortedSet::iterator iterator;
740 return m_pool.begin();
747 return m_pool.empty();
752 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
753 const Colour4b g_colour_screen( 0, 255, 255, 255 );
754 const Colour4b g_colour_selected( 255, 255, 0, 255 );
756 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
757 return ( selected ) ? g_colour_selected : colour;
760 template<typename remap_policy>
761 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
762 const double increment = c_pi / double(segments << 2);
764 std::size_t count = 0;
767 remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
768 while ( count < segments )
770 PointVertex* i = vertices + count;
771 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
773 PointVertex* k = i + ( segments << 1 );
774 PointVertex* l = j + ( segments << 1 );
777 PointVertex* m = i + ( segments << 2 );
778 PointVertex* n = j + ( segments << 2 );
779 PointVertex* o = k + ( segments << 2 );
780 PointVertex* p = l + ( segments << 2 );
783 remap_policy::set( i->vertex, x,-y, 0 );
784 remap_policy::set( k->vertex,-y,-x, 0 );
786 remap_policy::set( m->vertex,-x, y, 0 );
787 remap_policy::set( o->vertex, y, x, 0 );
793 const double theta = increment * count;
794 x = static_cast<float>( radius * cos( theta ) );
795 y = static_cast<float>( radius * sin( theta ) );
798 remap_policy::set( j->vertex, y,-x, 0 );
799 remap_policy::set( l->vertex,-x,-y, 0 );
801 remap_policy::set( n->vertex,-y, x, 0 );
802 remap_policy::set( p->vertex, x, y, 0 );
810 virtual Manipulatable* GetManipulatable() = 0;
811 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
813 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
815 virtual void setSelected( bool select ) = 0;
816 virtual bool isSelected() const = 0;
820 inline Vector3 normalised_safe( const Vector3& self ){
821 if ( vector3_equal( self, g_vector3_identity ) ) {
822 return g_vector3_identity;
824 return vector3_normalised( self );
828 class RotateManipulator : public Manipulator
830 struct RenderableCircle : public OpenGLRenderable
832 Array<PointVertex> m_vertices;
834 RenderableCircle( std::size_t size ) : m_vertices( size ){
836 void render( RenderStateFlags state ) const {
837 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
838 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
839 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
841 void setColour( const Colour4b& colour ){
842 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
844 ( *i ).colour = colour;
849 struct RenderableSemiCircle : public OpenGLRenderable
851 Array<PointVertex> m_vertices;
853 RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
855 void render( RenderStateFlags state ) const {
856 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
857 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
858 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
860 void setColour( const Colour4b& colour ){
861 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
863 ( *i ).colour = colour;
870 Vector3 m_axis_screen;
871 RenderableSemiCircle m_circle_x;
872 RenderableSemiCircle m_circle_y;
873 RenderableSemiCircle m_circle_z;
874 RenderableCircle m_circle_screen;
875 RenderableCircle m_circle_sphere;
876 SelectableBool m_selectable_x;
877 SelectableBool m_selectable_y;
878 SelectableBool m_selectable_z;
879 SelectableBool m_selectable_screen;
880 SelectableBool m_selectable_sphere;
882 Matrix4 m_local2world_x;
883 Matrix4 m_local2world_y;
884 Matrix4 m_local2world_z;
885 bool m_circle_x_visible;
886 bool m_circle_y_visible;
887 bool m_circle_z_visible;
889 static Shader* m_state_outer;
891 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
894 m_circle_x( ( segments << 2 ) + 1 ),
895 m_circle_y( ( segments << 2 ) + 1 ),
896 m_circle_z( ( segments << 2 ) + 1 ),
897 m_circle_screen( segments << 3 ),
898 m_circle_sphere( segments << 3 ){
899 draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
900 draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
901 draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
903 draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
904 draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
908 void UpdateColours(){
909 m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
910 m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
911 m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
912 m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
913 m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
916 void updateCircleTransforms(){
917 Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
919 m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
920 if ( m_circle_x_visible ) {
921 m_local2world_x = g_matrix4_identity;
922 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
923 vector3_cross( g_vector3_axis_x, localViewpoint )
925 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
926 vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
928 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
931 m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
932 if ( m_circle_y_visible ) {
933 m_local2world_y = g_matrix4_identity;
934 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
935 vector3_cross( g_vector3_axis_y, localViewpoint )
937 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
938 vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
940 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
943 m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
944 if ( m_circle_z_visible ) {
945 m_local2world_z = g_matrix4_identity;
946 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
947 vector3_cross( g_vector3_axis_z, localViewpoint )
949 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
950 vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
952 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
956 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
957 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
958 updateCircleTransforms();
963 renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
964 renderer.SetState( m_state_outer, Renderer::eFullMaterials );
966 renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
967 renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
969 if ( m_circle_x_visible ) {
970 renderer.addRenderable( m_circle_x, m_local2world_x );
972 if ( m_circle_y_visible ) {
973 renderer.addRenderable( m_circle_y, m_local2world_y );
975 if ( m_circle_z_visible ) {
976 renderer.addRenderable( m_circle_z, m_local2world_z );
979 void testSelect( const View& view, const Matrix4& pivot2world ){
980 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
981 updateCircleTransforms();
983 SelectionPool selector;
987 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
989 #if defined( DEBUG_SELECTION )
990 g_render_clipped.construct( view.GetViewMatrix() );
993 SelectionIntersection best;
994 LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
995 selector.addSelectable( best, &m_selectable_x );
999 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
1001 #if defined( DEBUG_SELECTION )
1002 g_render_clipped.construct( view.GetViewMatrix() );
1005 SelectionIntersection best;
1006 LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1007 selector.addSelectable( best, &m_selectable_y );
1011 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1013 #if defined( DEBUG_SELECTION )
1014 g_render_clipped.construct( view.GetViewMatrix() );
1017 SelectionIntersection best;
1018 LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1019 selector.addSelectable( best, &m_selectable_z );
1024 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1027 SelectionIntersection best;
1028 LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1029 selector.addSelectable( best, &m_selectable_screen );
1033 SelectionIntersection best;
1034 Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1035 selector.addSelectable( best, &m_selectable_sphere );
1039 m_axis_screen = m_pivot.m_axis_screen;
1041 if ( !selector.failed() ) {
1042 ( *selector.begin() ).second->setSelected( true );
1046 Manipulatable* GetManipulatable(){
1047 if ( m_selectable_x.isSelected() ) {
1048 m_axis.SetAxis( g_vector3_axis_x );
1051 else if ( m_selectable_y.isSelected() ) {
1052 m_axis.SetAxis( g_vector3_axis_y );
1055 else if ( m_selectable_z.isSelected() ) {
1056 m_axis.SetAxis( g_vector3_axis_z );
1059 else if ( m_selectable_screen.isSelected() ) {
1060 m_axis.SetAxis( m_axis_screen );
1068 void setSelected( bool select ){
1069 m_selectable_x.setSelected( select );
1070 m_selectable_y.setSelected( select );
1071 m_selectable_z.setSelected( select );
1072 m_selectable_screen.setSelected( select );
1074 bool isSelected() const {
1075 return m_selectable_x.isSelected()
1076 | m_selectable_y.isSelected()
1077 | m_selectable_z.isSelected()
1078 | m_selectable_screen.isSelected()
1079 | m_selectable_sphere.isSelected();
1083 Shader* RotateManipulator::m_state_outer;
1086 const float arrowhead_length = 16;
1087 const float arrowhead_radius = 4;
1089 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1090 ( *line++ ).vertex = vertex3f_identity;
1091 ( *line ).vertex = vertex3f_identity;
1092 vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1095 template<typename VertexRemap, typename NormalRemap>
1096 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1097 std::size_t head_tris = ( segments << 3 );
1098 const double head_segment = c_2pi / head_tris;
1099 for ( std::size_t i = 0; i < head_tris; ++i )
1102 FlatShadedVertex& point = vertices[i * 6 + 0];
1103 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1104 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1105 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1106 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1107 NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1108 NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1111 FlatShadedVertex& point = vertices[i * 6 + 1];
1112 VertexRemap::x( point.vertex ) = length;
1113 VertexRemap::y( point.vertex ) = 0;
1114 VertexRemap::z( point.vertex ) = 0;
1115 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1116 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1117 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1120 FlatShadedVertex& point = vertices[i * 6 + 2];
1121 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1122 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1123 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1124 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1125 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1126 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1130 FlatShadedVertex& point = vertices[i * 6 + 3];
1131 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1132 VertexRemap::y( point.vertex ) = 0;
1133 VertexRemap::z( point.vertex ) = 0;
1134 NormalRemap::x( point.normal ) = -1;
1135 NormalRemap::y( point.normal ) = 0;
1136 NormalRemap::z( point.normal ) = 0;
1139 FlatShadedVertex& point = vertices[i * 6 + 4];
1140 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1141 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1142 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1143 NormalRemap::x( point.normal ) = -1;
1144 NormalRemap::y( point.normal ) = 0;
1145 NormalRemap::z( point.normal ) = 0;
1148 FlatShadedVertex& point = vertices[i * 6 + 5];
1149 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1150 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1151 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1152 NormalRemap::x( point.normal ) = -1;
1153 NormalRemap::y( point.normal ) = 0;
1154 NormalRemap::z( point.normal ) = 0;
1159 template<typename Triple>
1160 class TripleRemapXYZ
1163 static float& x( Triple& triple ){
1166 static float& y( Triple& triple ){
1169 static float& z( Triple& triple ){
1174 template<typename Triple>
1175 class TripleRemapYZX
1178 static float& x( Triple& triple ){
1181 static float& y( Triple& triple ){
1184 static float& z( Triple& triple ){
1189 template<typename Triple>
1190 class TripleRemapZXY
1193 static float& x( Triple& triple ){
1196 static float& y( Triple& triple ){
1199 static float& z( Triple& triple ){
1204 void vector3_print( const Vector3& v ){
1205 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1208 class TranslateManipulator : public Manipulator
1210 struct RenderableArrowLine : public OpenGLRenderable
1212 PointVertex m_line[2];
1214 RenderableArrowLine(){
1216 void render( RenderStateFlags state ) const {
1217 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1218 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1219 glDrawArrays( GL_LINES, 0, 2 );
1221 void setColour( const Colour4b& colour ){
1222 m_line[0].colour = colour;
1223 m_line[1].colour = colour;
1226 struct RenderableArrowHead : public OpenGLRenderable
1228 Array<FlatShadedVertex> m_vertices;
1230 RenderableArrowHead( std::size_t size )
1231 : m_vertices( size ){
1233 void render( RenderStateFlags state ) const {
1234 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1235 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1236 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1237 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1239 void setColour( const Colour4b& colour ){
1240 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1242 ( *i ).colour = colour;
1246 struct RenderableQuad : public OpenGLRenderable
1248 PointVertex m_quad[4];
1249 void render( RenderStateFlags state ) const {
1250 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1251 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1252 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1254 void setColour( const Colour4b& colour ){
1255 m_quad[0].colour = colour;
1256 m_quad[1].colour = colour;
1257 m_quad[2].colour = colour;
1258 m_quad[3].colour = colour;
1262 TranslateFree m_free;
1263 TranslateAxis m_axis;
1264 RenderableArrowLine m_arrow_x;
1265 RenderableArrowLine m_arrow_y;
1266 RenderableArrowLine m_arrow_z;
1267 RenderableArrowHead m_arrow_head_x;
1268 RenderableArrowHead m_arrow_head_y;
1269 RenderableArrowHead m_arrow_head_z;
1270 RenderableQuad m_quad_screen;
1271 SelectableBool m_selectable_x;
1272 SelectableBool m_selectable_y;
1273 SelectableBool m_selectable_z;
1274 SelectableBool m_selectable_screen;
1275 Pivot2World m_pivot;
1277 static Shader* m_state_wire;
1278 static Shader* m_state_fill;
1280 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1281 m_free( translatable ),
1282 m_axis( translatable ),
1283 m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1284 m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1285 m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1286 draw_arrowline( length, m_arrow_x.m_line, 0 );
1287 draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1288 draw_arrowline( length, m_arrow_y.m_line, 1 );
1289 draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1290 draw_arrowline( length, m_arrow_z.m_line, 2 );
1291 draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1293 draw_quad( 16, m_quad_screen.m_quad );
1296 void UpdateColours(){
1297 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1298 m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1299 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1300 m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1301 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1302 m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1303 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1306 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1307 return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1310 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1311 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1316 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1317 bool show_x = manipulator_show_axis( m_pivot, x );
1319 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1320 bool show_y = manipulator_show_axis( m_pivot, y );
1322 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1323 bool show_z = manipulator_show_axis( m_pivot, z );
1325 renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1326 renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1329 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1332 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1335 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1338 renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1340 renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1341 renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1344 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1347 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1350 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1353 void testSelect( const View& view, const Matrix4& pivot2world ){
1354 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1356 SelectionPool selector;
1358 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1359 bool show_x = manipulator_show_axis( m_pivot, x );
1361 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1362 bool show_y = manipulator_show_axis( m_pivot, y );
1364 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1365 bool show_z = manipulator_show_axis( m_pivot, z );
1368 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1371 SelectionIntersection best;
1372 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1373 if ( best.valid() ) {
1374 best = SelectionIntersection( 0, 0 );
1375 selector.addSelectable( best, &m_selectable_screen );
1381 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1383 #if defined( DEBUG_SELECTION )
1384 g_render_clipped.construct( view.GetViewMatrix() );
1388 SelectionIntersection best;
1389 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1390 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1391 selector.addSelectable( best, &m_selectable_x );
1395 SelectionIntersection best;
1396 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1397 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1398 selector.addSelectable( best, &m_selectable_y );
1402 SelectionIntersection best;
1403 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1404 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1405 selector.addSelectable( best, &m_selectable_z );
1409 if ( !selector.failed() ) {
1410 ( *selector.begin() ).second->setSelected( true );
1414 Manipulatable* GetManipulatable(){
1415 if ( m_selectable_x.isSelected() ) {
1416 m_axis.SetAxis( g_vector3_axis_x );
1419 else if ( m_selectable_y.isSelected() ) {
1420 m_axis.SetAxis( g_vector3_axis_y );
1423 else if ( m_selectable_z.isSelected() ) {
1424 m_axis.SetAxis( g_vector3_axis_z );
1433 void setSelected( bool select ){
1434 m_selectable_x.setSelected( select );
1435 m_selectable_y.setSelected( select );
1436 m_selectable_z.setSelected( select );
1437 m_selectable_screen.setSelected( select );
1439 bool isSelected() const {
1440 return m_selectable_x.isSelected()
1441 | m_selectable_y.isSelected()
1442 | m_selectable_z.isSelected()
1443 | m_selectable_screen.isSelected();
1447 Shader* TranslateManipulator::m_state_wire;
1448 Shader* TranslateManipulator::m_state_fill;
1450 class ScaleManipulator : public Manipulator
1452 struct RenderableArrow : public OpenGLRenderable
1454 PointVertex m_line[2];
1456 void render( RenderStateFlags state ) const {
1457 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1458 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1459 glDrawArrays( GL_LINES, 0, 2 );
1461 void setColour( const Colour4b& colour ){
1462 m_line[0].colour = colour;
1463 m_line[1].colour = colour;
1466 struct RenderableQuad : public OpenGLRenderable
1468 PointVertex m_quad[4];
1469 void render( RenderStateFlags state ) const {
1470 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1471 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1472 glDrawArrays( GL_QUADS, 0, 4 );
1474 void setColour( const Colour4b& colour ){
1475 m_quad[0].colour = colour;
1476 m_quad[1].colour = colour;
1477 m_quad[2].colour = colour;
1478 m_quad[3].colour = colour;
1484 RenderableArrow m_arrow_x;
1485 RenderableArrow m_arrow_y;
1486 RenderableArrow m_arrow_z;
1487 RenderableQuad m_quad_screen;
1488 SelectableBool m_selectable_x;
1489 SelectableBool m_selectable_y;
1490 SelectableBool m_selectable_z;
1491 SelectableBool m_selectable_screen;
1492 Pivot2World m_pivot;
1494 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1497 draw_arrowline( length, m_arrow_x.m_line, 0 );
1498 draw_arrowline( length, m_arrow_y.m_line, 1 );
1499 draw_arrowline( length, m_arrow_z.m_line, 2 );
1501 draw_quad( 16, m_quad_screen.m_quad );
1504 Pivot2World& getPivot(){
1508 void UpdateColours(){
1509 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1510 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1511 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1512 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1515 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1516 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1521 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1522 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1523 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1525 renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1527 void testSelect( const View& view, const Matrix4& pivot2world ){
1528 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1530 SelectionPool selector;
1533 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1535 #if defined( DEBUG_SELECTION )
1536 g_render_clipped.construct( view.GetViewMatrix() );
1540 SelectionIntersection best;
1541 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1542 selector.addSelectable( best, &m_selectable_x );
1546 SelectionIntersection best;
1547 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1548 selector.addSelectable( best, &m_selectable_y );
1552 SelectionIntersection best;
1553 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1554 selector.addSelectable( best, &m_selectable_z );
1559 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1562 SelectionIntersection best;
1563 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1564 selector.addSelectable( best, &m_selectable_screen );
1568 if ( !selector.failed() ) {
1569 ( *selector.begin() ).second->setSelected( true );
1573 Manipulatable* GetManipulatable(){
1574 if ( m_selectable_x.isSelected() ) {
1575 m_axis.SetAxis( g_vector3_axis_x );
1578 else if ( m_selectable_y.isSelected() ) {
1579 m_axis.SetAxis( g_vector3_axis_y );
1582 else if ( m_selectable_z.isSelected() ) {
1583 m_axis.SetAxis( g_vector3_axis_z );
1591 void setSelected( bool select ){
1592 m_selectable_x.setSelected( select );
1593 m_selectable_y.setSelected( select );
1594 m_selectable_z.setSelected( select );
1595 m_selectable_screen.setSelected( select );
1597 bool isSelected() const {
1598 return m_selectable_x.isSelected()
1599 | m_selectable_y.isSelected()
1600 | m_selectable_z.isSelected()
1601 | m_selectable_screen.isSelected();
1606 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1607 return InstanceTypeCast<PlaneSelectable>::cast( instance );
1610 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1612 Selector& m_selector;
1613 SelectionTest& m_test;
1614 PlaneCallback m_selectedPlaneCallback;
1616 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1617 : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1619 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1620 if ( path.top().get().visible() ) {
1621 Selectable* selectable = Instance_getSelectable( instance );
1622 if ( selectable != 0 && selectable->isSelected() ) {
1623 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1624 if ( planeSelectable != 0 ) {
1625 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1633 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1635 Selector& m_selector;
1636 const SelectedPlanes& m_selectedPlanes;
1638 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1639 : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1641 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1642 if ( path.top().get().visible() ) {
1643 Selectable* selectable = Instance_getSelectable( instance );
1644 if ( selectable != 0 && selectable->isSelected() ) {
1645 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1646 if ( planeSelectable != 0 ) {
1647 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1655 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1656 graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1659 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1660 graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1667 bool operator()( const Plane3& plane, const Plane3& other ) const {
1668 if ( plane.a < other.a ) {
1671 if ( other.a < plane.a ) {
1675 if ( plane.b < other.b ) {
1678 if ( other.b < plane.b ) {
1682 if ( plane.c < other.c ) {
1685 if ( other.c < plane.c ) {
1689 if ( plane.d < other.d ) {
1692 if ( other.d < plane.d ) {
1700 typedef std::set<Plane3, PlaneLess> PlaneSet;
1702 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1703 self.insert( plane );
1706 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1707 return self.find( plane ) != self.end();
1711 class SelectedPlaneSet : public SelectedPlanes
1713 PlaneSet m_selectedPlanes;
1715 bool empty() const {
1716 return m_selectedPlanes.empty();
1719 void insert( const Plane3& plane ){
1720 PlaneSet_insert( m_selectedPlanes, plane );
1722 bool contains( const Plane3& plane ) const {
1723 return PlaneSet_contains( m_selectedPlanes, plane );
1725 typedef MemberCaller1<SelectedPlaneSet, const Plane3&, &SelectedPlaneSet::insert> InsertCaller;
1729 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1730 SelectedPlaneSet selectedPlanes;
1732 Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1733 Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1735 return !selectedPlanes.empty();
1738 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1739 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1740 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1741 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1742 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1743 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1745 class ResizeTranslatable : public Translatable
1747 void translate( const Vector3& translation ){
1748 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1752 class DragTranslatable : public Translatable
1754 void translate( const Vector3& translation ){
1755 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1756 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1760 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1765 class SelectionVolume : public SelectionTest
1767 Matrix4 m_local2view;
1773 SelectionVolume( const View& view )
1777 const VolumeTest& getVolume() const {
1781 const Vector3& getNear() const {
1784 const Vector3& getFar() const {
1788 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1789 m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
1791 // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1792 // Don't cull if the view is wireframe and the polygons are two-sided.
1793 m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
1796 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
1798 m_near = vector4_projected(
1799 matrix4_transformed_vector4(
1801 Vector4( 0, 0, -1, 1 )
1805 m_far = vector4_projected(
1806 matrix4_transformed_vector4(
1808 Vector4( 0, 0, 1, 1 )
1813 #if defined( DEBUG_SELECTION )
1814 g_render_clipped.construct( m_view.GetViewMatrix() );
1817 void TestPoint( const Vector3& point, SelectionIntersection& best ){
1819 if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
1820 best = select_point_from_clipped( clipped );
1823 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1825 for ( std::size_t i = 0; i + 2 < count; ++i )
1828 matrix4_clip_triangle(
1830 reinterpret_cast<const Vector3&>( vertices[0] ),
1831 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
1832 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
1841 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1846 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
1851 reinterpret_cast<const Vector3&>( ( *prev ) ),
1852 reinterpret_cast<const Vector3&>( ( *i ) ),
1861 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1866 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
1871 reinterpret_cast<const Vector3&>( ( *i ) ),
1872 reinterpret_cast<const Vector3&>( ( *next ) ),
1881 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1886 for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
1891 reinterpret_cast<const Vector3&>( ( *i ) ),
1892 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
1901 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1903 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
1906 matrix4_clip_triangle(
1908 reinterpret_cast<const Vector3&>( vertices[*i] ),
1909 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1910 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1919 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1921 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
1924 matrix4_clip_triangle(
1926 reinterpret_cast<const Vector3&>( vertices[*i] ),
1927 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1928 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1936 matrix4_clip_triangle(
1938 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1939 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1940 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1949 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1951 for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
1954 matrix4_clip_triangle(
1956 reinterpret_cast<const Vector3&>( vertices[*i] ),
1957 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1958 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1966 matrix4_clip_triangle(
1968 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1969 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1970 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1981 class SelectionCounter
1984 typedef const Selectable& first_argument_type;
1986 SelectionCounter( const SelectionChangeCallback& onchanged )
1987 : m_count( 0 ), m_onchanged( onchanged ){
1989 void operator()( const Selectable& selectable ){
1990 if ( selectable.isSelected() ) {
1995 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
1999 m_onchanged( selectable );
2001 bool empty() const {
2002 return m_count == 0;
2004 std::size_t size() const {
2008 std::size_t m_count;
2009 SelectionChangeCallback m_onchanged;
2012 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2013 view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2016 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2017 rect_t selection_box;
2018 selection_box.min[0] = device_point[0] - device_epsilon[0];
2019 selection_box.min[1] = device_point[1] - device_epsilon[1];
2020 selection_box.max[0] = device_point[0] + device_epsilon[0];
2021 selection_box.max[1] = device_point[1] + device_epsilon[1];
2022 return selection_box;
2025 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2026 rect_t selection_box;
2027 selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2028 selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2029 selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2030 selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2031 return selection_box;
2034 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2035 return quaternion_normalised( quaternion_multiplied_by_quaternion(
2036 quaternion_normalised( quaternion_multiplied_by_quaternion(
2037 quaternion_inverse( localToWorld ),
2044 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2045 matrix[0] = other[0];
2046 matrix[1] = other[1];
2047 matrix[2] = other[2];
2048 matrix[4] = other[4];
2049 matrix[5] = other[5];
2050 matrix[6] = other[6];
2051 matrix[8] = other[8];
2052 matrix[9] = other[9];
2053 matrix[10] = other[10];
2056 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2057 Editable* editable = Node_getEditable( instance.path().top() );
2058 if ( editable != 0 ) {
2059 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2063 matrix4_assign_rotation( matrix, instance.localToWorld() );
2067 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2068 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2069 return componentSelectionTestable != 0
2070 && componentSelectionTestable->isSelectedComponents();
2073 class TranslateSelected : public SelectionSystem::Visitor
2075 const Vector3& m_translate;
2077 TranslateSelected( const Vector3& translate )
2078 : m_translate( translate ){
2080 void visit( scene::Instance& instance ) const {
2081 Transformable* transform = Instance_getTransformable( instance );
2082 if ( transform != 0 ) {
2083 transform->setType( TRANSFORM_PRIMITIVE );
2084 transform->setTranslation( m_translate );
2089 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2090 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2091 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2095 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2097 matrix4_transformed_point(
2098 matrix4_full_inverse( localToWorld ),
2104 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2105 // we need a translation inside the parent system to move the origin of this object to the right place
2107 // mathematically, it must fulfill:
2109 // local_translation local_transform local_pivot = local_pivot
2110 // local_translation = local_pivot - local_transform local_pivot
2113 // local_transform local_translation local_pivot = local_pivot
2114 // local_translation local_pivot = local_transform^-1 local_pivot
2115 // local_translation + local_pivot = local_transform^-1 local_pivot
2116 // local_translation = local_transform^-1 local_pivot - local_pivot
2118 Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2120 Vector3 local_translation(
2123 matrix4_transformed_point(
2128 matrix4_transformed_point(
2129 matrix4_full_inverse(local_transform),
2137 translation_local2object( parent_translation, local_translation, localToParent );
2141 globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2142 globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2143 globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2144 globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2145 globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2149 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2150 translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2153 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2154 Matrix4 local_transform(
2155 matrix4_multiplied_by_matrix4(
2156 matrix4_full_inverse( localToWorld ),
2157 matrix4_multiplied_by_matrix4(
2158 matrix4_scale_for_vec3( world_scale ),
2163 local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2164 translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2167 class rotate_selected : public SelectionSystem::Visitor
2169 const Quaternion& m_rotate;
2170 const Vector3& m_world_pivot;
2172 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2173 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2175 void visit( scene::Instance& instance ) const {
2176 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2177 if ( transformNode != 0 ) {
2178 Transformable* transform = Instance_getTransformable( instance );
2179 if ( transform != 0 ) {
2180 transform->setType( TRANSFORM_PRIMITIVE );
2181 transform->setScale( c_scale_identity );
2182 transform->setTranslation( c_translation_identity );
2184 transform->setType( TRANSFORM_PRIMITIVE );
2185 transform->setRotation( m_rotate );
2188 Editable* editable = Node_getEditable( instance.path().top() );
2189 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2191 Vector3 parent_translation;
2192 translation_for_pivoted_rotation(
2196 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2197 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2200 transform->setTranslation( parent_translation );
2207 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2208 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2209 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2213 class scale_selected : public SelectionSystem::Visitor
2215 const Vector3& m_scale;
2216 const Vector3& m_world_pivot;
2218 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2219 : m_scale( scaling ), m_world_pivot( world_pivot ){
2221 void visit( scene::Instance& instance ) const {
2222 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2223 if ( transformNode != 0 ) {
2224 Transformable* transform = Instance_getTransformable( instance );
2225 if ( transform != 0 ) {
2226 transform->setType( TRANSFORM_PRIMITIVE );
2227 transform->setScale( c_scale_identity );
2228 transform->setTranslation( c_translation_identity );
2230 transform->setType( TRANSFORM_PRIMITIVE );
2231 transform->setScale( m_scale );
2233 Editable* editable = Node_getEditable( instance.path().top() );
2234 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2236 Vector3 parent_translation;
2237 translation_for_pivoted_scale(
2241 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2242 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2245 transform->setTranslation( parent_translation );
2252 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2253 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2254 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2259 class translate_component_selected : public SelectionSystem::Visitor
2261 const Vector3& m_translate;
2263 translate_component_selected( const Vector3& translate )
2264 : m_translate( translate ){
2266 void visit( scene::Instance& instance ) const {
2267 Transformable* transform = Instance_getTransformable( instance );
2268 if ( transform != 0 ) {
2269 transform->setType( TRANSFORM_COMPONENT );
2270 transform->setTranslation( m_translate );
2275 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2276 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2277 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2281 class rotate_component_selected : public SelectionSystem::Visitor
2283 const Quaternion& m_rotate;
2284 const Vector3& m_world_pivot;
2286 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2287 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2289 void visit( scene::Instance& instance ) const {
2290 Transformable* transform = Instance_getTransformable( instance );
2291 if ( transform != 0 ) {
2292 Vector3 parent_translation;
2293 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2295 transform->setType( TRANSFORM_COMPONENT );
2296 transform->setRotation( m_rotate );
2297 transform->setTranslation( parent_translation );
2302 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2303 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2304 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2308 class scale_component_selected : public SelectionSystem::Visitor
2310 const Vector3& m_scale;
2311 const Vector3& m_world_pivot;
2313 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2314 : m_scale( scaling ), m_world_pivot( world_pivot ){
2316 void visit( scene::Instance& instance ) const {
2317 Transformable* transform = Instance_getTransformable( instance );
2318 if ( transform != 0 ) {
2319 Vector3 parent_translation;
2320 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2322 transform->setType( TRANSFORM_COMPONENT );
2323 transform->setScale( m_scale );
2324 transform->setTranslation( parent_translation );
2329 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2330 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2331 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2336 class BooleanSelector : public Selector
2339 SelectionIntersection m_intersection;
2340 Selectable* m_selectable;
2342 BooleanSelector() : m_selected( false ){
2345 void pushSelectable( Selectable& selectable ){
2346 m_intersection = SelectionIntersection();
2347 m_selectable = &selectable;
2349 void popSelectable(){
2350 if ( m_intersection.valid() ) {
2353 m_intersection = SelectionIntersection();
2355 void addIntersection( const SelectionIntersection& intersection ){
2356 if ( m_selectable->isSelected() ) {
2357 assign_if_closer( m_intersection, intersection );
2366 class BestSelector : public Selector
2368 SelectionIntersection m_intersection;
2369 Selectable* m_selectable;
2370 SelectionIntersection m_bestIntersection;
2371 std::list<Selectable*> m_bestSelectable;
2373 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2376 void pushSelectable( Selectable& selectable ){
2377 m_intersection = SelectionIntersection();
2378 m_selectable = &selectable;
2380 void popSelectable(){
2381 if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2382 m_bestSelectable.push_back( m_selectable );
2383 m_bestIntersection = m_intersection;
2385 else if ( m_intersection < m_bestIntersection ) {
2386 m_bestSelectable.clear();
2387 m_bestSelectable.push_back( m_selectable );
2388 m_bestIntersection = m_intersection;
2390 m_intersection = SelectionIntersection();
2392 void addIntersection( const SelectionIntersection& intersection ){
2393 assign_if_closer( m_intersection, intersection );
2396 std::list<Selectable*>& best(){
2397 return m_bestSelectable;
2401 class DragManipulator : public Manipulator
2403 TranslateFree m_freeResize;
2404 TranslateFree m_freeDrag;
2405 ResizeTranslatable m_resize;
2406 DragTranslatable m_drag;
2407 SelectableBool m_dragSelectable;
2412 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2415 Manipulatable* GetManipulatable(){
2416 return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2419 void testSelect( const View& view, const Matrix4& pivot2world ){
2420 SelectionPool selector;
2422 SelectionVolume test( view );
2424 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2425 BooleanSelector booleanSelector;
2427 Scene_TestSelect_Primitive( booleanSelector, test, view );
2429 if ( booleanSelector.isSelected() ) {
2430 selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2435 m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2440 BestSelector bestSelector;
2441 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2442 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2444 if ( !( *i )->isSelected() ) {
2445 GlobalSelectionSystem().setSelectedAllComponents( false );
2448 selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2449 m_dragSelectable.setSelected( true );
2453 for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2455 ( *i ).second->setSelected( true );
2459 void setSelected( bool select ){
2460 m_selected = select;
2461 m_dragSelectable.setSelected( select );
2463 bool isSelected() const {
2464 return m_selected || m_dragSelectable.isSelected();
2468 class ClipManipulator : public Manipulator
2472 Manipulatable* GetManipulatable(){
2473 ERROR_MESSAGE( "clipper is not manipulatable" );
2477 void setSelected( bool select ){
2479 bool isSelected() const {
2484 class select_all : public scene::Graph::Walker
2488 select_all( bool select )
2489 : m_select( select ){
2491 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2492 Selectable* selectable = Instance_getSelectable( instance );
2493 if ( selectable != 0 ) {
2494 selectable->setSelected( m_select );
2500 class select_all_component : public scene::Graph::Walker
2503 SelectionSystem::EComponentMode m_mode;
2505 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2506 : m_select( select ), m_mode( mode ){
2508 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2509 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2510 if ( componentSelectionTestable ) {
2511 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2517 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2518 GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2522 // RadiantSelectionSystem
2523 class RadiantSelectionSystem :
2524 public SelectionSystem,
2525 public Translatable,
2530 mutable Matrix4 m_pivot2world;
2531 Matrix4 m_pivot2world_start;
2532 Matrix4 m_manip2pivot_start;
2533 Translation m_translation;
2534 Rotation m_rotation;
2537 static Shader* m_state;
2539 EManipulatorMode m_manipulator_mode;
2540 Manipulator* m_manipulator;
2545 EComponentMode m_componentmode;
2547 SelectionCounter m_count_primitive;
2548 SelectionCounter m_count_component;
2550 TranslateManipulator m_translate_manipulator;
2551 RotateManipulator m_rotate_manipulator;
2552 ScaleManipulator m_scale_manipulator;
2553 DragManipulator m_drag_manipulator;
2554 ClipManipulator m_clip_manipulator;
2556 typedef SelectionList<scene::Instance> selection_t;
2557 selection_t m_selection;
2558 selection_t m_component_selection;
2560 Signal1<const Selectable&> m_selectionChanged_callbacks;
2562 void ConstructPivot() const;
2563 mutable bool m_pivotChanged;
2564 bool m_pivot_moving;
2566 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2568 bool nothingSelected() const {
2569 return ( Mode() == eComponent && m_count_component.empty() )
2570 || ( Mode() == ePrimitive && m_count_primitive.empty() );
2583 RadiantSelectionSystem() :
2584 m_undo_begun( false ),
2585 m_mode( ePrimitive ),
2586 m_componentmode( eDefault ),
2587 m_count_primitive( SelectionChangedCaller( *this ) ),
2588 m_count_component( SelectionChangedCaller( *this ) ),
2589 m_translate_manipulator( *this, 2, 64 ),
2590 m_rotate_manipulator( *this, 8, 64 ),
2591 m_scale_manipulator( *this, 0, 64 ),
2592 m_pivotChanged( false ),
2593 m_pivot_moving( false ){
2594 SetManipulatorMode( eTranslate );
2596 addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2597 AddGridChangeCallback( PivotChangedCaller( *this ) );
2599 void pivotChanged() const {
2600 m_pivotChanged = true;
2601 SceneChangeNotify();
2603 typedef ConstMemberCaller<RadiantSelectionSystem, &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2604 void pivotChangedSelection( const Selectable& selectable ){
2607 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2609 void SetMode( EMode mode ){
2610 if ( m_mode != mode ) {
2615 EMode Mode() const {
2618 void SetComponentMode( EComponentMode mode ){
2619 m_componentmode = mode;
2621 EComponentMode ComponentMode() const {
2622 return m_componentmode;
2624 void SetManipulatorMode( EManipulatorMode mode ){
2625 m_manipulator_mode = mode;
2626 switch ( m_manipulator_mode )
2628 case eTranslate: m_manipulator = &m_translate_manipulator; break;
2629 case eRotate: m_manipulator = &m_rotate_manipulator; break;
2630 case eScale: m_manipulator = &m_scale_manipulator; break;
2631 case eDrag: m_manipulator = &m_drag_manipulator; break;
2632 case eClip: m_manipulator = &m_clip_manipulator; break;
2636 EManipulatorMode ManipulatorMode() const {
2637 return m_manipulator_mode;
2640 SelectionChangeCallback getObserver( EMode mode ){
2641 if ( mode == ePrimitive ) {
2642 return makeCallback1( m_count_primitive );
2646 return makeCallback1( m_count_component );
2649 std::size_t countSelected() const {
2650 return m_count_primitive.size();
2652 std::size_t countSelectedComponents() const {
2653 return m_count_component.size();
2655 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2656 if ( selectable.isSelected() ) {
2657 m_selection.append( instance );
2661 m_selection.erase( instance );
2664 ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2666 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2667 if ( selectable.isSelected() ) {
2668 m_component_selection.append( instance );
2672 m_component_selection.erase( instance );
2675 ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2677 scene::Instance& ultimateSelected() const {
2678 ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2679 return m_selection.back();
2681 scene::Instance& penultimateSelected() const {
2682 ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2683 return *( *( --( --m_selection.end() ) ) );
2685 void setSelectedAll( bool selected ){
2686 GlobalSceneGraph().traverse( select_all( selected ) );
2688 m_manipulator->setSelected( selected );
2690 void setSelectedAllComponents( bool selected ){
2691 Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2692 Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2693 Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2695 m_manipulator->setSelected( selected );
2698 void foreachSelected( const Visitor& visitor ) const {
2699 selection_t::const_iterator i = m_selection.begin();
2700 while ( i != m_selection.end() )
2702 visitor.visit( *( *( i++ ) ) );
2705 void foreachSelectedComponent( const Visitor& visitor ) const {
2706 selection_t::const_iterator i = m_component_selection.begin();
2707 while ( i != m_component_selection.end() )
2709 visitor.visit( *( *( i++ ) ) );
2713 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2714 m_selectionChanged_callbacks.connectLast( handler );
2716 void selectionChanged( const Selectable& selectable ){
2717 m_selectionChanged_callbacks( selectable );
2719 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2723 m_pivot2world_start = GetPivot2World();
2726 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2727 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2728 #if defined ( DEBUG_SELECTION )
2729 g_render_clipped.destroy();
2732 m_manipulator->setSelected( false );
2734 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2735 View scissored( view );
2736 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2737 m_manipulator->testSelect( scissored, GetPivot2World() );
2742 m_pivot_moving = m_manipulator->isSelected();
2744 if ( m_pivot_moving ) {
2746 pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2748 m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2750 Matrix4 device2manip;
2751 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2752 m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2754 m_undo_begun = false;
2757 SceneChangeNotify();
2760 return m_pivot_moving;
2764 if ( Mode() == eComponent ) {
2765 setSelectedAllComponents( false );
2769 setSelectedAll( false );
2773 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2774 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2775 if ( modifier == eReplace ) {
2777 setSelectedAllComponents( false );
2785 #if defined ( DEBUG_SELECTION )
2786 g_render_clipped.destroy();
2790 View scissored( view );
2791 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2793 SelectionVolume volume( scissored );
2794 SelectionPool selector;
2796 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2800 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2803 if ( !selector.failed() ) {
2806 case RadiantSelectionSystem::eToggle:
2808 SelectableSortedSet::iterator best = selector.begin();
2809 // toggle selection of the object with least depth
2810 if ( ( *best ).second->isSelected() ) {
2811 ( *best ).second->setSelected( false );
2814 ( *best ).second->setSelected( true );
2818 // if cycle mode not enabled, enable it
2819 case RadiantSelectionSystem::eReplace:
2822 ( *selector.begin() ).second->setSelected( true );
2825 // select the next object in the list from the one already selected
2826 case RadiantSelectionSystem::eCycle:
2828 SelectionPool::iterator i = selector.begin();
2829 while ( i != selector.end() )
2831 if ( ( *i ).second->isSelected() ) {
2832 ( *i ).second->setSelected( false );
2834 if ( i != selector.end() ) {
2835 i->second->setSelected( true );
2839 selector.begin()->second->setSelected( true );
2854 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2855 if ( modifier == eReplace ) {
2857 setSelectedAllComponents( false );
2865 #if defined ( DEBUG_SELECTION )
2866 g_render_clipped.destroy();
2870 View scissored( view );
2871 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
2873 SelectionVolume volume( scissored );
2876 Scene_TestSelect_Component( pool, volume, scissored, eFace );
2880 Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
2883 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
2885 ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
2891 void translate( const Vector3& translation ){
2892 if ( !nothingSelected() ) {
2893 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
2895 m_translation = translation;
2897 m_pivot2world = m_pivot2world_start;
2898 matrix4_translate_by_vec3( m_pivot2world, translation );
2900 if ( Mode() == eComponent ) {
2901 Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
2905 Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
2908 SceneChangeNotify();
2911 void outputTranslation( TextOutputStream& ostream ){
2912 ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
2914 void rotate( const Quaternion& rotation ){
2915 if ( !nothingSelected() ) {
2916 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
2918 m_rotation = rotation;
2920 if ( Mode() == eComponent ) {
2921 Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
2923 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
2927 Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
2929 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
2932 SceneChangeNotify();
2935 void outputRotation( TextOutputStream& ostream ){
2936 ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
2938 void scale( const Vector3& scaling ){
2939 if ( !nothingSelected() ) {
2942 if ( Mode() == eComponent ) {
2943 Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
2947 Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
2950 SceneChangeNotify();
2953 void outputScale( TextOutputStream& ostream ){
2954 ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
2957 void rotateSelected( const Quaternion& rotation ){
2962 void translateSelected( const Vector3& translation ){
2964 translate( translation );
2967 void scaleSelected( const Vector3& scaling ){
2973 void MoveSelected( const View& view, const float device_point[2] ){
2974 if ( m_manipulator->isSelected() ) {
2975 if ( !m_undo_begun ) {
2976 m_undo_begun = true;
2977 GlobalUndoSystem().start();
2980 Matrix4 device2manip;
2981 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2982 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1] );
2986 /// \todo Support view-dependent nudge.
2987 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
2988 if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
2989 translateSelected( nudge );
2994 void freezeTransforms();
2996 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
2997 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
2998 renderSolid( renderer, volume );
3001 const Matrix4& GetPivot2World() const {
3003 return m_pivot2world;
3006 static void constructStatic(){
3007 m_state = GlobalShaderCache().capture( "$POINT" );
3008 #if defined( DEBUG_SELECTION )
3009 g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3011 TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3012 TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3013 RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3016 static void destroyStatic(){
3017 #if defined( DEBUG_SELECTION )
3018 GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3020 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3021 GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3022 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3023 GlobalShaderCache().release( "$POINT" );
3027 Shader* RadiantSelectionSystem::m_state = 0;
3032 RadiantSelectionSystem* g_RadiantSelectionSystem;
3034 inline RadiantSelectionSystem& getSelectionSystem(){
3035 return *g_RadiantSelectionSystem;
3041 class testselect_entity_visible : public scene::Graph::Walker
3043 Selector& m_selector;
3044 SelectionTest& m_test;
3046 testselect_entity_visible( Selector& selector, SelectionTest& test )
3047 : m_selector( selector ), m_test( test ){
3049 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3050 Selectable* selectable = Instance_getSelectable( instance );
3051 if ( selectable != 0
3052 && Node_isEntity( path.top() ) ) {
3053 m_selector.pushSelectable( *selectable );
3056 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3057 if ( selectionTestable ) {
3058 selectionTestable->testSelect( m_selector, m_test );
3063 void post( const scene::Path& path, scene::Instance& instance ) const {
3064 Selectable* selectable = Instance_getSelectable( instance );
3065 if ( selectable != 0
3066 && Node_isEntity( path.top() ) ) {
3067 m_selector.popSelectable();
3072 class testselect_primitive_visible : public scene::Graph::Walker
3074 Selector& m_selector;
3075 SelectionTest& m_test;
3077 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3078 : m_selector( selector ), m_test( test ){
3080 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3081 Selectable* selectable = Instance_getSelectable( instance );
3082 if ( selectable != 0 ) {
3083 m_selector.pushSelectable( *selectable );
3086 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3087 if ( selectionTestable ) {
3088 selectionTestable->testSelect( m_selector, m_test );
3093 void post( const scene::Path& path, scene::Instance& instance ) const {
3094 Selectable* selectable = Instance_getSelectable( instance );
3095 if ( selectable != 0 ) {
3096 m_selector.popSelectable();
3101 class testselect_component_visible : public scene::Graph::Walker
3103 Selector& m_selector;
3104 SelectionTest& m_test;
3105 SelectionSystem::EComponentMode m_mode;
3107 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3108 : m_selector( selector ), m_test( test ), m_mode( mode ){
3110 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3111 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3112 if ( componentSelectionTestable ) {
3113 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3121 class testselect_component_visible_selected : public scene::Graph::Walker
3123 Selector& m_selector;
3124 SelectionTest& m_test;
3125 SelectionSystem::EComponentMode m_mode;
3127 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3128 : m_selector( selector ), m_test( test ), m_mode( mode ){
3130 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3131 Selectable* selectable = Instance_getSelectable( instance );
3132 if ( selectable != 0 && selectable->isSelected() ) {
3133 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3134 if ( componentSelectionTestable ) {
3135 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3143 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3144 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3147 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3148 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3151 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3152 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3155 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3160 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3164 Scene_TestSelect_Primitive( selector, test, view );
3167 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3172 class FreezeTransforms : public scene::Graph::Walker
3175 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3176 TransformNode* transformNode = Node_getTransformNode( path.top() );
3177 if ( transformNode != 0 ) {
3178 Transformable* transform = Instance_getTransformable( instance );
3179 if ( transform != 0 ) {
3180 transform->freezeTransform();
3187 void RadiantSelectionSystem::freezeTransforms(){
3188 GlobalSceneGraph().traverse( FreezeTransforms() );
3192 void RadiantSelectionSystem::endMove(){
3195 if ( Mode() == ePrimitive ) {
3196 if ( ManipulatorMode() == eDrag ) {
3197 Scene_SelectAll_Component( false, SelectionSystem::eFace );
3201 m_pivot_moving = false;
3204 SceneChangeNotify();
3206 if ( m_undo_begun ) {
3207 StringOutputStream command;
3209 if ( ManipulatorMode() == eTranslate ) {
3210 command << "translateTool";
3211 outputTranslation( command );
3213 else if ( ManipulatorMode() == eRotate ) {
3214 command << "rotateTool";
3215 outputRotation( command );
3217 else if ( ManipulatorMode() == eScale ) {
3218 command << "scaleTool";
3219 outputScale( command );
3221 else if ( ManipulatorMode() == eDrag ) {
3222 command << "dragTool";
3225 GlobalUndoSystem().finish( command.c_str() );
3230 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3231 Entity* entity = Node_getEntity( instance.path().top() );
3233 && ( entity->getEntityClass().fixedsize
3234 || !node_is_group( instance.path().top() ) ) ) {
3235 Editable* editable = Node_getEditable( instance.path().top() );
3236 if ( editable != 0 ) {
3237 return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3241 return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3245 return instance.worldAABB();
3248 class bounds_selected : public scene::Graph::Walker
3252 bounds_selected( AABB& bounds )
3253 : m_bounds( bounds ){
3256 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3257 Selectable* selectable = Instance_getSelectable( instance );
3258 if ( selectable != 0
3259 && selectable->isSelected() ) {
3260 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3266 class bounds_selected_component : public scene::Graph::Walker
3270 bounds_selected_component( AABB& bounds )
3271 : m_bounds( bounds ){
3274 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3275 Selectable* selectable = Instance_getSelectable( instance );
3276 if ( selectable != 0
3277 && selectable->isSelected() ) {
3278 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3279 if ( componentEditable ) {
3280 aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3287 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3288 graph.traverse( bounds_selected( bounds ) );
3291 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3292 graph.traverse( bounds_selected_component( bounds ) );
3296 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3297 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3298 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3299 && componentEditable != 0 ) {
3300 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3304 Bounded* bounded = Instance_getBounded( instance );
3305 if ( bounded != 0 ) {
3306 pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3310 pivot = g_matrix4_identity;
3316 void RadiantSelectionSystem::ConstructPivot() const {
3317 if ( !m_pivotChanged || m_pivot_moving ) {
3320 m_pivotChanged = false;
3322 Vector3 m_object_pivot;
3324 if ( !nothingSelected() ) {
3327 if ( Mode() == eComponent ) {
3328 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3332 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3334 m_object_pivot = bounds.origin;
3337 vector3_snap( m_object_pivot, GetSnapGridSize() );
3338 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3340 switch ( m_manipulator_mode )
3345 if ( Mode() == eComponent ) {
3346 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3350 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3354 if ( Mode() == eComponent ) {
3355 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3359 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3368 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3369 //if(view->TestPoint(m_object_pivot))
3370 if ( !nothingSelected() ) {
3371 renderer.Highlight( Renderer::ePrimitive, false );
3372 renderer.Highlight( Renderer::eFace, false );
3374 renderer.SetState( m_state, Renderer::eWireframeOnly );
3375 renderer.SetState( m_state, Renderer::eFullMaterials );
3377 m_manipulator->render( renderer, volume, GetPivot2World() );
3380 #if defined( DEBUG_SELECTION )
3381 renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3382 renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3383 renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3388 void SelectionSystem_OnBoundsChanged(){
3389 getSelectionSystem().pivotChanged();
3393 SignalHandlerId SelectionSystem_boundsChanged;
3395 void SelectionSystem_Construct(){
3396 RadiantSelectionSystem::constructStatic();
3398 g_RadiantSelectionSystem = new RadiantSelectionSystem;
3400 SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<SelectionSystem_OnBoundsChanged>() );
3402 GlobalShaderCache().attachRenderable( getSelectionSystem() );
3405 void SelectionSystem_Destroy(){
3406 GlobalShaderCache().detachRenderable( getSelectionSystem() );
3408 GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3410 delete g_RadiantSelectionSystem;
3412 RadiantSelectionSystem::destroyStatic();
3418 inline float screen_normalised( float pos, std::size_t size ){
3419 return ( ( 2.0f * pos ) / size ) - 1.0f;
3422 typedef Vector2 DeviceVector;
3424 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3425 return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3428 inline float device_constrained( float pos ){
3429 return std::min( 1.0f, std::max( -1.0f, pos ) );
3432 inline DeviceVector device_constrained( DeviceVector device ){
3433 return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3436 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3437 return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3440 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3441 return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3444 typedef Callback1<DeviceVector> MouseEventCallback;
3446 Single<MouseEventCallback> g_mouseMovedCallback;
3447 Single<MouseEventCallback> g_mouseUpCallback;
3450 const ButtonIdentifier c_button_select = c_buttonLeft;
3451 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3452 const ModifierFlags c_modifier_toggle = c_modifierShift;
3453 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3454 const ModifierFlags c_modifier_face = c_modifierControl;
3456 const ButtonIdentifier c_button_select = c_buttonLeft;
3457 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3458 const ModifierFlags c_modifier_toggle = c_modifierControl;
3459 const ModifierFlags c_modifier_replace = c_modifierNone;
3460 const ModifierFlags c_modifier_face = c_modifierShift;
3462 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3463 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3465 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3466 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3467 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3468 const ModifierFlags c_modifier_apply_texture3 = c_modifierShift;
3469 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3473 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3474 if ( state == c_modifier_toggle || state == c_modifier_toggle_face ) {
3475 return RadiantSelectionSystem::eToggle;
3477 if ( state == c_modifier_replace || state == c_modifier_replace_face ) {
3478 return RadiantSelectionSystem::eReplace;
3480 return RadiantSelectionSystem::eManipulator;
3483 rect_t getDeviceArea() const {
3484 DeviceVector delta( m_current - m_start );
3485 if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3486 return SelectionBoxForArea( &m_start[0], &delta[0] );
3490 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3491 return default_area;
3496 DeviceVector m_start;
3497 DeviceVector m_current;
3498 DeviceVector m_epsilon;
3499 std::size_t m_unmoved_replaces;
3500 ModifierFlags m_state;
3502 RectangleCallback m_window_update;
3504 Selector_() : m_start( 0.0f, 0.0f ), m_current( 0.0f, 0.0f ), m_unmoved_replaces( 0 ), m_state( c_modifierNone ){
3508 m_window_update( getDeviceArea() );
3511 void testSelect( DeviceVector position ){
3512 RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3513 if ( modifier != RadiantSelectionSystem::eManipulator ) {
3514 DeviceVector delta( position - m_start );
3515 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3516 DeviceVector delta( position - m_start );
3517 getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3521 if ( modifier == RadiantSelectionSystem::eReplace && m_unmoved_replaces++ > 0 ) {
3522 modifier = RadiantSelectionSystem::eCycle;
3524 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3528 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3532 bool selecting() const {
3533 return m_state != c_modifier_manipulator;
3536 void setState( ModifierFlags state ){
3537 bool was_selecting = selecting();
3539 if ( was_selecting ^ selecting() ) {
3544 ModifierFlags getState() const {
3548 void modifierEnable( ModifierFlags type ){
3549 setState( bitfield_enable( getState(), type ) );
3551 void modifierDisable( ModifierFlags type ){
3552 setState( bitfield_disable( getState(), type ) );
3555 void mouseDown( DeviceVector position ){
3556 m_start = m_current = device_constrained( position );
3559 void mouseMoved( DeviceVector position ){
3560 m_current = device_constrained( position );
3563 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseMoved> MouseMovedCaller;
3565 void mouseUp( DeviceVector position ){
3566 testSelect( device_constrained( position ) );
3568 g_mouseMovedCallback.clear();
3569 g_mouseUpCallback.clear();
3571 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseUp> MouseUpCaller;
3578 DeviceVector m_epsilon;
3581 bool mouseDown( DeviceVector position ){
3582 return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
3585 void mouseMoved( DeviceVector position ){
3586 getSelectionSystem().MoveSelected( *m_view, &position[0] );
3588 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseMoved> MouseMovedCaller;
3590 void mouseUp( DeviceVector position ){
3591 getSelectionSystem().endMove();
3592 g_mouseMovedCallback.clear();
3593 g_mouseUpCallback.clear();
3595 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseUp> MouseUpCaller;
3598 void Scene_copyClosestTexture( SelectionTest& test );
3599 void Scene_applyClosestTexture( SelectionTest& test );
3601 class RadiantWindowObserver : public SelectionSystemWindowObserver
3614 Selector_ m_selector;
3615 Manipulator_ m_manipulator;
3617 RadiantWindowObserver() : m_mouse_down( false ){
3622 void setView( const View& view ){
3623 m_selector.m_view = &view;
3624 m_manipulator.m_view = &view;
3626 void setRectangleDrawCallback( const RectangleCallback& callback ){
3627 m_selector.m_window_update = callback;
3629 void onSizeChanged( int width, int height ){
3632 DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
3633 m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
3635 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3636 if ( button == c_button_select ) {
3637 m_mouse_down = true;
3639 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
3640 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
3641 g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
3642 g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
3646 m_selector.mouseDown( devicePosition );
3647 g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
3648 g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
3651 else if ( button == c_button_texture ) {
3652 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
3654 View scissored( *m_selector.m_view );
3655 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
3656 SelectionVolume volume( scissored );
3658 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
3659 Scene_applyClosestTexture( volume );
3661 else if ( modifiers == c_modifier_copy_texture ) {
3662 Scene_copyClosestTexture( volume );
3666 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
3667 m_selector.m_unmoved_replaces = 0;
3669 if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
3670 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3673 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3674 if ( button == c_button_select && !g_mouseUpCallback.empty() ) {
3675 m_mouse_down = false;
3677 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3680 void onModifierDown( ModifierFlags type ){
3681 m_selector.modifierEnable( type );
3683 void onModifierUp( ModifierFlags type ){
3684 m_selector.modifierDisable( type );
3690 SelectionSystemWindowObserver* NewWindowObserver(){
3691 return new RadiantWindowObserver;
3696 #include "modulesystem/singletonmodule.h"
3697 #include "modulesystem/moduleregistry.h"
3699 class SelectionDependencies :
3700 public GlobalSceneGraphModuleRef,
3701 public GlobalShaderCacheModuleRef,
3702 public GlobalOpenGLModuleRef
3706 class SelectionAPI : public TypeSystemRef
3708 SelectionSystem* m_selection;
3710 typedef SelectionSystem Type;
3711 STRING_CONSTANT( Name, "*" );
3714 SelectionSystem_Construct();
3716 m_selection = &getSelectionSystem();
3719 SelectionSystem_Destroy();
3721 SelectionSystem* getTable(){
3726 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
3727 typedef Static<SelectionModule> StaticSelectionModule;
3728 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );