bevy_gizmos/
arcs.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
//! Additional [`Gizmos`] Functions -- Arcs
//!
//! Includes the implementation of [`Gizmos::arc_2d`],
//! and assorted support items.

use crate::{
    circles::DEFAULT_CIRCLE_RESOLUTION,
    prelude::{GizmoConfigGroup, Gizmos},
};
use bevy_color::Color;
use bevy_math::{Isometry2d, Isometry3d, Quat, Rot2, Vec2, Vec3};
use core::f32::consts::{FRAC_PI_2, TAU};

// === 2D ===

impl<'w, 's, Config, Clear> Gizmos<'w, 's, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    /// Draw an arc, which is a part of the circumference of a circle, in 2D.
    ///
    /// This should be called for each frame the arc needs to be rendered.
    ///
    /// # Arguments
    /// - `isometry` defines the translation and rotation of the arc.
    ///   - the translation specifies the center of the arc
    ///   - the rotation is counter-clockwise starting from `Vec2::Y`
    /// - `arc_angle` sets the length of this arc, in radians.
    /// - `radius` controls the distance from `position` to this arc, and thus its curvature.
    /// - `color` sets the color to draw the arc.
    ///
    /// # Example
    /// ```
    /// # use bevy_gizmos::prelude::*;
    /// # use bevy_math::prelude::*;
    /// # use std::f32::consts::FRAC_PI_4;
    /// # use bevy_color::palettes::basic::{GREEN, RED};
    /// fn system(mut gizmos: Gizmos) {
    ///     gizmos.arc_2d(Isometry2d::IDENTITY, FRAC_PI_4, 1., GREEN);
    ///
    ///     // Arcs have 32 line-segments by default.
    ///     // You may want to increase this for larger arcs.
    ///     gizmos
    ///         .arc_2d(Isometry2d::IDENTITY, FRAC_PI_4, 5., RED)
    ///         .resolution(64);
    /// }
    /// # bevy_ecs::system::assert_is_system(system);
    /// ```
    #[inline]
    pub fn arc_2d(
        &mut self,
        isometry: impl Into<Isometry2d>,
        arc_angle: f32,
        radius: f32,
        color: impl Into<Color>,
    ) -> Arc2dBuilder<'_, 'w, 's, Config, Clear> {
        Arc2dBuilder {
            gizmos: self,
            isometry: isometry.into(),
            arc_angle,
            radius,
            color: color.into(),
            resolution: None,
        }
    }
}

/// A builder returned by [`Gizmos::arc_2d`].
pub struct Arc2dBuilder<'a, 'w, 's, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    gizmos: &'a mut Gizmos<'w, 's, Config, Clear>,
    isometry: Isometry2d,
    arc_angle: f32,
    radius: f32,
    color: Color,
    resolution: Option<u32>,
}

impl<Config, Clear> Arc2dBuilder<'_, '_, '_, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    /// Set the number of lines used to approximate the geometry of this arc.
    pub fn resolution(mut self, resolution: u32) -> Self {
        self.resolution.replace(resolution);
        self
    }
}

impl<Config, Clear> Drop for Arc2dBuilder<'_, '_, '_, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    fn drop(&mut self) {
        if !self.gizmos.enabled {
            return;
        }

        let resolution = self
            .resolution
            .unwrap_or_else(|| resolution_from_angle(self.arc_angle));

        let positions =
            arc_2d_inner(self.arc_angle, self.radius, resolution).map(|vec2| self.isometry * vec2);
        self.gizmos.linestrip_2d(positions, self.color);
    }
}

fn arc_2d_inner(arc_angle: f32, radius: f32, resolution: u32) -> impl Iterator<Item = Vec2> {
    (0..=resolution)
        .map(move |n| arc_angle * n as f32 / resolution as f32)
        .map(|angle| angle + FRAC_PI_2)
        .map(Vec2::from_angle)
        .map(move |vec2| vec2 * radius)
}

// === 3D ===

impl<'w, 's, Config, Clear> Gizmos<'w, 's, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    /// Draw an arc, which is a part of the circumference of a circle, in 3D. For default values
    /// this is drawing a standard arc. A standard arc is defined as
    ///
    /// - an arc with a center at `Vec3::ZERO`
    /// - starting at `Vec3::X`
    /// - embedded in the XZ plane
    /// - rotates counterclockwise
    ///
    /// This should be called for each frame the arc needs to be rendered.
    ///
    /// # Arguments
    /// - `angle`: sets how much of a circle circumference is passed, e.g. PI is half a circle. This
    ///     value should be in the range (-2 * PI..=2 * PI)
    /// - `radius`: distance between the arc and its center point
    /// - `isometry` defines the translation and rotation of the arc.
    ///              - the translation specifies the center of the arc
    ///              - the rotation is counter-clockwise starting from `Vec3::Y`
    /// - `color`: color of the arc
    ///
    /// # Builder methods
    /// The resolution of the arc (i.e. the level of detail) can be adjusted with the
    /// `.resolution(...)` method.
    ///
    /// # Example
    /// ```
    /// # use bevy_gizmos::prelude::*;
    /// # use bevy_math::prelude::*;
    /// # use std::f32::consts::PI;
    /// # use bevy_color::palettes::css::ORANGE;
    /// fn system(mut gizmos: Gizmos) {
    ///     // rotation rotates normal to point in the direction of `Vec3::NEG_ONE`
    ///     let rotation = Quat::from_rotation_arc(Vec3::Y, Vec3::NEG_ONE.normalize());
    ///
    ///     gizmos
    ///        .arc_3d(
    ///          270.0_f32.to_radians(),
    ///          0.25,
    ///          Isometry3d::new(Vec3::ONE, rotation),
    ///          ORANGE
    ///          )
    ///          .resolution(100);
    /// }
    /// # bevy_ecs::system::assert_is_system(system);
    /// ```
    #[inline]
    pub fn arc_3d(
        &mut self,
        angle: f32,
        radius: f32,
        isometry: impl Into<Isometry3d>,
        color: impl Into<Color>,
    ) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
        Arc3dBuilder {
            gizmos: self,
            start_vertex: Vec3::X,
            isometry: isometry.into(),
            angle,
            radius,
            color: color.into(),
            resolution: None,
        }
    }

    /// Draws the shortest arc between two points (`from` and `to`) relative to a specified `center` point.
    ///
    /// # Arguments
    ///
    /// - `center`: The center point around which the arc is drawn.
    /// - `from`: The starting point of the arc.
    /// - `to`: The ending point of the arc.
    /// - `color`: color of the arc
    ///
    /// # Builder methods
    /// The resolution of the arc (i.e. the level of detail) can be adjusted with the
    /// `.resolution(...)` method.
    ///
    /// # Examples
    /// ```
    /// # use bevy_gizmos::prelude::*;
    /// # use bevy_math::prelude::*;
    /// # use bevy_color::palettes::css::ORANGE;
    /// fn system(mut gizmos: Gizmos) {
    ///     gizmos.short_arc_3d_between(
    ///        Vec3::ONE,
    ///        Vec3::ONE + Vec3::NEG_ONE,
    ///        Vec3::ZERO,
    ///        ORANGE
    ///        )
    ///        .resolution(100);
    /// }
    /// # bevy_ecs::system::assert_is_system(system);
    /// ```
    ///
    /// # Notes
    /// - This method assumes that the points `from` and `to` are distinct from `center`. If one of
    ///     the points is coincident with `center`, nothing is rendered.
    /// - The arc is drawn as a portion of a circle with a radius equal to the distance from the
    ///     `center` to `from`. If the distance from `center` to `to` is not equal to the radius, then
    ///     the results will behave as if this were the case
    #[inline]
    pub fn short_arc_3d_between(
        &mut self,
        center: Vec3,
        from: Vec3,
        to: Vec3,
        color: impl Into<Color>,
    ) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
        self.arc_from_to(center, from, to, color, |x| x)
    }

    /// Draws the longest arc between two points (`from` and `to`) relative to a specified `center` point.
    ///
    /// # Arguments
    /// - `center`: The center point around which the arc is drawn.
    /// - `from`: The starting point of the arc.
    /// - `to`: The ending point of the arc.
    /// - `color`: color of the arc
    ///
    /// # Builder methods
    /// The resolution of the arc (i.e. the level of detail) can be adjusted with the
    /// `.resolution(...)` method.
    ///
    /// # Examples
    /// ```
    /// # use bevy_gizmos::prelude::*;
    /// # use bevy_math::prelude::*;
    /// # use bevy_color::palettes::css::ORANGE;
    /// fn system(mut gizmos: Gizmos) {
    ///     gizmos.long_arc_3d_between(
    ///        Vec3::ONE,
    ///        Vec3::ONE + Vec3::NEG_ONE,
    ///        Vec3::ZERO,
    ///        ORANGE
    ///        )
    ///        .resolution(100);
    /// }
    /// # bevy_ecs::system::assert_is_system(system);
    /// ```
    ///
    /// # Notes
    /// - This method assumes that the points `from` and `to` are distinct from `center`. If one of
    ///     the points is coincident with `center`, nothing is rendered.
    /// - The arc is drawn as a portion of a circle with a radius equal to the distance from the
    ///     `center` to `from`. If the distance from `center` to `to` is not equal to the radius, then
    ///     the results will behave as if this were the case.
    #[inline]
    pub fn long_arc_3d_between(
        &mut self,
        center: Vec3,
        from: Vec3,
        to: Vec3,
        color: impl Into<Color>,
    ) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
        self.arc_from_to(center, from, to, color, |angle| {
            if angle > 0.0 {
                TAU - angle
            } else if angle < 0.0 {
                -TAU - angle
            } else {
                0.0
            }
        })
    }

    #[inline]
    fn arc_from_to(
        &mut self,
        center: Vec3,
        from: Vec3,
        to: Vec3,
        color: impl Into<Color>,
        angle_fn: impl Fn(f32) -> f32,
    ) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
        // `from` and `to` can be the same here since in either case nothing gets rendered and the
        // orientation ambiguity of `up` doesn't matter
        let from_axis = (from - center).normalize_or_zero();
        let to_axis = (to - center).normalize_or_zero();
        let (up, angle) = Quat::from_rotation_arc(from_axis, to_axis).to_axis_angle();

        let angle = angle_fn(angle);
        let radius = center.distance(from);
        let rotation = Quat::from_rotation_arc(Vec3::Y, up);

        let start_vertex = rotation.inverse() * from_axis;

        Arc3dBuilder {
            gizmos: self,
            start_vertex,
            isometry: Isometry3d::new(center, rotation),
            angle,
            radius,
            color: color.into(),
            resolution: None,
        }
    }

    /// Draws the shortest arc between two points (`from` and `to`) relative to a specified `center` point.
    ///
    /// # Arguments
    ///
    /// - `center`: The center point around which the arc is drawn.
    /// - `from`: The starting point of the arc.
    /// - `to`: The ending point of the arc.
    /// - `color`: color of the arc
    ///
    /// # Builder methods
    /// The resolution of the arc (i.e. the level of detail) can be adjusted with the
    /// `.resolution(...)` method.
    ///
    /// # Examples
    /// ```
    /// # use bevy_gizmos::prelude::*;
    /// # use bevy_math::prelude::*;
    /// # use bevy_color::palettes::css::ORANGE;
    /// fn system(mut gizmos: Gizmos) {
    ///     gizmos.short_arc_2d_between(
    ///        Vec2::ZERO,
    ///        Vec2::X,
    ///        Vec2::Y,
    ///        ORANGE
    ///        )
    ///        .resolution(100);
    /// }
    /// # bevy_ecs::system::assert_is_system(system);
    /// ```
    ///
    /// # Notes
    /// - This method assumes that the points `from` and `to` are distinct from `center`. If one of
    ///     the points is coincident with `center`, nothing is rendered.
    /// - The arc is drawn as a portion of a circle with a radius equal to the distance from the
    ///     `center` to `from`. If the distance from `center` to `to` is not equal to the radius, then
    ///     the results will behave as if this were the case
    #[inline]
    pub fn short_arc_2d_between(
        &mut self,
        center: Vec2,
        from: Vec2,
        to: Vec2,
        color: impl Into<Color>,
    ) -> Arc2dBuilder<'_, 'w, 's, Config, Clear> {
        self.arc_2d_from_to(center, from, to, color, core::convert::identity)
    }

    /// Draws the longest arc between two points (`from` and `to`) relative to a specified `center` point.
    ///
    /// # Arguments
    /// - `center`: The center point around which the arc is drawn.
    /// - `from`: The starting point of the arc.
    /// - `to`: The ending point of the arc.
    /// - `color`: color of the arc
    ///
    /// # Builder methods
    /// The resolution of the arc (i.e. the level of detail) can be adjusted with the
    /// `.resolution(...)` method.
    ///
    /// # Examples
    /// ```
    /// # use bevy_gizmos::prelude::*;
    /// # use bevy_math::prelude::*;
    /// # use bevy_color::palettes::css::ORANGE;
    /// fn system(mut gizmos: Gizmos) {
    ///     gizmos.long_arc_2d_between(
    ///        Vec2::ZERO,
    ///        Vec2::X,
    ///        Vec2::Y,
    ///        ORANGE
    ///        )
    ///        .resolution(100);
    /// }
    /// # bevy_ecs::system::assert_is_system(system);
    /// ```
    ///
    /// # Notes
    /// - This method assumes that the points `from` and `to` are distinct from `center`. If one of
    ///     the points is coincident with `center`, nothing is rendered.
    /// - The arc is drawn as a portion of a circle with a radius equal to the distance from the
    ///     `center` to `from`. If the distance from `center` to `to` is not equal to the radius, then
    ///     the results will behave as if this were the case.
    #[inline]
    pub fn long_arc_2d_between(
        &mut self,
        center: Vec2,
        from: Vec2,
        to: Vec2,
        color: impl Into<Color>,
    ) -> Arc2dBuilder<'_, 'w, 's, Config, Clear> {
        self.arc_2d_from_to(center, from, to, color, |angle| angle - TAU)
    }

    #[inline]
    fn arc_2d_from_to(
        &mut self,
        center: Vec2,
        from: Vec2,
        to: Vec2,
        color: impl Into<Color>,
        angle_fn: impl Fn(f32) -> f32,
    ) -> Arc2dBuilder<'_, 'w, 's, Config, Clear> {
        // `from` and `to` can be the same here since in either case nothing gets rendered and the
        // orientation ambiguity of `up` doesn't matter
        let from_axis = (from - center).normalize_or_zero();
        let to_axis = (to - center).normalize_or_zero();
        let rotation = Vec2::Y.angle_to(from_axis);
        let arc_angle_raw = from_axis.angle_to(to_axis);

        let arc_angle = angle_fn(arc_angle_raw);
        let radius = center.distance(from);

        Arc2dBuilder {
            gizmos: self,
            isometry: Isometry2d::new(center, Rot2::radians(rotation)),
            arc_angle,
            radius,
            color: color.into(),
            resolution: None,
        }
    }
}

/// A builder returned by [`Gizmos::arc_2d`].
pub struct Arc3dBuilder<'a, 'w, 's, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    gizmos: &'a mut Gizmos<'w, 's, Config, Clear>,
    // this is the vertex the arc starts on in the XZ plane. For the normal arc_3d method this is
    // always starting at Vec3::X. For the short/long arc methods we actually need a way to start
    // at the from position and this is where this internal field comes into play. Some implicit
    // assumptions:
    //
    // 1. This is always in the XZ plane
    // 2. This is always normalized
    //
    // DO NOT expose this field to users as it is easy to mess this up
    start_vertex: Vec3,
    isometry: Isometry3d,
    angle: f32,
    radius: f32,
    color: Color,
    resolution: Option<u32>,
}

impl<Config, Clear> Arc3dBuilder<'_, '_, '_, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    /// Set the number of lines for this arc.
    pub fn resolution(mut self, resolution: u32) -> Self {
        self.resolution.replace(resolution);
        self
    }
}

impl<Config, Clear> Drop for Arc3dBuilder<'_, '_, '_, Config, Clear>
where
    Config: GizmoConfigGroup,
    Clear: 'static + Send + Sync,
{
    fn drop(&mut self) {
        if !self.gizmos.enabled {
            return;
        }

        let resolution = self
            .resolution
            .unwrap_or_else(|| resolution_from_angle(self.angle));

        let positions = arc_3d_inner(
            self.start_vertex,
            self.isometry,
            self.angle,
            self.radius,
            resolution,
        );
        self.gizmos.linestrip(positions, self.color);
    }
}

fn arc_3d_inner(
    start_vertex: Vec3,
    isometry: Isometry3d,
    angle: f32,
    radius: f32,
    resolution: u32,
) -> impl Iterator<Item = Vec3> {
    // drawing arcs bigger than TAU degrees or smaller than -TAU degrees makes no sense since
    // we won't see the overlap and we would just decrease the level of details since the resolution
    // would be larger
    let angle = angle.clamp(-TAU, TAU);
    (0..=resolution)
        .map(move |frac| frac as f32 / resolution as f32)
        .map(move |percentage| angle * percentage)
        .map(move |frac_angle| Quat::from_axis_angle(Vec3::Y, frac_angle) * start_vertex)
        .map(move |vec3| vec3 * radius)
        .map(move |vec3| isometry * vec3)
}

// helper function for getting a default value for the resolution parameter
fn resolution_from_angle(angle: f32) -> u32 {
    ((angle.abs() / TAU) * DEFAULT_CIRCLE_RESOLUTION as f32).ceil() as u32
}