-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
voxel: Add explicit connections to code
- Loading branch information
Showing
13 changed files
with
589 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
softwareComponents/voxelReconfig/src/voxel_world/with_connections/impls/map_connections.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use super::super::Connections; | ||
use crate::atoms::Axis; | ||
use crate::pos::ord::OrdPos; | ||
use crate::pos::Pos; | ||
use std::collections::{btree_map, BTreeMap}; | ||
|
||
pub trait MapConnectionsIndex: | ||
num::Signed + Ord + Copy + std::hash::Hash + std::fmt::Debug | ||
{ | ||
} | ||
impl<T> MapConnectionsIndex for T where | ||
Self: num::Signed + Ord + Copy + std::hash::Hash + std::fmt::Debug | ||
{ | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct MapConnections<IndexType: MapConnectionsIndex> { | ||
connections: BTreeMap<OrdPos<IndexType>, [bool; 3]>, | ||
} | ||
|
||
impl<IndexType: MapConnectionsIndex> Default for MapConnections<IndexType> { | ||
fn default() -> Self { | ||
Self { | ||
connections: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
fn axes_from_inner_value(axes_connected: [bool; 3]) -> smallvec::SmallVec<[Axis; 3]> { | ||
axes_connected | ||
.into_iter() | ||
.zip(enum_iterator::all()) | ||
.filter(|&(axis_connected, _)| axis_connected) | ||
.map(|(_, axis)| axis) | ||
.collect() | ||
} | ||
|
||
impl<TIndex: MapConnectionsIndex> Connections for MapConnections<TIndex> { | ||
type IndexType = TIndex; | ||
type ConnectionIter<'a> = impl 'a + Iterator<Item = (Pos<Self::IndexType>, Axis)> | ||
where | ||
Self: 'a; | ||
|
||
fn connect(&mut self, pos_from: Pos<Self::IndexType>, connected_to: Axis) { | ||
self.connections.entry(OrdPos(pos_from)).or_default()[connected_to.as_index()] = true; | ||
} | ||
|
||
fn disconnect(&mut self, pos_from: Pos<Self::IndexType>, connected_to: Axis) { | ||
if let btree_map::Entry::Occupied(mut entry) = self.connections.entry(OrdPos(pos_from)) { | ||
entry.get_mut()[connected_to.as_index()] = false; | ||
if entry.get().iter().all(|&value| !value) { | ||
// Respect default equality | ||
entry.remove(); | ||
} | ||
} | ||
} | ||
|
||
fn is_connected(&self, pos_from: Pos<Self::IndexType>, connected_to: Axis) -> bool { | ||
if let Some(connections) = self.connections.get(&OrdPos(pos_from)) { | ||
connections[connected_to.as_index()] | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fn connections_from(&self, pos_from: Pos<Self::IndexType>) -> smallvec::SmallVec<[Axis; 3]> { | ||
self.connections | ||
.get(&OrdPos(pos_from)) | ||
.copied() | ||
.map_or_else(Default::default, axes_from_inner_value) | ||
} | ||
|
||
fn all_connections(&self) -> Self::ConnectionIter<'_> { | ||
self.connections.iter().flat_map(|(&OrdPos(pos), &axes)| { | ||
axes_from_inner_value(axes) | ||
.into_iter() | ||
.map(move |axis| (pos, axis)) | ||
}) | ||
} | ||
|
||
fn from_connections( | ||
connections_iter: impl IntoIterator<Item = (Pos<Self::IndexType>, Axis)>, | ||
) -> Self { | ||
let mut result = Self::default(); | ||
for (pos, axis) in connections_iter { | ||
result.connect(pos, axis); | ||
} | ||
result | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
softwareComponents/voxelReconfig/src/voxel_world/with_connections/impls/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod map_connections; | ||
pub mod set_connections; |
76 changes: 76 additions & 0 deletions
76
softwareComponents/voxelReconfig/src/voxel_world/with_connections/impls/set_connections.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use super::super::Connections; | ||
use crate::atoms::Axis; | ||
use crate::pos::ord::OrdPos; | ||
use crate::pos::Pos; | ||
use enum_iterator::Sequence; | ||
use std::collections::BTreeSet; | ||
|
||
pub trait SetConnectionsIndex: | ||
num::Signed + Ord + Copy + std::hash::Hash + std::fmt::Debug | ||
{ | ||
} | ||
impl<T> SetConnectionsIndex for T where | ||
Self: num::Signed + Ord + Copy + std::hash::Hash + std::fmt::Debug | ||
{ | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct SetConnections<IndexType: SetConnectionsIndex> { | ||
connections: BTreeSet<(OrdPos<IndexType>, Axis)>, | ||
} | ||
|
||
impl<TIndex: SetConnectionsIndex> Default for SetConnections<TIndex> { | ||
fn default() -> Self { | ||
Self { | ||
connections: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
fn iter_adaptor<TIndex: num::Num + Copy>(value: &(OrdPos<TIndex>, Axis)) -> (Pos<TIndex>, Axis) { | ||
let &(OrdPos(pos), axis) = value; | ||
(pos, axis) | ||
} | ||
|
||
impl<TIndex: SetConnectionsIndex> Connections for SetConnections<TIndex> { | ||
type IndexType = TIndex; | ||
type ConnectionIter<'a> = impl 'a + Iterator<Item = (Pos<Self::IndexType>, Axis)> | ||
where | ||
Self: 'a; | ||
|
||
fn connect(&mut self, pos_from: Pos<Self::IndexType>, connected_to: Axis) { | ||
self.connections.insert((OrdPos(pos_from), connected_to)); | ||
} | ||
|
||
fn disconnect(&mut self, pos_from: Pos<Self::IndexType>, connected_to: Axis) { | ||
self.connections.remove(&(OrdPos(pos_from), connected_to)); | ||
} | ||
|
||
fn is_connected(&self, pos_from: Pos<Self::IndexType>, connected_to: Axis) -> bool { | ||
self.connections.contains(&(OrdPos(pos_from), connected_to)) | ||
} | ||
|
||
fn connections_from(&self, pos_from: Pos<Self::IndexType>) -> smallvec::SmallVec<[Axis; 3]> { | ||
let first_axis = Axis::first().unwrap(); | ||
let last_axis = Axis::last().unwrap(); | ||
self.connections | ||
.range((OrdPos(pos_from), first_axis)..=(OrdPos(pos_from), last_axis)) | ||
.map(|&(_, axis)| axis) | ||
.collect() | ||
} | ||
|
||
fn all_connections(&self) -> Self::ConnectionIter<'_> { | ||
self.connections.iter().map(iter_adaptor) | ||
} | ||
|
||
fn from_connections( | ||
connections_iter: impl IntoIterator<Item = (Pos<Self::IndexType>, Axis)>, | ||
) -> Self { | ||
Self { | ||
connections: connections_iter | ||
.into_iter() | ||
.map(|(pos, axis)| (OrdPos(pos), axis)) | ||
.collect(), | ||
} | ||
} | ||
} |
Oops, something went wrong.