OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved. | 2 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above | 8 * 1. Redistributions of source code must retain the above |
9 * copyright notice, this list of conditions and the following | 9 * copyright notice, this list of conditions and the following |
10 * disclaimer. | 10 * disclaimer. |
(...skipping 30 matching lines...) Expand all Loading... |
41 ShapeInterval(T x1 = 0, T x2 = 0) | 41 ShapeInterval(T x1 = 0, T x2 = 0) |
42 : m_x1(x1) | 42 : m_x1(x1) |
43 , m_x2(x2) | 43 , m_x2(x2) |
44 { | 44 { |
45 ASSERT(x2 >= x1); | 45 ASSERT(x2 >= x1); |
46 } | 46 } |
47 | 47 |
48 T x1() const { return m_x1; } | 48 T x1() const { return m_x1; } |
49 T x2() const { return m_x2; } | 49 T x2() const { return m_x2; } |
50 T width() const { return m_x2 - m_x1; } | 50 T width() const { return m_x2 - m_x1; } |
51 bool isEmpty() const { return m_x1 == m_x2; } | 51 bool isEmpty() const { return !m_x1 && !m_x2; } |
52 | 52 |
53 void setX1(T x1) | 53 void setX1(T x1) |
54 { | 54 { |
55 ASSERT(m_x2 >= x1); | 55 ASSERT(m_x2 >= x1); |
56 m_x1 = x1; | 56 m_x1 = x1; |
57 } | 57 } |
58 | 58 |
59 void setX2(T x2) | 59 void setX2(T x2) |
60 { | 60 { |
61 ASSERT(x2 >= m_x1); | 61 ASSERT(x2 >= m_x1); |
(...skipping 10 matching lines...) Expand all Loading... |
72 bool overlaps(const ShapeInterval<T>& interval) const | 72 bool overlaps(const ShapeInterval<T>& interval) const |
73 { | 73 { |
74 return x2() >= interval.x1() && x1() <= interval.x2(); | 74 return x2() >= interval.x1() && x1() <= interval.x2(); |
75 } | 75 } |
76 | 76 |
77 bool contains(const ShapeInterval<T>& interval) const | 77 bool contains(const ShapeInterval<T>& interval) const |
78 { | 78 { |
79 return x1() <= interval.x1() && x2() >= interval.x2(); | 79 return x1() <= interval.x1() && x2() >= interval.x2(); |
80 } | 80 } |
81 | 81 |
82 ShapeInterval<T> intersect(const ShapeInterval<T>& interval) const | |
83 { | |
84 ASSERT(overlaps(interval)); | |
85 return ShapeInterval<T>(std::max<T>(x1(), interval.x1()), std::min<T>(x2
(), interval.x2())); | |
86 } | |
87 | |
88 typedef Vector<ShapeInterval<T> > ShapeIntervals; | |
89 typedef typename ShapeIntervals::const_iterator const_iterator; | |
90 typedef typename ShapeIntervals::iterator iterator; | |
91 | |
92 static void uniteShapeIntervals(const ShapeIntervals& a, const ShapeInterval
s& b, ShapeIntervals& result) | |
93 { | |
94 ASSERT(shapeIntervalsAreSortedAndDisjoint(a) && shapeIntervalsAreSortedA
ndDisjoint(b)); | |
95 | |
96 if (a.isEmpty() || a == b) { | |
97 result.appendRange(b.begin(), b.end()); | |
98 return; | |
99 } | |
100 | |
101 if (b.isEmpty()) { | |
102 result.appendRange(a.begin(), a.end()); | |
103 return; | |
104 } | |
105 | |
106 const_iterator aNext = a.begin(); | |
107 const_iterator bNext = b.begin(); | |
108 | |
109 while (aNext != a.end() || bNext != b.end()) { | |
110 const_iterator next = (bNext == b.end() || (aNext != a.end() && aNex
t->x1() < bNext->x1())) ? aNext++ : bNext++; | |
111 if (result.isEmpty() || !result.last().overlaps(*next)) | |
112 result.append(*next); | |
113 else | |
114 result.last().setX2(std::max<T>(result.last().x2(), next->x2()))
; | |
115 } | |
116 } | |
117 | |
118 static void intersectShapeIntervals(const ShapeIntervals& a, const ShapeInte
rvals& b, ShapeIntervals& result) | |
119 { | |
120 ASSERT(shapeIntervalsAreSortedAndDisjoint(a) && shapeIntervalsAreSortedA
ndDisjoint(b)); | |
121 | |
122 if (a.isEmpty() || b.isEmpty()) | |
123 return; | |
124 | |
125 if (a == b) { | |
126 result.appendRange(a.begin(), a.end()); | |
127 return; | |
128 } | |
129 | |
130 const_iterator aNext = a.begin(); | |
131 const_iterator bNext = b.begin(); | |
132 const_iterator working = aNext->x1() < bNext->x1() ? aNext++ : bNext++; | |
133 | |
134 while (aNext != a.end() || bNext != b.end()) { | |
135 const_iterator next = (bNext == b.end() || (aNext != a.end() && aNex
t->x1() < bNext->x1())) ? aNext++ : bNext++; | |
136 if (working->overlaps(*next)) { | |
137 result.append(working->intersect(*next)); | |
138 if (next->x2() > working->x2()) | |
139 working = next; | |
140 } else { | |
141 working = next; | |
142 } | |
143 } | |
144 } | |
145 | |
146 static void subtractShapeIntervals(const ShapeIntervals& a, const ShapeInter
vals& b, ShapeIntervals& result) | |
147 { | |
148 ASSERT(shapeIntervalsAreSortedAndDisjoint(a) && shapeIntervalsAreSortedA
ndDisjoint(b)); | |
149 | |
150 if (a.isEmpty() || a == b) | |
151 return; | |
152 | |
153 if (b.isEmpty()) { | |
154 result.appendRange(a.begin(), a.end()); | |
155 return; | |
156 } | |
157 | |
158 const_iterator aNext = a.begin(); | |
159 const_iterator bNext = b.begin(); | |
160 ShapeInterval<T> aValue = *aNext; | |
161 ShapeInterval<T> bValue = *bNext; | |
162 | |
163 do { | |
164 bool aIncrement = false; | |
165 bool bIncrement = false; | |
166 | |
167 if (bValue.contains(aValue)) { | |
168 aIncrement = true; | |
169 } else if (aValue.contains(bValue)) { | |
170 if (bValue.x1() > aValue.x1()) | |
171 result.append(ShapeInterval<T>(aValue.x1(), bValue.x1())); | |
172 if (aValue.x2() > bValue.x2()) | |
173 aValue.setX1(bValue.x2()); | |
174 else | |
175 aIncrement = true; | |
176 bIncrement = true; | |
177 } else if (aValue.overlaps(bValue)) { | |
178 if (aValue.x1() < bValue.x1()) { | |
179 result.append(ShapeInterval<T>(aValue.x1(), bValue.x1())); | |
180 aIncrement = true; | |
181 } else { | |
182 aValue.setX1(bValue.x2()); | |
183 bIncrement = true; | |
184 } | |
185 } else { | |
186 if (aValue.x1() < bValue.x1()) { | |
187 result.append(aValue); | |
188 aIncrement = true; | |
189 } else { | |
190 bIncrement = true; | |
191 } | |
192 } | |
193 | |
194 if (aIncrement && ++aNext != a.end()) | |
195 aValue = *aNext; | |
196 if (bIncrement && ++bNext != b.end()) | |
197 bValue = *bNext; | |
198 | |
199 } while (aNext != a.end() && bNext != b.end()); | |
200 | |
201 if (aNext != a.end()) { | |
202 result.append(aValue); | |
203 result.appendRange(++aNext, a.end()); | |
204 } | |
205 } | |
206 | |
207 bool operator==(const ShapeInterval<T>& other) const { return x1() == other.
x1() && x2() == other.x2(); } | 82 bool operator==(const ShapeInterval<T>& other) const { return x1() == other.
x1() && x2() == other.x2(); } |
208 bool operator!=(const ShapeInterval<T>& other) const { return !operator==(ot
her); } | 83 bool operator!=(const ShapeInterval<T>& other) const { return !operator==(ot
her); } |
209 | 84 |
210 void unite(const ShapeInterval<T>& interval) | 85 void unite(const ShapeInterval<T>& interval) |
211 { | 86 { |
212 if (interval.isEmpty()) | 87 if (interval.isEmpty()) |
213 return; | 88 return; |
214 if (isEmpty()) | 89 if (isEmpty()) |
215 set(interval.x1(), interval.x2()); | 90 set(interval.x1(), interval.x2()); |
216 else | 91 else |
217 set(std::min<T>(x1(), interval.x1()), std::max<T>(x2(), interval.x2(
))); | 92 set(std::min<T>(x1(), interval.x1()), std::max<T>(x2(), interval.x2(
))); |
218 } | 93 } |
219 | 94 |
220 private: | 95 private: |
221 T m_x1; | 96 T m_x1; |
222 T m_x2; | 97 T m_x2; |
223 | |
224 static bool shapeIntervalsAreSortedAndDisjoint(const ShapeIntervals& interva
ls) | |
225 { | |
226 for (unsigned i = 1; i < intervals.size(); i++) { | |
227 if (intervals[i - 1].x2() > intervals[i].x1()) | |
228 return false; | |
229 } | |
230 | |
231 return true; | |
232 } | |
233 }; | 98 }; |
234 | 99 |
235 typedef ShapeInterval<int> IntShapeInterval; | 100 typedef ShapeInterval<int> IntShapeInterval; |
236 typedef ShapeInterval<float> FloatShapeInterval; | 101 typedef ShapeInterval<float> FloatShapeInterval; |
237 | 102 |
238 typedef Vector<IntShapeInterval> IntShapeIntervals; | 103 typedef Vector<IntShapeInterval> IntShapeIntervals; |
239 typedef Vector<FloatShapeInterval> FloatShapeIntervals; | 104 typedef Vector<FloatShapeInterval> FloatShapeIntervals; |
240 | 105 |
241 } // namespace WebCore | 106 } // namespace WebCore |
242 | 107 |
243 #endif // ShapeInterval_h | 108 #endif // ShapeInterval_h |
OLD | NEW |