Skip to content

Commit

Permalink
added a PieceSetDescriptor and removed the const generic of Piece
Browse files Browse the repository at this point in the history
  • Loading branch information
Odilf committed Aug 11, 2023
1 parent b283c60 commit 774b15a
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 101 deletions.
4 changes: 2 additions & 2 deletions barbarosa/src/cube_n/cube3/heuristics/manhattan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use nalgebra::Vector3;
use crate::{
cube3::Cube3,
cube_n::{Corner, Edge},
generic::{Piece, PieceSet},
generic::{piece::PieceSetDescriptor, PieceSet},
};

fn vec_manhattan(a: &Vector3<f32>, b: &Vector3<f32>) -> f32 {
a.iter().zip(b.iter()).map(|(a, b)| (a - b).abs()).sum()
}

fn piece_set_manhattan<P: Piece<N>, const N: usize>(
fn piece_set_manhattan<P: PieceSetDescriptor<N>, const N: usize>(
set: &PieceSet<P, N>,
coords: impl Fn(&P::Position) -> Vector3<f32>,
) -> f32 {
Expand Down
2 changes: 1 addition & 1 deletion barbarosa/src/cube_n/cube3/mus/index/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
mus::{CornersMUS, HalfEdgesMUS},
Corner, Edge,
},
generic::Piece,
generic::{piece::PieceSetDescriptor, Piece},
};

use super::{
Expand Down
2 changes: 1 addition & 1 deletion barbarosa/src/cube_n/invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::fmt::Debug;

use crate::{
cube_n::{cube3::mus::index::PositionIndexable, Cube3, Edge},
generic::Piece,
generic::piece::PieceSetDescriptor,
};

use super::pieces::{corner::CornerSet, edge::EdgeSet};
Expand Down
25 changes: 16 additions & 9 deletions barbarosa/src/cube_n/moves/wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use crate::generic::{
parse::{self, Parsable},
};

use self::generic::{Alg, Cube, Movable, Piece, PieceSet};
use self::generic::{piece::PieceSetDescriptor, Alg, Cube, Movable, Piece, PieceSet};

use super::{
rotation::{AxisRotation, Rotatable},
Expand Down Expand Up @@ -183,13 +183,13 @@ impl Alg<AxisMove> {
}

/// A piece that can be moved by a wide move
pub trait DepthPiece<const N: usize>: Piece<N> {
pub trait DepthPiece: Piece {
/// Whether the piece is in the wide move if it has the given normal and tangent depth.
fn is_in_wide_move<const M: u32>(
fn is_in_wide_move<const N: u32>(
&self,
normal_depth: u32,
tangent_depth: u32,
m: &WideAxisMove<M>,
m: &WideAxisMove<N>,
) -> bool;
}

Expand All @@ -199,7 +199,7 @@ pub trait DepthPiece<const N: usize>: Piece<N> {
/// implements [`Movable`] for [`WideAxisMove`]s.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DepthPieceSet<
P: DepthPiece<N>,
P: DepthPiece + PieceSetDescriptor<N>,
const N: usize,
const NORMAL_DEPTH: u32,
const TANGENT_DEPTH: u32 = 0,
Expand All @@ -208,15 +208,22 @@ pub struct DepthPieceSet<
pub set: PieceSet<P, N>,
}

impl<P: DepthPiece<N>, const N: usize, const ND: u32, const TD: u32> DepthPieceSet<P, N, ND, TD> {
impl<P: DepthPiece + PieceSetDescriptor<N>, const N: usize, const ND: u32, const TD: u32>
DepthPieceSet<P, N, ND, TD>
{
/// Alias to [`Piece::SOLVED`]
pub const SOLVED: Self = Self {
set: PieceSet::SOLVED,
};
}

impl<P: DepthPiece<N> + Rotatable, const M: u32, const N: usize, const ND: u32, const TD: u32>
Movable<WideAxisMove<M>> for DepthPieceSet<P, N, ND, TD>
impl<
P: DepthPiece + Rotatable + PieceSetDescriptor<N>,
const M: u32,
const N: usize,
const ND: u32,
const TD: u32,
> Movable<WideAxisMove<M>> for DepthPieceSet<P, N, ND, TD>
{
fn apply(&mut self, m: &WideAxisMove<M>) {
self.set
Expand All @@ -226,7 +233,7 @@ impl<P: DepthPiece<N> + Rotatable, const M: u32, const N: usize, const ND: u32,
}
}

impl<P: DepthPiece<N>, const N: usize, const ND: u32, const TD: u32> Deref
impl<P: DepthPiece + PieceSetDescriptor<N>, const N: usize, const ND: u32, const TD: u32> Deref
for DepthPieceSet<P, N, ND, TD>
{
type Target = PieceSet<P, N>;
Expand Down
24 changes: 13 additions & 11 deletions barbarosa/src/cube_n/pieces/center/corner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
space::{Axis, Direction},
Vec3, WideAxisMove,
},
generic,
generic::{self, piece::PieceSetDescriptor},
};

/// A center corner piece of the cube. There are 4 of these in each face of a cube.
Expand All @@ -22,9 +22,19 @@ pub struct CenterCorner {
axis: Axis,
}

impl generic::Piece<24> for CenterCorner {
impl generic::Piece for CenterCorner {
type Position = Self;

fn position(&self) -> Self::Position {
self.clone()
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.position[self.axis] == original_pos.position[original_pos.axis]
}
}

impl PieceSetDescriptor<24> for CenterCorner {
const SOLVED: [Self; 24] = {
use Direction::*;

Expand All @@ -47,14 +57,6 @@ impl generic::Piece<24> for CenterCorner {
};

const REFERENCE_POSITIONS: [Self::Position; 24] = Self::SOLVED;

fn position(&self) -> Self::Position {
self.clone()
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.position[self.axis] == original_pos.position[original_pos.axis]
}
}

impl Rotatable for CenterCorner {
Expand All @@ -64,7 +66,7 @@ impl Rotatable for CenterCorner {
}
}

impl DepthPiece<24> for CenterCorner {
impl DepthPiece for CenterCorner {
fn is_in_wide_move<const M: u32>(
&self,
normal_depth: u32,
Expand Down
24 changes: 13 additions & 11 deletions barbarosa/src/cube_n/pieces/center/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
space::{faces, Axis, Direction, Face},
WideAxisMove,
},
generic,
generic::{self, piece::PieceSetDescriptor},
};

/// A center edge piece of the cube. There are 4 of these in each face of a cube.
Expand All @@ -34,9 +34,19 @@ pub struct CenterEdge {
pub side_direction: Direction,
}

impl generic::Piece<24> for CenterEdge {
impl generic::Piece for CenterEdge {
type Position = Self;

fn position(&self) -> Self::Position {
self.clone()
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.main_face == original_pos.main_face
}
}

impl PieceSetDescriptor<24> for CenterEdge {
const REFERENCE_POSITIONS: [Self::Position; 24] = {
use faces::*;
use Direction::*;
Expand All @@ -50,14 +60,6 @@ impl generic::Piece<24> for CenterEdge {
};

const SOLVED: [Self; 24] = Self::REFERENCE_POSITIONS;

fn position(&self) -> Self::Position {
self.clone()
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.main_face == original_pos.main_face
}
}

impl Rotatable for CenterEdge {
Expand Down Expand Up @@ -120,7 +122,7 @@ impl CenterEdge {
}
}

impl DepthPiece<24> for CenterEdge {
impl DepthPiece for CenterEdge {
fn is_in_wide_move<const M: u32>(
&self,
normal_depth: u32,
Expand Down
24 changes: 13 additions & 11 deletions barbarosa/src/cube_n/pieces/center/wing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
space::{faces, Axis, Direction, Face},
WideAxisMove,
},
generic,
generic::{self, piece::PieceSetDescriptor},
};

use super::edge::CenterEdge;
Expand All @@ -28,9 +28,19 @@ pub struct CenterWing {
pseudo_oriented: bool,
}

impl generic::Piece<48> for CenterWing {
impl generic::Piece for CenterWing {
type Position = Self;

fn position(&self) -> Self::Position {
self.clone()
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.main_face() == original_pos.main_face()
}
}

impl PieceSetDescriptor<48> for CenterWing {
const REFERENCE_POSITIONS: [Self::Position; 48] = {
use faces::*;
use Direction::*;
Expand All @@ -45,14 +55,6 @@ impl generic::Piece<48> for CenterWing {
};

const SOLVED: [Self; 48] = Self::REFERENCE_POSITIONS;

fn position(&self) -> Self::Position {
self.clone()
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.main_face() == original_pos.main_face()
}
}

impl Rotatable for CenterWing {
Expand Down Expand Up @@ -139,7 +141,7 @@ impl std::fmt::Debug for CenterWing {
}
}

impl DepthPiece<48> for CenterWing {
impl DepthPiece for CenterWing {
fn is_in_wide_move<const M: u32>(
&self,
normal_depth: u32,
Expand Down
25 changes: 15 additions & 10 deletions barbarosa/src/cube_n/pieces/corner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::{
space::{Axis, Direction, Face},
AxisMove, Vec3,
},
generic::{self, moves::impl_movable_array, utils::map_array_const, PieceSet},
generic::{
self, moves::impl_movable_array, piece::PieceSetDescriptor, utils::map_array_const,
PieceSet,
},
};

/// A corner piece of the cube.
Expand All @@ -25,9 +28,19 @@ pub struct Corner {
pub orientation_axis: Axis,
}

impl generic::Piece<8> for Corner {
impl generic::Piece for Corner {
type Position = Vec3;

fn position(&self) -> Self::Position {
self.position
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.position() == *original_pos && self.is_oriented()
}
}

impl PieceSetDescriptor<8> for Corner {
const REFERENCE_POSITIONS: [Self::Position; 8] = {
use Direction::*;

Expand All @@ -38,14 +51,6 @@ impl generic::Piece<8> for Corner {
};

const SOLVED: [Self; 8] = map_array_const!(Corner::REFERENCE_POSITIONS, 8, Corner::oriented);

fn position(&self) -> Self::Position {
self.position
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.position() == *original_pos && self.is_oriented()
}
}

impl Rotatable for Corner {
Expand Down
29 changes: 17 additions & 12 deletions barbarosa/src/cube_n/pieces/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::{
space::{Axis, Direction, Face},
AxisMove, Vec2,
},
generic::{self, moves::impl_movable_array, utils::map_array_const, Piece, PieceSet},
generic::{
self, moves::impl_movable_array, piece::PieceSetDescriptor, utils::map_array_const, Piece,
PieceSet,
},
};

// use super::{ContainedInMove, Corner};
Expand Down Expand Up @@ -44,9 +47,19 @@ pub struct Edge {
pub oriented: bool,
}

impl generic::Piece<12> for Edge {
impl generic::Piece for Edge {
type Position = (Axis, Vec2);

fn position(&self) -> Self::Position {
(self.normal_axis, self.slice_position)
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.position() == *original_pos && self.oriented
}
}

impl PieceSetDescriptor<12> for Edge {
// TODO: Try to use cartesian product to make this nicer
const REFERENCE_POSITIONS: [(Axis, Vec2); 12] = {
use Axis::*;
Expand Down Expand Up @@ -77,20 +90,12 @@ impl generic::Piece<12> for Edge {
///
/// See [crate::cube_n::cube3::mus] for more information.
const SOLVED: [Edge; 12] = {
const fn from_tuple((axis, pos): (Axis, Vec2)) -> Edge {
const fn from_tuple((axis, pos): <Edge as Piece>::Position) -> Edge {
Edge::oriented(axis, pos)
}

map_array_const!(Edge::REFERENCE_POSITIONS, 12, from_tuple)
};

fn position(&self) -> Self::Position {
(self.normal_axis, self.slice_position)
}

fn is_solved(&self, original_pos: &Self::Position) -> bool {
self.position() == *original_pos && self.oriented
}
}

impl Rotatable for Edge {
Expand Down Expand Up @@ -191,7 +196,7 @@ impl Edge {

/// Returns the standard coordinates of the edge
pub fn coordinates(
(normal_axis, slice_position): &<Edge as Piece<12>>::Position,
(normal_axis, slice_position): &<Edge as Piece>::Position,
) -> nalgebra::Vector3<f32> {
normal_axis.map_on_slice(Vector3::zeros(), |_| {
slice_position.map(|dir| dir.scalar() as f32)
Expand Down
Loading

0 comments on commit 774b15a

Please sign in to comment.