workaround segfault in compiler on macos-clang-intel
[LibreOffice.git] / include / basegfx / range / Range2D.hxx
blob57e472a98a4695b03199ef1459a7b5d54ddb8451
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 .
20 #pragma once
22 #include <basegfx/range/basicrange.hxx>
23 #include <basegfx/tuple/Tuple2D.hxx>
25 namespace basegfx
27 template <typename TYPE, typename TRAITS> class Range2D
29 protected:
30 basegfx::BasicRange<TYPE, TRAITS> maRangeX;
31 basegfx::BasicRange<TYPE, TRAITS> maRangeY;
33 public:
34 typedef TYPE ValueType;
35 typedef TRAITS TraitsType;
37 Range2D() = default;
39 /// Create degenerate interval consisting of a single point
40 explicit Range2D(const Tuple2D<TYPE>& rTuple)
41 : maRangeX(rTuple.getX())
42 , maRangeY(rTuple.getY())
46 /// Create proper interval between the two given points
47 Range2D(const Tuple2D<TYPE>& rTuple1, const Tuple2D<TYPE>& rTuple2)
48 : maRangeX(rTuple1.getX())
49 , maRangeY(rTuple1.getY())
51 expand(rTuple2);
54 /// Create proper interval between the two given pairs
55 Range2D(TYPE x1, TYPE y1, TYPE x2, TYPE y2)
56 : maRangeX(x1)
57 , maRangeY(y1)
59 maRangeX.expand(x2);
60 maRangeY.expand(y2);
63 /** Check if the interval set is empty
65 @return false, if no value is in this set - having a
66 single point included will already return true.
68 bool isEmpty() const { return maRangeX.isEmpty() || maRangeY.isEmpty(); }
70 /// reset the object to empty state again, clearing all values
71 void reset()
73 maRangeX.reset();
74 maRangeY.reset();
77 bool operator==(const Range2D& rRange) const
79 return maRangeX == rRange.maRangeX && maRangeY == rRange.maRangeY;
82 bool operator!=(const Range2D& rRange) const
84 return maRangeX != rRange.maRangeX || maRangeY != rRange.maRangeY;
87 bool equal(const Range2D& rRange) const
89 return maRangeX.equal(rRange.maRangeX) && maRangeY.equal(rRange.maRangeY);
92 /// get lower bound of the set. returns arbitrary values for empty sets.
93 TYPE getMinX() const { return maRangeX.getMinimum(); }
95 /// get lower bound of the set. returns arbitrary values for empty sets.
96 TYPE getMinY() const { return maRangeY.getMinimum(); }
98 /// get upper bound of the set. returns arbitrary values for empty sets.
99 TYPE getMaxX() const { return maRangeX.getMaximum(); }
101 /// get upper bound of the set. returns arbitrary values for empty sets.
102 TYPE getMaxY() const { return maRangeY.getMaximum(); }
104 /// return difference between upper and lower X value. returns 0 for empty sets.
105 TYPE getWidth() const { return maRangeX.getRange(); }
107 /// return difference between upper and lower Y value. returns 0 for empty sets.
108 TYPE getHeight() const { return maRangeY.getRange(); }
110 /// return center X value of set. returns 0 for empty sets.
111 double getCenterX() const { return maRangeX.getCenter(); }
113 /// return center Y value of set. returns 0 for empty sets.
114 double getCenterY() const { return maRangeY.getCenter(); }
116 /// yields true if given point is contained in set
117 bool isInside(const Tuple2D<TYPE>& rTuple) const
119 return maRangeX.isInside(rTuple.getX()) && maRangeY.isInside(rTuple.getY());
122 /// yields true if rRange is inside, or equal to set
123 bool isInside(const Range2D& rRange) const
125 return maRangeX.isInside(rRange.maRangeX) && maRangeY.isInside(rRange.maRangeY);
128 /// yields true if rRange at least partly inside set
129 bool overlaps(const Range2D& rRange) const
131 return maRangeX.overlaps(rRange.maRangeX) && maRangeY.overlaps(rRange.maRangeY);
134 /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
135 bool overlapsMore(const Range2D& rRange) const
137 return maRangeX.overlapsMore(rRange.maRangeX) && maRangeY.overlapsMore(rRange.maRangeY);
140 /// add point to the set, expanding as necessary
141 void expand(const Tuple2D<TYPE>& rTuple)
143 maRangeX.expand(rTuple.getX());
144 maRangeY.expand(rTuple.getY());
147 /// add rRange to the set, expanding as necessary
148 void expand(const Range2D& rRange)
150 maRangeX.expand(rRange.maRangeX);
151 maRangeY.expand(rRange.maRangeY);
154 /// calc set intersection
155 void intersect(const Range2D& rRange)
157 maRangeX.intersect(rRange.maRangeX);
158 maRangeY.intersect(rRange.maRangeY);
161 /// grow set by fValue on all sides
162 void grow(TYPE fValue)
164 maRangeX.grow(fValue);
165 maRangeY.grow(fValue);
168 /// grow set by axis aware values from rTuple
169 void grow(const Tuple2D<TYPE>& rTuple)
171 maRangeX.grow(rTuple.getX());
172 maRangeY.grow(rTuple.getY());
175 /// clamp value on range
176 Tuple2D<TYPE> clamp(const Tuple2D<TYPE>& rTuple) const
178 return Tuple2D<TYPE>(maRangeX.clamp(rTuple.getX()), maRangeY.clamp(rTuple.getY()));
182 } // end of namespace basegfx
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */