kdtree
#include <iostream>
#include <vector>
#include <algorithm>
struct Point {
int id;
double x, y, z;
};
struct KDNode {
Point point;
KDNode* left;
KDNode* right;
int axis;
KDNode(const Point& p) : point(p), left(nullptr), right(nullptr), axis(0) {}
};
class KDTree {
private:
KDNode* root;
// 选择切割轴
int chooseAxis(int depth) {
return depth % 3; // 循环使用x, y, z轴
}
// 找到中位数索引
int findMedian(std::vector<Point>& points, int axis) {
std::sort(points.begin(), points.end(), [axis](const Point& a, const Point& b) {
switch (axis) {
case 0: return a.x < b.x;
case 1: return a.y < b.y;
case 2: return a.z < b.z;
}
return false;
});
return points.size() / 2;
}
// 递归构建K-D树
KDNode* build(std::vector<Point>& points, int depth) {
if (points.empty()) return nullptr;
int axis = chooseAxis(depth);
int medianIndex = findMedian(points, axis);
Point pivot = points[medianIndex];
std::vector<Point> leftSubTree, rightSubTree;
for (size_t i = 0; i < points.size(); ++i) {
if (i < medianIndex) {
leftSubTree.push_back(points[i]);
}
else if (i > medianIndex) {
rightSubTree.push_back(points[i]);
}
}
KDNode* node = new KDNode(pivot);
node->axis = axis;
node->left = build(leftSubTree, depth + 1);
node->right = build(rightSubTree, depth + 1);
return node;
}
// 先序遍历K-D树
void preorder(KDNode* node) {
if (!node) return;
std::cout << node->point.id << " ";
preorder(node->left);
preorder(node->right);
}
public:
KDTree(std::vector<Point>& points) {
root = build(points, 0);
}
~KDTree() {
clear(root);
}
void clear(KDNode* node) {
if (!node) return;
clear(node->left);
clear(node->right);
delete node;
}
void printPreorder() {
preorder(root);
}
};
int main() {
int n;
std::cin >> n;
std::vector<Point> points;
for (int i = 1; i <= n; ++i) {
Point p;
p.id = i;
std::cin >> p.x >> p.y >> p.z;
points.push_back(p);
}
KDTree kdTree(points);
kdTree.printPreorder();
std::cout << std::endl;
return 0;
}
AABB求交
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include<iomanip>
using namespace std;
struct Point {
double x, y;
};
struct AABB {
Point min, max;
};
struct Ray {
Point origin;
Point direction;
};
double t;
bool ok(double y,double x1, double x2, double k, double tmin, double tmax) {
double ty = y + (x2 - x1) * k;
if (ty >= tmin && ty <= tmax) {
t = ty;
return true;
}
return false;
}
bool dir(double x,double y,const Ray& ray) {
if (x - ray.origin.x < 0 && ray.direction.x>0) {
return false;
}
if (x - ray.origin.x > 0 && ray.direction.x<0) {
return false;
}
if (y - ray.origin.y < 0 && ray.direction.y>0) {
return false;
}
if (y - ray.origin.y > 0 && ray.direction.y<0) {
return false;
}
return true;
}
Ray tRay;
bool comparePoints(const Point& a, const Point& b) {
return (a.x - tRay.origin.x) * (a.x - tRay.origin.x) + (a.y - tRay.origin.y) * (a.y - tRay.origin.y) <
(b.x - tRay.origin.x) * (b.x - tRay.origin.x) + (b.y - tRay.origin.y) * (b.y - tRay.origin.y);
}
bool equalForPoint(const Point& a, const Point& b){
return a.x == b.x && a.y == b.y;
}
// 检测射线是否与AABB相交,并返回交点
bool intersectRayAABB(const Ray& ray, const AABB& box, vector<Point>& intersections) {
if (ok(ray.origin.y, ray.origin.x, box.min.x, ray.direction.y / ray.direction.x, box.min.y, box.max.y)) {
if(dir(box.min.x, t,ray))
intersections.push_back({ box.min.x,t });
}
if (ok(ray.origin.y, ray.origin.x, box.max.x, ray.direction.y / ray.direction.x, box.min.y, box.max.y)) {
if(dir(box.max.x, t,ray))
intersections.push_back({ box.max.x,t });
}
if (ok(ray.origin.x, ray.origin.y, box.min.y, ray.direction.x / ray.direction.y, box.min.x, box.max.x)) {
if (dir(t, box.min.y,ray))
intersections.push_back({ t,box.min.y });
}
if (ok(ray.origin.x, ray.origin.y, box.max.y, ray.direction.x / ray.direction.y, box.min.x, box.max.x)) {
if (dir(t, box.max.y,ray))
intersections.push_back({ t,box.max.y });
}
vector<Point>::iterator new_end;
new_end = unique(intersections.begin(), intersections.end(), equalForPoint);
intersections.erase(new_end, intersections.end());
//new_end = std::unique(elevationPoints.begin(), elevationPoints.end(), equalForPoint);
if (intersections.size() != 0)
return true;
return false;
}
int main() {
int N, M;
cin >> N >> M;
AABB box[100];
Ray ray[100];
for (int i = 0; i < N; i++) {
cin >> box[i].min.x;
cin >> box[i].min.y;
cin >> box[i].max.x;
cin >> box[i].max.y;
}
for (int i = 0; i < M; i++) {
cin >> ray[i].origin.x;
cin >> ray[i].origin.y;
cin >> ray[i].direction.x;
cin >> ray[i].direction.y;
}
for (int i = 0; i < M; i++) {
vector<Point> intersections;
for (int j = 0; j < N; j++) {
intersectRayAABB(ray[i], box[j], intersections);
}
tRay = ray[i];
sort(intersections.begin(), intersections.end(), comparePoints);
if (intersections.size() != 0) {
cout << intersections.size() << " ";
//unique(intersections.begin(), intersections.end(), equalForPoint);
for (const auto& p : intersections) {
cout << setiosflags(ios::fixed) << setprecision(2);
cout << p.x << " " << p.y << " ";
}
}
else {
cout << "-1";
}
cout << endl;
}
return 0;
}