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