Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![warn(clippy_pedantic, clippy_restriction)]
- #![allow(missing_docs_in_private_items)]
- trait Position {
- fn position(&self) -> &[f64; 3];
- }
- #[derive(Debug)]
- enum OctreePart<T>
- where
- T: Position,
- {
- Item(T),
- Branch {
- depth: u8,
- position: [i128; 3],
- inners: [Option<usize>; 8],
- },
- }
- impl<T> OctreePart<T>
- where
- T: Position,
- {
- fn branch_from(inner_index: usize, depth: u8, position: [i128; 3]) -> Self {
- OctreePart::Branch {
- depth,
- position,
- inners: [None; 8],
- }
- }
- }
- #[derive(Debug)]
- struct Octree<T>
- where
- T: Position,
- {
- side_length: f64,
- inners: Vec<OctreePart<T>>,
- }
- impl<T> Octree<T>
- where
- T: Position,
- {
- fn new(side_length: f64) -> Self {
- Self {
- side_length,
- inners: Vec::new(),
- }
- }
- /*fn inner(&mut self, branch: &mut OctreePart<T>, inner_index: usize) -> usize {
- match branch {
- OctreePart::Item(_) => panic!(),
- OctreePart::Branch {
- depth,
- position,
- inners,
- } => *inners[inner_index]
- .map(|inner| {
- let inner_item = &mut self.inners[inner];
- if let OctreePart::Item(_) = inner_item {
- let item = std::mem::replace(
- inner_item,
- OctreePart::branch_from(inner_index, *depth, *position),
- );
- //inner_item
- }
- inner
- })
- .get_or_insert_with(|| {
- self.inners
- .push(OctreePart::branch_from(inner_index, *depth, *position));
- self.inners.len() - 1
- }),
- }
- }*/
- fn inner(&mut self, part_index: usize) -> Option<usize> {
- unimplemented!()
- }
- fn insert(&mut self, item: T) -> Result<(), T> {
- let mut part_index = 0;
- while let Some(inner_part_index) = self.inner(part_index) {
- part_index = inner_part_index;
- }
- Err(item)
- }
- }
- fn main() {
- #[derive(Debug)]
- struct Object {
- position: [f64; 3],
- velocity: [f64; 3],
- }
- impl Position for Object {
- fn position(&self) -> &[f64; 3] {
- &self.position
- }
- }
- let mut octree = Octree::new(2000.0);
- octree.insert(Object {
- position: [500.0; 3],
- velocity: [0.0; 3],
- });
- println!("{:?}", octree);
- }
Add Comment
Please, Sign In to add comment