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
use geom::{ArrowCap, Circle, Distance, PolyLine, Polygon, Pt2D};
use map_model::{DrivingSide, Map, SIDEWALK_THICKNESS};
use sim::{DrawPedCrowdInput, DrawPedestrianInput, PedCrowdLocation, PedestrianID, Sim};
use widgetry::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, Text};

use crate::colors::ColorScheme;
use crate::render::{grey_out_unhighlighted_people, DrawOptions, Renderable, OUTLINE_THICKNESS};
use crate::{AppLike, ID};

pub struct DrawPedestrian {
    pub id: PedestrianID,
    body_circle: Circle,
    zorder: isize,

    draw_default: Drawable,
}

impl DrawPedestrian {
    pub fn new(
        input: DrawPedestrianInput,
        step_count: usize,
        map: &Map,
        sim: &Sim,
        prerender: &Prerender,
        cs: &ColorScheme,
    ) -> DrawPedestrian {
        let mut draw_default = GeomBatch::new();
        DrawPedestrian::geometry(&mut draw_default, sim, cs, &input, step_count);

        let radius = SIDEWALK_THICKNESS / 4.0; // TODO make const after const fn is better
        let body_circle = Circle::new(input.pos, radius);

        if let Some(t) = input.waiting_for_turn {
            // A silly idea for peds... use hands to point at their turn?
            let angle = map.get_t(t).angle();
            draw_default.push(
                cs.turn_arrow,
                PolyLine::must_new(vec![
                    input.pos.project_away(radius / 2.0, angle.opposite()),
                    input.pos.project_away(radius / 2.0, angle),
                ])
                .make_arrow(Distance::meters(0.15), ArrowCap::Triangle),
            );
        }

        DrawPedestrian {
            id: input.id,
            body_circle,
            zorder: input.on.get_zorder(map),
            draw_default: prerender.upload(draw_default),
        }
    }

    pub fn geometry(
        batch: &mut GeomBatch,
        sim: &Sim,
        cs: &ColorScheme,
        input: &DrawPedestrianInput,
        step_count: usize,
    ) {
        // TODO Slight issues with rendering small pedestrians:
        // - route visualization is thick
        // - there are little skips when making turns
        // - front paths are too skinny
        let radius = SIDEWALK_THICKNESS / 4.0; // TODO make const after const fn is better
        let body_circle = Circle::new(input.pos, radius);

        let foot_radius = 0.2 * radius;
        let hand_radius = 0.2 * radius;
        let left_foot_angle = 30.0;
        let right_foot_angle = -30.0;
        let left_hand_angle = 70.0;
        let right_hand_angle = -70.0;

        let left_foot = Circle::new(
            input
                .pos
                .project_away(radius, input.facing.rotate_degs(left_foot_angle)),
            foot_radius,
        );
        let right_foot = Circle::new(
            input
                .pos
                .project_away(radius, input.facing.rotate_degs(right_foot_angle)),
            foot_radius,
        );
        let left_hand = Circle::new(
            input
                .pos
                .project_away(radius, input.facing.rotate_degs(left_hand_angle)),
            hand_radius,
        );
        let right_hand = Circle::new(
            input
                .pos
                .project_away(radius, input.facing.rotate_degs(right_hand_angle)),
            hand_radius,
        );
        let foot_color = cs.ped_foot;
        let hand_color = cs.ped_head;
        // Jitter based on ID so we don't all walk synchronized.
        let jitter = input.id.0 % 2 == 0;
        let remainder = step_count % 6;
        if input.waiting_for_turn.is_some() || input.waiting_for_bus {
            batch.push(foot_color, left_foot.to_polygon());
            batch.push(foot_color, right_foot.to_polygon());
            batch.push(hand_color, left_hand.to_polygon());
            batch.push(hand_color, right_hand.to_polygon());
        } else if jitter == (remainder < 3) {
            batch.push(foot_color, left_foot.to_polygon());
            batch.push(
                foot_color,
                Circle::new(
                    input
                        .pos
                        .project_away(0.9 * radius, input.facing.rotate_degs(right_foot_angle)),
                    foot_radius,
                )
                .to_polygon(),
            );

            batch.push(hand_color, right_hand.to_polygon());
            batch.push(
                hand_color,
                Circle::new(
                    input
                        .pos
                        .project_away(0.9 * radius, input.facing.rotate_degs(left_hand_angle)),
                    hand_radius,
                )
                .to_polygon(),
            );
        } else {
            batch.push(foot_color, right_foot.to_polygon());
            batch.push(
                foot_color,
                Circle::new(
                    input
                        .pos
                        .project_away(0.9 * radius, input.facing.rotate_degs(left_foot_angle)),
                    foot_radius,
                )
                .to_polygon(),
            );

            batch.push(hand_color, left_hand.to_polygon());
            batch.push(
                hand_color,
                Circle::new(
                    input
                        .pos
                        .project_away(0.9 * radius, input.facing.rotate_degs(right_hand_angle)),
                    hand_radius,
                )
                .to_polygon(),
            );
        };

        let head_circle = Circle::new(input.pos, 0.5 * radius);
        batch.push(
            grey_out_unhighlighted_people(
                if input.preparing_bike {
                    cs.ped_preparing_bike_body
                } else {
                    cs.rotating_color_agents(input.id.0)
                },
                &Some(input.person),
                sim,
            ),
            body_circle.to_polygon(),
        );
        batch.push(cs.ped_head, head_circle.to_polygon());
    }
}

impl Renderable for DrawPedestrian {
    fn get_id(&self) -> ID {
        ID::Pedestrian(self.id)
    }

    fn draw(&self, g: &mut GfxCtx, _: &dyn AppLike, _: &DrawOptions) {
        g.redraw(&self.draw_default);
    }

    fn get_outline(&self, _: &Map) -> Polygon {
        Circle::new(self.body_circle.center, Distance::meters(2.0))
            .to_outline(OUTLINE_THICKNESS)
            .unwrap()
    }

    fn contains_pt(&self, pt: Pt2D, _: &Map) -> bool {
        Circle::new(self.body_circle.center, Distance::meters(2.0)).contains_pt(pt)
    }

    fn get_zorder(&self) -> isize {
        self.zorder
    }
}

pub struct DrawPedCrowd {
    members: Vec<PedestrianID>,
    blob: Polygon,
    blob_pl: PolyLine,
    zorder: isize,

    draw_default: Drawable,
}

impl DrawPedCrowd {
    pub fn new(
        input: DrawPedCrowdInput,
        map: &Map,
        prerender: &Prerender,
        cs: &ColorScheme,
    ) -> DrawPedCrowd {
        let pl_shifted = match input.location {
            PedCrowdLocation::Sidewalk(on, contraflow) => {
                let pl_slice = on.exact_slice(input.low, input.high, map);
                if contraflow == (map.get_config().driving_side == DrivingSide::Right) {
                    pl_slice.shift_left(SIDEWALK_THICKNESS / 4.0)
                } else {
                    pl_slice.shift_right(SIDEWALK_THICKNESS / 4.0)
                }
                .unwrap_or_else(|_| on.exact_slice(input.low, input.high, map))
            }
            PedCrowdLocation::BldgDriveway(b) => map
                .get_b(b)
                .driveway_geom
                .exact_slice(input.low, input.high),
            PedCrowdLocation::LotDriveway(pl) => map
                .get_pl(pl)
                .sidewalk_line
                .to_polyline()
                .exact_slice(input.low, input.high),
        };
        let blob = pl_shifted.make_polygons(SIDEWALK_THICKNESS / 2.0);
        let mut batch = GeomBatch::new();
        batch.push(cs.ped_crowd, blob.clone());
        batch.append(
            Text::from(Line(format!("{}", input.members.len())).fg(Color::BLACK))
                .render_autocropped(prerender)
                .scale(0.02)
                .centered_on(blob.center()),
        );

        DrawPedCrowd {
            members: input.members,
            blob_pl: pl_shifted,
            blob,
            zorder: match input.location {
                PedCrowdLocation::Sidewalk(on, _) => on.get_zorder(map),
                PedCrowdLocation::BldgDriveway(_) => 0,
                PedCrowdLocation::LotDriveway(_) => 0,
            },
            draw_default: prerender.upload(batch),
        }
    }
}

impl Renderable for DrawPedCrowd {
    fn get_id(&self) -> ID {
        // Expensive! :(
        ID::PedCrowd(self.members.clone())
    }

    fn draw(&self, g: &mut GfxCtx, _: &dyn AppLike, _: &DrawOptions) {
        g.redraw(&self.draw_default);
    }

    fn get_outline(&self, _: &Map) -> Polygon {
        self.blob_pl
            .to_thick_boundary(SIDEWALK_THICKNESS / 2.0, OUTLINE_THICKNESS)
            .unwrap_or_else(|| self.blob.clone())
    }

    fn get_zorder(&self) -> isize {
        self.zorder
    }
}