Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PointCulling as trait #273

Closed
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tempdir = "0.3.7"

[workspace]
members = [
"point_cloud_client",
"point_viewer_grpc",
"point_viewer_grpc_proto_rust",
"point_viewer_proto_rust",
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DEFAULT_GOAL := all

.PHONY: all
all:
yarn build_all
cargo build --release --all
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"private": true,
"workspaces": [
"octree_web_viewer/client",
"xray/client"
],
"scripts": {
"build_all": "yarn install --silent --ignore-engines && yarn wsrun build",
"test": "yarn wsrun --exclude-missing test"
},
"devDependencies": {
"typescript": "^3.3.3",
"wsrun": "^3.3.3"
}
}
17 changes: 17 additions & 0 deletions point_cloud_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "point_cloud_client"
version = "0.1.0"
edition = "2018"

[[bin]]
name = "point_cloud_client_test"
path = "src/bin/test.rs"

[dependencies]
cgmath = "0.16.0"
collision = "0.18.0"
fnv = "1.0.6"
point_viewer = { path = ".." }
point_viewer_grpc = { path = "../point_viewer_grpc" }
protobuf = "2.0.0"
structopt = "0.2"
88 changes: 88 additions & 0 deletions point_cloud_client/src/bin/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// This removes clippy warnings regarding redundant StructOpt.
#![allow(clippy::redundant_closure)]

use cgmath::Point3;
use collision::Aabb3;
use point_cloud_client::PointCloudClient;
use point_viewer::errors::*;
use point_viewer::octree::{OctreeFactory, PointCulling, PointLocation};
use point_viewer::PointData;
use structopt::StructOpt;

fn point3f64_from_str(s: &str) -> std::result::Result<Point3<f64>, &'static str> {
let coords: std::result::Result<Vec<f64>, &'static str> = s
.split(|c| c == ' ' || c == ',' || c == ';')
.map(|s| s.parse::<f64>().map_err(|_| "Could not parse point."))
.collect();
let coords = coords?;
if coords.len() != 3 {
return Err("Wrong number of coordinates.");
}
Ok(Point3::new(coords[0], coords[1], coords[2]))
}

#[derive(StructOpt)]
#[structopt(about = "Simple point_cloud_client test.")]
struct CommandlineArguments {
/// The locations containing the octree data.
#[structopt(parse(from_str), raw(required = "true"))]
locations: Vec<String>,

/// The minimum value of the bounding box to be queried.
#[structopt(
long = "min",
default_value = "-500.0 -500.0 -500.0",
parse(try_from_str = "point3f64_from_str")
)]
min: Point3<f64>,

/// The maximum value of the bounding box to be queried.
#[structopt(
long = "max",
default_value = "500.0 500.0 500.0",
parse(try_from_str = "point3f64_from_str")
)]
max: Point3<f64>,

/// The maximum number of points to return.
#[structopt(long = "num-points", default_value = "50000000")]
num_points: usize,
}

fn main() {
let args = CommandlineArguments::from_args();
let num_points = args.num_points;
let point_cloud_client = PointCloudClient::new(&args.locations, OctreeFactory::new())
.expect("Couldn't create octree client.");
let point_location = PointLocation {
culling: PointCulling::Aabb(Aabb3::new(args.min, args.max)),
global_from_local: None,
};
let mut point_count: usize = 0;
let mut print_count: usize = 1;
let callback_func = |point_data: PointData| -> Result<()> {
point_count += point_data.position.len();
if point_count >= print_count * 1_000_000 {
print_count += 1;
println!("Streamed {}M points", point_count / 1_000_000);
}
if point_count >= num_points {
return Err(std::io::Error::new(
std::io::ErrorKind::Interrupted,
format!("Maximum number of {} points reached.", num_points),
)
.into());
}
Ok(())
};
match point_cloud_client.for_each_point_data(&point_location, callback_func) {
Ok(_) => (),
Err(e) => match e.kind() {
ErrorKind::Io(ref e) if e.kind() == std::io::ErrorKind::Interrupted => (),
_ => {
println!("Encountered error:\n{}", e);
std::process::exit(1);
}
},
}
}
39 changes: 39 additions & 0 deletions point_cloud_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use point_viewer::errors::*;
use point_viewer::octree::{
BatchIterator, Octree, OctreeFactory, PointLocation, NUM_POINTS_PER_BATCH,
};
use point_viewer::PointData;

pub struct PointCloudClient {
octrees: Vec<Octree>,
pub num_points_per_batch: usize,
}

impl PointCloudClient {
pub fn new(locations: &[String], octree_factory: OctreeFactory) -> Result<Self> {
let octrees: Result<Vec<Octree>> = locations
.iter()
.map(|location| {
octree_factory
.generate_octree(location)
.map(|octree| *octree)
})
.collect();
octrees.map(|octrees| PointCloudClient {
octrees,
num_points_per_batch: NUM_POINTS_PER_BATCH,
})
}

pub fn for_each_point_data<F>(&self, point_location: &PointLocation, mut func: F) -> Result<()>
where
F: FnMut(PointData) -> Result<()>,
{
for octree in &self.octrees {
let mut batch_iterator =
BatchIterator::new(octree, point_location, self.num_points_per_batch);
batch_iterator.try_for_each_batch(&mut func)?;
}
Ok(())
}
}
84 changes: 53 additions & 31 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub struct Cube {
edge_length: f64,
}

pub trait PointCulling<S: BaseFloat> {
fn contains(&self, point: &Point3<S>) -> bool;
fn intersects(&self, aabb: &Aabb3<S>) -> bool;
fn transform(&self, isometry: Isometry3<S>) -> Self;
}

impl Cube {
pub fn bounding(aabb: &Aabb3<f64>) -> Self {
let edge_length = (aabb.max().x - aabb.min().x)
Expand Down Expand Up @@ -73,6 +79,20 @@ impl Cube {
}
}

// impl PointCulling<f64> for Cube {
// fn intersects(&self, aabb: &Aabb3<f64>) -> bool {
// let aabb_cube = self.to_aabb3();
// aabb_cube.intersects(aabb)
// }

// fn contains(&self, p: &Point3<f64>) -> bool {
// let aabb_cube = self.to_aabb3();
// aabb_cube.contains(p);
// }

// fn transform(&self, Isometry3<S>)
// }

// This guards against the separating axes being NaN, which may happen when the orientation aligns with the unit axes.
fn is_finite<S: BaseFloat>(vec: &Vector3<S>) -> bool {
vec.x.is_finite() && vec.y.is_finite() && vec.z.is_finite()
Expand Down Expand Up @@ -192,22 +212,6 @@ impl<S: BaseFloat> Obb<S> {
}
}

pub fn intersects(&self, aabb: &Aabb3<S>) -> bool {
intersects(&self.corners, &self.separating_axes, aabb)
}

pub fn contains(&self, p: &Point3<S>) -> bool {
let Point3 { x, y, z } =
self.isometry_inv.rotation.rotate_point(*p) + self.isometry_inv.translation;
x.abs() <= self.half_extent.x
&& y.abs() <= self.half_extent.y
&& z.abs() <= self.half_extent.z
}

pub fn transform(&self, isometry: &Isometry3<S>) -> Self {
Self::new(isometry * &self.isometry_inv.inverse(), self.half_extent)
}

fn precompute_corners(isometry: &Isometry3<S>, half_extent: &Vector3<S>) -> [Point3<S>; 8] {
let corner_from = |x: S, y: S, z: S| {
isometry.rotation.rotate_point(Point3::new(x, y, z)) + isometry.translation
Expand Down Expand Up @@ -259,6 +263,22 @@ impl<S: BaseFloat> Obb<S> {
}
}

impl<S: BaseFloat> PointCulling<S> for Obb<S> {
fn intersects(&self, aabb: &Aabb3<S>) -> bool {
intersects(&self.corners, &self.separating_axes, aabb)
}

fn contains(&self, p: &Point3<S>) -> bool {
let Point3 { x, y, z } =
self.isometry_inv.rotation.rotate_point(*p) + self.isometry_inv.translation;
&&y.abs() <= self.half_extent.y && z.abs() <= self.half_extent.z
}

pub fn transform(&self, isometry: &Isometry3<S>) -> Self {
Self::new(isometry * &self.isometry_inv.inverse(), self.half_extent)
}
}

#[derive(Debug, Clone)]
pub struct OrientedBeam {
// The members here are an implementation detail and differ from the
Expand All @@ -280,21 +300,6 @@ impl OrientedBeam {
}
}

pub fn intersects(&self, aabb: &Aabb3<f64>) -> bool {
intersects(&self.corners, &self.separating_axes, aabb)
}

pub fn contains(&self, p: &Point3<f64>) -> bool {
// What is the point in beam coordinates?
let Point3 { x, y, .. } =
self.isometry_inv.rotation.rotate_point(*p) + self.isometry_inv.translation;
x.abs() <= self.half_extent.x && y.abs() <= self.half_extent.y
}

pub fn transform(&self, isometry: &Isometry3<f64>) -> Self {
Self::new(isometry * &self.isometry_inv.inverse(), self.half_extent)
}

fn precompute_corners(
isometry: &Isometry3<f64>,
half_extent: &Vector2<f64>,
Expand Down Expand Up @@ -332,6 +337,23 @@ impl OrientedBeam {
}
}

impl PointCulling<f64> for OrientedBeam<f64> {
fn intersects(&self, aabb: &Aabb3<f64>) -> bool {
intersects(&self.corners, &self.separating_axes, aabb)
}

fn contains(&self, p: &Point3<f64>) -> bool {
// What is the point in beam coordinates?
let Point3 { x, y, .. } =
self.isometry_inv.rotation.rotate_point(*p) + self.isometry_inv.translation;
x.abs() <= self.half_extent.x && y.abs() <= self.half_extent.y
}

fn transform(&self, isometry: &Isometry3<f64>) -> Self {
Self::new(isometry * &self.isometry_inv.inverse(), self.half_extent)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading