| 1 |
#ifndef _PLANE_H_ |
| 2 |
#define _PLANE_H_ |
| 3 |
#include "basic.h" |
| 4 |
#include <MiscLib/Vector.h> |
| 5 |
#include "PointCloud.h" |
| 6 |
#include <ostream> |
| 7 |
#include <istream> |
| 8 |
#include <stdio.h> |
| 9 |
#include <MiscLib/NoShrinkVector.h> |
| 10 |
#include <GfxTL/Plane.h> |
| 11 |
|
| 12 |
#ifndef DLL_LINKAGE |
| 13 |
#define DLL_LINKAGE |
| 14 |
#endif |
| 15 |
|
| 16 |
class DLL_LINKAGE Plane |
| 17 |
{ |
| 18 |
public: |
| 19 |
enum { RequiredSamples = 1 }; |
| 20 |
Plane() {} |
| 21 |
Plane(Vec3f p1, Vec3f p2, Vec3f p3); |
| 22 |
Plane(Vec3f p1, Vec3f normal); |
| 23 |
virtual ~Plane(); |
| 24 |
bool Init(Vec3f p1, Vec3f p2, Vec3f p3); |
| 25 |
bool Init(const MiscLib::Vector< Vec3f > &samples); |
| 26 |
bool InitAverage(const MiscLib::Vector< Vec3f > &samples); |
| 27 |
bool Init(bool binary, std::istream *i); |
| 28 |
void Init(FILE *i); |
| 29 |
void Init(float* array); |
| 30 |
float getDistance (const Vec3f &pos) const {return fabs(m_dist - m_normal.dot(pos));} |
| 31 |
float Distance(const Vec3f &pos) const { return fabs(m_dist - m_normal.dot(pos)); } |
| 32 |
float SignedDistance(const Vec3f &pos) const { return m_normal.dot(pos) - m_dist; } |
| 33 |
void Normal(const Vec3f &, Vec3f *n) const { *n = m_normal; } |
| 34 |
float DistanceAndNormal(const Vec3f &pos, Vec3f *n) const { *n = m_normal; return Distance(pos); } |
| 35 |
const Vec3f &getNormal () const {return m_normal;} |
| 36 |
const Vec3f &getPosition () const {return m_pos;} |
| 37 |
bool equals (Plane plane); |
| 38 |
float SignedDistToOrigin() const { return m_dist; } |
| 39 |
bool Fit(const PointCloud &pc, |
| 40 |
MiscLib::Vector< size_t >::const_iterator begin, |
| 41 |
MiscLib::Vector< size_t >::const_iterator end) |
| 42 |
{ return LeastSquaresFit(pc, begin, end); } |
| 43 |
bool LeastSquaresFit(const PointCloud &pc, |
| 44 |
MiscLib::Vector< size_t >::const_iterator begin, |
| 45 |
MiscLib::Vector< size_t >::const_iterator end); |
| 46 |
template< class IteratorT > |
| 47 |
bool LeastSquaresFit(IteratorT begin, IteratorT end); |
| 48 |
static bool Interpolate(const MiscLib::Vector< Plane > &planes, |
| 49 |
const MiscLib::Vector< float > &weights, Plane *ip); |
| 50 |
void Serialize(bool binary, std::ostream *o) const; |
| 51 |
static size_t SerializedSize(); |
| 52 |
void Serialize(FILE *o) const; |
| 53 |
void Serialize(float* array) const; |
| 54 |
static size_t SerializedFloatSize(); |
| 55 |
|
| 56 |
void Transform(float scale, const Vec3f &translate); |
| 57 |
float Intersect(const Vec3f &p, const Vec3f &r) const; |
| 58 |
|
| 59 |
protected: |
| 60 |
Vec3f m_normal; |
| 61 |
Vec3f m_pos; |
| 62 |
float m_dist; |
| 63 |
}; |
| 64 |
|
| 65 |
template< class IteratorT > |
| 66 |
bool Plane::LeastSquaresFit(IteratorT begin, IteratorT end) |
| 67 |
{ |
| 68 |
GfxTL::Plane< GfxTL::Vector3Df > pl; |
| 69 |
GfxTL::Vector3Df mean; |
| 70 |
GfxTL::Mean(begin, end, &mean); |
| 71 |
pl.Fit(mean, begin, end); |
| 72 |
*this = Plane(Vec3f(mean.Data()), Vec3f(pl.Normal().Data())); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
#endif //_PLANE_H_ |