Compare commits
4 Commits
9cab1916c9
...
c104a22407
| Author | SHA1 | Date | |
|---|---|---|---|
| c104a22407 | |||
| bd257cf2dd | |||
| 17d9cbc86f | |||
| ba02162d97 |
29
Cargo.toml
29
Cargo.toml
@@ -30,23 +30,26 @@ required-features = ["bin"]
|
||||
[features]
|
||||
bin = ["measure_time", "pretty_env_logger"]
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
[dependencies]
|
||||
ironsea_index = "^0.1"
|
||||
ironsea_index_sfc_dbc = "^0.1"
|
||||
ironsea_index_hashmap = "^0.1"
|
||||
ironsea_index = "0.1"
|
||||
ironsea_index_sfc_dbc = "0.1"
|
||||
ironsea_index_hashmap = "0.1"
|
||||
|
||||
arrayref = "^0.3" # For Positions Objects
|
||||
lazy_static = "^1.3"
|
||||
memmap = "^0.7"
|
||||
arrayref = "0.3" # For Positions Objects
|
||||
lazy_static = "1.5"
|
||||
memmap = "0.7"
|
||||
|
||||
serde = { version = "^1.0", features = ["derive"] }
|
||||
serde_json = "^1.0"
|
||||
bincode = "^1.1"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
bincode = "1.3"
|
||||
|
||||
# Logging macros API
|
||||
#log = { version = "^0.4", features = ["max_level_trace", "release_max_level_info"] }
|
||||
log = { version = "^0.4", features = ["max_level_trace", "release_max_level_trace"] }
|
||||
#log = { version = "0.4", features = ["max_level_trace", "release_max_level_info"] }
|
||||
log = { version = "0.4", features = ["max_level_trace", "release_max_level_trace"] }
|
||||
|
||||
# Used for main.rs as integration test
|
||||
pretty_env_logger = { version = "^0.3", optional = true } # Logger implementation
|
||||
measure_time = { version = "^0.6", optional = true } # To mesure parsing time, only required by binary
|
||||
pretty_env_logger = { version = "0.5", optional = true } # Logger implementation
|
||||
measure_time = { version = "0.8", optional = true } # To mesure parsing time, only required by binary
|
||||
|
||||
@@ -7,6 +7,8 @@ use super::space::Space;
|
||||
use super::space_db::SpaceDB;
|
||||
use super::space_index::SpaceSetObject;
|
||||
use super::DataBase;
|
||||
use super::IterObjects;
|
||||
use super::IterPositions;
|
||||
use super::ResultSet;
|
||||
|
||||
/// Query Parameters.
|
||||
@@ -174,11 +176,7 @@ impl Core {
|
||||
|
||||
// We cannot return less that the total number of individual Ids stored
|
||||
// in the index for a full-volume query.
|
||||
let max_elements = if let Some(elem) = max_elements {
|
||||
Some(elem.max(properties.len()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let max_elements = max_elements.map(|elem| elem.max(properties.len()));
|
||||
|
||||
for space in spaces {
|
||||
// Filter the points of this space, and encode them before creating the index.
|
||||
@@ -194,7 +192,7 @@ impl Core {
|
||||
object.set_position(space.encode(&position)?);
|
||||
}
|
||||
|
||||
space_dbs.push(SpaceDB::new(&space, filtered, scales.clone(), max_elements))
|
||||
space_dbs.push(SpaceDB::new(space, filtered, scales.clone(), max_elements))
|
||||
}
|
||||
|
||||
Ok(Core {
|
||||
@@ -220,31 +218,37 @@ impl Core {
|
||||
&self.properties
|
||||
}
|
||||
|
||||
fn decode_positions(
|
||||
list: &mut [(Position, &Properties)],
|
||||
space: &Space,
|
||||
db: &DataBase,
|
||||
fn decode_positions<'b>(
|
||||
list: IterObjects<'b>,
|
||||
space: &'b Space,
|
||||
db: &'b DataBase,
|
||||
output_space: &Option<&str>,
|
||||
) -> Result<(), String> {
|
||||
if let Some(unified_id) = *output_space {
|
||||
) -> Result<IterObjects<'b>, String> {
|
||||
let b: IterObjects = if let Some(unified_id) = *output_space {
|
||||
let unified = db.space(unified_id)?;
|
||||
|
||||
// Rebase the point to the requested output space before decoding.
|
||||
for (position, _) in list {
|
||||
*position = unified
|
||||
.decode(&Space::change_base(&position, space, unified)?)?
|
||||
.into();
|
||||
Box::new(list.filter_map(move |(position, properties)| {
|
||||
match Space::change_base(&position, space, unified) {
|
||||
Err(_) => None,
|
||||
Ok(rebased) => match unified.decode(&rebased) {
|
||||
Err(_) => None,
|
||||
Ok(decoded) => Some((decoded.into(), properties)),
|
||||
},
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
// Decode the positions into f64 values, which are defined in their
|
||||
// respective reference space.
|
||||
for (position, _) in list {
|
||||
// Simply decode
|
||||
*position = space.decode(&position)?.into();
|
||||
}
|
||||
}
|
||||
Box::new(list.filter_map(
|
||||
move |(position, properties)| match space.decode(&position) {
|
||||
Err(_) => None,
|
||||
Ok(decoded) => Some((decoded.into(), properties)),
|
||||
},
|
||||
))
|
||||
};
|
||||
|
||||
Ok(())
|
||||
Ok(b)
|
||||
}
|
||||
|
||||
/// Retrieve everything located at specific positions.
|
||||
@@ -262,46 +266,57 @@ impl Core {
|
||||
/// reference space.
|
||||
///
|
||||
/// [shape]: space/enum.Shape.html
|
||||
pub fn get_by_positions(
|
||||
&self,
|
||||
parameters: &CoreQueryParameters,
|
||||
positions: &[Position],
|
||||
space_id: &str,
|
||||
) -> ResultSet {
|
||||
pub fn get_by_positions<'d>(
|
||||
&'d self,
|
||||
parameters: &'d CoreQueryParameters,
|
||||
positions: Vec<Position>,
|
||||
space_id: &'d str,
|
||||
) -> ResultSet<'d> {
|
||||
let CoreQueryParameters {
|
||||
db, output_space, ..
|
||||
} = parameters;
|
||||
|
||||
let mut results = vec![];
|
||||
let count = positions.len();
|
||||
let from = db.space(space_id)?;
|
||||
|
||||
// Filter positions based on the view port, if present
|
||||
let filtered = match parameters.view_port(from) {
|
||||
None => positions.iter().map(|p| p).collect::<Vec<_>>(),
|
||||
Some(view_port) => positions
|
||||
.iter()
|
||||
.filter(|&p| view_port.contains(p))
|
||||
.collect::<Vec<_>>(),
|
||||
};
|
||||
|
||||
for s in &self.space_db {
|
||||
let to = db.space(s.name())?;
|
||||
let mut p = Vec::with_capacity(count);
|
||||
|
||||
for position in filtered.as_slice() {
|
||||
let position: Vec<f64> = Space::change_base(position, from, to)?.into();
|
||||
p.push(to.encode(&position)?);
|
||||
}
|
||||
|
||||
let mut r = s
|
||||
.get_by_positions(&p, parameters)?
|
||||
// Filter positions based on the view port, if present
|
||||
// FIXME: remove clone() on positions?
|
||||
let filtered: IterPositions = match parameters.view_port(from) {
|
||||
None => Box::new(positions.clone().into_iter()),
|
||||
Some(view_port) => Box::new(
|
||||
positions
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(position, fields)| (position, &self.properties[fields.value()]))
|
||||
.collect::<Vec<_>>();
|
||||
Self::decode_positions(r.as_mut_slice(), to, db, output_space)?;
|
||||
.filter(move |p| view_port.contains(p)),
|
||||
),
|
||||
};
|
||||
|
||||
results.push((s.name(), r));
|
||||
// Rebase the positions into the current space
|
||||
let p = filtered.filter_map(move |position| {
|
||||
match Space::change_base(&position, from, to) {
|
||||
Err(_) => None,
|
||||
Ok(position) => {
|
||||
let position: Vec<f64> = position.into();
|
||||
match to.encode(&position) {
|
||||
Err(_) => None,
|
||||
Ok(position) => Some(position),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Select the data based on the rebased viewport filter.
|
||||
let r = s
|
||||
.get_by_positions(p, parameters)?
|
||||
.map(move |(position, fields)| (position, &self.properties[fields.value()]));
|
||||
|
||||
results.push((
|
||||
s.name(),
|
||||
Self::decode_positions(Box::new(r), to, db, output_space)?,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(results)
|
||||
@@ -322,12 +337,12 @@ impl Core {
|
||||
/// reference space.
|
||||
///
|
||||
/// [shape]: space/enum.Shape.html
|
||||
pub fn get_by_shape(
|
||||
&self,
|
||||
parameters: &CoreQueryParameters,
|
||||
shape: &Shape,
|
||||
space_id: &str,
|
||||
) -> ResultSet {
|
||||
pub fn get_by_shape<'d>(
|
||||
&'d self,
|
||||
parameters: &'d CoreQueryParameters,
|
||||
shape: Shape,
|
||||
space_id: &'d str,
|
||||
) -> ResultSet<'d> {
|
||||
let CoreQueryParameters {
|
||||
db, output_space, ..
|
||||
} = parameters;
|
||||
@@ -343,14 +358,14 @@ impl Core {
|
||||
// let current_shape = shape.encode(current_space)?;
|
||||
// println!("current shape Encoded: {:?}", current_shape);
|
||||
|
||||
let mut r = s
|
||||
.get_by_shape(¤t_shape, parameters)?
|
||||
.into_iter()
|
||||
.map(|(position, fields)| (position, &self.properties[fields.value()]))
|
||||
.collect::<Vec<_>>();
|
||||
Self::decode_positions(r.as_mut_slice(), current_space, db, output_space)?;
|
||||
let r = s
|
||||
.get_by_shape(current_shape, parameters)?
|
||||
.map(move |(position, fields)| (position, &self.properties[fields.value()]));
|
||||
|
||||
results.push((s.name(), r));
|
||||
results.push((
|
||||
s.name(),
|
||||
Self::decode_positions(Box::new(r), current_space, db, output_space)?,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(results)
|
||||
@@ -366,11 +381,11 @@ impl Core {
|
||||
/// * `id`:
|
||||
/// Identifier for which to retrieve is positions.
|
||||
///
|
||||
pub fn get_by_id<S>(
|
||||
&self,
|
||||
parameters: &CoreQueryParameters,
|
||||
pub fn get_by_id<'s, S>(
|
||||
&'s self,
|
||||
parameters: &'s CoreQueryParameters,
|
||||
id: S,
|
||||
) -> Result<Vec<(&String, Vec<Position>)>, String>
|
||||
) -> Result<Vec<(&String, IterPositions<'s>)>, String>
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
@@ -391,26 +406,32 @@ impl Core {
|
||||
for s in &self.space_db {
|
||||
let current_space = db.space(s.name())?;
|
||||
|
||||
let mut positions = s.get_by_id(offset, parameters)?;
|
||||
let positions_by_id = s.get_by_id(offset, parameters)?;
|
||||
|
||||
//Self::decode_positions(r.as_mut_slice(), current_space, db, output_space)?;
|
||||
if let Some(unified_id) = *output_space {
|
||||
let positions: IterPositions = if let Some(unified_id) = *output_space {
|
||||
let unified = db.space(unified_id)?;
|
||||
|
||||
// Rebase the point to the requested output space before decoding.
|
||||
for position in &mut positions {
|
||||
*position = unified
|
||||
.decode(&Space::change_base(position, current_space, unified)?)?
|
||||
.into();
|
||||
Box::new(positions_by_id.filter_map(move |position| {
|
||||
match Space::change_base(&position, current_space, unified) {
|
||||
Err(_) => None,
|
||||
Ok(rebased) => match unified.decode(&rebased) {
|
||||
Err(_) => None,
|
||||
Ok(decoded) => Some(decoded.into()),
|
||||
},
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
// Decode the positions into f64 values, which are defined in their
|
||||
// respective reference space.
|
||||
for position in &mut positions {
|
||||
// Simply decode
|
||||
*position = current_space.decode(position)?.into();
|
||||
}
|
||||
Box::new(positions_by_id.filter_map(move |position| {
|
||||
match current_space.decode(&position) {
|
||||
Err(_) => None,
|
||||
Ok(decoded) => Some(decoded.into()),
|
||||
}
|
||||
}))
|
||||
};
|
||||
|
||||
results.push((s.name(), positions));
|
||||
}
|
||||
@@ -430,7 +451,11 @@ impl Core {
|
||||
/// * `id`:
|
||||
/// Identifier to use to define the search volume.
|
||||
///
|
||||
pub fn get_by_label<S>(&self, parameters: &CoreQueryParameters, id: S) -> ResultSet
|
||||
pub fn get_by_label<'d, S>(
|
||||
&'d self,
|
||||
parameters: &'d CoreQueryParameters,
|
||||
id: S,
|
||||
) -> ResultSet<'d>
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
@@ -454,7 +479,7 @@ impl Core {
|
||||
let search_volume = self
|
||||
.space_db
|
||||
.iter()
|
||||
.filter_map(|s| {
|
||||
.filter_map(move |s| {
|
||||
match db.space(s.name()) {
|
||||
Err(_) => None,
|
||||
Ok(from) => match s.get_by_id(offset, parameters) {
|
||||
@@ -475,42 +500,40 @@ impl Core {
|
||||
},
|
||||
}
|
||||
})
|
||||
.flat_map(|v| v);
|
||||
|
||||
let search_volume = if let Some(view) = view_port {
|
||||
search_volume
|
||||
.filter(|p| view.contains(p))
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
search_volume.collect::<Vec<_>>()
|
||||
};
|
||||
.flatten();
|
||||
|
||||
// Select based on the volume, and filter out the label position themselves.
|
||||
for s in &self.space_db {
|
||||
let to = db.space(s.name())?;
|
||||
let mut p = vec![];
|
||||
|
||||
let search_volume: IterPositions = if let Some(view) = view_port.clone() {
|
||||
Box::new(search_volume.clone().filter(move |p| view.contains(p)))
|
||||
} else {
|
||||
Box::new(search_volume.clone())
|
||||
};
|
||||
|
||||
// Convert the search Volume into the target space.
|
||||
for position in &search_volume {
|
||||
let position = Space::change_base(position, Space::universe(), to)?;
|
||||
p.push(position);
|
||||
let p = search_volume.filter_map(move |position| {
|
||||
match Space::change_base(&position, Space::universe(), to) {
|
||||
Err(_) => None,
|
||||
Ok(position) => Some(position),
|
||||
}
|
||||
});
|
||||
|
||||
let mut r = s
|
||||
.get_by_positions(&p, parameters)?
|
||||
.into_iter()
|
||||
.filter_map(|(position, fields)| {
|
||||
let r = s
|
||||
.get_by_positions(p, parameters)?
|
||||
.filter_map(move |(position, fields)| {
|
||||
if fields.value() == offset {
|
||||
None
|
||||
} else {
|
||||
Some((position, &self.properties[fields.value()]))
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
});
|
||||
|
||||
Self::decode_positions(r.as_mut_slice(), to, db, output_space)?;
|
||||
|
||||
results.push((s.name(), r));
|
||||
results.push((
|
||||
s.name(),
|
||||
Self::decode_positions(Box::new(r), to, db, output_space)?,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,13 +14,20 @@ pub use db_core::Properties;
|
||||
use space::Position;
|
||||
use space::Space;
|
||||
|
||||
/// TODO doc
|
||||
pub type IterPositions<'i> = Box<dyn Iterator<Item = Position> + 'i>;
|
||||
/// TODO doc
|
||||
pub type IterObjects<'i> = Box<dyn Iterator<Item = (Position, &'i Properties)> + 'i>;
|
||||
/// TODO doc
|
||||
pub type IterObjectsBySpaces<'i> = Vec<(&'i String, IterObjects<'i>)>;
|
||||
|
||||
/// Selected tuples matching a query.
|
||||
///
|
||||
/// This is either:
|
||||
/// * `Err` with a reason stored as a `String`
|
||||
/// * `Ok`, with a vector of tuples defined as:
|
||||
/// `(Space Name, [(Position, Properties)])`
|
||||
pub type ResultSet<'r> = Result<Vec<(&'r String, Vec<(Position, &'r Properties)>)>, String>;
|
||||
pub type ResultSet<'r> = Result<IterObjectsBySpaces<'r>, String>;
|
||||
|
||||
type ReferenceSpaceIndex = ironsea_index_hashmap::Index<Space, String>;
|
||||
type CoreIndex = ironsea_index_hashmap::Index<Core, String>;
|
||||
@@ -109,7 +116,7 @@ impl DataBase {
|
||||
list.len()
|
||||
))
|
||||
} else {
|
||||
Ok(&list[0])
|
||||
Ok(list[0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +135,10 @@ impl DataBase {
|
||||
if name == space::Space::universe().name() {
|
||||
Ok(space::Space::universe())
|
||||
} else {
|
||||
let r = self.reference_spaces.find(&name.to_string());
|
||||
let r = self
|
||||
.reference_spaces
|
||||
.find(&name.to_string())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Self::check_exactly_one(&r, "spaces", name)
|
||||
}
|
||||
@@ -146,7 +156,7 @@ impl DataBase {
|
||||
/// * `name`:
|
||||
/// The name of the dataset (core) to search for.
|
||||
pub fn core(&self, name: &str) -> Result<&Core, String> {
|
||||
let r = self.cores.find(&name.to_string());
|
||||
let r = self.cores.find(&name.to_string()).collect::<Vec<_>>();
|
||||
|
||||
Self::check_exactly_one(&r, "cores", name)
|
||||
}
|
||||
|
||||
@@ -206,10 +206,10 @@ impl From<u64> for Coordinate {
|
||||
// Slight syntax hack, as exclusive ranges are not yet available.
|
||||
// cf: https://github.com/rust-lang/rust/issues/37854
|
||||
match v {
|
||||
_ if v <= u64::from(std::u8::MAX) => Coordinate::CoordinateU8(v as u8),
|
||||
_ if v <= u64::from(std::u16::MAX) => Coordinate::CoordinateU16(v as u16),
|
||||
_ if v <= u64::from(std::u32::MAX) => Coordinate::CoordinateU32(v as u32),
|
||||
_ => Coordinate::CoordinateU64(v as u64),
|
||||
_ if v <= u64::from(u8::MAX) => Coordinate::CoordinateU8(v as u8),
|
||||
_ if v <= u64::from(u16::MAX) => Coordinate::CoordinateU16(v as u16),
|
||||
_ if v <= u64::from(u32::MAX) => Coordinate::CoordinateU32(v as u32),
|
||||
_ => Coordinate::CoordinateU64(v),
|
||||
/*_ => {
|
||||
panic!("Out of range {} > {}", v, std::u64::MAX);
|
||||
} */
|
||||
|
||||
@@ -84,8 +84,8 @@ impl CoordinateSystem {
|
||||
match self {
|
||||
CoordinateSystem::Universe { .. } => {
|
||||
for _ in 0..self.dimensions() {
|
||||
low.push(std::f64::MIN);
|
||||
high.push(std::f64::MAX);
|
||||
low.push(f64::MIN);
|
||||
high.push(f64::MAX);
|
||||
}
|
||||
}
|
||||
CoordinateSystem::AffineSystem { axes, .. } => {
|
||||
|
||||
@@ -19,7 +19,7 @@ use serde::Serialize;
|
||||
use super::coordinate::Coordinate;
|
||||
|
||||
/// Store a position as efficiently as possible in terms of space.
|
||||
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, Serialize)]
|
||||
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
|
||||
pub enum Position {
|
||||
/// 1 dimension positions.
|
||||
Position1(Coordinate),
|
||||
@@ -60,7 +60,8 @@ impl Position {
|
||||
/// Compute `||self||`.
|
||||
pub fn norm(&self) -> f64 {
|
||||
if let Position::Position1(coordinates) = self {
|
||||
// the square root of a single number to the square is its positive value, so ensure it is.
|
||||
// the square root of a single number to the square is its
|
||||
// positive value, so ensure it is.
|
||||
coordinates.f64().abs()
|
||||
} else {
|
||||
let point: Vec<&Coordinate> = self.into();
|
||||
@@ -127,6 +128,12 @@ impl Display for Position {
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Position {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.partial_cmp(other).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Position {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
// Let's restrict for now to same-length vectors.
|
||||
@@ -143,7 +150,7 @@ impl PartialOrd for Position {
|
||||
return None;
|
||||
}
|
||||
|
||||
let ordering = ordering.drain().filter_map(|v| v).collect::<Vec<_>>();
|
||||
let ordering = ordering.drain().flatten().collect::<Vec<_>>();
|
||||
match ordering.len() {
|
||||
3 => None,
|
||||
2 => {
|
||||
@@ -356,14 +363,14 @@ impl<'s> From<&'s Position> for Vec<&'s Coordinate> {
|
||||
fn from(position: &'s Position) -> Self {
|
||||
match position {
|
||||
Position::Position1(coordinate) => vec![coordinate],
|
||||
Position::Position2(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::Position3(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::Position4(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::Position5(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::Position6(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::Position7(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::Position8(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::PositionN(coordinates) => coordinates.iter().map(|c| c).collect(),
|
||||
Position::Position2(coordinates) => coordinates.iter().collect(),
|
||||
Position::Position3(coordinates) => coordinates.iter().collect(),
|
||||
Position::Position4(coordinates) => coordinates.iter().collect(),
|
||||
Position::Position5(coordinates) => coordinates.iter().collect(),
|
||||
Position::Position6(coordinates) => coordinates.iter().collect(),
|
||||
Position::Position7(coordinates) => coordinates.iter().collect(),
|
||||
Position::Position8(coordinates) => coordinates.iter().collect(),
|
||||
Position::PositionN(coordinates) => coordinates.iter().collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ impl Shape {
|
||||
//FIXME: Is the length properly dealt with? How do we process this for space conversions?
|
||||
let mut r = Vec::with_capacity(center.dimensions());
|
||||
for _ in 0..center.dimensions() {
|
||||
r.push(radius.clone());
|
||||
r.push(*radius);
|
||||
}
|
||||
let r = r.into();
|
||||
let r = from.absolute_position(&r)?;
|
||||
@@ -276,7 +276,7 @@ impl Shape {
|
||||
/// Compute the volume.
|
||||
pub fn volume(&self) -> f64 {
|
||||
match self {
|
||||
Shape::Point(_) => std::f64::EPSILON, // Smallest non-zero volume possible
|
||||
Shape::Point(_) => f64::EPSILON, // Smallest non-zero volume possible
|
||||
Shape::BoundingBox(low, high) => {
|
||||
let mut volume = 1.0;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
use std::hash::Hash;
|
||||
use std::hash::Hasher;
|
||||
|
||||
@@ -15,6 +16,7 @@ use super::space_index::SpaceIndex;
|
||||
use super::space_index::SpaceSetIndex;
|
||||
use super::space_index::SpaceSetObject;
|
||||
use super::CoreQueryParameters;
|
||||
use super::IterPositions;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct SpaceDB {
|
||||
@@ -59,8 +61,7 @@ impl SpaceDB {
|
||||
}
|
||||
|
||||
// Apply fixed scales
|
||||
let mut count = 0;
|
||||
for power in &powers {
|
||||
for (count, power) in powers.iter().enumerate() {
|
||||
space_objects = space_objects
|
||||
.into_iter()
|
||||
.map(|mut o| {
|
||||
@@ -79,8 +80,7 @@ impl SpaceDB {
|
||||
.collect();
|
||||
|
||||
// Make sure we do not shift more position than available
|
||||
let shift = if count >= 31 { 31 } else { count };
|
||||
count += 1;
|
||||
let shift: u32 = if count >= 31 { 31 } else { count.try_into().unwrap() };
|
||||
indices.push((
|
||||
SpaceSetIndex::new(space_objects.iter(), DIMENSIONS, CELL_BITS),
|
||||
vec![power.0, power.0, power.0],
|
||||
@@ -279,11 +279,11 @@ impl SpaceDB {
|
||||
|
||||
// Search by Id, a.k.a values
|
||||
// The results are in encoded space coordinates.
|
||||
pub fn get_by_id(
|
||||
&self,
|
||||
pub fn get_by_id<'s>(
|
||||
&'s self,
|
||||
id: usize,
|
||||
parameters: &CoreQueryParameters,
|
||||
) -> Result<Vec<Position>, String> {
|
||||
) -> Result<IterPositions<'s>, String> {
|
||||
// Is that ID referenced in the current space?
|
||||
let index = self.resolution(parameters);
|
||||
|
||||
@@ -292,15 +292,20 @@ impl SpaceDB {
|
||||
let view_port = parameters.view_port(space);
|
||||
|
||||
// Select the objects
|
||||
let objects = self.resolutions[index].find_by_value(&SpaceFields::new(self.name(), id));
|
||||
// FIXME: How to return an iterator instead of instantiating all
|
||||
// the points here? Needed because of &SpaceFields.
|
||||
let objects = self.resolutions[index]
|
||||
.find_by_value(&SpaceFields::new(self.name(), id))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let results = if let Some(view_port) = view_port {
|
||||
let results: IterPositions<'s> = if let Some(view_port) = view_port {
|
||||
Box::new(
|
||||
objects
|
||||
.into_iter()
|
||||
.filter(|position| view_port.contains(position))
|
||||
.collect::<Vec<_>>()
|
||||
.filter(move |position| view_port.contains(position)),
|
||||
)
|
||||
} else {
|
||||
objects
|
||||
Box::new(objects.into_iter())
|
||||
};
|
||||
|
||||
Ok(results)
|
||||
@@ -308,11 +313,11 @@ impl SpaceDB {
|
||||
|
||||
// Search by positions defining a volume.
|
||||
// The position is expressed in encoded space coordinates, and results are in encoded space coordinates.
|
||||
pub fn get_by_positions(
|
||||
&self,
|
||||
positions: &[Position],
|
||||
pub fn get_by_positions<'s>(
|
||||
&'s self,
|
||||
positions: impl Iterator<Item = Position> + 's,
|
||||
parameters: &CoreQueryParameters,
|
||||
) -> Result<Vec<(Position, &SpaceFields)>, String> {
|
||||
) -> Result<Box<dyn Iterator<Item = (Position, &SpaceFields)> + 's>, String> {
|
||||
let index = self.resolution(parameters);
|
||||
|
||||
// FIXME: Should I do it here, or add the assumption this is a clean list?
|
||||
@@ -321,17 +326,13 @@ impl SpaceDB {
|
||||
//let view_port = parameters.view_port(space);
|
||||
|
||||
// Select the objects
|
||||
let results = positions
|
||||
.iter()
|
||||
.flat_map(|position| {
|
||||
let results = positions.flat_map(move |position| {
|
||||
self.resolutions[index]
|
||||
.find(position)
|
||||
.into_iter()
|
||||
.find(&position)
|
||||
.map(move |fields| (position.clone(), fields))
|
||||
})
|
||||
.collect();
|
||||
});
|
||||
|
||||
Ok(results)
|
||||
Ok(Box::new(results))
|
||||
}
|
||||
|
||||
// Search by Shape defining a volume:
|
||||
@@ -340,11 +341,11 @@ impl SpaceDB {
|
||||
// * Point (Specific position)
|
||||
|
||||
// The Shape is expressed in encoded space coordinates, and results are in encoded space coordinates.
|
||||
pub fn get_by_shape(
|
||||
&self,
|
||||
shape: &Shape,
|
||||
pub fn get_by_shape<'s>(
|
||||
&'s self,
|
||||
shape: Shape,
|
||||
parameters: &CoreQueryParameters,
|
||||
) -> Result<Vec<(Position, &SpaceFields)>, String> {
|
||||
) -> Result<Box<dyn Iterator<Item = (Position, &SpaceFields)> + 's>, String> {
|
||||
let index = self.resolution(parameters);
|
||||
|
||||
// Convert the view port to the encoded space coordinates
|
||||
@@ -352,7 +353,7 @@ impl SpaceDB {
|
||||
let view_port = parameters.view_port(space);
|
||||
|
||||
// Select the objects
|
||||
let results = self.resolutions[index].find_by_shape(&shape, &view_port)?;
|
||||
let results = self.resolutions[index].find_by_shape(shape, &view_port)?;
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use serde::Serialize;
|
||||
use super::space::Coordinate;
|
||||
use super::space::Position;
|
||||
use super::space::Shape;
|
||||
use super::IterPositions;
|
||||
|
||||
#[derive(Clone, Debug, Hash)]
|
||||
pub struct SpaceSetObject {
|
||||
@@ -125,59 +126,63 @@ impl SpaceIndex {
|
||||
}
|
||||
|
||||
// Inputs and Results are expressed in encoded space coordinates.
|
||||
pub fn find(&self, key: &Position) -> Vec<&SpaceFields> {
|
||||
pub fn find<'s>(&'s self, key: &Position) -> Box<dyn Iterator<Item = &SpaceFields> + 's> {
|
||||
self.index.find(key)
|
||||
}
|
||||
|
||||
// Inputs and Results are expressed in encoded space coordinates.
|
||||
fn find_range(&self, start: &Position, end: &Position) -> Vec<(Position, &SpaceFields)> {
|
||||
fn find_range<'s>(
|
||||
&'s self,
|
||||
start: &Position,
|
||||
end: &Position,
|
||||
) -> Box<dyn Iterator<Item = (Position, &SpaceFields)> + 's> {
|
||||
self.index.find_range(start, end)
|
||||
}
|
||||
|
||||
// Inputs and Results are expressed in encoded space coordinates.
|
||||
pub fn find_by_value(&self, id: &SpaceFields) -> Vec<Position> {
|
||||
pub fn find_by_value<'s>(&'s self, id: &'s SpaceFields) -> IterPositions<'s> {
|
||||
self.index.find_by_value(id)
|
||||
}
|
||||
|
||||
// Inputs and Results are also in encoded space coordinates.
|
||||
pub fn find_by_shape(
|
||||
&self,
|
||||
shape: &Shape,
|
||||
pub fn find_by_shape<'s>(
|
||||
&'s self,
|
||||
shape: Shape,
|
||||
view_port: &Option<Shape>,
|
||||
) -> Result<Vec<(Position, &SpaceFields)>, String> {
|
||||
) -> Result<Box<dyn Iterator<Item = (Position, &SpaceFields)> + 's>, String> {
|
||||
match shape {
|
||||
Shape::Point(position) => {
|
||||
if let Some(mbb) = view_port {
|
||||
if !mbb.contains(position) {
|
||||
if !mbb.contains(&position) {
|
||||
return Err(format!(
|
||||
"View port '{:?}' does not contain '{:?}'",
|
||||
mbb, position
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(self
|
||||
.find(position)
|
||||
.into_iter()
|
||||
.map(|fields| (position.clone(), fields))
|
||||
.collect())
|
||||
Ok(Box::new(
|
||||
self.find(&position)
|
||||
.map(move |fields| (position.clone(), fields)),
|
||||
))
|
||||
}
|
||||
Shape::BoundingBox(bl, bh) => {
|
||||
if let Some(mbb) = view_port {
|
||||
match mbb {
|
||||
Shape::BoundingBox(vl, vh) => {
|
||||
// Compute the intersection of the two boxes.
|
||||
let lower = bl.max(vl);
|
||||
let higher = bh.min(vh);
|
||||
let lower = (&bl).max(vl);
|
||||
let higher = (&bh).min(vh);
|
||||
if higher < lower {
|
||||
Err(format!(
|
||||
"View port '{:?}' does not intersect '{:?}'",
|
||||
mbb, shape
|
||||
mbb,
|
||||
Shape::BoundingBox(bl.clone(), bh.clone())
|
||||
))
|
||||
} else {
|
||||
trace!(
|
||||
"mbb {:?} shape {:?} lower {:?} higher {:?}",
|
||||
mbb,
|
||||
shape,
|
||||
Shape::BoundingBox(bl.clone(), bh.clone()),
|
||||
lower,
|
||||
higher
|
||||
);
|
||||
@@ -187,11 +192,11 @@ impl SpaceIndex {
|
||||
_ => Err(format!("Invalid view port shape '{:?}'", mbb)),
|
||||
}
|
||||
} else {
|
||||
Ok(self.find_range(bl, bh))
|
||||
Ok(self.find_range(&bl, &bh))
|
||||
}
|
||||
}
|
||||
Shape::HyperSphere(center, radius) => {
|
||||
let (bl, bh) = &shape.get_mbb();
|
||||
let (bl, bh) = Shape::HyperSphere(center.clone(), radius).get_mbb();
|
||||
let lower;
|
||||
let higher;
|
||||
|
||||
@@ -199,26 +204,24 @@ impl SpaceIndex {
|
||||
match mbb {
|
||||
Shape::BoundingBox(vl, vh) => {
|
||||
// Compute the intersection of the two boxes.
|
||||
lower = bl.max(vl);
|
||||
higher = bh.min(vh);
|
||||
lower = (&bl).max(vl);
|
||||
higher = (&bh).min(vh);
|
||||
}
|
||||
_ => return Err(format!("Invalid view port shape '{:?}'", mbb)),
|
||||
}
|
||||
} else {
|
||||
lower = bl;
|
||||
higher = bh;
|
||||
lower = &bl;
|
||||
higher = &bh;
|
||||
}
|
||||
|
||||
// Filter out results using using a range query over the MBB,
|
||||
// then add the condition of the radius as we are working within
|
||||
// a sphere.
|
||||
let results = self
|
||||
.find_range(&lower, &higher)
|
||||
.into_iter()
|
||||
.filter(|(position, _)| (position - center).norm() <= radius.f64())
|
||||
.collect();
|
||||
.find_range(lower, higher)
|
||||
.filter(move |(position, _)| (position - ¢er).norm() <= radius.f64());
|
||||
|
||||
Ok(results)
|
||||
Ok(Box::new(results))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,9 +68,8 @@ pub mod v1 {
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::database;
|
||||
use database::space;
|
||||
|
||||
use super::database;
|
||||
use super::space;
|
||||
use super::Point;
|
||||
use super::Properties;
|
||||
|
||||
@@ -87,7 +86,8 @@ pub mod v1 {
|
||||
/// Define a Shape, within a specific reference space.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Shape {
|
||||
/// Type of the shape, which is used to interpret the list of `vertices`.
|
||||
/// Type of the shape, which is used to interpret the list of
|
||||
/// `vertices`.
|
||||
#[serde(rename = "type")]
|
||||
pub type_name: String,
|
||||
|
||||
@@ -99,8 +99,8 @@ pub mod v1 {
|
||||
pub vertices: Vec<Point>,
|
||||
}
|
||||
|
||||
/// Convert a list of properties grouped by space id, then positions to a
|
||||
/// list of Spatial Objects for the rest API v1.
|
||||
/// Convert a list of properties grouped by space id, then positions
|
||||
/// to a list of Spatial Objects for the rest API v1.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
@@ -109,13 +109,14 @@ pub mod v1 {
|
||||
pub fn to_spatial_objects(
|
||||
list: Vec<(&String, Vec<(space::Position, &database::Properties)>)>,
|
||||
) -> Vec<SpatialObject> {
|
||||
// Filter per Properties, in order to regroup by it, then build a single SpatialObject per Properties.
|
||||
// Filter per Properties, in order to regroup by it, then build
|
||||
// a single SpatialObject per Properties.
|
||||
let mut hashmap = HashMap::new();
|
||||
for (space, v) in list {
|
||||
for (position, properties) in v {
|
||||
hashmap
|
||||
.entry(properties)
|
||||
.or_insert_with(|| vec![])
|
||||
.or_insert_with(Vec::new)
|
||||
.push((space, position));
|
||||
}
|
||||
}
|
||||
@@ -150,9 +151,8 @@ pub mod v2 {
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::database;
|
||||
use database::space;
|
||||
|
||||
use super::database;
|
||||
use super::space;
|
||||
use super::Point;
|
||||
use super::Properties;
|
||||
|
||||
@@ -208,46 +208,88 @@ pub mod v2 {
|
||||
HyperSpheres(Vec<(Point, f64)>),
|
||||
}
|
||||
|
||||
/// Convert a list of properties grouped by space id, then positions to a
|
||||
/// list of Spatial Objects for the rest API v2.
|
||||
/// Convert a list of space id grouped by properties, then positions
|
||||
/// to a list of Spatial Objects for the rest API v2.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `list`:
|
||||
/// A list of (`&Properties`, [ ( **Space Id**, [ *Spatial position* ] ) ]) tuples.
|
||||
#[allow(clippy::type_complexity)]
|
||||
// Type alias cannot be used as Traits, so we can't use the alias to add the lifetime specifications.
|
||||
pub fn from_spaces_by_properties<'o>(
|
||||
objects: Box<
|
||||
(dyn Iterator<
|
||||
Item=(
|
||||
&'o database::Properties,
|
||||
Vec<(&'o String, Box<dyn Iterator<Item=space::Position> + 'o>)>,
|
||||
),
|
||||
> + 'o),
|
||||
>,
|
||||
) -> impl Iterator<Item=SpatialObject> + 'o {
|
||||
objects.map(|(property, positions_by_spaces)| {
|
||||
let volumes = positions_by_spaces
|
||||
.into_iter()
|
||||
.map(|(space, positions)| {
|
||||
// We are not using vec![] as we now beforehand we
|
||||
// will have only one element in the vector, so we
|
||||
// optimise for space by allocating it as such.
|
||||
let shapes = vec![
|
||||
Shape::Points(positions.map(|position|
|
||||
position.into()).collect::<Vec<_>>())
|
||||
];
|
||||
|
||||
Volume {
|
||||
space: space.clone(),
|
||||
shapes,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
SpatialObject {
|
||||
properties: (&property).into(),
|
||||
volumes,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Convert a list of properties grouped by space id, then positions
|
||||
/// to a list of Spatial Objects for the rest API v2.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `list`:
|
||||
/// A list of (**Space Id**, [ ( *Spatial position*, `&Properties` ) ]) tuples.
|
||||
pub fn to_spatial_objects(
|
||||
list: Vec<(&String, Vec<(space::Position, &database::Properties)>)>,
|
||||
) -> Vec<SpatialObject> {
|
||||
// Filter per Properties, in order to regroup by it, then build a single SpatialObject per Properties.
|
||||
pub fn from_properties_by_spaces(
|
||||
objects: database::IterObjectsBySpaces<'_>,
|
||||
) -> impl Iterator<Item=SpatialObject> + '_ {
|
||||
// Filter per Properties, in order to regroup by it, then build
|
||||
// a single SpatialObject per Properties.
|
||||
let mut hashmap = HashMap::new();
|
||||
for (space, v) in list {
|
||||
for (space, v) in objects {
|
||||
for (position, properties) in v {
|
||||
hashmap
|
||||
.entry(properties)
|
||||
.or_insert_with(HashMap::new)
|
||||
.entry(space)
|
||||
.or_insert_with(|| vec![])
|
||||
.push(position.into());
|
||||
.or_insert_with(Vec::new)
|
||||
.push(position);
|
||||
}
|
||||
}
|
||||
|
||||
let mut results = vec![];
|
||||
for (properties, v) in hashmap.iter_mut() {
|
||||
let volumes = v
|
||||
.drain()
|
||||
.map(|(space, positions)| Volume {
|
||||
space: space.clone(),
|
||||
shapes: vec![Shape::Points(positions)],
|
||||
let results = Box::new(hashmap.into_iter().map(|(property, hm)| {
|
||||
let positions = hm
|
||||
.into_iter()
|
||||
.map(|(space, positions)| {
|
||||
let positions: database::IterPositions = Box::new(positions.into_iter());
|
||||
(space, positions)
|
||||
})
|
||||
.collect();
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
results.push(SpatialObject {
|
||||
properties: properties.into(),
|
||||
volumes,
|
||||
});
|
||||
}
|
||||
(property, positions)
|
||||
}));
|
||||
|
||||
results
|
||||
from_spaces_by_properties(results)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,6 +452,9 @@ pub fn build_index(
|
||||
}
|
||||
|
||||
properties.append(&mut properties_hm.drain().map(|(_, v)| v).collect::<Vec<_>>());
|
||||
|
||||
// We we use sort_by_key, we get borrow checker errors.
|
||||
#[allow(clippy::unnecessary_sort_by)]
|
||||
properties.sort_unstable_by(|a, b| a.id().cmp(b.id()));
|
||||
|
||||
space_set_objects.iter_mut().for_each(|object| {
|
||||
|
||||
@@ -181,7 +181,7 @@ fn convert(string: &str) -> Result<Vec<SpatialObject>, Error> {
|
||||
let (x, y, z) = (x * 0.039_062_5, y * 0.039_062_5, z * 0.039_062_5);
|
||||
|
||||
oids.entry(oid)
|
||||
.or_insert_with(|| vec![])
|
||||
.or_insert_with(Vec::new)
|
||||
.push(vec![x, y, z]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user