00001 #ifndef __COMMON_H
00002 #define __COMMON_H
00003
00004 #include<cstdlib>
00005 #include"fv_compiler.h"
00006
00007 namespace FemViewer {
00008
00009 template< class T>
00010 class mfvSingleton : private T {
00011 private:
00012 mfvSingleton(void) {}
00013 ~mfvSingleton(void) {}
00014
00015 public:
00016 static T& instance(void) {
00017 static mfvSingleton<T> obj;
00018 return(obj);
00019 }
00020 };
00021
00022 template< class T>
00023 class mfvSingleton<T*> : private T {
00024 private:
00025 static mfvSingleton<T*> * _objp;
00026 mfvSingleton(void) {
00027 atexit(destroy);
00028 }
00029 ~mfvSingleton(void) {}
00030
00031 public:
00032 static void destroy(void) {
00033 if (_objp) { delete _objp; _objp= NULL; }
00034 }
00035 static T& instance(void) {
00036 if (!_objp) {
00037 _objp = new mfvSingleton<T>();
00038 }
00039 return(*_objp);
00040 }
00041 };
00042
00043 template<class T>
00044 mfvSingleton<T*>* mfvSingleton<T*>::_objp = NULL;
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template<typename T,uint32_t N>
00055 class Vec {
00056 public:
00057 T data[N];
00058 Vec() { memset(data,0x0, sizeof(T) * N); }
00059 Vec(T x) { for (uint32_t i(0);i<N;++i) data[i] = x; }
00060 Vec(const T *x) { for (uint32_t i(0);i<N;++i) data[i] = x[i]; };
00061 };
00062
00063
00064 template<uint32_t D, class C>
00065 class Array {
00066 public:
00067 Array(const Vec<size_t, D> &dim) : dimension(dim), arraySize(1)
00068 {
00069 for (uint32_t i(0); i < D; ++i) arraySize *= dim[i];
00070 data = new C[arraySize];
00071 }
00072 virtual ~Array() { delete [] data; }
00073 void clear() { memset(this->data, 0x0, sizeof(C) * this->arraySize); }
00074 void set(const C &c) { for (uint32_t i = 0; i < arraySize; ++i) data[i] = c; }
00075 Vec<size_t, D> dimension;
00076 size_t arraySize;
00077 C *data;
00078 };
00079
00080 template<class C>
00081 C bilinearInterpolate1(
00082 const double &s, const double &t,
00083 const C &a, const C &b, const C &c, const C &d)
00084 {
00085 C e = a * (1 - s) + b * s;
00086 C f = c * (1 - s) + d * s;
00087 return e * (1 - t) + f * t;
00088 }
00089
00090
00091 template<class C>
00092 class Array2 : public Array<2, C>
00093 {
00094 public:
00095 Array2(const Vec<size_t, 2> &dim) : Array<2, C>(dim) {}
00096 template<typename T> C interpolate(const T &s, const T &t) const
00097 {
00098 assert(s <= 1 && t <= 1);
00099 double x = s * (this->dimension[0] - 1), y = t * (this->dimension[1] - 1);
00100 uint32_t xi = static_cast<uint32_t>(x), yi = static_cast<uint32_t>(y);
00101 double dx = x - xi, dy = y - yi;
00102 const C &a = (*this)[yi][xi];
00103 const C &b = (*this)[yi][std::min(xi + 1, this->dimension[0] - 1)];
00104 const C &c = (*this)[std::min(yi + 1, this->dimension[1] - 1)][xi];
00105 const C &d = (*this)[std::min(yi + 1, this->dimension[1] - 1)][std::min(xi + 1, this->dimension[0] - 1)];
00106 return bilinearInterpolate1<C>(dx, dy, a, b, c, d);
00107 }
00108 C* operator [] (size_t j) { return this->data + j * this->dimension[0]; }
00109 C* operator [] (size_t j) const { return this->data + j * this->dimension[0]; }
00110 };
00111
00112 }
00113
00114
00115
00116 #endif