merge with master and fix 4 conflicts by hand
[ardour.git] / libs / canvas / polygon.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "canvas/polygon.h"
21
22 using namespace ArdourCanvas;
23
24 Polygon::Polygon (Group* parent)
25         : Item (parent)
26         , PolyItem (parent)
27         , Fill (parent)
28         , multiple (0)
29         , constant (0)
30         , cached_size (0)
31 {
32
33 }
34
35 Polygon::~Polygon ()
36 {
37         delete [] multiple;
38         delete [] constant;
39 }
40
41 void
42 Polygon::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
43 {
44         if (_outline || _fill) {
45                 render_path (area, context);
46                 
47                 if (!_points.empty ()) {
48                         /* close path */
49                         Duple p = item_to_window (Duple (_points.front().x, _points.front().y));
50                         context->move_to (p.x, p.y);
51                 }
52
53         }
54
55         if (_outline) {
56                 setup_outline_context (context);
57                 if (_fill) {
58                         context->stroke_preserve ();
59                 } else {
60                         context->stroke ();
61                 }
62         }
63
64         if (_fill) {
65                 setup_fill_context (context);
66                 context->fill ();
67         }
68 }
69
70 void 
71 Polygon::cache_shape_computation () const
72 {
73         Points::size_type npoints = _points.size();
74
75         if (npoints == 0) {
76                 return;
77         }
78
79         Points::size_type i;
80         Points::size_type j = npoints -1;
81
82         if (cached_size < npoints) {
83                 cached_size = npoints;
84                 delete [] multiple;
85                 multiple = new float[cached_size];
86                 delete [] constant;
87                 constant = new float[cached_size];
88         }
89
90         for (i = 0; i < npoints; i++) {
91                 if (_points[j].y == _points[i].y) {
92                         constant[i] = _points[i].x;
93                         multiple[i] = 0; 
94                 } else {
95                         constant[i] = _points[i].x-(_points[i].y*_points[j].x)/(_points[j].y-_points[i].y)+(_points[i].y*_points[i].x)/(_points[j].y-_points[i].y);
96                         multiple[i] = (_points[j].x-_points[i].x)/(_points[j].y-_points[i].y); 
97                 }
98
99                 j = i; 
100         }
101 }
102
103 bool 
104 Polygon::covers (Duple const & point) const
105 {
106         Duple p = canvas_to_item (point);
107
108         Points::size_type npoints = _points.size();
109
110         if (npoints == 0) {
111                 return false;
112         }
113
114         Points::size_type i;
115         Points::size_type j = npoints -1;
116         bool oddNodes = false;
117         
118         if (_bounding_box_dirty) {
119                 compute_bounding_box ();
120         }
121         
122         for (i = 0; i < npoints; i++) {
123                 if (((_points[i].y < p.y && _points[j].y >= p.y) || (_points[j].y < p.y && _points[i].y >= p.y))) {
124                         oddNodes ^= (p.y * multiple[i] + constant[i] < p.x); 
125                 }
126                 j = i; 
127         }
128
129         return oddNodes; 
130
131
132 void
133 Polygon::compute_bounding_box () const
134 {
135         PolyItem::compute_bounding_box ();
136         cache_shape_computation ();
137 }
138