吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1484|回复: 4
收起左侧

[其他原创] 分享个octree,方便朋友们调用

[复制链接]
durongze 发表于 2020-8-22 17:03
头文件
[C++] 纯文本查看 复制代码
#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__

源文件
[C++] 纯文本查看 复制代码
#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[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::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);
}


主程序
[Asm] 纯文本查看 复制代码
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();
}


免费评分

参与人数 3威望 +1 吾爱币 +11 热心值 +3 收起 理由
K_YJH + 1 + 1 谢谢@Thanks!
netspirit + 1 都没看到人回复 估计大家都不知道这是干啥的...
苏紫方璇 + 1 + 10 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

netspirit 发表于 2020-8-22 18:55
本帖最后由 netspirit 于 2020-8-23 10:28 编辑

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

免费评分

参与人数 3吾爱币 +4 热心值 +1 收起 理由
Zeaf + 1 + 1 我也没看明白,看来很硬核,都直接威望+1了
Smallbug + 1 我很赞同!
性本善 + 2 用心讨论,共获提升!

查看全部评分

 楼主| durongze 发表于 2020-8-22 17:22
以上代码,如果对你有用,麻烦评个分,谢谢。
netspirit 发表于 2020-8-22 18:26
这是干啥用的呀.........

免费评分

参与人数 1吾爱币 -2 收起 理由
Ldfd -2 请勿灌水,提高回帖质量是每位会员应尽的义务!

查看全部评分

Jersing 发表于 2020-9-1 17:21
不明觉厉,所以是做什么用的,谁能告诉告诉我
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 02:37

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表