Skip to content

Commit

Permalink
+ add WildMagic algorithm to compute minimum volume oriented box
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jul 2, 2016
1 parent 8a30ac7 commit 0c6d524
Show file tree
Hide file tree
Showing 22 changed files with 5,428 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Mod/Mesh/App/CMakeLists.txt
Expand Up @@ -87,6 +87,10 @@ SOURCE_GROUP("Core" FILES ${Core_SRCS})
SET(WildMagic4_SRCS
WildMagic4/Wm4ApprCylinderFit3.cpp
WildMagic4/Wm4ApprCylinderFit3.h
WildMagic4/Wm4ApprGaussPointsFit2.cpp
WildMagic4/Wm4ApprGaussPointsFit2.h
WildMagic4/Wm4ApprGaussPointsFit3.cpp
WildMagic4/Wm4ApprGaussPointsFit3.h
WildMagic4/Wm4ApprLineFit3.cpp
WildMagic4/Wm4ApprLineFit3.h
WildMagic4/Wm4ApprPlaneFit3.cpp
Expand All @@ -99,8 +103,22 @@ SET(WildMagic4_SRCS
WildMagic4/Wm4ApprSphereFit3.h
WildMagic4/Wm4BandedMatrix.h
WildMagic4/Wm4BandedMatrix.inl
WildMagic4/Wm4Box2.h
WildMagic4/Wm4Box2.inl
WildMagic4/Wm4Box3.h
WildMagic4/Wm4Box3.inl
WildMagic4/Wm4ContBox2.cpp
WildMagic4/Wm4ContBox2.h
WildMagic4/Wm4ContBox3.cpp
WildMagic4/Wm4ContBox3.h
WildMagic4/Wm4ConvexHull.cpp
WildMagic4/Wm4ConvexHull.h
WildMagic4/Wm4ConvexHull1.cpp
WildMagic4/Wm4ConvexHull1.h
WildMagic4/Wm4ConvexHull2.cpp
WildMagic4/Wm4ConvexHull2.h
WildMagic4/Wm4ConvexHull3.cpp
WildMagic4/Wm4ConvexHull3.h
WildMagic4/Wm4DelPolygonEdge.cpp
WildMagic4/Wm4DelPolygonEdge.h
WildMagic4/Wm4DelPolyhedronFace.cpp
Expand Down Expand Up @@ -207,6 +225,9 @@ SET(WildMagic4_SRCS
WildMagic4/Wm4Polynomial1.inl
WildMagic4/Wm4QuadricSurface.cpp
WildMagic4/Wm4QuadricSurface.h
WildMagic4/Wm4Quaternion.cpp
WildMagic4/Wm4Quaternion.h
WildMagic4/Wm4Quaternion.inl
WildMagic4/Wm4Query.h
WildMagic4/Wm4Query.inl
WildMagic4/Wm4Query2.h
Expand Down
73 changes: 73 additions & 0 deletions src/Mod/Mesh/App/WildMagic4/Wm4ApprGaussPointsFit2.cpp
@@ -0,0 +1,73 @@
// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 4.10.0 (2009/11/18)

#include "Wm4FoundationPCH.h"
#include "Wm4ApprGaussPointsFit2.h"
#include "Wm4Eigen.h"

namespace Wm4
{
//----------------------------------------------------------------------------
template <class Real>
Box2<Real> GaussPointsFit2 (int iQuantity, const Vector2<Real>* akPoint)
{
Box2<Real> kBox(Vector2<Real>::ZERO,Vector2<Real>::UNIT_X,
Vector2<Real>::UNIT_Y,(Real)1.0,(Real)1.0);

// compute the mean of the points
kBox.Center = akPoint[0];
int i;
for (i = 1; i < iQuantity; i++)
{
kBox.Center += akPoint[i];
}
Real fInvQuantity = ((Real)1.0)/iQuantity;
kBox.Center *= fInvQuantity;

// compute the covariance matrix of the points
Real fSumXX = (Real)0.0, fSumXY = (Real)0.0, fSumYY = (Real)0.0;
for (i = 0; i < iQuantity; i++)
{
Vector2<Real> kDiff = akPoint[i] - kBox.Center;
fSumXX += kDiff.X()*kDiff.X();
fSumXY += kDiff.X()*kDiff.Y();
fSumYY += kDiff.Y()*kDiff.Y();
}

fSumXX *= fInvQuantity;
fSumXY *= fInvQuantity;
fSumYY *= fInvQuantity;

// setup the eigensolver
Eigen<Real> kES(2);
kES(0,0) = fSumXX;
kES(0,1) = fSumXY;
kES(1,0) = fSumXY;
kES(1,1) = fSumYY;
kES.IncrSortEigenStuff2();

for (i = 0; i < 2; i++)
{
kBox.Extent[i] = kES.GetEigenvalue(i);
kES.GetEigenvector(i,kBox.Axis[i]);
}

return kBox;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// explicit instantiation
//----------------------------------------------------------------------------
template WM4_FOUNDATION_ITEM
Box2<float> GaussPointsFit2<float> (int, const Vector2<float>*);

template WM4_FOUNDATION_ITEM
Box2<double> GaussPointsFit2<double> (int, const Vector2<double>*);
//----------------------------------------------------------------------------
}
28 changes: 28 additions & 0 deletions src/Mod/Mesh/App/WildMagic4/Wm4ApprGaussPointsFit2.h
@@ -0,0 +1,28 @@
// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 4.10.0 (2009/11/18)

#ifndef WM4APPRGAUSSPOINTSFIT2_H
#define WM4APPRGAUSSPOINTSFIT2_H

#include "Wm4FoundationLIB.h"
#include "Wm4Box2.h"

namespace Wm4
{

// Fit points with a Gaussian distribution. The center is the mean of the
// points, the axes are the eigenvectors of the covariance matrix, and the
// extents are the eigenvalues of the covariance matrix and are returned in
// increasing order. The quantites are stored in a Box2<Real> just to have a
// single container.
template <class Real> WM4_FOUNDATION_ITEM
Box2<Real> GaussPointsFit2 (int iQuantity, const Vector2<Real>* akPoint);

}

#endif
86 changes: 86 additions & 0 deletions src/Mod/Mesh/App/WildMagic4/Wm4ApprGaussPointsFit3.cpp
@@ -0,0 +1,86 @@
// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 4.10.0 (2009/11/18)

#include "Wm4FoundationPCH.h"
#include "Wm4ApprGaussPointsFit3.h"
#include "Wm4Eigen.h"

namespace Wm4
{
//----------------------------------------------------------------------------
template <class Real>
Box3<Real> GaussPointsFit3 (int iQuantity, const Vector3<Real>* akPoint)
{
Box3<Real> kBox(Vector3<Real>::ZERO,Vector3<Real>::UNIT_X,
Vector3<Real>::UNIT_Y,Vector3<Real>::UNIT_Z,(Real)1.0,(Real)1.0,
(Real)1.0);

// compute the mean of the points
kBox.Center = akPoint[0];
int i;
for (i = 1; i < iQuantity; i++)
{
kBox.Center += akPoint[i];
}
Real fInvQuantity = ((Real)1.0)/iQuantity;
kBox.Center *= fInvQuantity;

// compute the covariance matrix of the points
Real fSumXX = (Real)0.0, fSumXY = (Real)0.0, fSumXZ = (Real)0.0;
Real fSumYY = (Real)0.0, fSumYZ = (Real)0.0, fSumZZ = (Real)0.0;
for (i = 0; i < iQuantity; i++)
{
Vector3<Real> kDiff = akPoint[i] - kBox.Center;
fSumXX += kDiff.X()*kDiff.X();
fSumXY += kDiff.X()*kDiff.Y();
fSumXZ += kDiff.X()*kDiff.Z();
fSumYY += kDiff.Y()*kDiff.Y();
fSumYZ += kDiff.Y()*kDiff.Z();
fSumZZ += kDiff.Z()*kDiff.Z();
}

fSumXX *= fInvQuantity;
fSumXY *= fInvQuantity;
fSumXZ *= fInvQuantity;
fSumYY *= fInvQuantity;
fSumYZ *= fInvQuantity;
fSumZZ *= fInvQuantity;

// setup the eigensolver
Eigen<Real> kES(3);
kES(0,0) = fSumXX;
kES(0,1) = fSumXY;
kES(0,2) = fSumXZ;
kES(1,0) = fSumXY;
kES(1,1) = fSumYY;
kES(1,2) = fSumYZ;
kES(2,0) = fSumXZ;
kES(2,1) = fSumYZ;
kES(2,2) = fSumZZ;
kES.IncrSortEigenStuff3();

for (i = 0; i < 3; i++)
{
kBox.Extent[i] = kES.GetEigenvalue(i);
kES.GetEigenvector(i,kBox.Axis[i]);
}

return kBox;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// explicit instantiation
//----------------------------------------------------------------------------
template WM4_FOUNDATION_ITEM
Box3<float> GaussPointsFit3<float> (int, const Vector3<float>*);

template WM4_FOUNDATION_ITEM
Box3<double> GaussPointsFit3<double> (int, const Vector3<double>*);
//----------------------------------------------------------------------------
}
28 changes: 28 additions & 0 deletions src/Mod/Mesh/App/WildMagic4/Wm4ApprGaussPointsFit3.h
@@ -0,0 +1,28 @@
// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 4.10.0 (2009/11/18)

#ifndef WM4APPRGAUSSPOINTSFIT3_H
#define WM4APPRGAUSSPOINTSFIT3_H

#include "Wm4FoundationLIB.h"
#include "Wm4Box3.h"

namespace Wm4
{

// Fit points with a Gaussian distribution. The center is the mean of the
// points, the axes are the eigenvectors of the covariance matrix, and the
// extents are the eigenvalues of the covariance matrix and are returned in
// increasing order. The quantites are stored in a Box3<Real> just to have a
// single container.
template <class Real> WM4_FOUNDATION_ITEM
Box3<Real> GaussPointsFit3 (int iQuantity, const Vector3<Real>* akPoint);

}

#endif
48 changes: 48 additions & 0 deletions src/Mod/Mesh/App/WildMagic4/Wm4Box2.h
@@ -0,0 +1,48 @@
// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 4.10.0 (2009/11/18)

#ifndef WM4BOX2_H
#define WM4BOX2_H

#include "Wm4FoundationLIB.h"
#include "Wm4Vector2.h"

namespace Wm4
{

template <class Real>
class Box2
{
public:
// A box has center C, axis directions U[0] and U[1] (both unit-length
// vectors), and extents e[0] and e[1] (both nonnegative numbers). A
// point X = C+y[0]*U[0]+y[1]*U[1] is inside or on the box whenever
// |y[i]| <= e[i] for all i.

// construction
Box2 (); // uninitialized
Box2 (const Vector2<Real>& rkCenter, const Vector2<Real>* akAxis,
const Real* afExtent);
Box2 (const Vector2<Real>& rkCenter, const Vector2<Real>& rkAxis0,
const Vector2<Real>& rkAxis1, Real fExtent0, Real fExtent1);

void ComputeVertices (Vector2<Real> akVertex[4]) const;

Vector2<Real> Center;
Vector2<Real> Axis[2]; // must be an orthonormal set of vectors
Real Extent[2]; // must be nonnegative
};

#include "Wm4Box2.inl"

typedef Box2<float> Box2f;
typedef Box2<double> Box2d;

}

#endif
55 changes: 55 additions & 0 deletions src/Mod/Mesh/App/WildMagic4/Wm4Box2.inl
@@ -0,0 +1,55 @@
// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 4.10.0 (2009/11/18)

//----------------------------------------------------------------------------
template <class Real>
Box2<Real>::Box2 ()
{
// uninitialized
}
//----------------------------------------------------------------------------
template <class Real>
Box2<Real>::Box2 (const Vector2<Real>& rkCenter, const Vector2<Real>* akAxis,
const Real* afExtent)
:
Center(rkCenter)
{
for (int i = 0; i < 2; i++)
{
Axis[i] = akAxis[i];
Extent[i] = afExtent[i];
}
}
//----------------------------------------------------------------------------
template <class Real>
Box2<Real>::Box2 (const Vector2<Real>& rkCenter, const Vector2<Real>& rkAxis0,
const Vector2<Real>& rkAxis1, Real fExtent0, Real fExtent1)
:
Center(rkCenter)
{
Axis[0] = rkAxis0;
Axis[1] = rkAxis1;
Extent[0] = fExtent0;
Extent[1] = fExtent1;
}
//----------------------------------------------------------------------------
template <class Real>
void Box2<Real>::ComputeVertices (Vector2<Real> akVertex[4]) const
{
Vector2<Real> akEAxis[2] =
{
Axis[0]*Extent[0],
Axis[1]*Extent[1]
};

akVertex[0] = Center - akEAxis[0] - akEAxis[1];
akVertex[1] = Center + akEAxis[0] - akEAxis[1];
akVertex[2] = Center + akEAxis[0] + akEAxis[1];
akVertex[3] = Center - akEAxis[0] + akEAxis[1];
}
//----------------------------------------------------------------------------

0 comments on commit 0c6d524

Please sign in to comment.