Conversation
|
Well, the trick is to use some unsafe code that we know is actually safe. When you annotate a type with // This is safe, because we defined Vec3r<R> as transparent over Vector3<R>
let particles = unsafe { std::mem::transmute::<Vec<Vec3r<R>>, Vec<Vector3<R>>>(particles_ply) };(we could drop the explicit type arguments on However, for this to work, your #[repr(transparent)]
struct Vec3r<R: Real>(Vector3<R>);and you would have to slightly adapt your trait implementation. Do you want to make these changes? |
|
Ah, and yes, it would probably make sense to modularize the IO stuff more (maybe move it to the lib) and add some unit tests with sample files, but I don't want to get into this right now. |
|
Whoops, made a mistake. The unsafe code actually has to be // Convert the particle vector from `Vec<Vec3r<R>>` to `Vec<Vector3<R>>`
let particles = unsafe {
// Ensure the original vector is not dropped
let mut particles = std::mem::ManuallyDrop::new(particles_ply);
// Construct the new vector by converting the data pointer
Vec::from_raw_parts(
// This is safe, because we defined Vec3r<R> transparent over Vector3<R>
particles.as_mut_ptr() as *mut Vector3<R>,
particles.len(),
particles.capacity(),
)
};because you are only allowed to transmute the actual data of the Rust |
|
Hrm, How about we leave it with the (non-idiomatic) approach for now? I am thinking of solving this on the Ply-rs side, i.e. adding a data marshalling interface for when your types arent local (honestly most people will be using 3rd party structs for their 3d data?). Cheers |
|
Right, support for this in ply-rs itself would be much cleaner. I'll add a few comments before merging. |
| } | ||
|
|
||
| // #[repr(transparent)] | ||
| struct Vec3r(Vector3<f32>); |
There was a problem hiding this comment.
I think we should rename this to Vec3f32 for now, because currently we only support reading PLY files with floats.
There was a problem hiding this comment.
- you can remove the repr attribtute
| Self { | ||
| 0: { Vector3::new(0.0, 0.0, 0.0) }, | ||
| } |
There was a problem hiding this comment.
Maybe a better default is to use NAN here? So that if properties are missing below, it is not as silent as it would be with zeros? Btw. you can write
Self(Vector3::repeat(std::f32::NAN))| let mut ply_file = std::io::BufReader::new(ply_file); | ||
|
|
||
| let vertex_parser = ply_rs::parser::Parser::<Vec3r>::new(); | ||
| let header = vertex_parser.read_header(&mut ply_file).unwrap(); |
There was a problem hiding this comment.
Instead of unwrap
.context("Failed to read PLY header")?|
Ok, I moved the file format stuff into submodules. You would have to move your changes into the |
|
Hey, I'll have a look at this soon, been moving around the last week, so it has been hard to get time at the computer.. Cheers! |
|
No rush |
|
So we should probably close this. Apologies, it has been quite a long time ago. IIRC I had a look into making it more idiomatic, but it was a PITA, and didnt make the code any clearer, or simpler. Cheers |
a2e7640 to
bfe4c19
Compare
This adds loading into structs directly from ply-rs.
as per your suggestions from #1.
Couple of things:
You may want me to pull the ply stuff out into a submodule, which I can do.
I may not have been using the repr(transparent) properly, I still had to access the inner struct through .0, so I have left that commented out for now.
Any advice welcome...
Thanks!