tensor.h
Go to the documentation of this file.00001 #ifndef __UTIL__TENSOR_HPP__
00002 #define __UTIL__TENSOR_HPP__
00003
00009 #include <config.h>
00010 #include <iostream>
00011 #include <cmath>
00012 #include <util/point.h>
00013
00014 namespace util {
00024 template <class T> class Tensor {
00025 public:
00031 Tensor(const T& s = T(), const T& t = T(), const T& theta = T()) :
00032 s(t), t(s), theta(theta), a(), b(), c(), d()
00033 {
00034 calculateMatrix();
00035 }
00036
00038 ~Tensor () {}
00039
00041 const T& scale_s() {return s;}
00042
00044 const T& scale_t() {return t;}
00045
00047 const T& angle_theta() {return theta;}
00048
00055 void set(const T& s, const T& t, const T& theta) {
00056 this->s = s;
00057 this->t = t;
00058 this->theta = theta;
00059 calculateMatrix();
00060 }
00061
00067 util::Point<T> grow(util::Point<T> p) {
00068 return util::Point<T>(a * p.x() + b * p.y(), c * p.x() + d * p.y());
00069 }
00070
00071 Tensor<T>& operator=(Tensor<T>& tensor) {
00072 theta = tensor.theta;
00073 s = tensor.s;
00074 t = tensor.t;
00075 a = tensor.a;
00076 b = tensor.b;
00077 c = tensor.c;
00078 d = tensor.d;
00079 return *this;
00080 }
00081 private:
00082 void calculateMatrix() {
00083 T sin_theta = std::sin(theta);
00084 T cos_theta = std::cos(theta);
00085 T sin2_theta = sin_theta * sin_theta;
00086 T cos2_theta = cos_theta * cos_theta;
00087 T w = 1.0 / (sin2_theta + cos2_theta);
00088
00089 a = w * (s * sin2_theta + t * cos2_theta);
00090 b = w * (t - s) * sin_theta * cos_theta;
00091 c = b;
00092 d = w * (s * cos2_theta + t * sin2_theta);
00093 }
00094
00095 T s;
00096 T t;
00097 T theta;
00098
00099
00100 T a, b, c, d;
00101 };
00102
00103 template <class T>
00104 std::istream& operator>>(std::istream& is, Tensor<T>& tensor) {
00105 T s = T(), t = T(), theta = T();
00106 is >> std::ws >> s >> std::ws >> t >> std::ws >> theta;
00107 tensor.set(s, t, theta);
00108 return is;
00109 }
00110
00111 template <class T>
00112 std::ostream& operator<<(std::ostream& os, Tensor<T>& tensor) {
00113 os << tensor.scale_s() << ' ' << tensor.scale_t() << ' ' << tensor.angle_theta();
00114 return os;
00115 }
00116 }
00117
00118 #endif