/src/libreoffice/include/basegfx/tuple/b3dtuple.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #pragma once |
21 | | |
22 | | #include <sal/types.h> |
23 | | #include <basegfx/numeric/ftools.hxx> |
24 | | #include <basegfx/basegfxdllapi.h> |
25 | | #include <basegfx/tuple/Tuple3D.hxx> |
26 | | |
27 | | namespace basegfx |
28 | | { |
29 | | class B3ITuple; |
30 | | |
31 | | /** Base class for all Points/Vectors with three double values |
32 | | |
33 | | This class provides all methods common to Point |
34 | | and Vector classes which are derived from here. |
35 | | |
36 | | @derive Use this class to implement Points or Vectors |
37 | | which are based on three double values |
38 | | */ |
39 | | class SAL_WARN_UNUSED BASEGFX_DLLPUBLIC B3DTuple : public Tuple3D<double> |
40 | | { |
41 | | public: |
42 | | /** Create a 3D Tuple |
43 | | |
44 | | The tuple is initialized to (0.0, 0.0, 0.0) |
45 | | */ |
46 | | B3DTuple() |
47 | 520k | : Tuple3D(0.0, 0.0, 0.0) |
48 | 520k | {} |
49 | | |
50 | | /** Create a 3D Tuple |
51 | | |
52 | | @param fX |
53 | | This parameter is used to initialize the X-coordinate |
54 | | of the 3D Tuple. |
55 | | |
56 | | @param fY |
57 | | This parameter is used to initialize the Y-coordinate |
58 | | of the 3D Tuple. |
59 | | |
60 | | @param fZ |
61 | | This parameter is used to initialize the Z-coordinate |
62 | | of the 3D Tuple. |
63 | | */ |
64 | | B3DTuple(double fX, double fY, double fZ) |
65 | 2.92M | : Tuple3D(fX, fY, fZ) |
66 | 2.92M | {} |
67 | | |
68 | | /// Array-access to 3D Tuple |
69 | | const double& operator[] (int nPos) const |
70 | 0 | { |
71 | 0 | // Here, normally two if(...)'s should be used. In the assumption that |
72 | 0 | // both double members can be accessed as an array a shortcut is used here. |
73 | 0 | // if(0 == nPos) return mnX; if(1 == nPos) return mnY; return mnZ; |
74 | 0 | return *((&mnX) + nPos); |
75 | 0 | } |
76 | | |
77 | | /// Array-access to 3D Tuple |
78 | | double& operator[] (int nPos) |
79 | 0 | { |
80 | | // Here, normally two if(...)'s should be used. In the assumption that |
81 | | // both double members can be accessed as an array a shortcut is used here. |
82 | | // if(0 == nPos) return mnX; if(1 == nPos) return mnY; return mnZ; |
83 | 0 | return *((&mnX) + nPos); |
84 | 0 | } |
85 | | |
86 | | // comparators with tolerance |
87 | | |
88 | | |
89 | | bool equalZero() const |
90 | 0 | { |
91 | 0 | return (this == &getEmptyTuple() || |
92 | 0 | (::basegfx::fTools::equalZero(mnX) |
93 | 0 | && ::basegfx::fTools::equalZero(mnY) |
94 | 0 | && ::basegfx::fTools::equalZero(mnZ))); |
95 | 0 | } |
96 | | |
97 | | bool equal(const B3DTuple& rTup) const |
98 | 2.34k | { |
99 | 2.34k | return ( |
100 | 2.34k | this == &rTup || |
101 | 2.33k | (::basegfx::fTools::equal(mnX, rTup.mnX) && |
102 | 1.84k | ::basegfx::fTools::equal(mnY, rTup.mnY) && |
103 | 10 | ::basegfx::fTools::equal(mnZ, rTup.mnZ))); |
104 | 2.34k | } |
105 | | |
106 | | // operators |
107 | | |
108 | | B3DTuple operator-(void) const |
109 | 0 | { |
110 | 0 | return B3DTuple(-mnX, -mnY, -mnZ); |
111 | 0 | } |
112 | | |
113 | | bool operator==(const B3DTuple& rTup) const |
114 | 270k | { |
115 | 270k | return ::basegfx::fTools::equal(mnX, rTup.mnX) && |
116 | 223k | ::basegfx::fTools::equal(mnY, rTup.mnY) && |
117 | 216k | ::basegfx::fTools::equal(mnZ, rTup.mnZ); |
118 | 270k | } |
119 | | |
120 | 147k | bool operator!=(const B3DTuple& rTup) const { return !operator==(rTup); } |
121 | | |
122 | | void correctValues(const double fCompareValue = 0.0) |
123 | 0 | { |
124 | 0 | if(0.0 == fCompareValue) |
125 | 0 | { |
126 | 0 | if(::basegfx::fTools::equalZero(mnX)) |
127 | 0 | { |
128 | 0 | mnX = 0.0; |
129 | 0 | } |
130 | |
|
131 | 0 | if(::basegfx::fTools::equalZero(mnY)) |
132 | 0 | { |
133 | 0 | mnY = 0.0; |
134 | 0 | } |
135 | |
|
136 | 0 | if(::basegfx::fTools::equalZero(mnZ)) |
137 | 0 | { |
138 | 0 | mnZ = 0.0; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | else |
142 | 0 | { |
143 | 0 | if(::basegfx::fTools::equal(mnX, fCompareValue)) |
144 | 0 | { |
145 | 0 | mnX = fCompareValue; |
146 | 0 | } |
147 | |
|
148 | 0 | if(::basegfx::fTools::equal(mnY, fCompareValue)) |
149 | 0 | { |
150 | 0 | mnY = fCompareValue; |
151 | 0 | } |
152 | |
|
153 | 0 | if(::basegfx::fTools::equal(mnZ, fCompareValue)) |
154 | 0 | { |
155 | 0 | mnZ = fCompareValue; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | static const B3DTuple& getEmptyTuple(); |
161 | | }; |
162 | | |
163 | | // external operators |
164 | | |
165 | | |
166 | | inline B3DTuple interpolate(const B3DTuple& rOld1, const B3DTuple& rOld2, double t) |
167 | 20.3k | { |
168 | 20.3k | if(rOld1 == rOld2) |
169 | 0 | { |
170 | 0 | return rOld1; |
171 | 0 | } |
172 | 20.3k | else if(0.0 >= t) |
173 | 0 | { |
174 | 0 | return rOld1; |
175 | 0 | } |
176 | 20.3k | else if(1.0 <= t) |
177 | 20.3k | { |
178 | 20.3k | return rOld2; |
179 | 20.3k | } |
180 | 0 | else |
181 | 0 | { |
182 | 0 | return B3DTuple( |
183 | 0 | ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(), |
184 | 0 | ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY(), |
185 | 0 | ((rOld2.getZ() - rOld1.getZ()) * t) + rOld1.getZ()); |
186 | 0 | } |
187 | 20.3k | } |
188 | | |
189 | | inline B3DTuple average(const B3DTuple& rOld1, const B3DTuple& rOld2) |
190 | 0 | { |
191 | 0 | return B3DTuple( |
192 | 0 | rtl_math_approxEqual(rOld1.getX(), rOld2.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5, |
193 | 0 | rtl_math_approxEqual(rOld1.getY(), rOld2.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5, |
194 | 0 | rtl_math_approxEqual(rOld1.getZ(), rOld2.getZ()) ? rOld1.getZ() : (rOld1.getZ() + rOld2.getZ()) * 0.5); |
195 | 0 | } |
196 | | |
197 | | inline B3DTuple operator+(const B3DTuple& rTupA, const B3DTuple& rTupB) |
198 | 7.01k | { |
199 | 7.01k | B3DTuple aSum(rTupA); |
200 | 7.01k | aSum += rTupB; |
201 | 7.01k | return aSum; |
202 | 7.01k | } |
203 | | |
204 | | inline B3DTuple operator-(const B3DTuple& rTupA, const B3DTuple& rTupB) |
205 | 35.0k | { |
206 | 35.0k | B3DTuple aSub(rTupA); |
207 | 35.0k | aSub -= rTupB; |
208 | 35.0k | return aSub; |
209 | 35.0k | } |
210 | | |
211 | | inline B3DTuple operator*(const B3DTuple& rTupA, const B3DTuple& rTupB) |
212 | 0 | { |
213 | 0 | B3DTuple aMul(rTupA); |
214 | 0 | aMul *= rTupB; |
215 | 0 | return aMul; |
216 | 0 | } |
217 | | |
218 | | inline B3DTuple operator*(const B3DTuple& rTup, double t) |
219 | 7.01k | { |
220 | 7.01k | B3DTuple aNew(rTup); |
221 | 7.01k | aNew *= t; |
222 | 7.01k | return aNew; |
223 | 7.01k | } |
224 | | |
225 | | inline B3DTuple operator/(const B3DTuple& rTup, double t) |
226 | 0 | { |
227 | 0 | B3DTuple aNew(rTup); |
228 | 0 | aNew /= t; |
229 | 0 | return aNew; |
230 | 0 | } |
231 | | |
232 | | /** Round double to nearest integer for 3D tuple |
233 | | |
234 | | @return the nearest integer for this tuple |
235 | | */ |
236 | | BASEGFX_DLLPUBLIC B3ITuple fround(const B3DTuple& rTup); |
237 | | } // end of namespace basegfx |
238 | | |
239 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |