]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/selection.cpp
Q3map2:
[xonotic/netradiant.git] / radiant / selection.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "selection.h"
23
24 #include "debugging/debugging.h"
25
26 #include <map>
27 #include <list>
28 #include <set>
29
30 #include "windowobserver.h"
31 #include "iundo.h"
32 #include "ientity.h"
33 #include "cullable.h"
34 #include "renderable.h"
35 #include "selectable.h"
36 #include "editable.h"
37
38 #include "math/frustum.h"
39 #include "signal/signal.h"
40 #include "generic/object.h"
41 #include "selectionlib.h"
42 #include "render.h"
43 #include "view.h"
44 #include "renderer.h"
45 #include "stream/stringstream.h"
46 #include "eclasslib.h"
47 #include "generic/bitfield.h"
48 #include "generic/static.h"
49 #include "pivot.h"
50 #include "stringio.h"
51 #include "container/container.h"
52
53 #include "grid.h"
54
55 TextOutputStream& ostream_write( TextOutputStream& t, const Vector4& v ){
56         return t << "[ " << v.x() << " " << v.y() << " " << v.z() << " " << v.w() << " ]";
57 }
58
59 TextOutputStream& ostream_write( TextOutputStream& t, const Matrix4& m ){
60         return t << "[ " << m.x() << " " << m.y() << " " << m.z() << " " << m.t() << " ]";
61 }
62
63 struct Pivot2World
64 {
65         Matrix4 m_worldSpace;
66         Matrix4 m_viewpointSpace;
67         Matrix4 m_viewplaneSpace;
68         Vector3 m_axis_screen;
69
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 );
74         }
75 };
76
77
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 ) ) );
81 }
82
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 );
86
87         // point at x, y, zFar
88         point_for_device_point( ray.direction, device2object, x, y, 1 );
89
90         // construct ray
91         vector3_subtract( ray.direction, ray.origin );
92         vector3_normalise( ray.direction );
93 }
94
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 );
99
100         if ( d > 0 ) {
101                 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a - sqrt( d ) ) );
102                 return true;
103         }
104         else
105         {
106                 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a ) );
107                 return false;
108         }
109 }
110
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
119
120         if ( D < 0.000001 ) {
121                 // the lines are almost parallel
122                 intersection = vector3_added( other.origin, vector3_scaled( other.direction, e ) );
123         }
124         else
125         {
126                 intersection = vector3_added( other.origin, vector3_scaled( other.direction, ( e - dot * d ) / D ) );
127         }
128 }
129
130 const Vector3 g_origin( 0, 0, 0 );
131 const float g_radius = 64;
132
133 void point_on_sphere( Vector3& point, const Matrix4& device2object, const float x, const float y ){
134         Ray ray;
135         ray_for_device_point( ray, device2object, x, y );
136         sphere_intersect_ray( g_origin, g_radius, ray, point );
137 }
138
139 void point_on_axis( Vector3& point, const Vector3& axis, const Matrix4& device2object, const float x, const float y ){
140         Ray ray;
141         ray_for_device_point( ray, device2object, x, y );
142         ray_intersect_ray( ray, Ray( Vector3( 0, 0, 0 ), axis ), point );
143 }
144
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 ) ) );
148 }
149
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 ) )
155                                                                    ) );
156 }
157
158
159 #if defined( _DEBUG )
160 class test_quat
161 {
162 public:
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 ) ) );
166 }
167 private:
168 };
169
170 static test_quat bleh( g_vector3_axis_x, g_vector3_axis_y );
171 #endif
172
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 ) ) ) );
176 }
177
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 );
182         }
183         else{
184                 return -angle_between( a, b );
185         }
186 }
187
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 ) );
190 }
191
192 class Manipulatable
193 {
194 public:
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;
197 };
198
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 )
203                 );
204 }
205
206 class Rotatable
207 {
208 public:
209 virtual void rotate( const Quaternion& rotation ) = 0;
210 };
211
212 class RotateFree : public Manipulatable
213 {
214 Vector3 m_start;
215 Rotatable& m_rotatable;
216 public:
217 RotateFree( Rotatable& rotatable )
218         : m_rotatable( rotatable ){
219 }
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 );
223 }
224 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
225         Vector3 current;
226
227         point_on_sphere( current, device2manip, x, y );
228         vector3_normalise( current );
229
230         m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
231 }
232 };
233
234 class RotateAxis : public Manipulatable
235 {
236 Vector3 m_axis;
237 Vector3 m_start;
238 Rotatable& m_rotatable;
239 public:
240 RotateAxis( Rotatable& rotatable )
241         : m_rotatable( rotatable ){
242 }
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 );
246 }
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 ){
249         Vector3 current;
250         point_on_sphere( current, device2manip, x, y );
251         constrain_to_axis( current, m_axis );
252
253         m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
254 }
255
256 void SetAxis( const Vector3& axis ){
257         m_axis = axis;
258 }
259 };
260
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 )
266                         )
267                 );
268 }
269
270 class Translatable
271 {
272 public:
273 virtual void translate( const Vector3& translation ) = 0;
274 };
275
276 class TranslateAxis : public Manipulatable
277 {
278 Vector3 m_start;
279 Vector3 m_axis;
280 Translatable& m_translatable;
281 public:
282 TranslateAxis( Translatable& translatable )
283         : m_translatable( translatable ){
284 }
285 void Construct( const Matrix4& device2manip, const float x, const float y ){
286         point_on_axis( m_start, m_axis, device2manip, x, y );
287 }
288 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
289         Vector3 current;
290         point_on_axis( current, m_axis, device2manip, x, y );
291         current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
292
293         translation_local2object( current, current, manip2object );
294         vector3_snap( current, GetSnapGridSize() );
295
296         m_translatable.translate( current );
297 }
298
299 void SetAxis( const Vector3& axis ){
300         m_axis = axis;
301 }
302 };
303
304 class TranslateFree : public Manipulatable
305 {
306 private:
307 Vector3 m_start;
308 Translatable& m_translatable;
309 public:
310 TranslateFree( Translatable& translatable )
311         : m_translatable( translatable ){
312 }
313 void Construct( const Matrix4& device2manip, const float x, const float y ){
314         point_on_plane( m_start, device2manip, x, y );
315 }
316 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
317         Vector3 current;
318         point_on_plane( current, device2manip, x, y );
319         current = vector3_subtracted( current, m_start );
320
321         translation_local2object( current, current, manip2object );
322         vector3_snap( current, GetSnapGridSize() );
323
324         m_translatable.translate( current );
325 }
326 };
327
328 void GetSelectionAABB( AABB& bounds );
329 const Matrix4& ssGetPivot2World();
330
331 class Scalable
332 {
333 public:
334 virtual void scale( const Vector3& scaling ) = 0;
335 };
336
337
338 class ScaleAxis : public Manipulatable
339 {
340 private:
341 Vector3 m_start;
342 Vector3 m_axis;
343 Scalable& m_scalable;
344
345 AABB m_aabb;
346 Vector3 m_transform_origin;
347 Vector3 m_choosen_extent;
348
349 public:
350 ScaleAxis( Scalable& scalable )
351         : m_scalable( scalable ){
352 }
353 void Construct( const Matrix4& device2manip, const float x, const float y ){
354         point_on_axis( m_start, m_axis, device2manip, x, y );
355
356         GetSelectionAABB( m_aabb );
357         m_transform_origin = vector4_to_vector3( ssGetPivot2World().t() );
358         m_choosen_extent = Vector3( std::max( m_aabb.origin[0] + m_aabb.extents[0] - m_transform_origin[0], - m_aabb.origin[0] + m_aabb.extents[0] + m_transform_origin[0] ),
359                                         std::max( m_aabb.origin[1] + m_aabb.extents[1] - m_transform_origin[1], - m_aabb.origin[1] + m_aabb.extents[1] + m_transform_origin[1] ),
360                                         std::max( m_aabb.origin[2] + m_aabb.extents[2] - m_transform_origin[2], - m_aabb.origin[2] + m_aabb.extents[2] + m_transform_origin[2] )
361                                                         );
362
363 }
364 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
365         //globalOutputStream() << "manip2object: " << manip2object << "  device2manip: " << device2manip << "  x: " << x << "  y:" << y <<"\n";
366         Vector3 current;
367         point_on_axis( current, m_axis, device2manip, x, y );
368         Vector3 delta = vector3_subtracted( current, m_start );
369
370         translation_local2object( delta, delta, manip2object );
371         vector3_snap( delta, GetSnapGridSize() );
372
373         Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
374         //globalOutputStream() << "start: " << start << "   delta: " << delta <<"\n";
375         Vector3 scale(
376                 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
377                 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
378                 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
379                 );
380
381         for( std::size_t i = 0; i < 3; i++ ){
382                 if( m_choosen_extent[i] > 0.0625 ){ //epsilon to prevent too high scale for set of models, having really small extent, formed by origins
383                         scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
384                 }
385         }
386
387         m_scalable.scale( scale );
388 }
389
390 void SetAxis( const Vector3& axis ){
391         m_axis = axis;
392 }
393 };
394
395 class ScaleFree : public Manipulatable
396 {
397 private:
398 Vector3 m_start;
399 Scalable& m_scalable;
400
401 AABB m_aabb;
402 Vector3 m_transform_origin;
403 Vector3 m_choosen_extent;
404
405 public:
406 ScaleFree( Scalable& scalable )
407         : m_scalable( scalable ){
408 }
409 void Construct( const Matrix4& device2manip, const float x, const float y ){
410         point_on_plane( m_start, device2manip, x, y );
411
412         GetSelectionAABB( m_aabb );
413         m_transform_origin = vector4_to_vector3( ssGetPivot2World().t() );
414         m_choosen_extent = Vector3( std::max( m_aabb.origin[0] + m_aabb.extents[0] - m_transform_origin[0], - m_aabb.origin[0] + m_aabb.extents[0] + m_transform_origin[0] ),
415                                         std::max( m_aabb.origin[1] + m_aabb.extents[1] - m_transform_origin[1], - m_aabb.origin[1] + m_aabb.extents[1] + m_transform_origin[1] ),
416                                         std::max( m_aabb.origin[2] + m_aabb.extents[2] - m_transform_origin[2], - m_aabb.origin[2] + m_aabb.extents[2] + m_transform_origin[2] )
417                                                         );
418 }
419 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
420         Vector3 current;
421         point_on_plane( current, device2manip, x, y );
422         Vector3 delta = vector3_subtracted( current, m_start );
423
424         translation_local2object( delta, delta, manip2object );
425         vector3_snap( delta, GetSnapGridSize() );
426
427         Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
428         Vector3 scale(
429                 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
430                 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
431                 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
432                 );
433
434         for( std::size_t i = 0; i < 3; i++ ){
435                 if( m_choosen_extent[i] > 0.0625 ){
436                         scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
437                 }
438         }
439
440         m_scalable.scale( scale );
441 }
442 };
443
444
445
446
447
448
449
450
451
452
453 class RenderableClippedPrimitive : public OpenGLRenderable
454 {
455 struct primitive_t
456 {
457         PointVertex m_points[9];
458         std::size_t m_count;
459 };
460 Matrix4 m_inverse;
461 std::vector<primitive_t> m_primitives;
462 public:
463 Matrix4 m_world;
464
465 void render( RenderStateFlags state ) const {
466         for ( std::size_t i = 0; i < m_primitives.size(); ++i )
467         {
468                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
469                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
470                 switch ( m_primitives[i].m_count )
471                 {
472                 case 1: break;
473                 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
474                 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
475                 }
476         }
477 }
478
479 void construct( const Matrix4& world2device ){
480         m_inverse = matrix4_full_inverse( world2device );
481         m_world = g_matrix4_identity;
482 }
483
484 void insert( const Vector4 clipped[9], std::size_t count ){
485         add_one();
486
487         m_primitives.back().m_count = count;
488         for ( std::size_t i = 0; i < count; ++i )
489         {
490                 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
491                 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
492         }
493 }
494
495 void destroy(){
496         m_primitives.clear();
497 }
498 private:
499 void add_one(){
500         m_primitives.push_back( primitive_t() );
501
502         const Colour4b colour_clipped( 255, 127, 0, 255 );
503
504         for ( std::size_t i = 0; i < 9; ++i )
505                 m_primitives.back().m_points[i].colour = colour_clipped;
506 }
507 };
508
509 #if defined( _DEBUG )
510 #define DEBUG_SELECTION
511 #endif
512
513 #if defined( DEBUG_SELECTION )
514 Shader* g_state_clipped;
515 RenderableClippedPrimitive g_render_clipped;
516 #endif
517
518
519 #if 0
520 // dist_Point_to_Line(): get the distance of a point to a line.
521 //    Input:  a Point P and a Line L (in any dimension)
522 //    Return: the shortest distance from P to L
523 float
524 dist_Point_to_Line( Point P, Line L ){
525         Vector v = L.P1 - L.P0;
526         Vector w = P - L.P0;
527
528         double c1 = dot( w,v );
529         double c2 = dot( v,v );
530         double b = c1 / c2;
531
532         Point Pb = L.P0 + b * v;
533         return d( P, Pb );
534 }
535 #endif
536
537 class Segment3D
538 {
539 typedef Vector3 point_type;
540 public:
541 Segment3D( const point_type& _p0, const point_type& _p1 )
542         : p0( _p0 ), p1( _p1 ){
543 }
544
545 point_type p0, p1;
546 };
547
548 typedef Vector3 Point3D;
549
550 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
551         return vector3_length_squared( b - a );
552 }
553
554 // get the distance of a point to a segment.
555 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
556         Vector3 v = segment.p1 - segment.p0;
557         Vector3 w = point - segment.p0;
558
559         double c1 = vector3_dot( w,v );
560         if ( c1 <= 0 ) {
561                 return segment.p0;
562         }
563
564         double c2 = vector3_dot( v,v );
565         if ( c2 <= c1 ) {
566                 return segment.p1;
567         }
568
569         return Point3D( segment.p0 + v * ( c1 / c2 ) );
570 }
571
572 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
573         return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
574 }
575
576 typedef Vector3 point_t;
577 typedef const Vector3* point_iterator_t;
578
579 // crossing number test for a point in a polygon
580 // This code is patterned after [Franklin, 2000]
581 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
582         std::size_t crossings = 0;
583
584         // loop through all edges of the polygon
585         for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
586         {  // edge from (*prev) to (*cur)
587                 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
588                          || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
589                                                                                       // compute the actual edge-ray intersect x-coordinate
590                         float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
591                         if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
592                                 ++crossings; // a valid crossing of y=P[1] right of P[0]
593                         }
594                 }
595         }
596         return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
597 }
598
599 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
600         return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
601 }
602
603 enum clipcull_t
604 {
605         eClipCullNone,
606         eClipCullCW,
607         eClipCullCCW,
608 };
609
610
611 inline SelectionIntersection select_point_from_clipped( Vector4& clipped ){
612         return SelectionIntersection( clipped[2] / clipped[3], static_cast<float>( vector3_length_squared( Vector3( clipped[0] / clipped[3], clipped[1] / clipped[3], 0 ) ) ) );
613 }
614
615 void BestPoint( std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull ){
616         Vector3 normalised[9];
617
618         {
619                 for ( std::size_t i = 0; i < count; ++i )
620                 {
621                         normalised[i][0] = clipped[i][0] / clipped[i][3];
622                         normalised[i][1] = clipped[i][1] / clipped[i][3];
623                         normalised[i][2] = clipped[i][2] / clipped[i][3];
624                 }
625         }
626
627         if ( cull != eClipCullNone && count > 2 ) {
628                 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
629
630                 if ( ( cull == eClipCullCW && signed_area > 0 )
631                          || ( cull == eClipCullCCW && signed_area < 0 ) ) {
632                         return;
633                 }
634         }
635
636         if ( count == 2 ) {
637                 Segment3D segment( normalised[0], normalised[1] );
638                 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
639                 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
640         }
641         else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
642                 point_iterator_t end = normalised + count;
643                 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
644                 {
645                         Segment3D segment( *previous, *current );
646                         Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
647                         float depth = point.z();
648                         point.z() = 0;
649                         float distance = static_cast<float>( vector3_length_squared( point ) );
650
651                         assign_if_closer( best, SelectionIntersection( depth, distance ) );
652                 }
653         }
654         else if ( count > 2 ) {
655                 assign_if_closer(
656                         best,
657                         SelectionIntersection(
658                                 static_cast<float>( ray_distance_to_plane(
659                                                                                 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
660                                                                                 plane3_for_points( normalised[0], normalised[1], normalised[2] )
661                                                                                 ) ),
662                                 0
663                                 )
664                         );
665         }
666
667 #if defined( DEBUG_SELECTION )
668         if ( count >= 2 ) {
669                 g_render_clipped.insert( clipped, count );
670         }
671 #endif
672 }
673
674 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
675         Vector4 clipped[2];
676         for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
677         {
678                 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
679                 BestPoint( count, clipped, best, eClipCullNone );
680         }
681 }
682
683 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
684         Vector4 clipped[2];
685         for ( std::size_t i = 0; i < size; ++i )
686         {
687                 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
688                 BestPoint( count, clipped, best, eClipCullNone );
689         }
690 }
691
692 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
693         Vector4 clipped[2];
694         const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
695         BestPoint( count, clipped, best, eClipCullNone );
696 }
697
698 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
699         Vector4 clipped[9];
700         for ( std::size_t i = 0; i < size; ++i )
701         {
702                 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 );
703                 BestPoint( count, clipped, best, cull );
704         }
705 }
706
707 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
708         Vector4 clipped[9];
709         {
710                 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 );
711                 BestPoint( count, clipped, best, cull );
712         }
713         {
714                 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 );
715                 BestPoint( count, clipped, best, cull );
716         }
717 }
718
719 struct FlatShadedVertex
720 {
721         Vertex3f vertex;
722         Colour4b colour;
723         Normal3f normal;
724
725         FlatShadedVertex(){
726         }
727 };
728
729
730 typedef FlatShadedVertex* FlatShadedVertexIterator;
731 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
732         for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
733         {
734                 Vector4 clipped[9];
735                 BestPoint(
736                         matrix4_clip_triangle(
737                                 local2view,
738                                 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
739                                 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
740                                 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
741                                 clipped
742                                 ),
743                         clipped,
744                         best,
745                         cull
746                         );
747         }
748 }
749
750
751 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
752
753 class SelectionPool : public Selector
754 {
755 SelectableSortedSet m_pool;
756 SelectionIntersection m_intersection;
757 Selectable* m_selectable;
758
759 public:
760 void pushSelectable( Selectable& selectable ){
761         m_intersection = SelectionIntersection();
762         m_selectable = &selectable;
763 }
764 void popSelectable(){
765         addSelectable( m_intersection, m_selectable );
766         m_intersection = SelectionIntersection();
767 }
768 void addIntersection( const SelectionIntersection& intersection ){
769         assign_if_closer( m_intersection, intersection );
770 }
771 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
772         if ( intersection.valid() ) {
773                 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
774         }
775 }
776
777 typedef SelectableSortedSet::iterator iterator;
778
779 iterator begin(){
780         return m_pool.begin();
781 }
782 iterator end(){
783         return m_pool.end();
784 }
785
786 bool failed(){
787         return m_pool.empty();
788 }
789 };
790
791
792 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
793 const Colour4b g_colour_screen( 0, 255, 255, 255 );
794 const Colour4b g_colour_selected( 255, 255, 0, 255 );
795
796 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
797         return ( selected ) ? g_colour_selected : colour;
798 }
799
800 template<typename remap_policy>
801 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
802         const double increment = c_pi / double(segments << 2);
803
804         std::size_t count = 0;
805         float x = radius;
806         float y = 0;
807         remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
808         while ( count < segments )
809         {
810                 PointVertex* i = vertices + count;
811                 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
812
813                 PointVertex* k = i + ( segments << 1 );
814                 PointVertex* l = j + ( segments << 1 );
815
816 #if 0
817                 PointVertex* m = i + ( segments << 2 );
818                 PointVertex* n = j + ( segments << 2 );
819                 PointVertex* o = k + ( segments << 2 );
820                 PointVertex* p = l + ( segments << 2 );
821 #endif
822
823                 remap_policy::set( i->vertex, x,-y, 0 );
824                 remap_policy::set( k->vertex,-y,-x, 0 );
825 #if 0
826                 remap_policy::set( m->vertex,-x, y, 0 );
827                 remap_policy::set( o->vertex, y, x, 0 );
828 #endif
829
830                 ++count;
831
832                 {
833                         const double theta = increment * count;
834                         x = static_cast<float>( radius * cos( theta ) );
835                         y = static_cast<float>( radius * sin( theta ) );
836                 }
837
838                 remap_policy::set( j->vertex, y,-x, 0 );
839                 remap_policy::set( l->vertex,-x,-y, 0 );
840 #if 0
841                 remap_policy::set( n->vertex,-y, x, 0 );
842                 remap_policy::set( p->vertex, x, y, 0 );
843 #endif
844         }
845 }
846
847 class Manipulator
848 {
849 public:
850 virtual Manipulatable* GetManipulatable() = 0;
851 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
852 }
853 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
854 }
855 virtual void setSelected( bool select ) = 0;
856 virtual bool isSelected() const = 0;
857 };
858
859
860 inline Vector3 normalised_safe( const Vector3& self ){
861         if ( vector3_equal( self, g_vector3_identity ) ) {
862                 return g_vector3_identity;
863         }
864         return vector3_normalised( self );
865 }
866
867
868 class RotateManipulator : public Manipulator
869 {
870 struct RenderableCircle : public OpenGLRenderable
871 {
872         Array<PointVertex> m_vertices;
873
874         RenderableCircle( std::size_t size ) : m_vertices( size ){
875         }
876         void render( RenderStateFlags state ) const {
877                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
878                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
879                 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
880         }
881         void setColour( const Colour4b& colour ){
882                 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
883                 {
884                         ( *i ).colour = colour;
885                 }
886         }
887 };
888
889 struct RenderableSemiCircle : public OpenGLRenderable
890 {
891         Array<PointVertex> m_vertices;
892
893         RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
894         }
895         void render( RenderStateFlags state ) const {
896                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
897                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
898                 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
899         }
900         void setColour( const Colour4b& colour ){
901                 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
902                 {
903                         ( *i ).colour = colour;
904                 }
905         }
906 };
907
908 RotateFree m_free;
909 RotateAxis m_axis;
910 Vector3 m_axis_screen;
911 RenderableSemiCircle m_circle_x;
912 RenderableSemiCircle m_circle_y;
913 RenderableSemiCircle m_circle_z;
914 RenderableCircle m_circle_screen;
915 RenderableCircle m_circle_sphere;
916 SelectableBool m_selectable_x;
917 SelectableBool m_selectable_y;
918 SelectableBool m_selectable_z;
919 SelectableBool m_selectable_screen;
920 SelectableBool m_selectable_sphere;
921 Pivot2World m_pivot;
922 Matrix4 m_local2world_x;
923 Matrix4 m_local2world_y;
924 Matrix4 m_local2world_z;
925 bool m_circle_x_visible;
926 bool m_circle_y_visible;
927 bool m_circle_z_visible;
928 public:
929 static Shader* m_state_outer;
930
931 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
932         m_free( rotatable ),
933         m_axis( rotatable ),
934         m_circle_x( ( segments << 2 ) + 1 ),
935         m_circle_y( ( segments << 2 ) + 1 ),
936         m_circle_z( ( segments << 2 ) + 1 ),
937         m_circle_screen( segments << 3 ),
938         m_circle_sphere( segments << 3 ){
939         draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
940         draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
941         draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
942
943         draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
944         draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
945 }
946
947
948 void UpdateColours(){
949         m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
950         m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
951         m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
952         m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
953         m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
954 }
955
956 void updateCircleTransforms(){
957         Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
958
959         m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
960         if ( m_circle_x_visible ) {
961                 m_local2world_x = g_matrix4_identity;
962                 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
963                         vector3_cross( g_vector3_axis_x, localViewpoint )
964                         );
965                 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
966                         vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
967                         );
968                 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
969         }
970
971         m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
972         if ( m_circle_y_visible ) {
973                 m_local2world_y = g_matrix4_identity;
974                 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
975                         vector3_cross( g_vector3_axis_y, localViewpoint )
976                         );
977                 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
978                         vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
979                         );
980                 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
981         }
982
983         m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
984         if ( m_circle_z_visible ) {
985                 m_local2world_z = g_matrix4_identity;
986                 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
987                         vector3_cross( g_vector3_axis_z, localViewpoint )
988                         );
989                 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
990                         vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
991                         );
992                 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
993         }
994 }
995
996 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
997         m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
998         updateCircleTransforms();
999
1000         // temp hack
1001         UpdateColours();
1002
1003         renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
1004         renderer.SetState( m_state_outer, Renderer::eFullMaterials );
1005
1006         renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
1007         renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
1008
1009         if ( m_circle_x_visible ) {
1010                 renderer.addRenderable( m_circle_x, m_local2world_x );
1011         }
1012         if ( m_circle_y_visible ) {
1013                 renderer.addRenderable( m_circle_y, m_local2world_y );
1014         }
1015         if ( m_circle_z_visible ) {
1016                 renderer.addRenderable( m_circle_z, m_local2world_z );
1017         }
1018 }
1019 void testSelect( const View& view, const Matrix4& pivot2world ){
1020         m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1021         updateCircleTransforms();
1022
1023         SelectionPool selector;
1024
1025         {
1026                 {
1027                         Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
1028
1029 #if defined( DEBUG_SELECTION )
1030                         g_render_clipped.construct( view.GetViewMatrix() );
1031 #endif
1032
1033                         SelectionIntersection best;
1034                         LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
1035                         selector.addSelectable( best, &m_selectable_x );
1036                 }
1037
1038                 {
1039                         Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
1040
1041 #if defined( DEBUG_SELECTION )
1042                         g_render_clipped.construct( view.GetViewMatrix() );
1043 #endif
1044
1045                         SelectionIntersection best;
1046                         LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1047                         selector.addSelectable( best, &m_selectable_y );
1048                 }
1049
1050                 {
1051                         Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1052
1053 #if defined( DEBUG_SELECTION )
1054                         g_render_clipped.construct( view.GetViewMatrix() );
1055 #endif
1056
1057                         SelectionIntersection best;
1058                         LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1059                         selector.addSelectable( best, &m_selectable_z );
1060                 }
1061         }
1062
1063         {
1064                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1065
1066                 {
1067                         SelectionIntersection best;
1068                         LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1069                         selector.addSelectable( best, &m_selectable_screen );
1070                 }
1071
1072                 {
1073                         SelectionIntersection best;
1074                         Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1075                         selector.addSelectable( best, &m_selectable_sphere );
1076                 }
1077         }
1078
1079         m_axis_screen = m_pivot.m_axis_screen;
1080
1081         if ( !selector.failed() ) {
1082                 ( *selector.begin() ).second->setSelected( true );
1083         }
1084 }
1085
1086 Manipulatable* GetManipulatable(){
1087         if ( m_selectable_x.isSelected() ) {
1088                 m_axis.SetAxis( g_vector3_axis_x );
1089                 return &m_axis;
1090         }
1091         else if ( m_selectable_y.isSelected() ) {
1092                 m_axis.SetAxis( g_vector3_axis_y );
1093                 return &m_axis;
1094         }
1095         else if ( m_selectable_z.isSelected() ) {
1096                 m_axis.SetAxis( g_vector3_axis_z );
1097                 return &m_axis;
1098         }
1099         else if ( m_selectable_screen.isSelected() ) {
1100                 m_axis.SetAxis( m_axis_screen );
1101                 return &m_axis;
1102         }
1103         else{
1104                 return &m_free;
1105         }
1106 }
1107
1108 void setSelected( bool select ){
1109         m_selectable_x.setSelected( select );
1110         m_selectable_y.setSelected( select );
1111         m_selectable_z.setSelected( select );
1112         m_selectable_screen.setSelected( select );
1113 }
1114 bool isSelected() const {
1115         return m_selectable_x.isSelected()
1116                    | m_selectable_y.isSelected()
1117                    | m_selectable_z.isSelected()
1118                    | m_selectable_screen.isSelected()
1119                    | m_selectable_sphere.isSelected();
1120 }
1121 };
1122
1123 Shader* RotateManipulator::m_state_outer;
1124
1125
1126 const float arrowhead_length = 16;
1127 const float arrowhead_radius = 4;
1128
1129 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1130         ( *line++ ).vertex = vertex3f_identity;
1131         ( *line ).vertex = vertex3f_identity;
1132         vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1133 }
1134
1135 template<typename VertexRemap, typename NormalRemap>
1136 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1137         std::size_t head_tris = ( segments << 3 );
1138         const double head_segment = c_2pi / head_tris;
1139         for ( std::size_t i = 0; i < head_tris; ++i )
1140         {
1141                 {
1142                         FlatShadedVertex& point = vertices[i * 6 + 0];
1143                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1144                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1145                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1146                         NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1147                         NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1148                         NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1149                 }
1150                 {
1151                         FlatShadedVertex& point = vertices[i * 6 + 1];
1152                         VertexRemap::x( point.vertex ) = length;
1153                         VertexRemap::y( point.vertex ) = 0;
1154                         VertexRemap::z( point.vertex ) = 0;
1155                         NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1156                         NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1157                         NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1158                 }
1159                 {
1160                         FlatShadedVertex& point = vertices[i * 6 + 2];
1161                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1162                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1163                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1164                         NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1165                         NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1166                         NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1167                 }
1168
1169                 {
1170                         FlatShadedVertex& point = vertices[i * 6 + 3];
1171                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1172                         VertexRemap::y( point.vertex ) = 0;
1173                         VertexRemap::z( point.vertex ) = 0;
1174                         NormalRemap::x( point.normal ) = -1;
1175                         NormalRemap::y( point.normal ) = 0;
1176                         NormalRemap::z( point.normal ) = 0;
1177                 }
1178                 {
1179                         FlatShadedVertex& point = vertices[i * 6 + 4];
1180                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1181                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1182                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1183                         NormalRemap::x( point.normal ) = -1;
1184                         NormalRemap::y( point.normal ) = 0;
1185                         NormalRemap::z( point.normal ) = 0;
1186                 }
1187                 {
1188                         FlatShadedVertex& point = vertices[i * 6 + 5];
1189                         VertexRemap::x( point.vertex ) = length - arrowhead_length;
1190                         VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1191                         VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1192                         NormalRemap::x( point.normal ) = -1;
1193                         NormalRemap::y( point.normal ) = 0;
1194                         NormalRemap::z( point.normal ) = 0;
1195                 }
1196         }
1197 }
1198
1199 template<typename Triple>
1200 class TripleRemapXYZ
1201 {
1202 public:
1203 static float& x( Triple& triple ){
1204         return triple.x();
1205 }
1206 static float& y( Triple& triple ){
1207         return triple.y();
1208 }
1209 static float& z( Triple& triple ){
1210         return triple.z();
1211 }
1212 };
1213
1214 template<typename Triple>
1215 class TripleRemapYZX
1216 {
1217 public:
1218 static float& x( Triple& triple ){
1219         return triple.y();
1220 }
1221 static float& y( Triple& triple ){
1222         return triple.z();
1223 }
1224 static float& z( Triple& triple ){
1225         return triple.x();
1226 }
1227 };
1228
1229 template<typename Triple>
1230 class TripleRemapZXY
1231 {
1232 public:
1233 static float& x( Triple& triple ){
1234         return triple.z();
1235 }
1236 static float& y( Triple& triple ){
1237         return triple.x();
1238 }
1239 static float& z( Triple& triple ){
1240         return triple.y();
1241 }
1242 };
1243
1244 void vector3_print( const Vector3& v ){
1245         globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1246 }
1247
1248 class TranslateManipulator : public Manipulator
1249 {
1250 struct RenderableArrowLine : public OpenGLRenderable
1251 {
1252         PointVertex m_line[2];
1253
1254         RenderableArrowLine(){
1255         }
1256         void render( RenderStateFlags state ) const {
1257                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1258                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1259                 glDrawArrays( GL_LINES, 0, 2 );
1260         }
1261         void setColour( const Colour4b& colour ){
1262                 m_line[0].colour = colour;
1263                 m_line[1].colour = colour;
1264         }
1265 };
1266 struct RenderableArrowHead : public OpenGLRenderable
1267 {
1268         Array<FlatShadedVertex> m_vertices;
1269
1270         RenderableArrowHead( std::size_t size )
1271                 : m_vertices( size ){
1272         }
1273         void render( RenderStateFlags state ) const {
1274                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1275                 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1276                 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1277                 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1278         }
1279         void setColour( const Colour4b& colour ){
1280                 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1281                 {
1282                         ( *i ).colour = colour;
1283                 }
1284         }
1285 };
1286 struct RenderableQuad : public OpenGLRenderable
1287 {
1288         PointVertex m_quad[4];
1289         void render( RenderStateFlags state ) const {
1290                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1291                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1292                 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1293         }
1294         void setColour( const Colour4b& colour ){
1295                 m_quad[0].colour = colour;
1296                 m_quad[1].colour = colour;
1297                 m_quad[2].colour = colour;
1298                 m_quad[3].colour = colour;
1299         }
1300 };
1301
1302 TranslateFree m_free;
1303 TranslateAxis m_axis;
1304 RenderableArrowLine m_arrow_x;
1305 RenderableArrowLine m_arrow_y;
1306 RenderableArrowLine m_arrow_z;
1307 RenderableArrowHead m_arrow_head_x;
1308 RenderableArrowHead m_arrow_head_y;
1309 RenderableArrowHead m_arrow_head_z;
1310 RenderableQuad m_quad_screen;
1311 SelectableBool m_selectable_x;
1312 SelectableBool m_selectable_y;
1313 SelectableBool m_selectable_z;
1314 SelectableBool m_selectable_screen;
1315 Pivot2World m_pivot;
1316 public:
1317 static Shader* m_state_wire;
1318 static Shader* m_state_fill;
1319
1320 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1321         m_free( translatable ),
1322         m_axis( translatable ),
1323         m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1324         m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1325         m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1326         draw_arrowline( length, m_arrow_x.m_line, 0 );
1327         draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1328         draw_arrowline( length, m_arrow_y.m_line, 1 );
1329         draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1330         draw_arrowline( length, m_arrow_z.m_line, 2 );
1331         draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1332
1333         draw_quad( 16, m_quad_screen.m_quad );
1334 }
1335
1336 void UpdateColours(){
1337         m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1338         m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1339         m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1340         m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1341         m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1342         m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1343         m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1344 }
1345
1346 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1347         return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1348 }
1349
1350 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1351         m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1352
1353         // temp hack
1354         UpdateColours();
1355
1356         Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1357         bool show_x = manipulator_show_axis( m_pivot, x );
1358
1359         Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1360         bool show_y = manipulator_show_axis( m_pivot, y );
1361
1362         Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1363         bool show_z = manipulator_show_axis( m_pivot, z );
1364
1365         renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1366         renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1367
1368         if ( show_x ) {
1369                 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1370         }
1371         if ( show_y ) {
1372                 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1373         }
1374         if ( show_z ) {
1375                 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1376         }
1377
1378         renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1379
1380         renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1381         renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1382
1383         if ( show_x ) {
1384                 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1385         }
1386         if ( show_y ) {
1387                 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1388         }
1389         if ( show_z ) {
1390                 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1391         }
1392 }
1393 void testSelect( const View& view, const Matrix4& pivot2world ){
1394         m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1395
1396         SelectionPool selector;
1397
1398         Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1399         bool show_x = manipulator_show_axis( m_pivot, x );
1400
1401         Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1402         bool show_y = manipulator_show_axis( m_pivot, y );
1403
1404         Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1405         bool show_z = manipulator_show_axis( m_pivot, z );
1406
1407         {
1408                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1409
1410                 {
1411                         SelectionIntersection best;
1412                         Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1413                         if ( best.valid() ) {
1414                                 best = SelectionIntersection( 0, 0 );
1415                                 selector.addSelectable( best, &m_selectable_screen );
1416                         }
1417                 }
1418         }
1419
1420         {
1421                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1422
1423 #if defined( DEBUG_SELECTION )
1424                 g_render_clipped.construct( view.GetViewMatrix() );
1425 #endif
1426
1427                 if ( show_x ) {
1428                         SelectionIntersection best;
1429                         Line_BestPoint( local2view, m_arrow_x.m_line, best );
1430                         Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1431                         selector.addSelectable( best, &m_selectable_x );
1432                 }
1433
1434                 if ( show_y ) {
1435                         SelectionIntersection best;
1436                         Line_BestPoint( local2view, m_arrow_y.m_line, best );
1437                         Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1438                         selector.addSelectable( best, &m_selectable_y );
1439                 }
1440
1441                 if ( show_z ) {
1442                         SelectionIntersection best;
1443                         Line_BestPoint( local2view, m_arrow_z.m_line, best );
1444                         Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1445                         selector.addSelectable( best, &m_selectable_z );
1446                 }
1447         }
1448
1449         if ( !selector.failed() ) {
1450                 ( *selector.begin() ).second->setSelected( true );
1451         }
1452 }
1453
1454 Manipulatable* GetManipulatable(){
1455         if ( m_selectable_x.isSelected() ) {
1456                 m_axis.SetAxis( g_vector3_axis_x );
1457                 return &m_axis;
1458         }
1459         else if ( m_selectable_y.isSelected() ) {
1460                 m_axis.SetAxis( g_vector3_axis_y );
1461                 return &m_axis;
1462         }
1463         else if ( m_selectable_z.isSelected() ) {
1464                 m_axis.SetAxis( g_vector3_axis_z );
1465                 return &m_axis;
1466         }
1467         else
1468         {
1469                 return &m_free;
1470         }
1471 }
1472
1473 void setSelected( bool select ){
1474         m_selectable_x.setSelected( select );
1475         m_selectable_y.setSelected( select );
1476         m_selectable_z.setSelected( select );
1477         m_selectable_screen.setSelected( select );
1478 }
1479 bool isSelected() const {
1480         return m_selectable_x.isSelected()
1481                    | m_selectable_y.isSelected()
1482                    | m_selectable_z.isSelected()
1483                    | m_selectable_screen.isSelected();
1484 }
1485 };
1486
1487 Shader* TranslateManipulator::m_state_wire;
1488 Shader* TranslateManipulator::m_state_fill;
1489
1490 class ScaleManipulator : public Manipulator
1491 {
1492 struct RenderableArrow : public OpenGLRenderable
1493 {
1494         PointVertex m_line[2];
1495
1496         void render( RenderStateFlags state ) const {
1497                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1498                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1499                 glDrawArrays( GL_LINES, 0, 2 );
1500         }
1501         void setColour( const Colour4b& colour ){
1502                 m_line[0].colour = colour;
1503                 m_line[1].colour = colour;
1504         }
1505 };
1506 struct RenderableQuad : public OpenGLRenderable
1507 {
1508         PointVertex m_quad[4];
1509         void render( RenderStateFlags state ) const {
1510                 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1511                 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1512                 glDrawArrays( GL_QUADS, 0, 4 );
1513         }
1514         void setColour( const Colour4b& colour ){
1515                 m_quad[0].colour = colour;
1516                 m_quad[1].colour = colour;
1517                 m_quad[2].colour = colour;
1518                 m_quad[3].colour = colour;
1519         }
1520 };
1521
1522 ScaleFree m_free;
1523 ScaleAxis m_axis;
1524 RenderableArrow m_arrow_x;
1525 RenderableArrow m_arrow_y;
1526 RenderableArrow m_arrow_z;
1527 RenderableQuad m_quad_screen;
1528 SelectableBool m_selectable_x;
1529 SelectableBool m_selectable_y;
1530 SelectableBool m_selectable_z;
1531 SelectableBool m_selectable_screen;
1532 Pivot2World m_pivot;
1533 public:
1534 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1535         m_free( scalable ),
1536         m_axis( scalable ){
1537         draw_arrowline( length, m_arrow_x.m_line, 0 );
1538         draw_arrowline( length, m_arrow_y.m_line, 1 );
1539         draw_arrowline( length, m_arrow_z.m_line, 2 );
1540
1541         draw_quad( 16, m_quad_screen.m_quad );
1542 }
1543
1544 Pivot2World& getPivot(){
1545         return m_pivot;
1546 }
1547
1548 void UpdateColours(){
1549         m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1550         m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1551         m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1552         m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1553 }
1554
1555 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1556         m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1557
1558         // temp hack
1559         UpdateColours();
1560
1561         renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1562         renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1563         renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1564
1565         renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1566 }
1567 void testSelect( const View& view, const Matrix4& pivot2world ){
1568         m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1569
1570         SelectionPool selector;
1571
1572         {
1573                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1574
1575 #if defined( DEBUG_SELECTION )
1576                 g_render_clipped.construct( view.GetViewMatrix() );
1577 #endif
1578
1579                 {
1580                         SelectionIntersection best;
1581                         Line_BestPoint( local2view, m_arrow_x.m_line, best );
1582                         selector.addSelectable( best, &m_selectable_x );
1583                 }
1584
1585                 {
1586                         SelectionIntersection best;
1587                         Line_BestPoint( local2view, m_arrow_y.m_line, best );
1588                         selector.addSelectable( best, &m_selectable_y );
1589                 }
1590
1591                 {
1592                         SelectionIntersection best;
1593                         Line_BestPoint( local2view, m_arrow_z.m_line, best );
1594                         selector.addSelectable( best, &m_selectable_z );
1595                 }
1596         }
1597
1598         {
1599                 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1600
1601                 {
1602                         SelectionIntersection best;
1603                         Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1604                         selector.addSelectable( best, &m_selectable_screen );
1605                 }
1606         }
1607
1608         if ( !selector.failed() ) {
1609                 ( *selector.begin() ).second->setSelected( true );
1610         }
1611 }
1612
1613 Manipulatable* GetManipulatable(){
1614         if ( m_selectable_x.isSelected() ) {
1615                 m_axis.SetAxis( g_vector3_axis_x );
1616                 return &m_axis;
1617         }
1618         else if ( m_selectable_y.isSelected() ) {
1619                 m_axis.SetAxis( g_vector3_axis_y );
1620                 return &m_axis;
1621         }
1622         else if ( m_selectable_z.isSelected() ) {
1623                 m_axis.SetAxis( g_vector3_axis_z );
1624                 return &m_axis;
1625         }
1626         else{
1627                 return &m_free;
1628         }
1629 }
1630
1631 void setSelected( bool select ){
1632         m_selectable_x.setSelected( select );
1633         m_selectable_y.setSelected( select );
1634         m_selectable_z.setSelected( select );
1635         m_selectable_screen.setSelected( select );
1636 }
1637 bool isSelected() const {
1638         return m_selectable_x.isSelected()
1639                    | m_selectable_y.isSelected()
1640                    | m_selectable_z.isSelected()
1641                    | m_selectable_screen.isSelected();
1642 }
1643 };
1644
1645
1646 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1647         return InstanceTypeCast<PlaneSelectable>::cast( instance );
1648 }
1649
1650 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1651 {
1652 Selector& m_selector;
1653 SelectionTest& m_test;
1654 PlaneCallback m_selectedPlaneCallback;
1655 public:
1656 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1657         : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1658 }
1659 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1660         if ( path.top().get().visible() ) {
1661                 Selectable* selectable = Instance_getSelectable( instance );
1662                 if ( selectable != 0 && selectable->isSelected() ) {
1663                         PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1664                         if ( planeSelectable != 0 ) {
1665                                 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1666                         }
1667                 }
1668         }
1669         return true;
1670 }
1671 };
1672
1673 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1674 {
1675 Selector& m_selector;
1676 const SelectedPlanes& m_selectedPlanes;
1677 public:
1678 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1679         : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1680 }
1681 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1682         if ( path.top().get().visible() ) {
1683                 Selectable* selectable = Instance_getSelectable( instance );
1684                 if ( selectable != 0 && selectable->isSelected() ) {
1685                         PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1686                         if ( planeSelectable != 0 ) {
1687                                 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1688                         }
1689                 }
1690         }
1691         return true;
1692 }
1693 };
1694
1695 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1696         graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1697 }
1698
1699 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1700         graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1701 }
1702
1703
1704 class PlaneLess
1705 {
1706 public:
1707 bool operator()( const Plane3& plane, const Plane3& other ) const {
1708         if ( plane.a < other.a ) {
1709                 return true;
1710         }
1711         if ( other.a < plane.a ) {
1712                 return false;
1713         }
1714
1715         if ( plane.b < other.b ) {
1716                 return true;
1717         }
1718         if ( other.b < plane.b ) {
1719                 return false;
1720         }
1721
1722         if ( plane.c < other.c ) {
1723                 return true;
1724         }
1725         if ( other.c < plane.c ) {
1726                 return false;
1727         }
1728
1729         if ( plane.d < other.d ) {
1730                 return true;
1731         }
1732         if ( other.d < plane.d ) {
1733                 return false;
1734         }
1735
1736         return false;
1737 }
1738 };
1739
1740 typedef std::set<Plane3, PlaneLess> PlaneSet;
1741
1742 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1743         self.insert( plane );
1744 }
1745
1746 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1747         return self.find( plane ) != self.end();
1748 }
1749
1750
1751 class SelectedPlaneSet : public SelectedPlanes
1752 {
1753 PlaneSet m_selectedPlanes;
1754 public:
1755 bool empty() const {
1756         return m_selectedPlanes.empty();
1757 }
1758
1759 void insert( const Plane3& plane ){
1760         PlaneSet_insert( m_selectedPlanes, plane );
1761 }
1762 bool contains( const Plane3& plane ) const {
1763         return PlaneSet_contains( m_selectedPlanes, plane );
1764 }
1765 typedef MemberCaller1<SelectedPlaneSet, const Plane3&, &SelectedPlaneSet::insert> InsertCaller;
1766 };
1767
1768
1769 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1770         SelectedPlaneSet selectedPlanes;
1771
1772         Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1773         Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1774
1775         return !selectedPlanes.empty();
1776 }
1777
1778 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1779 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1780 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1781 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1782 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1783 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1784
1785 class ResizeTranslatable : public Translatable
1786 {
1787 void translate( const Vector3& translation ){
1788         Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1789 }
1790 };
1791
1792 class DragTranslatable : public Translatable
1793 {
1794 void translate( const Vector3& translation ){
1795         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1796                 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1797         }
1798         else
1799         {
1800                 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1801         }
1802 }
1803 };
1804
1805 class SelectionVolume : public SelectionTest
1806 {
1807 Matrix4 m_local2view;
1808 const View& m_view;
1809 clipcull_t m_cull;
1810 Vector3 m_near;
1811 Vector3 m_far;
1812 public:
1813 SelectionVolume( const View& view )
1814         : m_view( view ){
1815 }
1816
1817 const VolumeTest& getVolume() const {
1818         return m_view;
1819 }
1820
1821 const Vector3& getNear() const {
1822         return m_near;
1823 }
1824 const Vector3& getFar() const {
1825         return m_far;
1826 }
1827
1828 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1829         m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
1830
1831         // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1832         // Don't cull if the view is wireframe and the polygons are two-sided.
1833         m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
1834
1835         {
1836                 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
1837
1838                 m_near = vector4_projected(
1839                         matrix4_transformed_vector4(
1840                                 screen2world,
1841                                 Vector4( 0, 0, -1, 1 )
1842                                 )
1843                         );
1844
1845                 m_far = vector4_projected(
1846                         matrix4_transformed_vector4(
1847                                 screen2world,
1848                                 Vector4( 0, 0, 1, 1 )
1849                                 )
1850                         );
1851         }
1852
1853 #if defined( DEBUG_SELECTION )
1854         g_render_clipped.construct( m_view.GetViewMatrix() );
1855 #endif
1856 }
1857 void TestPoint( const Vector3& point, SelectionIntersection& best ){
1858         Vector4 clipped;
1859         if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
1860                 best = select_point_from_clipped( clipped );
1861         }
1862 }
1863 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1864         Vector4 clipped[9];
1865         for ( std::size_t i = 0; i + 2 < count; ++i )
1866         {
1867                 BestPoint(
1868                         matrix4_clip_triangle(
1869                                 m_local2view,
1870                                 reinterpret_cast<const Vector3&>( vertices[0] ),
1871                                 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
1872                                 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
1873                                 clipped
1874                                 ),
1875                         clipped,
1876                         best,
1877                         m_cull
1878                         );
1879         }
1880 }
1881 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1882         if ( count == 0 ) {
1883                 return;
1884         }
1885         Vector4 clipped[9];
1886         for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
1887         {
1888                 BestPoint(
1889                         matrix4_clip_line(
1890                                 m_local2view,
1891                                 reinterpret_cast<const Vector3&>( ( *prev ) ),
1892                                 reinterpret_cast<const Vector3&>( ( *i ) ),
1893                                 clipped
1894                                 ),
1895                         clipped,
1896                         best,
1897                         m_cull
1898                         );
1899         }
1900 }
1901 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1902         if ( count == 0 ) {
1903                 return;
1904         }
1905         Vector4 clipped[9];
1906         for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
1907         {
1908                 BestPoint(
1909                         matrix4_clip_line(
1910                                 m_local2view,
1911                                 reinterpret_cast<const Vector3&>( ( *i ) ),
1912                                 reinterpret_cast<const Vector3&>( ( *next ) ),
1913                                 clipped
1914                                 ),
1915                         clipped,
1916                         best,
1917                         m_cull
1918                         );
1919         }
1920 }
1921 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1922         if ( count == 0 ) {
1923                 return;
1924         }
1925         Vector4 clipped[9];
1926         for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
1927         {
1928                 BestPoint(
1929                         matrix4_clip_line(
1930                                 m_local2view,
1931                                 reinterpret_cast<const Vector3&>( ( *i ) ),
1932                                 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
1933                                 clipped
1934                                 ),
1935                         clipped,
1936                         best,
1937                         m_cull
1938                         );
1939         }
1940 }
1941 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1942         Vector4 clipped[9];
1943         for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
1944         {
1945                 BestPoint(
1946                         matrix4_clip_triangle(
1947                                 m_local2view,
1948                                 reinterpret_cast<const Vector3&>( vertices[*i] ),
1949                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1950                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1951                                 clipped
1952                                 ),
1953                         clipped,
1954                         best,
1955                         m_cull
1956                         );
1957         }
1958 }
1959 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1960         Vector4 clipped[9];
1961         for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
1962         {
1963                 BestPoint(
1964                         matrix4_clip_triangle(
1965                                 m_local2view,
1966                                 reinterpret_cast<const Vector3&>( vertices[*i] ),
1967                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1968                                 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1969                                 clipped
1970                                 ),
1971                         clipped,
1972                         best,
1973                         m_cull
1974                         );
1975                 BestPoint(
1976                         matrix4_clip_triangle(
1977                                 m_local2view,
1978                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1979                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1980                                 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1981                                 clipped
1982                                 ),
1983                         clipped,
1984                         best,
1985                         m_cull
1986                         );
1987         }
1988 }
1989 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1990         Vector4 clipped[9];
1991         for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
1992         {
1993                 BestPoint(
1994                         matrix4_clip_triangle(
1995                                 m_local2view,
1996                                 reinterpret_cast<const Vector3&>( vertices[*i] ),
1997                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1998                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1999                                 clipped
2000                                 ),
2001                         clipped,
2002                         best,
2003                         m_cull
2004                         );
2005                 BestPoint(
2006                         matrix4_clip_triangle(
2007                                 m_local2view,
2008                                 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2009                                 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2010                                 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2011                                 clipped
2012                                 ),
2013                         clipped,
2014                         best,
2015                         m_cull
2016                         );
2017         }
2018 }
2019 };
2020
2021 class SelectionCounter
2022 {
2023 public:
2024 typedef const Selectable& first_argument_type;
2025
2026 SelectionCounter( const SelectionChangeCallback& onchanged )
2027         : m_count( 0 ), m_onchanged( onchanged ){
2028 }
2029 void operator()( const Selectable& selectable ){
2030         if ( selectable.isSelected() ) {
2031                 ++m_count;
2032         }
2033         else
2034         {
2035                 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
2036                 --m_count;
2037         }
2038
2039         m_onchanged( selectable );
2040 }
2041 bool empty() const {
2042         return m_count == 0;
2043 }
2044 std::size_t size() const {
2045         return m_count;
2046 }
2047 private:
2048 std::size_t m_count;
2049 SelectionChangeCallback m_onchanged;
2050 };
2051
2052 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2053         view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2054 }
2055
2056 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2057         rect_t selection_box;
2058         selection_box.min[0] = device_point[0] - device_epsilon[0];
2059         selection_box.min[1] = device_point[1] - device_epsilon[1];
2060         selection_box.max[0] = device_point[0] + device_epsilon[0];
2061         selection_box.max[1] = device_point[1] + device_epsilon[1];
2062         return selection_box;
2063 }
2064
2065 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2066         rect_t selection_box;
2067         selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2068         selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2069         selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2070         selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2071         return selection_box;
2072 }
2073
2074 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2075         return quaternion_normalised( quaternion_multiplied_by_quaternion(
2076                                                                           quaternion_normalised( quaternion_multiplied_by_quaternion(
2077                                                                                                                                  quaternion_inverse( localToWorld ),
2078                                                                                                                                  world
2079                                                                                                                                  ) ),
2080                                                                           localToWorld
2081                                                                           ) );
2082 }
2083
2084 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2085         matrix[0] = other[0];
2086         matrix[1] = other[1];
2087         matrix[2] = other[2];
2088         matrix[4] = other[4];
2089         matrix[5] = other[5];
2090         matrix[6] = other[6];
2091         matrix[8] = other[8];
2092         matrix[9] = other[9];
2093         matrix[10] = other[10];
2094 }
2095
2096 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2097         Editable* editable = Node_getEditable( instance.path().top() );
2098         if ( editable != 0 ) {
2099                 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2100         }
2101         else
2102         {
2103                 matrix4_assign_rotation( matrix, instance.localToWorld() );
2104         }
2105 }
2106
2107 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2108         ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2109         return componentSelectionTestable != 0
2110                    && componentSelectionTestable->isSelectedComponents();
2111 }
2112
2113 class TranslateSelected : public SelectionSystem::Visitor
2114 {
2115 const Vector3& m_translate;
2116 public:
2117 TranslateSelected( const Vector3& translate )
2118         : m_translate( translate ){
2119 }
2120 void visit( scene::Instance& instance ) const {
2121         Transformable* transform = Instance_getTransformable( instance );
2122         if ( transform != 0 ) {
2123                 transform->setType( TRANSFORM_PRIMITIVE );
2124                 transform->setTranslation( m_translate );
2125         }
2126 }
2127 };
2128
2129 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2130         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2131                 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2132         }
2133 }
2134
2135 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2136         return Vector3(
2137                            matrix4_transformed_point(
2138                                    matrix4_full_inverse( localToWorld ),
2139                                    world_pivot
2140                                    )
2141                            );
2142 }
2143
2144 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2145         // we need a translation inside the parent system to move the origin of this object to the right place
2146
2147         // mathematically, it must fulfill:
2148         //
2149         //   local_translation local_transform local_pivot = local_pivot
2150         //   local_translation = local_pivot - local_transform local_pivot
2151         //
2152         //   or maybe?
2153         //   local_transform local_translation local_pivot = local_pivot
2154         //                   local_translation local_pivot = local_transform^-1 local_pivot
2155         //                 local_translation + local_pivot = local_transform^-1 local_pivot
2156         //                   local_translation             = local_transform^-1 local_pivot - local_pivot
2157
2158         Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2159
2160         Vector3 local_translation(
2161                 vector3_subtracted(
2162                         local_pivot,
2163                         matrix4_transformed_point(
2164                                 local_transform,
2165                                 local_pivot
2166                                 )
2167                 /*
2168                     matrix4_transformed_point(
2169                         matrix4_full_inverse(local_transform),
2170                         local_pivot
2171                     ),
2172                     local_pivot
2173                  */
2174                         )
2175                 );
2176
2177         translation_local2object( parent_translation, local_translation, localToParent );
2178
2179         /*
2180            // verify it!
2181            globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2182            globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2183            globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2184            globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2185            globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2186          */
2187 }
2188
2189 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2190         translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2191 }
2192
2193 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2194         Matrix4 local_transform(
2195                 matrix4_multiplied_by_matrix4(
2196                         matrix4_full_inverse( localToWorld ),
2197                         matrix4_multiplied_by_matrix4(
2198                                 matrix4_scale_for_vec3( world_scale ),
2199                                 localToWorld
2200                                 )
2201                         )
2202                 );
2203         local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2204         translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2205 }
2206
2207 class rotate_selected : public SelectionSystem::Visitor
2208 {
2209 const Quaternion& m_rotate;
2210 const Vector3& m_world_pivot;
2211 public:
2212 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2213         : m_rotate( rotation ), m_world_pivot( world_pivot ){
2214 }
2215 void visit( scene::Instance& instance ) const {
2216         TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2217         if ( transformNode != 0 ) {
2218                 Transformable* transform = Instance_getTransformable( instance );
2219                 if ( transform != 0 ) {
2220                         transform->setType( TRANSFORM_PRIMITIVE );
2221                         transform->setScale( c_scale_identity );
2222                         transform->setTranslation( c_translation_identity );
2223
2224                         transform->setType( TRANSFORM_PRIMITIVE );
2225                         transform->setRotation( m_rotate );
2226
2227                         {
2228                                 Editable* editable = Node_getEditable( instance.path().top() );
2229                                 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2230
2231                                 Vector3 parent_translation;
2232                                 translation_for_pivoted_rotation(
2233                                         parent_translation,
2234                                         m_rotate,
2235                                         m_world_pivot,
2236                                         matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2237                                         matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2238                                         );
2239
2240                                 transform->setTranslation( parent_translation );
2241                         }
2242                 }
2243         }
2244 }
2245 };
2246
2247 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2248         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2249                 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2250         }
2251 }
2252
2253 class scale_selected : public SelectionSystem::Visitor
2254 {
2255 const Vector3& m_scale;
2256 const Vector3& m_world_pivot;
2257 public:
2258 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2259         : m_scale( scaling ), m_world_pivot( world_pivot ){
2260 }
2261 void visit( scene::Instance& instance ) const {
2262         TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2263         if ( transformNode != 0 ) {
2264                 Transformable* transform = Instance_getTransformable( instance );
2265                 if ( transform != 0 ) {
2266                         transform->setType( TRANSFORM_PRIMITIVE );
2267                         transform->setScale( c_scale_identity );
2268                         transform->setTranslation( c_translation_identity );
2269
2270                         transform->setType( TRANSFORM_PRIMITIVE );
2271                         transform->setScale( m_scale );
2272                         {
2273                                 Editable* editable = Node_getEditable( instance.path().top() );
2274                                 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2275
2276                                 Vector3 parent_translation;
2277                                 translation_for_pivoted_scale(
2278                                         parent_translation,
2279                                         m_scale,
2280                                         m_world_pivot,
2281                                         matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2282                                         matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2283                                         );
2284
2285                                 transform->setTranslation( parent_translation );
2286                         }
2287                 }
2288         }
2289 }
2290 };
2291
2292 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2293         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2294                 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2295         }
2296 }
2297
2298
2299 class translate_component_selected : public SelectionSystem::Visitor
2300 {
2301 const Vector3& m_translate;
2302 public:
2303 translate_component_selected( const Vector3& translate )
2304         : m_translate( translate ){
2305 }
2306 void visit( scene::Instance& instance ) const {
2307         Transformable* transform = Instance_getTransformable( instance );
2308         if ( transform != 0 ) {
2309                 transform->setType( TRANSFORM_COMPONENT );
2310                 transform->setTranslation( m_translate );
2311         }
2312 }
2313 };
2314
2315 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2316         if ( GlobalSelectionSystem().countSelected() != 0 ) {
2317                 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2318         }
2319 }
2320
2321 class rotate_component_selected : public SelectionSystem::Visitor
2322 {
2323 const Quaternion& m_rotate;
2324 const Vector3& m_world_pivot;
2325 public:
2326 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2327         : m_rotate( rotation ), m_world_pivot( world_pivot ){
2328 }
2329 void visit( scene::Instance& instance ) const {
2330         Transformable* transform = Instance_getTransformable( instance );
2331         if ( transform != 0 ) {
2332                 Vector3 parent_translation;
2333                 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2334
2335                 transform->setType( TRANSFORM_COMPONENT );
2336                 transform->setRotation( m_rotate );
2337                 transform->setTranslation( parent_translation );
2338         }
2339 }
2340 };
2341
2342 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2343         if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2344                 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2345         }
2346 }
2347
2348 class scale_component_selected : public SelectionSystem::Visitor
2349 {
2350 const Vector3& m_scale;
2351 const Vector3& m_world_pivot;
2352 public:
2353 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2354         : m_scale( scaling ), m_world_pivot( world_pivot ){
2355 }
2356 void visit( scene::Instance& instance ) const {
2357         Transformable* transform = Instance_getTransformable( instance );
2358         if ( transform != 0 ) {
2359                 Vector3 parent_translation;
2360                 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2361
2362                 transform->setType( TRANSFORM_COMPONENT );
2363                 transform->setScale( m_scale );
2364                 transform->setTranslation( parent_translation );
2365         }
2366 }
2367 };
2368
2369 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2370         if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2371                 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2372         }
2373 }
2374
2375
2376 class BooleanSelector : public Selector
2377 {
2378 bool m_selected;
2379 SelectionIntersection m_intersection;
2380 Selectable* m_selectable;
2381 public:
2382 BooleanSelector() : m_selected( false ){
2383 }
2384
2385 void pushSelectable( Selectable& selectable ){
2386         m_intersection = SelectionIntersection();
2387         m_selectable = &selectable;
2388 }
2389 void popSelectable(){
2390         if ( m_intersection.valid() ) {
2391                 m_selected = true;
2392         }
2393         m_intersection = SelectionIntersection();
2394 }
2395 void addIntersection( const SelectionIntersection& intersection ){
2396         if ( m_selectable->isSelected() ) {
2397                 assign_if_closer( m_intersection, intersection );
2398         }
2399 }
2400
2401 bool isSelected(){
2402         return m_selected;
2403 }
2404 };
2405
2406 class BestSelector : public Selector
2407 {
2408 SelectionIntersection m_intersection;
2409 Selectable* m_selectable;
2410 SelectionIntersection m_bestIntersection;
2411 std::list<Selectable*> m_bestSelectable;
2412 public:
2413 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2414 }
2415
2416 void pushSelectable( Selectable& selectable ){
2417         m_intersection = SelectionIntersection();
2418         m_selectable = &selectable;
2419 }
2420 void popSelectable(){
2421         if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2422                 m_bestSelectable.push_back( m_selectable );
2423                 m_bestIntersection = m_intersection;
2424         }
2425         else if ( m_intersection < m_bestIntersection ) {
2426                 m_bestSelectable.clear();
2427                 m_bestSelectable.push_back( m_selectable );
2428                 m_bestIntersection = m_intersection;
2429         }
2430         m_intersection = SelectionIntersection();
2431 }
2432 void addIntersection( const SelectionIntersection& intersection ){
2433         assign_if_closer( m_intersection, intersection );
2434 }
2435
2436 std::list<Selectable*>& best(){
2437         return m_bestSelectable;
2438 }
2439 };
2440
2441 class DragManipulator : public Manipulator
2442 {
2443 TranslateFree m_freeResize;
2444 TranslateFree m_freeDrag;
2445 ResizeTranslatable m_resize;
2446 DragTranslatable m_drag;
2447 SelectableBool m_dragSelectable;
2448 public:
2449
2450 bool m_selected;
2451
2452 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2453 }
2454
2455 Manipulatable* GetManipulatable(){
2456         return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2457 }
2458
2459 void testSelect( const View& view, const Matrix4& pivot2world ){
2460         SelectionPool selector;
2461
2462         SelectionVolume test( view );
2463
2464         if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2465                 BooleanSelector booleanSelector;
2466
2467                 Scene_TestSelect_Primitive( booleanSelector, test, view );
2468
2469                 if ( booleanSelector.isSelected() ) {
2470                         selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2471                         m_selected = false;
2472                 }
2473                 else
2474                 {
2475                         m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2476                 }
2477         }
2478         else
2479         {
2480                 BestSelector bestSelector;
2481                 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2482                 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2483                 {
2484                         if ( !( *i )->isSelected() ) {
2485                                 GlobalSelectionSystem().setSelectedAllComponents( false );
2486                         }
2487                         m_selected = false;
2488                         selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2489                         m_dragSelectable.setSelected( true );
2490                 }
2491         }
2492
2493         for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2494         {
2495                 ( *i ).second->setSelected( true );
2496         }
2497 }
2498
2499 void setSelected( bool select ){
2500         m_selected = select;
2501         m_dragSelectable.setSelected( select );
2502 }
2503 bool isSelected() const {
2504         return m_selected || m_dragSelectable.isSelected();
2505 }
2506 };
2507
2508 class ClipManipulator : public Manipulator
2509 {
2510 public:
2511
2512 Manipulatable* GetManipulatable(){
2513         ERROR_MESSAGE( "clipper is not manipulatable" );
2514         return 0;
2515 }
2516
2517 void setSelected( bool select ){
2518 }
2519 bool isSelected() const {
2520         return false;
2521 }
2522 };
2523
2524 class select_all : public scene::Graph::Walker
2525 {
2526 bool m_select;
2527 public:
2528 select_all( bool select )
2529         : m_select( select ){
2530 }
2531 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2532         Selectable* selectable = Instance_getSelectable( instance );
2533         if ( selectable != 0 ) {
2534                 selectable->setSelected( m_select );
2535         }
2536         return true;
2537 }
2538 };
2539
2540 class select_all_component : public scene::Graph::Walker
2541 {
2542 bool m_select;
2543 SelectionSystem::EComponentMode m_mode;
2544 public:
2545 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2546         : m_select( select ), m_mode( mode ){
2547 }
2548 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2549         ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2550         if ( componentSelectionTestable ) {
2551                 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2552         }
2553         return true;
2554 }
2555 };
2556
2557 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2558         GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2559 }
2560
2561
2562 // RadiantSelectionSystem
2563 class RadiantSelectionSystem :
2564         public SelectionSystem,
2565         public Translatable,
2566         public Rotatable,
2567         public Scalable,
2568         public Renderable
2569 {
2570 mutable Matrix4 m_pivot2world;
2571 Matrix4 m_pivot2world_start;
2572 Matrix4 m_manip2pivot_start;
2573 Translation m_translation;
2574 Rotation m_rotation;
2575 Scale m_scale;
2576 public:
2577 static Shader* m_state;
2578 private:
2579 EManipulatorMode m_manipulator_mode;
2580 Manipulator* m_manipulator;
2581
2582 // state
2583 bool m_undo_begun;
2584 EMode m_mode;
2585 EComponentMode m_componentmode;
2586
2587 SelectionCounter m_count_primitive;
2588 SelectionCounter m_count_component;
2589
2590 TranslateManipulator m_translate_manipulator;
2591 RotateManipulator m_rotate_manipulator;
2592 ScaleManipulator m_scale_manipulator;
2593 DragManipulator m_drag_manipulator;
2594 ClipManipulator m_clip_manipulator;
2595
2596 typedef SelectionList<scene::Instance> selection_t;
2597 selection_t m_selection;
2598 selection_t m_component_selection;
2599
2600 Signal1<const Selectable&> m_selectionChanged_callbacks;
2601
2602 void ConstructPivot() const;
2603 void setCustomPivotOrigin( Vector3& point ) const;
2604 public:
2605 void getSelectionAABB( AABB& bounds ) const;
2606 private:
2607 mutable bool m_pivotChanged;
2608 bool m_pivot_moving;
2609 mutable bool m_pivotIsCustom;
2610
2611 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2612
2613 bool nothingSelected() const {
2614         return ( Mode() == eComponent && m_count_component.empty() )
2615                    || ( Mode() == ePrimitive && m_count_primitive.empty() );
2616 }
2617
2618
2619 public:
2620 enum EModifier
2621 {
2622         eManipulator,
2623         eToggle,
2624         eReplace,
2625         eCycle,
2626         eSelect,
2627         eDeselect,
2628 };
2629
2630 RadiantSelectionSystem() :
2631         m_undo_begun( false ),
2632         m_mode( ePrimitive ),
2633         m_componentmode( eDefault ),
2634         m_count_primitive( SelectionChangedCaller( *this ) ),
2635         m_count_component( SelectionChangedCaller( *this ) ),
2636         m_translate_manipulator( *this, 2, 64 ),
2637         m_rotate_manipulator( *this, 8, 64 ),
2638         m_scale_manipulator( *this, 0, 64 ),
2639         m_pivotChanged( false ),
2640         m_pivot_moving( false ),
2641         m_pivotIsCustom( false ){
2642         SetManipulatorMode( eTranslate );
2643         pivotChanged();
2644         addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2645         AddGridChangeCallback( PivotChangedCaller( *this ) );
2646 }
2647 void pivotChanged() const {
2648         m_pivotChanged = true;
2649         SceneChangeNotify();
2650 }
2651 typedef ConstMemberCaller<RadiantSelectionSystem, &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2652 void pivotChangedSelection( const Selectable& selectable ){
2653         pivotChanged();
2654 }
2655 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2656
2657 void SetMode( EMode mode ){
2658         if ( m_mode != mode ) {
2659                 m_mode = mode;
2660                 pivotChanged();
2661         }
2662 }
2663 EMode Mode() const {
2664         return m_mode;
2665 }
2666 void SetComponentMode( EComponentMode mode ){
2667         m_componentmode = mode;
2668 }
2669 EComponentMode ComponentMode() const {
2670         return m_componentmode;
2671 }
2672 void SetManipulatorMode( EManipulatorMode mode ){
2673         m_pivotIsCustom = false;
2674         m_manipulator_mode = mode;
2675         switch ( m_manipulator_mode )
2676         {
2677         case eTranslate: m_manipulator = &m_translate_manipulator; break;
2678         case eRotate: m_manipulator = &m_rotate_manipulator; break;
2679         case eScale: m_manipulator = &m_scale_manipulator; break;
2680         case eDrag: m_manipulator = &m_drag_manipulator; break;
2681         case eClip: m_manipulator = &m_clip_manipulator; break;
2682         }
2683         pivotChanged();
2684 }
2685 EManipulatorMode ManipulatorMode() const {
2686         return m_manipulator_mode;
2687 }
2688
2689 SelectionChangeCallback getObserver( EMode mode ){
2690         if ( mode == ePrimitive ) {
2691                 return makeCallback1( m_count_primitive );
2692         }
2693         else
2694         {
2695                 return makeCallback1( m_count_component );
2696         }
2697 }
2698 std::size_t countSelected() const {
2699         return m_count_primitive.size();
2700 }
2701 std::size_t countSelectedComponents() const {
2702         return m_count_component.size();
2703 }
2704 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2705         if ( selectable.isSelected() ) {
2706                 m_selection.append( instance );
2707         }
2708         else
2709         {
2710                 m_selection.erase( instance );
2711         }
2712
2713         ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2714 }
2715 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2716         if ( selectable.isSelected() ) {
2717                 m_component_selection.append( instance );
2718         }
2719         else
2720         {
2721                 m_component_selection.erase( instance );
2722         }
2723
2724         ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2725 }
2726 scene::Instance& ultimateSelected() const {
2727         ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2728         return m_selection.back();
2729 }
2730 scene::Instance& penultimateSelected() const {
2731         ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2732         return *( *( --( --m_selection.end() ) ) );
2733 }
2734 void setSelectedAll( bool selected ){
2735         GlobalSceneGraph().traverse( select_all( selected ) );
2736
2737         m_manipulator->setSelected( selected );
2738 }
2739 void setSelectedAllComponents( bool selected ){
2740         Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2741         Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2742         Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2743
2744         m_manipulator->setSelected( selected );
2745 }
2746
2747 void foreachSelected( const Visitor& visitor ) const {
2748         selection_t::const_iterator i = m_selection.begin();
2749         while ( i != m_selection.end() )
2750         {
2751                 visitor.visit( *( *( i++ ) ) );
2752         }
2753 }
2754 void foreachSelectedComponent( const Visitor& visitor ) const {
2755         selection_t::const_iterator i = m_component_selection.begin();
2756         while ( i != m_component_selection.end() )
2757         {
2758                 visitor.visit( *( *( i++ ) ) );
2759         }
2760 }
2761
2762 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2763         m_selectionChanged_callbacks.connectLast( handler );
2764 }
2765 void selectionChanged( const Selectable& selectable ){
2766         m_selectionChanged_callbacks( selectable );
2767 }
2768 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2769
2770
2771 void startMove(){
2772         m_pivot2world_start = GetPivot2World();
2773 }
2774
2775 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2776         if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2777 #if defined ( DEBUG_SELECTION )
2778                 g_render_clipped.destroy();
2779 #endif
2780
2781                 m_manipulator->setSelected( false );
2782
2783                 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2784                         View scissored( view );
2785                         ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2786                         m_manipulator->testSelect( scissored, GetPivot2World() );
2787                 }
2788
2789                 startMove();
2790
2791                 m_pivot_moving = m_manipulator->isSelected();
2792
2793                 if ( m_pivot_moving ) {
2794                         Pivot2World pivot;
2795                         pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2796
2797                         m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2798
2799                         Matrix4 device2manip;
2800                         ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2801                         m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2802
2803                         m_undo_begun = false;
2804                 }
2805
2806                 SceneChangeNotify();
2807         }
2808
2809         return m_pivot_moving;
2810 }
2811
2812 void deselectAll(){
2813         if ( Mode() == eComponent ) {
2814                 setSelectedAllComponents( false );
2815         }
2816         else
2817         {
2818                 setSelectedAll( false );
2819         }
2820 }
2821
2822 void deselectComponentsOrAll( bool components ){
2823         if ( components ) {
2824                 setSelectedAllComponents( false );
2825         }
2826         else
2827         {
2828                 deselectAll();
2829         }
2830 }
2831
2832 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2833         //globalOutputStream() << device_point[0] << "   " << device_point[1] << "\n";
2834         ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2835
2836         if ( modifier == eReplace ) {
2837                 deselectComponentsOrAll( face );
2838         }
2839 /*
2840 //nothingSelected() doesn't consider faces, selected in non-component mode, m
2841         if ( modifier == eCycle && nothingSelected() ){
2842                 modifier = eReplace;
2843         }
2844 */
2845   #if defined ( DEBUG_SELECTION )
2846         g_render_clipped.destroy();
2847   #endif
2848
2849         {
2850                 View scissored( view );
2851                 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2852
2853                 SelectionVolume volume( scissored );
2854                 SelectionPool selector;
2855                 if ( face ) {
2856                         Scene_TestSelect_Component( selector, volume, scissored, eFace );
2857                 }
2858                 else
2859                 {
2860                         Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2861                 }
2862
2863                 if ( !selector.failed() ) {
2864                         switch ( modifier )
2865                         {
2866                         case RadiantSelectionSystem::eToggle:
2867                         {
2868                                 SelectableSortedSet::iterator best = selector.begin();
2869                                 // toggle selection of the object with least depth
2870                                 if ( ( *best ).second->isSelected() ) {
2871                                         ( *best ).second->setSelected( false );
2872                                 }
2873                                 else{
2874                                         ( *best ).second->setSelected( true );
2875                                 }
2876                         }
2877                         break;
2878                         // if cycle mode not enabled, enable it
2879                         case RadiantSelectionSystem::eReplace:
2880                         {
2881                                 // select closest
2882                                 ( *selector.begin() ).second->setSelected( true );
2883                         }
2884                         break;
2885                         // select the next object in the list from the one already selected
2886                         case RadiantSelectionSystem::eCycle:
2887                         {
2888                                 bool CycleSelectionOccured = false;
2889                                 SelectionPool::iterator i = selector.begin();
2890                                 while ( i != selector.end() )
2891                                 {
2892                                         if ( ( *i ).second->isSelected() ) {
2893                                                 deselectComponentsOrAll( face );
2894                                                 ++i;
2895                                                 if ( i != selector.end() ) {
2896                                                         i->second->setSelected( true );
2897                                                 }
2898                                                 else
2899                                                 {
2900                                                         selector.begin()->second->setSelected( true );
2901                                                 }
2902                                                 CycleSelectionOccured = true;
2903                                                 break;
2904                                         }
2905                                         ++i;
2906                                 }
2907                                 if( !CycleSelectionOccured ){
2908                                         deselectComponentsOrAll( face );
2909                                         ( *selector.begin() ).second->setSelected( true );
2910                                 }
2911                         }
2912                         break;
2913                         case RadiantSelectionSystem::eSelect:
2914                         {
2915                                 SelectionPool::iterator best = selector.begin();
2916                                 if( !( *best ).second->isSelected() ){
2917                                         ( *best ).second->setSelected( true );
2918                                 }
2919                                 SelectionPool::iterator i = best;
2920                                 ++i;
2921                                 while ( i != selector.end() )
2922                                 {
2923                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2924                                                 if( !( *i ).second->isSelected() ){
2925                                                         ( *i ).second->setSelected( true );
2926                                                 }
2927                                         }
2928                                         else{
2929                                                 break;
2930                                         }
2931                                         ++i;
2932                                 }
2933                         }
2934                         break;
2935                         case RadiantSelectionSystem::eDeselect:
2936                         {
2937                                 SelectionPool::iterator best = selector.begin();
2938                                 if( ( *best ).second->isSelected() ){
2939                                         ( *best ).second->setSelected( false );
2940                                 }
2941                                 SelectionPool::iterator i = best;
2942                                 ++i;
2943                                 while ( i != selector.end() )
2944                                 {
2945                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2946                                                 if( ( *i ).second->isSelected() ){
2947                                                         ( *i ).second->setSelected( false );
2948                                                 }
2949                                         }
2950                                         else{
2951                                                 break;
2952                                         }
2953                                         ++i;
2954                                 }
2955                         }
2956                         break;
2957                         default:
2958                                 break;
2959                         }
2960                 }
2961                 else if( modifier == eCycle ){
2962                         deselectComponentsOrAll( face );
2963                 }
2964         }
2965 }
2966
2967 bool SelectPoint_InitPaint( const View& view, const float device_point[2], const float device_epsilon[2], bool face ){
2968         ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2969   #if defined ( DEBUG_SELECTION )
2970         g_render_clipped.destroy();
2971   #endif
2972
2973         {
2974                 View scissored( view );
2975                 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2976
2977                 SelectionVolume volume( scissored );
2978                 SelectionPool selector;
2979                 if ( face ) {
2980                         Scene_TestSelect_Component( selector, volume, scissored, eFace );
2981                 }
2982                 else
2983                 {
2984                         Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2985                 }
2986
2987                 if ( !selector.failed() ) {
2988                         SelectableSortedSet::iterator best = selector.begin();
2989                         const bool wasSelected = ( *best ).second->isSelected();
2990                         ( *best ).second->setSelected( !wasSelected );
2991                         SelectableSortedSet::iterator i = best;
2992                         ++i;
2993                         while ( i != selector.end() )
2994                         {
2995                                 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2996                                         ( *i ).second->setSelected( !wasSelected );
2997                                 }
2998                                 else{
2999                                         break;
3000                                 }
3001                                 ++i;
3002                         }
3003                         return !wasSelected;
3004                 }
3005                 else{
3006                         return true;
3007                 }
3008         }
3009 }
3010
3011 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3012         if ( modifier == eReplace ) {
3013                 deselectComponentsOrAll( face );
3014         }
3015
3016   #if defined ( DEBUG_SELECTION )
3017         g_render_clipped.destroy();
3018   #endif
3019
3020         {
3021                 View scissored( view );
3022                 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
3023
3024                 SelectionVolume volume( scissored );
3025                 SelectionPool pool;
3026                 if ( face ) {
3027                         Scene_TestSelect_Component( pool, volume, scissored, eFace );
3028                 }
3029                 else
3030                 {
3031                         Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
3032                 }
3033
3034                 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
3035                 {
3036                         ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
3037                 }
3038         }
3039 }
3040
3041
3042 void translate( const Vector3& translation ){
3043         if ( !nothingSelected() ) {
3044                 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3045
3046                 m_translation = translation;
3047
3048                 m_pivot2world = m_pivot2world_start;
3049                 matrix4_translate_by_vec3( m_pivot2world, translation );
3050
3051                 if ( Mode() == eComponent ) {
3052                         Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
3053                 }
3054                 else
3055                 {
3056                         Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
3057                 }
3058
3059                 SceneChangeNotify();
3060         }
3061 }
3062 void outputTranslation( TextOutputStream& ostream ){
3063         ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3064 }
3065 void rotate( const Quaternion& rotation ){
3066         if ( !nothingSelected() ) {
3067                 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3068
3069                 m_rotation = rotation;
3070
3071                 if ( Mode() == eComponent ) {
3072                         Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3073
3074                         matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3075                 }
3076                 else
3077                 {
3078                         Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3079
3080                         matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3081                 }
3082
3083                 SceneChangeNotify();
3084         }
3085 }
3086 void outputRotation( TextOutputStream& ostream ){
3087         ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3088 }
3089 void scale( const Vector3& scaling ){
3090         if ( !nothingSelected() ) {
3091                 m_scale = scaling;
3092
3093                 if ( Mode() == eComponent ) {
3094                         Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3095                 }
3096                 else
3097                 {
3098                         Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3099                 }
3100
3101                 SceneChangeNotify();
3102         }
3103 }
3104 void outputScale( TextOutputStream& ostream ){
3105         ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3106 }
3107
3108 void rotateSelected( const Quaternion& rotation ){
3109         startMove();
3110         rotate( rotation );
3111         freezeTransforms();
3112 }
3113 void translateSelected( const Vector3& translation ){
3114         startMove();
3115         translate( translation );
3116         freezeTransforms();
3117 }
3118 void scaleSelected( const Vector3& scaling ){
3119         startMove();
3120         scale( scaling );
3121         freezeTransforms();
3122 }
3123
3124 void MoveSelected( const View& view, const float device_point[2] ){
3125         if ( m_manipulator->isSelected() ) {
3126                 if ( !m_undo_begun ) {
3127                         m_undo_begun = true;
3128                         GlobalUndoSystem().start();
3129                 }
3130
3131                 Matrix4 device2manip;
3132                 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3133                 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1] );
3134         }
3135 }
3136
3137 /// \todo Support view-dependent nudge.
3138 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
3139         if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
3140                 translateSelected( nudge );
3141         }
3142 }
3143
3144 void endMove();
3145 void freezeTransforms();
3146
3147 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
3148 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3149         renderSolid( renderer, volume );
3150 }
3151
3152 const Matrix4& GetPivot2World() const {
3153         ConstructPivot();
3154         return m_pivot2world;
3155 }
3156
3157 static void constructStatic(){
3158         m_state = GlobalShaderCache().capture( "$POINT" );
3159   #if defined( DEBUG_SELECTION )
3160         g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3161   #endif
3162         TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3163         TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3164         RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3165 }
3166
3167 static void destroyStatic(){
3168   #if defined( DEBUG_SELECTION )
3169         GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3170   #endif
3171         GlobalShaderCache().release( "$WIRE_OVERLAY" );
3172         GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3173         GlobalShaderCache().release( "$WIRE_OVERLAY" );
3174         GlobalShaderCache().release( "$POINT" );
3175 }
3176 };
3177
3178 Shader* RadiantSelectionSystem::m_state = 0;
3179
3180
3181 namespace
3182 {
3183 RadiantSelectionSystem* g_RadiantSelectionSystem;
3184
3185 inline RadiantSelectionSystem& getSelectionSystem(){
3186         return *g_RadiantSelectionSystem;
3187 }
3188 }
3189
3190
3191
3192 class testselect_entity_visible : public scene::Graph::Walker
3193 {
3194 Selector& m_selector;
3195 SelectionTest& m_test;
3196 public:
3197 testselect_entity_visible( Selector& selector, SelectionTest& test )
3198         : m_selector( selector ), m_test( test ){
3199 }
3200 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3201         Selectable* selectable = Instance_getSelectable( instance );
3202         if ( selectable != 0
3203                  && Node_isEntity( path.top() ) ) {
3204                 m_selector.pushSelectable( *selectable );
3205         }
3206
3207         SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3208         if ( selectionTestable ) {
3209                 selectionTestable->testSelect( m_selector, m_test );
3210         }
3211
3212         return true;
3213 }
3214 void post( const scene::Path& path, scene::Instance& instance ) const {
3215         Selectable* selectable = Instance_getSelectable( instance );
3216         if ( selectable != 0
3217                  && Node_isEntity( path.top() ) ) {
3218                 m_selector.popSelectable();
3219         }
3220 }
3221 };
3222
3223 class testselect_primitive_visible : public scene::Graph::Walker
3224 {
3225 Selector& m_selector;
3226 SelectionTest& m_test;
3227 public:
3228 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3229         : m_selector( selector ), m_test( test ){
3230 }
3231 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3232         Selectable* selectable = Instance_getSelectable( instance );
3233         if ( selectable != 0 ) {
3234                 m_selector.pushSelectable( *selectable );
3235         }
3236
3237         SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3238         if ( selectionTestable ) {
3239                 selectionTestable->testSelect( m_selector, m_test );
3240         }
3241
3242         return true;
3243 }
3244 void post( const scene::Path& path, scene::Instance& instance ) const {
3245         Selectable* selectable = Instance_getSelectable( instance );
3246         if ( selectable != 0 ) {
3247                 m_selector.popSelectable();
3248         }
3249 }
3250 };
3251
3252 class testselect_component_visible : public scene::Graph::Walker
3253 {
3254 Selector& m_selector;
3255 SelectionTest& m_test;
3256 SelectionSystem::EComponentMode m_mode;
3257 public:
3258 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3259         : m_selector( selector ), m_test( test ), m_mode( mode ){
3260 }
3261 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3262         ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3263         if ( componentSelectionTestable ) {
3264                 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3265         }
3266
3267         return true;
3268 }
3269 };
3270
3271
3272 class testselect_component_visible_selected : public scene::Graph::Walker
3273 {
3274 Selector& m_selector;
3275 SelectionTest& m_test;
3276 SelectionSystem::EComponentMode m_mode;
3277 public:
3278 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3279         : m_selector( selector ), m_test( test ), m_mode( mode ){
3280 }
3281 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3282         Selectable* selectable = Instance_getSelectable( instance );
3283         if ( selectable != 0 && selectable->isSelected() ) {
3284                 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3285                 if ( componentSelectionTestable ) {
3286                         componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3287                 }
3288         }
3289
3290         return true;
3291 }
3292 };
3293
3294 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3295         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3296 }
3297
3298 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3299         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3300 }
3301
3302 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3303         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3304 }
3305
3306 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3307         switch ( mode )
3308         {
3309         case eEntity:
3310         {
3311                 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3312         }
3313         break;
3314         case ePrimitive:
3315                 Scene_TestSelect_Primitive( selector, test, view );
3316                 break;
3317         case eComponent:
3318                 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3319                 break;
3320         }
3321 }
3322
3323 class FreezeTransforms : public scene::Graph::Walker
3324 {
3325 public:
3326 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3327         TransformNode* transformNode = Node_getTransformNode( path.top() );
3328         if ( transformNode != 0 ) {
3329                 Transformable* transform = Instance_getTransformable( instance );
3330                 if ( transform != 0 ) {
3331                         transform->freezeTransform();
3332                 }
3333         }
3334         return true;
3335 }
3336 };
3337
3338 void RadiantSelectionSystem::freezeTransforms(){
3339         GlobalSceneGraph().traverse( FreezeTransforms() );
3340 }
3341
3342
3343 void RadiantSelectionSystem::endMove(){
3344         freezeTransforms();
3345
3346         if ( Mode() == ePrimitive ) {
3347                 if ( ManipulatorMode() == eDrag ) {
3348                         Scene_SelectAll_Component( false, SelectionSystem::eFace );
3349                 }
3350         }
3351
3352         m_pivot_moving = false;
3353         pivotChanged();
3354
3355         SceneChangeNotify();
3356
3357         if ( m_undo_begun ) {
3358                 StringOutputStream command;
3359
3360                 if ( ManipulatorMode() == eTranslate ) {
3361                         command << "translateTool";
3362                         outputTranslation( command );
3363                 }
3364                 else if ( ManipulatorMode() == eRotate ) {
3365                         command << "rotateTool";
3366                         outputRotation( command );
3367                 }
3368                 else if ( ManipulatorMode() == eScale ) {
3369                         command << "scaleTool";
3370                         outputScale( command );
3371                 }
3372                 else if ( ManipulatorMode() == eDrag ) {
3373                         command << "dragTool";
3374                 }
3375
3376                 GlobalUndoSystem().finish( command.c_str() );
3377         }
3378
3379 }
3380
3381 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3382         Entity* entity = Node_getEntity( instance.path().top() );
3383         if ( entity != 0
3384                  && ( entity->getEntityClass().fixedsize
3385                           || !node_is_group( instance.path().top() ) ) ) {
3386                 Editable* editable = Node_getEditable( instance.path().top() );
3387                 if ( editable != 0 ) {
3388                         return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3389                 }
3390                 else
3391                 {
3392                         return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3393                 }
3394         }
3395
3396         return instance.worldAABB();
3397 }
3398
3399 class bounds_selected : public scene::Graph::Walker
3400 {
3401 AABB& m_bounds;
3402 public:
3403 bounds_selected( AABB& bounds )
3404         : m_bounds( bounds ){
3405         m_bounds = AABB();
3406 }
3407 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3408         Selectable* selectable = Instance_getSelectable( instance );
3409         if ( selectable != 0
3410                  && selectable->isSelected() ) {
3411                 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3412         }
3413         return true;
3414 }
3415 };
3416
3417 class bounds_selected_component : public scene::Graph::Walker
3418 {
3419 AABB& m_bounds;
3420 public:
3421 bounds_selected_component( AABB& bounds )
3422         : m_bounds( bounds ){
3423         m_bounds = AABB();
3424 }
3425 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3426         Selectable* selectable = Instance_getSelectable( instance );
3427         if ( selectable != 0
3428                  && selectable->isSelected() ) {
3429                 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3430                 if ( componentEditable ) {
3431                         aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3432                 }
3433         }
3434         return true;
3435 }
3436 };
3437
3438 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3439         graph.traverse( bounds_selected( bounds ) );
3440 }
3441
3442 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3443         graph.traverse( bounds_selected_component( bounds ) );
3444 }
3445
3446 #if 0
3447 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3448         ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3449         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3450                  && componentEditable != 0 ) {
3451                 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3452         }
3453         else
3454         {
3455                 Bounded* bounded = Instance_getBounded( instance );
3456                 if ( bounded != 0 ) {
3457                         pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3458                 }
3459                 else
3460                 {
3461                         pivot = g_matrix4_identity;
3462                 }
3463         }
3464 }
3465 #endif
3466
3467 void RadiantSelectionSystem::ConstructPivot() const {
3468         if ( !m_pivotChanged || m_pivot_moving || m_pivotIsCustom ) {
3469                 return;
3470         }
3471         m_pivotChanged = false;
3472
3473         Vector3 m_object_pivot;
3474
3475         if ( !nothingSelected() ) {
3476                 {
3477                         AABB bounds;
3478                         if ( Mode() == eComponent ) {
3479                                 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3480                         }
3481                         else
3482                         {
3483                                 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3484                         }
3485                         m_object_pivot = bounds.origin;
3486                 }
3487
3488                 //vector3_snap( m_object_pivot, GetSnapGridSize() );
3489                 //globalOutputStream() << m_object_pivot << "\n";
3490                 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3491
3492                 switch ( m_manipulator_mode )
3493                 {
3494                 case eTranslate:
3495                         break;
3496                 case eRotate:
3497                         if ( Mode() == eComponent ) {
3498                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3499                         }
3500                         else
3501                         {
3502                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3503                         }
3504                         break;
3505                 case eScale:
3506                         if ( Mode() == eComponent ) {
3507                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3508                         }
3509                         else
3510                         {
3511                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3512                         }
3513                         break;
3514                 default:
3515                         break;
3516                 }
3517         }
3518 }
3519
3520 void RadiantSelectionSystem::setCustomPivotOrigin( Vector3& point ) const {
3521         /*if ( !m_pivotChanged || m_pivot_moving ) {
3522                 return;
3523         }*/
3524         //m_pivotChanged = false;
3525
3526         if ( !nothingSelected() && ( m_manipulator_mode == eTranslate || m_manipulator_mode == eRotate || m_manipulator_mode == eScale ) ) {
3527                 AABB bounds;
3528                 if ( Mode() == eComponent ) {
3529                         Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3530                 }
3531                 else
3532                 {
3533                         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3534                 }
3535                 //globalOutputStream() << point << "\n";
3536                 const float gridsize = GetSnapGridSize();
3537                 //const float bbox_epsilon = gridsize / 4.0;
3538
3539                 for( std::size_t i = 0; i < 3; i++ ){
3540                         if( point[i] < 900000 ){
3541                                 float bestsnapDist = fabs( bounds.origin[i] - point[i] );
3542                                 float bestsnapTo = bounds.origin[i];
3543                                 float othersnapDist = fabs( bounds.origin[i] + bounds.extents[i] - point[i] );
3544                                 if( othersnapDist < bestsnapDist ){
3545                                         bestsnapDist = othersnapDist;
3546                                         bestsnapTo = bounds.origin[i] + bounds.extents[i];
3547                                 }
3548                                 othersnapDist = fabs( bounds.origin[i] - bounds.extents[i] - point[i] );
3549                                 if( othersnapDist < bestsnapDist ){
3550                                         bestsnapDist = othersnapDist;
3551                                         bestsnapTo = bounds.origin[i] - bounds.extents[i];
3552                                 }
3553                                 othersnapDist = fabs( float_snapped( point[i], gridsize ) - point[i] );
3554                                 if( othersnapDist < bestsnapDist ){
3555                                         bestsnapDist = othersnapDist;
3556                                         bestsnapTo = float_snapped( point[i], gridsize );
3557                                 }
3558                                 point[i] = bestsnapTo;
3559
3560 /*                              if( float_equal_epsilon( point[i], bestsnapTo, bbox_epsilon ) ){
3561                                         point[i] = bestsnapTo;
3562                                 }
3563                                 else{
3564                                         point[i] = float_snapped( point[i], gridsize );
3565                                 }
3566                                 */
3567                                 m_pivot2world[i + 12] = point[i]; //m_pivot2world.tx() .ty() .tz()
3568                         }
3569                 }
3570
3571                 switch ( m_manipulator_mode )
3572                 {
3573                 case eTranslate:
3574                         break;
3575                 case eRotate:
3576                         if ( Mode() == eComponent ) {
3577                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3578                         }
3579                         else
3580                         {
3581                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3582                         }
3583                         break;
3584                 case eScale:
3585                         if ( Mode() == eComponent ) {
3586                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3587                         }
3588                         else
3589                         {
3590                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3591                         }
3592                         break;
3593                 default:
3594                         break;
3595                 }
3596         }
3597         m_pivotIsCustom = true;
3598 }
3599
3600 void RadiantSelectionSystem::getSelectionAABB( AABB& bounds ) const {
3601         if ( !nothingSelected() ) {
3602                 if ( Mode() == eComponent ) {
3603                         Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3604                 }
3605                 else
3606                 {
3607                         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3608                 }
3609         }
3610 }
3611
3612 void GetSelectionAABB( AABB& bounds ){
3613         getSelectionSystem().getSelectionAABB( bounds );
3614 }
3615
3616 const Matrix4& ssGetPivot2World(){
3617         return getSelectionSystem().GetPivot2World();
3618 }
3619
3620 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3621         //if(view->TestPoint(m_object_pivot))
3622         if ( !nothingSelected() ) {
3623                 renderer.Highlight( Renderer::ePrimitive, false );
3624                 renderer.Highlight( Renderer::eFace, false );
3625
3626                 renderer.SetState( m_state, Renderer::eWireframeOnly );
3627                 renderer.SetState( m_state, Renderer::eFullMaterials );
3628
3629                 m_manipulator->render( renderer, volume, GetPivot2World() );
3630         }
3631
3632 #if defined( DEBUG_SELECTION )
3633         renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3634         renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3635         renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3636 #endif
3637 }
3638
3639
3640 void SelectionSystem_OnBoundsChanged(){
3641         getSelectionSystem().pivotChanged();
3642 }
3643
3644
3645 SignalHandlerId SelectionSystem_boundsChanged;
3646
3647 void SelectionSystem_Construct(){
3648         RadiantSelectionSystem::constructStatic();
3649
3650         g_RadiantSelectionSystem = new RadiantSelectionSystem;
3651
3652         SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<SelectionSystem_OnBoundsChanged>() );
3653
3654         GlobalShaderCache().attachRenderable( getSelectionSystem() );
3655 }
3656
3657 void SelectionSystem_Destroy(){
3658         GlobalShaderCache().detachRenderable( getSelectionSystem() );
3659
3660         GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3661
3662         delete g_RadiantSelectionSystem;
3663
3664         RadiantSelectionSystem::destroyStatic();
3665 }
3666
3667
3668
3669
3670 inline float screen_normalised( float pos, std::size_t size ){
3671         return ( ( 2.0f * pos ) / size ) - 1.0f;
3672 }
3673
3674 typedef Vector2 DeviceVector;
3675
3676 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3677         return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3678 }
3679
3680 inline float device_constrained( float pos ){
3681         return std::min( 1.0f, std::max( -1.0f, pos ) );
3682 }
3683
3684 inline DeviceVector device_constrained( DeviceVector device ){
3685         return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3686 }
3687
3688 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3689         return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3690 }
3691
3692 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3693         return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3694 }
3695
3696 typedef Callback1<DeviceVector> MouseEventCallback;
3697
3698 Single<MouseEventCallback> g_mouseMovedCallback;
3699 Single<MouseEventCallback> g_mouseUpCallback;
3700
3701 #if 1
3702 const ButtonIdentifier c_button_select = c_buttonLeft;
3703 const ButtonIdentifier c_button_select2 = c_buttonRight;
3704 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3705 const ModifierFlags c_modifier_toggle = c_modifierShift;
3706 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3707 const ModifierFlags c_modifier_face = c_modifierControl;
3708 #else
3709 const ButtonIdentifier c_button_select = c_buttonLeft;
3710 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3711 const ModifierFlags c_modifier_toggle = c_modifierControl;
3712 const ModifierFlags c_modifier_replace = c_modifierNone;
3713 const ModifierFlags c_modifier_face = c_modifierShift;
3714 #endif
3715 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3716 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3717
3718 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3719 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3720 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3721 const ModifierFlags c_modifier_apply_texture3 =                     c_modifierShift;
3722 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3723
3724 class Selector_
3725 {
3726 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3727         if ( ( state == c_modifier_toggle || state == c_modifier_toggle_face || state == c_modifier_face ) ) {
3728                 if( m_mouse2 ){
3729                         return RadiantSelectionSystem::eReplace;
3730                 }
3731                 else{
3732                         return RadiantSelectionSystem::eToggle;
3733                 }
3734         }
3735         return RadiantSelectionSystem::eManipulator;
3736 }
3737
3738 rect_t getDeviceArea() const {
3739         DeviceVector delta( m_current - m_start );
3740         if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3741                 return SelectionBoxForArea( &m_start[0], &delta[0] );
3742         }
3743         else
3744         {
3745                 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3746                 return default_area;
3747         }
3748 }
3749
3750 public:
3751 DeviceVector m_start;
3752 DeviceVector m_current;
3753 DeviceVector m_epsilon;
3754 ModifierFlags m_state;
3755 bool m_mouse2;
3756 bool m_mouseMoved;
3757 bool m_mouseMovedWhilePressed;
3758 bool m_paintSelect;
3759 const View* m_view;
3760 RectangleCallback m_window_update;
3761
3762 Selector_() : m_start( 0.0f, 0.0f ), m_current( 0.0f, 0.0f ), m_state( c_modifierNone ), m_mouse2( false ), m_mouseMoved( false ), m_mouseMovedWhilePressed( false ){
3763 }
3764
3765 void draw_area(){
3766         m_window_update( getDeviceArea() );
3767 }
3768
3769 void testSelect( DeviceVector position ){
3770         RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3771         if ( modifier != RadiantSelectionSystem::eManipulator ) {
3772                 DeviceVector delta( position - m_start );
3773                 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3774                         DeviceVector delta( position - m_start );
3775                         //getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3776                         getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], RadiantSelectionSystem::eToggle, ( m_state & c_modifier_face ) != c_modifierNone );
3777                 }
3778                 else if( !m_mouseMovedWhilePressed ){
3779                         if ( modifier == RadiantSelectionSystem::eReplace && !m_mouseMoved ) {
3780                                 modifier = RadiantSelectionSystem::eCycle;
3781                         }
3782                         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3783                 }
3784         }
3785
3786         m_start = m_current = DeviceVector( 0.0f, 0.0f );
3787         draw_area();
3788 }
3789
3790 void testSelect_simpleM1( DeviceVector position ){
3791         /*RadiantSelectionSystem::EModifier modifier = RadiantSelectionSystem::eReplace;
3792         DeviceVector delta( position - m_start );
3793         if ( fabs( delta.x() ) < m_epsilon.x() && fabs( delta.y() ) < m_epsilon.y() ) {
3794                 modifier = RadiantSelectionSystem::eCycle;
3795         }
3796         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, false );*/
3797         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], m_mouseMoved ? RadiantSelectionSystem::eReplace : RadiantSelectionSystem::eCycle, false );
3798         m_start = m_current = device_constrained( position );
3799 }
3800
3801
3802 bool selecting() const {
3803         return m_state != c_modifier_manipulator && m_mouse2;
3804 }
3805
3806 void setState( ModifierFlags state ){
3807         bool was_selecting = selecting();
3808         m_state = state;
3809         if ( was_selecting ^ selecting() ) {
3810                 draw_area();
3811         }
3812 }
3813
3814 ModifierFlags getState() const {
3815         return m_state;
3816 }
3817
3818 void modifierEnable( ModifierFlags type ){
3819         setState( bitfield_enable( getState(), type ) );
3820 }
3821 void modifierDisable( ModifierFlags type ){
3822         setState( bitfield_disable( getState(), type ) );
3823 }
3824
3825 void mouseDown( DeviceVector position ){
3826         m_start = m_current = device_constrained( position );
3827         if( !m_mouse2 && m_state != c_modifierNone ){
3828                 m_paintSelect = getSelectionSystem().SelectPoint_InitPaint( *m_view, &position[0], &m_epsilon[0], ( m_state & c_modifier_face ) != c_modifierNone );
3829         }
3830 }
3831
3832 void mouseMoved( DeviceVector position ){
3833         m_current = device_constrained( position );
3834         m_mouseMovedWhilePressed = true;
3835         if( m_mouse2 ){
3836                 draw_area();
3837         }
3838         else if( m_state != c_modifier_manipulator ){
3839                 getSelectionSystem().SelectPoint( *m_view, &m_current[0], &m_epsilon[0],
3840                                                                                 m_paintSelect ? RadiantSelectionSystem::eSelect : RadiantSelectionSystem::eDeselect,
3841                                                                                 ( m_state & c_modifier_face ) != c_modifierNone );
3842         }
3843 }
3844 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseMoved> MouseMovedCaller;
3845
3846 void mouseUp( DeviceVector position ){
3847         if( m_mouse2 ){
3848                 testSelect( device_constrained( position ) );
3849         }
3850         else{
3851                 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3852         }
3853
3854         g_mouseMovedCallback.clear();
3855         g_mouseUpCallback.clear();
3856 }
3857 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseUp> MouseUpCaller;
3858 };
3859
3860
3861 class Manipulator_
3862 {
3863 public:
3864 DeviceVector m_epsilon;
3865 const View* m_view;
3866
3867 bool mouseDown( DeviceVector position ){
3868         return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
3869 }
3870
3871 void mouseMoved( DeviceVector position ){
3872         getSelectionSystem().MoveSelected( *m_view, &position[0] );
3873 }
3874 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseMoved> MouseMovedCaller;
3875
3876 void mouseUp( DeviceVector position ){
3877         getSelectionSystem().endMove();
3878         g_mouseMovedCallback.clear();
3879         g_mouseUpCallback.clear();
3880 }
3881 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseUp> MouseUpCaller;
3882 };
3883
3884 void Scene_copyClosestTexture( SelectionTest& test );
3885 void Scene_applyClosestTexture( SelectionTest& test );
3886
3887 class RadiantWindowObserver : public SelectionSystemWindowObserver
3888 {
3889 enum
3890 {
3891         SELECT_EPSILON = 8,
3892 };
3893
3894 int m_width;
3895 int m_height;
3896
3897 bool m_mouse_down;
3898
3899 public:
3900 Selector_ m_selector;
3901 Manipulator_ m_manipulator;
3902
3903 RadiantWindowObserver() : m_mouse_down( false ){
3904 }
3905 void release(){
3906         delete this;
3907 }
3908 void setView( const View& view ){
3909         m_selector.m_view = &view;
3910         m_manipulator.m_view = &view;
3911 }
3912 void setRectangleDrawCallback( const RectangleCallback& callback ){
3913         m_selector.m_window_update = callback;
3914 }
3915 void onSizeChanged( int width, int height ){
3916         m_width = width;
3917         m_height = height;
3918         DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
3919         m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
3920 }
3921 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3922         if ( button == c_button_select || ( button == c_button_select2 && modifiers != c_modifierNone ) ) {
3923                 m_mouse_down = true;
3924                 //m_selector.m_mouseMoved = false;
3925
3926                 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
3927                 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
3928                         g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
3929                         g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
3930                 }
3931                 else
3932                 {
3933                         if ( button == c_button_select ) {
3934                                 m_selector.m_mouse2 = false;
3935                         }
3936                         else{
3937                                 m_selector.m_mouse2 = true;
3938                         }
3939                         m_selector.mouseDown( devicePosition );
3940                         g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
3941                         g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
3942                 }
3943         }
3944         else if ( button == c_button_texture ) {
3945                 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
3946
3947                 View scissored( *m_selector.m_view );
3948                 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
3949                 SelectionVolume volume( scissored );
3950
3951                 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
3952                         Scene_applyClosestTexture( volume );
3953                 }
3954                 else if ( modifiers == c_modifier_copy_texture ) {
3955                         Scene_copyClosestTexture( volume );
3956                 }
3957         }
3958 }
3959 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
3960         m_selector.m_mouseMoved = true;
3961         if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
3962                 m_selector.m_mouseMovedWhilePressed = true;
3963                 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3964         }
3965 }
3966 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3967         if ( ( button == c_button_select || button == c_button_select2 ) && !g_mouseUpCallback.empty() ) {
3968                 m_mouse_down = false;
3969
3970                 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3971         }
3972         //L button w/o scene changed = tunnel selection
3973         if( // !getSelectionSystem().m_undo_begun &&
3974                 modifiers == c_modifierNone && button == c_button_select &&
3975                 //( !m_selector.m_mouseMoved || !m_mouse_down ) &&
3976                 !m_selector.m_mouseMovedWhilePressed &&
3977                 ( getSelectionSystem().Mode() != SelectionSystem::eComponent || getSelectionSystem().ManipulatorMode() != SelectionSystem::eDrag ) ){
3978                 m_selector.testSelect_simpleM1( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
3979         }
3980         //getSelectionSystem().m_undo_begun = false;
3981         m_selector.m_mouseMoved = false;
3982         m_selector.m_mouseMovedWhilePressed = false;
3983 }
3984 void onModifierDown( ModifierFlags type ){
3985         m_selector.modifierEnable( type );
3986 }
3987 void onModifierUp( ModifierFlags type ){
3988         m_selector.modifierDisable( type );
3989 }
3990 };
3991
3992
3993
3994 SelectionSystemWindowObserver* NewWindowObserver(){
3995         return new RadiantWindowObserver;
3996 }
3997
3998
3999
4000 #include "modulesystem/singletonmodule.h"
4001 #include "modulesystem/moduleregistry.h"
4002
4003 class SelectionDependencies :
4004         public GlobalSceneGraphModuleRef,
4005         public GlobalShaderCacheModuleRef,
4006         public GlobalOpenGLModuleRef
4007 {
4008 };
4009
4010 class SelectionAPI : public TypeSystemRef
4011 {
4012 SelectionSystem* m_selection;
4013 public:
4014 typedef SelectionSystem Type;
4015 STRING_CONSTANT( Name, "*" );
4016
4017 SelectionAPI(){
4018         SelectionSystem_Construct();
4019
4020         m_selection = &getSelectionSystem();
4021 }
4022 ~SelectionAPI(){
4023         SelectionSystem_Destroy();
4024 }
4025 SelectionSystem* getTable(){
4026         return m_selection;
4027 }
4028 };
4029
4030 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4031 typedef Static<SelectionModule> StaticSelectionModule;
4032 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );