Add #define to transform Object bounds on the fly
[ipdf/code.git] / src / path.cpp
1 #include "ipdf.h"
2 #include "path.h"
3 using namespace std;
4
5 namespace IPDF
6 {
7
8 Path::Path(const Objects & objects, unsigned start, unsigned end, const Colour & fill, const Colour & stroke)
9         : m_start(start), m_end(end), m_fill(fill), m_stroke(stroke)
10 {
11         Real xmin = 0; Real ymin = 0; 
12         Real xmax = 0; Real ymax = 0;
13         
14         // Find the bounds coordinates
15         //  and identify the top left and bottom right objects
16         
17         unsigned left = m_start;
18         unsigned right = m_start;
19         unsigned top = m_start;
20         unsigned bottom = m_start;
21         
22         for (unsigned i = m_start; i <= m_end; ++i)
23         {
24                 if (i >= objects.bounds.size())
25                         break;
26                 const Rect & objb = objects.bounds[i];
27                 
28                 if (i == m_start || objb.x < xmin)
29                 {
30                         xmin = objb.x;
31                         left = i;
32                 }
33                 if (i == m_start || (objb.x+objb.w) > xmax)
34                 {
35                         xmax = (objb.x+objb.w);
36                         right = i;
37                 }
38                         
39                 if (i == m_start || objb.y < ymin)
40                 {
41                         ymin = objb.y;
42                         top = i;
43                 }
44                 if (i == m_start || (objb.y+objb.h) > ymax)
45                 {
46                         ymax = (objb.y+objb.h);
47                         bottom = i;
48                 }       
49         }
50         
51         // Get actual turning point coords of the 4 edge case beziers
52         m_top = objects.beziers[objects.data_indices[top]].ToAbsolute(objects.bounds[top]).GetTop();
53         m_bottom = objects.beziers[objects.data_indices[bottom]].ToAbsolute(objects.bounds[bottom]).GetBottom();
54         m_left = objects.beziers[objects.data_indices[left]].ToAbsolute(objects.bounds[left]).GetLeft();
55         m_right = objects.beziers[objects.data_indices[right]].ToAbsolute(objects.bounds[right]).GetRight();    
56 }
57
58
59 bool Path::PointInside(const Objects & objects, const Vec2 & pt, bool debug) const
60 {
61         vector<Vec2> x_ints;
62         vector<Vec2> y_ints;
63         for (unsigned i = m_start; i <= m_end; ++i)
64         {
65                 Bezier bez(objects.beziers[objects.data_indices[i]].ToAbsolute(objects.bounds[i]));
66                 vector<Vec2> xi(bez.SolveX(pt.x));
67                 vector<Vec2> yi(bez.SolveY(pt.y));
68                 x_ints.insert(x_ints.end(), xi.begin(), xi.end());
69                 y_ints.insert(y_ints.end(), yi.begin(), yi.end());
70         }
71         //Debug("Solved for intersections");
72         unsigned bigger = 0;
73         unsigned smaller = 0;
74         for (unsigned i = 0; i < x_ints.size(); ++i)
75         {
76                 if (debug)
77                                 Debug("X Intersection %u at %f,%f vs %f,%f", i,Double(x_ints[i].x), Double(x_ints[i].y), Double(pt.x), Double(pt.y));
78                 if (x_ints[i].y >= pt.y)
79                 {
80                         
81                         ++bigger;
82                 }
83         }
84         smaller = x_ints.size() - bigger;
85         if (debug)
86         {
87                 Debug("%u horizontal, %u bigger, %u smaller", x_ints.size(), bigger, smaller);
88         }
89         if (smaller % 2 == 0 || bigger % 2 == 0)
90                 return false;
91                 
92         bigger = 0;
93         smaller = 0;
94
95         for (unsigned i = 0; i < y_ints.size(); ++i)
96         {
97                 if (debug)
98                                 Debug("Y Intersection %u at %f,%f vs %f,%f", i,Double(y_ints[i].x), Double(y_ints[i].y), Double(pt.x), Double(pt.y));
99                 if (y_ints[i].x >= pt.x)
100                 {
101                         
102                         ++bigger;
103                 }
104         }
105         smaller = y_ints.size() - bigger;
106         if (debug)
107         {
108                 Debug("%u vertical, %u bigger, %u smaller", y_ints.size(), bigger, smaller);
109         }
110         if (smaller % 2 == 0 || bigger % 2 == 0)
111                 return false;
112                 
113                 
114         return true;
115 }
116
117 vector<Vec2> & Path::FillPoints(const Objects & objects, const View & view)
118 {
119         if (m_fill_points.size() != 0)
120                 return m_fill_points;
121                 
122         
123         for (unsigned i = m_start; i <= m_end; ++i)
124         {
125                 const Rect & objb = objects.bounds[i];
126                 // find fill points
127                 Vec2 pt;
128                 // left
129                 pt = Vec2(objb.x, objb.y+objb.h/Real(2));
130                 if (PointInside(objects, pt))
131                         m_fill_points.push_back(pt);
132                 // right
133                 pt = Vec2(objb.x+objb.w, objb.y+objb.h/Real(2));
134                 if (PointInside(objects, pt))
135                         m_fill_points.push_back(pt);
136                 // bottom
137                 pt = Vec2(objb.x+objb.w/Real(2), objb.y+objb.h);
138                 if (PointInside(objects, pt))
139                         m_fill_points.push_back(pt);
140                 // top
141                 pt = Vec2(objb.x+objb.w/Real(2), objb.y);
142                 if (PointInside(objects, pt))
143                         m_fill_points.push_back(pt);
144                         
145                 // topleft
146                 pt = Vec2(objb.x, objb.y);
147                 if (PointInside(objects, pt))
148                         m_fill_points.push_back(pt);
149                 // topright
150                 pt = Vec2(objb.x+objb.w, objb.y);
151                 if (PointInside(objects, pt))
152                         m_fill_points.push_back(pt);
153                 // bottom left
154                 pt = Vec2(objb.x, objb.y+objb.h);
155                 if (PointInside(objects, pt))
156                         m_fill_points.push_back(pt);
157                 // bottom right
158                 pt = Vec2(objb.x+objb.w, objb.y);
159                 if (PointInside(objects, pt))
160                         m_fill_points.push_back(pt);
161                         
162                 // mid
163                 pt = Vec2(objb.x+objb.w/Real(2), objb.y+objb.h/Real(2));
164                 if (PointInside(objects, pt))
165                         m_fill_points.push_back(pt);
166                 
167                 
168         }
169         
170         // 4 extrema
171         Vec2 pt = (m_top + m_bottom)/2;
172         if (PointInside(objects, pt))
173                 m_fill_points.push_back(pt);
174         pt = (m_left + m_right)/2;
175         if (PointInside(objects, pt))
176                 m_fill_points.push_back(pt);
177         pt = (m_left + m_right + m_top + m_bottom)/4;
178         if (PointInside(objects, pt))
179                 m_fill_points.push_back(pt);
180                 
181         return m_fill_points;
182 }
183
184 Rect Path::SolveBounds(const Objects & objects) const
185 {
186                 return Rect(m_left.x, m_top.y, m_right.x-m_left.x, m_bottom.y-m_top.y);
187 }
188
189 }

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