Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / tools / poly.hxx
blob40a129414a38acd1befc81e9c2ae8c5365f2108f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_TOOLS_POLY_HXX
20 #define INCLUDED_TOOLS_POLY_HXX
22 #include <tools/toolsdllapi.h>
23 #include <tools/gen.hxx>
24 #include <tools/debug.hxx>
25 #include <o3tl/typed_flags_set.hxx>
27 #include <vector>
29 #define POLY_APPEND (0xFFFF)
30 #define POLYPOLY_APPEND (0xFFFF)
32 enum class PolyOptimizeFlags {
33 NONE = 0x0000,
34 OPEN = 0x0001,
35 CLOSE = 0x0002,
36 NO_SAME = 0x0004,
37 REDUCE = 0x0008,
38 EDGES = 0x0010,
40 namespace o3tl
42 template<> struct typed_flags<PolyOptimizeFlags> : is_typed_flags<PolyOptimizeFlags, 0x001f> {};
45 enum class PolyStyle
47 Arc = 1,
48 Pie = 2,
49 Chord = 3
52 enum class PolyFlags : sal_uInt8
54 Normal, // start-/endpoint of a curve or a line
55 Smooth, // smooth transition between curves
56 Control, // control handles of a Bezier curve
57 Symmetric // smooth and symmetrical transition between curves
60 class SvStream;
61 class ImplPolygon;
62 class ImplPolyPolygon;
63 namespace tools { class PolyPolygon; }
65 namespace basegfx
67 class B2DPolygon;
68 class B2DPolyPolygon;
71 namespace tools {
73 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Polygon
75 private:
76 ImplPolygon* mpImplPolygon;
78 TOOLS_DLLPRIVATE inline void ImplMakeUnique();
80 public:
81 static void ImplReduceEdges( tools::Polygon& rPoly, const double& rArea, sal_uInt16 nPercent );
82 void ImplRead( SvStream& rIStream );
83 void ImplWrite( SvStream& rOStream ) const;
85 public:
86 Polygon();
87 Polygon( sal_uInt16 nSize );
88 Polygon( sal_uInt16 nPoints, const Point* pPtAry,
89 const PolyFlags* pFlagAry = nullptr );
90 Polygon( const tools::Rectangle& rRect );
91 Polygon( const tools::Rectangle& rRect,
92 sal_uInt32 nHorzRound, sal_uInt32 nVertRound );
93 Polygon( const Point& rCenter,
94 long nRadX, long nRadY );
95 Polygon( const tools::Rectangle& rBound,
96 const Point& rStart, const Point& rEnd,
97 PolyStyle ePolyStyle = PolyStyle::Arc,
98 bool bWholeCircle = false );
99 Polygon( const Point& rBezPt1, const Point& rCtrlPt1,
100 const Point& rBezPt2, const Point& rCtrlPt2,
101 sal_uInt16 nPoints );
103 Polygon( const tools::Polygon& rPoly );
104 ~Polygon();
106 void SetPoint( const Point& rPt, sal_uInt16 nPos );
107 const Point& GetPoint( sal_uInt16 nPos ) const;
109 void SetFlags( sal_uInt16 nPos, PolyFlags eFlags );
110 PolyFlags GetFlags( sal_uInt16 nPos ) const;
111 bool HasFlags() const;
113 bool IsRect() const;
115 void SetSize( sal_uInt16 nNewSize );
116 sal_uInt16 GetSize() const;
118 void Clear();
120 tools::Rectangle GetBoundRect() const;
121 bool IsInside( const Point& rPt ) const;
122 double CalcDistance( sal_uInt16 nPt1, sal_uInt16 nPt2 );
123 void Clip( const tools::Rectangle& rRect );
124 void Optimize( PolyOptimizeFlags nOptimizeFlags );
126 /** Adaptive subdivision of polygons with curves
128 This method adaptively subdivides bezier arcs within the
129 polygon to straight line segments and returns the resulting
130 polygon.
132 @param rResult
133 The resulting subdivided polygon
135 @param d
136 This parameter controls the amount of subdivision. The
137 original curve is guaranteed to not differ by more than this
138 amount per bezier segment from the subdivided
139 lines. Concretely, if the polygon is in device coordinates and
140 d equals 1.0, then the difference between the subdivided and
141 the original polygon is guaranteed to be smaller than one
142 pixel.
144 void AdaptiveSubdivide( tools::Polygon& rResult, const double d = 1.0 ) const;
145 static Polygon SubdivideBezier( const Polygon& rPoly );
147 void Move( long nHorzMove, long nVertMove );
148 void Translate( const Point& rTrans );
149 void Scale( double fScaleX, double fScaleY );
150 void Rotate( const Point& rCenter, double fSin, double fCos );
151 void Rotate( const Point& rCenter, sal_uInt16 nAngle10 );
153 void Insert( sal_uInt16 nPos, const Point& rPt );
154 void Insert( sal_uInt16 nPos, const tools::Polygon& rPoly );
156 const Point& operator[]( sal_uInt16 nPos ) const { return GetPoint( nPos ); }
157 Point& operator[]( sal_uInt16 nPos );
159 tools::Polygon& operator=( const tools::Polygon& rPoly );
160 tools::Polygon& operator=( tools::Polygon&& rPoly );
161 bool operator==( const tools::Polygon& rPoly ) const;
162 bool operator!=( const tools::Polygon& rPoly ) const
163 { return !(Polygon::operator==( rPoly )); }
164 bool IsEqual( const tools::Polygon& rPoly ) const;
166 // streaming a Polygon does ignore PolyFlags, so use the Write Or Read
167 // method to take care of PolyFlags
168 TOOLS_DLLPUBLIC friend SvStream& ReadPolygon( SvStream& rIStream, tools::Polygon& rPoly );
169 TOOLS_DLLPUBLIC friend SvStream& WritePolygon( SvStream& rOStream, const tools::Polygon& rPoly );
171 void Read( SvStream& rIStream );
172 void Write( SvStream& rOStream ) const;
174 const Point* GetConstPointAry() const;
175 const PolyFlags* GetConstFlagAry() const;
177 // convert to ::basegfx::B2DPolygon and return
178 ::basegfx::B2DPolygon getB2DPolygon() const;
180 // constructor to convert from ::basegfx::B2DPolygon
181 // #i76339# made explicit
182 explicit Polygon(const ::basegfx::B2DPolygon& rPolygon);
186 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC PolyPolygon
188 private:
189 ImplPolyPolygon* mpImplPolyPolygon;
191 enum class PolyClipOp {
192 INTERSECT,
193 UNION
195 TOOLS_DLLPRIVATE void ImplDoOperation( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult, PolyClipOp nOperation ) const;
197 public:
198 PolyPolygon( sal_uInt16 nInitSize = 16, sal_uInt16 nResize = 16 );
199 PolyPolygon( const tools::Polygon& rPoly );
200 PolyPolygon( const tools::PolyPolygon& rPolyPoly );
201 ~PolyPolygon();
203 void Insert( const tools::Polygon& rPoly, sal_uInt16 nPos = POLYPOLY_APPEND );
204 void Remove( sal_uInt16 nPos );
205 void Replace( const Polygon& rPoly, sal_uInt16 nPos );
206 const tools::Polygon& GetObject( sal_uInt16 nPos ) const;
208 bool IsRect() const;
210 void Clear();
212 sal_uInt16 Count() const;
213 tools::Rectangle GetBoundRect() const;
214 void Clip( const tools::Rectangle& rRect );
215 void Optimize( PolyOptimizeFlags nOptimizeFlags );
217 /** Adaptive subdivision of polygons with curves
219 This method adaptively subdivides bezier arcs within the
220 polygon to straight line segments and returns the resulting
221 polygon.
223 @param rResult
224 The resulting subdivided polygon
226 If the polygon is in device coordinates, then the difference between the subdivided and
227 the original polygon is guaranteed to be smaller than one
228 pixel.
230 void AdaptiveSubdivide( tools::PolyPolygon& rResult ) const;
231 static tools::PolyPolygon SubdivideBezier( const tools::PolyPolygon& rPolyPoly );
233 void GetIntersection( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult ) const;
234 void GetUnion( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult ) const;
236 void Move( long nHorzMove, long nVertMove );
237 void Translate( const Point& rTrans );
238 void Scale( double fScaleX, double fScaleY );
239 void Rotate( const Point& rCenter, double fSin, double fCos );
240 void Rotate( const Point& rCenter, sal_uInt16 nAngle10 );
242 const tools::Polygon& operator[]( sal_uInt16 nPos ) const { return GetObject( nPos ); }
243 tools::Polygon& operator[]( sal_uInt16 nPos );
245 tools::PolyPolygon& operator=( const tools::PolyPolygon& rPolyPoly );
246 tools::PolyPolygon& operator=( tools::PolyPolygon&& rPolyPoly );
247 bool operator==( const tools::PolyPolygon& rPolyPoly ) const;
248 bool operator!=( const tools::PolyPolygon& rPolyPoly ) const
249 { return !(PolyPolygon::operator==( rPolyPoly )); }
251 TOOLS_DLLPUBLIC friend SvStream& ReadPolyPolygon( SvStream& rIStream, tools::PolyPolygon& rPolyPoly );
252 TOOLS_DLLPUBLIC friend SvStream& WritePolyPolygon( SvStream& rOStream, const tools::PolyPolygon& rPolyPoly );
254 void Read( SvStream& rIStream );
255 void Write( SvStream& rOStream ) const;
257 // convert to ::basegfx::B2DPolyPolygon and return
258 ::basegfx::B2DPolyPolygon getB2DPolyPolygon() const;
260 // constructor to convert from ::basegfx::B2DPolyPolygon
261 // #i76339# made explicit
262 explicit PolyPolygon(const ::basegfx::B2DPolyPolygon& rPolyPolygon);
265 } /* namespace tools */
267 typedef std::vector< tools::PolyPolygon > PolyPolyVector;
270 template<typename charT, typename traits>
271 inline std::basic_ostream<charT, traits> & operator <<(
272 std::basic_ostream<charT, traits> & stream, const tools::PolyPolygon& rPolyPoly)
274 if (!rPolyPoly.Count())
275 stream << "EMPTY";
276 for (sal_uInt16 i = 0; i < rPolyPoly.Count(); ++i)
277 stream << "[" << i << "] " << rPolyPoly.GetObject(i);
278 return stream;
281 #endif
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */