0

I have originally written the following Matlab code to find intersection between a set of Axes Aligned Bounding Boxes (AABB) and space partitions (here 8 partitions). I believe it is readable by itself, moreover, I have added some comments for even more clarity.

function [A,B] = AABBPart(bbx,it)                                     % bbx: aabb, it: iteration
global F
IT = it+1;
n = size(bbx,1);
F = cell(n,it);
A = Part([min(bbx(:,1:3)),max(bbx(:,4:6))],it,0);                     % recursive partitioning
B = F;                                                                % matlab does not allow
    function s = Part(bx,it,J)                                        %   output to be global
        s = {};
        if it < 1; return; end
        s = cell(8,1);
        p = bx(1:3);
        q = bx(4:6);
        h = 0.5*(p+q);
        prt = [p,h;...                                                % 8 sub-parts (octa)
            h(1),p(2:3),q(1),h(2:3);...
            p(1),h(2),p(3),h(1),q(2),h(3);...
            h(1:2),p(3),q(1:2),h(3);...
            p(1:2),h(1),h(1:2),q(3);...
            h(1),p(2),h(3),q(1),h(2),q(3);...
            p(1),h(2:3),h(1),q(2:3);...
            h,q];
        for j=1:8                                                     % check for each sub-part
            k = 0;
            t = zeros(0,1);
            for i=1:n
                if all(bbx(i,1:3) <= prt(j,4:6)) && ...               % interscetion test for
                        all(prt(j,1:3) <= bbx(i,4:6))                 %   every aabb and sub-parts
                    k = k+1;
                    t(k) = i;
                end
            end
            if ~isempty(t)
                s{j,1} = [t; Part(prt(j,:),it-1,j)];                  % recursive call
                for i=1:numel(t)                                      % collecting the results
                    if isempty(F{t(i),IT-it})
                        F{t(i),IT-it} = [-J,j];
                    else
                        F{t(i),IT-it} = [F{t(i),IT-it}; [-J,j]];
                    end
                end
            end
        end
    end
end

Concerns:

  • In my tests, it seems that probably few intersections are missing, say, 10 or so for 1000 or more setup. So I would be glad if you could help to find out any problematic parts in the code.

  • I am also concerned about using global F. I prefer to get rid of it.

  • Any other better solution in terms of speed, will be loved.

Note that the code is complete. And you can easily try it by some following setup.

n = 10000;                        % in the original application, n would be millions
bbx = rand(n,6);
it = 3;
[A,B] = AABBPart(bbx,it);
Developer
  • 8,258
  • 8
  • 49
  • 58
  • This is probably more appropriate for [code review](http://codereview.stackexchange.com/) – sco1 Mar 26 '17 at 21:06
  • @excaza If the author suspects that the code is giving incorrect results, then the question is off-topic for Code Review. – 200_success Mar 26 '17 at 21:09
  • The code is not so readable and it is not our duty to reverse-engineer it, please explain the method. –  Mar 28 '17 at 08:42
  • @YvesDaoust While I was encouraged to add some more explanation, I suddenly noticed 4 close votes. This was enough discouraging to me not put any further effort on this post. I will sort out a solution in any other way. I was only passionate to share my code and seek for advice, probably of use for any future reader. Thanks anyway. – Developer Mar 31 '17 at 20:59
  • Code reading can be a difficult exercise. You will increase your chances of getting help by helping the answerers understand what you did, rather than dumping your whole problem and let them do the work. –  Apr 01 '17 at 10:01

0 Answers0