Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. use std::rc::Rc;
  2. use std::cell::{RefCell, RefMut, Ref};
  3. use std::ptr;
  4.  
  5.  
  6. #[derive(Debug)]
  7. struct SharedRcOption<T>(Option<Rc<RefCell<T>>>);
  8.  
  9. impl<T> SharedRcOption<T> {
  10. fn none() -> Self {
  11. Self(None)
  12. }
  13.  
  14. fn new(element: T) -> Self {
  15. Self(Some(Rc::new(RefCell::new(element))))
  16. }
  17.  
  18. fn empty(&self) -> bool {
  19. self.0.is_none()
  20. }
  21.  
  22. fn borrow_mut(&mut self) -> RefMut<T> {
  23. self.0.as_mut().expect(
  24. "Empty SharedRCOption cannot be unwrapped"
  25. ).borrow_mut()
  26. }
  27.  
  28. fn borrow(&self) -> Ref<T> {
  29. self.0.as_ref().expect(
  30. "Empty SharedRCOption cannot be unwrapped"
  31. ).borrow()
  32. }
  33.  
  34. fn clear(&mut self) {
  35. self.0 = None;
  36. }
  37. }
  38.  
  39. impl<T> Clone for SharedRcOption<T> {
  40. fn clone(&self) -> Self {
  41. Self(self.0.clone())
  42. }
  43. }
  44.  
  45. trait ToShared where Self: Sized {
  46. fn to_shared(self) -> SharedRcOption<Self> {
  47. SharedRcOption::new(self)
  48. }
  49. }
  50.  
  51. #[derive(Debug)]
  52. struct Face {
  53. neighbor_edge: SharedRcOption<HalfEdge>,
  54. }
  55.  
  56. #[derive(Debug)]
  57. struct Vertex {
  58. location: [i32; 2],
  59. outgoing: SharedRcOption<HalfEdge>,
  60. }
  61.  
  62. impl Vertex {
  63. fn new(location: [i32; 2]) -> Self {
  64. Self {
  65. location,
  66. outgoing: SharedRcOption::none(),
  67. }
  68. }
  69. }
  70.  
  71. #[derive(Debug)]
  72. struct HalfEdge {
  73. next: SharedRcOption<HalfEdge>,
  74. opposite: SharedRcOption<HalfEdge>,
  75. end_vertex: SharedRcOption<Vertex>,
  76. face: SharedRcOption<Face>,
  77. }
  78.  
  79. impl HalfEdge {
  80. fn empty() -> Self {
  81. Self {
  82. next: SharedRcOption::none(),
  83. opposite: SharedRcOption::none(),
  84. end_vertex: SharedRcOption::none(),
  85. face: SharedRcOption::none(),
  86. }
  87. }
  88.  
  89. fn connect(mut from: SharedRcOption<Vertex>, to: SharedRcOption<Vertex>) -> SharedRcOption<Self> {
  90. let mut opposite = Self {
  91. end_vertex: from.clone(),
  92. ..Self::empty()
  93. }.to_shared();
  94.  
  95. let new_edge = Self {
  96. opposite: opposite.clone(),
  97. end_vertex: to,
  98. ..Self::empty()
  99. }.to_shared();
  100.  
  101. if from.borrow().outgoing.empty() {
  102. from.borrow_mut().outgoing = new_edge.clone();
  103. }
  104. opposite.borrow_mut().opposite = new_edge.clone();
  105.  
  106. new_edge
  107. }
  108. }
  109.  
  110. impl ToShared for HalfEdge {}
  111. impl ToShared for Face {}
  112. impl ToShared for Vertex {}
  113.  
  114. struct Mesh {
  115. vertices: Vec<SharedRcOption<Vertex>>,
  116. }
  117.  
  118. impl Mesh {
  119. fn new(start_coordinates: [i32; 2]) -> Self {
  120. Self { vertices: vec![Vertex::new(start_coordinates).to_shared()] }
  121. }
  122.  
  123. fn line_to(&mut self, from: SharedRcOption<Vertex>, to: [i32; 2]) -> SharedRcOption<HalfEdge> {
  124. if from.borrow().location == to {
  125. panic!("Cannot connect a vertex with a new vertex at the same coordinates");
  126. }
  127. let target_vertex = Vertex::new(to).to_shared();
  128. self.vertices.push(target_vertex.clone());
  129. HalfEdge::connect(from, target_vertex)
  130. }
  131.  
  132. fn connect(&mut self, from: SharedRcOption<Vertex>, to: SharedRcOption<Vertex>) -> SharedRcOption<HalfEdge> {
  133. if ptr::eq(&*from.borrow(), &*to.borrow()) {
  134. panic!("Cannot connect a vertex with itself");
  135. }
  136. HalfEdge::connect(from, to)
  137. }
  138. }
  139.  
  140. fn main() {
  141. let mut mesh = Mesh::new([0, 0]);
  142.  
  143. let edge = mesh.line_to(mesh.vertices[0].clone(), [2, 4]);
  144. let start = mesh.vertices[0].borrow().location;
  145. let target_1 = edge.borrow().end_vertex.borrow().location;
  146. let target_2 = edge.borrow().opposite.borrow().end_vertex.borrow().location;
  147. let target_3 = edge.borrow().opposite.borrow().opposite.borrow().end_vertex.borrow().location;
  148. let target_4 = edge.borrow().opposite.borrow().opposite.borrow().opposite.borrow().end_vertex.borrow().location;
  149. mesh.connect(mesh.vertices[1].clone(), mesh.vertices[1].clone());
  150. println!(
  151. "{:?} -> {:?} | {:?} -> {:?} | {:?} -> {:?}",
  152. start, target_1,
  153. target_1, target_2,
  154. target_4, target_3,
  155. );
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement