6 use piston_window::{PistonWindow, TextureSettings, Texture};
9 use image::{ImageFormat, load_from_memory_with_format};
11 use std::process::{Command, Output};
14 use graphics::{Line, Rectangle, Polygon, Ellipse, DrawState, Transformed};
16 const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
17 const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
19 fn rand_color() -> [f32; 4] {
28 pub fn view_space<G: graphics::Graphics>(s: &Space, (win_w, win_h): (f64, f64), c: graphics::Context, g: &mut G) {
29 let (xctr, zctr) = ((win_w - 150.0) / 2.0, (win_h - 50.0) / 2.0);
30 let (xcur, ycur, zcur) = (s.size_before(Axis::X) as f64,
31 (s.size_before(Axis::Y) / 2) as f64,
32 s.size_before(Axis::Z) as f64);
33 let (wshift, hshift) = (xctr - (xcur + ycur) + 150.0,
34 zctr - (zcur + ycur) + 50.0);
36 for iy in 0..s.faces[0].len() {
37 let ysize = (s.size[Space::axis_to_idx(Axis::Y)][iy] / 3) as f64;
39 for ix in 0..s.faces.len() {
40 let xsize = s.size[Space::axis_to_idx(Axis::X)][ix] as f64;
42 for iz in 0..s.faces[0][0].len() {
43 let zsize = s.size[Space::axis_to_idx(Axis::Z)][iz] as f64;
44 match s.faces[iz][iy][ix] {
48 if ss[Space::ori_to_idx(Ori::Front)] {
49 Rectangle::new(rand_color())
50 .draw([0.0, 0.0, xsize, zsize],
51 &DrawState::default(),
52 c.transform.trans(x, z),
54 Rectangle::new_border(BLACK, 1.0)
55 .draw([0.0, 0.0, xsize, zsize],
56 &DrawState::default(),
57 c.transform.trans(x, z),
60 if ss[Space::ori_to_idx(Ori::Floor)] {
61 Polygon::new(rand_color())
64 [x + xsize - ysize, z - ysize],
65 [x - ysize, z - ysize]],
66 &DrawState::default(),
72 &DrawState::default(),
77 x + xsize - ysize, z - ysize],
78 &DrawState::default(),
82 .draw([x + xsize - ysize, z - ysize,
83 x - ysize, z - ysize],
84 &DrawState::default(),
88 .draw([x - ysize, z - ysize,
90 &DrawState::default(),
94 if ss[Space::ori_to_idx(Ori::Side)] {
95 Polygon::new(rand_color())
96 .draw(&[[x, z + zsize],
98 [x - ysize, z - ysize],
99 [x - ysize, z + zsize - ysize]],
100 &DrawState::default(),
103 Line::new(BLACK, 1.0)
106 &DrawState::default(),
109 Line::new(BLACK, 1.0)
111 x - ysize, z - ysize],
112 &DrawState::default(),
115 Line::new(BLACK, 1.0)
116 .draw([x - ysize, z - ysize,
117 x - ysize, z + zsize - ysize],
118 &DrawState::default(),
121 Line::new(BLACK, 1.0)
122 .draw([x - ysize, z + zsize - ysize,
124 &DrawState::default(),
128 if (ix, iy, iz) == s.current_node_coord() {
130 .draw([-3.0, -3.0, 7.0, 7.0],
131 &DrawState::default(),
132 c.transform.trans(x, z),
135 Pos::E((_, a)) => match a {
136 Axis::X => Line::new(RED, 1.0)
137 .draw([x, z, x + xsize, z],
138 &DrawState::default(),
141 Axis::Y => Line::new(RED, 1.0)
142 .draw([x, z, x - ysize, z - ysize],
143 &DrawState::default(),
146 Axis::Z => Line::new(RED, 1.0)
147 .draw([x, z, x, z + zsize],
148 &DrawState::default(),
152 Pos::F((_, o)) => match o {
153 Ori::Front => Rectangle::new_border(RED, 1.0)
154 .draw([0.0, 0.0, xsize, zsize],
155 &DrawState::default(),
156 c.transform.trans(x, z),
162 &DrawState::default(),
167 x + xsize - ysize, z - ysize],
168 &DrawState::default(),
172 .draw([x + xsize - ysize, z - ysize,
173 x - ysize, z - ysize],
174 &DrawState::default(),
178 .draw([x - ysize, z - ysize,
180 &DrawState::default(),
188 &DrawState::default(),
193 x - ysize, z - ysize],
194 &DrawState::default(),
198 .draw([x - ysize, z - ysize,
199 x - ysize, z + zsize - ysize],
200 &DrawState::default(),
204 .draw([x - ysize, z + zsize - ysize,
206 &DrawState::default(),
224 pub fn view_visual<G: graphics::Graphics>(w: &PistonWindow,
226 c: graphics::Context,
228 let output = Command::new("echo")
229 .arg(format!("'{}'", s.dot_graph_code()))
234 .unwrap_or_else(|e| panic!("system call failed: {}", e));
235 assert!(output.status.success());
236 let rgba_img = load_from_memory_with_format(&output.stdout,
238 .unwrap_or_else(|e| panic!("PNG image loading failed: {}", e))
240 /*piston_window::image(&Texture::from_image(&mut w.factory,
242 &TextureSettings::new())
243 .unwrap_or_else(|_| panic!("image import failed: {}")),