You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

713 lines
15 KiB

  1. #![allow(dead_code)]
  2. use std::convert::{AsMut, AsRef};
  3. use std::fmt::{Debug, Formatter, Result as FmtResult};
  4. use std::ops::{Add, Deref, DerefMut, Mul, Sub};
  5. #[cfg(feature = "serde")]
  6. use serde::{
  7. de::{Deserialize, Deserializer, Error},
  8. ser::{Serialize, Serializer},
  9. };
  10. pub use super::{
  11. angle::Angle,
  12. numeric::{Float, Numeric},
  13. };
  14. macro_rules! first_ptr {
  15. ($this:ident, $first:ident $(, $other:ident)*) => {
  16. unsafe { &$this.$first }
  17. };
  18. }
  19. macro_rules! define_vec {
  20. ($Name:ident, $size:tt, $($T:ident => $i:tt => $f:ident),*) => {
  21. #[repr(C, packed)]
  22. pub struct $Name<T> {
  23. $(pub $f: T,)+
  24. }
  25. impl<T> $Name<T> {
  26. #[inline]
  27. pub fn new($($f: T,)+) -> Self {
  28. Self { $($f,)+ }
  29. }
  30. #[inline]
  31. pub fn as_ptr(&self) -> * const T {
  32. first_ptr!(self $(, $f)+)
  33. }
  34. }
  35. impl<T> Default for $Name<T>
  36. where
  37. T: Default
  38. {
  39. #[inline]
  40. fn default() -> Self {
  41. Self {
  42. $($f: T::default(),)+
  43. }
  44. }
  45. }
  46. impl<T> Debug for $Name<T>
  47. where
  48. T: Debug
  49. {
  50. fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
  51. fmt.debug_list().entries(self.as_ref().iter()).finish()
  52. }
  53. }
  54. impl<T, S> PartialEq<$Name<S>> for $Name<T>
  55. where
  56. S: PartialEq<T>,
  57. {
  58. fn eq(&self, other: &$Name<S>) -> bool {
  59. unsafe { true $(&& other.$f.eq(&self.$f))+ }
  60. }
  61. }
  62. impl<T> Eq for $Name<T>
  63. where
  64. T: Eq
  65. { }
  66. impl<T> Deref for $Name<T> {
  67. type Target = [T; $size];
  68. #[inline]
  69. fn deref(&self) -> &Self::Target {
  70. self.as_ref()
  71. }
  72. }
  73. impl<T> DerefMut for $Name<T> {
  74. #[inline]
  75. fn deref_mut(&mut self) -> &mut Self::Target {
  76. self.as_mut()
  77. }
  78. }
  79. impl<T> AsRef<[T; $size]> for $Name<T> {
  80. #[inline]
  81. fn as_ref(&self) -> &[T; $size] {
  82. unsafe {
  83. let raw: * const _ = self;
  84. let raw = raw as * const [T; $size];
  85. &*raw
  86. }
  87. }
  88. }
  89. impl<T> AsMut<[T; $size]> for $Name<T> {
  90. #[inline]
  91. fn as_mut(&mut self) -> &mut [T; $size] {
  92. unsafe {
  93. let raw: * mut _ = self;
  94. let raw = raw as * mut [T; $size];
  95. &mut *raw
  96. }
  97. }
  98. }
  99. impl<T> Clone for $Name<T>
  100. where
  101. T: Clone
  102. {
  103. #[inline]
  104. fn clone(&self) -> Self {
  105. unsafe {
  106. Self {
  107. $($f: self.$f.clone(),)+
  108. }
  109. }
  110. }
  111. }
  112. impl<T> Copy for $Name<T>
  113. where
  114. T: Copy
  115. { }
  116. impl<T> From<[T; $size]> for $Name<T>
  117. where
  118. T: Copy,
  119. {
  120. #[inline]
  121. fn from(arr: [T; $size]) -> Self {
  122. Self {
  123. $($f: arr[$i],)+
  124. }
  125. }
  126. }
  127. impl<T> From<T> for $Name<T>
  128. where
  129. T: Copy,
  130. {
  131. #[inline]
  132. fn from(value: T) -> Self {
  133. Self {
  134. $($f: value,)+
  135. }
  136. }
  137. }
  138. impl<T> From<($($T,)+)> for $Name<T> {
  139. #[inline]
  140. fn from(($($f,)+): ($($T,)+)) -> Self {
  141. Self {
  142. $($f,)+
  143. }
  144. }
  145. }
  146. #[cfg(feature = "serde")]
  147. impl<T> Serialize for $Name<T>
  148. where
  149. T: Serialize,
  150. {
  151. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  152. where
  153. S: Serializer
  154. {
  155. unsafe { vec![$(&self.$f,)+].serialize(serializer) }
  156. }
  157. }
  158. #[cfg(feature = "serde")]
  159. impl<'de, T> Deserialize<'de> for $Name<T>
  160. where
  161. T: Deserialize<'de>,
  162. {
  163. fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  164. where
  165. D: Deserializer<'de>
  166. {
  167. let mut data = Vec::<T>::deserialize(deserializer)?.into_iter();
  168. Ok(Self {
  169. $($f: data.next().ok_or_else(|| D::Error::custom("Vector is missing some elements!"))?,)+
  170. })
  171. }
  172. }
  173. };
  174. }
  175. define_vec!(Vector2, 2, T => 0 => x, T => 1 => y);
  176. define_vec!(Vector3, 3, T => 0 => x, T => 1 => y, T => 2 => z);
  177. define_vec!(Vector4, 4, T => 0 => x, T => 1 => y, T => 2 => z, T => 3 => w);
  178. pub type Vector2f = Vector2<gl::GLfloat>;
  179. pub type Vector3f = Vector3<gl::GLfloat>;
  180. pub type Vector4f = Vector4<gl::GLfloat>;
  181. pub type Vector2d = Vector2<gl::GLdouble>;
  182. pub type Vector3d = Vector3<gl::GLdouble>;
  183. pub type Vector4d = Vector4<gl::GLdouble>;
  184. pub type Vector2i = Vector2<gl::GLint>;
  185. pub type Vector3i = Vector3<gl::GLint>;
  186. pub type Vector4i = Vector4<gl::GLint>;
  187. /* Vector2 */
  188. impl<T> Vector2<T>
  189. where
  190. T: Numeric,
  191. {
  192. #[inline]
  193. pub fn multiply(mut self, v: T) -> Self {
  194. self.x = self.x * v;
  195. self.y = self.y * v;
  196. self
  197. }
  198. #[inline]
  199. pub fn length_sqr(&self) -> T {
  200. self.x * self.x + self.y * self.y
  201. }
  202. #[inline]
  203. pub fn length(&self) -> T {
  204. Numeric::sqrt(self.length_sqr())
  205. }
  206. #[inline]
  207. pub fn normalize(mut self) -> Self {
  208. let len = self.length();
  209. self.x = self.x / len;
  210. self.y = self.y / len;
  211. self
  212. }
  213. #[inline]
  214. pub fn scalar(&self, other: &Self) -> T {
  215. self.x * other.x + self.y * other.y
  216. }
  217. }
  218. impl<T> Vector2<T>
  219. where
  220. T: Float,
  221. {
  222. #[inline]
  223. pub fn angle(&self, other: &Self) -> Angle<T> {
  224. let s = self.scalar(other);
  225. let l1 = self.length();
  226. let l2 = self.length();
  227. Angle::Rad(T::acos(s / (l1 + l2)))
  228. }
  229. #[inline]
  230. pub fn angle2(&self, other: &Self) -> Angle<T> {
  231. Angle::Rad(T::atan2(
  232. other.x * self.y - other.y * self.x,
  233. other.x * self.x + other.y * self.y,
  234. ))
  235. }
  236. }
  237. impl<T> Add for Vector2<T>
  238. where
  239. T: Numeric,
  240. {
  241. type Output = Self;
  242. #[inline]
  243. fn add(mut self, other: Self) -> Self::Output {
  244. self.x = self.x + other.x;
  245. self.y = self.y + other.y;
  246. self
  247. }
  248. }
  249. impl<T> Sub for Vector2<T>
  250. where
  251. T: Numeric,
  252. {
  253. type Output = Self;
  254. #[inline]
  255. fn sub(mut self, other: Self) -> Self::Output {
  256. self.x = self.x - other.x;
  257. self.y = self.y - other.y;
  258. self
  259. }
  260. }
  261. impl<T> Mul for Vector2<T>
  262. where
  263. T: Numeric,
  264. {
  265. type Output = T;
  266. #[inline]
  267. fn mul(self, rhs: Self) -> Self::Output {
  268. self.scalar(&rhs)
  269. }
  270. }
  271. impl<T> Mul<T> for Vector2<T>
  272. where
  273. T: Numeric,
  274. {
  275. type Output = Self;
  276. #[inline]
  277. fn mul(self, rhs: T) -> Self::Output {
  278. self.multiply(rhs)
  279. }
  280. }
  281. /* Vector3 */
  282. impl<T> Vector3<T>
  283. where
  284. T: Numeric,
  285. {
  286. #[inline]
  287. pub fn multiply(mut self, v: T) -> Self {
  288. self.x = self.x * v;
  289. self.y = self.y * v;
  290. self.z = self.z * v;
  291. self
  292. }
  293. #[inline]
  294. pub fn length_sqr(&self) -> T {
  295. self.x * self.x + self.y * self.y + self.z * self.z
  296. }
  297. #[inline]
  298. pub fn length(&self) -> T {
  299. Numeric::sqrt(self.length_sqr())
  300. }
  301. #[inline]
  302. pub fn normalize(mut self) -> Self {
  303. let len = self.length();
  304. self.x = self.x / len;
  305. self.y = self.y / len;
  306. self.z = self.z / len;
  307. self
  308. }
  309. #[inline]
  310. pub fn scalar(&self, other: &Self) -> T {
  311. self.x * other.x + self.y * other.y + self.z * other.z
  312. }
  313. #[inline]
  314. pub fn cross(&self, other: &Self) -> Self {
  315. Self {
  316. x: self.y * other.z - self.z * other.y,
  317. y: self.z * other.x - self.x * other.z,
  318. z: self.x * other.y - self.y * other.x,
  319. }
  320. }
  321. }
  322. impl<T> Vector3<T>
  323. where
  324. T: Float,
  325. {
  326. #[inline]
  327. pub fn angle(&self, other: &Self) -> Angle<T> {
  328. let s = self.scalar(other);
  329. let l1 = self.length();
  330. let l2 = self.length();
  331. Angle::Rad(T::acos(s / (l1 + l2)))
  332. }
  333. }
  334. impl<T> Add for Vector3<T>
  335. where
  336. T: Numeric,
  337. {
  338. type Output = Self;
  339. #[inline]
  340. fn add(mut self, other: Self) -> Self::Output {
  341. self.x = self.x + other.x;
  342. self.y = self.y + other.y;
  343. self.z = self.z + other.z;
  344. self
  345. }
  346. }
  347. impl<T> Sub for Vector3<T>
  348. where
  349. T: Numeric,
  350. {
  351. type Output = Self;
  352. #[inline]
  353. fn sub(mut self, other: Self) -> Self::Output {
  354. self.x = self.x - other.x;
  355. self.y = self.y - other.y;
  356. self.z = self.z - other.z;
  357. self
  358. }
  359. }
  360. impl<T> Mul for Vector3<T>
  361. where
  362. T: Numeric,
  363. {
  364. type Output = T;
  365. #[inline]
  366. fn mul(self, rhs: Self) -> Self::Output {
  367. self.scalar(&rhs)
  368. }
  369. }
  370. impl<T> Mul<T> for Vector3<T>
  371. where
  372. T: Numeric,
  373. {
  374. type Output = Self;
  375. #[inline]
  376. fn mul(self, rhs: T) -> Self::Output {
  377. self.multiply(rhs)
  378. }
  379. }
  380. /* Vector4 */
  381. impl<T> Vector4<T>
  382. where
  383. T: Numeric,
  384. {
  385. #[inline]
  386. pub fn multiply(mut self, v: T) -> Self {
  387. self.x = self.x * v;
  388. self.y = self.y * v;
  389. self.z = self.z * v;
  390. self
  391. }
  392. #[inline]
  393. pub fn length_sqr(self) -> T {
  394. self.xyz().length_sqr()
  395. }
  396. #[inline]
  397. pub fn length(self) -> T {
  398. self.xyz().length()
  399. }
  400. #[inline]
  401. pub fn normalize(mut self) -> Self {
  402. let len = self.length();
  403. if unsafe { !Numeric::is_zero(&self.w) } {
  404. self.x = self.x / self.w;
  405. self.y = self.y / self.w;
  406. self.z = self.z / self.w;
  407. } else {
  408. self.x = Numeric::zero();
  409. self.y = Numeric::zero();
  410. self.z = Numeric::zero();
  411. }
  412. self.x = self.x / len;
  413. self.y = self.y / len;
  414. self.z = self.z / len;
  415. self.w = Numeric::one();
  416. self
  417. }
  418. #[inline]
  419. pub fn scalar(&self, other: &Self) -> T {
  420. self.xyz().scalar(&other.xyz())
  421. }
  422. #[inline]
  423. pub fn cross(&self, other: &Self) -> Self {
  424. (self.xyz().cross(&other.xyz()), Numeric::one()).into()
  425. }
  426. #[inline]
  427. pub fn xyz(self) -> Vector3<T> {
  428. if unsafe { Numeric::is_zero(&self.w) } {
  429. Vector3 {
  430. x: Numeric::zero(),
  431. y: Numeric::zero(),
  432. z: Numeric::zero(),
  433. }
  434. } else {
  435. Vector3 {
  436. x: self.x / self.w,
  437. y: self.y / self.w,
  438. z: self.z / self.w,
  439. }
  440. }
  441. }
  442. }
  443. impl<T> Vector4<T>
  444. where
  445. T: Float,
  446. {
  447. #[inline]
  448. pub fn angle(&self, other: &Self) -> Angle<T> {
  449. self.xyz().angle(&other.xyz())
  450. }
  451. }
  452. impl<T> Add for Vector4<T>
  453. where
  454. T: Numeric,
  455. {
  456. type Output = Self;
  457. #[inline]
  458. fn add(self, other: Self) -> Self::Output {
  459. (self.xyz() + other.xyz(), T::one()).into()
  460. }
  461. }
  462. impl<T> Sub for Vector4<T>
  463. where
  464. T: Numeric,
  465. {
  466. type Output = Self;
  467. #[inline]
  468. fn sub(self, other: Self) -> Self::Output {
  469. (self.xyz() - other.xyz(), T::one()).into()
  470. }
  471. }
  472. impl<T> Mul for Vector4<T>
  473. where
  474. T: Numeric,
  475. {
  476. type Output = T;
  477. fn mul(self, rhs: Self) -> Self::Output {
  478. self.scalar(&rhs)
  479. }
  480. }
  481. impl<T> Mul<T> for Vector4<T>
  482. where
  483. T: Numeric,
  484. {
  485. type Output = Self;
  486. #[inline]
  487. fn mul(self, rhs: T) -> Self::Output {
  488. self.multiply(rhs)
  489. }
  490. }
  491. impl<T> From<(Vector3<T>, T)> for Vector4<T> {
  492. fn from((vec3, w): (Vector3<T>, T)) -> Self {
  493. Self {
  494. x: vec3.x,
  495. y: vec3.y,
  496. z: vec3.z,
  497. w,
  498. }
  499. }
  500. }
  501. #[cfg(test)]
  502. mod tests {
  503. use super::*;
  504. #[test]
  505. fn add() {
  506. let v1 = Vector2f::from((1.0, 0.0));
  507. let v2 = Vector2f::from((0.0, 1.0));
  508. let ex = Vector2f::from((1.0, 1.0));
  509. assert_eq!(ex, v1 + v2);
  510. let v1 = Vector3f::from((1.0, 0.0, 1.8));
  511. let v2 = Vector3f::from((0.0, 2.0, 1.2));
  512. let ex = Vector3f::from((1.0, 2.0, 3.0));
  513. assert_eq!(ex, v1 + v2);
  514. let v1 = Vector4f::from((1.0, 0.0, 1.8, 0.5));
  515. let v2 = Vector4f::from((0.0, 2.0, 1.2, 1.0));
  516. let ex = Vector4f::from((2.0, 2.0, 4.8, 1.0));
  517. assert_eq!(ex, v1 + v2);
  518. }
  519. #[test]
  520. fn sub() {
  521. let v1 = Vector2f::from((1.0, 0.0));
  522. let v2 = Vector2f::from((0.0, 1.0));
  523. let ex = Vector2f::from((1.0, -1.0));
  524. assert_eq!(ex, v1 - v2);
  525. let v1 = Vector3d::from((1.0, 0.0, 1.75));
  526. let v2 = Vector3d::from((0.0, 2.0, 1.0));
  527. let ex = Vector3d::from((1.0, -2.0, 0.75));
  528. assert_eq!(ex, v1 - v2);
  529. let v1 = Vector4d::from((1.0, 0.0, 1.0, 0.5));
  530. let v2 = Vector4d::from((0.0, 2.0, -2.0, 1.0));
  531. let ex = Vector4d::from((2.0, -2.0, 4.0, 1.0));
  532. assert_eq!(ex, v1 - v2);
  533. }
  534. #[test]
  535. fn multiply() {
  536. let v1 = Vector2f::from((1.0, 0.0)).multiply(2.0);
  537. let ex = Vector2f::from((2.0, 0.0));
  538. assert_eq!(ex, v1);
  539. let v1 = Vector3d::from((1.0, 0.0, 1.75)).multiply(2.0);
  540. let ex = Vector3d::from((2.0, 0.0, 3.5));
  541. assert_eq!(ex, v1);
  542. let v1 = Vector4d::from((1.0, 0.0, 0.5, 0.5)).multiply(3.0);
  543. let ex = Vector4d::from((3.0, 0.0, 1.5, 0.5));
  544. assert_eq!(ex, v1);
  545. }
  546. #[test]
  547. fn scalar() {
  548. let v1 = Vector2f::from((1.0, 0.0));
  549. let v2 = Vector2f::from((0.0, 1.0));
  550. assert_eq!(0.0, v1.scalar(&v2));
  551. let v1 = Vector3f::from((1.0, 0.0, 0.0));
  552. let v2 = Vector3f::from((0.0, 1.0, 0.0));
  553. assert_eq!(0.0, v1.scalar(&v2));
  554. let v1 = Vector4f::from((3.0, 0.0, 0.0, 1.0));
  555. let v2 = Vector4f::from((0.0, 2.0, 0.0, 0.8));
  556. assert_eq!(0.0, v1.scalar(&v2));
  557. }
  558. #[test]
  559. fn normalize() {
  560. let v1 = Vector2f::from((1.0, 0.0)).normalize();
  561. let ex = Vector2f::from((1.0, 0.0));
  562. assert_eq!(ex, v1);
  563. let v1 = Vector3d::from((1.0, 0.0, 1.75)).normalize();
  564. let ex = Vector3d::from((0.49613893835683387, 0.0, 0.8682431421244593));
  565. assert_eq!(ex, v1);
  566. let v1 = Vector4d::from((1.0, 0.0, 0.5, 0.5)).normalize();
  567. let ex = Vector4d::from((0.8944271909999159, 0.0, 0.4472135954999579, 1.0));
  568. assert_eq!(ex, v1);
  569. }
  570. #[test]
  571. fn cross() {
  572. let v1 = Vector3d::from((1.0, 0.0, 0.0));
  573. let v2 = Vector3d::from((0.0, 1.0, 0.0));
  574. let ex = Vector3d::from((0.0, 0.0, 1.0));
  575. assert_eq!(ex, v1.cross(&v2));
  576. let v1 = Vector4d::from((1.0, 0.0, 0.0, 0.5));
  577. let v2 = Vector4d::from((0.0, 3.0, 0.0, 1.0));
  578. let ex = Vector4d::from((0.0, 0.0, 6.0, 1.0));
  579. assert_eq!(ex, v1.cross(&v2));
  580. }
  581. }