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