Skip to content

Commit

Permalink
Refactors the ray tracing code to remove exceptions and make it more
Browse files Browse the repository at this point in the history
efficient. Fixes compilation on win64. Tests are still failing for
win32.
  • Loading branch information
fakufaku committed Jan 16, 2019
1 parent cc030d6 commit bfc18d5
Show file tree
Hide file tree
Showing 15 changed files with 426 additions and 2,205 deletions.
1,321 changes: 59 additions & 1,262 deletions examples/raytracing.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/room_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
shoebox.fs)
)

import pdb; pdb.set_trace()

# run ism
shoebox.simulate()

Expand Down
55 changes: 19 additions & 36 deletions notebooks/rir_demo.ipynb

Large diffs are not rendered by default.

71 changes: 19 additions & 52 deletions pyroomacoustics/libroom_src/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@

#include <iostream>
#include <cmath>
#include "utility.hpp"
#include "geometry.hpp"


double clamp(double value, double min, double max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}


int ccw3p(const Eigen::Vector2f &p1, const Eigen::Vector2f &p2, const Eigen::Vector2f &p3)
{
/*
Expand Down Expand Up @@ -307,7 +317,7 @@ int is_inside_2d_polygon(const Eigen::Vector2f &p,
}


float area_2d_polygon(const Eigen::MatrixXf &corners)
float area_2d_polygon(const Eigen::Matrix<float, 2, Eigen::Dynamic> &corners)
{
/*
Computes the signed area of a 2D surface represented by its corners.
Expand All @@ -318,18 +328,6 @@ float area_2d_polygon(const Eigen::MatrixXf &corners)
positive area means anti-clockwise ordered corners.
negative area means clockwise ordered corners.
*/
if (corners.rows() != 2)
{
std::cerr << "Only 2D polygons are supported" << std::endl;
throw std::exception();
}

if (corners.cols() < 3)
{
std::cerr << "The corners should have more than 2 points" << std::endl;
throw std::exception();
}

float a = 0;
for (int c1 = 0 ; c1 < corners.cols() ; c1++)
{
Expand All @@ -342,8 +340,9 @@ float area_2d_polygon(const Eigen::MatrixXf &corners)
}


float cos_angle_between(const Eigen::VectorXf & v1,
const Eigen::VectorXf & v2)
float cos_angle_between(
const Eigen::VectorXf &v1,
const Eigen::VectorXf &v2)
{

/* This function computes the cosinus of the angle between two vectors.
Expand All @@ -354,24 +353,14 @@ float cos_angle_between(const Eigen::VectorXf & v1,
:returns: a value in [-1;1] representing the cosinus of the angle
between the two vectors*/

int s1 = v1.size();
int s2 = v2.size();

if (s1 < 2 or s1 > 3 or s2 < 2 or s2 > 3 or s1 != s2)
{
std::cerr << "size of v1 :" << s1 << std::endl;
std::cerr << "size of v2 :" << s2 << std::endl;
std::cerr << "cos_angle_between() : Only 2D and 3D vectors are supported" << std::endl;
throw std::exception();
}

return clamp(v1.normalized().dot(v2.normalized()), -1., 1.);
}


float dist_line_point(const Eigen::VectorXf & start,
const Eigen::VectorXf & end,
const Eigen::VectorXf & point)
float dist_line_point(
const Eigen::VectorXf &start,
const Eigen::VectorXf &end,
const Eigen::VectorXf &point)
{

/* This function computes the smallest distance between a point and an
Expand All @@ -384,28 +373,6 @@ float dist_line_point(const Eigen::VectorXf & start,
:returns: the smallest distance between 'point' and the line defined
by 'start' and 'end'*/

size_t s_start = start.size();
size_t s_end = end.size();
size_t s_point = point.size();

if (s_start < 2 or s_start > 3 or s_end < 2 or s_end > 3 or s_point < 2 or s_point > 3)
{
std::cerr << "dist_line_point : Only 2D and 3D vectors are supported" << std::endl;
std::cerr << "Dim start = " << s_start << std::endl;
std::cerr << "Dim end = " << s_end << std::endl;
std::cerr << "Dim point = " << s_point << std::endl;
throw std::exception();
}

if (s_start != s_point or s_start != s_end or s_end != s_point)
{
std::cerr << "The 3 vectors objects must have the same dimension !" << std::endl;
std::cerr << "Dim start = " << s_start << std::endl;
std::cerr << "Dim end = " << s_end << std::endl;
std::cerr << "Dim point = " << s_point << std::endl;
throw std::exception();
}

Eigen::VectorXf unit_vec = (end - start).normalized(); // vector
Eigen::VectorXf v = point - start; // vector

Expand Down
2 changes: 1 addition & 1 deletion pyroomacoustics/libroom_src/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Eigen::Vector3f cross(Eigen::Vector3f v1, Eigen::Vector3f v2);
int is_inside_2d_polygon(const Eigen::Vector2f &p,
const Eigen::Matrix<float,2,Eigen::Dynamic> &corners);

float area_2d_polygon(const Eigen::MatrixXf &);
float area_2d_polygon(const Eigen::Matrix<float, 2, Eigen::Dynamic> &corners);

float cos_angle_between(const Eigen::VectorXf & v1,
const Eigen::VectorXf & v2);
Expand Down
22 changes: 2 additions & 20 deletions pyroomacoustics/libroom_src/libroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <Eigen/Dense>

#include "geometry.hpp"
#include "utility.hpp"
#include "wall.hpp"
#include "room.hpp"

Expand Down Expand Up @@ -150,6 +149,7 @@ PYBIND11_MODULE(libroom, m) {
.def("intersects", &Wall<3>::intersects)
.def("side", &Wall<3>::side)
.def("reflect", &Wall<3>::reflect)
.def("normal_reflect", &Wall<3>::normal_reflect)
.def("same_as", &Wall<3>::same_as)
.def_readonly("dim", &Wall<3>::dim)
.def_readwrite("absorption", &Wall<3>::absorption)
Expand Down Expand Up @@ -179,6 +179,7 @@ PYBIND11_MODULE(libroom, m) {
.def("intersects", &Wall<2>::intersects)
.def("side", &Wall<2>::side)
.def("reflect", &Wall<2>::reflect)
.def("normal_reflect", &Wall<2>::normal_reflect)
.def("same_as", &Wall<2>::same_as)
.def_readonly("dim", &Wall<2>::dim)
.def_readwrite("absorption", &Wall<2>::absorption)
Expand Down Expand Up @@ -229,24 +230,5 @@ PYBIND11_MODULE(libroom, m) {
m.def("dist_line_point", &dist_line_point,
"Computes the distance between a point and an infinite line");


// Routines for the utility packages
m.def("equation", &equation,
"Computes the a and b coefficients in the expression y=ax+b given two points lying on that line.");

m.def("compute_segment_end", &compute_segment_end,
"Computes the end point of a segment given the start point, the length, and the orientation");

m.def("compute_reflected_end", &compute_reflected_end,
"This function operates when we know the vector [start, hit_point]. This function computes the end point E so that [hit_point, E] is the reflected vector of [start, hit_point] with the correct magnitude");

m.def("intersects_mic", &intersects_mic,
"Determines if a segment intersects the microphone of specified center and radius");

m.def("solve_quad", &solve_quad,
"Solves the quadratic system and outputs real roots");

m.def("mic_intersection", &mic_intersection,
"Computes the intersection point between the ray and the microphone");
}

0 comments on commit bfc18d5

Please sign in to comment.