types.rs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. use std::{
  2. fmt::{Debug, Display},
  3. ops::{Add, AddAssign, Div, Mul, Sub, SubAssign},
  4. };
  5. use binrw::{binrw, BinRead, BinWrite};
  6. use diff::{Diff, VecDiff};
  7. use float_cmp::approx_eq;
  8. use num_enum::{IntoPrimitive, TryFromPrimitive};
  9. use rand::{thread_rng, Rng};
  10. use serde::{Deserialize, Serialize};
  11. use serde_repr::{Deserialize_repr, Serialize_repr};
  12. use strum::EnumIter;
  13. use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf, FP};
  14. /// Generic field with structure of length + data
  15. pub type Field = ArrayOfPrimitive<u8>;
  16. pub type U16 = FieldOf<u16>;
  17. pub type I16 = FieldOf<i16>;
  18. pub type U32 = FieldOf<u32>;
  19. pub type I32 = FieldOf<i32>;
  20. pub type F64 = FieldOf<f64>;
  21. pub type Bool = FieldOf<u32>;
  22. impl From<FieldOf<u32>> for bool {
  23. fn from(value: FieldOf<u32>) -> Self {
  24. value.value != 0
  25. }
  26. }
  27. impl From<bool> for FieldOf<u32> {
  28. fn from(value: bool) -> Self {
  29. Self {
  30. value: match value {
  31. true => 1,
  32. false => 0,
  33. },
  34. }
  35. }
  36. }
  37. #[binrw]
  38. #[derive(Clone, Default, PartialEq)]
  39. pub struct WString {
  40. // Temporary variable that holds number of elements
  41. #[br(temp)]
  42. #[bw(try_calc(u32::try_from(value.len() * core::mem::size_of::<u16>())))]
  43. size: u32,
  44. #[br(count = size / core::mem::size_of::<u16>() as u32)]
  45. pub value: Vec<u16>,
  46. }
  47. impl From<&WString> for String {
  48. fn from(value: &WString) -> Self {
  49. String::from_utf16_lossy(&value.value)
  50. }
  51. }
  52. impl From<String> for WString {
  53. fn from(value: String) -> Self {
  54. let value: Vec<u16> = value.encode_utf16().collect();
  55. Self { value }
  56. }
  57. }
  58. // Custom Diff implementation to only diff internal value
  59. impl Diff for WString {
  60. type Repr = VecDiff<u16>;
  61. fn diff(&self, other: &Self) -> Self::Repr {
  62. self.value.diff(&other.value)
  63. }
  64. fn apply(&mut self, diff: &Self::Repr) {
  65. self.value.apply(diff)
  66. }
  67. fn identity() -> Self {
  68. Self { value: vec![] }
  69. }
  70. }
  71. impl Debug for WString {
  72. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  73. write!(f, "\"{}\"", String::from(self))
  74. }
  75. }
  76. impl Display for WString {
  77. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  78. write!(f, "{}", String::from(self))
  79. }
  80. }
  81. #[derive(Clone, Debug, Default, Diff, PartialEq, BinRead, BinWrite)]
  82. #[diff(attr(
  83. #[derive(Debug, PartialEq)]
  84. ))]
  85. pub struct Rgba {
  86. pub red: u8,
  87. pub green: u8,
  88. pub blue: u8,
  89. pub alpha: u8,
  90. }
  91. impl From<(u8, u8, u8)> for Rgba {
  92. fn from(value: (u8, u8, u8)) -> Self {
  93. Self {
  94. red: value.0,
  95. green: value.1,
  96. blue: value.2,
  97. alpha: 0,
  98. }
  99. }
  100. }
  101. impl Rgba {
  102. pub fn random() -> Self {
  103. Self {
  104. red: thread_rng().gen(),
  105. green: thread_rng().gen(),
  106. blue: thread_rng().gen(),
  107. alpha: 0,
  108. }
  109. }
  110. }
  111. #[derive(Copy, Clone, Debug, Default, Diff, BinRead, BinWrite, Serialize, Deserialize)]
  112. #[diff(attr(
  113. #[derive(Debug, PartialEq)]
  114. ))]
  115. #[serde(rename_all = "PascalCase")]
  116. pub struct Point {
  117. pub x: f64,
  118. pub y: f64,
  119. }
  120. impl PartialEq for Point {
  121. fn eq(&self, other: &Self) -> bool {
  122. approx_eq!(f64, self.x, other.x) && approx_eq!(f64, self.y, other.y)
  123. }
  124. }
  125. impl Display for Point {
  126. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  127. write!(f, "({:.FP$}, {:.FP$})", self.x, self.y)
  128. }
  129. }
  130. impl Sub for Point {
  131. type Output = Point;
  132. fn sub(self, rhs: Self) -> Self::Output {
  133. Point {
  134. x: self.x - rhs.x,
  135. y: self.y - rhs.y,
  136. }
  137. }
  138. }
  139. impl SubAssign for Point {
  140. fn sub_assign(&mut self, rhs: Self) {
  141. self.x -= rhs.x;
  142. self.y -= rhs.y;
  143. }
  144. }
  145. impl Add for Point {
  146. type Output = Point;
  147. fn add(self, rhs: Self) -> Self::Output {
  148. Point {
  149. x: self.x + rhs.x,
  150. y: self.y + rhs.y,
  151. }
  152. }
  153. }
  154. impl AddAssign for Point {
  155. fn add_assign(&mut self, rhs: Self) {
  156. self.x += rhs.x;
  157. self.y += rhs.y;
  158. }
  159. }
  160. impl Mul for Point {
  161. type Output = Point;
  162. fn mul(self, rhs: Self) -> Self::Output {
  163. Point {
  164. x: self.x * rhs.x,
  165. y: self.y * rhs.y,
  166. }
  167. }
  168. }
  169. impl Div for Point {
  170. type Output = Point;
  171. fn div(self, rhs: Self) -> Self::Output {
  172. Point {
  173. x: self.x / rhs.x,
  174. y: self.y / rhs.y,
  175. }
  176. }
  177. }
  178. impl From<(f64, f64)> for Point {
  179. fn from(value: (f64, f64)) -> Self {
  180. Self {
  181. x: value.0,
  182. y: value.1,
  183. }
  184. }
  185. }
  186. impl From<f64> for Point {
  187. fn from(value: f64) -> Self {
  188. Self { x: value, y: value }
  189. }
  190. }
  191. impl Point {
  192. pub fn min(&self, rhs: Point) -> Point {
  193. Point {
  194. x: self.x.min(rhs.x),
  195. y: self.y.min(rhs.y),
  196. }
  197. }
  198. pub fn max(&self, rhs: Point) -> Point {
  199. Point {
  200. x: self.x.max(rhs.x),
  201. y: self.y.max(rhs.y),
  202. }
  203. }
  204. }
  205. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  206. #[diff(attr(
  207. #[derive(Debug, PartialEq)]
  208. ))]
  209. #[brw(repr(u32))]
  210. #[repr(u32)]
  211. pub enum WobbleType {
  212. Spiral = 0,
  213. Sinusoidal = 1,
  214. Ellipse = 2,
  215. Vert8 = 3,
  216. Hori8 = 4,
  217. Unknown = 5,
  218. }
  219. impl Default for WobbleType {
  220. fn default() -> Self {
  221. WobbleType::Unknown
  222. }
  223. }
  224. #[derive(Clone, Debug, Diff, PartialEq, BinRead, BinWrite, strum::Display)]
  225. #[diff(attr(
  226. #[derive(Debug, PartialEq)]
  227. ))]
  228. #[brw(repr(u16))]
  229. #[repr(u16)]
  230. pub enum ObjectType {
  231. Unknown = 0,
  232. Curve = 1,
  233. Point = 2,
  234. Rectangle = 3,
  235. Circle = 4,
  236. Ellipse = 5,
  237. Polygon = 6,
  238. HatchLine = 16,
  239. Hatch = 32,
  240. }
  241. impl Default for ObjectType {
  242. fn default() -> Self {
  243. ObjectType::Unknown
  244. }
  245. }
  246. #[derive(
  247. Copy,
  248. Clone,
  249. Debug,
  250. Diff,
  251. PartialEq,
  252. strum::Display,
  253. Serialize_repr,
  254. Deserialize_repr,
  255. EnumIter,
  256. IntoPrimitive,
  257. TryFromPrimitive,
  258. )]
  259. #[diff(attr(
  260. #[derive(Debug, PartialEq)]
  261. ))]
  262. #[repr(u32)]
  263. pub enum PulseWidth {
  264. Ns2 = 2,
  265. Ns4 = 4,
  266. Ns6 = 6,
  267. Ns8 = 8,
  268. Ns12 = 12,
  269. Ns20 = 20,
  270. Ns30 = 30,
  271. Ns45 = 45,
  272. Ns60 = 60,
  273. Ns80 = 80,
  274. Ns100 = 100,
  275. Ns150 = 150,
  276. Ns200 = 200,
  277. Ns250 = 250,
  278. Ns350 = 350,
  279. Ns500 = 500,
  280. }
  281. impl PulseWidth {
  282. pub fn min_freq(&self) -> u32 {
  283. match self {
  284. PulseWidth::Ns2 => 850_000,
  285. PulseWidth::Ns4 => 500_000,
  286. PulseWidth::Ns6 => 320_000,
  287. PulseWidth::Ns8 => 250_000,
  288. PulseWidth::Ns12 => 170_000,
  289. PulseWidth::Ns20 => 115_000,
  290. PulseWidth::Ns30 => 90_000,
  291. PulseWidth::Ns45 => 75_000,
  292. PulseWidth::Ns60 => 65_000,
  293. PulseWidth::Ns80 => 60_000,
  294. PulseWidth::Ns100 => 45_000,
  295. PulseWidth::Ns150 => 30_000,
  296. PulseWidth::Ns200 => 25_000,
  297. PulseWidth::Ns250 => 25_000,
  298. PulseWidth::Ns350 => 25_000,
  299. PulseWidth::Ns500 => 25_000,
  300. }
  301. }
  302. pub fn max_freq(&self) -> u32 {
  303. match self {
  304. PulseWidth::Ns2 => 4_000_000,
  305. PulseWidth::Ns4 => 4_000_000,
  306. PulseWidth::Ns6 => 4_000_000,
  307. PulseWidth::Ns8 => 4_000_000,
  308. PulseWidth::Ns12 => 3_000_000,
  309. PulseWidth::Ns20 => 3_000_000,
  310. PulseWidth::Ns30 => 3_000_000,
  311. PulseWidth::Ns45 => 2_000_000,
  312. PulseWidth::Ns60 => 2_000_000,
  313. PulseWidth::Ns80 => 2_000_000,
  314. PulseWidth::Ns100 => 1_000_000,
  315. PulseWidth::Ns150 => 1_000_000,
  316. PulseWidth::Ns200 => 1_000_000,
  317. PulseWidth::Ns250 => 900_000,
  318. PulseWidth::Ns350 => 600_000,
  319. PulseWidth::Ns500 => 500_000,
  320. }
  321. }
  322. /// Ratio of total output power per pulse from the area under the curve of the pulse
  323. /// waveform over time. Pulses below 20ns are assumed to be linear discrete pulses
  324. /// with no decay over time, while longer pulse widths will follow natural log profile
  325. pub fn power_ratio(&self) -> f64 {
  326. match self {
  327. PulseWidth::Ns2 => 1.0,
  328. PulseWidth::Ns4 => 3.17,
  329. PulseWidth::Ns6 => 5.34,
  330. PulseWidth::Ns8 => 7.51,
  331. PulseWidth::Ns12 => 11.85,
  332. PulseWidth::Ns20 => 17.29,
  333. PulseWidth::Ns30 => 22.04,
  334. PulseWidth::Ns45 => 26.78,
  335. PulseWidth::Ns60 => 30.17,
  336. PulseWidth::Ns80 => 33.54,
  337. PulseWidth::Ns100 => 36.16,
  338. PulseWidth::Ns150 => 40.91,
  339. PulseWidth::Ns200 => 44.28,
  340. PulseWidth::Ns250 => 46.90,
  341. PulseWidth::Ns350 => 50.84,
  342. PulseWidth::Ns500 => 55.03,
  343. }
  344. }
  345. }