1c142477cSAndrew Rist /**************************************************************
2*6d8c2cf3Smseidel *
3c142477cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4c142477cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5c142477cSAndrew Rist * distributed with this work for additional information
6c142477cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7c142477cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8c142477cSAndrew Rist * "License"); you may not use this file except in compliance
9c142477cSAndrew Rist * with the License. You may obtain a copy of the License at
10*6d8c2cf3Smseidel *
11c142477cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*6d8c2cf3Smseidel *
13c142477cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14c142477cSAndrew Rist * software distributed under the License is distributed on an
15c142477cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c142477cSAndrew Rist * KIND, either express or implied. See the License for the
17c142477cSAndrew Rist * specific language governing permissions and limitations
18c142477cSAndrew Rist * under the License.
19*6d8c2cf3Smseidel *
20c142477cSAndrew Rist *************************************************************/
21c142477cSAndrew Rist
22c142477cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sdext.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "PresenterGeometryHelper.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <math.h>
30cdf0e10cSrcweir #include <algorithm>
31cdf0e10cSrcweir
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir using namespace ::com::sun::star::uno;
34cdf0e10cSrcweir
35cdf0e10cSrcweir namespace {
36cdf0e10cSrcweir
Right(const awt::Rectangle & rBox)37cdf0e10cSrcweir sal_Int32 Right (const awt::Rectangle& rBox)
38cdf0e10cSrcweir {
39*6d8c2cf3Smseidel return rBox.X + rBox.Width - 1;
40cdf0e10cSrcweir }
41cdf0e10cSrcweir
Bottom(const awt::Rectangle & rBox)42cdf0e10cSrcweir sal_Int32 Bottom (const awt::Rectangle& rBox)
43cdf0e10cSrcweir {
44*6d8c2cf3Smseidel return rBox.Y + rBox.Height - 1;
45cdf0e10cSrcweir }
46cdf0e10cSrcweir
Width(const sal_Int32 nLeft,const sal_Int32 nRight)47cdf0e10cSrcweir sal_Int32 Width (const sal_Int32 nLeft, const sal_Int32 nRight)
48cdf0e10cSrcweir {
49*6d8c2cf3Smseidel return nRight - nLeft + 1;
50cdf0e10cSrcweir }
51cdf0e10cSrcweir
Height(const sal_Int32 nTop,const sal_Int32 nBottom)52cdf0e10cSrcweir sal_Int32 Height (const sal_Int32 nTop, const sal_Int32 nBottom)
53cdf0e10cSrcweir {
54*6d8c2cf3Smseidel return nBottom - nTop + 1;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
57cdf0e10cSrcweir
58cdf0e10cSrcweir } // end of anonymous namespace
59cdf0e10cSrcweir
60cdf0e10cSrcweir
61cdf0e10cSrcweir
62cdf0e10cSrcweir namespace sdext { namespace presenter {
63cdf0e10cSrcweir
Floor(const double nValue)64cdf0e10cSrcweir sal_Int32 PresenterGeometryHelper::Floor (const double nValue)
65cdf0e10cSrcweir {
66*6d8c2cf3Smseidel return sal::static_int_cast<sal_Int32>(floor(nValue));
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
69cdf0e10cSrcweir
70cdf0e10cSrcweir
71cdf0e10cSrcweir
Ceil(const double nValue)72cdf0e10cSrcweir sal_Int32 PresenterGeometryHelper::Ceil (const double nValue)
73cdf0e10cSrcweir {
74*6d8c2cf3Smseidel return sal::static_int_cast<sal_Int32>(ceil(nValue));
75cdf0e10cSrcweir }
76cdf0e10cSrcweir
77cdf0e10cSrcweir
78cdf0e10cSrcweir
79cdf0e10cSrcweir
Round(const double nValue)80cdf0e10cSrcweir sal_Int32 PresenterGeometryHelper::Round (const double nValue)
81cdf0e10cSrcweir {
82*6d8c2cf3Smseidel return sal::static_int_cast<sal_Int32>(floor(0.5 + nValue));
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
85cdf0e10cSrcweir
86cdf0e10cSrcweir
87cdf0e10cSrcweir
ConvertRectangle(const geometry::RealRectangle2D & rBox)88cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::ConvertRectangle (
89*6d8c2cf3Smseidel const geometry::RealRectangle2D& rBox)
90cdf0e10cSrcweir {
91*6d8c2cf3Smseidel const sal_Int32 nLeft (Floor(rBox.X1));
92*6d8c2cf3Smseidel const sal_Int32 nTop (Floor(rBox.Y1));
93*6d8c2cf3Smseidel const sal_Int32 nRight (Ceil(rBox.X2));
94*6d8c2cf3Smseidel const sal_Int32 nBottom (Ceil(rBox.Y2));
95*6d8c2cf3Smseidel return awt::Rectangle (nLeft,nTop,nRight-nLeft,nBottom-nTop);
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
98cdf0e10cSrcweir
99cdf0e10cSrcweir
100cdf0e10cSrcweir
ConvertRectangleWithConstantSize(const geometry::RealRectangle2D & rBox)101cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::ConvertRectangleWithConstantSize (
102*6d8c2cf3Smseidel const geometry::RealRectangle2D& rBox)
103cdf0e10cSrcweir {
104*6d8c2cf3Smseidel return awt::Rectangle (
105*6d8c2cf3Smseidel Round(rBox.X1),
106*6d8c2cf3Smseidel Round(rBox.Y1),
107*6d8c2cf3Smseidel Round(rBox.X2 - rBox.X1),
108*6d8c2cf3Smseidel Round(rBox.Y2 - rBox.Y1));
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
111cdf0e10cSrcweir
112cdf0e10cSrcweir
113cdf0e10cSrcweir
ConvertRectangle(const css::awt::Rectangle & rBox)114cdf0e10cSrcweir geometry::RealRectangle2D PresenterGeometryHelper::ConvertRectangle (
115*6d8c2cf3Smseidel const css::awt::Rectangle& rBox)
116cdf0e10cSrcweir {
117*6d8c2cf3Smseidel return geometry::RealRectangle2D(
118*6d8c2cf3Smseidel rBox.X,
119*6d8c2cf3Smseidel rBox.Y,
120*6d8c2cf3Smseidel rBox.X + rBox.Width,
121*6d8c2cf3Smseidel rBox.Y + rBox.Height);
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
124cdf0e10cSrcweir
125cdf0e10cSrcweir
126*6d8c2cf3Smseidel
TranslateRectangle(const css::awt::Rectangle & rBox,const sal_Int32 nXOffset,const sal_Int32 nYOffset)127cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::TranslateRectangle (
128*6d8c2cf3Smseidel const css::awt::Rectangle& rBox,
129*6d8c2cf3Smseidel const sal_Int32 nXOffset,
130*6d8c2cf3Smseidel const sal_Int32 nYOffset)
131cdf0e10cSrcweir {
132*6d8c2cf3Smseidel return awt::Rectangle(rBox.X + nXOffset, rBox.Y + nYOffset, rBox.Width, rBox.Height);
133cdf0e10cSrcweir }
134*6d8c2cf3Smseidel
135cdf0e10cSrcweir
136cdf0e10cSrcweir
137cdf0e10cSrcweir
Intersection(const css::awt::Rectangle & rBox1,const css::awt::Rectangle & rBox2)138cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::Intersection (
139*6d8c2cf3Smseidel const css::awt::Rectangle& rBox1,
140*6d8c2cf3Smseidel const css::awt::Rectangle& rBox2)
141cdf0e10cSrcweir {
142*6d8c2cf3Smseidel const sal_Int32 nLeft (::std::max(rBox1.X, rBox2.X));
143*6d8c2cf3Smseidel const sal_Int32 nTop (::std::max(rBox1.Y, rBox2.Y));
144*6d8c2cf3Smseidel const sal_Int32 nRight (::std::min(Right(rBox1), Right(rBox2)));
145*6d8c2cf3Smseidel const sal_Int32 nBottom (::std::min(Bottom(rBox1), Bottom(rBox2)));
146*6d8c2cf3Smseidel if (nLeft >= nRight || nTop >= nBottom)
147*6d8c2cf3Smseidel return awt::Rectangle();
148*6d8c2cf3Smseidel else
149*6d8c2cf3Smseidel return awt::Rectangle(nLeft,nTop, Width(nLeft,nRight), Height(nTop,nBottom));
150cdf0e10cSrcweir }
151cdf0e10cSrcweir
152cdf0e10cSrcweir
153cdf0e10cSrcweir
154cdf0e10cSrcweir
Intersection(const geometry::RealRectangle2D & rBox1,const geometry::RealRectangle2D & rBox2)155cdf0e10cSrcweir geometry::RealRectangle2D PresenterGeometryHelper::Intersection (
156*6d8c2cf3Smseidel const geometry::RealRectangle2D& rBox1,
157*6d8c2cf3Smseidel const geometry::RealRectangle2D& rBox2)
158cdf0e10cSrcweir {
159*6d8c2cf3Smseidel const double nLeft (::std::max(rBox1.X1, rBox2.X1));
160*6d8c2cf3Smseidel const double nTop (::std::max(rBox1.Y1, rBox2.Y1));
161*6d8c2cf3Smseidel const double nRight (::std::min(rBox1.X2, rBox2.X2));
162*6d8c2cf3Smseidel const double nBottom (::std::min(rBox1.Y2, rBox2.Y2));
163*6d8c2cf3Smseidel if (nLeft >= nRight || nTop >= nBottom)
164*6d8c2cf3Smseidel return geometry::RealRectangle2D(0,0,0,0);
165*6d8c2cf3Smseidel else
166*6d8c2cf3Smseidel return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom);
167cdf0e10cSrcweir }
168cdf0e10cSrcweir
169cdf0e10cSrcweir
170cdf0e10cSrcweir
171cdf0e10cSrcweir
IsInside(const css::geometry::RealRectangle2D & rBox,const css::geometry::RealPoint2D & rPoint)172cdf0e10cSrcweir bool PresenterGeometryHelper::IsInside (
173*6d8c2cf3Smseidel const css::geometry::RealRectangle2D& rBox,
174*6d8c2cf3Smseidel const css::geometry::RealPoint2D& rPoint)
175cdf0e10cSrcweir {
176*6d8c2cf3Smseidel return rBox.X1 <= rPoint.X
177*6d8c2cf3Smseidel && rBox.Y1 <= rPoint.Y
178*6d8c2cf3Smseidel && rBox.X2 >= rPoint.X
179*6d8c2cf3Smseidel && rBox.Y2 >= rPoint.Y;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir
182cdf0e10cSrcweir
183cdf0e10cSrcweir
184cdf0e10cSrcweir
IsInside(const css::awt::Rectangle & rBox1,const css::awt::Rectangle & rBox2)185cdf0e10cSrcweir bool PresenterGeometryHelper::IsInside (
186*6d8c2cf3Smseidel const css::awt::Rectangle& rBox1,
187*6d8c2cf3Smseidel const css::awt::Rectangle& rBox2)
188cdf0e10cSrcweir {
189*6d8c2cf3Smseidel return rBox1.X >= rBox2.X
190*6d8c2cf3Smseidel && rBox1.Y >= rBox2.Y
191*6d8c2cf3Smseidel && rBox1.X+rBox1.Width <= rBox2.X+rBox2.Width
192*6d8c2cf3Smseidel && rBox1.Y+rBox1.Height <= rBox2.Y+rBox2.Height;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir
195cdf0e10cSrcweir
196cdf0e10cSrcweir
197cdf0e10cSrcweir
Union(const css::awt::Rectangle & rBox1,const css::awt::Rectangle & rBox2)198cdf0e10cSrcweir awt::Rectangle PresenterGeometryHelper::Union (
199*6d8c2cf3Smseidel const css::awt::Rectangle& rBox1,
200*6d8c2cf3Smseidel const css::awt::Rectangle& rBox2)
201cdf0e10cSrcweir {
202*6d8c2cf3Smseidel if (rBox1.Width<=0 || rBox1.Height<=0)
203*6d8c2cf3Smseidel return rBox2;
204*6d8c2cf3Smseidel else if (rBox2.Width<=0 || rBox2.Height<=0)
205*6d8c2cf3Smseidel return rBox1;
206*6d8c2cf3Smseidel
207*6d8c2cf3Smseidel const sal_Int32 nLeft (::std::min(rBox1.X, rBox2.X));
208*6d8c2cf3Smseidel const sal_Int32 nTop (::std::min(rBox1.Y, rBox2.Y));
209*6d8c2cf3Smseidel const sal_Int32 nRight (::std::max(Right(rBox1), Right(rBox2)));
210*6d8c2cf3Smseidel const sal_Int32 nBottom (::std::max(Bottom(rBox1), Bottom(rBox2)));
211*6d8c2cf3Smseidel if (nLeft >= nRight || nTop >= nBottom)
212*6d8c2cf3Smseidel return awt::Rectangle();
213*6d8c2cf3Smseidel else
214*6d8c2cf3Smseidel return awt::Rectangle(nLeft,nTop, Width(nLeft,nRight), Height(nTop,nBottom));
215cdf0e10cSrcweir }
216cdf0e10cSrcweir
217cdf0e10cSrcweir
218cdf0e10cSrcweir
219cdf0e10cSrcweir
Union(const geometry::RealRectangle2D & rBox1,const geometry::RealRectangle2D & rBox2)220cdf0e10cSrcweir geometry::RealRectangle2D PresenterGeometryHelper::Union (
221*6d8c2cf3Smseidel const geometry::RealRectangle2D& rBox1,
222*6d8c2cf3Smseidel const geometry::RealRectangle2D& rBox2)
223cdf0e10cSrcweir {
224*6d8c2cf3Smseidel const double nLeft (::std::min(rBox1.X1, rBox2.X1));
225*6d8c2cf3Smseidel const double nTop (::std::min(rBox1.Y1, rBox2.Y1));
226*6d8c2cf3Smseidel const double nRight (::std::max(rBox1.X2, rBox2.X2));
227*6d8c2cf3Smseidel const double nBottom (::std::max(rBox1.Y2, rBox2.Y2));
228*6d8c2cf3Smseidel if (nLeft >= nRight || nTop >= nBottom)
229*6d8c2cf3Smseidel return geometry::RealRectangle2D(0,0,0,0);
230*6d8c2cf3Smseidel else
231*6d8c2cf3Smseidel return geometry::RealRectangle2D(nLeft,nTop, nRight, nBottom);
232cdf0e10cSrcweir }
233cdf0e10cSrcweir
234cdf0e10cSrcweir
235cdf0e10cSrcweir
236cdf0e10cSrcweir
AreRectanglesDisjoint(const css::awt::Rectangle & rBox1,const css::awt::Rectangle & rBox2)237cdf0e10cSrcweir bool PresenterGeometryHelper::AreRectanglesDisjoint (
238*6d8c2cf3Smseidel const css::awt::Rectangle& rBox1,
239*6d8c2cf3Smseidel const css::awt::Rectangle& rBox2)
240cdf0e10cSrcweir {
241*6d8c2cf3Smseidel return rBox1.X+rBox1.Width <= rBox2.X
242*6d8c2cf3Smseidel || rBox1.Y+rBox1.Height <= rBox2.Y
243*6d8c2cf3Smseidel || rBox1.X >= rBox2.X+rBox2.Width
244*6d8c2cf3Smseidel || rBox1.Y >= rBox2.Y+rBox2.Height;
245cdf0e10cSrcweir }
246cdf0e10cSrcweir
247cdf0e10cSrcweir
248cdf0e10cSrcweir
249cdf0e10cSrcweir
CreatePolygon(const awt::Rectangle & rBox,const Reference<rendering::XGraphicDevice> & rxDevice)250cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
251*6d8c2cf3Smseidel const awt::Rectangle& rBox,
252*6d8c2cf3Smseidel const Reference<rendering::XGraphicDevice>& rxDevice)
253cdf0e10cSrcweir {
254*6d8c2cf3Smseidel if ( ! rxDevice.is())
255*6d8c2cf3Smseidel return NULL;
256*6d8c2cf3Smseidel
257*6d8c2cf3Smseidel Sequence<Sequence<geometry::RealPoint2D> > aPoints(1);
258*6d8c2cf3Smseidel aPoints[0] = Sequence<geometry::RealPoint2D>(4);
259*6d8c2cf3Smseidel aPoints[0][0] = geometry::RealPoint2D(rBox.X, rBox.Y);
260*6d8c2cf3Smseidel aPoints[0][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height);
261*6d8c2cf3Smseidel aPoints[0][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height);
262*6d8c2cf3Smseidel aPoints[0][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y);
263*6d8c2cf3Smseidel Reference<rendering::XLinePolyPolygon2D> xPolygon (
264*6d8c2cf3Smseidel rxDevice->createCompatibleLinePolyPolygon(aPoints));
265*6d8c2cf3Smseidel Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
266*6d8c2cf3Smseidel if (xRectangle.is())
267*6d8c2cf3Smseidel xRectangle->setClosed(0, sal_True);
268*6d8c2cf3Smseidel
269*6d8c2cf3Smseidel return xRectangle;
270cdf0e10cSrcweir }
271cdf0e10cSrcweir
272cdf0e10cSrcweir
273cdf0e10cSrcweir
274cdf0e10cSrcweir
CreatePolygon(const geometry::RealRectangle2D & rBox,const Reference<rendering::XGraphicDevice> & rxDevice)275cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
276*6d8c2cf3Smseidel const geometry::RealRectangle2D& rBox,
277*6d8c2cf3Smseidel const Reference<rendering::XGraphicDevice>& rxDevice)
278cdf0e10cSrcweir {
279*6d8c2cf3Smseidel if ( ! rxDevice.is())
280*6d8c2cf3Smseidel return NULL;
281*6d8c2cf3Smseidel
282*6d8c2cf3Smseidel Sequence<Sequence<geometry::RealPoint2D> > aPoints(1);
283*6d8c2cf3Smseidel aPoints[0] = Sequence<geometry::RealPoint2D>(4);
284*6d8c2cf3Smseidel aPoints[0][0] = geometry::RealPoint2D(rBox.X1, rBox.Y1);
285*6d8c2cf3Smseidel aPoints[0][1] = geometry::RealPoint2D(rBox.X1, rBox.Y2);
286*6d8c2cf3Smseidel aPoints[0][2] = geometry::RealPoint2D(rBox.X2, rBox.Y2);
287*6d8c2cf3Smseidel aPoints[0][3] = geometry::RealPoint2D(rBox.X2, rBox.Y1);
288*6d8c2cf3Smseidel Reference<rendering::XLinePolyPolygon2D> xPolygon (
289*6d8c2cf3Smseidel rxDevice->createCompatibleLinePolyPolygon(aPoints));
290*6d8c2cf3Smseidel Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
291*6d8c2cf3Smseidel if (xRectangle.is())
292*6d8c2cf3Smseidel xRectangle->setClosed(0, sal_True);
293*6d8c2cf3Smseidel
294*6d8c2cf3Smseidel return xRectangle;
295cdf0e10cSrcweir }
296cdf0e10cSrcweir
297cdf0e10cSrcweir
298cdf0e10cSrcweir
299cdf0e10cSrcweir
CreatePolygon(const::std::vector<css::awt::Rectangle> & rBoxes,const Reference<rendering::XGraphicDevice> & rxDevice)300cdf0e10cSrcweir Reference<rendering::XPolyPolygon2D> PresenterGeometryHelper::CreatePolygon(
301*6d8c2cf3Smseidel const ::std::vector<css::awt::Rectangle>& rBoxes,
302*6d8c2cf3Smseidel const Reference<rendering::XGraphicDevice>& rxDevice)
303cdf0e10cSrcweir {
304*6d8c2cf3Smseidel if ( ! rxDevice.is())
305*6d8c2cf3Smseidel return NULL;
306*6d8c2cf3Smseidel
307*6d8c2cf3Smseidel const sal_Int32 nCount (rBoxes.size());
308*6d8c2cf3Smseidel Sequence<Sequence<geometry::RealPoint2D> > aPoints(nCount);
309*6d8c2cf3Smseidel for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
310*6d8c2cf3Smseidel {
311*6d8c2cf3Smseidel const awt::Rectangle& rBox (rBoxes[nIndex]);
312*6d8c2cf3Smseidel aPoints[nIndex] = Sequence<geometry::RealPoint2D>(4);
313*6d8c2cf3Smseidel aPoints[nIndex][0] = geometry::RealPoint2D(rBox.X, rBox.Y);
314*6d8c2cf3Smseidel aPoints[nIndex][1] = geometry::RealPoint2D(rBox.X, rBox.Y+rBox.Height);
315*6d8c2cf3Smseidel aPoints[nIndex][2] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y+rBox.Height);
316*6d8c2cf3Smseidel aPoints[nIndex][3] = geometry::RealPoint2D(rBox.X+rBox.Width, rBox.Y);
317*6d8c2cf3Smseidel }
318*6d8c2cf3Smseidel
319*6d8c2cf3Smseidel Reference<rendering::XLinePolyPolygon2D> xPolygon (
320*6d8c2cf3Smseidel rxDevice->createCompatibleLinePolyPolygon(aPoints));
321*6d8c2cf3Smseidel Reference<rendering::XPolyPolygon2D> xRectangle (xPolygon, UNO_QUERY);
322*6d8c2cf3Smseidel if (xRectangle.is())
323*6d8c2cf3Smseidel for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
324*6d8c2cf3Smseidel xRectangle->setClosed(nIndex, sal_True);
325*6d8c2cf3Smseidel
326*6d8c2cf3Smseidel return xRectangle;
327cdf0e10cSrcweir }
328cdf0e10cSrcweir
329cdf0e10cSrcweir
330cdf0e10cSrcweir } }
331