1#include "transformation_gizmo.h"
3#include <cgv/math/intersection.h>
4#include <cgv/math/constants.h>
11transformation_gizmo::transformation_gizmo() {
12 _boxes.style.illumination_mode = IM_OFF;
13 _boxes.style.map_color_to_material = CM_COLOR_AND_OPACITY;
14 _boxes.style.default_extent = _handle_size;
16 _cones.style.illumination_mode = IM_OFF;
17 _cones.style.map_color_to_material = CM_COLOR_AND_OPACITY;
18 _cones.style.radius = 0.02f;
20 _sphere.style.illumination_mode = IM_OFF;
21 _sphere.style.map_color_to_material = CM_COLOR_AND_OPACITY;
22 _sphere.style.halo_color = { 1.0f };
24 _rectangles.style.illumination_mode = IM_OFF;
25 _rectangles.style.map_color_to_material = CM_COLOR_AND_OPACITY;
28 for(
size_t i = 0; i < _ring_segment_count; ++i) {
29 float t =
static_cast<float>(i) /
static_cast<float>(_ring_segment_count - 1);
30 t *=
static_cast<float>(2.0 * cgv::math::constants::pi);
31 _ring_points.push_back({ std::cos(t), std::sin(t) });
33 _ring_points.back() = _ring_points.front();
39 success &= _box_renderer.
init(ctx);
40 success &= _cone_renderer.
init(ctx);
41 success &= _rectangle_renderer.
init(ctx);
42 success &= _sphere_renderer.
init(ctx);
44 success &= _boxes.init(ctx);
45 success &= _cones.init(ctx);
46 success &= _rectangles.init(ctx);
47 success &= _sphere.init(ctx);
53 _box_renderer.
clear(ctx);
54 _cone_renderer.
clear(ctx);
55 _rectangle_renderer.
clear(ctx);
56 _sphere_renderer.
clear(ctx);
60 _rectangles.destruct(ctx);
61 _sphere.destruct(ctx);
64transformation_gizmo::Mode transformation_gizmo::get_mode()
const {
68void transformation_gizmo::set_mode(Mode mode) {
70 _interaction_feature = InteractionFeature::kNone;
71 set_geometry_out_of_date();
75vec3 transformation_gizmo::get_scale()
const {
79void transformation_gizmo::set_scale(
const vec3& scale) {
84void transformation_gizmo::create_geometry() {
88 const vec3 vx(1.0f, 0.0f, 0.0f);
89 const vec3 vy(0.0f, 1.0f, 0.0f);
90 const vec3 vz(0.0f, 0.0f, 1.0f);
92 hls red =
rgb(1.0f, 0.0f, 0.0f);
93 hls green =
rgb(0.0f, 1.0f, 0.0f);
94 hls blue =
rgb(0.0f, 0.0f, 1.0f);
96 const float saturation = 0.9f;
97 const float lightness = 0.3f;
100 green.S() = saturation;
101 blue.S() = saturation;
103 green.L() = lightness;
104 blue.L() = lightness;
106 const rgb x_color = red;
107 const rgb y_color = green;
108 const rgb z_color = blue;
116 _mode == Mode::kTranslation ||
117 _mode == Mode::kScale ||
118 _mode == Mode::kModel;
120 bool has_translation =
121 _mode == Mode::kTranslation ||
122 _mode == Mode::kModel;
125 _mode == Mode::kRotation ||
126 _mode == Mode::kModel;
129 _mode == Mode::kScale ||
130 _mode == Mode::kModel;
133 float axis_length = 1.0f;
135 if(has_translation && has_scale)
136 axis_length -= 2.0f * _handle_size;
137 else if(has_translation)
138 axis_length -= 2.0f * _handle_size;
140 axis_length -= _handle_size;
142 vec3 hx = axis_length * vx;
143 vec3 hy = axis_length * vy;
144 vec3 hz = axis_length * vz;
146 _cones.add(v0 + _center_radius * vx, hx);
147 _cones.add(v0 + _center_radius * vy, hy);
148 _cones.add(v0 + _center_radius * vz, hz);
149 _cones.fill_radii(_axis_radius);
150 _cones.add_segment_color({ x_color, 1.0f });
151 _cones.add_segment_color({ y_color, 1.0f });
152 _cones.add_segment_color({ z_color, 1.0f });
154 float arrow_offset = has_scale ? 3.0f * _handle_size : 0.0f;
156 if(has_translation) {
158 _cones.add(hx + arrow_offset * vx, (1.0f + arrow_offset) * vx);
159 _cones.add(hy + arrow_offset * vy, (1.0f + arrow_offset) * vy);
160 _cones.add(hz + arrow_offset * vz, (1.0f + arrow_offset) * vz);
161 _cones.add(0.5f * _handle_size, 0.0f);
162 _cones.add(0.5f * _handle_size, 0.0f);
163 _cones.add(0.5f * _handle_size, 0.0f);
164 _cones.add_segment_color({ x_color, 1.0f });
165 _cones.add_segment_color({ y_color, 1.0f });
166 _cones.add_segment_color({ z_color, 1.0f });
171 float box_offset = axis_length + 0.5f * _handle_size;
173 _boxes.add_position(v0 + box_offset * vx);
174 _boxes.add_position(v0 + box_offset * vy);
175 _boxes.add_position(v0 + box_offset * vz);
176 _boxes.add_color({ x_color, 1.0f });
177 _boxes.add_color({ y_color, 1.0f });
178 _boxes.add_color({ z_color, 1.0f });
184 const auto add_ring = [
this](
auto transform,
const rgba& color) {
185 for(
size_t i = 0; i < _ring_points.size(); ++i)
186 _cones.add(transform(_ring_points[i]), transform(_ring_points[(i + 1) % _ring_points.size()]));
187 _cones.fill_colors(color);
190 add_ring([](
vec2 p) {
return vec3(0.0f, p.
x(), p.
y()); }, { x_color, 1.0f });
191 add_ring([](
vec2 p) {
return vec3(p.
x(), 0.0f, p.
y()); }, { y_color, 1.0f });
192 add_ring([](
vec2 p) {
return vec3(p.
x(), p.
y(), 0.0f); }, { z_color, 1.0f });
193 _cones.fill_radii(_axis_radius);
197 if(_mode == Mode::kTranslation || _mode == Mode::kScale) {
198 _rectangles.add_position(v0 + 0.5f * (vy + vz));
199 _rectangles.add_position(v0 + 0.5f * (vx + vz));
200 _rectangles.add_position(v0 + 0.5f * (vx + vy));
201 _rectangles.fill_extents(
vec2(_plane_size));
203 _rectangles.add_color({ x_color, 0.5f });
204 _rectangles.add_color({ y_color, 0.5f });
205 _rectangles.add_color({ z_color, 0.5f });
207 _rectangles.add_border_color({ x_color, 1.0f });
208 _rectangles.add_border_color({ y_color, 1.0f });
209 _rectangles.add_border_color({ z_color, 1.0f });
211 _rectangles.add_rotation(
quat(vy, cgv::math::deg2rad(90.0f)));
212 _rectangles.add_rotation(
quat(vx, cgv::math::deg2rad(-90.0f)));
213 _rectangles.add_rotation(
quat());
217 _sphere.add(v0, _axis_radius);
218 _sphere.colors.push_back({ 1.0f });
220 _sphere.add(v0, _center_radius);
221 _sphere.colors.push_back({ 0.7f, 0.7f, 0.7f, 0.0f });
227 const auto saturate_color = [](
rgba& color) {
231 color = {
rgb(hls), color.alpha() };
234 int axis_idx = axis_id_to_index(_interaction_axis_id);
235 switch(_interaction_feature) {
236 case InteractionFeature::kAxis:
238 size_t base_idx = 2 *
static_cast<size_t>(axis_idx);
239 if(has_translation && has_scale && _interaction_mode == Mode::kScale || has_translation != has_scale) {
240 saturate_color(_cones.colors[base_idx]);
241 saturate_color(_cones.colors[base_idx + 1]);
244 if(_interaction_mode == Mode::kTranslation) {
245 saturate_color(_cones.colors[base_idx + 6]);
246 saturate_color(_cones.colors[base_idx + 7]);
249 if(_interaction_mode == Mode::kScale)
250 saturate_color(_boxes.colors[axis_idx]);
253 case InteractionFeature::kPlane:
255 size_t base_idx =
static_cast<size_t>(axis_idx) * 2 * _ring_segment_count;
256 if(_mode == Mode::kModel)
259 for(
size_t i = 0; i < 2 * _ring_segment_count; ++i)
260 saturate_color(_cones.colors[base_idx + i]);
262 saturate_color(_rectangles.colors[axis_idx]);
263 saturate_color(_rectangles.border_colors[axis_idx]);
266 case InteractionFeature::kCenter:
267 _sphere.colors.back().alpha() = 0.2f;
274void transformation_gizmo::draw_geometry(
context& ctx) {
276 _rectangle_renderer.set_y_view_angle(
static_cast<float>(get_view()->get_y_view_angle()));
277 _sphere_renderer.set_y_view_angle(
static_cast<float>(get_view()->get_y_view_angle()));
281 const float size = get_size();
283 _sphere.style.halo_width_in_pixel = -3.0f / size;
284 _sphere.style.blend_width_in_pixel = 1.0f / size;
286 _rectangles.style.border_width_in_pixel = -3.0f / size;
287 _rectangles.style.pixel_blend = 2.0f / size;
289 _sphere.render(ctx, _sphere_renderer);
290 _rectangles.render(ctx, _rectangle_renderer);
291 _cones.render(ctx, _cone_renderer);
292 _boxes.render(ctx, _box_renderer);
295bool transformation_gizmo::intersect_bounding_box(
const cgv::math::ray3& ray) {
299 if(_mode == Mode::kRotation || _mode == Mode::kModel)
302 min -= _center_radius;
304 if(_mode == Mode::kModel)
305 max += 3.0f * _handle_size;
310 vec2 t = std::numeric_limits<float>::max();
311 return cgv::math::ray_box_intersection(ray, min, max, t) != 0;
315 float min_t = std::numeric_limits<float>::max();
317 const auto update_t_if_closer = [
this, &min_t](
float t, Mode transformation, InteractionFeature feature, AxisId axis_id) {
318 if(t >= 0.0f && t < min_t) {
320 _interaction_mode = transformation;
321 _interaction_feature = feature;
322 _interaction_axis_id = axis_id;
326 if(_mode == Mode::kRotation || _mode == Mode::kModel) {
328 size_t start_offset = _mode == Mode::kModel ? 12 : 0;
329 for(
size_t i = start_offset; i < _cones.size(); i += 2) {
330 vec3 pa = _cones.positions[i];
331 vec3 pb = _cones.positions[i + 1];
333 float t = std::numeric_limits<float>::max();
334 if(cgv::math::ray_cylinder_intersection2(ray, pa, pb, 3.0f * _axis_radius, t)) {
335 int axis_idx =
static_cast<int>((i - start_offset) / (2 * _ring_segment_count));
336 update_t_if_closer(t, Mode::kRotation, InteractionFeature::kPlane, index_to_axis_id(axis_idx));
342 std::array<std::pair<vec3, vec3>, 6> cylinders;
343 cylinders.fill({ 0.0f, 0.0f });
344 size_t cylinder_count = 0;
345 float axis_length = 1.0f;
347 if(_mode == Mode::kModel) {
348 axis_length -= _handle_size;
351 if(_mode == Mode::kTranslation || _mode == Mode::kScale || _mode == Mode::kModel) {
353 for(
unsigned i = 0; i < 3; ++i) {
354 cylinders[i].first[i] = _center_radius;
355 cylinders[i].second[i] = axis_length;
359 if(_mode == Mode::kModel) {
361 for(
int i = 0; i < 3; ++i) {
362 cylinders[i + 3].first[i] = axis_length + 2.0f * _handle_size;
363 cylinders[i + 3].second[i] = axis_length + 4.0f * _handle_size;
367 for(
size_t i = 0; i < cylinder_count; ++i) {
368 float t = std::numeric_limits<float>::max();
369 if(cgv::math::ray_cylinder_intersection2(ray, cylinders[i].first, cylinders[i].second, _handle_size, t)) {
370 Mode transformation = _mode;
371 if(cylinder_count > 3)
372 transformation = i < 3 ? Mode::kScale : Mode::kTranslation;
374 update_t_if_closer(t, transformation, InteractionFeature::kAxis, index_to_axis_id(
static_cast<int>(i % 3)));
379 if(_mode == Mode::kTranslation || _mode == Mode::kScale) {
380 float t = std::numeric_limits<float>::max();
381 for(
int i = 0; i < 3; ++i) {
382 vec3 position = { 0.5f };
384 if(cgv::math::ray_axis_aligned_rectangle_intersection(ray, position, { _plane_size },
static_cast<int>(i), t))
385 update_t_if_closer(t, _mode, InteractionFeature::kPlane, index_to_axis_id(
static_cast<int>(i)));
390 vec2 ts(std::numeric_limits<float>::max());
391 if(cgv::math::ray_sphere_intersection(ray, { 0.0f }, _center_radius, ts)) {
392 Mode transformation = _mode == Mode::kModel ? Mode::kTranslation : _mode;
393 update_t_if_closer(ts.x(), transformation, InteractionFeature::kCenter, AxisId::kX);
396 return min_t > 0.0f && min_t < std::numeric_limits<float>::max();
400 int axis_idx = axis_id_to_index(_interaction_axis_id);
401 vec3 axis = get_axis(axis_idx);
405 _interaction_plane.origin = get_position();
407 const auto get_rotated_axis = [
this](
const vec3& axis) {
409 get_rotation().
rotate(rotated);
413 if(_interaction_mode == Mode::kRotation) {
414 switch(_interaction_feature) {
415 case InteractionFeature::kPlane:
417 _interaction_plane.normal = get_rotated_axis(axis);
419 float threshold_angle = std::cos(cgv::math::deg2rad(75.0f));
421 float incident_angle = dot(_interaction_plane.normal, ray.direction);
422 if(std::abs(incident_angle) < threshold_angle)
423 _interaction_plane.normal = incident_angle < 0.0f ? -view_dir : view_dir;
426 case InteractionFeature::kCenter:
427 _interaction_plane.normal = view_dir;
433 switch(_interaction_feature) {
434 case InteractionFeature::kAxis:
438 _interaction_plane.normal = view_dir;
441 case InteractionFeature::kPlane:
442 _interaction_plane.normal = get_rotated_axis(axis);
444 case InteractionFeature::kCenter:
445 _interaction_plane.normal = view_dir;
453 if(!cgv::math::ray_plane_intersection(ray, _interaction_plane.origin, _interaction_plane.normal, t))
457 _drag_start_position = get_position();
458 _drag_start_scale = _scale;
459 _drag_start_rotation = get_rotation();
462 on_change(GizmoAction::kDragStart, _interaction_mode);
468 int axis_idx = axis_id_to_index(_interaction_axis_id);
469 vec3 axis = get_axis(axis_idx);
471 const vec3 position = get_position();
473 if(!_interaction_plane.valid())
477 if(!cgv::math::ray_plane_intersection(ray, _interaction_plane.origin, _interaction_plane.normal, t) || t < 0.0f) {
479 set_position(_drag_start_position);
480 set_scale(_drag_start_scale);
481 set_rotation(_drag_start_rotation);
483 vec3 start_intersection_position = _drag_start_ray.
position(_drag_start_t);
486 if(get_orientation() == GizmoOrientation::kLocal)
487 _drag_start_rotation.
rotate(axis);
489 switch(_interaction_mode) {
490 case Mode::kTranslation:
495 float start_offset = ray_ray_closest_approach(_drag_start_ray, axis_ray).second;
496 float offset = ray_ray_closest_approach(ray, axis_ray).second;
498 vec3 new_position = _drag_start_position;
499 if(_interaction_feature == InteractionFeature::kAxis)
500 new_position += (offset - start_offset) * axis;
502 new_position += intersection_position - start_intersection_position;
504 set_position(new_position);
510 vec3 new_local_offset = start_intersection_position - _drag_start_position;
511 if(_interaction_feature == InteractionFeature::kAxis)
512 new_local_offset[axis_idx] = (intersection_position - position)[axis_idx];
514 new_local_offset = intersection_position - position;
516 vec3 scale_mult = new_local_offset / (start_intersection_position - _drag_start_position);
518 vec3 new_scale = _drag_start_scale;
519 switch(_interaction_feature) {
520 case InteractionFeature::kAxis:
521 new_scale[axis_idx] *= scale_mult[axis_idx];
523 case InteractionFeature::kPlane:
524 for(
int i = 0; i < 3; ++i) {
526 new_scale[i] *= scale_mult[i];
529 case InteractionFeature::kCenter:
530 new_scale *= length(scale_mult);
536 set_scale(new_scale);
539 case Mode::kRotation:
541 vec3 start_dir = start_intersection_position - _drag_start_position;
542 vec3 end_dir = intersection_position - position;
550 vec3 plane_tangent = normalize(cross(_interaction_plane.normal, start_dir));
552 float cos_theta = dot(start_dir, end_dir);
553 cos_theta = cgv::math::clamp(cos_theta, -1.0f, 1.0f);
554 float angle = std::acos(cos_theta);
557 if(dot(plane_tangent, end_dir) < 0.0f)
558 angle =
static_cast<float>(2.0 * cgv::math::constants::pi) - angle;
560 vec3 rotaton_axis = _interaction_feature == InteractionFeature::kCenter ? _interaction_plane.normal : axis;
562 quat new_rotation =
quat(rotaton_axis, angle) * _drag_start_rotation;
564 set_rotation(new_rotation);
573 on_change(GizmoAction::kDrag, _interaction_mode);
580 on_change(GizmoAction::kDragEnd, _interaction_mode);
584 vec3 ba = r1.direction;
585 vec3 oa = r0.origin - r1.origin;
587 float a = dot(ba, ba);
588 float b = dot(r0.direction, ba);
589 float c = dot(oa, ba);
590 float e = dot(oa, r0.direction);
592 vec2 st =
vec2(c - b * e, b * c - a * e) / (a - b * b);
594 return { st.
y(), st.
x() };
T & y()
return second component
T normalize()
normalize the vector using the L2-Norm and return the length
T & x()
return first component
void rotate(vec_type &v) const
rotate vector according to quaternion
void clear(const context &ctx) override
the clear function destructs the shader program and resets the texture pointers
base class for all drawables, which is independent of the used rendering API.
void post_redraw()
posts a redraw event to the current context if one is available
bool init(context &ctx) override
call init() once before using renderer
virtual bool init(context &ctx)
call init() once before using renderer
virtual void clear(const context &ctx)
the clear function destructs the shader program
const dvec3 & get_view_dir() const
query current view direction
namespace for api independent GPU programming
cgv::math::quaternion< float > quat
declare type of quaternion
cgv::media::color< float, cgv::media::RGB > rgb
declare rgb color type with 32 bit components
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Struct template for fixed n-dimensional rays with arbitrary data type defined by origin and direction...
fvec< T, N > position(float t) const
Return the position of the ray at the given distance (ray parameter t) from its origin.