Guest User

Untitled

a guest
Jul 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #![warn(clippy_pedantic, clippy_restriction)]
  2. #![allow(missing_docs_in_private_items)]
  3.  
  4. trait Position {
  5. fn position(&self) -> &[f64; 3];
  6. }
  7.  
  8. #[derive(Debug)]
  9. enum OctreePart<T>
  10. where
  11. T: Position,
  12. {
  13. Item(T),
  14. Branch {
  15. depth: u8,
  16. position: [i128; 3],
  17. inners: [Option<usize>; 8],
  18. },
  19. }
  20.  
  21. impl<T> OctreePart<T>
  22. where
  23. T: Position,
  24. {
  25. fn branch_from(inner_index: usize, depth: u8, position: [i128; 3]) -> Self {
  26. OctreePart::Branch {
  27. depth,
  28. position,
  29. inners: [None; 8],
  30. }
  31. }
  32. }
  33.  
  34. #[derive(Debug)]
  35. struct Octree<T>
  36. where
  37. T: Position,
  38. {
  39. side_length: f64,
  40. inners: Vec<OctreePart<T>>,
  41. }
  42.  
  43. impl<T> Octree<T>
  44. where
  45. T: Position,
  46. {
  47. fn new(side_length: f64) -> Self {
  48. Self {
  49. side_length,
  50. inners: Vec::new(),
  51. }
  52. }
  53.  
  54. /*fn inner(&mut self, branch: &mut OctreePart<T>, inner_index: usize) -> usize {
  55. match branch {
  56. OctreePart::Item(_) => panic!(),
  57. OctreePart::Branch {
  58. depth,
  59. position,
  60. inners,
  61. } => *inners[inner_index]
  62. .map(|inner| {
  63. let inner_item = &mut self.inners[inner];
  64.  
  65. if let OctreePart::Item(_) = inner_item {
  66. let item = std::mem::replace(
  67. inner_item,
  68. OctreePart::branch_from(inner_index, *depth, *position),
  69. );
  70.  
  71. //inner_item
  72. }
  73.  
  74. inner
  75. })
  76. .get_or_insert_with(|| {
  77. self.inners
  78. .push(OctreePart::branch_from(inner_index, *depth, *position));
  79.  
  80. self.inners.len() - 1
  81. }),
  82. }
  83. }*/
  84.  
  85. fn inner(&mut self, part_index: usize) -> Option<usize> {
  86. unimplemented!()
  87. }
  88.  
  89. fn insert(&mut self, item: T) -> Result<(), T> {
  90. let mut part_index = 0;
  91.  
  92. while let Some(inner_part_index) = self.inner(part_index) {
  93. part_index = inner_part_index;
  94. }
  95.  
  96. Err(item)
  97. }
  98. }
  99.  
  100. fn main() {
  101. #[derive(Debug)]
  102. struct Object {
  103. position: [f64; 3],
  104. velocity: [f64; 3],
  105. }
  106.  
  107. impl Position for Object {
  108. fn position(&self) -> &[f64; 3] {
  109. &self.position
  110. }
  111. }
  112.  
  113. let mut octree = Octree::new(2000.0);
  114.  
  115. octree.insert(Object {
  116. position: [500.0; 3],
  117. velocity: [0.0; 3],
  118. });
  119.  
  120. println!("{:?}", octree);
  121. }
Add Comment
Please, Sign In to add comment