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 void rotate( const Quaternion& rotation ) = 0;
212 class RotateFree : public Manipulatable
215 Rotatable& m_rotatable;
217 RotateFree( Rotatable& rotatable )
218 : m_rotatable( rotatable ){
220 void Construct( const Matrix4& device2manip, const float x, const float y ){
221 point_on_sphere( m_start, device2manip, x, y );
222 vector3_normalise( m_start );
224 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
227 point_on_sphere( current, device2manip, x, y );
228 vector3_normalise( current );
230 m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
234 class RotateAxis : public Manipulatable
238 Rotatable& m_rotatable;
240 RotateAxis( Rotatable& rotatable )
241 : m_rotatable( rotatable ){
243 void Construct( const Matrix4& device2manip, const float x, const float y ){
244 point_on_sphere( m_start, device2manip, x, y );
245 constrain_to_axis( m_start, m_axis );
247 /// \brief Converts current position to a normalised vector orthogonal to axis.
248 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
250 point_on_sphere( current, device2manip, x, y );
251 constrain_to_axis( current, m_axis );
253 m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
256 void SetAxis( const Vector3& axis ){
261 void translation_local2object( Vector3& object, const Vector3& local, const Matrix4& local2object ){
262 object = matrix4_get_translation_vec3(
263 matrix4_multiplied_by_matrix4(
264 matrix4_translated_by_vec3( local2object, local ),
265 matrix4_full_inverse( local2object )
273 virtual void translate( const Vector3& translation ) = 0;
276 class TranslateAxis : public Manipulatable
280 Translatable& m_translatable;
282 TranslateAxis( Translatable& translatable )
283 : m_translatable( translatable ){
285 void Construct( const Matrix4& device2manip, const float x, const float y ){
286 point_on_axis( m_start, m_axis, device2manip, x, y );
288 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
290 point_on_axis( current, m_axis, device2manip, x, y );
291 current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
293 translation_local2object( current, current, manip2object );
294 vector3_snap( current, GetSnapGridSize() );
296 m_translatable.translate( current );
299 void SetAxis( const Vector3& axis ){
304 class TranslateFree : public Manipulatable
308 Translatable& m_translatable;
310 TranslateFree( Translatable& translatable )
311 : m_translatable( translatable ){
313 void Construct( const Matrix4& device2manip, const float x, const float y ){
314 point_on_plane( m_start, device2manip, x, y );
316 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
318 point_on_plane( current, device2manip, x, y );
319 current = vector3_subtracted( current, m_start );
321 translation_local2object( current, current, manip2object );
322 vector3_snap( current, GetSnapGridSize() );
324 m_translatable.translate( current );
332 virtual void scale( const Vector3& scaling ) = 0;
336 class ScaleAxis : public Manipulatable
341 Scalable& m_scalable;
343 ScaleAxis( Scalable& scalable )
344 : m_scalable( scalable ){
346 void Construct( const Matrix4& device2manip, const float x, const float y ){
347 point_on_axis( m_start, m_axis, device2manip, x, y );
349 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
351 point_on_axis( current, m_axis, device2manip, x, y );
352 Vector3 delta = vector3_subtracted( current, m_start );
354 translation_local2object( delta, delta, manip2object );
355 vector3_snap( delta, GetSnapGridSize() );
357 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
359 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
360 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
361 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
363 m_scalable.scale( scale );
366 void SetAxis( const Vector3& axis ){
371 class ScaleFree : public Manipulatable
375 Scalable& m_scalable;
377 ScaleFree( Scalable& scalable )
378 : m_scalable( scalable ){
380 void Construct( const Matrix4& device2manip, const float x, const float y ){
381 point_on_plane( m_start, device2manip, x, y );
383 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
385 point_on_plane( current, device2manip, x, y );
386 Vector3 delta = vector3_subtracted( current, m_start );
388 translation_local2object( delta, delta, manip2object );
389 vector3_snap( delta, GetSnapGridSize() );
391 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
393 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
394 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
395 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
397 m_scalable.scale( scale );
410 class RenderableClippedPrimitive : public OpenGLRenderable
414 PointVertex m_points[9];
418 std::vector<primitive_t> m_primitives;
422 void render( RenderStateFlags state ) const {
423 for ( std::size_t i = 0; i < m_primitives.size(); ++i )
425 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
426 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
427 switch ( m_primitives[i].m_count )
430 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
431 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
436 void construct( const Matrix4& world2device ){
437 m_inverse = matrix4_full_inverse( world2device );
438 m_world = g_matrix4_identity;
441 void insert( const Vector4 clipped[9], std::size_t count ){
444 m_primitives.back().m_count = count;
445 for ( std::size_t i = 0; i < count; ++i )
447 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
448 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
453 m_primitives.clear();
457 m_primitives.push_back( primitive_t() );
459 const Colour4b colour_clipped( 255, 127, 0, 255 );
461 for ( std::size_t i = 0; i < 9; ++i )
462 m_primitives.back().m_points[i].colour = colour_clipped;
466 #if defined( _DEBUG )
467 #define DEBUG_SELECTION
470 #if defined( DEBUG_SELECTION )
471 Shader* g_state_clipped;
472 RenderableClippedPrimitive g_render_clipped;
477 // dist_Point_to_Line(): get the distance of a point to a line.
478 // Input: a Point P and a Line L (in any dimension)
479 // Return: the shortest distance from P to L
481 dist_Point_to_Line( Point P, Line L ){
482 Vector v = L.P1 - L.P0;
485 double c1 = dot( w,v );
486 double c2 = dot( v,v );
489 Point Pb = L.P0 + b * v;
496 typedef Vector3 point_type;
498 Segment3D( const point_type& _p0, const point_type& _p1 )
499 : p0( _p0 ), p1( _p1 ){
505 typedef Vector3 Point3D;
507 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
508 return vector3_length_squared( b - a );
511 // get the distance of a point to a segment.
512 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
513 Vector3 v = segment.p1 - segment.p0;
514 Vector3 w = point - segment.p0;
516 double c1 = vector3_dot( w,v );
521 double c2 = vector3_dot( v,v );
526 return Point3D( segment.p0 + v * ( c1 / c2 ) );
529 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
530 return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
533 typedef Vector3 point_t;
534 typedef const Vector3* point_iterator_t;
536 // crossing number test for a point in a polygon
537 // This code is patterned after [Franklin, 2000]
538 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
539 std::size_t crossings = 0;
541 // loop through all edges of the polygon
542 for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
543 { // edge from (*prev) to (*cur)
544 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
545 || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
546 // compute the actual edge-ray intersect x-coordinate
547 float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
548 if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
549 ++crossings; // a valid crossing of y=P[1] right of P[0]
553 return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
556 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
557 return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
568 inline SelectionIntersection select_point_from_clipped( Vector4& clipped ){
569 return SelectionIntersection( clipped[2] / clipped[3], static_cast<float>( vector3_length_squared( Vector3( clipped[0] / clipped[3], clipped[1] / clipped[3], 0 ) ) ) );
572 void BestPoint( std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull ){
573 Vector3 normalised[9];
576 for ( std::size_t i = 0; i < count; ++i )
578 normalised[i][0] = clipped[i][0] / clipped[i][3];
579 normalised[i][1] = clipped[i][1] / clipped[i][3];
580 normalised[i][2] = clipped[i][2] / clipped[i][3];
584 if ( cull != eClipCullNone && count > 2 ) {
585 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
587 if ( ( cull == eClipCullCW && signed_area > 0 )
588 || ( cull == eClipCullCCW && signed_area < 0 ) ) {
594 Segment3D segment( normalised[0], normalised[1] );
595 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
596 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
598 else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
599 point_iterator_t end = normalised + count;
600 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
602 Segment3D segment( *previous, *current );
603 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
604 float depth = point.z();
606 float distance = static_cast<float>( vector3_length_squared( point ) );
608 assign_if_closer( best, SelectionIntersection( depth, distance ) );
611 else if ( count > 2 ) {
614 SelectionIntersection(
615 static_cast<float>( ray_distance_to_plane(
616 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
617 plane3_for_points( normalised[0], normalised[1], normalised[2] )
624 #if defined( DEBUG_SELECTION )
626 g_render_clipped.insert( clipped, count );
631 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
633 for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
635 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
636 BestPoint( count, clipped, best, eClipCullNone );
640 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
642 for ( std::size_t i = 0; i < size; ++i )
644 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
645 BestPoint( count, clipped, best, eClipCullNone );
649 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
651 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
652 BestPoint( count, clipped, best, eClipCullNone );
655 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
657 for ( std::size_t i = 0; i < size; ++i )
659 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 );
660 BestPoint( count, clipped, best, cull );
664 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
667 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 );
668 BestPoint( count, clipped, best, cull );
671 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 );
672 BestPoint( count, clipped, best, cull );
676 struct FlatShadedVertex
687 typedef FlatShadedVertex* FlatShadedVertexIterator;
688 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
689 for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
693 matrix4_clip_triangle(
695 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
696 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
697 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
708 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
710 class SelectionPool : public Selector
712 SelectableSortedSet m_pool;
713 SelectionIntersection m_intersection;
714 Selectable* m_selectable;
717 void pushSelectable( Selectable& selectable ){
718 m_intersection = SelectionIntersection();
719 m_selectable = &selectable;
721 void popSelectable(){
722 addSelectable( m_intersection, m_selectable );
723 m_intersection = SelectionIntersection();
725 void addIntersection( const SelectionIntersection& intersection ){
726 assign_if_closer( m_intersection, intersection );
728 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
729 if ( intersection.valid() ) {
730 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
734 typedef SelectableSortedSet::iterator iterator;
737 return m_pool.begin();
744 return m_pool.empty();
749 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
750 const Colour4b g_colour_screen( 0, 255, 255, 255 );
751 const Colour4b g_colour_selected( 255, 255, 0, 255 );
753 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
754 return ( selected ) ? g_colour_selected : colour;
757 template<typename remap_policy>
758 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
759 const double increment = c_pi / double(segments << 2);
761 std::size_t count = 0;
764 remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
765 while ( count < segments )
767 PointVertex* i = vertices + count;
768 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
770 PointVertex* k = i + ( segments << 1 );
771 PointVertex* l = j + ( segments << 1 );
774 PointVertex* m = i + ( segments << 2 );
775 PointVertex* n = j + ( segments << 2 );
776 PointVertex* o = k + ( segments << 2 );
777 PointVertex* p = l + ( segments << 2 );
780 remap_policy::set( i->vertex, x,-y, 0 );
781 remap_policy::set( k->vertex,-y,-x, 0 );
783 remap_policy::set( m->vertex,-x, y, 0 );
784 remap_policy::set( o->vertex, y, x, 0 );
790 const double theta = increment * count;
791 x = static_cast<float>( radius * cos( theta ) );
792 y = static_cast<float>( radius * sin( theta ) );
795 remap_policy::set( j->vertex, y,-x, 0 );
796 remap_policy::set( l->vertex,-x,-y, 0 );
798 remap_policy::set( n->vertex,-y, x, 0 );
799 remap_policy::set( p->vertex, x, y, 0 );
807 virtual Manipulatable* GetManipulatable() = 0;
808 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
810 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
812 virtual void setSelected( bool select ) = 0;
813 virtual bool isSelected() const = 0;
817 inline Vector3 normalised_safe( const Vector3& self ){
818 if ( vector3_equal( self, g_vector3_identity ) ) {
819 return g_vector3_identity;
821 return vector3_normalised( self );
825 class RotateManipulator : public Manipulator
827 struct RenderableCircle : public OpenGLRenderable
829 Array<PointVertex> m_vertices;
831 RenderableCircle( std::size_t size ) : m_vertices( size ){
833 void render( RenderStateFlags state ) const {
834 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
835 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
836 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
838 void setColour( const Colour4b& colour ){
839 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
841 ( *i ).colour = colour;
846 struct RenderableSemiCircle : public OpenGLRenderable
848 Array<PointVertex> m_vertices;
850 RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
852 void render( RenderStateFlags state ) const {
853 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
854 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
855 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
857 void setColour( const Colour4b& colour ){
858 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
860 ( *i ).colour = colour;
867 Vector3 m_axis_screen;
868 RenderableSemiCircle m_circle_x;
869 RenderableSemiCircle m_circle_y;
870 RenderableSemiCircle m_circle_z;
871 RenderableCircle m_circle_screen;
872 RenderableCircle m_circle_sphere;
873 SelectableBool m_selectable_x;
874 SelectableBool m_selectable_y;
875 SelectableBool m_selectable_z;
876 SelectableBool m_selectable_screen;
877 SelectableBool m_selectable_sphere;
879 Matrix4 m_local2world_x;
880 Matrix4 m_local2world_y;
881 Matrix4 m_local2world_z;
882 bool m_circle_x_visible;
883 bool m_circle_y_visible;
884 bool m_circle_z_visible;
886 static Shader* m_state_outer;
888 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
891 m_circle_x( ( segments << 2 ) + 1 ),
892 m_circle_y( ( segments << 2 ) + 1 ),
893 m_circle_z( ( segments << 2 ) + 1 ),
894 m_circle_screen( segments << 3 ),
895 m_circle_sphere( segments << 3 ){
896 draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
897 draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
898 draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
900 draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
901 draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
905 void UpdateColours(){
906 m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
907 m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
908 m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
909 m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
910 m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
913 void updateCircleTransforms(){
914 Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
916 m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
917 if ( m_circle_x_visible ) {
918 m_local2world_x = g_matrix4_identity;
919 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
920 vector3_cross( g_vector3_axis_x, localViewpoint )
922 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
923 vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
925 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
928 m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
929 if ( m_circle_y_visible ) {
930 m_local2world_y = g_matrix4_identity;
931 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
932 vector3_cross( g_vector3_axis_y, localViewpoint )
934 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
935 vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
937 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
940 m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
941 if ( m_circle_z_visible ) {
942 m_local2world_z = g_matrix4_identity;
943 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
944 vector3_cross( g_vector3_axis_z, localViewpoint )
946 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
947 vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
949 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
953 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
954 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
955 updateCircleTransforms();
960 renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
961 renderer.SetState( m_state_outer, Renderer::eFullMaterials );
963 renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
964 renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
966 if ( m_circle_x_visible ) {
967 renderer.addRenderable( m_circle_x, m_local2world_x );
969 if ( m_circle_y_visible ) {
970 renderer.addRenderable( m_circle_y, m_local2world_y );
972 if ( m_circle_z_visible ) {
973 renderer.addRenderable( m_circle_z, m_local2world_z );
976 void testSelect( const View& view, const Matrix4& pivot2world ){
977 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
978 updateCircleTransforms();
980 SelectionPool selector;
984 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
986 #if defined( DEBUG_SELECTION )
987 g_render_clipped.construct( view.GetViewMatrix() );
990 SelectionIntersection best;
991 LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
992 selector.addSelectable( best, &m_selectable_x );
996 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
998 #if defined( DEBUG_SELECTION )
999 g_render_clipped.construct( view.GetViewMatrix() );
1002 SelectionIntersection best;
1003 LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1004 selector.addSelectable( best, &m_selectable_y );
1008 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1010 #if defined( DEBUG_SELECTION )
1011 g_render_clipped.construct( view.GetViewMatrix() );
1014 SelectionIntersection best;
1015 LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1016 selector.addSelectable( best, &m_selectable_z );
1021 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1024 SelectionIntersection best;
1025 LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1026 selector.addSelectable( best, &m_selectable_screen );
1030 SelectionIntersection best;
1031 Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1032 selector.addSelectable( best, &m_selectable_sphere );
1036 m_axis_screen = m_pivot.m_axis_screen;
1038 if ( !selector.failed() ) {
1039 ( *selector.begin() ).second->setSelected( true );
1043 Manipulatable* GetManipulatable(){
1044 if ( m_selectable_x.isSelected() ) {
1045 m_axis.SetAxis( g_vector3_axis_x );
1048 else if ( m_selectable_y.isSelected() ) {
1049 m_axis.SetAxis( g_vector3_axis_y );
1052 else if ( m_selectable_z.isSelected() ) {
1053 m_axis.SetAxis( g_vector3_axis_z );
1056 else if ( m_selectable_screen.isSelected() ) {
1057 m_axis.SetAxis( m_axis_screen );
1065 void setSelected( bool select ){
1066 m_selectable_x.setSelected( select );
1067 m_selectable_y.setSelected( select );
1068 m_selectable_z.setSelected( select );
1069 m_selectable_screen.setSelected( select );
1071 bool isSelected() const {
1072 return m_selectable_x.isSelected()
1073 | m_selectable_y.isSelected()
1074 | m_selectable_z.isSelected()
1075 | m_selectable_screen.isSelected()
1076 | m_selectable_sphere.isSelected();
1080 Shader* RotateManipulator::m_state_outer;
1083 const float arrowhead_length = 16;
1084 const float arrowhead_radius = 4;
1086 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1087 ( *line++ ).vertex = vertex3f_identity;
1088 ( *line ).vertex = vertex3f_identity;
1089 vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1092 template<typename VertexRemap, typename NormalRemap>
1093 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1094 std::size_t head_tris = ( segments << 3 );
1095 const double head_segment = c_2pi / head_tris;
1096 for ( std::size_t i = 0; i < head_tris; ++i )
1099 FlatShadedVertex& point = vertices[i * 6 + 0];
1100 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1101 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1102 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1103 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1104 NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1105 NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1108 FlatShadedVertex& point = vertices[i * 6 + 1];
1109 VertexRemap::x( point.vertex ) = length;
1110 VertexRemap::y( point.vertex ) = 0;
1111 VertexRemap::z( point.vertex ) = 0;
1112 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1113 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1114 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1117 FlatShadedVertex& point = vertices[i * 6 + 2];
1118 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1119 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1120 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1121 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1122 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1123 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1127 FlatShadedVertex& point = vertices[i * 6 + 3];
1128 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1129 VertexRemap::y( point.vertex ) = 0;
1130 VertexRemap::z( point.vertex ) = 0;
1131 NormalRemap::x( point.normal ) = -1;
1132 NormalRemap::y( point.normal ) = 0;
1133 NormalRemap::z( point.normal ) = 0;
1136 FlatShadedVertex& point = vertices[i * 6 + 4];
1137 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1138 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1139 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1140 NormalRemap::x( point.normal ) = -1;
1141 NormalRemap::y( point.normal ) = 0;
1142 NormalRemap::z( point.normal ) = 0;
1145 FlatShadedVertex& point = vertices[i * 6 + 5];
1146 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1147 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1148 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1149 NormalRemap::x( point.normal ) = -1;
1150 NormalRemap::y( point.normal ) = 0;
1151 NormalRemap::z( point.normal ) = 0;
1156 template<typename Triple>
1157 class TripleRemapXYZ
1160 static float& x( Triple& triple ){
1163 static float& y( Triple& triple ){
1166 static float& z( Triple& triple ){
1171 template<typename Triple>
1172 class TripleRemapYZX
1175 static float& x( Triple& triple ){
1178 static float& y( Triple& triple ){
1181 static float& z( Triple& triple ){
1186 template<typename Triple>
1187 class TripleRemapZXY
1190 static float& x( Triple& triple ){
1193 static float& y( Triple& triple ){
1196 static float& z( Triple& triple ){
1201 void vector3_print( const Vector3& v ){
1202 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1205 class TranslateManipulator : public Manipulator
1207 struct RenderableArrowLine : public OpenGLRenderable
1209 PointVertex m_line[2];
1211 RenderableArrowLine(){
1213 void render( RenderStateFlags state ) const {
1214 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1215 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1216 glDrawArrays( GL_LINES, 0, 2 );
1218 void setColour( const Colour4b& colour ){
1219 m_line[0].colour = colour;
1220 m_line[1].colour = colour;
1223 struct RenderableArrowHead : public OpenGLRenderable
1225 Array<FlatShadedVertex> m_vertices;
1227 RenderableArrowHead( std::size_t size )
1228 : m_vertices( size ){
1230 void render( RenderStateFlags state ) const {
1231 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1232 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1233 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1234 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1236 void setColour( const Colour4b& colour ){
1237 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1239 ( *i ).colour = colour;
1243 struct RenderableQuad : public OpenGLRenderable
1245 PointVertex m_quad[4];
1246 void render( RenderStateFlags state ) const {
1247 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1248 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1249 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1251 void setColour( const Colour4b& colour ){
1252 m_quad[0].colour = colour;
1253 m_quad[1].colour = colour;
1254 m_quad[2].colour = colour;
1255 m_quad[3].colour = colour;
1259 TranslateFree m_free;
1260 TranslateAxis m_axis;
1261 RenderableArrowLine m_arrow_x;
1262 RenderableArrowLine m_arrow_y;
1263 RenderableArrowLine m_arrow_z;
1264 RenderableArrowHead m_arrow_head_x;
1265 RenderableArrowHead m_arrow_head_y;
1266 RenderableArrowHead m_arrow_head_z;
1267 RenderableQuad m_quad_screen;
1268 SelectableBool m_selectable_x;
1269 SelectableBool m_selectable_y;
1270 SelectableBool m_selectable_z;
1271 SelectableBool m_selectable_screen;
1272 Pivot2World m_pivot;
1274 static Shader* m_state_wire;
1275 static Shader* m_state_fill;
1277 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1278 m_free( translatable ),
1279 m_axis( translatable ),
1280 m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1281 m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1282 m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1283 draw_arrowline( length, m_arrow_x.m_line, 0 );
1284 draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1285 draw_arrowline( length, m_arrow_y.m_line, 1 );
1286 draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1287 draw_arrowline( length, m_arrow_z.m_line, 2 );
1288 draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1290 draw_quad( 16, m_quad_screen.m_quad );
1293 void UpdateColours(){
1294 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1295 m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1296 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1297 m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1298 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1299 m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1300 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1303 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1304 return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1307 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1308 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1313 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1314 bool show_x = manipulator_show_axis( m_pivot, x );
1316 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1317 bool show_y = manipulator_show_axis( m_pivot, y );
1319 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1320 bool show_z = manipulator_show_axis( m_pivot, z );
1322 renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1323 renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1326 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1329 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1332 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1335 renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1337 renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1338 renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1341 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1344 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1347 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1350 void testSelect( const View& view, const Matrix4& pivot2world ){
1351 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1353 SelectionPool selector;
1355 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1356 bool show_x = manipulator_show_axis( m_pivot, x );
1358 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1359 bool show_y = manipulator_show_axis( m_pivot, y );
1361 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1362 bool show_z = manipulator_show_axis( m_pivot, z );
1365 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1368 SelectionIntersection best;
1369 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1370 if ( best.valid() ) {
1371 best = SelectionIntersection( 0, 0 );
1372 selector.addSelectable( best, &m_selectable_screen );
1378 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1380 #if defined( DEBUG_SELECTION )
1381 g_render_clipped.construct( view.GetViewMatrix() );
1385 SelectionIntersection best;
1386 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1387 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1388 selector.addSelectable( best, &m_selectable_x );
1392 SelectionIntersection best;
1393 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1394 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1395 selector.addSelectable( best, &m_selectable_y );
1399 SelectionIntersection best;
1400 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1401 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1402 selector.addSelectable( best, &m_selectable_z );
1406 if ( !selector.failed() ) {
1407 ( *selector.begin() ).second->setSelected( true );
1411 Manipulatable* GetManipulatable(){
1412 if ( m_selectable_x.isSelected() ) {
1413 m_axis.SetAxis( g_vector3_axis_x );
1416 else if ( m_selectable_y.isSelected() ) {
1417 m_axis.SetAxis( g_vector3_axis_y );
1420 else if ( m_selectable_z.isSelected() ) {
1421 m_axis.SetAxis( g_vector3_axis_z );
1430 void setSelected( bool select ){
1431 m_selectable_x.setSelected( select );
1432 m_selectable_y.setSelected( select );
1433 m_selectable_z.setSelected( select );
1434 m_selectable_screen.setSelected( select );
1436 bool isSelected() const {
1437 return m_selectable_x.isSelected()
1438 | m_selectable_y.isSelected()
1439 | m_selectable_z.isSelected()
1440 | m_selectable_screen.isSelected();
1444 Shader* TranslateManipulator::m_state_wire;
1445 Shader* TranslateManipulator::m_state_fill;
1447 class ScaleManipulator : public Manipulator
1449 struct RenderableArrow : public OpenGLRenderable
1451 PointVertex m_line[2];
1453 void render( RenderStateFlags state ) const {
1454 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1455 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1456 glDrawArrays( GL_LINES, 0, 2 );
1458 void setColour( const Colour4b& colour ){
1459 m_line[0].colour = colour;
1460 m_line[1].colour = colour;
1463 struct RenderableQuad : public OpenGLRenderable
1465 PointVertex m_quad[4];
1466 void render( RenderStateFlags state ) const {
1467 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1468 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1469 glDrawArrays( GL_QUADS, 0, 4 );
1471 void setColour( const Colour4b& colour ){
1472 m_quad[0].colour = colour;
1473 m_quad[1].colour = colour;
1474 m_quad[2].colour = colour;
1475 m_quad[3].colour = colour;
1481 RenderableArrow m_arrow_x;
1482 RenderableArrow m_arrow_y;
1483 RenderableArrow m_arrow_z;
1484 RenderableQuad m_quad_screen;
1485 SelectableBool m_selectable_x;
1486 SelectableBool m_selectable_y;
1487 SelectableBool m_selectable_z;
1488 SelectableBool m_selectable_screen;
1489 Pivot2World m_pivot;
1491 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1494 draw_arrowline( length, m_arrow_x.m_line, 0 );
1495 draw_arrowline( length, m_arrow_y.m_line, 1 );
1496 draw_arrowline( length, m_arrow_z.m_line, 2 );
1498 draw_quad( 16, m_quad_screen.m_quad );
1501 Pivot2World& getPivot(){
1505 void UpdateColours(){
1506 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1507 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1508 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1509 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1512 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1513 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1518 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1519 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1520 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1522 renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1524 void testSelect( const View& view, const Matrix4& pivot2world ){
1525 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1527 SelectionPool selector;
1530 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1532 #if defined( DEBUG_SELECTION )
1533 g_render_clipped.construct( view.GetViewMatrix() );
1537 SelectionIntersection best;
1538 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1539 selector.addSelectable( best, &m_selectable_x );
1543 SelectionIntersection best;
1544 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1545 selector.addSelectable( best, &m_selectable_y );
1549 SelectionIntersection best;
1550 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1551 selector.addSelectable( best, &m_selectable_z );
1556 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1559 SelectionIntersection best;
1560 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1561 selector.addSelectable( best, &m_selectable_screen );
1565 if ( !selector.failed() ) {
1566 ( *selector.begin() ).second->setSelected( true );
1570 Manipulatable* GetManipulatable(){
1571 if ( m_selectable_x.isSelected() ) {
1572 m_axis.SetAxis( g_vector3_axis_x );
1575 else if ( m_selectable_y.isSelected() ) {
1576 m_axis.SetAxis( g_vector3_axis_y );
1579 else if ( m_selectable_z.isSelected() ) {
1580 m_axis.SetAxis( g_vector3_axis_z );
1588 void setSelected( bool select ){
1589 m_selectable_x.setSelected( select );
1590 m_selectable_y.setSelected( select );
1591 m_selectable_z.setSelected( select );
1592 m_selectable_screen.setSelected( select );
1594 bool isSelected() const {
1595 return m_selectable_x.isSelected()
1596 | m_selectable_y.isSelected()
1597 | m_selectable_z.isSelected()
1598 | m_selectable_screen.isSelected();
1603 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1604 return InstanceTypeCast<PlaneSelectable>::cast( instance );
1607 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1609 Selector& m_selector;
1610 SelectionTest& m_test;
1611 PlaneCallback m_selectedPlaneCallback;
1613 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1614 : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1616 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1617 if ( path.top().get().visible() ) {
1618 Selectable* selectable = Instance_getSelectable( instance );
1619 if ( selectable != 0 && selectable->isSelected() ) {
1620 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1621 if ( planeSelectable != 0 ) {
1622 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1630 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1632 Selector& m_selector;
1633 const SelectedPlanes& m_selectedPlanes;
1635 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1636 : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1638 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1639 if ( path.top().get().visible() ) {
1640 Selectable* selectable = Instance_getSelectable( instance );
1641 if ( selectable != 0 && selectable->isSelected() ) {
1642 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1643 if ( planeSelectable != 0 ) {
1644 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1652 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1653 graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1656 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1657 graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1664 bool operator()( const Plane3& plane, const Plane3& other ) const {
1665 if ( plane.a < other.a ) {
1668 if ( other.a < plane.a ) {
1672 if ( plane.b < other.b ) {
1675 if ( other.b < plane.b ) {
1679 if ( plane.c < other.c ) {
1682 if ( other.c < plane.c ) {
1686 if ( plane.d < other.d ) {
1689 if ( other.d < plane.d ) {
1697 typedef std::set<Plane3, PlaneLess> PlaneSet;
1699 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1700 self.insert( plane );
1703 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1704 return self.find( plane ) != self.end();
1708 class SelectedPlaneSet : public SelectedPlanes
1710 PlaneSet m_selectedPlanes;
1712 bool empty() const {
1713 return m_selectedPlanes.empty();
1716 void insert( const Plane3& plane ){
1717 PlaneSet_insert( m_selectedPlanes, plane );
1719 bool contains( const Plane3& plane ) const {
1720 return PlaneSet_contains( m_selectedPlanes, plane );
1722 typedef MemberCaller1<SelectedPlaneSet, const Plane3&, &SelectedPlaneSet::insert> InsertCaller;
1726 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1727 SelectedPlaneSet selectedPlanes;
1729 Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1730 Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1732 return !selectedPlanes.empty();
1735 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1736 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1737 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1738 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1739 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1740 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1742 class ResizeTranslatable : public Translatable
1744 void translate( const Vector3& translation ){
1745 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1749 class DragTranslatable : public Translatable
1751 void translate( const Vector3& translation ){
1752 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1753 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1757 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1762 class SelectionVolume : public SelectionTest
1764 Matrix4 m_local2view;
1770 SelectionVolume( const View& view )
1774 const VolumeTest& getVolume() const {
1778 const Vector3& getNear() const {
1781 const Vector3& getFar() const {
1785 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1786 m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
1788 // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1789 // Don't cull if the view is wireframe and the polygons are two-sided.
1790 m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
1793 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
1795 m_near = vector4_projected(
1796 matrix4_transformed_vector4(
1798 Vector4( 0, 0, -1, 1 )
1802 m_far = vector4_projected(
1803 matrix4_transformed_vector4(
1805 Vector4( 0, 0, 1, 1 )
1810 #if defined( DEBUG_SELECTION )
1811 g_render_clipped.construct( m_view.GetViewMatrix() );
1814 void TestPoint( const Vector3& point, SelectionIntersection& best ){
1816 if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
1817 best = select_point_from_clipped( clipped );
1820 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1822 for ( std::size_t i = 0; i + 2 < count; ++i )
1825 matrix4_clip_triangle(
1827 reinterpret_cast<const Vector3&>( vertices[0] ),
1828 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
1829 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
1838 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1843 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
1848 reinterpret_cast<const Vector3&>( ( *prev ) ),
1849 reinterpret_cast<const Vector3&>( ( *i ) ),
1858 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1863 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
1868 reinterpret_cast<const Vector3&>( ( *i ) ),
1869 reinterpret_cast<const Vector3&>( ( *next ) ),
1878 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1883 for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
1888 reinterpret_cast<const Vector3&>( ( *i ) ),
1889 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
1898 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1900 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
1903 matrix4_clip_triangle(
1905 reinterpret_cast<const Vector3&>( vertices[*i] ),
1906 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1907 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1916 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1918 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
1921 matrix4_clip_triangle(
1923 reinterpret_cast<const Vector3&>( vertices[*i] ),
1924 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1925 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1933 matrix4_clip_triangle(
1935 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1936 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1937 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1946 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1948 for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
1951 matrix4_clip_triangle(
1953 reinterpret_cast<const Vector3&>( vertices[*i] ),
1954 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1955 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1963 matrix4_clip_triangle(
1965 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1966 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1967 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1978 class SelectionCounter
1981 typedef const Selectable& first_argument_type;
1983 SelectionCounter( const SelectionChangeCallback& onchanged )
1984 : m_count( 0 ), m_onchanged( onchanged ){
1986 void operator()( const Selectable& selectable ){
1987 if ( selectable.isSelected() ) {
1992 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
1996 m_onchanged( selectable );
1998 bool empty() const {
1999 return m_count == 0;
2001 std::size_t size() const {
2005 std::size_t m_count;
2006 SelectionChangeCallback m_onchanged;
2009 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2010 view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2013 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2014 rect_t selection_box;
2015 selection_box.min[0] = device_point[0] - device_epsilon[0];
2016 selection_box.min[1] = device_point[1] - device_epsilon[1];
2017 selection_box.max[0] = device_point[0] + device_epsilon[0];
2018 selection_box.max[1] = device_point[1] + device_epsilon[1];
2019 return selection_box;
2022 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2023 rect_t selection_box;
2024 selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2025 selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2026 selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2027 selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2028 return selection_box;
2031 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2032 return quaternion_normalised( quaternion_multiplied_by_quaternion(
2033 quaternion_normalised( quaternion_multiplied_by_quaternion(
2034 quaternion_inverse( localToWorld ),
2041 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2042 matrix[0] = other[0];
2043 matrix[1] = other[1];
2044 matrix[2] = other[2];
2045 matrix[4] = other[4];
2046 matrix[5] = other[5];
2047 matrix[6] = other[6];
2048 matrix[8] = other[8];
2049 matrix[9] = other[9];
2050 matrix[10] = other[10];
2053 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2054 Editable* editable = Node_getEditable( instance.path().top() );
2055 if ( editable != 0 ) {
2056 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2060 matrix4_assign_rotation( matrix, instance.localToWorld() );
2064 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2065 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2066 return componentSelectionTestable != 0
2067 && componentSelectionTestable->isSelectedComponents();
2070 class TranslateSelected : public SelectionSystem::Visitor
2072 const Vector3& m_translate;
2074 TranslateSelected( const Vector3& translate )
2075 : m_translate( translate ){
2077 void visit( scene::Instance& instance ) const {
2078 Transformable* transform = Instance_getTransformable( instance );
2079 if ( transform != 0 ) {
2080 transform->setType( TRANSFORM_PRIMITIVE );
2081 transform->setTranslation( m_translate );
2086 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2087 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2088 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2092 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2094 matrix4_transformed_point(
2095 matrix4_full_inverse( localToWorld ),
2101 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2102 // we need a translation inside the parent system to move the origin of this object to the right place
2104 // mathematically, it must fulfill:
2106 // local_translation local_transform local_pivot = local_pivot
2107 // local_translation = local_pivot - local_transform local_pivot
2110 // local_transform local_translation local_pivot = local_pivot
2111 // local_translation local_pivot = local_transform^-1 local_pivot
2112 // local_translation + local_pivot = local_transform^-1 local_pivot
2113 // local_translation = local_transform^-1 local_pivot - local_pivot
2115 Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2117 Vector3 local_translation(
2120 matrix4_transformed_point(
2125 matrix4_transformed_point(
2126 matrix4_full_inverse(local_transform),
2134 translation_local2object( parent_translation, local_translation, localToParent );
2138 globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2139 globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2140 globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2141 globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2142 globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2146 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2147 translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2150 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2151 Matrix4 local_transform(
2152 matrix4_multiplied_by_matrix4(
2153 matrix4_full_inverse( localToWorld ),
2154 matrix4_multiplied_by_matrix4(
2155 matrix4_scale_for_vec3( world_scale ),
2160 local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2161 translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2164 class rotate_selected : public SelectionSystem::Visitor
2166 const Quaternion& m_rotate;
2167 const Vector3& m_world_pivot;
2169 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2170 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2172 void visit( scene::Instance& instance ) const {
2173 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2174 if ( transformNode != 0 ) {
2175 Transformable* transform = Instance_getTransformable( instance );
2176 if ( transform != 0 ) {
2177 transform->setType( TRANSFORM_PRIMITIVE );
2178 transform->setScale( c_scale_identity );
2179 transform->setTranslation( c_translation_identity );
2181 transform->setType( TRANSFORM_PRIMITIVE );
2182 transform->setRotation( m_rotate );
2185 Editable* editable = Node_getEditable( instance.path().top() );
2186 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2188 Vector3 parent_translation;
2189 translation_for_pivoted_rotation(
2193 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2194 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2197 transform->setTranslation( parent_translation );
2204 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2205 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2206 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2210 class scale_selected : public SelectionSystem::Visitor
2212 const Vector3& m_scale;
2213 const Vector3& m_world_pivot;
2215 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2216 : m_scale( scaling ), m_world_pivot( world_pivot ){
2218 void visit( scene::Instance& instance ) const {
2219 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2220 if ( transformNode != 0 ) {
2221 Transformable* transform = Instance_getTransformable( instance );
2222 if ( transform != 0 ) {
2223 transform->setType( TRANSFORM_PRIMITIVE );
2224 transform->setScale( c_scale_identity );
2225 transform->setTranslation( c_translation_identity );
2227 transform->setType( TRANSFORM_PRIMITIVE );
2228 transform->setScale( m_scale );
2230 Editable* editable = Node_getEditable( instance.path().top() );
2231 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2233 Vector3 parent_translation;
2234 translation_for_pivoted_scale(
2238 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2239 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2242 transform->setTranslation( parent_translation );
2249 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2250 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2251 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2256 class translate_component_selected : public SelectionSystem::Visitor
2258 const Vector3& m_translate;
2260 translate_component_selected( const Vector3& translate )
2261 : m_translate( translate ){
2263 void visit( scene::Instance& instance ) const {
2264 Transformable* transform = Instance_getTransformable( instance );
2265 if ( transform != 0 ) {
2266 transform->setType( TRANSFORM_COMPONENT );
2267 transform->setTranslation( m_translate );
2272 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2273 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2274 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2278 class rotate_component_selected : public SelectionSystem::Visitor
2280 const Quaternion& m_rotate;
2281 const Vector3& m_world_pivot;
2283 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2284 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2286 void visit( scene::Instance& instance ) const {
2287 Transformable* transform = Instance_getTransformable( instance );
2288 if ( transform != 0 ) {
2289 Vector3 parent_translation;
2290 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2292 transform->setType( TRANSFORM_COMPONENT );
2293 transform->setRotation( m_rotate );
2294 transform->setTranslation( parent_translation );
2299 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2300 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2301 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2305 class scale_component_selected : public SelectionSystem::Visitor
2307 const Vector3& m_scale;
2308 const Vector3& m_world_pivot;
2310 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2311 : m_scale( scaling ), m_world_pivot( world_pivot ){
2313 void visit( scene::Instance& instance ) const {
2314 Transformable* transform = Instance_getTransformable( instance );
2315 if ( transform != 0 ) {
2316 Vector3 parent_translation;
2317 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2319 transform->setType( TRANSFORM_COMPONENT );
2320 transform->setScale( m_scale );
2321 transform->setTranslation( parent_translation );
2326 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2327 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2328 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2333 class BooleanSelector : public Selector
2336 SelectionIntersection m_intersection;
2337 Selectable* m_selectable;
2339 BooleanSelector() : m_selected( false ){
2342 void pushSelectable( Selectable& selectable ){
2343 m_intersection = SelectionIntersection();
2344 m_selectable = &selectable;
2346 void popSelectable(){
2347 if ( m_intersection.valid() ) {
2350 m_intersection = SelectionIntersection();
2352 void addIntersection( const SelectionIntersection& intersection ){
2353 if ( m_selectable->isSelected() ) {
2354 assign_if_closer( m_intersection, intersection );
2363 class BestSelector : public Selector
2365 SelectionIntersection m_intersection;
2366 Selectable* m_selectable;
2367 SelectionIntersection m_bestIntersection;
2368 std::list<Selectable*> m_bestSelectable;
2370 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2373 void pushSelectable( Selectable& selectable ){
2374 m_intersection = SelectionIntersection();
2375 m_selectable = &selectable;
2377 void popSelectable(){
2378 if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2379 m_bestSelectable.push_back( m_selectable );
2380 m_bestIntersection = m_intersection;
2382 else if ( m_intersection < m_bestIntersection ) {
2383 m_bestSelectable.clear();
2384 m_bestSelectable.push_back( m_selectable );
2385 m_bestIntersection = m_intersection;
2387 m_intersection = SelectionIntersection();
2389 void addIntersection( const SelectionIntersection& intersection ){
2390 assign_if_closer( m_intersection, intersection );
2393 std::list<Selectable*>& best(){
2394 return m_bestSelectable;
2398 class DragManipulator : public Manipulator
2400 TranslateFree m_freeResize;
2401 TranslateFree m_freeDrag;
2402 ResizeTranslatable m_resize;
2403 DragTranslatable m_drag;
2404 SelectableBool m_dragSelectable;
2409 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2412 Manipulatable* GetManipulatable(){
2413 return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2416 void testSelect( const View& view, const Matrix4& pivot2world ){
2417 SelectionPool selector;
2419 SelectionVolume test( view );
2421 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2422 BooleanSelector booleanSelector;
2424 Scene_TestSelect_Primitive( booleanSelector, test, view );
2426 if ( booleanSelector.isSelected() ) {
2427 selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2432 m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2437 BestSelector bestSelector;
2438 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2439 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2441 if ( !( *i )->isSelected() ) {
2442 GlobalSelectionSystem().setSelectedAllComponents( false );
2445 selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2446 m_dragSelectable.setSelected( true );
2450 for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2452 ( *i ).second->setSelected( true );
2456 void setSelected( bool select ){
2457 m_selected = select;
2458 m_dragSelectable.setSelected( select );
2460 bool isSelected() const {
2461 return m_selected || m_dragSelectable.isSelected();
2465 class ClipManipulator : public Manipulator
2469 Manipulatable* GetManipulatable(){
2470 ERROR_MESSAGE( "clipper is not manipulatable" );
2474 void setSelected( bool select ){
2476 bool isSelected() const {
2481 class select_all : public scene::Graph::Walker
2485 select_all( bool select )
2486 : m_select( select ){
2488 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2489 Selectable* selectable = Instance_getSelectable( instance );
2490 if ( selectable != 0 ) {
2491 selectable->setSelected( m_select );
2497 class select_all_component : public scene::Graph::Walker
2500 SelectionSystem::EComponentMode m_mode;
2502 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2503 : m_select( select ), m_mode( mode ){
2505 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2506 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2507 if ( componentSelectionTestable ) {
2508 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2514 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2515 GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2519 // RadiantSelectionSystem
2520 class RadiantSelectionSystem :
2521 public SelectionSystem,
2522 public Translatable,
2527 mutable Matrix4 m_pivot2world;
2528 Matrix4 m_pivot2world_start;
2529 Matrix4 m_manip2pivot_start;
2530 Translation m_translation;
2531 Rotation m_rotation;
2534 static Shader* m_state;
2536 EManipulatorMode m_manipulator_mode;
2537 Manipulator* m_manipulator;
2542 EComponentMode m_componentmode;
2544 SelectionCounter m_count_primitive;
2545 SelectionCounter m_count_component;
2547 TranslateManipulator m_translate_manipulator;
2548 RotateManipulator m_rotate_manipulator;
2549 ScaleManipulator m_scale_manipulator;
2550 DragManipulator m_drag_manipulator;
2551 ClipManipulator m_clip_manipulator;
2553 typedef SelectionList<scene::Instance> selection_t;
2554 selection_t m_selection;
2555 selection_t m_component_selection;
2557 Signal1<const Selectable&> m_selectionChanged_callbacks;
2559 void ConstructPivot() const;
2560 mutable bool m_pivotChanged;
2561 bool m_pivot_moving;
2563 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2565 bool nothingSelected() const {
2566 return ( Mode() == eComponent && m_count_component.empty() )
2567 || ( Mode() == ePrimitive && m_count_primitive.empty() );
2580 RadiantSelectionSystem() :
2581 m_undo_begun( false ),
2582 m_mode( ePrimitive ),
2583 m_componentmode( eDefault ),
2584 m_count_primitive( SelectionChangedCaller( *this ) ),
2585 m_count_component( SelectionChangedCaller( *this ) ),
2586 m_translate_manipulator( *this, 2, 64 ),
2587 m_rotate_manipulator( *this, 8, 64 ),
2588 m_scale_manipulator( *this, 0, 64 ),
2589 m_pivotChanged( false ),
2590 m_pivot_moving( false ){
2591 SetManipulatorMode( eTranslate );
2593 addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2594 AddGridChangeCallback( PivotChangedCaller( *this ) );
2596 void pivotChanged() const {
2597 m_pivotChanged = true;
2598 SceneChangeNotify();
2600 typedef ConstMemberCaller<RadiantSelectionSystem, &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2601 void pivotChangedSelection( const Selectable& selectable ){
2604 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2606 void SetMode( EMode mode ){
2607 if ( m_mode != mode ) {
2612 EMode Mode() const {
2615 void SetComponentMode( EComponentMode mode ){
2616 m_componentmode = mode;
2618 EComponentMode ComponentMode() const {
2619 return m_componentmode;
2621 void SetManipulatorMode( EManipulatorMode mode ){
2622 m_manipulator_mode = mode;
2623 switch ( m_manipulator_mode )
2625 case eTranslate: m_manipulator = &m_translate_manipulator; break;
2626 case eRotate: m_manipulator = &m_rotate_manipulator; break;
2627 case eScale: m_manipulator = &m_scale_manipulator; break;
2628 case eDrag: m_manipulator = &m_drag_manipulator; break;
2629 case eClip: m_manipulator = &m_clip_manipulator; break;
2633 EManipulatorMode ManipulatorMode() const {
2634 return m_manipulator_mode;
2637 SelectionChangeCallback getObserver( EMode mode ){
2638 if ( mode == ePrimitive ) {
2639 return makeCallback1( m_count_primitive );
2643 return makeCallback1( m_count_component );
2646 std::size_t countSelected() const {
2647 return m_count_primitive.size();
2649 std::size_t countSelectedComponents() const {
2650 return m_count_component.size();
2652 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2653 if ( selectable.isSelected() ) {
2654 m_selection.append( instance );
2658 m_selection.erase( instance );
2661 ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2663 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2664 if ( selectable.isSelected() ) {
2665 m_component_selection.append( instance );
2669 m_component_selection.erase( instance );
2672 ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2674 scene::Instance& ultimateSelected() const {
2675 ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2676 return m_selection.back();
2678 scene::Instance& penultimateSelected() const {
2679 ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2680 return *( *( --( --m_selection.end() ) ) );
2682 void setSelectedAll( bool selected ){
2683 GlobalSceneGraph().traverse( select_all( selected ) );
2685 m_manipulator->setSelected( selected );
2687 void setSelectedAllComponents( bool selected ){
2688 Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2689 Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2690 Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2692 m_manipulator->setSelected( selected );
2695 void foreachSelected( const Visitor& visitor ) const {
2696 selection_t::const_iterator i = m_selection.begin();
2697 while ( i != m_selection.end() )
2699 visitor.visit( *( *( i++ ) ) );
2702 void foreachSelectedComponent( const Visitor& visitor ) const {
2703 selection_t::const_iterator i = m_component_selection.begin();
2704 while ( i != m_component_selection.end() )
2706 visitor.visit( *( *( i++ ) ) );
2710 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2711 m_selectionChanged_callbacks.connectLast( handler );
2713 void selectionChanged( const Selectable& selectable ){
2714 m_selectionChanged_callbacks( selectable );
2716 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2720 m_pivot2world_start = GetPivot2World();
2723 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2724 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2725 #if defined ( DEBUG_SELECTION )
2726 g_render_clipped.destroy();
2729 m_manipulator->setSelected( false );
2731 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2732 View scissored( view );
2733 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2734 m_manipulator->testSelect( scissored, GetPivot2World() );
2739 m_pivot_moving = m_manipulator->isSelected();
2741 if ( m_pivot_moving ) {
2743 pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2745 m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2747 Matrix4 device2manip;
2748 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2749 m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2751 m_undo_begun = false;
2754 SceneChangeNotify();
2757 return m_pivot_moving;
2761 if ( Mode() == eComponent ) {
2762 setSelectedAllComponents( false );
2766 setSelectedAll( false );
2770 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2771 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2772 if ( modifier == eReplace ) {
2774 setSelectedAllComponents( false );
2782 #if defined ( DEBUG_SELECTION )
2783 g_render_clipped.destroy();
2787 View scissored( view );
2788 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2790 SelectionVolume volume( scissored );
2791 SelectionPool selector;
2793 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2797 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2800 if ( !selector.failed() ) {
2803 case RadiantSelectionSystem::eToggle:
2805 SelectableSortedSet::iterator best = selector.begin();
2806 // toggle selection of the object with least depth
2807 if ( ( *best ).second->isSelected() ) {
2808 ( *best ).second->setSelected( false );
2811 ( *best ).second->setSelected( true );
2815 // if cycle mode not enabled, enable it
2816 case RadiantSelectionSystem::eReplace:
2819 ( *selector.begin() ).second->setSelected( true );
2822 // select the next object in the list from the one already selected
2823 case RadiantSelectionSystem::eCycle:
2825 SelectionPool::iterator i = selector.begin();
2826 while ( i != selector.end() )
2828 if ( ( *i ).second->isSelected() ) {
2829 ( *i ).second->setSelected( false );
2831 if ( i != selector.end() ) {
2832 i->second->setSelected( true );
2836 selector.begin()->second->setSelected( true );
2851 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2852 if ( modifier == eReplace ) {
2854 setSelectedAllComponents( false );
2862 #if defined ( DEBUG_SELECTION )
2863 g_render_clipped.destroy();
2867 View scissored( view );
2868 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
2870 SelectionVolume volume( scissored );
2873 Scene_TestSelect_Component( pool, volume, scissored, eFace );
2877 Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
2880 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
2882 ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
2888 void translate( const Vector3& translation ){
2889 if ( !nothingSelected() ) {
2890 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
2892 m_translation = translation;
2894 m_pivot2world = m_pivot2world_start;
2895 matrix4_translate_by_vec3( m_pivot2world, translation );
2897 if ( Mode() == eComponent ) {
2898 Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
2902 Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
2905 SceneChangeNotify();
2908 void outputTranslation( TextOutputStream& ostream ){
2909 ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
2911 void rotate( const Quaternion& rotation ){
2912 if ( !nothingSelected() ) {
2913 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
2915 m_rotation = rotation;
2917 if ( Mode() == eComponent ) {
2918 Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
2920 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
2924 Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
2926 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
2929 SceneChangeNotify();
2932 void outputRotation( TextOutputStream& ostream ){
2933 ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
2935 void scale( const Vector3& scaling ){
2936 if ( !nothingSelected() ) {
2939 if ( Mode() == eComponent ) {
2940 Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
2944 Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
2947 SceneChangeNotify();
2950 void outputScale( TextOutputStream& ostream ){
2951 ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
2954 void rotateSelected( const Quaternion& rotation ){
2959 void translateSelected( const Vector3& translation ){
2961 translate( translation );
2964 void scaleSelected( const Vector3& scaling ){
2970 void MoveSelected( const View& view, const float device_point[2] ){
2971 if ( m_manipulator->isSelected() ) {
2972 if ( !m_undo_begun ) {
2973 m_undo_begun = true;
2974 GlobalUndoSystem().start();
2977 Matrix4 device2manip;
2978 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2979 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1] );
2983 /// \todo Support view-dependent nudge.
2984 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
2985 if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
2986 translateSelected( nudge );
2991 void freezeTransforms();
2993 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
2994 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
2995 renderSolid( renderer, volume );
2998 const Matrix4& GetPivot2World() const {
3000 return m_pivot2world;
3003 static void constructStatic(){
3004 m_state = GlobalShaderCache().capture( "$POINT" );
3005 #if defined( DEBUG_SELECTION )
3006 g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3008 TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3009 TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3010 RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3013 static void destroyStatic(){
3014 #if defined( DEBUG_SELECTION )
3015 GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3017 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3018 GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3019 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3020 GlobalShaderCache().release( "$POINT" );
3024 Shader* RadiantSelectionSystem::m_state = 0;
3029 RadiantSelectionSystem* g_RadiantSelectionSystem;
3031 inline RadiantSelectionSystem& getSelectionSystem(){
3032 return *g_RadiantSelectionSystem;
3038 class testselect_entity_visible : public scene::Graph::Walker
3040 Selector& m_selector;
3041 SelectionTest& m_test;
3043 testselect_entity_visible( Selector& selector, SelectionTest& test )
3044 : m_selector( selector ), m_test( test ){
3046 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3047 Selectable* selectable = Instance_getSelectable( instance );
3048 if ( selectable != 0
3049 && Node_isEntity( path.top() ) ) {
3050 m_selector.pushSelectable( *selectable );
3053 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3054 if ( selectionTestable ) {
3055 selectionTestable->testSelect( m_selector, m_test );
3060 void post( const scene::Path& path, scene::Instance& instance ) const {
3061 Selectable* selectable = Instance_getSelectable( instance );
3062 if ( selectable != 0
3063 && Node_isEntity( path.top() ) ) {
3064 m_selector.popSelectable();
3069 class testselect_primitive_visible : public scene::Graph::Walker
3071 Selector& m_selector;
3072 SelectionTest& m_test;
3074 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3075 : m_selector( selector ), m_test( test ){
3077 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3078 Selectable* selectable = Instance_getSelectable( instance );
3079 if ( selectable != 0 ) {
3080 m_selector.pushSelectable( *selectable );
3083 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3084 if ( selectionTestable ) {
3085 selectionTestable->testSelect( m_selector, m_test );
3090 void post( const scene::Path& path, scene::Instance& instance ) const {
3091 Selectable* selectable = Instance_getSelectable( instance );
3092 if ( selectable != 0 ) {
3093 m_selector.popSelectable();
3098 class testselect_component_visible : public scene::Graph::Walker
3100 Selector& m_selector;
3101 SelectionTest& m_test;
3102 SelectionSystem::EComponentMode m_mode;
3104 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3105 : m_selector( selector ), m_test( test ), m_mode( mode ){
3107 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3108 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3109 if ( componentSelectionTestable ) {
3110 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3118 class testselect_component_visible_selected : public scene::Graph::Walker
3120 Selector& m_selector;
3121 SelectionTest& m_test;
3122 SelectionSystem::EComponentMode m_mode;
3124 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3125 : m_selector( selector ), m_test( test ), m_mode( mode ){
3127 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3128 Selectable* selectable = Instance_getSelectable( instance );
3129 if ( selectable != 0 && selectable->isSelected() ) {
3130 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3131 if ( componentSelectionTestable ) {
3132 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3140 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3141 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3144 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3145 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3148 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3149 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3152 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3157 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3161 Scene_TestSelect_Primitive( selector, test, view );
3164 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3169 class FreezeTransforms : public scene::Graph::Walker
3172 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3173 TransformNode* transformNode = Node_getTransformNode( path.top() );
3174 if ( transformNode != 0 ) {
3175 Transformable* transform = Instance_getTransformable( instance );
3176 if ( transform != 0 ) {
3177 transform->freezeTransform();
3184 void RadiantSelectionSystem::freezeTransforms(){
3185 GlobalSceneGraph().traverse( FreezeTransforms() );
3189 void RadiantSelectionSystem::endMove(){
3192 if ( Mode() == ePrimitive ) {
3193 if ( ManipulatorMode() == eDrag ) {
3194 Scene_SelectAll_Component( false, SelectionSystem::eFace );
3198 m_pivot_moving = false;
3201 SceneChangeNotify();
3203 if ( m_undo_begun ) {
3204 StringOutputStream command;
3206 if ( ManipulatorMode() == eTranslate ) {
3207 command << "translateTool";
3208 outputTranslation( command );
3210 else if ( ManipulatorMode() == eRotate ) {
3211 command << "rotateTool";
3212 outputRotation( command );
3214 else if ( ManipulatorMode() == eScale ) {
3215 command << "scaleTool";
3216 outputScale( command );
3218 else if ( ManipulatorMode() == eDrag ) {
3219 command << "dragTool";
3222 GlobalUndoSystem().finish( command.c_str() );
3227 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3228 Entity* entity = Node_getEntity( instance.path().top() );
3230 && ( entity->getEntityClass().fixedsize
3231 || !node_is_group( instance.path().top() ) ) ) {
3232 Editable* editable = Node_getEditable( instance.path().top() );
3233 if ( editable != 0 ) {
3234 return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3238 return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3242 return instance.worldAABB();
3245 class bounds_selected : public scene::Graph::Walker
3249 bounds_selected( AABB& bounds )
3250 : m_bounds( bounds ){
3253 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3254 Selectable* selectable = Instance_getSelectable( instance );
3255 if ( selectable != 0
3256 && selectable->isSelected() ) {
3257 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3263 class bounds_selected_component : public scene::Graph::Walker
3267 bounds_selected_component( AABB& bounds )
3268 : m_bounds( bounds ){
3271 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3272 Selectable* selectable = Instance_getSelectable( instance );
3273 if ( selectable != 0
3274 && selectable->isSelected() ) {
3275 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3276 if ( componentEditable ) {
3277 aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3284 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3285 graph.traverse( bounds_selected( bounds ) );
3288 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3289 graph.traverse( bounds_selected_component( bounds ) );
3293 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3294 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3295 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3296 && componentEditable != 0 ) {
3297 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3301 Bounded* bounded = Instance_getBounded( instance );
3302 if ( bounded != 0 ) {
3303 pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3307 pivot = g_matrix4_identity;
3313 void RadiantSelectionSystem::ConstructPivot() const {
3314 if ( !m_pivotChanged || m_pivot_moving ) {
3317 m_pivotChanged = false;
3319 Vector3 m_object_pivot;
3321 if ( !nothingSelected() ) {
3324 if ( Mode() == eComponent ) {
3325 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3329 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3331 m_object_pivot = bounds.origin;
3334 vector3_snap( m_object_pivot, GetSnapGridSize() );
3335 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3337 switch ( m_manipulator_mode )
3342 if ( Mode() == eComponent ) {
3343 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3347 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3351 if ( Mode() == eComponent ) {
3352 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3356 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3365 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3366 //if(view->TestPoint(m_object_pivot))
3367 if ( !nothingSelected() ) {
3368 renderer.Highlight( Renderer::ePrimitive, false );
3369 renderer.Highlight( Renderer::eFace, false );
3371 renderer.SetState( m_state, Renderer::eWireframeOnly );
3372 renderer.SetState( m_state, Renderer::eFullMaterials );
3374 m_manipulator->render( renderer, volume, GetPivot2World() );
3377 #if defined( DEBUG_SELECTION )
3378 renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3379 renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3380 renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3385 void SelectionSystem_OnBoundsChanged(){
3386 getSelectionSystem().pivotChanged();
3390 SignalHandlerId SelectionSystem_boundsChanged;
3392 void SelectionSystem_Construct(){
3393 RadiantSelectionSystem::constructStatic();
3395 g_RadiantSelectionSystem = new RadiantSelectionSystem;
3397 SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<SelectionSystem_OnBoundsChanged>() );
3399 GlobalShaderCache().attachRenderable( getSelectionSystem() );
3402 void SelectionSystem_Destroy(){
3403 GlobalShaderCache().detachRenderable( getSelectionSystem() );
3405 GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3407 delete g_RadiantSelectionSystem;
3409 RadiantSelectionSystem::destroyStatic();
3415 inline float screen_normalised( float pos, std::size_t size ){
3416 return ( ( 2.0f * pos ) / size ) - 1.0f;
3419 typedef Vector2 DeviceVector;
3421 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3422 return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3425 inline float device_constrained( float pos ){
3426 return std::min( 1.0f, std::max( -1.0f, pos ) );
3429 inline DeviceVector device_constrained( DeviceVector device ){
3430 return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3433 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3434 return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3437 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3438 return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3441 typedef Callback1<DeviceVector> MouseEventCallback;
3443 Single<MouseEventCallback> g_mouseMovedCallback;
3444 Single<MouseEventCallback> g_mouseUpCallback;
3447 const ButtonIdentifier c_button_select = c_buttonLeft;
3448 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3449 const ModifierFlags c_modifier_toggle = c_modifierShift;
3450 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3451 const ModifierFlags c_modifier_face = c_modifierControl;
3453 const ButtonIdentifier c_button_select = c_buttonLeft;
3454 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3455 const ModifierFlags c_modifier_toggle = c_modifierControl;
3456 const ModifierFlags c_modifier_replace = c_modifierNone;
3457 const ModifierFlags c_modifier_face = c_modifierShift;
3459 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3460 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3462 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3463 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3464 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3465 const ModifierFlags c_modifier_apply_texture3 = c_modifierShift;
3466 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3470 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3471 if ( state == c_modifier_toggle || state == c_modifier_toggle_face ) {
3472 return RadiantSelectionSystem::eToggle;
3474 if ( state == c_modifier_replace || state == c_modifier_replace_face ) {
3475 return RadiantSelectionSystem::eReplace;
3477 return RadiantSelectionSystem::eManipulator;
3480 rect_t getDeviceArea() const {
3481 DeviceVector delta( m_current - m_start );
3482 if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3483 return SelectionBoxForArea( &m_start[0], &delta[0] );
3487 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3488 return default_area;
3493 DeviceVector m_start;
3494 DeviceVector m_current;
3495 DeviceVector m_epsilon;
3496 std::size_t m_unmoved_replaces;
3497 ModifierFlags m_state;
3499 RectangleCallback m_window_update;
3501 Selector_() : m_start( 0.0f, 0.0f ), m_current( 0.0f, 0.0f ), m_unmoved_replaces( 0 ), m_state( c_modifierNone ){
3505 m_window_update( getDeviceArea() );
3508 void testSelect( DeviceVector position ){
3509 RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3510 if ( modifier != RadiantSelectionSystem::eManipulator ) {
3511 DeviceVector delta( position - m_start );
3512 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3513 DeviceVector delta( position - m_start );
3514 getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3518 if ( modifier == RadiantSelectionSystem::eReplace && m_unmoved_replaces++ > 0 ) {
3519 modifier = RadiantSelectionSystem::eCycle;
3521 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3525 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3529 bool selecting() const {
3530 return m_state != c_modifier_manipulator;
3533 void setState( ModifierFlags state ){
3534 bool was_selecting = selecting();
3536 if ( was_selecting ^ selecting() ) {
3541 ModifierFlags getState() const {
3545 void modifierEnable( ModifierFlags type ){
3546 setState( bitfield_enable( getState(), type ) );
3548 void modifierDisable( ModifierFlags type ){
3549 setState( bitfield_disable( getState(), type ) );
3552 void mouseDown( DeviceVector position ){
3553 m_start = m_current = device_constrained( position );
3556 void mouseMoved( DeviceVector position ){
3557 m_current = device_constrained( position );
3560 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseMoved> MouseMovedCaller;
3562 void mouseUp( DeviceVector position ){
3563 testSelect( device_constrained( position ) );
3565 g_mouseMovedCallback.clear();
3566 g_mouseUpCallback.clear();
3568 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseUp> MouseUpCaller;
3575 DeviceVector m_epsilon;
3578 bool mouseDown( DeviceVector position ){
3579 return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
3582 void mouseMoved( DeviceVector position ){
3583 getSelectionSystem().MoveSelected( *m_view, &position[0] );
3585 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseMoved> MouseMovedCaller;
3587 void mouseUp( DeviceVector position ){
3588 getSelectionSystem().endMove();
3589 g_mouseMovedCallback.clear();
3590 g_mouseUpCallback.clear();
3592 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseUp> MouseUpCaller;
3595 void Scene_copyClosestTexture( SelectionTest& test );
3596 void Scene_applyClosestTexture( SelectionTest& test );
3598 class RadiantWindowObserver : public SelectionSystemWindowObserver
3611 Selector_ m_selector;
3612 Manipulator_ m_manipulator;
3614 RadiantWindowObserver() : m_mouse_down( false ){
3619 void setView( const View& view ){
3620 m_selector.m_view = &view;
3621 m_manipulator.m_view = &view;
3623 void setRectangleDrawCallback( const RectangleCallback& callback ){
3624 m_selector.m_window_update = callback;
3626 void onSizeChanged( int width, int height ){
3629 DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
3630 m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
3632 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3633 if ( button == c_button_select ) {
3634 m_mouse_down = true;
3636 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
3637 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
3638 g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
3639 g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
3643 m_selector.mouseDown( devicePosition );
3644 g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
3645 g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
3648 else if ( button == c_button_texture ) {
3649 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
3651 View scissored( *m_selector.m_view );
3652 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
3653 SelectionVolume volume( scissored );
3655 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
3656 Scene_applyClosestTexture( volume );
3658 else if ( modifiers == c_modifier_copy_texture ) {
3659 Scene_copyClosestTexture( volume );
3663 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
3664 m_selector.m_unmoved_replaces = 0;
3666 if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
3667 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3670 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3671 if ( button == c_button_select && !g_mouseUpCallback.empty() ) {
3672 m_mouse_down = false;
3674 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3677 void onModifierDown( ModifierFlags type ){
3678 m_selector.modifierEnable( type );
3680 void onModifierUp( ModifierFlags type ){
3681 m_selector.modifierDisable( type );
3687 SelectionSystemWindowObserver* NewWindowObserver(){
3688 return new RadiantWindowObserver;
3693 #include "modulesystem/singletonmodule.h"
3694 #include "modulesystem/moduleregistry.h"
3696 class SelectionDependencies :
3697 public GlobalSceneGraphModuleRef,
3698 public GlobalShaderCacheModuleRef,
3699 public GlobalOpenGLModuleRef
3703 class SelectionAPI : public TypeSystemRef
3705 SelectionSystem* m_selection;
3707 typedef SelectionSystem Type;
3708 STRING_CONSTANT( Name, "*" );
3711 SelectionSystem_Construct();
3713 m_selection = &getSelectionSystem();
3716 SelectionSystem_Destroy();
3718 SelectionSystem* getTable(){
3723 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
3724 typedef Static<SelectionModule> StaticSelectionModule;
3725 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );