Compositing on CPU sort of kind of works if we ignore Alpha*
[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;
18         unsigned right;
19         unsigned top;
20         unsigned bottom;
21         
22         for (unsigned i = m_start; i <= m_end; ++i)
23         {
24                 const Rect & objb = objects.bounds[i];
25                 
26                 if (i == m_start || objb.x < xmin)
27                 {
28                         xmin = objb.x;
29                         left = i;
30                 }
31                 if (i == m_start || (objb.x+objb.w) > xmax)
32                 {
33                         xmax = (objb.x+objb.w);
34                         right = i;
35                 }
36                         
37                 if (i == m_start || objb.y < ymin)
38                 {
39                         ymin = objb.y;
40                         top = i;
41                 }
42                 if (i == m_start || (objb.y+objb.h) > ymax)
43                 {
44                         ymax = (objb.y+objb.h);
45                         bottom = i;
46                 }
47                 
48                 // find fill points
49                 Vec2 pt;
50                 // left
51                 pt = Vec2(objb.x, objb.y+objb.h/Real(2));
52                 if (PointInside(objects, pt))
53                         m_fill_points.push_back(pt);
54                 // right
55                 pt = Vec2(objb.x+objb.w, objb.y+objb.h/Real(2));
56                 if (PointInside(objects, pt))
57                         m_fill_points.push_back(pt);
58                 // bottom
59                 pt = Vec2(objb.x+objb.w/Real(2), objb.y+objb.h);
60                 if (PointInside(objects, pt))
61                         m_fill_points.push_back(pt);
62                 // top
63                 pt = Vec2(objb.x+objb.w/Real(2), objb.y);
64                 if (PointInside(objects, pt))
65                         m_fill_points.push_back(pt);
66                         
67                 // topleft
68                 pt = Vec2(objb.x, objb.y);
69                 if (PointInside(objects, pt))
70                         m_fill_points.push_back(pt);
71                 // topright
72                 pt = Vec2(objb.x+objb.w, objb.y);
73                 if (PointInside(objects, pt))
74                         m_fill_points.push_back(pt);
75                 // bottom left
76                 pt = Vec2(objb.x, objb.y+objb.h);
77                 if (PointInside(objects, pt))
78                         m_fill_points.push_back(pt);
79                 // bottom right
80                 pt = Vec2(objb.x+objb.w, objb.y);
81                 if (PointInside(objects, pt))
82                         m_fill_points.push_back(pt);
83                         
84                 // mid
85                 pt = Vec2(objb.x+objb.w/Real(2), objb.y+objb.h/Real(2));
86                 if (PointInside(objects, pt))
87                         m_fill_points.push_back(pt);
88                 
89                 
90         }
91         
92         // Get actual turning point coords of the 4 edge case beziers
93         m_top = objects.beziers[objects.data_indices[top]].ToAbsolute(objects.bounds[top]).GetTop();
94         m_bottom = objects.beziers[objects.data_indices[bottom]].ToAbsolute(objects.bounds[bottom]).GetBottom();
95         m_left = objects.beziers[objects.data_indices[left]].ToAbsolute(objects.bounds[left]).GetLeft();
96         m_right = objects.beziers[objects.data_indices[right]].ToAbsolute(objects.bounds[right]).GetRight();
97         
98         Vec2 pt = (m_top + m_bottom)/2;
99         if (PointInside(objects, pt))
100                 m_fill_points.push_back(pt);
101         pt = (m_left + m_right)/2;
102         if (PointInside(objects, pt))
103                 m_fill_points.push_back(pt);
104         pt = (m_left + m_right + m_top + m_bottom)/4;
105         if (PointInside(objects, pt))
106                 m_fill_points.push_back(pt);
107                 
108 }
109
110
111 bool Path::PointInside(const Objects & objects, const Vec2 & pt, bool debug) const
112 {
113         vector<Vec2> x_ints;
114         vector<Vec2> y_ints;
115         for (unsigned i = m_start; i <= m_end; ++i)
116         {
117                 Bezier bez(objects.beziers[objects.data_indices[i]].ToAbsolute(objects.bounds[i]));
118                 vector<Vec2> xi(bez.SolveX(pt.x));
119                 vector<Vec2> yi(bez.SolveY(pt.y));
120                 x_ints.insert(x_ints.end(), xi.begin(), xi.end());
121                 y_ints.insert(y_ints.end(), yi.begin(), yi.end());
122         }
123         //Debug("Solved for intersections");
124         unsigned bigger = 0;
125         unsigned smaller = 0;
126         for (unsigned i = 0; i < x_ints.size(); ++i)
127         {
128                 if (debug)
129                                 Debug("X Intersection %u at %f,%f vs %f,%f", i,x_ints[i].x, x_ints[i].y, pt.x, pt.y);
130                 if (x_ints[i].y >= pt.y)
131                 {
132                         
133                         ++bigger;
134                 }
135         }
136         smaller = x_ints.size() - bigger;
137         if (debug)
138         {
139                 Debug("%u horizontal, %u bigger, %u smaller", x_ints.size(), bigger, smaller);
140         }
141         if (smaller % 2 == 0 || bigger % 2 == 0)
142                 return false;
143                 
144         bigger = 0;
145         smaller = 0;
146
147         for (unsigned i = 0; i < y_ints.size(); ++i)
148         {
149                 if (debug)
150                                 Debug("Y Intersection %u at %f,%f vs %f,%f", i,x_ints[i].x, x_ints[i].y, pt.x, pt.y);
151                 if (y_ints[i].x >= pt.x)
152                 {
153                         
154                         ++bigger;
155                 }
156         }
157         smaller = y_ints.size() - bigger;
158         if (debug)
159         {
160                 Debug("%u vertical, %u bigger, %u smaller", y_ints.size(), bigger, smaller);
161         }
162         if (smaller % 2 == 0 || bigger % 2 == 0)
163                 return false;
164                 
165                 
166         return true;
167 }
168
169 Rect Path::SolveBounds(const Objects & objects) const
170 {
171                 return Rect(m_left.x, m_top.y, m_right.x-m_left.x, m_bottom.y-m_top.y);
172 }
173
174 }

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