-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlift.rs
600 lines (548 loc) · 21.8 KB
/
lift.rs
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
* Copyright (C) 2022 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::{interaction::Selectable, shapes::*, site::*, CurrentWorkspace};
use bevy::{prelude::*, render::primitives::Aabb};
use rmf_site_format::{Edge, LiftCabin};
use std::collections::BTreeSet;
#[derive(Clone, Copy, Debug, Component, Deref, DerefMut)]
pub struct ChildLiftCabinGroup(pub Entity);
#[derive(Clone, Copy, Debug, Component, Deref, DerefMut)]
pub struct ChildCabinAnchorGroup(pub Entity);
#[derive(Clone, Copy, Debug, Component, Default)]
pub struct CabinAnchorGroup;
#[derive(Clone, Copy, Debug, Bundle)]
pub struct CabinAnchorGroupBundle {
tag: CabinAnchorGroup,
category: Category,
}
impl Default for CabinAnchorGroupBundle {
fn default() -> Self {
Self {
tag: Default::default(),
category: Category::Lift,
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum CabinDoorId {
#[allow(dead_code)]
Entity(Entity),
RectFace(RectFace),
}
#[derive(Clone, Copy, Debug, Component)]
pub struct LiftDoormat {
pub for_lift: Entity,
pub on_level: Entity,
pub cabin_door: CabinDoorId,
pub door_available: bool,
}
impl LiftDoormat {
pub fn toggle_availability(&self) -> ToggleLiftDoorAvailability {
ToggleLiftDoorAvailability {
for_lift: self.for_lift,
on_level: self.on_level,
cabin_door: self.cabin_door,
door_available: !self.door_available,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct ToggleLiftDoorAvailability {
pub for_lift: Entity,
pub on_level: Entity,
pub cabin_door: CabinDoorId,
pub door_available: bool,
}
fn make_lift_transform(
entity: Entity,
reference_anchors: &Edge<Entity>,
anchors: &AnchorParams,
) -> Transform {
let p_start = anchors
.point_in_parent_frame_of(reference_anchors.start(), Category::Lift, entity)
.unwrap();
let p_end = anchors
.point_in_parent_frame_of(reference_anchors.end(), Category::Lift, entity)
.unwrap();
let (p_start, p_end) = if reference_anchors.left() == reference_anchors.right() {
(p_start, p_start - DEFAULT_CABIN_WIDTH * Vec3::Y)
} else {
(p_start, p_end)
};
let dp = p_start - p_end;
let yaw = (-dp.x).atan2(dp.y);
let center = (p_start + p_end) / 2.0;
Transform {
translation: Vec3::new(center.x, center.y, 0.),
rotation: Quat::from_rotation_z(yaw),
..default()
}
}
pub fn add_tags_to_lift(
mut commands: Commands,
new_lifts: Query<(Entity, &Edge<Entity>), Added<LiftCabin<Entity>>>,
orphan_lifts: Query<Entity, (With<LiftCabin<Entity>>, Without<Parent>)>,
open_sites: Query<Entity, With<NameOfSite>>,
mut dependents: Query<&mut Dependents, With<Anchor>>,
current_workspace: Res<CurrentWorkspace>,
) {
for (e, edge) in &new_lifts {
let mut lift_cmds = commands.entity(e);
lift_cmds
.insert(SpatialBundle::default())
.insert(EdgeLabels::LeftRight)
.insert(Category::Lift);
if orphan_lifts.contains(e) {
// Assume that a newly created lift that doesn't have a parent
// belongs in whatever the current site happens to be.
if let Some(current_site) = current_workspace.to_site(&open_sites) {
commands.entity(current_site).add_child(e);
} else {
error!("Could not find a current site to put a newly created lift inside of!");
}
}
for anchor in edge.array() {
if let Ok(mut deps) = dependents.get_mut(anchor) {
deps.insert(e);
}
}
}
}
pub fn update_lift_cabin(
mut commands: Commands,
lifts: Query<
(
Entity,
&LiftCabin<Entity>,
Option<&RecallLiftCabin<Entity>>,
Option<&ChildCabinAnchorGroup>,
Option<&ChildLiftCabinGroup>,
&Parent,
),
Or<(Changed<LiftCabin<Entity>>, Changed<Parent>)>,
>,
mut cabin_anchor_groups: Query<&mut Transform, With<CabinAnchorGroup>>,
level_visits: Query<&LevelVisits<Entity>>,
children: Query<&Children>,
doors: Query<&Edge<Entity>, With<LiftCabinDoorMarker>>,
mut anchors: Query<&mut Anchor>,
assets: Res<SiteAssets>,
mut meshes: ResMut<Assets<Mesh>>,
levels: Query<(Entity, &Parent), With<LevelElevation>>,
) {
for (e, cabin, recall, child_anchor_group, child_cabin_group, site) in &lifts {
// Despawn the previous cabin
if let Some(cabin_group) = child_cabin_group {
commands.entity(cabin_group.0).despawn_recursive();
}
let cabin_tf = match cabin {
LiftCabin::Rect(params) => {
let Aabb { center, .. } = params.aabb();
let cabin_tf =
Transform::from_translation(Vec3::new(center.x, center.y, FLOOR_LAYER_START));
let floor_mesh: Mesh = make_flat_rect_mesh(
params.depth + 2.0 * params.thickness(),
params.width + 2.0 * params.thickness(),
)
.into();
let wall_mesh: Mesh = params
.cabin_wall_coordinates()
.into_iter()
.map(|wall| {
make_wall_mesh(
wall[0],
wall[1],
params.thickness(),
DEFAULT_LEVEL_HEIGHT / 3.0,
None,
None,
)
})
.fold(MeshBuffer::default(), |sum, next| sum.merge_with(next))
.into();
let cabin_entity = commands
.spawn(SpatialBundle::from_transform(cabin_tf))
.with_children(|parent| {
parent
.spawn(PbrBundle {
mesh: meshes.add(floor_mesh),
material: assets.lift_floor_material.clone(),
..default()
})
.insert(Selectable::new(e));
parent
.spawn(PbrBundle {
mesh: meshes.add(wall_mesh),
material: assets.lift_wall_material.clone(),
..default()
})
.insert(Selectable::new(e));
for (level, level_site) in &levels {
if level_site.get() != site.get() {
continue;
}
for (face, door, mut aabb) in params.level_doormats(0.3, recall) {
let door_available = door
.filter(|d| {
level_visits
.get(*d)
.ok()
.unwrap_or(&LevelVisits::default())
.contains(&level)
})
.is_some();
aabb.center.z = LANE_LAYER_LIMIT;
let mesh = make_flat_mesh_for_aabb(aabb);
parent
.spawn(PbrBundle {
mesh: meshes.add(mesh.into()),
// Doormats are not visible by default.
// Other plugins should make them visible
// if using them as a visual cue.
visibility: Visibility { is_visible: false },
..default()
})
.insert(LiftDoormat {
for_lift: e,
on_level: level,
cabin_door: CabinDoorId::RectFace(face),
door_available,
});
}
}
})
.id();
commands
.entity(e)
.insert(ChildLiftCabinGroup(cabin_entity))
.add_child(cabin_entity);
// Update transforms for door anchors
for face in RectFace::iter_all() {
if let (Some(p), Some(new_edge)) =
(params.door(face), params.level_door_anchors(face))
{
if let Ok(edge) = doors.get(p.door) {
for (a, new_anchor) in
edge.array().into_iter().zip(new_edge.into_iter())
{
if let Ok(mut anchor) = anchors.get_mut(a) {
*anchor = new_anchor;
}
}
}
}
}
cabin_tf
}
};
let cabin_anchor_group = if let Some(child_anchor_group) = child_anchor_group {
Some(**child_anchor_group)
} else if let Ok(children) = children.get(e) {
let found_group = children
.iter()
.find(|c| cabin_anchor_groups.contains(**c))
.copied();
if let Some(group) = found_group {
commands.entity(e).insert(ChildCabinAnchorGroup(group));
}
found_group
} else {
None
};
match cabin_anchor_group {
Some(group) => {
*cabin_anchor_groups.get_mut(group).unwrap() = cabin_tf;
}
None => {
let group = commands.entity(e).add_children(|p| {
p.spawn(SpatialBundle::from_transform(cabin_tf))
.insert(CabinAnchorGroupBundle::default())
.id()
});
commands.entity(e).insert(ChildCabinAnchorGroup(group));
}
};
}
}
pub fn update_lift_edge(
mut lifts: Query<
(Entity, &Edge<Entity>, &mut Transform),
(Changed<Edge<Entity>>, With<LiftCabin<Entity>>),
>,
anchors: AnchorParams,
) {
for (e, edge, mut tf) in &mut lifts {
*tf = make_lift_transform(e, edge, &anchors);
}
}
pub fn update_lift_for_moved_anchors(
mut lifts: Query<(Entity, &Edge<Entity>, &mut Transform), With<LiftCabin<Entity>>>,
anchors: AnchorParams,
changed_anchors: Query<
&Dependents,
(
With<Anchor>,
Or<(Changed<Anchor>, Changed<GlobalTransform>)>,
),
>,
) {
for changed_anchor in &changed_anchors {
for dependent in changed_anchor.iter() {
if let Ok((e, edge, mut tf)) = lifts.get_mut(*dependent) {
*tf = make_lift_transform(e, edge, &anchors);
}
}
}
}
pub fn update_lift_door_availability(
mut commands: Commands,
mut toggles: EventReader<ToggleLiftDoorAvailability>,
mut lifts: Query<(
&mut LiftCabin<Entity>,
Option<&RecallLiftCabin<Entity>>,
&ChildCabinAnchorGroup,
)>,
mut doors: Query<(Entity, &Edge<Entity>, &mut LevelVisits<Entity>), With<LiftCabinDoorMarker>>,
dependents: Query<&Dependents, With<Anchor>>,
current_level: Res<CurrentLevel>,
new_levels: Query<(), Added<LevelElevation>>,
all_levels: Query<(), With<LevelElevation>>,
removed_levels: RemovedComponents<LevelElevation>,
parents: Query<&Parent>,
) {
for toggle in toggles.iter() {
let (mut cabin, recall_cabin, anchor_group) = match lifts.get_mut(toggle.for_lift) {
Ok(lift) => lift,
Err(_) => continue,
};
if toggle.door_available {
if !all_levels.contains(toggle.on_level) {
// If we're being asked to toggle availability on for something
// that isn't a level, then ignore this request.
error!(
"Asking to turn on lift {:?} door {:?} availability \
for a level {:?} that does not exist.",
toggle.for_lift, toggle.cabin_door, toggle.on_level,
);
continue;
}
let cabin_door = match toggle.cabin_door {
CabinDoorId::Entity(e) => e,
CabinDoorId::RectFace(face) => {
match cabin.as_mut() {
LiftCabin::Rect(params) => {
if let Some(cabin_door) = params.door(face).map(|p| p.door) {
cabin_door
} else if let Some(old_cabin_door) =
recall_cabin.map(|r| r.rect_door(face).as_ref()).flatten()
{
// A cabin door used to exist but was removed by
// the user in the past. We should revive it
// instead of creating a whole new one.
*params.door_mut(face) = Some(old_cabin_door.clone());
old_cabin_door.door
} else {
// Create a new door with new anchors
let new_door = commands.spawn_empty().id();
*params.door_mut(face) = Some(LiftCabinDoorPlacement::new(
new_door,
params.width.min(params.depth) / 2.0,
));
let anchors =
params.level_door_anchors(face).unwrap().map(|anchor| {
commands
.spawn(AnchorBundle::new(anchor))
.insert(Subordinate(Some(toggle.for_lift)))
.id()
});
for anchor in anchors {
commands.entity(**anchor_group).add_child(anchor);
}
commands
.entity(new_door)
.insert(LiftCabinDoor {
kind: DoorType::DoubleSliding(DoubleSlidingDoor::default()),
reference_anchors: anchors.into(),
visits: LevelVisits(BTreeSet::from_iter([toggle.on_level])),
marker: Default::default(),
})
.insert(Dependents::single(toggle.for_lift));
commands.entity(toggle.for_lift).add_child(new_door);
new_door
}
} //_ => continue,
}
}
};
if let Ok((_, _, mut visits)) = doors.get_mut(cabin_door) {
visits.insert(toggle.on_level);
if let Some(current_level) = **current_level {
commands.entity(cabin_door).insert(Visibility {
is_visible: visits.contains(¤t_level),
});
}
}
commands.entity(cabin_door).remove::<Pending>();
if let Ok((_, existing_anchors, _)) = doors.get(cabin_door) {
// Make sure visibility is turned on for the anchors and
// the Pending is removed.
for anchor in existing_anchors.array() {
commands
.entity(anchor)
.remove::<Pending>()
.insert(Visibility { is_visible: true });
}
}
} else {
let cabin_door = match toggle.cabin_door {
CabinDoorId::Entity(e) => Some(e),
CabinDoorId::RectFace(face) => match &*cabin {
LiftCabin::Rect(params) => params.door(face).map(|p| p.door),
//_ => None,
},
};
// If the cabin door that's being removed cannot be found then there
// is nothing for us to do on this loop.
let cabin_door = match cabin_door {
Some(e) => e,
None => continue,
};
let need_to_remove_door = if let Ok((_, _, mut visits)) = doors.get_mut(cabin_door) {
visits.remove(&toggle.on_level);
if let Some(current_level) = **current_level {
commands.entity(cabin_door).insert(Visibility {
is_visible: visits.contains(¤t_level),
});
}
visits.is_empty()
} else {
false
};
if need_to_remove_door {
remove_door(
cabin_door,
&mut commands,
cabin.as_mut(),
&doors,
&dependents,
);
}
// This is a silly hack to dirty the change tracker for this
// lift and force it to be refreshed in the update_lift_cabin
// system. We do that so the lift door doormats can be updated
// to reflect their new state. When time allows, it would be worth
// considering a more efficient strategy for updating the doormats.
cabin.set_changed();
}
}
if current_level.is_changed() {
// Loop through all the cabin doors to check if their visibility needs
// to change.
if let Some(current_level) = **current_level {
for (e, _, visits) in &doors {
commands.entity(e).insert(Visibility {
is_visible: visits.contains(¤t_level),
});
}
}
}
if !new_levels.is_empty() {
// A silly dirty hack to force lift cabins to update their doormats
// when a new level is added.
for (mut cabin, _, _) in &mut lifts {
cabin.set_changed();
}
}
for removed_level in removed_levels.iter() {
// When a level is removed, we should clear it from all visitation
// information and redo the cabin rendering.
let mut doors_to_remove = Vec::new();
for (e_door, _, mut visits) in &mut doors {
let mut need_to_remove_door = false;
if visits.remove(&removed_level) {
if visits.is_empty() {
need_to_remove_door = true;
}
}
if need_to_remove_door {
doors_to_remove.push(e_door);
}
}
for e_door in doors_to_remove {
let e_lift = match parents.get(e_door) {
Ok(e_lift) => e_lift,
Err(_) => {
error!(
"Unable to find parent for lift door \
{e_door:?} while handling a removed level"
);
continue;
}
};
let (mut cabin, _, _) = match lifts.get_mut(e_lift.get()) {
Ok(cabin) => cabin,
Err(_) => {
error!("Unable to find cabin for lift {e_lift:?}");
continue;
}
};
remove_door(e_door, &mut commands, cabin.as_mut(), &doors, &dependents);
}
}
}
fn remove_door(
cabin_door: Entity,
commands: &mut Commands,
cabin: &mut LiftCabin<Entity>,
doors: &Query<(Entity, &Edge<Entity>, &mut LevelVisits<Entity>), With<LiftCabinDoorMarker>>,
dependents: &Query<&Dependents, With<Anchor>>,
) {
cabin.remove_door(cabin_door);
commands
.entity(cabin_door)
.insert(Pending)
.insert(Visibility { is_visible: false });
// Clear out the anchors if nothing besides the cabin door depends on them
let remove_anchors = if let Ok((_, anchors, _)) = doors.get(cabin_door) {
let mut remove_anchors = true;
'outer: for anchor in anchors.array() {
if let Ok(deps) = dependents.get(anchor) {
for dependent in deps.iter() {
if *dependent != cabin_door {
remove_anchors = false;
break 'outer;
}
}
}
}
if remove_anchors {
Some(*anchors)
} else {
None
}
} else {
None
};
if let Some(anchors) = remove_anchors {
for anchor in anchors.array() {
commands
.entity(anchor)
.insert(Pending)
.insert(Visibility { is_visible: false });
}
}
}