David's final changes: more profiler features, fixes.
[ipdf/code.git] / src / rect.h
1 #ifndef _RECT_H
2 #define _RECT_H
3
4 #include "common.h"
5 #include "real.h"
6
7 namespace IPDF
8 {
9         template <class T = IPDF::Real>
10         struct TRect
11         {
12                 T x; T y; T w; T h;
13                 //TRect() = default; // Needed so we can fread/fwrite this struct
14                 TRect(T _x=0, T _y=0, T _w=1, T _h=1) : x(_x), y(_y), w(_w), h(_h) {}
15                 template <class B> TRect(const TRect<B> & cpy) : x(T(cpy.x)), y(T(cpy.y)), w(T(cpy.w)), h(T(cpy.h)) {}
16                 
17                 std::string Str() const
18                 {
19                         std::stringstream s;
20                         // float conversion needed because it is fucking impossible to get ostreams working with template classes
21                         s << "{" << x << ", " << y << ", " << (x + w) << ", " << (y + h) << " (w: " << w <<", h: " << h <<")}";
22                         return s.str();
23                 }
24                 inline bool PointIn(T pt_x, T pt_y) const
25                 {
26                         if (pt_x <= x) return false;
27                         if (pt_y <= y) return false;
28                         if (pt_x >= x + w) return false;
29                         if (pt_y >= y + h) return false;
30                         return true;
31                 }
32
33                 inline bool Intersects(const TRect& other) const
34                 {
35                         if (x + w < other.x) return false;
36                         if (y + h < other.y) return false;
37                         if (x > other.x + other.w) return false;
38                         if (y > other.y + other.h) return false;
39                         return true;
40                 }
41
42                 inline bool Contains(const TRect& other) const
43                 {
44                         return PointIn(other.x, other.y) && PointIn(other.x + other.w, other.y + other.h);
45                 }
46                 
47                 template <class B> TRect<B> Convert() const {return TRect<B>(B(x), B(y), B(w), B(h));}
48                 
49                 template <class B> TRect<T> & operator=(const TRect<B> & equ)
50                 {
51                         x = T(equ.x);
52                         y = T(equ.y);
53                         w = T(equ.w);
54                         h = T(equ.h);
55                         return *this;
56                 }
57
58                 // Clips "other" to "this"
59                 inline TRect Clip(const TRect& other) const
60                 {
61                         TRect clipped = other;
62                         if (clipped.x < x)
63                         {
64                                 clipped.w += clipped.x - x;
65                                 clipped.x = x;
66                         }
67                         if (clipped.y < y)
68                         {
69                                 clipped.h += clipped.y - y;
70                                 clipped.y = 0;
71                         }
72                         if (clipped.x + clipped.w > x + w)
73                         {
74                                 clipped.w += ((x + w) - (clipped.x + clipped.w));
75                         }
76                         if (clipped.y + clipped.h > y + h)
77                         {
78                                 clipped.h += ((y + h) - (clipped.y + clipped.h));
79                         }
80                         return clipped;
81                 }
82         };
83
84
85
86         template <class T = IPDF::Real>
87         inline TRect<T> TransformRectCoordinates(const TRect<T> & view, const TRect<T> & r)
88         {
89                 TRect<T> out;
90                 T w = (view.w == T(0))?T(1):view.w;
91                 T h = (view.h == T(0))?T(1):view.h;
92                 out.x = (r.x - view.x) / w; //r.x = out.x *w + view.x
93                 out.y = (r.y - view.y) / h; // r.y = out.y*h + view.y
94                 out.w = r.w / w; // r.w = out.w * w
95                 out.h = r.h / h; // r.h = out.h * h
96                 return out;
97         }
98
99
100         template <class T = IPDF::Real>
101         inline Vec2 TransformPointCoordinates(const TRect<T> & view, const Vec2& v)
102         {
103                 Vec2 out;
104                 out.x = (v.x - view.x) / view.w;
105                 out.y = (v.y - view.y) / view.h;
106                 return out;
107         }
108         
109         typedef TRect<Real> Rect;
110         
111
112 }
113
114 #endif //_RECT_H

UCC git Repository :: git.ucc.asn.au