c# - 我正在尝试将四叉树代码更新为八叉树
问题描述
我正在尝试使用 QuadTree 代码来开发八叉树代码。但是,在将 Rectangle3d 更改为 Box 时,我被卡住了。基本上我有一个分割节点的功能,在分割矩形时,我使用宽度和高度并将它们分割,然后使用构造函数 - Rectangle3d(Plane, Double, Double) 但我不知道要使用哪个构造函数以及如何使用当我从 Rectangle3d 更改为 Box 时计算它。谁能帮我这个?
public static Octree oct;
public static DataTree < Point3d > psOUT;
public static List<Line> lns = new List<Line>();
//////////Octree////////
public class Octree
{
public int MAX_OBJECTS = 10;
public int MAX_LEVELS = 8;
private int level;
private List<Point3d>objects;
private Box bounds;
private Octree[] nodes;
/*
* Constructor
*/
public Octree(int pLevel, Box pBounds)
{
level = pLevel;
objects = new List<Point3d>();
bounds = pBounds;
nodes = new Octree[8];
}
// implement the five methods of a Octree: clear, split, getIndex, insert, and retrieve.
/*
* Clears the Octree
*/
public void clear()
{
objects.Clear();
for (int i = 0; i < nodes.Length; i++)
{
if (nodes[i] != null)
{
nodes[i].clear();
nodes[i] = null;
}
}
}
/*
* Splits the node into 8 subnodes
*/
private void split()
{
double subWidth = bounds.X * 0.5;
double subDepth = bounds.Y * 0.5;
double subHeight = bounds.Z *0.5;
double x = bounds.X.T0;
double y = bounds.Y.T0;
double z = bounds.Z.T0;
nodes[3] = new Quadtree(level + 1, new Box(Plane.WorldXY, new Point3d(x + subWidth, y, 0), new Point3d(x + 2 * subWidth, y + subHeight, 0)));
nodes[2] = new Quadtree(level + 1, new Box(Plane.WorldXY, new Point3d(x, y, 0), new Point3d(x + subWidth, y + subHeight, 0)));
nodes[1] = new Quadtree(level + 1, new Box(Plane.WorldXY, new Point3d(x, y + subHeight, 0), new Point3d(x + subWidth, y + 2 * subHeight, 0)));
nodes[0] = new Quadtree(level + 1, new Box(Plane.WorldXY, new Point3d(x + subWidth, y + subHeight, 0), new Point3d(x + 2 * subWidth, y + 2 * subHeight, 0)));
}
解决方案
也许有点晚了,但它是这样的:
考虑到您已经在 3D 空间中构建了一个 Box。为了将其划分为 8 个较小的框,您可以使用每个角和框中心点之间的 X、Y、Z 坐标间隔。所以,你会有这样的事情:
private List<Box> Split(Box box)
{
List<Box> boxes = new List<Box>();
foreach(Point3d corner in box.GetCorners())
{
Box newbox = CreateBoxFromPlaneAndTwoCorners(box.Plane, box.Center, corner);
boxes.Add(newbox);
}
return boxes;
}
private Box CreateBoxFromPlaneAndTwoCorners(Plane plane, Point3d cornerA, Point3d cornerB) {
plane.RemapToPlaneSpace(cornerA, out Point3d remapA);
plane.RemapToPlaneSpace(cornerB, out Point3d remapB);
Interval intX = new Interval(remapA.X,remapB.X);
Interval intY = new Interval(remapA.Y,remapB.Y);
Interval intZ = new Interval(remapA.Z,remapB.Z);
return new Box(plane,intX,intY,intZ);
}
推荐阅读
- java - java运行时在运行时执行类型擦除时如何知道泛型类型
- angular - Angular 分组 ngFor 不显示值
- angular - 将文本/csv 文件转换为 blob [Angular 7 / Java]
- javascript - console.log 在 Mac 上没有输出在 MacBook 上工作
- python - Python不执行脚本
- javascript - Reddit 使用哪个 Markdown 编辑器?
- mysql - 在 Laravel 中调用查询生成器后立即更新所有列的值
- javascript - 地图创建后如何设置谷歌地图背景。map.setOptions() 不起作用
- javascript - 如何使用 Mongoose 操作从数据库中读取的字段和引用?
- git - 为什么我的 git 突然神奇地能够显示 pdf 文件的文本差异?