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();
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();
private:
	int m_childNum;
	int m_depth;
	OctreeNode *m_root;
};
#endif // !__OCTREE_H__

源文件

#include "stdafx.h"
#include "octree.h"
#include <iostream>
#include <fstream>

std::fstream g_fs;

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[iter->first] = 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[iter->first] = child;
	}
	return 0;
}

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

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

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

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

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()
{

}

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()
{
	g_fs.open("octree_log.txt", std::ios::trunc | std::ios::out | std::ios::in);
	m_root->Dump();
	g_fs.close();
}

主程序

int main(int argc, char** argv)
{
	Octree oct(7);
	oct.Dump();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值