distancing.cu 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of NVIDIA CORPORATION nor the names of its
  12. * contributors may be used to endorse or promote products derived
  13. * from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <pybind11/stl.h>
  28. #include <thread>
  29. #include <vector>
  30. #include <cmath>
  31. using namespace std;
  32. namespace py = pybind11;
  33. typedef tuple<float, float, float> centroid;
  34. float compute_dist(centroid& p1, centroid& p2)
  35. {
  36. float x1, y1, h1, x2, y2, h2;
  37. std::tie(x1, y1, h1) = p1;
  38. std::tie(x2, y2, h2) = p2;
  39. float dx = x2 - x1;
  40. float dy = y2 - y1;
  41. float lx = dx * 170 * (1/h1 + 1/h2) / 2;
  42. float ly = dy * 170 * (1/h1 + 1/h2) / 2;
  43. float l = sqrt(lx*lx + ly*ly);
  44. return l;
  45. }
  46. float compute_min_dist(int p, centroid& point, vector<centroid>& points)
  47. {
  48. vector<float> distances;
  49. for (auto & p2 : points) {
  50. distances.push_back(compute_dist(point, p2));
  51. }
  52. distances[p] = 1000000.0;
  53. float min_dist = *std::min_element(distances.begin(), distances.end());
  54. return min_dist;
  55. }
  56. vector<float> get_min_distances(vector<centroid>& points)
  57. {
  58. vector<float> out;
  59. for (int p = 0; p < points.size(); p++) {
  60. float min_dist = compute_min_dist(p, points[p], points);
  61. out.push_back(min_dist);
  62. }
  63. return out;
  64. }
  65. PYBIND11_MODULE(distancing, m) {
  66. m.def("get_min_distances", &get_min_distances, "Get min distances");
  67. }