File Exchange

## Minimal Bounding Box

version 1.1 (5.04 KB) by

Minimal bounding box around points in the (x,y,z) space

Updated

With this function one can easily compute the minimal box (with right angles) around a set of points in 3d.
The extremal property of the box is determined either in terms of volume, surface or sum of edgelengths.

The calculation is based on heuristics only, but a huge number of tests did not show any counterexamples yet.

The algorithm behind the function is subdivided into three levels of accuracy with differing runtimes.

x = rand(10000,1);
y = rand(10000,1);
z = rand(10000,1);

tic;[rotmat,cornerpoints,volume,surface] = minboundbox(x,y,z,'v',3);toc
Elapsed time is 10.772170 seconds.

volume =
0.9993
surface =
5.9974

My thanks to John d'Errico and Roger Stafford for numerous discussions about proofs and algorithms in this context.

John also wrote minboundrect from the FEX, which heavily influenced this submission.

Also in this archive i included a small plot function (plotminbox) to show the resulting box via connecting the cornerpoints.

deru jian

Olivier Pasquier

Thanks a lot

liang

ibtissem jbira

### ibtissem jbira (view profile)

i need the algorithm of this code :
I did not understand how you calculated BOO
at the beginning you start with calucul of its convex hull but after I understood nothing !!!

Johannes Korsawe

### Johannes Korsawe (view profile)

Ishan,
i tested the code on the latest release R2014b and it worked. No idea what is going wrong on your side. The error has to be debugged on your system line by line.
Regards, Johannes

Ishan

### Ishan (view profile)

Johannes,

Indeed, I replaced it with convhulln and the necessary argument. However, the error persists.

Are there any online resources available which can guide me through the process which you have used to program this function?

I am planning to write an updated version of your function which will be compatible for more recent versions of MATLAB. If I can gain a clear understanding of the background, perhaps I will be able to release it on FEX.

Any help would be much appreciated!

Ishan

Johannes Korsawe

### Johannes Korsawe (view profile)

Ishan,
i wrote an update for releases >= 2010a. Perhaps convhull failed in your test.
Best, Johannes

Ishan

### Ishan (view profile)

On MATLAB 2014a, I am encountering the following error:

Error using *
Inner matrix dimensions must agree.

Error in minboundbox>minrect (line 310)
rot_i = Rmat(-edgeangles(i));xyr = xy*rot_i;

Error in minboundbox>checkbox (line 274)
rot2 = minrect(x_i,y_i,metric); % find the optimal
rotation around z-axis

Error in minboundbox (line 170)
[d, rotmat, minmax] =
checkbox(alpha(i),beta(i),gamma(i),xyz,metric,d,rotmat,minmax);

Could someone please help me understand what flags to look for (or tests to run) in order to debug this? For input, I've used Johannes example of 10000x1 rand vectors.

Wolfgang

fourar redha

Matt Kindig

### Matt Kindig (view profile)

A simple fix to reduce the run-time is to replace the line:

rot_i = Rmat(-edgeangles(i));

with these lines:
theta = -edgeangles(i);
rot_i = [cos(theta), sin(theta); -sin(theta) cos(theta)];

In my testing, this reduces the run-time by a factor of ~2.

Shunzhou

jia

Steven

### Steven (view profile)

Nice work. Thank you.

Mark

### Mark (view profile)

This file saved me a ton of work, and has worked great for all of my tests, thanks!

 16 Feb 2015 1.1 Compatability issues. Convhull changed in Release 2010a.
##### MATLAB Release
MATLAB 8.4 (R2014b)