数据结构——八叉树

八叉树(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;
}
相关推荐
阿里巴巴AI编程社区2 分钟前
Qoder 提效实战:数据开发工程师用 Qoder 提效50%
数据结构
消失的旧时光-19435 分钟前
从 C 链表到 Android Looper:MessageQueue 的底层原理一条线讲透
android·数据结构·链表
C++业余爱好者13 分钟前
SQL语言家族入门指南:标准SQL、T-SQL与PL/SQL详解
数据库·sql
夏乌_Wx13 分钟前
练题100天——DAY28:找消失的数字+分发饼干
数据结构·算法
白驹过隙^^15 分钟前
OB-USP-AGENT安装使用方法
数据库·经验分享·网络协议·tcp/ip·github·ssl
计算机程序设计小李同学16 分钟前
基于Python的在线零食购物商城系统的设计与实现
数据库·sqlite
lzh2004091918 分钟前
二叉搜索树与双向链表
数据结构·链表
西格电力科技25 分钟前
面向工业用户的绿电直连架构适配技术:高可靠与高弹性的双重设计
大数据·服务器·人工智能·架构·能源
Java爱好狂.32 分钟前
Java面试Redis核心知识点整理!
java·数据库·redis·分布式锁·java面试·后端开发·java八股文
小程故事多_8032 分钟前
开源界核弹级输出!蚂蚁 Agentar-Scale-SQL 凭 “编排式扩展” 技术,成为 Text-to-SQL 天花板
数据库·人工智能·sql·开源·aigc·embedding