四叉数之Box索引Point

上接第一篇四叉数之Box索引Box,以下源码如题。

这种情况貌似,再Node节点中在存一份Box更好。索引效率应该更高。

// box 索引 点的情况
public class QuadTreePoint<T> {

    private Node<T> root;

    public static class Node<T> {

        public double x, y;
        public Node<T> NW, NE, SE, SW;
        public T s;

        public Node(double x, double y, T s) {
            this.x = x;
            this.y = y;
            this.s = s;
        }
    }

    public static class Box {

        private double xLow, xHigh, yLow, yHigh;

        public Box(double xLow, double xHigh, double yLow, double yHigh) {
            this.xLow = xLow;
            this.xHigh = xHigh;
            this.yLow = yLow;
            this.yHigh = yHigh;
        }

        public boolean contain(double x, double y) {
            return (x <= xHigh && x >= xLow && y <= yHigh && y >= yLow);
        }

    }

    public Node<T> insert(Node<T> node, double x, double y, T s) {

        if (node == null) {
            return new Node<T>(x, y, s);
        }

        if (x < node.x && y < node.y) {
            node.NW = insert(node.NW, x, y, s);
            return node;
        }

        if (x < node.x && !(y < node.y)) {
            node.SW = insert(node.SW, x, y, s);
            return node;
        }

        if (!(x < node.x) && y < node.y) {
            node.NE = insert(node.NE, x, y, s);
            return node;
        }

        if (!(x < node.x) && !(y < node.y)) {
            node.SE = insert(node.SE, x, y, s);
        }

        return null;
    }

    public List<Node<T>> query(Box box) {

        List<Node<T>> result = Lists.newArrayList();
        query(root, box, result);
        return result;
    }

    private void query(Node<T> node, Box box, List<Node<T>> result) {

        if (node == null) {
            return;
        }

        if (box.contains(h.x, h.y))
            result.add(h);

        if ( (box.xlow < h.x) &&  (box.ylow < h.y))
            query(h.SW, box, result);
        if ( (box.xlow < h.x) && !(box.yhigh < h.y))
            query(h.NW, box, result);
        if (!(box.xhigh < h.x) &&  (box.ylow < h.y))
            query(h.SE, box, result);
        if (!(box.xhigh < h.x) && !(box.yhigh < h.y))
            query(h.NE, box, result);

    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Matlab代码,用于搜索四叉树quadtree的每个节点,获取区域范围与射线相交的叶子节点中存储的三角面索引,并且根据索引得到三角面的顶点坐标和法向量,最后用射线与三角面进行求交运算,最后遍历完整棵四叉树,得到射线与所有三角面的交点,返回交点坐标和索引信息。 ```matlab function [vertices, normals, indices] = quadtree_ray_tracing(root, ray_origin, ray_direction) % Recursive function to traverse the quadtree and find intersections % with ray % Check if current node is a leaf node if isempty(root.children) % Check if ray intersects with leaf node's bounding box intersection = ray_box_intersection(root.box, ray_origin, ray_direction); if intersection % Get triangle indices from leaf node indices = root.indices; % Get vertices and normals from indices [vertices, normals] = get_vertices_normals(indices); % Find ray-triangle intersections [intersections, ~] = TriangleRayIntersection(ray_origin, ray_direction, vertices); % Return intersection information if ~isempty(intersections) % Find closest intersection [~, index] = min(intersections); intersection_point = vertices(index, :); intersection_normal = normals(index, :); return end end else % Recursively traverse child nodes for i = 1:length(root.children) child = root.children(i); intersection = ray_box_intersection(child.box, ray_origin, ray_direction); if intersection [vertices, normals, indices] = quadtree_ray_tracing(child, ray_origin, ray_direction); if ~isempty(indices) return end end end end % No intersection found vertices = []; normals = []; indices = []; end function [vertices, normals] = get_vertices_normals(indices) % Get vertices and normals from triangle indices % Load mesh data mesh_data = load('mesh_data.mat'); vertices = mesh_data.vertices; normals = mesh_data.normals; % Get vertices and normals for each triangle vertices = vertices(indices, :); normals = normals(indices, :); end function intersection = ray_box_intersection(box, ray_origin, ray_direction) % Check if ray intersects with bounding box % Check for intersection with each face of bounding box tmin = (box([1 2 3]) - ray_origin) ./ ray_direction; tmax = (box([4 5 6]) - ray_origin) ./ ray_direction; tmin = max(min(tmin, tmax), 0); tmax = min(max(tmin, tmax), 1); intersection = all(tmax >= tmin); end function [t, u, v] = TriangleRayIntersection(Orig, Dir, Verts) % Calculate ray-triangle intersection % Triangle vertices v1 = Verts(1,:); v2 = Verts(2,:); v3 = Verts(3,:); % Edge vectors e1 = v2 - v1; e2 = v3 - v1; % Normal vector n = cross(e1, e2); % Check if ray is parallel to triangle ndotd = dot(n, Dir); if abs(ndotd) < eps t = Inf; u = 0; v = 0; return end % Calculate intersection point w = Orig - v1; ndotw = dot(n, w); t = -ndotw / ndotd; % Check if intersection is behind ray origin if t < 0 t = Inf; u = 0; v = 0; return end % Calculate barycentric coordinates q = cross(Dir, e2); u = dot(w, q) / dot(n, q); p = cross(w, e1); v = dot(Dir, p) / dot(n, p); % Check if point is inside triangle if u < 0 || v < 0 || u + v > 1 t = Inf; u = 0; v = 0; return end end ``` 请注意,上面的代码只是一个简单的示例,需要根据具体的应用场景和数据格式进行修改和调整。同时,代码中还需要加载存储三角面数据的文件,这里假设该数据以matlab格式存储,文件名为`mesh_data.mat`,包含`vertices`和`normals`两个变量,分别存储所有三角面的顶点坐标和法向量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值