3 const SPACE_SIZE: usize = 11;
4 const NOF_DIM: usize = 3;
5 const DEFAULT_FACE_SIZE: usize = 64;
7 #[derive(Clone, Copy, PartialEq, Debug)]
14 #[derive(Clone, Copy, PartialEq, Debug)]
21 #[derive(Clone, Copy, PartialEq, Debug)]
27 #[derive(Clone, Copy, PartialEq, Debug)]
35 #[derive(Clone, Copy, PartialEq, Debug)]
41 pub type Coord = (usize, usize, usize);
42 pub type Edge = (Coord, Axis);
43 pub type Face = (Coord, Ori);
44 pub type FaceColor = (f32, f32, f32);
45 pub type Surface = [Option<FaceColor>; NOF_DIM];
48 pub size: [[usize; SPACE_SIZE]; NOF_DIM],
49 pub faces: [[[Option<Surface>; SPACE_SIZE]; SPACE_SIZE]; SPACE_SIZE],
54 //macro_rules! tuple_array {
55 // ($a:expr, $(($x:expr, $y:expr, $z:expr),)+) => [$((($x, $y, $z), $a),)+],
60 let mid = SPACE_SIZE / 2;
61 let current_node = (mid, mid, mid);
63 size: [[DEFAULT_FACE_SIZE; SPACE_SIZE]; NOF_DIM],
64 faces: [[[None; SPACE_SIZE]; SPACE_SIZE]; SPACE_SIZE],
65 current: Pos::E((current_node, Axis::X)),
68 tmp.add_empty_node(current_node);
69 tmp.add_face((current_node, Ori::Top));
73 pub fn axis_idx(a: Axis) -> usize {
81 pub fn ori_idx(o: Ori) -> usize {
89 fn adj_faces(((x, y, z), a): Edge) -> Vec<Face> {
90 let mut faces = Vec::new();
93 faces.push(((x, y, z), Ori::Top));
94 faces.push(((x, y, z), Ori::Back));
95 if y < (SPACE_SIZE - 1) {
96 faces.push(((x, y+1, z), Ori::Top));
99 faces.push(((x, y, z-1), Ori::Back));
103 faces.push(((x, y, z), Ori::Top));
104 faces.push(((x, y, z), Ori::Side));
106 faces.push(((x-1, y, z), Ori::Top));
109 faces.push(((x, y, z-1), Ori::Side));
113 faces.push(((x, y, z), Ori::Back));
114 faces.push(((x, y, z), Ori::Side));
116 faces.push(((x-1, y, z), Ori::Back));
118 if y < (SPACE_SIZE - 1) {
119 faces.push(((x, y+1, z), Ori::Side));
126 fn is_node(&self, (x, y, z): Coord) -> bool {
130 if let Some(_) = self.faces[x][y][z] {
140 fn is_edge(&self, (c, a): Edge) -> bool {
141 assert!(self.is_node(c));
142 self.is_adj_existing_faces((c, a))
145 fn is_face(&self, ((x, y, z), o): Face) -> bool {
146 self.is_node((x, y, z))
147 && match self.faces[x][y][z] {
148 Some(faces) => faces[Space::ori_idx(o)] != None,
153 pub fn current_node_coord(&self) -> Coord {
160 pub fn row_size_before_current(&self, a: Axis) -> usize {
161 let (xcur, ycur, zcur) = self.current_node_coord();
166 res += self.size[Space::axis_idx(Axis::X)][x];
171 res += self.size[Space::axis_idx(Axis::Y)][y];
176 res += self.size[Space::axis_idx(Axis::Z)][z];
183 pub fn row_size_after_current(&self, a: Axis) -> usize {
184 let (xcur, ycur, zcur) = self.current_node_coord();
188 let xidx = Space::axis_idx(Axis::X);
189 for x in xcur..self.size[xidx].len() {
190 res += self.size[xidx][x];
194 let yidx = Space::axis_idx(Axis::Y);
195 for y in ycur..self.size[yidx].len() {
196 res += self.size[yidx][y];
200 let zidx = Space::axis_idx(Axis::Z);
201 for z in zcur..self.size[zidx].len() {
202 res += self.size[zidx][z];
209 pub fn row_size(&self, a: Axis) -> usize {
210 self.size[Space::axis_idx(a)].iter()
211 .fold(0, |row, &s| row + s)
214 pub fn get_edge_size(&self, ((x, y, z), a): Edge) -> usize {
215 //assert!(self.is_edge(((x, y, z), a)));
216 self.size[Space::axis_idx(a)][match a {
223 pub fn get_current_face_color_if_any(&self) -> Option<FaceColor> {
226 Pos::F(((x, y, z), o)) => match self.faces[x][y][z] {
227 Some(ref f) => f[Space::ori_idx(o)],
233 pub fn set_current_face_color_if_any(&mut self, c: FaceColor) {
235 Pos::E(_) => panic!("current is not a face"),
236 Pos::F(((x, y, z), o)) => match self.faces[x][y][z] {
237 Some(ref mut f) => f[Space::ori_idx(o)] = Some(c),
238 None => panic!("no face here"),
243 fn edge_size_no_change(&mut self, ((x, y, z), a): Edge, new_size: usize) {
244 //assert!(self.is_edge(((x, y, z), a)));
245 let size = &mut self.size[Space::axis_idx(a)][match a {
250 if *size != new_size {
255 pub fn edge_size(&mut self, e: Edge, new_size: usize) {
256 self.edge_size_no_change(e, new_size);
257 self.change = Change::Size(e, new_size);
260 pub fn inc_size(&mut self) {
263 let size = self.get_edge_size(e);
264 if size < usize::MAX {
265 self.edge_size(e, size + 1);
268 Pos::F(_) => panic!("resize on a face"),
272 pub fn dec_size(&mut self) {
275 let size = self.get_edge_size(e);
277 self.edge_size(e, size - 1);
280 Pos::F(_) => panic!("resize on a face"),
284 fn face_edges(&self, ((x, y, z), o): Face) -> Vec<Edge> {
285 let mut edges = Vec::new();
288 edges.push(((x, y, z), Axis::X));
289 edges.push(((x, y, z), Axis::Y));
290 if x < SPACE_SIZE-1 {
291 edges.push(((x+1, y, z), Axis::Y));
294 edges.push(((x, y-1, z), Axis::X));
298 edges.push(((x, y, z), Axis::X));
299 edges.push(((x, y, z), Axis::Z));
300 if x < SPACE_SIZE-1 {
301 edges.push(((x+1, y, z), Axis::Z));
303 if z < SPACE_SIZE-1 {
304 edges.push(((x, y, z+1), Axis::X));
308 edges.push(((x, y, z), Axis::Y));
309 edges.push(((x, y, z), Axis::Z));
311 edges.push(((x, y-1, z), Axis::Z));
313 if z < SPACE_SIZE-1 {
314 edges.push(((x, y, z+1), Axis::Y));
321 fn add_empty_node(&mut self, (x, y, z): Coord) {
322 assert!(!self.is_node((x, y, z)));
323 self.faces[x][y][z] = Some([None; NOF_DIM]);
324 println!("empty node change");
325 self.change = Change::Space;
328 fn adj_available_faces(&self, e: Edge) -> Vec<Face> {
329 Space::adj_faces(e).iter()
330 .filter_map(|&adj_f| if self.is_face(adj_f) {
334 }).collect::<Vec<_>>()
337 fn adj_existing_faces(&self, e: Edge) -> Vec<Face> {
338 Space::adj_faces(e).iter()
339 .filter_map(|&adj_f| if self.is_face(adj_f) {
343 }).collect::<Vec<_>>()
346 fn is_adj_existing_faces(&self, e: Edge) -> bool {
347 for f in Space::adj_faces(e).iter() {
348 if self.is_face(*f) {
355 pub fn dec_pos(&mut self) {
356 let tmp = match self.current {
357 Pos::E(((mut x, mut y, mut z), a)) => {
365 Pos::F(((mut x, mut y, mut z), o)) => {
374 if self.is_node(tmp) {
375 println!("pos change");
376 self.change = Change::Current;
378 Pos::E((ref mut n, _)) => *n = tmp,
379 Pos::F((ref mut n, _)) => *n = tmp,
384 pub fn inc_pos(&mut self) {
385 let tmp = match self.current {
386 Pos::E(((mut x, mut y, mut z), a)) => {
394 Pos::F(((mut x, mut y, mut z), o)) => {
403 if self.is_node(tmp) {
404 println!("pos change");
405 self.change = Change::Current;
407 Pos::E((ref mut n, _)) => *n = tmp,
408 Pos::F((ref mut n, _)) => *n = tmp,
413 pub fn rotate(&mut self) {
414 println!("rotate change");
415 self.change = Change::Current;
417 Pos::E((_, ref mut a)) => match *a {
418 Axis::X => *a = Axis::Y,
419 Axis::Y => *a = Axis::Z,
420 Axis::Z => *a = Axis::X,
422 Pos::F((_, ref mut o)) => match *o {
423 Ori::Top => *o = Ori::Back,
424 Ori::Back => *o = Ori::Side,
425 Ori::Side => *o = Ori::Top,
430 pub fn set_pos_kind(&mut self, pk: PosKind) {
432 PosKind::Face => if let Pos::E((c, a)) = self.current {
433 self.change = Change::Current;
434 self.current = Pos::F((c, match a {
435 Axis::X => Ori::Side,
436 Axis::Y => Ori::Back,
440 PosKind::Edge => if let Pos::F((c, o)) = self.current {
441 self.change = Change::Current;
442 self.current = Pos::E((c, match o {
443 Ori::Side => Axis::X,
444 Ori::Back => Axis::Y,
451 pub fn switch_pos_type(&mut self) {
452 println!("switch change");
454 Pos::E(_) => self.set_pos_kind(PosKind::Face),
455 Pos::F(_) => self.set_pos_kind(PosKind::Edge),
459 fn all_nodes_coord(&self) -> Vec<Coord> {
460 let mut nodes = Vec::new();
461 for x in 0..self.faces.len() {
462 for y in 0..self.faces[x].len() {
463 for z in 0..self.faces[x][y].len() {
464 if self.is_node((x, y, z)) {
465 nodes.push((x, y, z));
473 fn connecting_edge(&self, f1: Face, f2: Face) -> Option<Edge> {
474 for e1 in self.face_edges(f1) {
475 for e2 in self.face_edges(f2) {
484 fn all_faces(&self) -> Vec<Face> {
485 let mut faces = Vec::new();
486 for n in self.all_nodes_coord() {
487 for &o in [Ori::Top, Ori::Side, Ori::Back].iter() {
488 if self.is_face((n, o)) {
496 fn are_same_face(((ax, ay, az), ao): Face,
497 ((bx, by, bz), bo): Face) -> bool {
505 fn faces_map(&self) -> Vec<(Face, Vec<Face>)> {
506 let mut map = Vec::new();
507 for f in self.all_faces() {
508 let mut adj_faces = Vec::new();
509 for &e in self.face_edges(f).iter() {
510 for &adj_f in self.adj_existing_faces(e).iter() {
512 adj_faces.push(adj_f);
516 map.push((f, adj_faces));
521 fn face_dot_label_fmt(((x, y, z), o): Face, s: &str) -> String {
522 format!("f{0}_{1}_{2}_{3} [label=\"{0} {1} {2} {3}\" {4}]",
523 x, y, z, Space::ori_idx(o), s)
526 fn face_dot_fmt(((x, y, z), o): Face) -> String {
527 format!("f{}_{}_{}_{}", x, y, z, Space::ori_idx(o))
530 pub fn dot_graph_code(&self) -> String {
531 let mut code = String::from("graph {\n");
532 let mut pairs = Vec::new();
533 for (f, adj_fs) in self.faces_map() {
534 code.push_str(&format!(" {};\n", Space::face_dot_label_fmt(f,
535 if Pos::F(f) == self.current { "color=red" } else { "" })));
536 for adj_f in adj_fs {
537 pairs.push((f, adj_f));
541 let pairs_clean = pairs.iter().filter(|&&(a1, b1)| {
543 !pairs.iter().take(i-1)
544 .any(|&(b2, a2)| a1 == a2 && b1 == b2)
545 }).collect::<Vec<_>>();
546 for &(a, b) in pairs_clean {
547 code.push_str(&format!(" {} -- {} {};\n",
548 Space::face_dot_fmt(a),
549 Space::face_dot_fmt(b),
550 if let Some(e) = self.connecting_edge(a, b) {
551 if Pos::E(e) == self.current {
557 println!("{}", code);
561 fn add_empty_node_if_none(&mut self, n: Coord) {
562 if !self.is_node(n) {
563 self.add_empty_node(n);
567 fn remove_node(&mut self, (x, y, z): Coord) {
568 assert!(self.is_node((x, y, z)));
569 panic!("nothing here");
572 fn remove_node_if_any(&mut self, n: Coord) {
578 fn set_face(&mut self, ((x, y, z), o): Face, v: Option<FaceColor>) {
579 match self.faces[x][y][z] {
581 if f[Space::ori_idx(o)] != v {
582 println!("face change");
583 self.change = Change::Space;
585 f[Space::ori_idx(o)] = v;
587 None => panic!("there is no node here"),
591 fn face_adj_available_faces(&self, f: Face) -> Vec<Face> {
592 let mut faces = Vec::new();
593 for e in self.face_edges(f) {
594 for adj_f in self.adj_available_faces(e) {
603 fn face_adj_existing_faces(&self, f: Face) -> Vec<Face> {
604 let mut faces = Vec::new();
605 for e in self.face_edges(f) {
606 for adj_f in self.adj_existing_faces(e) {
615 fn suround_with_nodes(&mut self, (n, o): Face) {
616 assert!(self.is_node(n));
617 for (nf, _) in self.face_adj_available_faces((n, o)) {
618 self.add_empty_node_if_none(nf);
622 pub fn add_face(&mut self, f: Face) {
623 self.suround_with_nodes(f);
624 self.set_face(f, Some((1.0, 1.0, 1.0)));
627 pub fn remove_face(&mut self, f: Face) {
628 assert!(self.is_face(f));
629 //panic!("not yet implemented");
630 self.set_face(f, None);
633 pub fn set_current_face_if_none(&mut self) {
635 Pos::F(f) => if !self.is_face(f) { self.add_face(f) },
641 impl Clone for Space {
642 fn clone(&self) -> Space {
646 current: self.current,
666 fn new() -> History {
674 fn add(&mut self, s: Save) {
675 if self.saves.len() > self.id {
676 println!("clear next saves");
677 while self.saves.len() > self.id {
685 pub fn has_prev(&self) -> bool {
689 pub fn has_next(&self) -> bool {
690 self.saves.len() > self.id
693 fn prev(&mut self) -> Save {
694 assert!(self.has_prev());
697 self.saves[self.id-1].clone()
700 fn next(&mut self) -> Save {
701 assert!(self.has_next());
704 self.saves[self.id-1].clone()
707 fn current(&self) -> Option<&Save> {
711 Some(&self.saves[self.id-1])
715 fn replace_current(&mut self, s: Save) {
716 assert!(self.id != 0);
717 self.saves[self.id-1] = s;
723 pub history: History,
727 pub fn new() -> Data {
728 Data { space: Space::new(), history: History::new() }
731 fn save_to_add(&self) -> Option<Save> {
732 match self.space.change {
734 let mut space = self.space.clone();
735 space.change = Change::None;
736 Some(Save::Space(space))
738 Change::Current => Some(Save::Current(self.space.current)),
739 Change::Size(e, s) => Some(Save::Size(e, s)),
740 Change::None => None,
744 pub fn save_if_changed(&mut self) {
745 match self.save_to_add() {
746 Some(Save::Size(e, s)) =>
747 match self.history.current() {
748 Some(&Save::Size(e, _)) =>
749 self.history.replace_current(Save::Size(e, s)),
750 _ => self.history.add(Save::Size(e, s)),
752 Some(s) => self.history.add(s),
755 self.space.change = Change::None;
758 pub fn load_prev(&mut self) {
759 println!("load prev");
760 match self.history.prev() {
761 Save::Current(c) => self.space.current = c,
762 Save::Space(s) => self.space = s,
763 Save::Size(e, s) => self.space.edge_size_no_change(e, s),
767 pub fn load_next(&mut self) {
768 println!("load next");
769 match self.history.next() {
770 Save::Current(c) => self.space.current = c,
771 Save::Space(s) => self.space = s,
772 Save::Size(e, s) => self.space.edge_size_no_change(e, s),
776 pub fn load_prev_if_any(&mut self) {
777 if self.history.has_prev() {
782 pub fn load_next_if_any(&mut self) {
783 if self.history.has_next() {