Skip to content

Commit

Permalink
Add with_x, with_y, etc. to vector types. (#493)
Browse files Browse the repository at this point in the history
Fixes #493
  • Loading branch information
bitshifter committed Mar 14, 2024
1 parent 30948c4 commit 246ae4c
Show file tree
Hide file tree
Showing 36 changed files with 803 additions and 3 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ The format is based on [Keep a Changelog], and this project adheres to

### Added

* Added `midpoint` method to vector types that returns the point between two
* Added `with_x()`, `with_y()`, etc. to vector types which returns a copy of
the vector with the new component value.

* Added `midpoint()` method to vector types that returns the point between two
points.

* Added saturating add and sub methods for signed and unsigned integer vector
Expand All @@ -33,9 +36,9 @@ The format is based on [Keep a Changelog], and this project adheres to

* Added element wise absolute values method for matrix types.

* Added `from_array` method for boolean vector types.
* Added `from_array()` method for boolean vector types.

* Added `normalize_or` method to vector types that returns the specified value
* Added `normalize_or()` method to vector types that returns the specified value
if normalization failed.

* Added `From<BVecN>` support for all vector types.
Expand Down
11 changes: 11 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,17 @@ impl {{ self_t }} {
}
{% endif %}


{% for c in components %}
/// Creates a {{ dim }}D vector from `self` with the given value of `{{ c }}`.
#[inline]
#[must_use]
pub fn with_{{ c }}(mut self, {{ c }}: {{ scalar_t }}) -> Self {
self.{{ c }} = {{ c }};
self
}
{% endfor %}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
24 changes: 24 additions & 0 deletions src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@ impl Vec3A {
self.xy()
}

/// Creates a 3D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 3D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 3D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
32 changes: 32 additions & 0 deletions src/f32/coresimd/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,38 @@ impl Vec4 {
self.xyz()
}

/// Creates a 4D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 4D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 4D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Creates a 4D vector from `self` with the given value of `w`.
#[inline]
#[must_use]
pub fn with_w(mut self, w: f32) -> Self {
self.w = w;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
24 changes: 24 additions & 0 deletions src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ impl Vec3A {
self.xy()
}

/// Creates a 3D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 3D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 3D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
32 changes: 32 additions & 0 deletions src/f32/scalar/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,38 @@ impl Vec4 {
self.xyz()
}

/// Creates a 4D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 4D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 4D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Creates a 4D vector from `self` with the given value of `w`.
#[inline]
#[must_use]
pub fn with_w(mut self, w: f32) -> Self {
self.w = w;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
24 changes: 24 additions & 0 deletions src/f32/sse2/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ impl Vec3A {
self.xy()
}

/// Creates a 3D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 3D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 3D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
32 changes: 32 additions & 0 deletions src/f32/sse2/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ impl Vec4 {
self.xyz()
}

/// Creates a 4D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 4D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 4D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Creates a 4D vector from `self` with the given value of `w`.
#[inline]
#[must_use]
pub fn with_w(mut self, w: f32) -> Self {
self.w = w;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
16 changes: 16 additions & 0 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ impl Vec2 {
Vec3::new(self.x, self.y, z)
}

/// Creates a 2D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 2D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
24 changes: 24 additions & 0 deletions src/f32/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ impl Vec3 {
self.xy()
}

/// Creates a 3D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 3D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 3D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
24 changes: 24 additions & 0 deletions src/f32/wasm32/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ impl Vec3A {
self.xy()
}

/// Creates a 3D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 3D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 3D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
32 changes: 32 additions & 0 deletions src/f32/wasm32/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,38 @@ impl Vec4 {
self.xyz()
}

/// Creates a 4D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}

/// Creates a 4D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}

/// Creates a 4D vector from `self` with the given value of `z`.
#[inline]
#[must_use]
pub fn with_z(mut self, z: f32) -> Self {
self.z = z;
self
}

/// Creates a 4D vector from `self` with the given value of `w`.
#[inline]
#[must_use]
pub fn with_w(mut self, w: f32) -> Self {
self.w = w;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down
16 changes: 16 additions & 0 deletions src/f64/dvec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ impl DVec2 {
DVec3::new(self.x, self.y, z)
}

/// Creates a 2D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f64) -> Self {
self.x = x;
self
}

/// Creates a 2D vector from `self` with the given value of `y`.
#[inline]
#[must_use]
pub fn with_y(mut self, y: f64) -> Self {
self.y = y;
self
}

/// Computes the dot product of `self` and `rhs`.
#[inline]
#[must_use]
Expand Down

0 comments on commit 246ae4c

Please sign in to comment.