⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ell.cpp

📁 快速fft变换的c实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ell.cpp: implementation of the Cell class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FastRBF.h"
#include "ell.h"
#include <math.h>
#include <GL\glut.h>
#include "MathMethod\LU.h"

#define MAXPOINTNUM 16

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Cell::Cell(int level, float ox, float oy, float oz)
{
	m_fOriginX = ox;
	m_fOriginY = oy;
	m_fOriginZ = oz;
	m_iLevel = level;
	m_iPointNum = 0;
	m_pPointIndex = 0;
	m_bIsLeaf = TRUE;
	for (int i=0; i<8; i++)
		m_pChild[i] = 0;
}

Cell::~Cell()
{
	if (m_bIsLeaf)
	{
		if (m_iPointNum > 0)
			delete[] m_pPointIndex;
	}
	else
	{
		for (int i=0; i<8; i++)
		{
			if (m_pChild[i])
			{
				delete m_pChild[i];
				m_pChild[i] = 0;
			}
			m_bIsLeaf = TRUE;
		}
	}
}

void Cell::SetOrigin(float ox, float oy, float oz)
{
	m_fOriginX = ox;
	m_fOriginY = oy;
	m_fOriginZ = oz;
}

void Cell::GetOrigin(float &ox, float &oy, float &oz)
{
	ox = m_fOriginX;
	oy = m_fOriginY;
	oz = m_fOriginZ;
}

float Cell::GetSize(float bigSize)
{
	return (float)(bigSize*pow(2, -m_iLevel));
}

void Cell::CreatChildren(float sizeX, float sizeY, float sizeZ)
{
	m_bIsLeaf = FALSE;
	float ox1, oy1, oz1;
	float sx = 0.5f*GetSize(sizeX);
	float sy = 0.5f*GetSize(sizeY);
	float sz = 0.5f*GetSize(sizeZ);
	
	for(int i=0; i<8; i++)
	{
		if(i%2>0)
			ox1 = m_fOriginX + sx;
		else
			ox1 = m_fOriginX;

		if(i%4>1)
			oy1 = m_fOriginY + sy;
		else
			oy1 = m_fOriginY;

		if(i>3)
			oz1 = m_fOriginZ + sz;
		else
			oz1 = m_fOriginZ;

		m_pChild[i] = new Cell(m_iLevel+1, ox1, oy1, oz1);
	}
}

void Cell::AddPoint(int index, PointSet *ps, float sizeX, float sizeY, float sizeZ)
{
	if (m_bIsLeaf)
	{
		if (!m_iPointNum)
			m_pPointIndex = new int[MAXPOINTNUM];

		if (m_iPointNum > MAXPOINTNUM-1)
		{
			CreatChildren(sizeX, sizeY, sizeZ);
			for (int i=0; i<m_iPointNum; i++)
				AddPoint(m_pPointIndex[i], ps, sizeX, sizeY,sizeZ);

			AddPoint(index, ps, sizeX, sizeY, sizeZ);
			m_iPointNum = 0;
			delete[] m_pPointIndex;
		}
		else
			m_pPointIndex[m_iPointNum++] = index;
	}
	else
	{
		int j = 0;
		float *point = ps->GetPoint(index);
		
		if(point[0] >= 0.5*GetSize(sizeX) + m_fOriginX)
			j += 1;
		if(point[1] >= 0.5*GetSize(sizeY) + m_fOriginY)
			j += 2;
		if(point[2] >= 0.5*GetSize(sizeZ) + m_fOriginZ)
			j += 4;

		m_pChild[j]->AddPoint(index, ps, sizeX, sizeY, sizeZ);
	}
}

//split the children to the max level
void Cell::SplitChild(int MAXLEVEL, PointSet *ps, float sizeX, float sizeY, float sizeZ)
{
	if (!m_bIsLeaf)
	{
		for (int i=0; i<8; i++)
			m_pChild[i]->SplitChild(MAXLEVEL, ps, sizeX, sizeY, sizeZ);
	}
	else if (m_iPointNum>1 && m_iLevel<MAXLEVEL-1)
	{
		CreatChildren(sizeX, sizeY, sizeZ);
		for (int i=0; i<m_iPointNum; i++)
			AddPoint(m_pPointIndex[i], ps, sizeX, sizeY, sizeZ);

		delete[] m_pPointIndex;
		for (i=0; i<8; i++)
		{
			if (m_pChild[i]->m_iPointNum == 0)
			{
				delete m_pChild[i];
				m_pChild[i] = 0;
			}
			else
				m_pChild[i]->SplitChild(MAXLEVEL, ps, sizeX, sizeY, sizeZ);
		}
	}
}

void Cell::CleanChildren()
{
	if (!m_bIsLeaf)
	{
		for (int i=0; i<8; i++)
		{
			if (m_pChild[i]->IsLeaf() && m_pChild[i]->GetPointNum()==0)
			{
				delete m_pChild[i];
				m_pChild[i] = 0;
			}
			else
				m_pChild[i]->CleanChildren();
		}
	}
}

BOOL Cell::IsLeaf()
{
	return m_bIsLeaf;
}

int Cell::GetPointNum()
{
	return m_iPointNum;
}

void Cell::AddLeafSize(float sizeX, float sizeY, float sizeZ, int &count, float &SIZE)
{
	if (m_bIsLeaf)
	{
		if (!m_iPointNum)
			return;
		else
		{
			count++;
			float sx = GetSize(sizeX);
			float sy = GetSize(sizeY);
			float sz = GetSize(sizeZ);
			SIZE += (float)sqrt(sx*sx + sy*sy + sz*sz);
		}
	}
	else
	{
		for (int i=0; i<8; i++)
		{
			if (m_pChild[i] && m_pChild[i]->GetPointNum())
				m_pChild[i]->AddLeafSize(sizeX, sizeY, sizeZ, count, SIZE);
		}
	}
}

void Cell::AddIntoTable(PointSet *ps, int *table, int &tableSize, float x, float y, float z, float r, float sizeX, float sizeY, float sizeZ)
{
	if (m_bIsLeaf)
	{
		for (int i=0; i<m_iPointNum; i++)
		{
			float *point = ps->GetPoint(m_pPointIndex[i]);
			if ((point[0]-x)*(point[0]-x) + (point[1]-y)*(point[1]-y) + (point[2]-z)*(point[2]-z)
				< r*r)
				table[tableSize++] = m_pPointIndex[i];
		}
	}
	else
	{
		float cx = (float)(m_fOriginX + 0.5*GetSize(sizeX));
		float cy = (float)(m_fOriginY + 0.5*GetSize(sizeY));
		float cz = (float)(m_fOriginZ + 0.5*GetSize(sizeZ));
		if(x + r < cx)
		{
			if(y + r < cy)
			{
				if(z + r < cz)
				{
					if(m_pChild[0] != NULL)
						m_pChild[0]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else if(z - r >= cz)
				{
					if(m_pChild[4] != NULL)
						m_pChild[4]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else
				{
					if(m_pChild[0] != NULL)
						m_pChild[0]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[4] != NULL)
						m_pChild[4]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
			}
			else if(y - r >= cy)
			{
				if(z + r < cz)
				{
					if(m_pChild[2] != NULL)
						m_pChild[2]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else if(z - r >= cz)
				{
					if(m_pChild[6] != NULL)
						m_pChild[6]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else
				{
					if(m_pChild[2] != NULL)
						m_pChild[2]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[6] != NULL)
						m_pChild[6]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
			}
			else
			{
				if(z + r < cz)
				{
					if(m_pChild[0] != NULL)
						m_pChild[0]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[2] != NULL)
						m_pChild[2]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else if(z - r >= cz)
				{
					if(m_pChild[4] != NULL)
						m_pChild[4]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[6] != NULL)
						m_pChild[6]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else
				{
					if(m_pChild[0] != NULL)
						m_pChild[0]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[4] != NULL)
						m_pChild[4]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[2] != NULL)
						m_pChild[2]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[6] != NULL)
						m_pChild[6]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
			}
		}
		else if(x -r >= cx)
		{
			if(y + r < cy)
			{
				if(z + r < cz)
				{
					if(m_pChild[1] != NULL)
						m_pChild[1]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else if(z - r >= cz)
				{
					if(m_pChild[5] != NULL)
						m_pChild[5]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else
				{
					if(m_pChild[1] != NULL)
						m_pChild[1]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[5] != NULL)
						m_pChild[5]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
			}
			else if(y - r >= cy)
			{
				if(z + r < cz)
				{
					if(m_pChild[3] != NULL)
						m_pChild[3]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else if(z - r >= cz)
				{
					if(m_pChild[7] != NULL)
						m_pChild[7]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else
				{
					if(m_pChild[3] != NULL)
						m_pChild[3]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[7] != NULL)
						m_pChild[7]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
			}
			else
			{
				if(z + r < cz)
				{
					if(m_pChild[1] != NULL)
						m_pChild[1]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[3] != NULL)
						m_pChild[3]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else if(z - r >= cz)
				{
					if(m_pChild[5] != NULL)
						m_pChild[5]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[7] != NULL)
						m_pChild[7]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
				}
				else
				{
					if(m_pChild[1] != NULL)
						m_pChild[1]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[5] != NULL)
						m_pChild[5]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[3] != NULL)
						m_pChild[3]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);
					if(m_pChild[7] != NULL)
						m_pChild[7]->AddIntoTable(ps, table, tableSize, x, y, z, r, sizeX, sizeY, sizeZ);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -