gradientspace / geometry3Sharp

C# library for 2D/3D geometric computation, mesh algorithms, and so on. Boost license.
http://www.gradientspace.com
Boost Software License 1.0
1.73k stars 391 forks source link

ContOrientedBox3 orientation seems wrong #56

Open tpetri94 opened 6 years ago

tpetri94 commented 6 years ago

Hi,

I'm using the the ContOrientedBox3() class to generate oriented bounding boxes from convex point sets. The problem that I encountered is that these boxes, while seemingly having the correct size, are oriented in weird directions.

My code looks as following:

Box3d box = new ContOrientedBox3(points).Box;
Matrix3f RotationMatrix = new Matrix3f(box.AxisX, box.AxisY, box.AxisZ, false);
Quaternionf quaternionFromRotation = new Quaternionf(RotationMatrix);

When I use axis aligned bounding boxes and generate the rotation quaternion exactly as above, I get decent results. Here are two results for the same 3 point sets (which form a pistol). Left oriented, right AA:

screenshot_edited

I repainted the edges manually, as the boxes were just plain white before, which was harder to see. Before using g3s, I used trimesh in Python, which also has an implementation for oriented bounding boxes - which produced different, good looking results for the same underlying points .

rms80 commented 6 years ago

The algorithm that ContOrientedBox3() uses is not the minimal oriented bounding box, it's just a containing oriented box. It is a very simple/fast algorithm, based on computing the covariance matrix of the point set.

To compute the minimal box, first it is necessary to compute the convex hull, and then do rotating calipers on the convex hull. This is much slower and I've only done it for the 2D case so far. There is C++ code here that uses many of the same class-names/conventions as g3Sharp (lots of g3Sharp is ported from this library):

https://www.geometrictools.com/GTEngine/Include/Mathematics/GteMinimumVolumeBox3.h

but it's not cut-and-paste porting, unfortunately.

UrYuWang commented 4 years ago

In ContOrientedBox3, is there a value of rotation matrix stored? I want to keep a rotation matrix.

DPD85 commented 6 months ago

Hi, @rms80 there is a plan to porting the minimal oriented bounding box?