]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/selection.cpp
menus...
[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 bool m_bPreferPointEntsIn2D;
2579 private:
2580 EManipulatorMode m_manipulator_mode;
2581 Manipulator* m_manipulator;
2582
2583 // state
2584 bool m_undo_begun;
2585 EMode m_mode;
2586 EComponentMode m_componentmode;
2587
2588 SelectionCounter m_count_primitive;
2589 SelectionCounter m_count_component;
2590
2591 TranslateManipulator m_translate_manipulator;
2592 RotateManipulator m_rotate_manipulator;
2593 ScaleManipulator m_scale_manipulator;
2594 DragManipulator m_drag_manipulator;
2595 ClipManipulator m_clip_manipulator;
2596
2597 typedef SelectionList<scene::Instance> selection_t;
2598 selection_t m_selection;
2599 selection_t m_component_selection;
2600
2601 Signal1<const Selectable&> m_selectionChanged_callbacks;
2602
2603 void ConstructPivot() const;
2604 void setCustomPivotOrigin( Vector3& point ) const;
2605 public:
2606 void getSelectionAABB( AABB& bounds ) const;
2607 private:
2608 mutable bool m_pivotChanged;
2609 bool m_pivot_moving;
2610 mutable bool m_pivotIsCustom;
2611
2612 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2613
2614 bool nothingSelected() const {
2615         return ( Mode() == eComponent && m_count_component.empty() )
2616                    || ( Mode() == ePrimitive && m_count_primitive.empty() );
2617 }
2618
2619
2620 public:
2621 enum EModifier
2622 {
2623         eManipulator,
2624         eToggle,
2625         eReplace,
2626         eCycle,
2627         eSelect,
2628         eDeselect,
2629 };
2630
2631 RadiantSelectionSystem() :
2632         m_bPreferPointEntsIn2D( true ),
2633         m_undo_begun( false ),
2634         m_mode( ePrimitive ),
2635         m_componentmode( eDefault ),
2636         m_count_primitive( SelectionChangedCaller( *this ) ),
2637         m_count_component( SelectionChangedCaller( *this ) ),
2638         m_translate_manipulator( *this, 2, 64 ),
2639         m_rotate_manipulator( *this, 8, 64 ),
2640         m_scale_manipulator( *this, 0, 64 ),
2641         m_pivotChanged( false ),
2642         m_pivot_moving( false ),
2643         m_pivotIsCustom( false ){
2644         SetManipulatorMode( eTranslate );
2645         pivotChanged();
2646         addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2647         AddGridChangeCallback( PivotChangedCaller( *this ) );
2648 }
2649 void pivotChanged() const {
2650         m_pivotChanged = true;
2651         SceneChangeNotify();
2652 }
2653 typedef ConstMemberCaller<RadiantSelectionSystem, &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2654 void pivotChangedSelection( const Selectable& selectable ){
2655         pivotChanged();
2656 }
2657 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2658
2659 void SetMode( EMode mode ){
2660         if ( m_mode != mode ) {
2661                 m_mode = mode;
2662                 pivotChanged();
2663         }
2664 }
2665 EMode Mode() const {
2666         return m_mode;
2667 }
2668 void SetComponentMode( EComponentMode mode ){
2669         m_componentmode = mode;
2670 }
2671 EComponentMode ComponentMode() const {
2672         return m_componentmode;
2673 }
2674 void SetManipulatorMode( EManipulatorMode mode ){
2675         m_pivotIsCustom = false;
2676         m_manipulator_mode = mode;
2677         switch ( m_manipulator_mode )
2678         {
2679         case eTranslate: m_manipulator = &m_translate_manipulator; break;
2680         case eRotate: m_manipulator = &m_rotate_manipulator; break;
2681         case eScale: m_manipulator = &m_scale_manipulator; break;
2682         case eDrag: m_manipulator = &m_drag_manipulator; break;
2683         case eClip: m_manipulator = &m_clip_manipulator; break;
2684         }
2685         pivotChanged();
2686 }
2687 EManipulatorMode ManipulatorMode() const {
2688         return m_manipulator_mode;
2689 }
2690
2691 SelectionChangeCallback getObserver( EMode mode ){
2692         if ( mode == ePrimitive ) {
2693                 return makeCallback1( m_count_primitive );
2694         }
2695         else
2696         {
2697                 return makeCallback1( m_count_component );
2698         }
2699 }
2700 std::size_t countSelected() const {
2701         return m_count_primitive.size();
2702 }
2703 std::size_t countSelectedComponents() const {
2704         return m_count_component.size();
2705 }
2706 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2707         if ( selectable.isSelected() ) {
2708                 m_selection.append( instance );
2709         }
2710         else
2711         {
2712                 m_selection.erase( instance );
2713         }
2714
2715         ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2716 }
2717 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2718         if ( selectable.isSelected() ) {
2719                 m_component_selection.append( instance );
2720         }
2721         else
2722         {
2723                 m_component_selection.erase( instance );
2724         }
2725
2726         ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2727 }
2728 scene::Instance& ultimateSelected() const {
2729         ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2730         return m_selection.back();
2731 }
2732 scene::Instance& penultimateSelected() const {
2733         ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2734         return *( *( --( --m_selection.end() ) ) );
2735 }
2736 void setSelectedAll( bool selected ){
2737         GlobalSceneGraph().traverse( select_all( selected ) );
2738
2739         m_manipulator->setSelected( selected );
2740 }
2741 void setSelectedAllComponents( bool selected ){
2742         Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2743         Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2744         Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2745
2746         m_manipulator->setSelected( selected );
2747 }
2748
2749 void foreachSelected( const Visitor& visitor ) const {
2750         selection_t::const_iterator i = m_selection.begin();
2751         while ( i != m_selection.end() )
2752         {
2753                 visitor.visit( *( *( i++ ) ) );
2754         }
2755 }
2756 void foreachSelectedComponent( const Visitor& visitor ) const {
2757         selection_t::const_iterator i = m_component_selection.begin();
2758         while ( i != m_component_selection.end() )
2759         {
2760                 visitor.visit( *( *( i++ ) ) );
2761         }
2762 }
2763
2764 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2765         m_selectionChanged_callbacks.connectLast( handler );
2766 }
2767 void selectionChanged( const Selectable& selectable ){
2768         m_selectionChanged_callbacks( selectable );
2769 }
2770 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2771
2772
2773 void startMove(){
2774         m_pivot2world_start = GetPivot2World();
2775 }
2776
2777 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2778         if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2779 #if defined ( DEBUG_SELECTION )
2780                 g_render_clipped.destroy();
2781 #endif
2782
2783                 m_manipulator->setSelected( false );
2784
2785                 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2786                         View scissored( view );
2787                         ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2788                         m_manipulator->testSelect( scissored, GetPivot2World() );
2789                 }
2790
2791                 startMove();
2792
2793                 m_pivot_moving = m_manipulator->isSelected();
2794
2795                 if ( m_pivot_moving ) {
2796                         Pivot2World pivot;
2797                         pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2798
2799                         m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2800
2801                         Matrix4 device2manip;
2802                         ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2803                         m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2804
2805                         m_undo_begun = false;
2806                 }
2807
2808                 SceneChangeNotify();
2809         }
2810
2811         return m_pivot_moving;
2812 }
2813
2814 void deselectAll(){
2815         if ( Mode() == eComponent ) {
2816                 setSelectedAllComponents( false );
2817         }
2818         else
2819         {
2820                 setSelectedAll( false );
2821         }
2822 }
2823
2824 void deselectComponentsOrAll( bool components ){
2825         if ( components ) {
2826                 setSelectedAllComponents( false );
2827         }
2828         else
2829         {
2830                 deselectAll();
2831         }
2832 }
2833
2834 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2835         //globalOutputStream() << device_point[0] << "   " << device_point[1] << "\n";
2836         ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2837
2838         if ( modifier == eReplace ) {
2839                 deselectComponentsOrAll( face );
2840         }
2841 /*
2842 //nothingSelected() doesn't consider faces, selected in non-component mode, m
2843         if ( modifier == eCycle && nothingSelected() ){
2844                 modifier = eReplace;
2845         }
2846 */
2847   #if defined ( DEBUG_SELECTION )
2848         g_render_clipped.destroy();
2849   #endif
2850
2851         {
2852                 View scissored( view );
2853                 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2854
2855                 SelectionVolume volume( scissored );
2856                 SelectionPool selector;
2857                 SelectionPool selector_point_ents;
2858                 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face
2859                         && ( modifier == RadiantSelectionSystem::eReplace || modifier == RadiantSelectionSystem::eSelect || modifier == RadiantSelectionSystem::eDeselect );
2860
2861                 if( prefer_point_ents ){
2862                         Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
2863                 }
2864                 if( prefer_point_ents && !selector_point_ents.failed() ){
2865                         switch ( modifier )
2866                         {
2867                         // if cycle mode not enabled, enable it
2868                         case RadiantSelectionSystem::eReplace:
2869                         {
2870                                 // select closest
2871                                 ( *selector_point_ents.begin() ).second->setSelected( true );
2872                         }
2873                         break;
2874                         case RadiantSelectionSystem::eSelect:
2875                         {
2876                                 SelectionPool::iterator best = selector_point_ents.begin();
2877                                 if( !( *best ).second->isSelected() ){
2878                                         ( *best ).second->setSelected( true );
2879                                 }
2880                                 SelectionPool::iterator i = best;
2881                                 ++i;
2882                                 while ( i != selector_point_ents.end() )
2883                                 {
2884                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2885                                                 if( !( *i ).second->isSelected() ){
2886                                                         ( *i ).second->setSelected( true );
2887                                                 }
2888                                         }
2889                                         else{
2890                                                 break;
2891                                         }
2892                                         ++i;
2893                                 }
2894                         }
2895                         break;
2896                         case RadiantSelectionSystem::eDeselect:
2897                         {
2898                                 SelectionPool::iterator best = selector_point_ents.begin();
2899                                 if( ( *best ).second->isSelected() ){
2900                                         ( *best ).second->setSelected( false );
2901                                 }
2902                                 SelectionPool::iterator i = best;
2903                                 ++i;
2904                                 while ( i != selector_point_ents.end() )
2905                                 {
2906                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2907                                                 if( ( *i ).second->isSelected() ){
2908                                                         ( *i ).second->setSelected( false );
2909                                                 }
2910                                         }
2911                                         else{
2912                                                 break;
2913                                         }
2914                                         ++i;
2915                                 }
2916                         }
2917                         break;
2918                         default:
2919                                 break;
2920                         }
2921                 }
2922                 else{
2923                         if ( face ){
2924                                 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2925                         }
2926                         else{
2927                                 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2928                         }
2929
2930                         if ( !selector.failed() ) {
2931                                 switch ( modifier )
2932                                 {
2933                                 case RadiantSelectionSystem::eToggle:
2934                                 {
2935                                         SelectableSortedSet::iterator best = selector.begin();
2936                                         // toggle selection of the object with least depth
2937                                         if ( ( *best ).second->isSelected() ) {
2938                                                 ( *best ).second->setSelected( false );
2939                                         }
2940                                         else{
2941                                                 ( *best ).second->setSelected( true );
2942                                         }
2943                                 }
2944                                 break;
2945                                 // if cycle mode not enabled, enable it
2946                                 case RadiantSelectionSystem::eReplace:
2947                                 {
2948                                         // select closest
2949                                         ( *selector.begin() ).second->setSelected( true );
2950                                 }
2951                                 break;
2952                                 // select the next object in the list from the one already selected
2953                                 case RadiantSelectionSystem::eCycle:
2954                                 {
2955                                         bool CycleSelectionOccured = false;
2956                                         SelectionPool::iterator i = selector.begin();
2957                                         while ( i != selector.end() )
2958                                         {
2959                                                 if ( ( *i ).second->isSelected() ) {
2960                                                         deselectComponentsOrAll( face );
2961                                                         ++i;
2962                                                         if ( i != selector.end() ) {
2963                                                                 i->second->setSelected( true );
2964                                                         }
2965                                                         else
2966                                                         {
2967                                                                 selector.begin()->second->setSelected( true );
2968                                                         }
2969                                                         CycleSelectionOccured = true;
2970                                                         break;
2971                                                 }
2972                                                 ++i;
2973                                         }
2974                                         if( !CycleSelectionOccured ){
2975                                                 deselectComponentsOrAll( face );
2976                                                 ( *selector.begin() ).second->setSelected( true );
2977                                         }
2978                                 }
2979                                 break;
2980                                 case RadiantSelectionSystem::eSelect:
2981                                 {
2982                                         SelectionPool::iterator best = selector.begin();
2983                                         if( !( *best ).second->isSelected() ){
2984                                                 ( *best ).second->setSelected( true );
2985                                         }
2986                                         SelectionPool::iterator i = best;
2987                                         ++i;
2988                                         while ( i != selector.end() )
2989                                         {
2990                                                 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2991                                                         if( !( *i ).second->isSelected() ){
2992                                                                 ( *i ).second->setSelected( true );
2993                                                         }
2994                                                 }
2995                                                 else{
2996                                                         break;
2997                                                 }
2998                                                 ++i;
2999                                         }
3000                                 }
3001                                 break;
3002                                 case RadiantSelectionSystem::eDeselect:
3003                                 {
3004                                         SelectionPool::iterator best = selector.begin();
3005                                         if( ( *best ).second->isSelected() ){
3006                                                 ( *best ).second->setSelected( false );
3007                                         }
3008                                         SelectionPool::iterator i = best;
3009                                         ++i;
3010                                         while ( i != selector.end() )
3011                                         {
3012                                                 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3013                                                         if( ( *i ).second->isSelected() ){
3014                                                                 ( *i ).second->setSelected( false );
3015                                                         }
3016                                                 }
3017                                                 else{
3018                                                         break;
3019                                                 }
3020                                                 ++i;
3021                                         }
3022                                 }
3023                                 break;
3024                                 default:
3025                                         break;
3026                                 }
3027                         }
3028                         else if( modifier == eCycle ){
3029                                 deselectComponentsOrAll( face );
3030                         }
3031                 }
3032         }
3033 }
3034
3035 bool SelectPoint_InitPaint( const View& view, const float device_point[2], const float device_epsilon[2], bool face ){
3036         ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
3037   #if defined ( DEBUG_SELECTION )
3038         g_render_clipped.destroy();
3039   #endif
3040
3041         {
3042                 View scissored( view );
3043                 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
3044
3045                 SelectionVolume volume( scissored );
3046                 SelectionPool selector;
3047                 SelectionPool selector_point_ents;
3048                 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face;
3049
3050                 if( prefer_point_ents ){
3051                         Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
3052                 }
3053                 if( prefer_point_ents && !selector_point_ents.failed() ){
3054                         SelectableSortedSet::iterator best = selector_point_ents.begin();
3055                         const bool wasSelected = ( *best ).second->isSelected();
3056                         ( *best ).second->setSelected( !wasSelected );
3057                         SelectableSortedSet::iterator i = best;
3058                         ++i;
3059                         while ( i != selector_point_ents.end() )
3060                         {
3061                                 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3062                                         ( *i ).second->setSelected( !wasSelected );
3063                                 }
3064                                 else{
3065                                         break;
3066                                 }
3067                                 ++i;
3068                         }
3069                         return !wasSelected;
3070                 }
3071                 else{//do primitives, if ents failed
3072                         if ( face ){
3073                                 Scene_TestSelect_Component( selector, volume, scissored, eFace );
3074                         }
3075                         else{
3076                                 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
3077                         }
3078                         if ( !selector.failed() ){
3079                                 SelectableSortedSet::iterator best = selector.begin();
3080                                 const bool wasSelected = ( *best ).second->isSelected();
3081                                 ( *best ).second->setSelected( !wasSelected );
3082                                 SelectableSortedSet::iterator i = best;
3083                                 ++i;
3084                                 while ( i != selector.end() )
3085                                 {
3086                                         if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3087                                                 ( *i ).second->setSelected( !wasSelected );
3088                                         }
3089                                         else{
3090                                                 break;
3091                                         }
3092                                         ++i;
3093                                 }
3094                                 return !wasSelected;
3095                         }
3096                         else{
3097                                 return true;
3098                         }
3099                 }
3100         }
3101 }
3102
3103 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3104         if ( modifier == eReplace ) {
3105                 deselectComponentsOrAll( face );
3106         }
3107
3108   #if defined ( DEBUG_SELECTION )
3109         g_render_clipped.destroy();
3110   #endif
3111
3112         {
3113                 View scissored( view );
3114                 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
3115
3116                 SelectionVolume volume( scissored );
3117                 SelectionPool pool;
3118                 if ( face ) {
3119                         Scene_TestSelect_Component( pool, volume, scissored, eFace );
3120                 }
3121                 else
3122                 {
3123                         Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
3124                 }
3125
3126                 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
3127                 {
3128                         ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
3129                 }
3130         }
3131 }
3132
3133
3134 void translate( const Vector3& translation ){
3135         if ( !nothingSelected() ) {
3136                 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3137
3138                 m_translation = translation;
3139
3140                 m_pivot2world = m_pivot2world_start;
3141                 matrix4_translate_by_vec3( m_pivot2world, translation );
3142
3143                 if ( Mode() == eComponent ) {
3144                         Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
3145                 }
3146                 else
3147                 {
3148                         Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
3149                 }
3150
3151                 SceneChangeNotify();
3152         }
3153 }
3154 void outputTranslation( TextOutputStream& ostream ){
3155         ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3156 }
3157 void rotate( const Quaternion& rotation ){
3158         if ( !nothingSelected() ) {
3159                 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3160
3161                 m_rotation = rotation;
3162
3163                 if ( Mode() == eComponent ) {
3164                         Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3165
3166                         matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3167                 }
3168                 else
3169                 {
3170                         Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3171
3172                         matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3173                 }
3174
3175                 SceneChangeNotify();
3176         }
3177 }
3178 void outputRotation( TextOutputStream& ostream ){
3179         ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3180 }
3181 void scale( const Vector3& scaling ){
3182         if ( !nothingSelected() ) {
3183                 m_scale = scaling;
3184
3185                 if ( Mode() == eComponent ) {
3186                         Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3187                 }
3188                 else
3189                 {
3190                         Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3191                 }
3192
3193                 SceneChangeNotify();
3194         }
3195 }
3196 void outputScale( TextOutputStream& ostream ){
3197         ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3198 }
3199
3200 void rotateSelected( const Quaternion& rotation ){
3201         startMove();
3202         rotate( rotation );
3203         freezeTransforms();
3204 }
3205 void translateSelected( const Vector3& translation ){
3206         startMove();
3207         translate( translation );
3208         freezeTransforms();
3209 }
3210 void scaleSelected( const Vector3& scaling ){
3211         startMove();
3212         scale( scaling );
3213         freezeTransforms();
3214 }
3215
3216 void MoveSelected( const View& view, const float device_point[2] ){
3217         if ( m_manipulator->isSelected() ) {
3218                 if ( !m_undo_begun ) {
3219                         m_undo_begun = true;
3220                         GlobalUndoSystem().start();
3221                 }
3222
3223                 Matrix4 device2manip;
3224                 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3225                 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1] );
3226         }
3227 }
3228
3229 /// \todo Support view-dependent nudge.
3230 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
3231         if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
3232                 translateSelected( nudge );
3233         }
3234 }
3235
3236 void endMove();
3237 void freezeTransforms();
3238
3239 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
3240 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3241         renderSolid( renderer, volume );
3242 }
3243
3244 const Matrix4& GetPivot2World() const {
3245         ConstructPivot();
3246         return m_pivot2world;
3247 }
3248
3249 static void constructStatic(){
3250         m_state = GlobalShaderCache().capture( "$POINT" );
3251   #if defined( DEBUG_SELECTION )
3252         g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3253   #endif
3254         TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3255         TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3256         RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3257 }
3258
3259 static void destroyStatic(){
3260   #if defined( DEBUG_SELECTION )
3261         GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3262   #endif
3263         GlobalShaderCache().release( "$WIRE_OVERLAY" );
3264         GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3265         GlobalShaderCache().release( "$WIRE_OVERLAY" );
3266         GlobalShaderCache().release( "$POINT" );
3267 }
3268 };
3269
3270 Shader* RadiantSelectionSystem::m_state = 0;
3271
3272
3273 namespace
3274 {
3275 RadiantSelectionSystem* g_RadiantSelectionSystem;
3276
3277 inline RadiantSelectionSystem& getSelectionSystem(){
3278         return *g_RadiantSelectionSystem;
3279 }
3280 }
3281
3282 #include "map.h"
3283
3284 class testselect_entity_visible : public scene::Graph::Walker
3285 {
3286 Selector& m_selector;
3287 SelectionTest& m_test;
3288 public:
3289 testselect_entity_visible( Selector& selector, SelectionTest& test )
3290         : m_selector( selector ), m_test( test ){
3291 }
3292 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3293         if( path.top().get_pointer() == Map_GetWorldspawn( g_map ) ||
3294                 node_is_group( path.top().get() ) ){
3295                 return false;
3296         }
3297         Selectable* selectable = Instance_getSelectable( instance );
3298         if ( selectable != 0
3299                  && Node_isEntity( path.top() ) ) {
3300                 m_selector.pushSelectable( *selectable );
3301         }
3302
3303         SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3304         if ( selectionTestable ) {
3305                 selectionTestable->testSelect( m_selector, m_test );
3306         }
3307
3308         return true;
3309 }
3310 void post( const scene::Path& path, scene::Instance& instance ) const {
3311         Selectable* selectable = Instance_getSelectable( instance );
3312         if ( selectable != 0
3313                  && Node_isEntity( path.top() ) ) {
3314                 m_selector.popSelectable();
3315         }
3316 }
3317 };
3318
3319 class testselect_primitive_visible : public scene::Graph::Walker
3320 {
3321 Selector& m_selector;
3322 SelectionTest& m_test;
3323 public:
3324 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3325         : m_selector( selector ), m_test( test ){
3326 }
3327 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3328         Selectable* selectable = Instance_getSelectable( instance );
3329         if ( selectable != 0 ) {
3330                 m_selector.pushSelectable( *selectable );
3331         }
3332
3333         SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3334         if ( selectionTestable ) {
3335                 selectionTestable->testSelect( m_selector, m_test );
3336         }
3337
3338         return true;
3339 }
3340 void post( const scene::Path& path, scene::Instance& instance ) const {
3341         Selectable* selectable = Instance_getSelectable( instance );
3342         if ( selectable != 0 ) {
3343                 m_selector.popSelectable();
3344         }
3345 }
3346 };
3347
3348 class testselect_component_visible : public scene::Graph::Walker
3349 {
3350 Selector& m_selector;
3351 SelectionTest& m_test;
3352 SelectionSystem::EComponentMode m_mode;
3353 public:
3354 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3355         : m_selector( selector ), m_test( test ), m_mode( mode ){
3356 }
3357 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3358         ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3359         if ( componentSelectionTestable ) {
3360                 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3361         }
3362
3363         return true;
3364 }
3365 };
3366
3367
3368 class testselect_component_visible_selected : public scene::Graph::Walker
3369 {
3370 Selector& m_selector;
3371 SelectionTest& m_test;
3372 SelectionSystem::EComponentMode m_mode;
3373 public:
3374 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3375         : m_selector( selector ), m_test( test ), m_mode( mode ){
3376 }
3377 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3378         Selectable* selectable = Instance_getSelectable( instance );
3379         if ( selectable != 0 && selectable->isSelected() ) {
3380                 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3381                 if ( componentSelectionTestable ) {
3382                         componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3383                 }
3384         }
3385
3386         return true;
3387 }
3388 };
3389
3390 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3391         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3392 }
3393
3394 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3395         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3396 }
3397
3398 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3399         Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3400 }
3401
3402 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3403         switch ( mode )
3404         {
3405         case eEntity:
3406         {
3407                 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3408         }
3409         break;
3410         case ePrimitive:
3411                 Scene_TestSelect_Primitive( selector, test, view );
3412                 break;
3413         case eComponent:
3414                 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3415                 break;
3416         }
3417 }
3418
3419 class FreezeTransforms : public scene::Graph::Walker
3420 {
3421 public:
3422 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3423         TransformNode* transformNode = Node_getTransformNode( path.top() );
3424         if ( transformNode != 0 ) {
3425                 Transformable* transform = Instance_getTransformable( instance );
3426                 if ( transform != 0 ) {
3427                         transform->freezeTransform();
3428                 }
3429         }
3430         return true;
3431 }
3432 };
3433
3434 void RadiantSelectionSystem::freezeTransforms(){
3435         GlobalSceneGraph().traverse( FreezeTransforms() );
3436 }
3437
3438
3439 void RadiantSelectionSystem::endMove(){
3440         freezeTransforms();
3441
3442         if ( Mode() == ePrimitive ) {
3443                 if ( ManipulatorMode() == eDrag ) {
3444                         Scene_SelectAll_Component( false, SelectionSystem::eFace );
3445                 }
3446         }
3447
3448         m_pivot_moving = false;
3449         pivotChanged();
3450
3451         SceneChangeNotify();
3452
3453         if ( m_undo_begun ) {
3454                 StringOutputStream command;
3455
3456                 if ( ManipulatorMode() == eTranslate ) {
3457                         command << "translateTool";
3458                         outputTranslation( command );
3459                 }
3460                 else if ( ManipulatorMode() == eRotate ) {
3461                         command << "rotateTool";
3462                         outputRotation( command );
3463                 }
3464                 else if ( ManipulatorMode() == eScale ) {
3465                         command << "scaleTool";
3466                         outputScale( command );
3467                 }
3468                 else if ( ManipulatorMode() == eDrag ) {
3469                         command << "dragTool";
3470                 }
3471
3472                 GlobalUndoSystem().finish( command.c_str() );
3473         }
3474
3475 }
3476
3477 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3478         Entity* entity = Node_getEntity( instance.path().top() );
3479         if ( entity != 0
3480                  && ( entity->getEntityClass().fixedsize
3481                           || !node_is_group( instance.path().top() ) ) ) {
3482                 Editable* editable = Node_getEditable( instance.path().top() );
3483                 if ( editable != 0 ) {
3484                         return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3485                 }
3486                 else
3487                 {
3488                         return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3489                 }
3490         }
3491
3492         return instance.worldAABB();
3493 }
3494
3495 class bounds_selected : public scene::Graph::Walker
3496 {
3497 AABB& m_bounds;
3498 public:
3499 bounds_selected( AABB& bounds )
3500         : m_bounds( bounds ){
3501         m_bounds = AABB();
3502 }
3503 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3504         Selectable* selectable = Instance_getSelectable( instance );
3505         if ( selectable != 0
3506                  && selectable->isSelected() ) {
3507                 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3508         }
3509         return true;
3510 }
3511 };
3512
3513 class bounds_selected_component : public scene::Graph::Walker
3514 {
3515 AABB& m_bounds;
3516 public:
3517 bounds_selected_component( AABB& bounds )
3518         : m_bounds( bounds ){
3519         m_bounds = AABB();
3520 }
3521 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3522         Selectable* selectable = Instance_getSelectable( instance );
3523         if ( selectable != 0
3524                  && selectable->isSelected() ) {
3525                 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3526                 if ( componentEditable ) {
3527                         aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3528                 }
3529         }
3530         return true;
3531 }
3532 };
3533
3534 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3535         graph.traverse( bounds_selected( bounds ) );
3536 }
3537
3538 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3539         graph.traverse( bounds_selected_component( bounds ) );
3540 }
3541
3542 #if 0
3543 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3544         ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3545         if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3546                  && componentEditable != 0 ) {
3547                 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3548         }
3549         else
3550         {
3551                 Bounded* bounded = Instance_getBounded( instance );
3552                 if ( bounded != 0 ) {
3553                         pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3554                 }
3555                 else
3556                 {
3557                         pivot = g_matrix4_identity;
3558                 }
3559         }
3560 }
3561 #endif
3562
3563 void RadiantSelectionSystem::ConstructPivot() const {
3564         if ( !m_pivotChanged || m_pivot_moving || m_pivotIsCustom ) {
3565                 return;
3566         }
3567         m_pivotChanged = false;
3568
3569         Vector3 m_object_pivot;
3570
3571         if ( !nothingSelected() ) {
3572                 {
3573                         AABB bounds;
3574                         if ( Mode() == eComponent ) {
3575                                 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3576                         }
3577                         else
3578                         {
3579                                 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3580                         }
3581                         m_object_pivot = bounds.origin;
3582                 }
3583
3584                 //vector3_snap( m_object_pivot, GetSnapGridSize() );
3585                 //globalOutputStream() << m_object_pivot << "\n";
3586                 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3587
3588                 switch ( m_manipulator_mode )
3589                 {
3590                 case eTranslate:
3591                         break;
3592                 case eRotate:
3593                         if ( Mode() == eComponent ) {
3594                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3595                         }
3596                         else
3597                         {
3598                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3599                         }
3600                         break;
3601                 case eScale:
3602                         if ( Mode() == eComponent ) {
3603                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3604                         }
3605                         else
3606                         {
3607                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3608                         }
3609                         break;
3610                 default:
3611                         break;
3612                 }
3613         }
3614 }
3615
3616 void RadiantSelectionSystem::setCustomPivotOrigin( Vector3& point ) const {
3617         /*if ( !m_pivotChanged || m_pivot_moving ) {
3618                 return;
3619         }*/
3620         //m_pivotChanged = false;
3621
3622         if ( !nothingSelected() && ( m_manipulator_mode == eTranslate || m_manipulator_mode == eRotate || m_manipulator_mode == eScale ) ) {
3623                 AABB bounds;
3624                 if ( Mode() == eComponent ) {
3625                         Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3626                 }
3627                 else
3628                 {
3629                         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3630                 }
3631                 //globalOutputStream() << point << "\n";
3632                 const float gridsize = GetSnapGridSize();
3633                 //const float bbox_epsilon = gridsize / 4.0;
3634
3635                 for( std::size_t i = 0; i < 3; i++ ){
3636                         if( point[i] < 900000 ){
3637                                 float bestsnapDist = fabs( bounds.origin[i] - point[i] );
3638                                 float bestsnapTo = bounds.origin[i];
3639                                 float othersnapDist = fabs( bounds.origin[i] + bounds.extents[i] - point[i] );
3640                                 if( othersnapDist < bestsnapDist ){
3641                                         bestsnapDist = othersnapDist;
3642                                         bestsnapTo = bounds.origin[i] + bounds.extents[i];
3643                                 }
3644                                 othersnapDist = fabs( bounds.origin[i] - bounds.extents[i] - point[i] );
3645                                 if( othersnapDist < bestsnapDist ){
3646                                         bestsnapDist = othersnapDist;
3647                                         bestsnapTo = bounds.origin[i] - bounds.extents[i];
3648                                 }
3649                                 othersnapDist = fabs( float_snapped( point[i], gridsize ) - point[i] );
3650                                 if( othersnapDist < bestsnapDist ){
3651                                         bestsnapDist = othersnapDist;
3652                                         bestsnapTo = float_snapped( point[i], gridsize );
3653                                 }
3654                                 point[i] = bestsnapTo;
3655
3656 /*                              if( float_equal_epsilon( point[i], bestsnapTo, bbox_epsilon ) ){
3657                                         point[i] = bestsnapTo;
3658                                 }
3659                                 else{
3660                                         point[i] = float_snapped( point[i], gridsize );
3661                                 }
3662                                 */
3663                                 m_pivot2world[i + 12] = point[i]; //m_pivot2world.tx() .ty() .tz()
3664                         }
3665                 }
3666
3667                 switch ( m_manipulator_mode )
3668                 {
3669                 case eTranslate:
3670                         break;
3671                 case eRotate:
3672                         if ( Mode() == eComponent ) {
3673                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3674                         }
3675                         else
3676                         {
3677                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3678                         }
3679                         break;
3680                 case eScale:
3681                         if ( Mode() == eComponent ) {
3682                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3683                         }
3684                         else
3685                         {
3686                                 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3687                         }
3688                         break;
3689                 default:
3690                         break;
3691                 }
3692         }
3693         m_pivotIsCustom = true;
3694 }
3695
3696 void RadiantSelectionSystem::getSelectionAABB( AABB& bounds ) const {
3697         if ( !nothingSelected() ) {
3698                 if ( Mode() == eComponent ) {
3699                         Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3700                 }
3701                 else
3702                 {
3703                         Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3704                 }
3705         }
3706 }
3707
3708 void GetSelectionAABB( AABB& bounds ){
3709         getSelectionSystem().getSelectionAABB( bounds );
3710 }
3711
3712 const Matrix4& ssGetPivot2World(){
3713         return getSelectionSystem().GetPivot2World();
3714 }
3715
3716 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3717         //if(view->TestPoint(m_object_pivot))
3718         if ( !nothingSelected() ) {
3719                 renderer.Highlight( Renderer::ePrimitive, false );
3720                 renderer.Highlight( Renderer::eFace, false );
3721
3722                 renderer.SetState( m_state, Renderer::eWireframeOnly );
3723                 renderer.SetState( m_state, Renderer::eFullMaterials );
3724
3725                 m_manipulator->render( renderer, volume, GetPivot2World() );
3726         }
3727
3728 #if defined( DEBUG_SELECTION )
3729         renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3730         renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3731         renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3732 #endif
3733 }
3734
3735 #include "preferencesystem.h"
3736 #include "preferences.h"
3737
3738 void SelectionSystem_constructPreferences( PreferencesPage& page ){
3739         page.appendCheckBox( "", "Prefer point entities in 2D", getSelectionSystem().m_bPreferPointEntsIn2D );
3740 }
3741 void SelectionSystem_constructPage( PreferenceGroup& group ){
3742         PreferencesPage page( group.createPage( "Selection", "Selection System Settings" ) );
3743         SelectionSystem_constructPreferences( page );
3744 }
3745 void SelectionSystem_registerPreferencesPage(){
3746         PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, SelectionSystem_constructPage>() );
3747 }
3748
3749
3750
3751 void SelectionSystem_OnBoundsChanged(){
3752         getSelectionSystem().pivotChanged();
3753 }
3754
3755 SignalHandlerId SelectionSystem_boundsChanged;
3756
3757 void SelectionSystem_Construct(){
3758         RadiantSelectionSystem::constructStatic();
3759
3760         g_RadiantSelectionSystem = new RadiantSelectionSystem;
3761
3762         SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<SelectionSystem_OnBoundsChanged>() );
3763
3764         GlobalShaderCache().attachRenderable( getSelectionSystem() );
3765
3766         GlobalPreferenceSystem().registerPreference( "PreferPointEntsIn2D", BoolImportStringCaller( getSelectionSystem().m_bPreferPointEntsIn2D ), BoolExportStringCaller( getSelectionSystem().m_bPreferPointEntsIn2D ) );
3767         SelectionSystem_registerPreferencesPage();
3768 }
3769
3770 void SelectionSystem_Destroy(){
3771         GlobalShaderCache().detachRenderable( getSelectionSystem() );
3772
3773         GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3774
3775         delete g_RadiantSelectionSystem;
3776
3777         RadiantSelectionSystem::destroyStatic();
3778 }
3779
3780
3781
3782
3783 inline float screen_normalised( float pos, std::size_t size ){
3784         return ( ( 2.0f * pos ) / size ) - 1.0f;
3785 }
3786
3787 typedef Vector2 DeviceVector;
3788
3789 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3790         return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3791 }
3792
3793 inline float device_constrained( float pos ){
3794         return std::min( 1.0f, std::max( -1.0f, pos ) );
3795 }
3796
3797 inline DeviceVector device_constrained( DeviceVector device ){
3798         return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3799 }
3800
3801 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3802         return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3803 }
3804
3805 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3806         return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3807 }
3808
3809 typedef Callback1<DeviceVector> MouseEventCallback;
3810
3811 Single<MouseEventCallback> g_mouseMovedCallback;
3812 Single<MouseEventCallback> g_mouseUpCallback;
3813
3814 #if 1
3815 const ButtonIdentifier c_button_select = c_buttonLeft;
3816 const ButtonIdentifier c_button_select2 = c_buttonRight;
3817 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3818 const ModifierFlags c_modifier_toggle = c_modifierShift;
3819 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3820 const ModifierFlags c_modifier_face = c_modifierControl;
3821 #else
3822 const ButtonIdentifier c_button_select = c_buttonLeft;
3823 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3824 const ModifierFlags c_modifier_toggle = c_modifierControl;
3825 const ModifierFlags c_modifier_replace = c_modifierNone;
3826 const ModifierFlags c_modifier_face = c_modifierShift;
3827 #endif
3828 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3829 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3830
3831 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3832 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3833 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3834 const ModifierFlags c_modifier_apply_texture3 =                     c_modifierShift;
3835 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3836
3837 class Selector_
3838 {
3839 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3840         if ( ( state == c_modifier_toggle || state == c_modifier_toggle_face || state == c_modifier_face ) ) {
3841                 if( m_mouse2 ){
3842                         return RadiantSelectionSystem::eReplace;
3843                 }
3844                 else{
3845                         return RadiantSelectionSystem::eToggle;
3846                 }
3847         }
3848         return RadiantSelectionSystem::eManipulator;
3849 }
3850
3851 rect_t getDeviceArea() const {
3852         DeviceVector delta( m_current - m_start );
3853         if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3854                 return SelectionBoxForArea( &m_start[0], &delta[0] );
3855         }
3856         else
3857         {
3858                 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3859                 return default_area;
3860         }
3861 }
3862
3863 public:
3864 DeviceVector m_start;
3865 DeviceVector m_current;
3866 DeviceVector m_epsilon;
3867 ModifierFlags m_state;
3868 bool m_mouse2;
3869 bool m_mouseMoved;
3870 bool m_mouseMovedWhilePressed;
3871 bool m_paintSelect;
3872 const View* m_view;
3873 RectangleCallback m_window_update;
3874
3875 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 ){
3876 }
3877
3878 void draw_area(){
3879         m_window_update( getDeviceArea() );
3880 }
3881
3882 void testSelect( DeviceVector position ){
3883         RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3884         if ( modifier != RadiantSelectionSystem::eManipulator ) {
3885                 DeviceVector delta( position - m_start );
3886                 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3887                         DeviceVector delta( position - m_start );
3888                         //getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3889                         getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], RadiantSelectionSystem::eToggle, ( m_state & c_modifier_face ) != c_modifierNone );
3890                 }
3891                 else if( !m_mouseMovedWhilePressed ){
3892                         if ( modifier == RadiantSelectionSystem::eReplace && !m_mouseMoved ) {
3893                                 modifier = RadiantSelectionSystem::eCycle;
3894                         }
3895                         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3896                 }
3897         }
3898
3899         m_start = m_current = DeviceVector( 0.0f, 0.0f );
3900         draw_area();
3901 }
3902
3903 void testSelect_simpleM1( DeviceVector position ){
3904         /*RadiantSelectionSystem::EModifier modifier = RadiantSelectionSystem::eReplace;
3905         DeviceVector delta( position - m_start );
3906         if ( fabs( delta.x() ) < m_epsilon.x() && fabs( delta.y() ) < m_epsilon.y() ) {
3907                 modifier = RadiantSelectionSystem::eCycle;
3908         }
3909         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, false );*/
3910         getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], m_mouseMoved ? RadiantSelectionSystem::eReplace : RadiantSelectionSystem::eCycle, false );
3911         m_start = m_current = device_constrained( position );
3912 }
3913
3914
3915 bool selecting() const {
3916         return m_state != c_modifier_manipulator && m_mouse2;
3917 }
3918
3919 void setState( ModifierFlags state ){
3920         bool was_selecting = selecting();
3921         m_state = state;
3922         if ( was_selecting ^ selecting() ) {
3923                 draw_area();
3924         }
3925 }
3926
3927 ModifierFlags getState() const {
3928         return m_state;
3929 }
3930
3931 void modifierEnable( ModifierFlags type ){
3932         setState( bitfield_enable( getState(), type ) );
3933 }
3934 void modifierDisable( ModifierFlags type ){
3935         setState( bitfield_disable( getState(), type ) );
3936 }
3937
3938 void mouseDown( DeviceVector position ){
3939         m_start = m_current = device_constrained( position );
3940         if( !m_mouse2 && m_state != c_modifierNone ){
3941                 m_paintSelect = getSelectionSystem().SelectPoint_InitPaint( *m_view, &position[0], &m_epsilon[0], ( m_state & c_modifier_face ) != c_modifierNone );
3942         }
3943 }
3944
3945 void mouseMoved( DeviceVector position ){
3946         m_current = device_constrained( position );
3947         m_mouseMovedWhilePressed = true;
3948         if( m_mouse2 ){
3949                 draw_area();
3950         }
3951         else if( m_state != c_modifier_manipulator ){
3952                 getSelectionSystem().SelectPoint( *m_view, &m_current[0], &m_epsilon[0],
3953                                                                                 m_paintSelect ? RadiantSelectionSystem::eSelect : RadiantSelectionSystem::eDeselect,
3954                                                                                 ( m_state & c_modifier_face ) != c_modifierNone );
3955         }
3956 }
3957 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseMoved> MouseMovedCaller;
3958
3959 void mouseUp( DeviceVector position ){
3960         if( m_mouse2 ){
3961                 testSelect( device_constrained( position ) );
3962         }
3963         else{
3964                 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3965         }
3966
3967         g_mouseMovedCallback.clear();
3968         g_mouseUpCallback.clear();
3969 }
3970 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseUp> MouseUpCaller;
3971 };
3972
3973
3974 class Manipulator_
3975 {
3976 public:
3977 DeviceVector m_epsilon;
3978 const View* m_view;
3979
3980 bool mouseDown( DeviceVector position ){
3981         return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
3982 }
3983
3984 void mouseMoved( DeviceVector position ){
3985         getSelectionSystem().MoveSelected( *m_view, &position[0] );
3986 }
3987 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseMoved> MouseMovedCaller;
3988
3989 void mouseUp( DeviceVector position ){
3990         getSelectionSystem().endMove();
3991         g_mouseMovedCallback.clear();
3992         g_mouseUpCallback.clear();
3993 }
3994 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseUp> MouseUpCaller;
3995 };
3996
3997 void Scene_copyClosestTexture( SelectionTest& test );
3998 void Scene_applyClosestTexture( SelectionTest& test );
3999
4000 class RadiantWindowObserver : public SelectionSystemWindowObserver
4001 {
4002 enum
4003 {
4004         SELECT_EPSILON = 8,
4005 };
4006
4007 int m_width;
4008 int m_height;
4009
4010 bool m_mouse_down;
4011
4012 public:
4013 Selector_ m_selector;
4014 Manipulator_ m_manipulator;
4015
4016 RadiantWindowObserver() : m_mouse_down( false ){
4017 }
4018 void release(){
4019         delete this;
4020 }
4021 void setView( const View& view ){
4022         m_selector.m_view = &view;
4023         m_manipulator.m_view = &view;
4024 }
4025 void setRectangleDrawCallback( const RectangleCallback& callback ){
4026         m_selector.m_window_update = callback;
4027 }
4028 void onSizeChanged( int width, int height ){
4029         m_width = width;
4030         m_height = height;
4031         DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
4032         m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
4033 }
4034 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4035         if ( button == c_button_select || ( button == c_button_select2 && modifiers != c_modifierNone ) ) {
4036                 m_mouse_down = true;
4037                 //m_selector.m_mouseMoved = false;
4038
4039                 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
4040                 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
4041                         g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
4042                         g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
4043                 }
4044                 else
4045                 {
4046                         if ( button == c_button_select ) {
4047                                 m_selector.m_mouse2 = false;
4048                         }
4049                         else{
4050                                 m_selector.m_mouse2 = true;
4051                         }
4052                         m_selector.mouseDown( devicePosition );
4053                         g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
4054                         g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
4055                 }
4056         }
4057         else if ( button == c_button_texture ) {
4058                 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4059
4060                 View scissored( *m_selector.m_view );
4061                 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
4062                 SelectionVolume volume( scissored );
4063
4064                 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
4065                         Scene_applyClosestTexture( volume );
4066                 }
4067                 else if ( modifiers == c_modifier_copy_texture ) {
4068                         Scene_copyClosestTexture( volume );
4069                 }
4070         }
4071 }
4072 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
4073         m_selector.m_mouseMoved = true;
4074         if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
4075                 m_selector.m_mouseMovedWhilePressed = true;
4076                 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4077         }
4078 }
4079 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4080         if ( ( button == c_button_select || button == c_button_select2 ) && !g_mouseUpCallback.empty() ) {
4081                 m_mouse_down = false;
4082
4083                 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4084         }
4085         //L button w/o scene changed = tunnel selection
4086         if( // !getSelectionSystem().m_undo_begun &&
4087                 modifiers == c_modifierNone && button == c_button_select &&
4088                 //( !m_selector.m_mouseMoved || !m_mouse_down ) &&
4089                 !m_selector.m_mouseMovedWhilePressed &&
4090                 ( getSelectionSystem().Mode() != SelectionSystem::eComponent || getSelectionSystem().ManipulatorMode() != SelectionSystem::eDrag ) ){
4091                 m_selector.testSelect_simpleM1( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4092         }
4093         //getSelectionSystem().m_undo_begun = false;
4094         m_selector.m_mouseMoved = false;
4095         m_selector.m_mouseMovedWhilePressed = false;
4096 }
4097 void onModifierDown( ModifierFlags type ){
4098         m_selector.modifierEnable( type );
4099 }
4100 void onModifierUp( ModifierFlags type ){
4101         m_selector.modifierDisable( type );
4102 }
4103 };
4104
4105
4106
4107 SelectionSystemWindowObserver* NewWindowObserver(){
4108         return new RadiantWindowObserver;
4109 }
4110
4111
4112
4113 #include "modulesystem/singletonmodule.h"
4114 #include "modulesystem/moduleregistry.h"
4115
4116 class SelectionDependencies :
4117         public GlobalSceneGraphModuleRef,
4118         public GlobalShaderCacheModuleRef,
4119         public GlobalOpenGLModuleRef
4120 {
4121 };
4122
4123 class SelectionAPI : public TypeSystemRef
4124 {
4125 SelectionSystem* m_selection;
4126 public:
4127 typedef SelectionSystem Type;
4128 STRING_CONSTANT( Name, "*" );
4129
4130 SelectionAPI(){
4131         SelectionSystem_Construct();
4132
4133         m_selection = &getSelectionSystem();
4134 }
4135 ~SelectionAPI(){
4136         SelectionSystem_Destroy();
4137 }
4138 SelectionSystem* getTable(){
4139         return m_selection;
4140 }
4141 };
4142
4143 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4144 typedef Static<SelectionModule> StaticSelectionModule;
4145 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );