libvisual  0.5.0
lv_rectangle.cpp
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include "config.h"
25 #include "lv_rectangle.h"
26 #include "lv_common.h"
27 #include "lv_math.h"
28 
29 namespace LV {
30 
31  bool Rect::intersects (Rect const& r) const
32  {
33  if (x > (r.x + r.width - 1))
34  return false;
35 
36  if (y > (r.y + r.height - 1))
37  return false;
38 
39  if (r.x > (x + width - 1))
40  return false;
41 
42  if (r.y > (y + height - 1))
43  return false;
44 
45  return true;
46  }
47 
48  bool Rect::contains (Rect const& r) const
49  {
50  if (r.x < x)
51  return false;
52 
53  if (r.y < y)
54  return false;
55 
56  if ((r.x + r.width) > (x + width))
57  return false;
58 
59  if ((r.y + r.height) > (y + height))
60  return false;
61 
62  return true;
63  }
64 
65  Rect Rect::clip (Rect const& r) const
66  {
67  // Return an empty rectangle
68  if (!intersects (r)) {
69  return Rect ();
70  }
71 
72  auto result = r;
73 
74  // Left, Upper boundries
75  if (r.x < x) {
76  result.width = r.width - (x - r.x);
77  result.x = x;
78  }
79 
80  if (r.y < y) {
81  result.height = r.height - (y - r.y);
82  result.y = y;
83  }
84 
85  // Right, Lower boundries
86  if (result.x + result.width > width)
87  result.width = width - result.x;
88 
89  if (result.y + result.height > height)
90  result.height = height - result.y;
91 
92  return result;
93  }
94 
95  void Rect::denormalize_points (float const* fxlist, float const* fylist, int32_t *xlist, int32_t *ylist, unsigned int size) const
96  {
97  visual_return_if_fail (fxlist != nullptr);
98  visual_return_if_fail (fylist != nullptr);
99  visual_return_if_fail (xlist != nullptr);
100  visual_return_if_fail (ylist != nullptr);
101  visual_return_if_fail (size > 0);
102 
103  visual_math_simd_denorm_floats_to_int32s (xlist, fxlist, width, size);
104  visual_math_simd_denorm_floats_to_int32s (ylist, fylist, height, size);
105  }
106 
107  void Rect::denormalize_points_neg (float const* fxlist, float const* fylist, int32_t *xlist, int32_t *ylist, unsigned int size) const
108  {
109  visual_return_if_fail (fxlist != nullptr);
110  visual_return_if_fail (fylist != nullptr);
111  visual_return_if_fail (xlist != nullptr);
112  visual_return_if_fail (ylist != nullptr);
113  visual_return_if_fail (size > 0);
114 
115  visual_math_simd_denorm_neg_floats_to_int32s (xlist, fxlist, size, width);
116  visual_math_simd_denorm_neg_floats_to_int32s (ylist, fylist, size, height);
117  }
118 
119 } // LV namespace