00001
00002 #ifndef INTERPOL_H_INCLUDED
00003 #define INTERPOL_H_INCLUDED
00004
00005 #include <vector>
00006 #include <map>
00007 #include "Tools/Math/LA_Vector.h"
00008 #include "Tools/Streams/InOut.h"
00009
00010 namespace interpol {
00011
00012 class Point
00013 {
00014 public:
00015 Point();
00016 Point(double x, double y);
00017
00018 public:
00019 double x;
00020 double y;
00021 };
00022
00023 bool operator<(const Point& p1, const Point& p2);
00024
00025 class Interpolation;
00026 class LinearInterpolation;
00027 class SplineInterpolation;
00028
00029 class InterpolationFactory
00030 {
00031 public:
00032 enum Type
00033 {
00034 Linear,
00035 Spline
00036 };
00037
00038 public:
00039 static Interpolation* Create(Type type);
00040 static Interpolation* Create(Type type, const Interpolation* pSource);
00041 static Interpolation* Transform(Type type, Interpolation* pSource);
00042 static void Destroy(Interpolation* pInterpolation);
00043
00044 static void Write(Out& out, const Interpolation* pInterpolation);
00045 static Interpolation* Read(In& in);
00046 };
00047
00048 class Interpolation
00049 {
00050 public:
00051 typedef std::map<int, Point>::const_iterator ConstPointIterator;
00052
00053 protected:
00054 Interpolation();
00055 virtual ~Interpolation();
00056
00057 private:
00058 Interpolation(const Interpolation& interpolation) {}
00059 Interpolation& operator=(const Interpolation& interpolation) { return *this; }
00060
00061 public:
00062 virtual int AddPoint(const Point& p);
00063 virtual int AddPoint(double x, double y);
00064 virtual bool MovePoint(int index, const Point& p);
00065 virtual bool RemovePoint(int index);
00066
00067 virtual double GetInterpolatedY(double x) const = 0;
00068
00069 virtual ConstPointIterator GetPointBegin() const;
00070 virtual ConstPointIterator GetPointEnd() const;
00071
00072 virtual InterpolationFactory::Type GetType() const = 0;
00073
00074
00075 protected:
00076 virtual void Assign(const Interpolation& interpolation);
00077 virtual void OnChange() {}
00078
00079 private:
00080 virtual void BuildSortedPoints();
00081
00082 protected:
00083 std::vector<Point> xSortedPoints;
00084
00085 private:
00086 int nextIndex;
00087 std::map<int, Point> points;
00088
00089 friend class InterpolationFactory;
00090 friend Out& operator<<(Out& stream, const Interpolation& interpolation);
00091 friend In& operator>>(In& stream, Interpolation& interpolation);
00092 };
00093
00094 class LinearInterpolation : public Interpolation
00095 {
00096 protected:
00097 LinearInterpolation();
00098 virtual ~LinearInterpolation();
00099
00100 public:
00101 virtual double GetInterpolatedY(double x) const;
00102 virtual InterpolationFactory::Type GetType() const;
00103
00104 friend class InterpolationFactory;
00105 };
00106
00107 class SplineInterpolation : public Interpolation
00108 {
00109 protected:
00110 SplineInterpolation();
00111 virtual ~SplineInterpolation();
00112
00113 public:
00114 virtual double GetInterpolatedY(double x) const;
00115 virtual InterpolationFactory::Type GetType() const;
00116
00117 protected:
00118 virtual void OnChange();
00119
00120 private:
00121 virtual void CalcSplines();
00122
00123 private:
00124 LA::Vector x;
00125 LA::Vector y;
00126 LA::Vector a;
00127 LA::Vector b;
00128 LA::Vector c;
00129 LA::Vector d;
00130
00131 friend class InterpolationFactory;
00132 };
00133
00134 Out& operator<<(Out& stream, const Point& p);
00135 In& operator>>(In& stream, Point& p);
00136 }
00137
00138 #endif
00139