فهرست منبع

updated vec struct

Johann Woelper 6 سال پیش
والد
کامیت
952dafc7c6
1فایلهای تغییر یافته به همراه132 افزوده شده و 17 حذف شده
  1. 132 17
      src/main.rs

+ 132 - 17
src/main.rs

@@ -5,17 +5,79 @@ extern crate serde_json;
 use std::io::BufWriter;
 use std::io::BufReader;
 use std::fs::File;
-
+use std::ops::{Add, Sub};
 use std::collections::HashMap;
 use nalgebra::Vector3;
 
-#[derive(Clone, Debug, Default)]
+
+#[derive(Clone, Debug, Serialize, Deserialize, Copy, Eq, PartialEq, Hash)]
+struct V3 {
+    x: i32,
+    y: i32,
+    z: i32
+}
+
+impl V3 {
+    fn new(x: i32, y: i32, z: i32) -> V3 {
+        V3 { x: x, y: y, z: z }
+    }
+
+    fn neighbors(self) -> [V3;6] {
+        [
+            self+V3::new(1,0,0),
+            self+V3::new(-1,0,0),
+            self+V3::new(0,1,0),
+            self+V3::new(0,-1,0),
+            self+V3::new(0,0,1),
+            self+V3::new(0,0,-1),
+        ]
+    }
+
+    fn offsets(self) -> [V3;6] {
+        [
+            V3::new(1,0,0),
+            V3::new(-1,0,0),
+            V3::new(0,1,0),
+            V3::new(0,-1,0),
+            V3::new(0,0,1),
+            V3::new(0,0,-1),
+        ]
+    }
+}
+
+
+impl Add<V3> for  V3 {
+    type Output = V3;
+    fn add(self, other: V3) -> V3 {
+        V3 {
+            x: self.x + other.x,
+            y: self.y + other.y,
+            z: self.z + other.z,
+        }
+    }
+}
+impl Sub<V3> for  V3 {
+    type Output = V3;
+    fn sub(self, other: V3) -> V3 {
+        V3 {
+            x: self.x - other.x,
+            y: self.y - other.y,
+            z: self.z - other.z,
+        }
+    }
+}
+
+
+#[derive(Clone, Debug)]
 struct Voxel {
     id: String,
     // adjacency: HashMap<&'static str, u16>,
-    adjacency: HashMap<Vector3<i32>, Vec<String>>
+    adjacency: HashMap<V3, Vec<String>>,
+    // offsets: [V3; 6]
+
 }
 
+/// A graphics tile as ex- or imported from a 3d program
 #[derive(Clone, Debug, Serialize, Deserialize)]
 struct GfxTile {
     name: String,
@@ -23,9 +85,10 @@ struct GfxTile {
 }
 
 
-fn unitized_grid(vol: &HashMap<Vector3<i32>, String>, divisor: i32) -> HashMap<Vector3<i32>, String> {
-    // vol
-    vol.iter().map(|(k,v)| (k.clone()/divisor,v.clone())).collect()
+
+
+fn unitized_grid(vol: &HashMap<Vector3<i32>, String>, divisor: i32) -> HashMap<V3, String> {
+    vol.iter().map(|(k,v)| (V3{x: k.x/divisor, y: k.y/divisor, z: k.z/divisor},v.clone())).collect()
 }
 
 
@@ -44,7 +107,7 @@ fn guess_gridsize(vol: &HashMap<Vector3<i32>, String>) -> i32{
         last = Some(px.abs());
     }
 
-    let mut grid = 0;
+    let mut grid = 1;
     let mut maxcount = 0;
     for (size, count) in dist_count {
         if count > maxcount {
@@ -58,11 +121,59 @@ fn guess_gridsize(vol: &HashMap<Vector3<i32>, String>) -> i32{
 
 }
 
-fn train_volume(vol: &HashMap<Vector3<i32>, Voxel>) {
-    let mut trained: HashMap<Vector3<i32>, Voxel> = HashMap::new();
-    for voxel in vol {
-
+fn train_volume(vol: &HashMap<V3, Voxel>) {
+    
+    // let mut trainedd: HashMap<Vector3<i32>, Vec<String>> = HashMap::new();
+    //                      Tile name        offset        Vec of tile names
+    let mut trained: HashMap<String, HashMap<V3, Vec<String>>> = HashMap::new();
+
+    for (pos, voxel) in vol {
+        // lookup neighbors
+
+        // let mut neighbors: HashMap<V3, Vec<String>> = HashMap::new();
+
+        let tile = trained.entry(voxel.id.clone()).or_insert(
+            HashMap::new()
+        );
+
+        // for p_n in pos.neighbors().iter() {
+
+        //     dbg!(&p_n);
+        //     dbg!("======================================");
+        // }
+
+        dbg!(&voxel.id);
+        dbg!(&pos);
+        dbg!(&pos.neighbors()[0]);
+        dbg!(&pos.offsets()[0]);
+
+        for p_o in pos.offsets().iter() {
+            let p_n = p_o.clone() + pos.clone();
+            // dbg!(&p_n);
+            if let Some(neighbor) = vol.get(&p_n) {
+                dbg!(&neighbor.id);
+                // dbg!(&tile);
+                
+                // tile.get_mut(p_n).unwrap().push(neighbor.id.clone());
+
+                if let Some(neighbor_tile) = tile.get_mut(&p_o) {
+                    neighbor_tile.push(neighbor.id.clone());
+                } else {
+                    tile.insert(p_o.clone(), vec![neighbor.id.clone()]);
+                }  
+
+                // neighbors.insert(p_n.clone(), vec![neighbor.id.clone()]);
+                
+                // let counter = trained.entry(voxel.id).or_insert(
+
+                // );
+                // *trained.entry(&voxel.id).or_insert(10) = ;
+            }
+        }
+        // dbg!(&neighbors);
+        // *tile;
     }
+    dbg!(&trained);
 }
 
 
@@ -93,22 +204,26 @@ fn load_export(path: &'static str) -> HashMap<Vector3<i32>, String> {
 
 fn main() {
 
-    let mut vol: HashMap<Vector3<i32>, Voxel> = HashMap::new();
+
+
+
+    let mut vol: HashMap<V3, Voxel> = HashMap::new();
 
     let imported_tiles = load_export("out.json");
-    let grid = guess_gridsize(&imported_tiles);
-    let imported_tiles = unitized_grid(&imported_tiles, grid);
+    let gridsize = guess_gridsize(&imported_tiles);
+    dbg!(&gridsize);
+    let imported_tiles = unitized_grid(&imported_tiles, gridsize);
 
 
     for (pos, tile) in imported_tiles {
-        // dbg!(&pos, &tile);
-        vol.insert(pos, Voxel {id: tile, ..Default::default()});
+        vol.insert(pos, Voxel {id: tile, adjacency: HashMap::new()});
     }
 
+    let trained_volume = train_volume(&vol);
 
 
 
-    dbg!(&vol);
+    // dbg!(&vol);
     // let mut adj_map: HashMap<String, Adjacency> = HashMap::new();