ArchitectureColoredPainting/ArchitectureColoredPainting/BvhTree.h

68 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include <QOpenGLTexture>
#include <QOpenGLContext>
#include <QTextCodec>
#include <algorithm>
#include <QVector4D>
#include <iostream>
#include <memory>
#include <vector>
#include <cmath>
#define bvhPtr std::tr1::shared_ptr<BvhNode>
// BvhTree 节点
struct BvhNode {
GLuint lab;
QVector4D bound;
bvhPtr child[2];
static bool x_compare(QVector4D a, QVector4D b) {
return a.x() < b.x();
}
static bool y_compare(QVector4D a, QVector4D b) {
return a.y() < b.y();
}
GLuint getLeftSon(int k) {
if (child[0] == NULL) {
return INT_MAX;
}
return child[0].get()->lab;
}
GLuint getRightSon(int k) {
if (child[1] == NULL) {
return 0;
}
return child[1].get()->lab;
}
BvhNode() {
child[0] = child[1] = NULL;
}
int maximumBound() {
return (std::fabs(bound.x() - bound.w()) < std::fabs(bound.y() - bound.z()));
}
~BvhNode() {}
};
class BvhTree
{
private:
GLuint tot;
bvhPtr root;
static QVector4D calcBound(QVector4D initBound[], int l, int r);
static QVector4D Union(QVector4D a, QVector4D b);
public:
BvhTree() {
tot = 0;
root = NULL;
}
// 根据底层包围盒生成bvh树
void buildBvhTree(QVector4D initBound[], int len);
bvhPtr subBvhTree(QVector4D initBound[], int l, int r);
void traverseBvhTree(bvhPtr now, std::vector<GLuint>& children, std::vector<QVector4D>& bounds);
// 获得 Bvh rootBvh部分的 children 数组,包围盒数组 vector 传输
void getBvhArray(std::vector<GLuint>& children, std::vector<QVector4D>& bounds);
// 获得 BvhTree 中节点总数
GLuint getBvhNodeNum();
};