Skip to content

Commit 8b69a81

Browse files
committed
Inline more
1 parent 3b3fcfb commit 8b69a81

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ impl Primitive2d for Segment2d {}
233233

234234
impl Segment2d {
235235
/// Create a new `Segment2d` from a direction and full length of the segment
236+
#[inline(always)]
236237
pub fn new(direction: Direction2d, length: f32) -> Self {
237238
Self {
238239
direction,
@@ -245,6 +246,7 @@ impl Segment2d {
245246
/// # Panics
246247
///
247248
/// Panics if `point1 == point2`
249+
#[inline(always)]
248250
pub fn from_points(point1: Vec2, point2: Vec2) -> (Self, Vec2) {
249251
let diff = point2 - point1;
250252
let length = diff.length();
@@ -257,11 +259,13 @@ impl Segment2d {
257259
}
258260

259261
/// Get the position of the first point on the line segment
262+
#[inline(always)]
260263
pub fn point1(&self) -> Vec2 {
261264
*self.direction * -self.half_length
262265
}
263266

264267
/// Get the position of the second point on the line segment
268+
#[inline(always)]
265269
pub fn point2(&self) -> Vec2 {
266270
*self.direction * self.half_length
267271
}
@@ -332,19 +336,22 @@ impl Primitive2d for Triangle2d {}
332336

333337
impl Triangle2d {
334338
/// Create a new `Triangle2d` from points `a`, `b`, and `c`
339+
#[inline(always)]
335340
pub fn new(a: Vec2, b: Vec2, c: Vec2) -> Self {
336341
Self {
337342
vertices: [a, b, c],
338343
}
339344
}
340345

341346
/// Get the area of the triangle
347+
#[inline(always)]
342348
pub fn area(&self) -> f32 {
343349
let [a, b, c] = self.vertices;
344350
(a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)).abs() / 2.0
345351
}
346352

347353
/// Get the perimeter of the triangle
354+
#[inline(always)]
348355
pub fn perimeter(&self) -> f32 {
349356
let [a, b, c] = self.vertices;
350357

@@ -356,6 +363,7 @@ impl Triangle2d {
356363
}
357364

358365
/// Get the [`WindingOrder`] of the triangle
366+
#[inline(always)]
359367
#[doc(alias = "orientation")]
360368
pub fn winding_order(&self) -> WindingOrder {
361369
let [a, b, c] = self.vertices;
@@ -406,6 +414,7 @@ impl Triangle2d {
406414

407415
/// Reverse the [`WindingOrder`] of the triangle
408416
/// by swapping the second and third vertices
417+
#[inline(always)]
409418
pub fn reverse(&mut self) {
410419
self.vertices.swap(1, 2);
411420
}
@@ -421,35 +430,41 @@ pub struct Rectangle {
421430

422431
impl Rectangle {
423432
/// Create a new `Rectangle` from a full width and height
433+
#[inline(always)]
424434
pub fn new(width: f32, height: f32) -> Self {
425435
Self::from_size(Vec2::new(width, height))
426436
}
427437

428438
/// Create a new `Rectangle` from a given full size
439+
#[inline(always)]
429440
pub fn from_size(size: Vec2) -> Self {
430441
Self {
431442
half_size: size / 2.0,
432443
}
433444
}
434445

435446
/// Create a new `Rectangle` from two corner points
447+
#[inline(always)]
436448
pub fn from_corners(point1: Vec2, point2: Vec2) -> Self {
437449
Self {
438450
half_size: (point2 - point1).abs() / 2.0,
439451
}
440452
}
441453

442454
/// Get the size of the rectangle
455+
#[inline(always)]
443456
pub fn size(&self) -> Vec2 {
444457
2.0 * self.half_size
445458
}
446459

447460
/// Get the area of the rectangle
461+
#[inline(always)]
448462
pub fn area(&self) -> f32 {
449463
4.0 * self.half_size.x * self.half_size.y
450464
}
451465

452466
/// Get the perimeter of the rectangle
467+
#[inline(always)]
453468
pub fn perimeter(&self) -> f32 {
454469
4.0 * (self.half_size.x + self.half_size.y)
455470
}
@@ -537,6 +552,7 @@ impl RegularPolygon {
537552
/// # Panics
538553
///
539554
/// Panics if `circumradius` is non-positive
555+
#[inline(always)]
540556
pub fn new(circumradius: f32, sides: usize) -> Self {
541557
assert!(circumradius > 0.0, "polygon has a non-positive radius");
542558
assert!(sides > 2, "polygon has less than 3 sides");
@@ -551,31 +567,36 @@ impl RegularPolygon {
551567

552568
/// Get the radius of the circumcircle on which all vertices
553569
/// of the regular polygon lie
570+
#[inline(always)]
554571
pub fn circumradius(&self) -> f32 {
555572
self.circumcircle.radius
556573
}
557574

558575
/// Get the inradius or apothem of the regular polygon.
559576
/// This is the radius of the largest circle that can
560577
/// be drawn within the polygon
578+
#[inline(always)]
561579
#[doc(alias = "apothem")]
562580
pub fn inradius(&self) -> f32 {
563581
self.circumradius() * (PI / self.sides as f32).cos()
564582
}
565583

566584
/// Get the length of one side of the regular polygon
585+
#[inline(always)]
567586
pub fn side_length(&self) -> f32 {
568587
2.0 * self.circumradius() * (PI / self.sides as f32).sin()
569588
}
570589

571590
/// Get the area of the regular polygon
591+
#[inline(always)]
572592
pub fn area(&self) -> f32 {
573593
let angle: f32 = 2.0 * PI / (self.sides as f32);
574594
(self.sides as f32) * self.circumradius().powi(2) * angle.sin() / 2.0
575595
}
576596

577597
/// Get the perimeter of the regular polygon.
578598
/// This is the sum of its sides
599+
#[inline(always)]
579600
pub fn perimeter(&self) -> f32 {
580601
self.sides as f32 * self.side_length()
581602
}
@@ -584,6 +605,7 @@ impl RegularPolygon {
584605
///
585606
/// This is the angle formed by two adjacent sides with points
586607
/// within the angle being in the interior of the polygon
608+
#[inline(always)]
587609
pub fn internal_angle_degrees(&self) -> f32 {
588610
(self.sides - 2) as f32 / self.sides as f32 * 180.0
589611
}
@@ -592,6 +614,7 @@ impl RegularPolygon {
592614
///
593615
/// This is the angle formed by two adjacent sides with points
594616
/// within the angle being in the interior of the polygon
617+
#[inline(always)]
595618
pub fn internal_angle_radians(&self) -> f32 {
596619
(self.sides - 2) as f32 * PI / self.sides as f32
597620
}
@@ -600,6 +623,7 @@ impl RegularPolygon {
600623
///
601624
/// This is the angle formed by two adjacent sides with points
602625
/// within the angle being in the exterior of the polygon
626+
#[inline(always)]
603627
pub fn external_angle_degrees(&self) -> f32 {
604628
360.0 / self.sides as f32
605629
}
@@ -608,6 +632,7 @@ impl RegularPolygon {
608632
///
609633
/// This is the angle formed by two adjacent sides with points
610634
/// within the angle being in the exterior of the polygon
635+
#[inline(always)]
611636
pub fn external_angle_radians(&self) -> f32 {
612637
2.0 * PI / self.sides as f32
613638
}

0 commit comments

Comments
 (0)