build fix
[LibreOffice.git] / include / tools / poly.hxx
blob88f2486b60919f30af5d0b39ce4cf086dd62d61a
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 PolyStyle
47 POLY_ARC = 1,
48 POLY_PIE = 2,
49 POLY_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 Rectangle& rRect );
91 Polygon( const Rectangle& rRect,
92 sal_uInt32 nHorzRound, sal_uInt32 nVertRound );
93 Polygon( const Point& rCenter,
94 long nRadX, long nRadY );
95 Polygon( const Rectangle& rBound,
96 const Point& rStart, const Point& rEnd,
97 PolyStyle ePolyStyle = POLY_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 Rectangle GetBoundRect() const;
121 double GetSignedArea() const;
122 bool IsInside( const Point& rPt ) const;
123 bool IsRightOrientated() const;
124 double CalcDistance( sal_uInt16 nPt1, sal_uInt16 nPt2 );
125 void Clip( const Rectangle& rRect );
126 void Optimize( PolyOptimizeFlags nOptimizeFlags );
128 /** Adaptive subdivision of polygons with curves
130 This method adaptively subdivides bezier arcs within the
131 polygon to straight line segments and returns the resulting
132 polygon.
134 @param rResult
135 The resulting subdivided polygon
137 @param d
138 This parameter controls the amount of subdivision. The
139 original curve is guaranteed to not differ by more than this
140 amount per bezier segment from the subdivided
141 lines. Concretely, if the polygon is in device coordinates and
142 d equals 1.0, then the difference between the subdivided and
143 the original polygon is guaranteed to be smaller than one
144 pixel.
146 void AdaptiveSubdivide( tools::Polygon& rResult, const double d = 1.0 ) const;
147 static Polygon SubdivideBezier( const Polygon& rPoly );
149 void Move( long nHorzMove, long nVertMove );
150 void Translate( const Point& rTrans );
151 void Scale( double fScaleX, double fScaleY );
152 void Rotate( const Point& rCenter, double fSin, double fCos );
153 void Rotate( const Point& rCenter, sal_uInt16 nAngle10 );
155 void Insert( sal_uInt16 nPos, const Point& rPt );
156 void Insert( sal_uInt16 nPos, const tools::Polygon& rPoly );
158 const Point& operator[]( sal_uInt16 nPos ) const { return GetPoint( nPos ); }
159 Point& operator[]( sal_uInt16 nPos );
161 tools::Polygon& operator=( const tools::Polygon& rPoly );
162 tools::Polygon& operator=( tools::Polygon&& rPoly );
163 bool operator==( const tools::Polygon& rPoly ) const;
164 bool operator!=( const tools::Polygon& rPoly ) const
165 { return !(Polygon::operator==( rPoly )); }
166 bool IsEqual( const tools::Polygon& rPoly ) const;
168 // streaming a Polygon does ignore PolyFlags, so use the Write Or Read
169 // method to take care of PolyFlags
170 TOOLS_DLLPUBLIC friend SvStream& ReadPolygon( SvStream& rIStream, tools::Polygon& rPoly );
171 TOOLS_DLLPUBLIC friend SvStream& WritePolygon( SvStream& rOStream, const tools::Polygon& rPoly );
173 void Read( SvStream& rIStream );
174 void Write( SvStream& rOStream ) const;
176 Point * GetPointAry();
177 const Point* GetConstPointAry() const;
178 const PolyFlags* GetConstFlagAry() const;
180 // convert to ::basegfx::B2DPolygon and return
181 ::basegfx::B2DPolygon getB2DPolygon() const;
183 // constructor to convert from ::basegfx::B2DPolygon
184 // #i76339# made explicit
185 explicit Polygon(const ::basegfx::B2DPolygon& rPolygon);
189 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC PolyPolygon
191 private:
192 ImplPolyPolygon* mpImplPolyPolygon;
194 enum class PolyClipOp {
195 INTERSECT,
196 UNION,
197 DIFF,
200 TOOLS_DLLPRIVATE void ImplDoOperation( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult, PolyClipOp nOperation ) const;
202 public:
203 PolyPolygon( sal_uInt16 nInitSize = 16, sal_uInt16 nResize = 16 );
204 PolyPolygon( const tools::Polygon& rPoly );
205 PolyPolygon( const tools::PolyPolygon& rPolyPoly );
206 ~PolyPolygon();
208 void Insert( const tools::Polygon& rPoly, sal_uInt16 nPos = POLYPOLY_APPEND );
209 void Remove( sal_uInt16 nPos );
210 void Replace( const Polygon& rPoly, sal_uInt16 nPos );
211 const tools::Polygon& GetObject( sal_uInt16 nPos ) const;
213 bool IsRect() const;
215 void Clear();
217 sal_uInt16 Count() const;
218 Rectangle GetBoundRect() const;
219 void Clip( const Rectangle& rRect );
220 void Optimize( PolyOptimizeFlags nOptimizeFlags );
222 /** Adaptive subdivision of polygons with curves
224 This method adaptively subdivides bezier arcs within the
225 polygon to straight line segments and returns the resulting
226 polygon.
228 @param rResult
229 The resulting subdivided polygon
231 If the polygon is in device coordinates, then the difference between the subdivided and
232 the original polygon is guaranteed to be smaller than one
233 pixel.
235 void AdaptiveSubdivide( tools::PolyPolygon& rResult ) const;
236 static tools::PolyPolygon SubdivideBezier( const tools::PolyPolygon& rPolyPoly );
238 void GetIntersection( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult ) const;
239 void GetUnion( const tools::PolyPolygon& rPolyPoly, tools::PolyPolygon& rResult ) const;
241 void Move( long nHorzMove, long nVertMove );
242 void Translate( const Point& rTrans );
243 void Scale( double fScaleX, double fScaleY );
244 void Rotate( const Point& rCenter, double fSin, double fCos );
245 void Rotate( const Point& rCenter, sal_uInt16 nAngle10 );
247 const tools::Polygon& operator[]( sal_uInt16 nPos ) const { return GetObject( nPos ); }
248 tools::Polygon& operator[]( sal_uInt16 nPos );
250 tools::PolyPolygon& operator=( const tools::PolyPolygon& rPolyPoly );
251 tools::PolyPolygon& operator=( tools::PolyPolygon&& rPolyPoly );
252 bool operator==( const tools::PolyPolygon& rPolyPoly ) const;
253 bool operator!=( const tools::PolyPolygon& rPolyPoly ) const
254 { return !(PolyPolygon::operator==( rPolyPoly )); }
256 TOOLS_DLLPUBLIC friend SvStream& ReadPolyPolygon( SvStream& rIStream, tools::PolyPolygon& rPolyPoly );
257 TOOLS_DLLPUBLIC friend SvStream& WritePolyPolygon( SvStream& rOStream, const tools::PolyPolygon& rPolyPoly );
259 void Read( SvStream& rIStream );
260 void Write( SvStream& rOStream ) const;
262 // convert to ::basegfx::B2DPolyPolygon and return
263 ::basegfx::B2DPolyPolygon getB2DPolyPolygon() const;
265 // constructor to convert from ::basegfx::B2DPolyPolygon
266 // #i76339# made explicit
267 explicit PolyPolygon(const ::basegfx::B2DPolyPolygon& rPolyPolygon);
270 } /* namespace tools */
272 typedef std::vector< tools::PolyPolygon > PolyPolyVector;
275 template<typename charT, typename traits>
276 inline std::basic_ostream<charT, traits> & operator <<(
277 std::basic_ostream<charT, traits> & stream, const tools::PolyPolygon& rPolyPoly)
279 if (!rPolyPoly.Count())
280 stream << "EMPTY";
281 for (sal_uInt16 i = 0; i < rPolyPoly.Count(); ++i)
282 stream << "[" << i << "] " << rPolyPoly.GetObject(i);
283 return stream;
286 #endif
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */