Local preservation snapshot, not a new deployment or simulation acceptance.
80 lines
3.6 KiB
C++
80 lines
3.6 KiB
C++
// Exact close-pair hazard and connectedness test; same predicates as NumPy.
|
|
// C ABI keeps this numerical hot loop independent of ROS and Python versions.
|
|
#include <cmath>
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <vector>
|
|
template<class T> int connected_grade(const T* p, int n) {
|
|
if (n <= 0) return 0;
|
|
std::vector<int> parent(n);
|
|
for(int i=0;i<n;i++) parent[i]=i;
|
|
auto root=[&](int i) { while(parent[i]!=i) {parent[i]=parent[parent[i]];i=parent[i];} return i; };
|
|
for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) {
|
|
const T dx=p[3*i]-p[3*j], dy=p[3*i+1]-p[3*j+1];
|
|
const T distance=std::sqrt(dx*dx+dy*dy);
|
|
if(distance<=T(0.12)) {
|
|
if(std::abs(p[3*i+2]-p[3*j+2])>T(0.1001)) return 0;
|
|
parent[root(i)]=root(j);
|
|
}
|
|
}
|
|
const int first=root(0);
|
|
for(int i=1;i<n;i++) if(root(i)!=first) return 0;
|
|
return 1;
|
|
}
|
|
extern "C" int terrain_connected_f32(const float* p,int n) {return connected_grade(p,n);}
|
|
extern "C" int terrain_connected_f64(const double* p,int n) {return connected_grade(p,n);}
|
|
|
|
// Exact counterpart of terrain_costs._neighborhoods. Return source indices in
|
|
// source order; Python retains the unchanged fitting and hazard predicates.
|
|
template<class T> struct Neighborhoods {
|
|
std::vector<T> points;
|
|
std::map<std::pair<int64_t,int64_t>,std::vector<int>> cells;
|
|
Neighborhoods(const T* p,int n): points(p,p+3*n) {
|
|
for(int i=0;i<n;i++) cells[key(i)].push_back(i);
|
|
}
|
|
std::pair<int64_t,int64_t> key(int i) const {
|
|
return {int64_t(std::floor(points[3*i]/T(0.4))),
|
|
int64_t(std::floor(points[3*i+1]/T(0.4)))};
|
|
}
|
|
int query(int i,int* output) const {
|
|
const auto k=key(i);
|
|
int count=0;
|
|
for(int dx=-1;dx<=1;dx++) for(int dy=-1;dy<=1;dy++) {
|
|
const auto found=cells.find({k.first+dx,k.second+dy});
|
|
if(found==cells.end()) continue;
|
|
for(int j:found->second) {
|
|
const T x=points[3*j]-points[3*i], y=points[3*j+1]-points[3*i+1];
|
|
if(std::sqrt(x*x+y*y)<=T(0.4)) output[count++]=j;
|
|
}
|
|
}
|
|
std::sort(output,output+count);
|
|
return count;
|
|
}
|
|
// Batch the existing Python count/span rejection before allocating a
|
|
// NumPy neighborhood per point. Relative subtraction and span retain T's
|
|
// rounding, exactly as terrain[index-neighbors] - terrain[index] does.
|
|
int screen(const int* indices,int n,int minimum,double maximum_span,int* output) const {
|
|
std::vector<int> nearby(points.size()/3);
|
|
int kept=0;
|
|
for(int k=0;k<n;k++) {
|
|
const int i=indices[k], count=query(i,nearby.data());
|
|
if(count<minimum) continue;
|
|
T low=points[3*nearby[0]+2]-points[3*i+2], high=low;
|
|
for(int j=1;j<count;j++) {
|
|
const T z=points[3*nearby[j]+2]-points[3*i+2];
|
|
low=std::min(low,z); high=std::max(high,z);
|
|
}
|
|
if(double(T(high-low))<=maximum_span) output[kept++]=i;
|
|
}
|
|
return kept;
|
|
}
|
|
};
|
|
#define NEIGHBOR_API(T,S) \
|
|
extern "C" void* terrain_neighbors_create_##S(const T* p,int n) {return new Neighborhoods<T>(p,n);} \
|
|
extern "C" int terrain_neighbors_query_##S(void* h,int i,int* out) {return static_cast<Neighborhoods<T>*>(h)->query(i,out);} \
|
|
extern "C" int terrain_neighbors_screen_##S(void* h,const int* i,int n,int minimum,double span,int* out) {return static_cast<Neighborhoods<T>*>(h)->screen(i,n,minimum,span,out);} \
|
|
extern "C" void terrain_neighbors_destroy_##S(void* h) {delete static_cast<Neighborhoods<T>*>(h);}
|
|
NEIGHBOR_API(float,f32)
|
|
NEIGHBOR_API(double,f64)
|