linalg.rs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_trait::async_trait;
  19. use darkfi_serial::{SerialDecodable, SerialEncodable};
  20. use std::ops::{Add, AddAssign, Div, DivAssign, Mul, Sub, SubAssign};
  21. #[derive(Clone, Copy, Debug, SerialEncodable, SerialDecodable)]
  22. pub struct Dimension {
  23. pub w: f32,
  24. pub h: f32,
  25. }
  26. impl Dimension {
  27. pub fn contains(&self, other: &Dimension) -> bool {
  28. other.w <= self.w && other.h <= self.h
  29. }
  30. }
  31. impl From<[f32; 2]> for Dimension {
  32. fn from(dim: [f32; 2]) -> Self {
  33. Self { w: dim[0], h: dim[1] }
  34. }
  35. }
  36. impl Mul<f32> for Dimension {
  37. type Output = Dimension;
  38. fn mul(self, scale: f32) -> Self::Output {
  39. Self { w: self.w * scale, h: self.h * scale }
  40. }
  41. }
  42. impl Div<f32> for Dimension {
  43. type Output = Dimension;
  44. fn div(self, scale: f32) -> Self::Output {
  45. Self { w: self.w / scale, h: self.h / scale }
  46. }
  47. }
  48. #[derive(Clone, Copy, Default, SerialEncodable, SerialDecodable)]
  49. pub struct Point {
  50. pub x: f32,
  51. pub y: f32,
  52. }
  53. impl Point {
  54. pub const fn new(x: f32, y: f32) -> Self {
  55. Self { x, y }
  56. }
  57. pub fn zero() -> Self {
  58. Self { x: 0., y: 0. }
  59. }
  60. pub fn unpack(&self) -> (f32, f32) {
  61. (self.x, self.y)
  62. }
  63. pub fn as_arr(&self) -> [f32; 2] {
  64. [self.x, self.y]
  65. }
  66. pub fn offset(&self, off_x: f32, off_y: f32) -> Self {
  67. Self { x: self.x + off_x, y: self.y + off_y }
  68. }
  69. pub fn to_rect(&self, w: f32, h: f32) -> Rectangle {
  70. Rectangle { x: self.x, y: self.y, w, h }
  71. }
  72. pub fn dist_sq(&self, other: Point) -> f32 {
  73. (self.x - other.x).powi(2) + (self.y - other.y).powi(2)
  74. }
  75. pub fn dist(&self, other: Point) -> f32 {
  76. self.dist_sq(other).sqrt()
  77. }
  78. pub fn normalize(&mut self) {
  79. let scale = self.dist(Point::zero());
  80. self.x /= scale;
  81. self.y /= scale;
  82. assert!((self.dist(Point::zero()) - 1.) < f32::EPSILON);
  83. }
  84. /// Counterclockwise perp vector (with -y up)
  85. pub fn perp_left(&self) -> Point {
  86. Point::new(self.y, -self.x)
  87. }
  88. /// Clockwise perp vector (with +y down)
  89. pub fn perp_right(&self) -> Point {
  90. Point::new(-self.y, self.x)
  91. }
  92. }
  93. impl From<[f32; 2]> for Point {
  94. fn from(pos: [f32; 2]) -> Self {
  95. Self { x: pos[0], y: pos[1] }
  96. }
  97. }
  98. impl std::fmt::Debug for Point {
  99. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  100. write!(f, "({}, {})", self.x, self.y)
  101. }
  102. }
  103. impl Add for Point {
  104. type Output = Self;
  105. fn add(self, other: Self) -> Self::Output {
  106. Self { x: self.x + other.x, y: self.y + other.y }
  107. }
  108. }
  109. impl Sub for Point {
  110. type Output = Self;
  111. fn sub(self, other: Self) -> Self::Output {
  112. Self { x: self.x - other.x, y: self.y - other.y }
  113. }
  114. }
  115. impl AddAssign for Point {
  116. fn add_assign(&mut self, other: Self) {
  117. *self = Self { x: self.x + other.x, y: self.y + other.y };
  118. }
  119. }
  120. impl SubAssign for Point {
  121. fn sub_assign(&mut self, other: Self) {
  122. *self = Self { x: self.x - other.x, y: self.y - other.y };
  123. }
  124. }
  125. impl Mul<f32> for Point {
  126. type Output = Self;
  127. fn mul(self, scale: f32) -> Self {
  128. Point::new(scale * self.x, scale * self.y)
  129. }
  130. }
  131. #[derive(Clone, Copy, SerialEncodable, SerialDecodable)]
  132. pub struct Rectangle {
  133. pub x: f32,
  134. pub y: f32,
  135. pub w: f32,
  136. pub h: f32,
  137. }
  138. impl Rectangle {
  139. pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self {
  140. Self { x, y, w, h }
  141. }
  142. pub fn zero() -> Self {
  143. Self { x: 0., y: 0., w: 0., h: 0. }
  144. }
  145. /// Use from() instead
  146. #[deprecated]
  147. pub fn from_array(arr: [f32; 4]) -> Self {
  148. Self { x: arr[0], y: arr[1], w: arr[2], h: arr[3] }
  149. }
  150. pub fn clip(&self, other: &Self) -> Option<Self> {
  151. if other.x + other.w < self.x ||
  152. other.x > self.x + self.w ||
  153. other.y + other.h < self.y ||
  154. other.y > self.y + self.h
  155. {
  156. return None
  157. }
  158. let mut clipped = other.clone();
  159. if clipped.x < self.x {
  160. clipped.x = self.x;
  161. clipped.w = other.x + other.w - clipped.x;
  162. }
  163. if clipped.y < self.y {
  164. clipped.y = self.y;
  165. clipped.h = other.y + other.h - clipped.y;
  166. }
  167. if clipped.x + clipped.w > self.x + self.w {
  168. clipped.w = self.x + self.w - clipped.x;
  169. }
  170. if clipped.y + clipped.h > self.y + self.h {
  171. clipped.h = self.y + self.h - clipped.y;
  172. }
  173. Some(clipped)
  174. }
  175. pub fn with_zero_pos(&self) -> Self {
  176. Self::new(0., 0., self.w, self.h)
  177. }
  178. pub fn clip_point(&self, mut point: Point) -> Point {
  179. if point.x < self.x {
  180. point.x = self.x;
  181. }
  182. if point.y < self.y {
  183. point.y = self.y;
  184. }
  185. if point.x > self.x + self.w {
  186. point.x = self.x + self.w;
  187. }
  188. if point.y > self.y + self.h {
  189. point.y = self.y + self.h;
  190. }
  191. point
  192. }
  193. pub fn contains(&self, point: Point) -> bool {
  194. self.x <= point.x &&
  195. point.x <= self.x + self.w &&
  196. self.y <= point.y &&
  197. point.y <= self.y + self.h
  198. }
  199. pub fn rhs(&self) -> f32 {
  200. self.x + self.w
  201. }
  202. pub fn bhs(&self) -> f32 {
  203. self.y + self.h
  204. }
  205. pub fn pos(&self) -> Point {
  206. Point { x: self.x, y: self.y }
  207. }
  208. pub fn corner(&self) -> Point {
  209. Point { x: self.x + self.w, y: self.y + self.h }
  210. }
  211. pub fn center(&self) -> Point {
  212. Point { x: self.x + self.w / 2., y: self.y + self.h / 2. }
  213. }
  214. pub fn top_right(&self) -> Point {
  215. Point { x: self.rhs(), y: self.y }
  216. }
  217. pub fn bot_left(&self) -> Point {
  218. Point { x: self.x, y: self.y + self.h }
  219. }
  220. pub fn dim(&self) -> Dimension {
  221. Dimension { w: self.w, h: self.h }
  222. }
  223. #[deprecated]
  224. pub fn top_left(&self) -> Point {
  225. Point { x: self.x, y: self.y }
  226. }
  227. #[deprecated]
  228. pub fn bottom_right(&self) -> Point {
  229. Point { x: self.x + self.w, y: self.y + self.h }
  230. }
  231. pub fn includes(&self, child: &Self) -> bool {
  232. self.contains(child.pos()) && self.contains(child.corner())
  233. }
  234. }
  235. impl From<[f32; 4]> for Rectangle {
  236. fn from(rect: [f32; 4]) -> Self {
  237. Self { x: rect[0], y: rect[1], w: rect[2], h: rect[3] }
  238. }
  239. }
  240. impl Add<Point> for Rectangle {
  241. type Output = Rectangle;
  242. fn add(self, other: Point) -> Self::Output {
  243. Self { x: self.x + other.x, y: self.y + other.y, w: self.w, h: self.h }
  244. }
  245. }
  246. impl Sub<Point> for Rectangle {
  247. type Output = Rectangle;
  248. fn sub(self, other: Point) -> Self::Output {
  249. Self { x: self.x - other.x, y: self.y - other.y, w: self.w, h: self.h }
  250. }
  251. }
  252. impl Mul<f32> for Rectangle {
  253. type Output = Rectangle;
  254. fn mul(self, scale: f32) -> Self::Output {
  255. Self { x: self.x * scale, y: self.y * scale, w: self.w * scale, h: self.h * scale }
  256. }
  257. }
  258. impl Div<f32> for Rectangle {
  259. type Output = Rectangle;
  260. fn div(self, scale: f32) -> Self::Output {
  261. Self { x: self.x / scale, y: self.y / scale, w: self.w / scale, h: self.h / scale }
  262. }
  263. }
  264. impl DivAssign<f32> for Rectangle {
  265. fn div_assign(&mut self, scale: f32) {
  266. self.x /= scale;
  267. self.y /= scale;
  268. self.w /= scale;
  269. self.h /= scale;
  270. }
  271. }
  272. impl std::fmt::Debug for Rectangle {
  273. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  274. write!(f, "({}, {}, {}, {})", self.x, self.y, self.w, self.h)
  275. }
  276. }
  277. impl From<parley::BoundingBox> for Rectangle {
  278. fn from(rect: parley::BoundingBox) -> Self {
  279. Self::new(rect.x0 as f32, rect.y0 as f32, rect.width() as f32, rect.height() as f32)
  280. }
  281. }