mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 18:54:05 +02:00
split off vector
This commit is contained in:
parent
a0e505d907
commit
7216783734
2 changed files with 84 additions and 79 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
mod vector;
|
||||||
|
|
||||||
use crate::bspfile::LumpType;
|
use crate::bspfile::LumpType;
|
||||||
use crate::StringError;
|
use crate::StringError;
|
||||||
use arrayvec::ArrayString;
|
use arrayvec::ArrayString;
|
||||||
|
|
@ -6,90 +8,12 @@ use binrw::{BinRead, BinResult, ReadOptions};
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use bv::BitVec;
|
use bv::BitVec;
|
||||||
use num_enum::TryFromPrimitive;
|
use num_enum::TryFromPrimitive;
|
||||||
use std::cmp::Ordering;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::io::{Read, Seek};
|
use std::io::{Read, Seek};
|
||||||
use std::mem::{align_of, size_of};
|
use std::mem::{align_of, size_of};
|
||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
use std::ops::{Add, Mul, Sub};
|
pub use vector::Vector;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, BinRead)]
|
|
||||||
pub struct Vector {
|
|
||||||
pub x: f32,
|
|
||||||
pub y: f32,
|
|
||||||
pub z: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Vector {
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = f32> {
|
|
||||||
[self.x, self.y, self.z].into_iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn length_squared(&self) -> f32 {
|
|
||||||
self.x.powf(2.0) + self.y.powf(2.0) + self.z.powf(2.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add<Vector> for Vector {
|
|
||||||
type Output = Vector;
|
|
||||||
|
|
||||||
fn add(self, rhs: Vector) -> Self::Output {
|
|
||||||
Vector {
|
|
||||||
x: self.x + rhs.x,
|
|
||||||
y: self.y + rhs.y,
|
|
||||||
z: self.z + rhs.z,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub<Vector> for Vector {
|
|
||||||
type Output = Vector;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Vector) -> Self::Output {
|
|
||||||
Vector {
|
|
||||||
x: self.x - rhs.x,
|
|
||||||
y: self.y - rhs.y,
|
|
||||||
z: self.z - rhs.z,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul<f32> for Vector {
|
|
||||||
type Output = Vector;
|
|
||||||
|
|
||||||
fn mul(self, rhs: f32) -> Self::Output {
|
|
||||||
Vector {
|
|
||||||
x: self.x * rhs,
|
|
||||||
y: self.y * rhs,
|
|
||||||
z: self.z * rhs,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for Vector {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.x == other.x && self.y == other.y && self.z == other.z
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for Vector {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
self.length_squared().partial_cmp(&other.length_squared())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Vector> for [f32; 3] {
|
|
||||||
fn from(vector: Vector) -> Self {
|
|
||||||
[vector.x, vector.y, vector.z]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&Vector> for [f32; 3] {
|
|
||||||
fn from(vector: &Vector) -> Self {
|
|
||||||
[vector.x, vector.y, vector.z]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn test_read_bytes<T: BinRead>()
|
fn test_read_bytes<T: BinRead>()
|
||||||
81
src/data/vector.rs
Normal file
81
src/data/vector.rs
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
use binrw::BinRead;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use std::ops::{Add, Mul, Sub};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, BinRead)]
|
||||||
|
pub struct Vector {
|
||||||
|
pub x: f32,
|
||||||
|
pub y: f32,
|
||||||
|
pub z: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Vector {
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = f32> {
|
||||||
|
[self.x, self.y, self.z].into_iter()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn length_squared(&self) -> f32 {
|
||||||
|
self.x.powf(2.0) + self.y.powf(2.0) + self.z.powf(2.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add<Vector> for Vector {
|
||||||
|
type Output = Vector;
|
||||||
|
|
||||||
|
fn add(self, rhs: Vector) -> Self::Output {
|
||||||
|
Vector {
|
||||||
|
x: self.x + rhs.x,
|
||||||
|
y: self.y + rhs.y,
|
||||||
|
z: self.z + rhs.z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub<Vector> for Vector {
|
||||||
|
type Output = Vector;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Vector) -> Self::Output {
|
||||||
|
Vector {
|
||||||
|
x: self.x - rhs.x,
|
||||||
|
y: self.y - rhs.y,
|
||||||
|
z: self.z - rhs.z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<f32> for Vector {
|
||||||
|
type Output = Vector;
|
||||||
|
|
||||||
|
fn mul(self, rhs: f32) -> Self::Output {
|
||||||
|
Vector {
|
||||||
|
x: self.x * rhs,
|
||||||
|
y: self.y * rhs,
|
||||||
|
z: self.z * rhs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Vector {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.x == other.x && self.y == other.y && self.z == other.z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for Vector {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
self.length_squared().partial_cmp(&other.length_squared())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Vector> for [f32; 3] {
|
||||||
|
fn from(vector: Vector) -> Self {
|
||||||
|
[vector.x, vector.y, vector.z]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Vector> for [f32; 3] {
|
||||||
|
fn from(vector: &Vector) -> Self {
|
||||||
|
[vector.x, vector.y, vector.z]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue