#3d cube help

7 messages · Page 1 of 1 (latest)

civic pelican
#
use std::sync::Mutex;

struct Cube {
    points: [Point; 8],
}

struct Cube2D {
    points: [Point2D; 8],
}

struct Point {
    x: f64,
    y: f64,
    z: f64,
}

struct Point2D {
    x: f64,
    y: f64,
}

impl Clone for Cube {
    fn clone(&self) -> Self {
        Cube {
            points: self.points.clone(),
        }
    }
}

impl Clone for Point {
    fn clone(&self) -> Self {
        Point {
            x: self.x,
            y: self.y,
            z: self.z,
        }
    }
}

// How to access the cube
/*  
    let binding = &SCREEN_CUBE;
    let guard = binding.lock().unwrap();
*/

static SCREEN_CUBE: Mutex<Cube> = Mutex::new(Cube {
    points: [
        Point { x: 0.0, y: 0.0, z: 0.0 },
        Point { x: 0.0, y: 0.0, z: 1.0 },
        Point { x: 0.0, y: 1.0, z: 0.0 },
        Point { x: 0.0, y: 1.0, z: 1.0 },
        Point { x: 1.0, y: 0.0, z: 0.0 },
        Point { x: 1.0, y: 0.0, z: 1.0 },
        Point { x: 1.0, y: 1.0, z: 0.0 },
        Point { x: 1.0, y: 1.0, z: 1.0 },
    ],
});

const BASE_CUBE: Cube = Cube {
    points: [
        Point { x: 0.0, y: 0.0, z: 0.0 },
        Point { x: 0.0, y: 0.0, z: 1.0 },
        Point { x: 0.0, y: 1.0, z: 0.0 },
        Point { x: 0.0, y: 1.0, z: 1.0 },
        Point { x: 1.0, y: 0.0, z: 0.0 },
        Point { x: 1.0, y: 0.0, z: 1.0 },
        Point { x: 1.0, y: 1.0, z: 0.0 },
        Point { x: 1.0, y: 1.0, z: 1.0 },
    ],
};

const Z_OFFSET: f64 = -4.0;
const CUBE_SIZE: f64 = 70.0;

fn main() {
    let mut rotation: f64 = 0.0;
    
    loop {
        rotation += 0.1;
        let points = calc_points(rotation);
        println!("{} {} {} {} {} {} {} {}",
            points.points[0].x,
            points.points[0].y,
            points.points[1].x,
            points.points[1].y,
            points.points[2].x,
            points.points[2].y,
            points.points[3].x,
            points.points[3].y,
        );
    }
}

fn calc_points(rotation: f64) -> Cube2D {
    let mut projected_cube: [Point2D; 8] = [
        Point2D { x: 0.0, y: 0.0 },
        Point2D { x: 0.0, y: 0.0 },
        Point2D { x: 0.0, y: 0.0 },
        Point2D { x: 0.0, y: 0.0 },
        Point2D { x: 0.0, y: 0.0 },
        Point2D { x: 0.0, y: 0.0 },
        Point2D { x: 0.0, y: 0.0 },
        Point2D { x: 0.0, y: 0.0 },
    ];

    let binding = &SCREEN_CUBE;
    let guard = binding.lock().unwrap();

    let itter_clone = guard.points.clone();
    let mut guard_points = guard.points.clone();

    drop(guard);

    for (i, _point) in itter_clone.iter().enumerate() {
/*(line 115)*/        guard_points[i].x = BASE_CUBE.points[i].x * rad(rotation).cos() - BASE_CUBE.points[i].y * rad(rotation).sin();
        guard_points[i].y = BASE_CUBE.points[i].y;
        guard_points[i].z = BASE_CUBE.points[i].z * rad(rotation).cos() + BASE_CUBE.points[i].z * rad(rotation).cos() + Z_OFFSET;

        projected_cube[i].x = SCREEN_CUBE.lock().unwrap().points[i].x / SCREEN_CUBE.lock().unwrap().points[i].z * CUBE_SIZE;
        projected_cube[i].y = SCREEN_CUBE.lock().unwrap().points[i].y / SCREEN_CUBE.lock().unwrap().points[i].z * CUBE_SIZE;
    }

    Cube2D {
        points: projected_cube,
    }
}

fn rad(deg: f64) -> f64 {
    deg * std::f32::consts::PI as f64 / 180.0
}

My code seems to be freezing up and doing nothing while returning no errors, it seems to be getting suck on line 115 and I have no idea why all I could do is find where its getting stuck.

#

Also please use pings in replys

next sierra
#

@civic pelican you're locking SCREEN_CUBE twice in one line and this deadlocks. here's a simpler program that fails the same way:

#

?eval

let m = std::sync::Mutex::new(1);
let result = *m.lock().unwrap() + *m.lock().unwrap();
result
grave schoonerBOT
#
The operation timed out```
next sierra
#

repeated locking is inefficient; you should lock it once for the entire function

#

better yet, don't use a mutable static at all