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