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