commit 12b9729956fe5f071988397df27cb013202c8f76
parent bbcacfaf837095f64d58597779568c6fd7878c79
Author: Egor Achkasov <eaachkasov@edu.hse.ru>
Date: Tue, 10 Dec 2024 23:04:16 +0100
Remove RT hits debuging; Make gains complex
Diffstat:
5 files changed, 88 insertions(+), 70 deletions(-)
diff --git a/compute_paths.c b/compute_paths.c
@@ -248,18 +248,19 @@ void moeller_trumbore(Ray *ray, Mesh *mesh, float *t, int32_t *ind)
/* ==== MAIN FUNCTION ==== */
void compute_paths(
- IN const char *mesh_filepath, /* path to the mesh file */
+ IN const char *mesh_filepath, /* path to the mesh file */
IN const float *rx_positions, /* shape (num_rx, 3) */
IN const float *tx_positions, /* shape (num_tx, 3) */
IN const float *rx_velocities, /* shape (num_rx, 3) */
IN const float *tx_velocities, /* shape (num_tx, 3) */
IN float carrier_frequency, /* > 0.0 */
- IN size_t num_rx, /* number of receivers */
- IN size_t num_tx, /* number of transmitters */
- IN size_t num_paths, /* number of paths */
- IN size_t num_bounces, /* number of bounces */
- OUT float *a, /* output array of gains (num_paths,) */
- OUT float *tau /* output array of delays (num_paths,) */
+ IN size_t num_rx, /* number of receivers */
+ IN size_t num_tx, /* number of transmitters */
+ IN size_t num_paths, /* number of paths */
+ IN size_t num_bounces, /* number of bounces */
+ OUT float *a_re, /* output array real parts of gains (num_tx, num_paths) */
+ OUT float *a_im, /* output array imaginary parts of gains (num_tx, num_paths) */
+ OUT float *tau /* output array of delays (num_tx, num_paths) */
)
{
/* Load the scene */
@@ -297,65 +298,45 @@ void compute_paths(
- a???
*/
/* TODO calculate a and tau in moeller_trumbore */
- /* shape (num_bounces, num_tx, num_paths) */
- Vec3 *hits = (Vec3*)malloc(num_bounces * num_tx * num_paths * sizeof(Vec3));
- int32_t *hit_indices = (int32_t*)malloc(num_bounces * num_tx * num_paths * sizeof(int32_t));
- tau = (float*)memset(tau, 0, num_paths * sizeof(float));
+ /* shape (num_tx, num_paths) */
+ Vec3 *hits = (Vec3*)malloc(num_tx * num_paths * sizeof(Vec3));
+ int32_t *hit_indices = (int32_t*)malloc(num_tx * num_paths * sizeof(int32_t));
+ tau = (float*)memset(tau, 0, num_tx * num_paths * sizeof(float));
float t;
int32_t ind;
Ray *r;
Vec3 *h;
- for (size_t i = 0; i < num_bounces; ++i)
+ size_t off;
+ for (size_t i = 0; i < num_bounces; ++i) {
+ off = 0;
for (size_t j = 0; j < num_tx; ++j)
for (size_t k = 0; k < num_paths; ++k) {
t = -1.;
ind = -1;
- r = &rays[j * num_paths + k];
+ r = &rays[off];
moeller_trumbore(r, &mesh, &t, &ind);
- tau[k] += t;
- hit_indices[i * num_tx * num_paths + j * num_paths + k] = ind;
- h = &hits[i * num_tx * num_paths + j * num_paths + k];
+ tau[off] += t;
+ hit_indices[off] = ind;
+ h = &hits[off];
*h = vec3_scale(&r->d, t);
*h = vec3_add(h, &r->o);
r->o = *h;
+ off += 1;
}
+ }
/* Calculate tau */
for (size_t i = 0; i < num_paths; ++i)
tau[i] /= SPEED_OF_LIGHT;
- /* TODO Remove */
- for (size_t i = 0; i < num_bounces; ++i)
- for (size_t j = 0; j < num_rx; ++j)
- for (size_t k = 0; k < num_paths; ++k) {
- h = &hits[i * num_tx * num_paths + j * num_paths + k];
- a[i * num_rx * num_paths * 3 + j * num_paths * 3 + k * 3] = h->x;
- a[i * num_rx * num_paths * 3 + j * num_paths * 3 + k * 3 + 1] = h->y;
- a[i * num_rx * num_paths * 3 + j * num_paths * 3 + k * 3 + 2] = h->z;
- }
-
/* Free */
- /* Free the hits */
- /*
- for (size_t i = 0; i < num_bounces; ++i) {
- for (size_t j = 0; j < num_tx; ++j) {
- free(hits[i][j]);
- free(hit_indices[i][j]);
- }
- free(hits[i]);
- free(hit_indices[i]);
- }
free(hits);
free(hit_indices);
- */
- /* Free the rays */
- // for (size_t i = 0; i < num_tx; ++i)
- // free(rays[i]);
- // free(rays);
- // /* Free the mesh */
- // free(mesh.vertices);
- // free(mesh.rms);
- // free(mesh.indices);
- // free(mesh.rm_indices);
- // free(mesh.normals);
+ free(rays);
+ /* Free the mesh */
+ free(mesh.vertices);
+ free(mesh.rms);
+ free(mesh.indices);
+ free(mesh.rm_indices);
+ free(mesh.normals);
}
diff --git a/compute_paths.h b/compute_paths.h
@@ -22,8 +22,9 @@
* \param num_rx number of receivers. Must be > 0
* \param num_tx number of transmitters. Must be > 0
* \param num_paths number of paths to compute. Must be > 0
- * \param a output array of gains, shape (num_paths,)
- * \param tau output array of delays in seconds, shape (num_paths,)
+ * \param a_re output array of real parts of gains, shape (num_tx, num_paths)
+ * \param a_im output array of imaginary parts of gains, shape (num_tx, num_paths)
+ * \param tau output array of delays in seconds, shape (num_tx, num_paths)
*/
void compute_paths(
IN const char *mesh_filepath, /* path to the mesh file */
@@ -36,8 +37,9 @@ void compute_paths(
IN size_t num_tx, /* number of transmitters */
IN size_t num_paths, /* number of paths */
IN size_t num_bounces, /* number of bounces */
- OUT float *a, /* output array of gains (num_paths,) */
- OUT float *tau /* output array of delays in seconds (num_paths,) */
+ OUT float *a_re, /* output array real parts of gains (num_tx, num_paths) */
+ OUT float *a_im, /* output array imaginary parts of gains (num_tx, num_paths) */
+ OUT float *tau /* output array of delays (num_tx, num_paths) */
);
#endif /* COMPUTE_PATHS_H */
diff --git a/compute_paths_pybind11.cpp b/compute_paths_pybind11.cpp
@@ -11,7 +11,7 @@ extern "C" {
namespace py = pybind11;
-std::tuple< py::array_t<float>, py::array_t<float> >
+std::tuple< py::array_t<float>, py::array_t<float>, py::array_t<float> >
compute_paths_wrapper(
const std::string &mesh_filepath,
py::array_t<float> rx_positions,
@@ -31,38 +31,41 @@ compute_paths_wrapper(
py::buffer_info tx_vel_info = tx_velocities.request();
// Output arrays
- // float *a_im = new float[num_paths];
- // float *a_re = new float[num_paths];
- float *a = new float[num_bounces * num_tx * num_paths * 3];
- float *tau = new float[num_paths];
+ float *a_im = new float[num_tx * num_paths];
+ float *a_re = new float[num_tx * num_paths];
+ float *tau = new float[num_tx * num_paths];
// Call the C function
compute_paths(
mesh_filepath.c_str(),
- (float*)rx_pos_info.ptr, // Rx positions
- (float*)tx_pos_info.ptr, // Tx positions
- (float*)rx_vel_info.ptr, // Rx velocities
- (float*)tx_vel_info.ptr, // Tx velocities
+ (const float*)rx_pos_info.ptr, // Rx positions
+ (const float*)tx_pos_info.ptr, // Tx positions
+ (const float*)rx_vel_info.ptr, // Rx velocities
+ (const float*)tx_vel_info.ptr, // Tx velocities
carrier_frequency,
(size_t)num_rx,
(size_t)num_tx,
(size_t)num_paths,
(size_t)num_bounces,
- a,
+ a_re,
+ a_im,
tau
);
// Convert output arrays into numpy arrays for easy use in Python
- //py::array_t<float> a_array = py::array_t<float>(num_paths, a);
- py::array_t<float> a_array = py::array_t<float>({num_bounces, num_tx, num_paths, 3}, a);
- py::array_t<float> tau_array = py::array_t<float>(num_paths, tau);
+ // TODO prevent copying data
+ // TODO construct np.complex64 array for a
+ py::array_t<float> a_im_array = py::array_t<float>({num_tx, num_paths}, a_im);
+ py::array_t<float> a_re_array = py::array_t<float>({num_tx, num_paths}, a_re);
+ py::array_t<float> tau_array = py::array_t<float>({num_tx, num_paths}, tau);
// Deallocate arrays
- // delete[] a;
- // delete[] tau;
+ delete[] a_im;
+ delete[] a_re;
+ delete[] tau;
// Return the results as a tuple (gains, delays)
- return std::make_tuple(a_array, tau_array);
+ return std::make_tuple(a_re_array, a_im_array, tau_array);
}
PYBIND11_MODULE(rt, m) {
diff --git a/test.c b/test.c
@@ -19,8 +19,9 @@ int main(int argc, char **argv)
size_t num_tx = 1;
size_t num_paths = 10000;
size_t num_bounces = 3;
- float *a = (float*)malloc(num_bounces * num_tx * num_paths * 3 * sizeof(float));
- float *tau = (float*)malloc(num_paths * sizeof(float));
+ float *a_re = (float*)malloc(num_tx * num_paths * sizeof(float));
+ float *a_im = (float*)malloc(num_tx * num_paths * sizeof(float));
+ float *tau = (float*)malloc(num_tx * num_paths * sizeof(float));
compute_paths(
argv[1],
rx_positions,
@@ -29,5 +30,5 @@ int main(int argc, char **argv)
tx_velocities,
carrier_frequency,
num_rx, num_tx, num_paths, num_bounces,
- a, tau);
+ a_re, a_im, tau);
}
diff --git a/test.py b/test.py
@@ -0,0 +1,31 @@
+import numpy as np
+from rt import compute_paths
+
+# Define inputs
+mesh_filepath = __file__[:__file__.rfind('/') + 1] + 'scenes/box.ply'
+rx_positions = np.array([[0., 0., 2.5]], dtype=np.float32)
+tx_positions = np.array([[0., 0., 2.5]], dtype=np.float32)
+rx_velocities = np.array([[0., 0., 0.]], dtype=np.float32)
+tx_velocities = np.array([[0., 0., 0.]], dtype=np.float32)
+carrier_frequency = 2.4e9
+num_rx = 1
+num_tx = 1
+num_paths = 10000
+num_bounces = 3
+
+# Call compute_paths
+a_im, a_re, tau = compute_paths(
+ mesh_filepath,
+ rx_positions,
+ tx_positions,
+ rx_velocities,
+ tx_velocities,
+ carrier_frequency,
+ num_rx,
+ num_tx,
+ num_paths,
+ num_bounces
+)
+
+print("Gains:", a_re + 1.j*a_im)
+print("Delays:", tau)