首页 > 解决方案 > 我正在尝试将四叉树代码更新为八叉树

问题描述

我正在尝试使用 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)));
 }

标签: c#rhinograsshopper

解决方案


也许有点晚了,但它是这样的:

考虑到您已经在 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);
}


推荐阅读