SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLRect.h
Go to the documentation of this file.
1 /**
2  * \file SLRect.h
3  * \date August 2018
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLRECT_H
11 #define SLRECT_H
12 
13 #include <SL.h>
14 #include <SLGLVertexArrayExt.h>
15 #include <SLVec2.h>
16 #include <SLVec3.h>
17 
18 //-----------------------------------------------------------------------------
19 //! A rectangle template class
20 /*! Defines a rectangle with a top-left corner at x,y measured from top-left
21 of the window and with its width and height. It is used e.g. to draw a
22 selection rectangle in SLSceneView::draw2DGL.
23 */
24 // clang-format off
25 template<class T, class V>
26 class SLRect
27 {
28  public:
29  T x, y, width, height;
30 
31  SLRect () {setZero();}
32  SLRect (const T WIDTH,
33  const T HEIGHT) {x=0;y=0;width=WIDTH;height=HEIGHT;}
34  SLRect (const T X,
35  const T Y,
36  const T WIDTH,
37  const T HEIGHT) {x=X;y=Y;width=WIDTH;height=HEIGHT;}
38  SLRect (const V& tl,
39  const V& br) {x=tl.x; y=tl.y;
40  width=br.x-tl.x;
41  height=br.y-tl.y;}
42  void set (const T X,
43  const T Y,
44  const T WIDTH,
45  const T HEIGHT) {x=X;y=Y;width=WIDTH;height=HEIGHT;}
46  void set (const T v[2]) {x=v[0]; y=v[1];}
47  void set (const V tl,
48  const V br) {tl(tl); br(br);}
49  void setZero () {x=0; y=0; width=0; height=0;}
50 
51  // Component wise compare
52  SLbool operator == (const SLRect& r) const {return (x==r.x && y==r.y && width==r.width && height==r.height);}
53  SLbool operator != (const SLRect& r) const {return (x!=r.x || y!=r.y || width!=r.width || height!=r.height);}
54 
55  // Assign operators
56  SLRect& operator = (const SLRect& r) {x=r.x; y=r.y; width=r.width; height=r.height; return *this;}
57 
58  // Stream output operator
59  friend std::ostream& operator << (std::ostream& output,
60  const SLRect& r){output<<"["<<r.x<<","<<r.y<<","<<r.width<<","<<r.height<<"]"; return output;}
61 
62  // Misc. setters
63  void tl (V v) {x = v.x; y = v.y;} //!< top-left corner
64  void br (V v) {if (v.x < x) x = v.x;
65  if (v.y < y) y = v.y;
66  width = v.x-x; height = v.y-y;} //!< bottom-right corner
67  void setScnd (V v) {if (v.x>x) {width =v.x-x;} else {width+=x-v.x; x=v.x;}
68  if (v.y>y) {height=v.y-y;} else {height+=y-v.y; y=v.y;}}
69 
70  // Misc. getters
71  V tl () {return V(x,y);} //!< top-left corner
72  V br () {return V(x+width,y+height);} //!< bottom-right corner
73  T area () {return width * height;}
74  SLbool isEmpty () const {return (width==0 || height==0);} ;
75  SLbool isZero () {return (x==0 && y==0 && width==0 && height==0);}
76  SLbool contains (T X, T Y) {return (X>=x && X<=x+width && Y>=y && Y<=y+height);}
77  SLbool contains (V v) {return (v.x>=x && v.x<=x+width && v.y>=y && v.y<=y+height);}
78  SLbool contains (const SLRect& r) {return (r.x>=x && r.x<=x+width && r.y>=y && r.y<=y+height && r.width <= width && r.height <= height);}
79 
80  void print (const char* str=nullptr)
81  {
82  if (str)
83  SL_LOG("%s",str);
84  SL_LOG("% 3.1f, % 3.1f, % 3.1f, % 3.1f",x, y, width, height);
85  }
86 
87  // Draw rectangle with OpenGL VAO
88  void drawGL(const SLCol4f& color)
89  {
90  SLVVec3f P;
91  P.push_back(SLVec3f(x, -y, 0));
92  P.push_back(SLVec3f(x+width,-y, 0));
93  P.push_back(SLVec3f(x+width,-y-height, 0));
94  P.push_back(SLVec3f(x ,-y-height, 0));
97  }
98 
99  private:
101 
102 };
103 //-----------------------------------------------------------------------------
106 typedef vector<SLRecti> SLVRecti;
107 typedef vector<SLRectf> SLVRectf;
108 //-----------------------------------------------------------------------------
109 #endif
#define SL_LOG(...)
Definition: SL.h:233
bool SLbool
Definition: SL.h:175
@ PT_lineLoop
Definition: SLGLEnums.h:33
Extension class with functions for quick line & point drawing.
vector< SLRecti > SLVRecti
Definition: SLRect.h:106
SLRect< SLfloat, SLVec2f > SLRectf
Definition: SLRect.h:105
vector< SLRectf > SLVRectf
Definition: SLRect.h:107
SLRect< SLint, SLVec2i > SLRecti
Definition: SLRect.h:104
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
SLGLVertexArray adds Helper Functions for quick Line & Point Drawing.
void generateVertexPos(SLVVec2f *p)
Adds or updates & generates a position vertex attribute for colored line or point drawing.
void drawArrayAsColored(SLGLPrimitiveType primitiveType, SLCol4f color, SLfloat lineOrPointSize=1.0f, SLuint indexFirstVertex=0, SLuint countVertices=0)
Draws the array as the specified primitive with the color.
A rectangle template class.
Definition: SLRect.h:27
T width
Definition: SLRect.h:29
SLRect()
Definition: SLRect.h:31
void tl(V v)
top-left corner
Definition: SLRect.h:63
T y
Definition: SLRect.h:29
SLbool operator!=(const SLRect &r) const
Definition: SLRect.h:53
friend std::ostream & operator<<(std::ostream &output, const SLRect &r)
Definition: SLRect.h:59
void set(const T X, const T Y, const T WIDTH, const T HEIGHT)
Definition: SLRect.h:42
SLbool isEmpty() const
Definition: SLRect.h:74
V tl()
top-left corner
Definition: SLRect.h:71
T x
Definition: SLRect.h:29
void set(const V tl, const V br)
Definition: SLRect.h:47
SLRect(const T WIDTH, const T HEIGHT)
Definition: SLRect.h:32
T area()
Definition: SLRect.h:73
SLbool contains(V v)
Definition: SLRect.h:77
SLbool operator==(const SLRect &r) const
Definition: SLRect.h:52
void setScnd(V v)
Definition: SLRect.h:67
void print(const char *str=nullptr)
Definition: SLRect.h:80
SLbool contains(T X, T Y)
Definition: SLRect.h:76
SLbool contains(const SLRect &r)
Definition: SLRect.h:78
void drawGL(const SLCol4f &color)
Definition: SLRect.h:88
void br(V v)
bottom-right corner
Definition: SLRect.h:64
SLbool isZero()
Definition: SLRect.h:75
SLRect(const T X, const T Y, const T WIDTH, const T HEIGHT)
Definition: SLRect.h:34
SLGLVertexArrayExt _vao
Definition: SLRect.h:100
void set(const T v[2])
Definition: SLRect.h:46
SLRect(const V &tl, const V &br)
Definition: SLRect.h:38
T height
Definition: SLRect.h:29
SLRect & operator=(const SLRect &r)
Definition: SLRect.h:56
V br()
bottom-right corner
Definition: SLRect.h:72
void setZero()
Definition: SLRect.h:49