rmf_site_camera 0.0.2

Camera management utilities for the rmf_site_editor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Copyright (C) 2024 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

use crate::{
    components::{OrthographicCameraRoot, PerspectiveCameraRoot},
    resources::CameraConfig,
};

use super::{
    CameraCommandType, MAX_FOV, MAX_SCALE, MIN_FOV, MIN_SCALE, ProjectionMode,
    get_groundplane_else_default_selection, orbit_camera_around_point, zoom_distance_factor,
};
use bevy_ecs::prelude::*;
use bevy_input::{
    mouse::{MouseScrollUnit, MouseWheel},
    prelude::*,
};
use bevy_math::prelude::*;
use bevy_picking::{
    backend::ray::RayMap,
    pointer::{PointerId, PointerInteraction},
};
use bevy_reflect::Reflect;
use bevy_render::prelude::*;
use bevy_transform::components::{GlobalTransform, Transform};
use bevy_window::{PrimaryWindow, Window};
use nalgebra::{Matrix3, Matrix3x1};
use tracing::warn;

pub const SCALE_ZOOM_SENSITIVITY: f32 = 0.1;
pub const TRANSLATION_ZOOM_SENSITIVITY: f32 = 0.2;
pub const FOV_ZOOM_SENSITIVITY: f32 = 0.1;

/// Current cursor command for active camera.
#[derive(Resource, Reflect)]
#[reflect(Resource)]
pub struct CursorCommand {
    pub translation_delta: Vec3,
    pub rotation_delta: Quat,
    pub scale_delta: f32,
    pub fov_delta: f32,
    pub cursor_selection: Option<Vec3>,
    pub cursor_direction_camera_frame: Option<Vec3>,
    pub command_type: CameraCommandType,
}

impl Default for CursorCommand {
    fn default() -> Self {
        Self {
            translation_delta: Vec3::ZERO,
            rotation_delta: Quat::IDENTITY,
            scale_delta: 0.0,
            fov_delta: 0.0,
            cursor_selection: None,
            cursor_direction_camera_frame: None,
            command_type: CameraCommandType::Inactive,
        }
    }
}

impl CursorCommand {
    pub fn take_translation_delta(&mut self) -> Vec3 {
        std::mem::replace(&mut self.translation_delta, Vec3::ZERO)
    }

    pub fn take_rotation_delta(&mut self) -> Quat {
        std::mem::replace(&mut self.rotation_delta, Quat::IDENTITY)
    }

    pub fn take_scale_delta(&mut self) -> f32 {
        std::mem::replace(&mut self.scale_delta, 0.0)
    }

    pub fn take_fov_delta(&mut self) -> f32 {
        std::mem::replace(&mut self.fov_delta, 0.0)
    }
}

pub fn update_cursor_command(
    mut camera_config: ResMut<CameraConfig>,
    mut cursor_command: ResMut<CursorCommand>,
    mut mouse_wheel: EventReader<MouseWheel>,
    mouse_input: Res<ButtonInput<MouseButton>>,
    projection_mode: Res<ProjectionMode>,
    keyboard_input: Res<ButtonInput<KeyCode>>,
    pointers: Query<(&PointerId, &PointerInteraction)>,
    ortho_cam: Query<Entity, With<OrthographicCameraRoot>>,
    persp_cam: Query<Entity, With<PerspectiveCameraRoot>>,
    ray_map: Res<RayMap>,
    cameras: Query<(&Projection, &Transform, &GlobalTransform)>,
    primary_windows: Query<&Window, With<PrimaryWindow>>,
) {
    if let Ok(window) = primary_windows.single() {
        // Return if cursor not within window
        if window.cursor_position().is_none() {
            return;
        }

        // Scroll input
        let mut scroll_motion = 0.0;
        for ev in mouse_wheel.read() {
            scroll_motion += match ev.unit {
                MouseScrollUnit::Line => ev.y,
                MouseScrollUnit::Pixel => ev.y / 100.0,
            };
        }

        // Command type, return if inactive
        let command_type_prev = cursor_command.command_type;
        let command_type = get_command_type(
            &keyboard_input,
            &mouse_input,
            &scroll_motion,
            *projection_mode,
        );
        if command_type == CameraCommandType::Inactive {
            *cursor_command = CursorCommand::default();
            return;
        }

        // Camera projection and transform
        let active_camera_entity = match *projection_mode {
            ProjectionMode::Orthographic => ortho_cam.single(),
            ProjectionMode::Perspective => persp_cam.single(),
        };

        let Ok(active_camera_entity) = active_camera_entity
            .inspect_err(|err| warn!("could not update cursor command due to {:#}", err))
        else {
            return;
        };

        let (camera_proj, camera_transform, _) = cameras.get(active_camera_entity).unwrap();

        // Get selection under cursor, cursor direction
        let Some((_, cursor_ray)) = ray_map
            .iter()
            .find(|(id, _)| id.camera == active_camera_entity)
        else {
            return;
        };

        let cursor_selection_new = pointers
            .single()
            .ok()
            .and_then(|(_, interactions)| {
                interactions
                    .iter()
                    .find(|(_, hit)| hit.camera == active_camera_entity)
            })
            .and_then(|(_, hit_data)| hit_data.position)
            .unwrap_or_else(|| {
                get_groundplane_else_default_selection(
                    cursor_ray.origin,
                    *cursor_ray.direction,
                    *camera_transform.forward(),
                )
            });
        let cursor_selection = match cursor_command.cursor_selection {
            Some(selection) => selection,
            None => cursor_selection_new,
        };

        let cursor_direction = cursor_ray.direction.normalize();
        let cursor_direction_camera_frame = camera_transform.rotation.inverse() * cursor_direction;
        let cursor_direction_camera_frame_prev = cursor_command
            .cursor_direction_camera_frame
            .unwrap_or(cursor_direction_camera_frame);

        // Update orbit center
        if command_type != CameraCommandType::Orbit {
            camera_config.orbit_center = None;
        } else if (command_type == CameraCommandType::Orbit && command_type != command_type_prev)
            || camera_config.orbit_center.is_none()
        {
            camera_config.orbit_center = Some(cursor_selection);
        }
        let orbit_center = camera_config.orbit_center.unwrap_or(cursor_selection);

        match *projection_mode {
            ProjectionMode::Orthographic => {
                if let Projection::Orthographic(camera_proj) = camera_proj {
                    *cursor_command = get_orthographic_cursor_command(
                        &camera_transform,
                        &camera_proj,
                        command_type,
                        cursor_selection,
                        cursor_selection_new,
                        scroll_motion,
                    );
                }
            }
            ProjectionMode::Perspective => {
                if let Projection::Perspective(camera_proj) = camera_proj {
                    *cursor_command = get_perspective_cursor_command(
                        &camera_transform,
                        &camera_proj,
                        command_type,
                        cursor_direction,
                        cursor_direction_camera_frame,
                        cursor_direction_camera_frame_prev,
                        cursor_selection,
                        scroll_motion,
                        orbit_center,
                    );
                }
            }
        }
    } else {
        *cursor_command = CursorCommand::default();
    }
}

fn get_orthographic_cursor_command(
    camera_transform: &Transform,
    camera_proj: &OrthographicProjection,
    command_type: CameraCommandType,
    cursor_selection: Vec3,
    cursor_selection_new: Vec3,
    scroll_motion: f32,
) -> CursorCommand {
    let mut cursor_command = CursorCommand::default();
    let mut is_cursor_selecting = false;

    // Zoom
    let target_scale = (camera_proj.scale
        - scroll_motion * camera_proj.scale * SCALE_ZOOM_SENSITIVITY)
        .clamp(MIN_SCALE, MAX_SCALE);
    cursor_command.scale_delta = target_scale - camera_proj.scale;

    //TODO(@reuben-thomas) Find out why cursor ray cannot be used for direction
    let cursor_direction = (cursor_selection_new - camera_transform.translation).normalize();

    match command_type {
        CameraCommandType::Pan => {
            let camera_transform_next = pan_camera_with_cursor(
                camera_transform,
                cursor_selection,
                cursor_direction,
                scroll_motion,
            );
            cursor_command.translation_delta =
                camera_transform_next.translation - camera_transform.translation;
            is_cursor_selecting = true;
        }
        CameraCommandType::Orbit => {
            let cursor_direction_prev =
                (cursor_selection - camera_transform.translation).normalize();

            let heading_0 =
                (cursor_direction_prev - cursor_direction_prev.project_onto(Vec3::Z)).normalize();
            let heading_1 = (cursor_direction - cursor_direction.project_onto(Vec3::Z)).normalize();
            let is_clockwise = heading_0.cross(heading_1).dot(Vec3::Z) > 0.0;
            let yaw = heading_0.angle_between(heading_1) * if is_clockwise { -1.0 } else { 1.0 };
            let yaw = Quat::from_rotation_z(yaw);

            cursor_command.rotation_delta = yaw;
            is_cursor_selecting = true;
        }
        _ => (),
    }

    cursor_command.command_type = command_type;
    cursor_command.cursor_selection = if is_cursor_selecting {
        Some(cursor_selection)
    } else {
        None
    };

    return cursor_command;
}

fn get_perspective_cursor_command(
    camera_transform: &Transform,
    camera_proj: &PerspectiveProjection,
    command_type: CameraCommandType,
    cursor_direction: Vec3,
    cursor_direction_camera_frame: Vec3,
    cursor_direction_camera_frame_prev: Vec3,
    cursor_selection: Vec3,
    scroll_motion: f32,
    orbit_center: Vec3,
) -> CursorCommand {
    let mut cursor_command = CursorCommand::default();
    let mut is_cursor_selecting = false;

    match command_type {
        CameraCommandType::FovZoom => {
            let target_fov = (camera_proj.fov - scroll_motion * FOV_ZOOM_SENSITIVITY)
                .clamp(MIN_FOV.to_radians(), MAX_FOV.to_radians());
            cursor_command.fov_delta = target_fov - camera_proj.fov;
        }
        CameraCommandType::TranslationZoom => {
            cursor_command.translation_delta = cursor_direction
                * zoom_distance_factor(camera_transform.translation, cursor_selection)
                * TRANSLATION_ZOOM_SENSITIVITY
                * scroll_motion
        }
        CameraCommandType::Pan => {
            let camera_transform_next = pan_camera_with_cursor(
                camera_transform,
                cursor_selection,
                cursor_direction,
                scroll_motion,
            );
            cursor_command.translation_delta =
                camera_transform_next.translation - camera_transform.translation;
            is_cursor_selecting = true;
        }
        CameraCommandType::Orbit => {
            // Pitch and yaw inputs from cursor direction vector as spherical coordinates
            let pitch = cursor_direction_camera_frame_prev.y.acos()
                - cursor_direction_camera_frame.y.acos();
            let yaw = (cursor_direction_camera_frame_prev
                .z
                .atan2(cursor_direction_camera_frame_prev.x))
                - (cursor_direction_camera_frame
                    .z
                    .atan2(cursor_direction_camera_frame.x));

            // Scale inputs to viewport range
            let pitch_viewport_range = camera_proj.fov;
            let pitch = -pitch / pitch_viewport_range * std::f32::consts::PI;
            let yaw_viewport_range = camera_proj.fov * camera_proj.aspect_ratio;
            let yaw = yaw / yaw_viewport_range * 2.0 * std::f32::consts::PI;
            let zoom = TRANSLATION_ZOOM_SENSITIVITY * scroll_motion;

            let camera_transform_next =
                orbit_camera_around_point(camera_transform, orbit_center, pitch, yaw, zoom);

            cursor_command.translation_delta =
                camera_transform_next.translation - camera_transform.translation;
            cursor_command.rotation_delta =
                camera_transform.rotation.inverse() * camera_transform_next.rotation;
        }
        _ => (),
    }

    cursor_command.command_type = command_type;
    cursor_command.cursor_direction_camera_frame = Some(cursor_direction_camera_frame);
    cursor_command.cursor_selection = if is_cursor_selecting {
        Some(cursor_selection)
    } else {
        None
    };

    return cursor_command;
}

/// Pans camera such that selection remains under cursor
fn pan_camera_with_cursor(
    camera_transform: &Transform,
    cursor_selection: Vec3,
    cursor_direction: Vec3,
    scroll_motion: f32,
) -> Transform {
    let mut camera_transform_next = camera_transform.clone();
    // To keep the same point below the cursor, we solve
    // selection_to_camera + translation_delta = selection_to_camera_next
    // selection_to_camera_next = x3 * -cursor_direction
    // translation_delta = x1 * right_ transltion + x2 * up_translation
    let selection_to_camera = cursor_selection - camera_transform.translation;
    let right_translation = camera_transform.rotation * Vec3::X;
    let up_translation = camera_transform.rotation * Vec3::Y;

    let a = Matrix3::new(
        right_translation.x,
        up_translation.x,
        -cursor_direction.x,
        right_translation.y,
        up_translation.y,
        -cursor_direction.y,
        right_translation.z,
        up_translation.z,
        -cursor_direction.z,
    );
    let b = Matrix3x1::new(
        selection_to_camera.x,
        selection_to_camera.y,
        selection_to_camera.z,
    );
    let x = a.lu().solve(&b).unwrap();
    camera_transform_next.translation += x[0] * right_translation + x[1] * up_translation;

    let zoom_translation = cursor_direction
        * zoom_distance_factor(
            camera_transform.translation,
            camera_transform_next.translation,
        )
        * TRANSLATION_ZOOM_SENSITIVITY
        * scroll_motion;
    camera_transform_next.translation += zoom_translation;

    return camera_transform_next;
}

fn get_command_type(
    keyboard_input: &Res<ButtonInput<KeyCode>>,
    mouse_input: &Res<ButtonInput<MouseButton>>,
    scroll_motion: &f32,
    projection_mode: ProjectionMode,
) -> CameraCommandType {
    // Inputs
    let is_scrolling = *scroll_motion != 0.;
    let is_shifting =
        keyboard_input.pressed(KeyCode::ShiftLeft) || keyboard_input.pressed(KeyCode::ShiftRight);

    // Panning
    if !is_shifting && mouse_input.pressed(MouseButton::Right) {
        return CameraCommandType::Pan;
    }

    // Orbitting
    if mouse_input.pressed(MouseButton::Middle)
        || (is_shifting && mouse_input.pressed(MouseButton::Right))
    {
        return CameraCommandType::Orbit;
    }

    // Zoom
    if projection_mode == ProjectionMode::Orthographic && is_scrolling {
        return CameraCommandType::ScaleZoom;
    }
    if projection_mode == ProjectionMode::Perspective && is_scrolling {
        if is_shifting {
            return CameraCommandType::FovZoom;
        } else {
            return CameraCommandType::TranslationZoom;
        }
    }

    return CameraCommandType::Inactive;
}