durongze 发表于 2020-8-22 17:03

分享个octree,方便朋友们调用

头文件
#ifndef __OCTREE_H__
#define __OCTREE_H__
#include <map>

class OctreeNode
{
public:
        OctreeNode();
        OctreeNode(int level, int value);
        OctreeNode(const OctreeNode& node);
        ~OctreeNode();

public:
        int Assign(int level, int value);
        int Assign(const OctreeNode& node);
        int InsertChild(int idx, OctreeNode*& node);
        int RemoveChild(int idx);
        int PeekChild(int idx, OctreeNode& node);
        void Dump(std::fstream& fs);
private:
        std::map<int, OctreeNode*> m_childNode;
        int m_val;
        int m_lvl;
};

class Octree
{
public:
        Octree();
        Octree(int depth);
        ~Octree();
        void InitChild(OctreeNode *child, int d, int depth);
        void Dump(std::fstream& fs);
private:
        int m_childNum;
        int m_depth;
        OctreeNode *m_root;
};
#endif // !__OCTREE_H__
源文件
#include "stdafx.h"
#include "octree.h"
#include <iostream>
#include <fstream>

OctreeNode::OctreeNode()
        :m_val(0), m_childNode(), m_lvl(0)
{
}

OctreeNode::OctreeNode(int level, int value)
        :m_val(value), m_childNode(), m_lvl(level)
{
}

OctreeNode::OctreeNode(const OctreeNode& node)
{
        if (this == &node) {
                return;
        }
        m_val = node.m_val;
        m_lvl = node.m_lvl;
        std::map<int, OctreeNode*>::const_iterator iter;
        for (iter = node.m_childNode.begin(); node.m_childNode.end() != iter; iter++) {
                OctreeNode *child = NULL;
                if (iter->second) {
                        child = new OctreeNode(*iter->second);
                }
                m_childNode = child;
        }
}

OctreeNode::~OctreeNode()
{
        std::map<int, OctreeNode*>::iterator iter;
        for (iter = m_childNode.begin(); m_childNode.end() != iter; iter++) {
                if (iter->second) {
                        delete iter->second; //
                        iter->second = NULL;
                }
        }
        m_childNode.clear();
}

int OctreeNode::Assign(int level, int value)
{
        m_lvl = level;
        m_val = value;
        return 0;
}

int OctreeNode::Assign(const OctreeNode& node)
{
        if (this == &node) {
                return 0;
        }
        m_val = node.m_val;
        m_lvl = node.m_lvl;
        std::map<int, OctreeNode*>::const_iterator iter;
        for (iter = node.m_childNode.begin(); node.m_childNode.end() != iter; iter++) {
                OctreeNode *child = NULL;
                if (iter->second) {
                        child = new OctreeNode(*iter->second);
                }
                m_childNode = child;
        }
        return 0;
}

int OctreeNode::InsertChild(int idx, OctreeNode*& node)
{
        m_childNode = node;
        return 0;
}

int OctreeNode::RemoveChild(int idx)
{
        if (m_childNode != NULL) {
                delete m_childNode;
                m_childNode = NULL;
        }
        return 0;
}

int OctreeNode::PeekChild(int idx, OctreeNode& node)
{
        if (m_childNode != NULL) {
                node.Assign(*m_childNode);
        }
        return 0;
}

void OctreeNode::Dump(std::fstream& fs)
{
        std::map<int, OctreeNode*>::const_iterator iter;
        fs << "[" << m_lvl << ","<< m_val << "] ";
        fs << std::endl;
        for (iter = m_childNode.begin(); m_childNode.end() != iter; iter++) {
                for (int id = 0; id < m_lvl; id++) {
                        fs << "    ";
                }
                if (iter->second) {
                        iter->second->Dump(fs);
                }
        }
}

Octree::Octree()
        :m_depth(0), m_root(NULL),m_childNum(8)
{

}

Octree::Octree(int depth)
        : m_depth(depth), m_root(NULL), m_childNum(8)
{
        m_root = new OctreeNode();
        InitChild(m_root, 0, m_depth);
}

Octree::~Octree()
{
        delete m_root;
}

void Octree::InitChild(OctreeNode *root, int d, int depth)
{
        if (root == NULL) {
                return;
        }
        if (++d >= depth) {
                return;
        }
        unsigned int l;
        for (l = 0; l < m_childNum; l++) {
                int value = l;
                OctreeNode *child = new OctreeNode(d, value);
                if (child != NULL) {
                        root->InsertChild(l, child);
                        InitChild(child, d, depth);
                }
        }
        return;
}

void Octree::Dump(std::fstream& fs)
{
        m_root->Dump(fs);
}

主程序
int main(int argc, char** argv)
{
        Octree oct(7);
        std::fstream fs;
        fs.open("octree_log.txt", std::ios::trunc | std::ios::out | std::ios::in);
        oct.Dump(fs);
        fs.close();
}

netspirit 发表于 2020-8-22 18:55

本帖最后由 netspirit 于 2020-8-23 10:28 编辑

咋莫名其妙-2 一进来一脸懵逼真不知道是干啥的{:301_972:}

durongze 发表于 2020-8-22 17:22

以上代码,如果对你有用,麻烦评个分,谢谢。{:1_936:}

netspirit 发表于 2020-8-22 18:26

这是干啥用的呀.........

Jersing 发表于 2020-9-1 17:21

不明觉厉,所以是做什么用的,谁能告诉告诉我
页: [1]
查看完整版本: 分享个octree,方便朋友们调用