| 1 |
#ifndef SPHEREPRIMITIVESHAPE_HEADER |
| 2 |
#define SPHEREPRIMITIVESHAPE_HEADER |
| 3 |
#include "BitmapPrimitiveShape.h" |
| 4 |
#include "Sphere.h" |
| 5 |
#include <GfxTL/AABox.h> |
| 6 |
#include "LevMarFunc.h" |
| 7 |
#include <istream> |
| 8 |
#include <GfxTL/MathHelper.h> |
| 9 |
#include <GfxTL/IndexedIterator.h> |
| 10 |
#include <GfxTL/Mean.h> |
| 11 |
#include <GfxTL/Covariance.h> |
| 12 |
#include <GfxTL/Jacobi.h> |
| 13 |
#include "LowStretchSphereParametrization.h" |
| 14 |
|
| 15 |
#ifndef DLL_LINKAGE |
| 16 |
#define DLL_LINKAGE |
| 17 |
#endif |
| 18 |
|
| 19 |
class DLL_LINKAGE SpherePrimitiveShape |
| 20 |
: public BitmapPrimitiveShape |
| 21 |
{ |
| 22 |
public: |
| 23 |
typedef LowStretchSphereParametrization ParametrizationType; |
| 24 |
SpherePrimitiveShape() : m_parametrization(m_sphere), m_minRadius(-std::numeric_limits<float>::infinity()), m_maxRadius(std::numeric_limits<float>::infinity()) {} |
| 25 |
SpherePrimitiveShape(const Sphere &s, float minRadius = -std::numeric_limits<float>::infinity(), float maxRadius = std::numeric_limits<float>::infinity()); |
| 26 |
SpherePrimitiveShape(const SpherePrimitiveShape &sps); |
| 27 |
size_t Identifier() const; |
| 28 |
unsigned int RequiredSamples() const { return Sphere::RequiredSamples; } |
| 29 |
bool Init(bool binary, std::istream *i); |
| 30 |
PrimitiveShape *Clone() const; |
| 31 |
float Distance(const Vec3f &p) const; |
| 32 |
float SignedDistance(const Vec3f &p) const; |
| 33 |
float NormalDeviation(const Vec3f &p, const Vec3f &n) const; |
| 34 |
void DistanceAndNormalDeviation(const Vec3f &p, const Vec3f &n, |
| 35 |
std::pair< float, float > *dn) const; |
| 36 |
void Project(const Vec3f &p, Vec3f *pp) const; |
| 37 |
void Normal(const Vec3f &p, Vec3f *n) const; |
| 38 |
unsigned int ConfidenceTests(unsigned int numTests, float epsilon, |
| 39 |
float normalThresh, float rms, const PointCloud &pc, |
| 40 |
const MiscLib::Vector< size_t > &indices) const; |
| 41 |
void Description(std::string *s) const; |
| 42 |
bool Fit(const PointCloud &pc, float epsilon, float normalThresh, |
| 43 |
MiscLib::Vector< size_t >::const_iterator begin, |
| 44 |
MiscLib::Vector< size_t >::const_iterator end); |
| 45 |
PrimitiveShape *LSFit(const PointCloud &pc, float epsilon, |
| 46 |
float normalThresh, |
| 47 |
MiscLib::Vector< size_t >::const_iterator begin, |
| 48 |
MiscLib::Vector< size_t >::const_iterator end, |
| 49 |
std::pair< size_t, float > *score) const; |
| 50 |
LevMarFunc< float > *SignedDistanceFunc() const; |
| 51 |
void Serialize(std::ostream *o, bool binary = true) const; |
| 52 |
void Deserialize(std::istream *i, bool binary); |
| 53 |
size_t SerializedSize() const; |
| 54 |
virtual void Serialize(float* array) const {m_sphere.Serialize(array);} |
| 55 |
virtual size_t SerializedFloatSize() const {return m_sphere.SerializedFloatSize();} |
| 56 |
|
| 57 |
void Transform(float scale, const Vec3f &translate); |
| 58 |
void Visit(PrimitiveShapeVisitor *visitor) const; |
| 59 |
void SuggestSimplifications(const PointCloud &pc, |
| 60 |
MiscLib::Vector< size_t >::const_iterator begin, |
| 61 |
MiscLib::Vector< size_t >::const_iterator end, float distThresh, |
| 62 |
MiscLib::Vector< MiscLib::RefCountPtr< PrimitiveShape > > *suggestions) const; |
| 63 |
void OptimizeParametrization(const PointCloud &pc, |
| 64 |
MiscLib::Vector< size_t >::const_iterator begin, |
| 65 |
MiscLib::Vector< size_t >::const_iterator end, float epsilon); |
| 66 |
void OptimizeParametrization(const PointCloud &pc, |
| 67 |
size_t begin, size_t end, float epsilon); |
| 68 |
bool Similar(float tolerance, const SpherePrimitiveShape &shape) const; |
| 69 |
const Sphere &Internal() const { return m_sphere; } |
| 70 |
|
| 71 |
// implementation of bitmap primitive shape |
| 72 |
void Parameters(const Vec3f &p, |
| 73 |
std::pair< float, float > *param) const; |
| 74 |
void Parameters(GfxTL::IndexedIterator< MiscLib::Vector< size_t >::iterator, |
| 75 |
PointCloud::const_iterator > begin, |
| 76 |
GfxTL::IndexedIterator< MiscLib::Vector< size_t >::iterator, |
| 77 |
PointCloud::const_iterator > end, |
| 78 |
MiscLib::Vector< std::pair< float, float > > *bmpParams) const; |
| 79 |
void Parameters(GfxTL::IndexedIterator< IndexIterator, |
| 80 |
PointCloud::const_iterator > begin, |
| 81 |
GfxTL::IndexedIterator< IndexIterator, |
| 82 |
PointCloud::const_iterator > end, |
| 83 |
MiscLib::Vector< std::pair< float, float > > *bmpParams) const; |
| 84 |
bool InSpace(float u, float v, Vec3f *p, Vec3f *n) const; |
| 85 |
void BitmapExtent(float epsilon, |
| 86 |
GfxTL::AABox< GfxTL::Vector2Df > *bbox, |
| 87 |
MiscLib::Vector< std::pair< float, float > > *params, |
| 88 |
size_t *uextent, size_t *vextent); |
| 89 |
void InBitmap(const std::pair< float, float > ¶m, |
| 90 |
float epsilon, const GfxTL::AABox< GfxTL::Vector2Df > &bbox, |
| 91 |
size_t uextent, size_t vextent, |
| 92 |
std::pair< int, int > *inBmp) const; |
| 93 |
void WrapBitmap(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, |
| 94 |
float epsilon, bool *uwrap, bool *vwrap) const; |
| 95 |
void WrapComponents(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, |
| 96 |
float epsilon, size_t uextent, size_t vextent, |
| 97 |
MiscLib::Vector< int > *componentImg, |
| 98 |
MiscLib::Vector< std::pair< int, size_t > > *labels) const; |
| 99 |
bool InSpace(size_t u, size_t v, float epsilon, |
| 100 |
const GfxTL::AABox< GfxTL::Vector2Df > &bbox, size_t uextent, |
| 101 |
size_t vextent, Vec3f *p, Vec3f *n) const; |
| 102 |
bool CheckGeneratedShapeWithinLimits(const PointCloud& pc, |
| 103 |
MiscLib::Vector< size_t >::const_iterator begin, |
| 104 |
MiscLib::Vector< size_t >::const_iterator end) override |
| 105 |
{ |
| 106 |
if ((m_sphere.Radius() >= m_minRadius) && (m_sphere.Radius() <= m_maxRadius)) |
| 107 |
{ |
| 108 |
return true; |
| 109 |
} |
| 110 |
return false; |
| 111 |
} |
| 112 |
private: |
| 113 |
template< class IteratorT > |
| 114 |
void ParametersImpl(IteratorT begin, IteratorT end, |
| 115 |
MiscLib::Vector< std::pair< float, float > > *bmpParams) const; |
| 116 |
|
| 117 |
private: |
| 118 |
Sphere m_sphere; |
| 119 |
float m_minRadius; |
| 120 |
float m_maxRadius; |
| 121 |
ParametrizationType m_parametrization; |
| 122 |
}; |
| 123 |
|
| 124 |
template< class IteratorT > |
| 125 |
void SpherePrimitiveShape::ParametersImpl(IteratorT begin, IteratorT end, |
| 126 |
MiscLib::Vector< std::pair< float, float > > *bmpParams) const |
| 127 |
{ |
| 128 |
bmpParams->resize(end - begin); |
| 129 |
size_t j = 0; |
| 130 |
for(IteratorT i = begin; i != end; ++i, ++j) |
| 131 |
m_parametrization.Parameters(*i, &(*bmpParams)[j]); |
| 132 |
} |
| 133 |
|
| 134 |
class DLL_LINKAGE SphereLevMarFunc |
| 135 |
: public LevMarFunc< float > |
| 136 |
{ |
| 137 |
public: |
| 138 |
SphereLevMarFunc(const Sphere &s) |
| 139 |
: m_sphere(s) |
| 140 |
{} |
| 141 |
|
| 142 |
float operator()(const float *x) const |
| 143 |
{ |
| 144 |
return m_sphere.SignedDistance(*((const Vec3f *)x)); |
| 145 |
} |
| 146 |
|
| 147 |
void operator()(const float *x, float *gradient) const |
| 148 |
{ |
| 149 |
m_sphere.Normal(*((const Vec3f *)x), (Vec3f *)gradient); |
| 150 |
} |
| 151 |
|
| 152 |
private: |
| 153 |
Sphere m_sphere; |
| 154 |
}; |
| 155 |
|
| 156 |
#endif |