From: Ian Jackson Date: Thu, 25 Mar 2021 12:00:03 +0000 (+0000) Subject: geometry: Massive overhaul of types and fields X-Git-Tag: otter-0.5.0~423 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=5c1156c2c24f7517b9cd068ed5bcf5aeb0af7937;p=otter.git geometry: Massive overhaul of types and fields Signed-off-by: Ian Jackson --- diff --git a/apitest/at-otter.rs b/apitest/at-otter.rs index ae5eae45..b30d2317 100644 --- a/apitest/at-otter.rs +++ b/apitest/at-otter.rs @@ -375,10 +375,9 @@ pub fn update_update_pieces( let (op, d) = v["op"].as_object().unwrap().iter().next().unwrap(); fn coord(j: &JsV) -> Pos { - PosC( + PosC::from_iter_2( j.as_array().unwrap().iter() .map(|n| n.as_i64().unwrap().try_into().unwrap()) - .collect::>().into_inner().unwrap() ) } @@ -411,7 +410,7 @@ impl PieceOp for PieceOpData { fn api(&self) -> Option { Some((self.0, self.1.clone())) } } impl PieceOp for Pos { - fn api(&self) -> Option { Some(("m", json![self.0])) } + fn api(&self) -> Option { Some(("m", json![self.coords])) } fn update(&self, pi: &mut PieceInfo) { pi.pos = *self } } impl PieceOp for () { @@ -501,7 +500,7 @@ impl Ctx { let llm: [_;2] = llm.into_inner().unwrap(); dbgc!(&llm); - for (llm, &pos) in izip!(&llm, [PosC([5,5]), PosC([50,25])].iter()) + for (llm, &pos) in izip!(&llm, [PosC::new(5,5), PosC::new(50,25)].iter()) { session.api_piece(GH::With, &llm.id, pos)?; } @@ -559,7 +558,7 @@ impl Ctx { .collect::>() .into_inner().unwrap(); - pawns.sort_by_key(|&p| -pieces[p].pos.0[0]); + pawns.sort_by_key(|&p| -pieces[p].pos.x()); dbgc!(pawns) } @@ -572,7 +571,7 @@ impl Ctx { // ----- alice: move pawns into alice's hand ----- for (&pawn, &xoffset) in izip!(&a_pawns, [10,20].iter()) { - let pos = (a_pieces[hand].pos + PosC([xoffset, 0]))?; + let pos = (a_pieces[hand].pos + PosC::new(xoffset, 0))?; alice.api_piece(GH::With, (&mut a_pieces, pawn), pos)?; } @@ -589,7 +588,7 @@ impl Ctx { { let p = a_pawns[0]; - let alice_move_to = (a_pieces[p].pos + PosC([5, 5]))?; + let alice_move_to = (a_pieces[p].pos + PosC::new(5,5))?; let mut a_p = (&mut a_pieces, p); alice.api_piece(GH::Grab, PuSynch(&mut a_p), ())?; @@ -619,7 +618,7 @@ impl Ctx { alice.api_piece(GH::With, (&mut a_pieces, a_pawns[0]), - PosC([ 15, 20 ]))?; + PosC::new( 15, 20 ))?; alice.synchu(&mut a_pieces)?; bob.synchu(&mut b_pieces)?; diff --git a/base/geometry.rs b/base/geometry.rs index 9a126888..b0b9318f 100644 --- a/base/geometry.rs +++ b/base/geometry.rs @@ -12,16 +12,16 @@ use num_traits::NumCast; pub type Coord = i32; -#[derive(Clone,Copy,Debug,Serialize,Deserialize,Hash)] +#[derive(Clone,Copy,Serialize,Deserialize,Hash)] #[derive(Eq,PartialEq,Ord,PartialOrd)] #[serde(transparent)] -pub struct PosC(pub [T; 2]); +pub struct PosC{ pub coords: [T; 2] } pub type Pos = PosC; -#[derive(Clone,Copy,Debug,Serialize,Deserialize,Hash)] -#[derive(Eq,PartialEq)] +#[derive(Clone,Copy,Serialize,Deserialize,Hash)] +#[derive(Eq,PartialEq,Ord,PartialOrd)] #[serde(transparent)] -pub struct RectC(pub [PosC; 2]); +pub struct RectC{ pub corners: [PosC; 2] } pub type Rect = RectC; // ---------- CheckedArith ---------- @@ -95,14 +95,42 @@ impl PosC { pub struct PosCFromIteratorError; display_as_debug!{PosCFromIteratorError} +impl PosC { + pub const fn new(x: T, y: T) -> Self { PosC{ coords: [x,y] } } + pub fn both(v: T) -> Self where T: Copy { PosC::new(v,v) } + pub fn zero() -> Self where T: num_traits::Zero + Copy { + PosC::both(::zero()) + } +} +impl PosC where T: Copy { + pub fn x(self) -> T { self.coords[0] } + pub fn y(self) -> T { self.coords[1] } +} + impl PosC { #[throws(PosCFromIteratorError)] - pub fn from_iter>(i: I) -> Self { PosC( + pub fn from_iter>(i: I) -> Self { PosC{ coords: i .collect::>() .into_inner() .map_err(|_| PosCFromIteratorError)? - )} + }} +} + +impl PosC where T: Debug { + pub fn from_iter_2>(i: I) -> Self { PosC{ coords: + i + .collect::>() + .into_inner() + .unwrap() + }} +} + +impl Debug for PosC where T: Debug + Copy { + #[throws(fmt::Error)] + fn fmt(&self, f: &mut Formatter) { + write!(f, "[{:?},{:?}]", self.x(), self.y())?; + } } impl PosC { @@ -111,11 +139,11 @@ impl PosC { pub fn try_from_iter_2< E: Debug, I: Iterator> - >(i: I) -> Self { PosC( + >(i: I) -> Self { PosC{ coords: i .collect::,E>>()? .into_inner().unwrap() - )} + }} } impl Add> for PosC { @@ -124,8 +152,8 @@ impl Add> for PosC { fn add(self, rhs: PosC) -> PosC { PosC::try_from_iter_2( itertools::zip_eq( - self.0.iter().cloned(), - rhs .0.iter().cloned(), + self.coords.iter().cloned(), + rhs .coords.iter().cloned(), ).map( |(a,b)| a.checked_add(b) ) @@ -139,8 +167,8 @@ impl Sub> for PosC { fn sub(self, rhs: PosC) -> PosC { PosC::try_from_iter_2( itertools::zip_eq( - self.0.iter().cloned(), - rhs .0.iter().cloned(), + self.coords.iter().cloned(), + rhs .coords.iter().cloned(), ).map(|(a,b)| a.checked_sub(b)) )? } @@ -151,7 +179,7 @@ impl> Mul for PosC { #[throws(CoordinateOverflow)] fn mul(self, rhs: S) -> PosC { PosC::try_from_iter_2( - self.0.iter().cloned().map( + self.coords.iter().cloned().map( |a| a.checked_mul(rhs) ) )? @@ -163,7 +191,7 @@ impl Neg for PosC { #[throws(CoordinateOverflow)] fn neg(self) -> Self { PosC::try_from_iter_2( - self.0.iter().cloned().map(|a| a.checked_neg()) + self.coords.iter().cloned().map(|a| a.checked_neg()) )? } } @@ -171,7 +199,7 @@ impl Neg for PosC { impl PosC { pub fn map U>(self, f: F) -> PosC { PosC::from_iter( - self.0.iter().cloned().map(f) + self.coords.iter().cloned().map(f) ).unwrap() } } @@ -181,51 +209,61 @@ impl PosC { (self, f: F) -> Result,E> { PosC::try_from_iter_2( - self.0.iter().cloned().map(f) + self.coords.iter().cloned().map(f) ) } } impl Mean for PosC where T: Mean + Debug { fn mean(&self, other: &Self) -> Self where T: Mean { - PosC::try_from_iter_2( - izip!(&self.0, &other.0) - .map(|(a,b)| Ok::<_,Void>(a.mean(b))) - ).unwrap_or_else(|v| match v { }) + PosC::from_iter_2( + izip!(&self.coords, &other.coords) + .map(|(a,b)| a.mean(b)) + ) } } // ---------- Rect ---------- +impl RectC where T: Copy { + pub fn tl(&self) -> PosC { self.corners[0] } + pub fn br(&self) -> PosC { self.corners[1] } +} + +impl Debug for RectC where T: Debug + Copy { + #[throws(fmt::Error)] + fn fmt(&self, f: &mut Formatter) { + write!(f, "Rect[{:?},{:?}]", self.tl(), self.br())?; + } +} + impl RectC { - pub fn contains(&self, p: PosC) -> bool where T: PartialOrd { + pub fn contains(&self, p: PosC) -> bool where T: PartialOrd + Copy { (0..2).all(|i| { - p.0[i] >= self.0[0].0[i] && - p.0[i] <= self.0[1].0[i] + p.coords[i] >= self.tl().coords[i] && + p.coords[i] <= self.br().coords[i] }) } - pub fn overlaps(&self, other: &RectC) -> bool where T: PartialOrd { + pub fn overlaps(&self, other: &RectC) -> bool where T: PartialOrd + Copy { ! (0..2).any(|i| ( - other.0[1].0[i] < self .0[0].0[i] || - self .0[1].0[i] < other.0[0].0[i] + other.br().coords[i] < self .tl().coords[i] || + self .br().coords[i] < other.tl().coords[i] )) } - pub fn empty() -> Self where T: Copy + num_traits::Zero + num_traits::One { - let zero = ::zero(); - let one = ::one(); - RectC([ - PosC([ one, one ]), - PosC([ zero, zero ]), - ]) + pub fn empty() -> Self where T: num_traits::Zero + num_traits::One + Copy { + RectC{ corners: [ + PosC::both( ::one() ), + PosC::both( ::zero() ), + ]} } } -impl RectC where T: Mean + Debug { +impl RectC where T: Mean + Debug + Copy { pub fn middle(&self) -> PosC { - Mean::mean(&self.0[0], - &self.0[1]) + Mean::mean(&self.tl(), + &self.br()) } } @@ -233,19 +271,20 @@ impl RectC where T: Mean + Debug { fn empty_area() { let empty = Rect::empty(); for x in -3..3 { for y in -3..3 { - assert!(! empty.contains(PosC([x,y]))); + dbg!(empty,x,y); + assert!(! empty.contains(PosC::new(x,y))); } } } // ---------- Region ---------- #[derive(Clone,Debug,Serialize,Deserialize)] -pub enum RegionC { +pub enum RegionC { Rectangle(RectC), } pub type Region = RegionC; -impl RegionC { +impl RegionC { pub fn contains(&self, pos: PosC) -> bool where T: PartialOrd { use RegionC::*; match &self { diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 4d1cc87b..a41d1eed 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -28,8 +28,8 @@ const USERLIST: &str = "/etc/userlist"; const CREATE_PIECES_MAX: u32 = 300; const OVERALL_PIECES_MAX: usize = 100_000; // don't make not fit in i32 -const DEFAULT_POS_START: Pos = PosC([20,20]); -const DEFAULT_POS_DELTA: Pos = PosC([5,5]); +const DEFAULT_POS_START: Pos = PosC::new(20,20); +const DEFAULT_POS_DELTA: Pos = PosC::new(5,5); pub struct CommandListener { listener: UnixListener, @@ -322,7 +322,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( (U{ pcs: vec![], log: vec![ LogEntry { html: Html(format!("{} resized the table to {}x{}", - &who.0, size.0[0], size.0[1])), + &who.0, size.x(), size.y())), }], raw: Some(vec![ PreparedUpdateEntry::SetTableSize(size) ]) }, Fine, None, ig) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 708b90d9..a6da6273 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -1121,10 +1121,10 @@ mod library_add { } Good([a, b]) => { // todo: take account of the space used by the markers themselves - let lhs = min(a.0[0], b.0[0]); - let rhs = max(a.0[0], b.0[0]); - let top = min(a.0[1], b.0[1]); - let bot = max(a.0[1], b.0[1]); + let lhs = min(a.x(), b.x()); + let rhs = max(a.x(), b.x()); + let top = min(a.y(), b.y()); + let bot = max(a.y(), b.y()); Placement { lhs, rhs, top, bot, clhs: lhs, cbot: top, @@ -1136,10 +1136,10 @@ mod library_add { impl Placement { /// If returns None, has already maybe tried to take some space #[throws(AE)] - fn place(&mut self, bbox: &[Pos;2], + fn place(&mut self, bbox: &Rect, pieces: &Vec, ma: &MainOpts) -> Option { - let PosC([w,h]) = (bbox[1] - bbox[0])?; + let PosC{ coords: [w,h] } = (bbox.br() - bbox.tl())?; let mut did_newline = false; let (ncbot, tlhs) = 'search: loop { @@ -1155,12 +1155,12 @@ mod library_add { if let Some((nclhs, clash_bot)) = pieces.iter() .filter_map(|p| (|| if_chain! { if let Some(pv) = p.visible.as_ref(); - let tl = (pv.pos + pv.bbox[0])?; - let br = (pv.pos + pv.bbox[1])?; - if !(tl.0[0] >= self.clhs - || tl.0[1] >= ncbot - || br.0[0] <= tlhs - || br.0[1] <= self.top); + let tl = (pv.pos + pv.bbox.tl())?; + let br = (pv.pos + pv.bbox.br())?; + if !(tl.x() >= self.clhs + || tl.y() >= ncbot + || br.x() <= tlhs + || br.y() <= self.top); then { if ma.verbose > 2 { eprintln!( @@ -1168,7 +1168,7 @@ mod library_add { &self, tlhs, ncbot, &p.itemname, &tl, &br ) } - Ok::<_,AE>(Some((br.0[0], br.0[1]))) + Ok::<_,AE>(Some((br.x(), br.y()))) } else { Ok::<_,AE>(None) } @@ -1197,8 +1197,8 @@ mod library_add { // if we are simply too wide, we'll just loop until off the bottom }; self.cbot = ncbot; - let ttopleft = PosC([tlhs, self.top]); - let tnominal = (ttopleft - bbox[0])?; + let ttopleft = PosC::new(tlhs, self.top); + let tnominal = (ttopleft - bbox.tl())?; if ma.verbose > 3 { dbgc!(&self, &tnominal); } Some(tnominal) diff --git a/src/bin/otterlib.rs b/src/bin/otterlib.rs index b99c0874..5617bc70 100644 --- a/src/bin/otterlib.rs +++ b/src/bin/otterlib.rs @@ -101,9 +101,9 @@ fn preview(items: Vec) { let bbox = p .bbox_approx()?; - let mut bbox = bbox - .iter() - .map(|PosC(xy)| xy.iter().map(|&p| p as f64).collect::>()) + let mut bbox = bbox.corners.iter() + .map(|PosC{coords}| coords.iter().map(|&p| p as f64) + .collect::>()) .collect::>(); for xy in &mut bbox[0] { *xy -= BORDER } for xy in &mut bbox[1] { *xy += BORDER } diff --git a/src/clock.rs b/src/clock.rs index 6be53166..174f9133 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -388,7 +388,7 @@ impl ThreadState { const W: Coord = 40; const H: Coord = 14; -const OUTLINE: Rectangle = Rectangle { xy: PosC([W as f64, H as f64]) }; +const OUTLINE: Rectangle = Rectangle { xy: PosC::new(W as f64, H as f64) }; // ==================== piece management, loading, etc. ==================== @@ -426,7 +426,7 @@ impl OutlineTrait for Clock { to OUTLINE { fn outline_path(&self, scale: f64) -> Result; fn thresh_dragraise(&self) -> Result, IE>; - fn bbox_approx(&self) -> Result<[Pos;2], IE>; + fn bbox_approx(&self) -> Result; } } } diff --git a/src/commands.rs b/src/commands.rs index 2ba0c286..158d6e9c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -157,7 +157,7 @@ pub struct MgmtGamePieceVisibleInfo { pub pos: Pos, pub face: FaceId, pub desc_html: Html, - pub bbox: [Pos;2], + pub bbox: Rect, } #[derive(Debug,Copy,Clone,Serialize,Deserialize)] diff --git a/src/deck.rs b/src/deck.rs index 7f1fc945..5d6e3294 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -29,7 +29,7 @@ impl OutlineTrait for Deck { to self.shape { fn outline_path(&self, scale: f64) -> Result; fn thresh_dragraise(&self) -> Result,IE>; - fn bbox_approx(&self) -> Result<[Pos;2], IE>; + fn bbox_approx(&self) -> Result; } } } diff --git a/src/gamestate.rs b/src/gamestate.rs index 7b086ba9..efaef512 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -27,7 +27,7 @@ pub struct Html(pub String); #[serde(transparent)] pub struct Timestamp(pub u64); /* time_t */ -pub const DEFAULT_TABLE_SIZE: Pos = PosC([ 400, 200 ]); +pub const DEFAULT_TABLE_SIZE: Pos = PosC::new( 400, 200 ); // ---------- general data types ---------- @@ -105,7 +105,7 @@ pub trait OutlineTrait: Debug + Sync + Send + 'static { self.outline_path(SELECT_SCALE) } fn thresh_dragraise(&self) -> Result, IE>; - fn bbox_approx(&self) -> Result<[Pos;2], IE>; + fn bbox_approx(&self) -> Result; } #[derive(Debug,Copy,Clone,Serialize,Deserialize)] @@ -271,13 +271,13 @@ impl ClampTable for Pos { fn clamped(self, range: Pos) -> Result { let mut output = ArrayVec::new(); let mut ok = true; - for (&pos, &rng) in izip!(self.0.iter(), range.0.iter()) { + for (&pos, &rng) in izip!(self.coords.iter(), range.coords.iter()) { output.push(match pos.clamped(rng) { Ok(pos) => pos, Err(pos) => { ok = false; pos }, }) } - let output = PosC(output.into_inner().unwrap()); + let output = PosC{ coords: output.into_inner().unwrap() }; if ok { Ok(output) } else { Err(output) } } } @@ -332,7 +332,7 @@ impl GPiece { pub fn dummy() -> Self { let gen_dummy = Generation(1); GPiece { - pos: PosC([0,0]), + pos: PosC::zero(), face: default(), held: None, zlevel: ZLevel { z: default(), zg: gen_dummy }, @@ -413,7 +413,7 @@ fn xdata_get_mut_inner< impl GameState { pub fn dummy() -> Self { GameState { table_colour: Html::lit("green"), - table_size: PosC([300,200]), + table_size: PosC::new(300,200), pieces: default(), gen: Generation(0), log: default(), diff --git a/src/hand.rs b/src/hand.rs index 934ba895..ea74fe87 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -42,7 +42,7 @@ impl OutlineTrait for Hand { to self.shape { fn outline_path(&self, scale: f64) -> Result; fn thresh_dragraise(&self) -> Result,IE>; - fn bbox_approx(&self) -> Result<[Pos;2], IE>; + fn bbox_approx(&self) -> Result; } } } diff --git a/src/hidden.rs b/src/hidden.rs index 09355461..aef082e1 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -287,34 +287,34 @@ impl OccDisplacement { OD::Stack{pos} => *pos, OD::Rect{rect} => (|| Some({ let notch: Coord = notch.try_into().ok()?; - let mut spare = ((rect.0[1] - rect.0[0]).ok()? + let mut spare = ((rect.br() - rect.tl()).ok()? - ppiece_use_size).ok()?; - for s in &mut spare.0 { *s = max(*s,1) } + for s in &mut spare.coords { *s = max(*s,1) } let fi = 0; let gi = 1; - let f_stride = max(ppiece_use_size.0[fi] / 4, 1); - let g_stride = max(ppiece_use_size.0[gi] / 3, 1); - let f_count = max(spare.0[fi] / f_stride, 1); - let g_count = max(spare.0[gi] / g_stride, 1); + let f_stride = max(ppiece_use_size.coords[fi] / 4, 1); + let g_stride = max(ppiece_use_size.coords[gi] / 3, 1); + let f_count = max(spare.coords[fi] / f_stride, 1); + let g_count = max(spare.coords[gi] / g_stride, 1); let mut f_num = notch % f_count; let g_num = notch / f_count; if g_num % 2 != 0 { f_num = f_count - 1 - f_num } - let f_coord = rect.0[1].0[fi] - ppiece_use_size.0[fi] / 2 - + let f_coord = rect.br().coords[fi] - ppiece_use_size.coords[fi] / 2 - f_stride * f_num; - let g_coord = rect.0[0].0[gi] + ppiece_use_size.0[gi] / 2 + + let g_coord = rect.tl().coords[gi] + ppiece_use_size.coords[gi] / 2 + if g_num < g_count { g_stride * g_num - } else if g_num < spare.0[gi] { + } else if g_num < spare.coords[gi] { g_num } else { - spare.0[gi] - 1 + spare.coords[gi] - 1 }; trace_dbg!("placement", spare, f_stride, f_count, f_num, f_coord, g_stride, g_count, g_num, g_coord); - let mut pos = PosC([0,0]); - pos.0[fi] = f_coord; - pos.0[gi] = g_coord; + let mut pos = PosC::zero(); + pos.coords[fi] = f_coord; + pos.coords[gi] = g_coord; pos }))().unwrap_or_else(||{ rect.middle() @@ -634,7 +634,7 @@ fn recalculate_occultation_general< let ilk = ilk.borrow(); if let Some(ilk) = ioccults.ilks.get(ilk); // expected, really if let Ok::<_,IE>(bbox) = ilk.p_occ.bbox_approx(); // expected, really - if let Ok(size) = bbox[1] - bbox[0]; // expected, really + if let Ok(size) = bbox.br() - bbox.tl(); // expected, really then { occ.ppiece_use_size = size; } }; let notch = occ.notches @@ -872,7 +872,7 @@ pub fn create_occultation( region, occulter, views, - ppiece_use_size: PosC([0,0]), + ppiece_use_size: PosC::zero(), notches: default(), }; debug!("creating occultation {:?}", &occultation); diff --git a/src/pieces.rs b/src/pieces.rs index febc131d..fed4679e 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -106,7 +106,7 @@ pub fn svg_circle_path(diam: f64) -> Html { } #[throws(SvgE)] -pub fn svg_rectangle_path(PosC([x,y]): PosC) -> Html { +pub fn svg_rectangle_path(PosC{coords: [x,y]}: PosC) -> Html { Html(format!("M {} {} h {} v {} h {} z", -x*0.5, -y*0.5, x, y, -x)) } @@ -120,7 +120,7 @@ impl OutlineTrait for GenericSimpleShape to self.outline { fn outline_path(&self, scale: f64) -> Result; fn thresh_dragraise(&self) -> Result,IE>; - fn bbox_approx(&self) -> Result<[Pos;2], IE>; + fn bbox_approx(&self) -> Result; } } } @@ -161,15 +161,15 @@ impl piece_specs::PieceLabel { else { "black" } }; let fontsz = 4.; - let PosC([x,y]) = { + let PosC{ coords: [x,y] } = { use piece_specs::PieceLabelPlace::*; let inout = match self.place { BottomLeft | TopLeft => 1., BottomLeftOutside | TopLeftOutside => -1., }; - let eff_size = (outline.xy - PosC([2., inout * 2.]))?; + let eff_size = (outline.xy - PosC::new(2., inout * 2.))?; let mut pos = (eff_size * -0.5)?; - let y = &mut pos.0[1]; + let y = &mut pos.coords[1]; *y += 0.5 * fontsz * inout; match self.place { BottomLeft | BottomLeftOutside => { *y *= -1. }, @@ -304,8 +304,8 @@ impl piece_specs::Square { #[throws(SpecError)] fn xy(&self) -> Pos { match *self.size.as_slice() { - [s,] => PosC([s,s]), - [x,y] => PosC([x,y]), + [s,] => PosC::new(s,s), + [x,y] => PosC::new(x,y), _ => throw!(SpecError::ImproperSizeSpec), } } diff --git a/src/shapelib.rs b/src/shapelib.rs index 40a41252..34242520 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -175,7 +175,7 @@ struct ItemOccultable { impl OutlineTrait for ItemOccultable { delegate! { to self.outline { fn outline_path(&self, scale: f64) -> Result; fn thresh_dragraise(&self) -> Result, IE>; - fn bbox_approx(&self) -> Result<[Pos; 2], IE>; + fn bbox_approx(&self) -> Result; }}} #[typetag::serde(name="Lib")] impl OccultedPieceTrait for ItemOccultable { @@ -191,7 +191,7 @@ impl OccultedPieceTrait for ItemOccultable { pub struct ItemEnquiryData { pub itemname: String, pub f0desc: Html, - pub f0bbox: [Pos; 2], + pub f0bbox: Rect, } impl ItemEnquiryData { @@ -204,7 +204,7 @@ impl ItemEnquiryData { impl OutlineTrait for Item { delegate! { to self.outline { fn outline_path(&self, scale: f64) -> Result; fn thresh_dragraise(&self) -> Result, IE>; - fn bbox_approx(&self) -> Result<[Pos; 2], IE>; + fn bbox_approx(&self) -> Result; }}} impl FaceTransform { @@ -748,9 +748,9 @@ impl OutlineTrait for Circle { Some((self.diam * 0.5) as Coord) } #[throws(IE)] - fn bbox_approx(&self) -> [Pos;2] { + fn bbox_approx(&self) -> Rect { let d = (self.diam * 0.5).ceil() as Coord; - [PosC([-d,-d]), PosC([d, d])] + Rect{ corners: [PosC::new(-d,-d), PosC::new(d, d)]} } } @@ -787,13 +787,13 @@ impl Rectangle { let offset = offset.try_map( |c| c.floor().to_i32().ok_or(CoordinateOverflow) )?; - let rect = RectC( + let rect = RectC{ corners: [-1,1].iter().map(|&signum| Ok::<_,CoordinateOverflow>({ (centre + (offset * signum)?)? })) .collect::,_>>()? .into_inner().unwrap() - ); + }; rect } @@ -812,17 +812,17 @@ impl OutlineTrait for Rectangle { } #[throws(IE)] fn thresh_dragraise(&self) -> Option { - let smallest: f64 = self.xy.0.iter().cloned() + let smallest: f64 = self.xy.coords.iter().cloned() .map(OrderedFloat::from).min().unwrap().into(); Some((smallest * 0.5) as Coord) } #[throws(IE)] - fn bbox_approx(&self) -> [Pos;2] { + fn bbox_approx(&self) -> Rect { let pos: Pos = self.xy.map( |v| ((v * 0.5).ceil()) as Coord ); let neg = (-pos)?; - [ neg, pos ] + Rect{ corners: [ neg, pos ] } } } @@ -841,14 +841,14 @@ impl OutlineDefn for SquareDefn { impl SquareDefn { #[throws(LibraryLoadError)] fn get(group: &GroupData) -> Rectangle { - Rectangle { xy: PosC( + Rectangle { xy: PosC{ coords: match group.d.size.as_slice() { &[s] => [s,s], s if s.len() == 2 => s.try_into().unwrap(), size => throw!(LLE::WrongNumberOfSizeDimensions { got: size.len(), expected: [1,2]}), } - )} + }} } } diff --git a/wasm/wasm.rs b/wasm/wasm.rs index d92a50a1..00f9c379 100644 --- a/wasm/wasm.rs +++ b/wasm/wasm.rs @@ -175,7 +175,7 @@ impl RegionList { self.0 .values() .any( - |r| r.contains(PosC([x,y])) + |r| r.contains(PosC::new(x,y)) ) } } diff --git a/wdriver/wdriver.rs b/wdriver/wdriver.rs index 088b9846..bec022fc 100644 --- a/wdriver/wdriver.rs +++ b/wdriver/wdriver.rs @@ -272,7 +272,7 @@ impl<'g> WindowGuard<'g> { })?; (||{ let vec: ndarray::Array1 = - posg.0.iter() + posg.coords.iter() .cloned() .map(|v| v as f64) .chain(iter::once(1.)) @@ -340,7 +340,7 @@ impl<'g> PieceElement<'g> { ); let x = a("x")?; let y = a("y")?; - Ok::<_,AE>(PosC([x,y])) + Ok::<_,AE>(PosC::new(x,y)) })() .with_context(|| self.pieceid.to_owned()) .context("read position of piece out of x,y attributes")? diff --git a/wdriver/wdt-hand.rs b/wdriver/wdt-hand.rs index 7d6ed305..1abb761f 100644 --- a/wdriver/wdt-hand.rs +++ b/wdriver/wdt-hand.rs @@ -87,7 +87,7 @@ impl Ctx { w.action_chain() .move_pos(&pawn)? .click_and_hold() - .move_w(&w, (hand_posg + PosC([20,0]))?)? + .move_w(&w, (hand_posg + PosC::new(20,0))?)? .release() .perform()?; w.synch()?; diff --git a/wdriver/wdt-simple.rs b/wdriver/wdt-simple.rs index 88b9e281..8e9a51d9 100644 --- a/wdriver/wdt-simple.rs +++ b/wdriver/wdt-simple.rs @@ -103,7 +103,7 @@ impl Ctx { let mut w = su.w(&self.alice)?; let p = w.find_piece(pc)?; let start = p.posg()?; - let end = |d| { let mut e = start; e.0[1] = table_size.0[1] + d; e }; + let end = |d| { let mut e = start; e.coords[1] = table_size.y() + d; e }; let try_end = end(10); let exp_end = end(0); w.action_chain() @@ -142,7 +142,7 @@ impl Ctx { { let mut w = su.w(&self.alice)?; w.action_chain() - .move_w(&w, PosC([10,10]))? + .move_w(&w, PosC::new(10,10))? .click() .release() .perform() @@ -183,7 +183,7 @@ impl Ctx { let mut w = su.w(window)?; let p = w.find_piece(pc)?; let start = p.posg()?; - let try_end = (start + PosC([dx, 0]))?; + let try_end = (start + PosC::new(dx, 0))?; let (sx,sy) = w.posg2posw(start)?; w.action_chain()