// 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 #include #include #include #include template int connected_grade(const T* p, int n) { if (n <= 0) return 0; std::vector parent(n); for(int i=0;iT(0.1001)) return 0; parent[root(i)]=root(j); } } const int first=root(0); for(int i=1;i struct Neighborhoods { std::vector points; std::map,std::vector> cells; Neighborhoods(const T* p,int n): points(p,p+3*n) { for(int i=0;i 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 nearby(points.size()/3); int kept=0; for(int k=0;k(p,n);} \ extern "C" int terrain_neighbors_query_##S(void* h,int i,int* out) {return static_cast*>(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*>(h)->screen(i,n,minimum,span,out);} \ extern "C" void terrain_neighbors_destroy_##S(void* h) {delete static_cast*>(h);} NEIGHBOR_API(float,f32) NEIGHBOR_API(double,f64)