#How do you split an array of an array of 3 numbers into an vec of arrays of 3 arrays of 3 numbers?

16 messages · Page 1 of 1 (latest)

mystic valley
#

Pardon the tongue twister, but I'm trying to do what the title says in order to group an ordered vec of verticies([f32; 3]), into a vec of faces. I'm not sure how
to coerce .collect() into doing that. My current attempt at that is below.


use itertools::Itertools;
use glam::*;

pub fn main() {
    let vertices = [([0.0, 1.0, 2.0]), ([3.0, 4.0, 5.0]), ([0.0, 1.0, 2.0]), ([0.0, 1.0, 2.0]), ([0.0, 1.0, 2.0]), ([0.0, 1.0, 2.0])];
    let faces = slice.iter().collect::<Vec<[[f32; 3], 3]>>(); //<-- broken
}

vague dew
frail pasture
#

There's also chunks

mystic valley
# frail pasture There's also `chunks`

chunks will cause run-time errors for my usecase. I want to get a compile time error if I'm trying to give a malformed input, I.E: if i feed 5 verticies and not 3, 6, etc... , faces should throw an error like "something something only accepts [[f32; 3]; 3]", when trying to collect into an iter.

frail pasture
#
const VERTICES: &[[f32; 3]] = &[
    ([0.0, 1.0, 2.0]),
    ([3.0, 4.0, 5.0]),
    ([0.0, 1.0, 2.0]),
    ([0.0, 1.0, 2.0]),
    ([0.0, 1.0, 2.0]),
    ([0.0, 1.0, 2.0]),
];
const _: () = {
    if VERTICES.len() % 3 != 0 {
        panic!("invalid number of vertices");
    }
};
mystic valley
frail pasture
#

It is a compile error. Try it.

mystic valley
frail pasture
#

I think it would be better to make a macro for this, though. Or just take an array of arrays of arrays in the first place.

mystic valley
#

actually wait, verticies are not going to be const, verticies are going to be part of a shape generator. I need the vec of array of 3 arrays of three numbers so I can generate normals for those shapes, and those shapes are variable.

frail pasture
#

how would that make a compile error then

mystic valley
#

verticies would be generated based on user input for like radius/diameter/number of sides, so they wouldnt be const

frail pasture
#

You can't make a compile error out of runtime data

mystic valley
#

gtg, brb

frail pasture
#

Then just store them as [[[f32; 3]; 3]]