数据结构——八叉树

八叉树(Octree)是一种用于表示和管理三维空间的树状数据结构。它将三维空间递归地分割成八个八分体(octant),每个八分体可以继续分割,以实现对三维空间的更精细的划分。八叉树通常用于解决空间搜索和查询问题,例如三维物体碰撞检测、体素化(Voxelization)、地理信息系统等领域。

cpp 复制代码
#include <iostream>
#include <vector>

// 定义三维点的结构体
struct Point3D {
    float x;
    float y;
    float z;
    Point3D(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
};

// 定义八叉树节点
struct OctreeNode {
    Point3D center;
    float size;
    OctreeNode* children[8];

    OctreeNode(Point3D _center, float _size) : center(_center), size(_size) {
        for (int i = 0; i < 8; i++) {
            children[i] = nullptr;
        }
    }
};

class Octree {
private:
    OctreeNode* root;
    float rootSize;

    // 在指定深度下递归插入节点
    OctreeNode* insert(OctreeNode* node, Point3D point, float size) {
        if (node == nullptr) {
            return new OctreeNode(point, size);
        }

        // 确定点位于八分体的哪个子节点
        int index = 0;
        if (point.x >= node->center.x) index |= 1;
        if (point.y >= node->center.y) index |= 2;
        if (point.z >= node->center.z) index |= 4;

        // 递归插入到相应的子节点
        float newSize = node->size / 2.0f;
        node->children[index] = insert(node->children[index], point, newSize);

        return node;
    }

    // 在指定深度下递归搜索节点
    bool search(OctreeNode* node, Point3D point, float size) {
        if (node == nullptr) {
            return false;
        }

        if (node->center.x == point.x && node->center.y == point.y && node->center.z == point.z) {
            return true;
        }

        // 确定点位于八分体的哪个子节点
        int index = 0;
        if (point.x >= node->center.x) index |= 1;
        if (point.y >= node->center.y) index |= 2;
        if (point.z >= node->center.z) index |= 4;

        // 递归搜索相应的子节点
        float newSize = node->size / 2.0f;
        return search(node->children[index], point, newSize);
    }

public:
    Octree(float size) : root(nullptr), rootSize(size) {}

    // 插入一个点
    void insert(Point3D point) {
        root = insert(root, point, rootSize);
    }

    // 搜索一个点是否存在
    bool search(Point3D point) {
        return search(root, point, rootSize);
    }
};

int main() {
    Octree octree(100.0f); // 创建八叉树,定义根节点的大小

    Point3D point1(10.0f, 20.0f, 30.0f);
    Point3D point2(80.0f, 90.0f, 110.0f);

    octree.insert(point1); // 插入点1
    octree.insert(point2); // 插入点2

    Point3D searchPoint(80.0f, 90.0f, 110.0f);
    bool found = octree.search(searchPoint); // 搜索点2
    if (found) {
        std::cout << "Point found in the octree." << std::endl;
    } else {
        std::cout << "Point not found in the octree." << std::endl;
    }

    return 0;
}
相关推荐
丘山望岳2 小时前
Linux基础指令入门篇
linux·运维·服务器
想做小南娘,发现自己是女生喵2 小时前
LAN Device Monitor 第二周学习文档
java·服务器·数据库
byte轻骑兵2 小时前
【TextIn xParse + WorkBuddy】把8小时的标书审查压进1小时:招标要求逐条核验与废标风险预警工作台
大数据·数据库·aigc·textin·ai数据层基础设施
翔云1234562 小时前
Gaea(小米MySQL Proxy)整体架构
运维·数据库·mysql
jaysee-sjc2 小时前
【苍穹外卖】Day10:定时任务与 WebSocket 实时提醒实战
java·开发语言·javascript·数据库·vue.js·websocket·网络协议
敲代码的苦132 小时前
头歌 数据结构—消防资源调度
数据结构·算法
ysu_03142 小时前
【C++ 进阶之路】(二)从内联函数到类与对象:深挖内存对齐、this 指针与三大默认成员函数
开发语言·数据结构·c++·笔记·面向对象·this
叼烟扛炮2 小时前
Linux 学习笔记(三十二):线程控制——创建、等待与分离
linux·运维·服务器·笔记·学习·1024程序员节
WJ.Polar2 小时前
异常、泛型、集合、Stream流
java·数据结构
牧瀬クリスだ2 小时前
Redis五大数据类型全解析:从入门到实战
java·数据库·redis·后端·缓存