Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/chart2/source/view/inc/PlottingPositionHelper.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
#pragma once
20
21
#include <sal/config.h>
22
23
#include <memory>
24
25
#include <chartview/ExplicitScaleValues.hxx>
26
27
#include <basegfx/range/b2drectangle.hxx>
28
#include <tools/long.hxx>
29
#include <com/sun/star/drawing/Direction3D.hpp>
30
#include <com/sun/star/drawing/Position3D.hpp>
31
#include <basegfx/matrix/b3dhommatrix.hxx>
32
#include <com/sun/star/awt/Point.hpp>
33
#include <com/sun/star/uno/Sequence.hxx>
34
#include <rtl/ref.hxx>
35
#include <svx/unoshape.hxx>
36
37
namespace com::sun::star::drawing { struct HomogenMatrix; }
38
namespace com::sun::star::drawing { struct PolyPolygonShape3D; }
39
40
namespace chart
41
{
42
43
/** allows the transformation of numeric values from one
44
     coordinate-system into another.  Values may be transformed using
45
     any mapping.
46
     This is a non-UNO variant of the css::chart2::XTransformation interface,
47
     but using more efficient calling and returning types.
48
  */
49
class XTransformation2
50
{
51
public:
52
    virtual ~XTransformation2();
53
     /** transforms the given input data tuple, given in the source
54
         coordinate system, according to the internal transformation
55
         rules, into a tuple of transformed coordinates in the
56
         destination coordinate system.
57
58
         <p>Note that both coordinate systems may have different
59
         dimensions, e.g., if a transformation does simply a projection
60
         into a lower-dimensional space.</p>
61
62
         @param aValues a source tuple of data that is to be
63
                transformed.  The length of this sequence must be
64
                equivalent to the dimension of the source coordinate
65
                system.
66
67
         @return the transformed data tuple.  The length of this
68
                 sequence is equal to the dimension of the output
69
                 coordinate system.
70
71
         @throws ::com::sun::star::lang::IllegalArgumentException
72
                if the dimension of the input vector is not equal to the
73
                dimension given in getSourceDimension().
74
      */
75
    virtual css::drawing::Position3D transform(
76
        const css::drawing::Position3D& rSourceValues ) const = 0;
77
    virtual css::drawing::Position3D transform(
78
        const css::uno::Sequence< double >& rSourceValues ) const = 0;
79
};
80
81
82
class PlottingPositionHelper
83
{
84
public:
85
    PlottingPositionHelper();
86
    PlottingPositionHelper( const PlottingPositionHelper& rSource );
87
    virtual ~PlottingPositionHelper();
88
89
    virtual std::unique_ptr<PlottingPositionHelper> clone() const;
90
    std::unique_ptr<PlottingPositionHelper> createSecondaryPosHelper( const ExplicitScaleData& rSecondaryScale );
91
92
    virtual void setTransformationSceneToScreen( const css::drawing::HomogenMatrix& rMatrix);
93
94
    virtual void setScales( std::vector< ExplicitScaleData >&& rScales, bool bSwapXAndYAxis );
95
0
    const std::vector< ExplicitScaleData >& getScales() const { return m_aScales;}
96
97
    //better performance for big data
98
    inline void   setCoordinateSystemResolution( const css::uno::Sequence< sal_Int32 >& rCoordinateSystemResolution );
99
    inline bool   isSameForGivenResolution( double fX, double fY, double fZ
100
                                , double fX2, double fY2, double fZ2 );
101
102
    inline bool   isStrongLowerRequested( sal_Int32 nDimensionIndex ) const;
103
    inline bool   isLogicVisible( double fX, double fY, double fZ ) const;
104
    inline void   doLogicScaling( double* pX, double* pY, double* pZ ) const;
105
    inline void   doUnshiftedLogicScaling( double* pX, double* pY, double* pZ ) const;
106
    inline void   clipLogicValues( double* pX, double* pY, double* pZ ) const;
107
           void   clipScaledLogicValues( double* pX, double* pY, double* pZ ) const;
108
    inline bool   clipYRange( double& rMin, double& rMax ) const;
109
110
    inline void   doLogicScaling( css::drawing::Position3D& rPos ) const;
111
112
    virtual ::chart::XTransformation2*
113
                  getTransformationScaledLogicToScene() const;
114
115
    virtual css::drawing::Position3D
116
            transformLogicToScene( double fX, double fY, double fZ, bool bClip ) const;
117
118
    virtual css::drawing::Position3D
119
            transformScaledLogicToScene( double fX, double fY, double fZ, bool bClip ) const;
120
121
    void    transformScaledLogicToScene( css::drawing::PolyPolygonShape3D& rPoly ) const;
122
    void    transformScaledLogicToScene( std::vector<std::vector<css::drawing::Position3D>>& rPoly ) const;
123
124
    static css::awt::Point transformSceneToScreenPosition(
125
                  const css::drawing::Position3D& rScenePosition3D
126
                , const rtl::Reference<SvxShapeGroupAnyD>& xSceneTarget
127
                , sal_Int32 nDimensionCount );
128
129
    inline double getLogicMinX() const;
130
    inline double getLogicMinY() const;
131
    inline double getLogicMinZ() const;
132
    inline double getLogicMaxX() const;
133
    inline double getLogicMaxY() const;
134
    inline double getLogicMaxZ() const;
135
136
    inline bool isMathematicalOrientationX() const;
137
    inline bool isMathematicalOrientationY() const;
138
    inline bool isMathematicalOrientationZ() const;
139
140
    ::basegfx::B2DRectangle     getScaledLogicClipDoubleRect() const;
141
    css::drawing::Direction3D getScaledLogicWidth() const;
142
143
    inline bool isSwapXAndY() const;
144
145
    bool isPercentY() const;
146
147
    double getBaseValueY() const;
148
149
    inline bool maySkipPointsInRegressionCalculation() const;
150
151
    void setTimeResolution( tools::Long nTimeResolution, const Date& rNullDate );
152
    virtual void setScaledCategoryWidth( double fScaledCategoryWidth );
153
    void AllowShiftXAxisPos( bool bAllowShift );
154
    void AllowShiftZAxisPos( bool bAllowShift );
155
156
protected: //member
157
    std::vector< ExplicitScaleData >  m_aScales;
158
    ::basegfx::B3DHomMatrix             m_aMatrixScreenToScene;
159
160
    //this is calculated based on m_aScales and m_aMatrixScreenToScene
161
    mutable std::unique_ptr< ::chart::XTransformation2 >  m_xTransformationLogicToScene;
162
163
    bool    m_bSwapXAndY;//e.g. true for bar chart and false for column chart
164
165
    sal_Int32 m_nXResolution;
166
    sal_Int32 m_nYResolution;
167
    sal_Int32 m_nZResolution;
168
169
    bool m_bMaySkipPointsInRegressionCalculation;
170
171
    bool m_bDateAxis;
172
    tools::Long m_nTimeResolution;
173
    Date m_aNullDate;
174
175
    double m_fScaledCategoryWidth;
176
    bool   m_bAllowShiftXAxisPos;
177
    bool   m_bAllowShiftZAxisPos;
178
};
179
180
class PolarPlottingPositionHelper : public PlottingPositionHelper
181
{
182
public:
183
    PolarPlottingPositionHelper();
184
    PolarPlottingPositionHelper( const PolarPlottingPositionHelper& rSource );
185
    virtual ~PolarPlottingPositionHelper() override;
186
187
    virtual std::unique_ptr<PlottingPositionHelper> clone() const override;
188
189
    virtual void setTransformationSceneToScreen( const css::drawing::HomogenMatrix& rMatrix) override;
190
    virtual void setScales( std::vector< ExplicitScaleData >&& rScales, bool bSwapXAndYAxis ) override;
191
192
0
    const ::basegfx::B3DHomMatrix& getUnitCartesianToScene() const { return m_aUnitCartesianToScene;}
193
194
    virtual ::chart::XTransformation2*
195
                  getTransformationScaledLogicToScene() const override;
196
197
    //the resulting values provided by the following 3 methods should be used
198
    //for input to the transformation received with
199
    //'getTransformationScaledLogicToScene'
200
201
    /** Given a value in the radius axis scale range, it returns the normalized
202
     *  value.
203
     */
204
    double  transformToRadius( double fLogicValueOnRadiusAxis, bool bDoScaling=true ) const;
205
206
    /** Given a value in the angle axis scale range (e.g. [0,1] for pie charts)
207
     *  this method returns the related angle in degree.
208
     */
209
    double  transformToAngleDegree( double fLogicValueOnAngleAxis, bool bDoScaling=true ) const;
210
211
    /** Given 2 values in the angle axis scale range (e.g. [0,1] for pie charts)
212
     *  this method returns the angle between the 2 values keeping into account
213
     *  the correct axis orientation; (for instance, this method is used for
214
     *  computing the angle width of a pie slice).
215
     */
216
    double  getWidthAngleDegree( double& fStartLogicValueOnAngleAxis, double& fEndLogicValueOnAngleAxis ) const;
217
218
    virtual css::drawing::Position3D
219
            transformLogicToScene( double fX, double fY, double fZ, bool bClip ) const override;
220
    virtual css::drawing::Position3D
221
            transformScaledLogicToScene( double fX, double fY, double fZ, bool bClip ) const override;
222
    css::drawing::Position3D
223
            transformAngleRadiusToScene( double fLogicValueOnAngleAxis, double fLogicValueOnRadiusAxis, double fLogicZ, bool bDoScaling=true ) const;
224
225
    /** Return the scene coordinates of the passed point: this point is
226
     *  described through a normalized cylindrical coordinate system, with an
227
     *  optional offset.
228
     *  (For a standard pie chart the origin of the coordinate system is the
229
     *  pie center; for an of-pie chart the components of the aOffset
230
     *  parameter are not all zero).
231
     */
232
    css::drawing::Position3D
233
            transformUnitCircleToScene( double fUnitAngleDegree
234
                    , double fUnitRadius, double fLogicZ
235
                    , const ::basegfx::B3DVector& aOffset = ::basegfx::B3DVector()) const;
236
237
    using PlottingPositionHelper::transformScaledLogicToScene;
238
239
    double  getOuterLogicRadius() const;
240
241
    inline bool isMathematicalOrientationAngle() const;
242
    inline bool isMathematicalOrientationRadius() const;
243
public:
244
    ///m_bSwapXAndY (inherited): by default the X axis (scale[0]) represents
245
    ///the angle axis and the Y axis (scale[1]) represents the radius axis;
246
    ///when this parameter is true, the opposite happens (this is the case for
247
    ///pie charts).
248
249
    ///Offset for radius axis in absolute logic scaled values (1.0 == 1 category)
250
    ///For a donut, it represents the non-normalized inner radius (see notes for
251
    ///transformToRadius)
252
    double      m_fRadiusOffset;
253
    ///Offset for angle axis in real degree.
254
    ///For a pie it represents the angle offset at which the first slice have to
255
    ///start;
256
    double      m_fAngleDegreeOffset;
257
258
private:
259
    ::basegfx::B3DHomMatrix m_aUnitCartesianToScene;
260
261
    ::basegfx::B3DHomMatrix impl_calculateMatrixUnitCartesianToScene( const ::basegfx::B3DHomMatrix& rMatrixScreenToScene ) const;
262
};
263
264
bool PolarPlottingPositionHelper::isMathematicalOrientationAngle() const
265
0
{
266
0
    const ExplicitScaleData& rScale = m_bSwapXAndY ? m_aScales[1] : m_aScales[2];
267
0
    if( css::chart2::AxisOrientation_MATHEMATICAL==rScale.Orientation )
268
0
        return true;
269
0
    return false;
270
0
}
271
bool PolarPlottingPositionHelper::isMathematicalOrientationRadius() const
272
0
{
273
0
    const ExplicitScaleData& rScale = m_bSwapXAndY ? m_aScales[0] : m_aScales[1];
274
0
    if( css::chart2::AxisOrientation_MATHEMATICAL==rScale.Orientation )
275
0
        return true;
276
0
    return false;
277
0
}
278
279
//better performance for big data
280
void PlottingPositionHelper::setCoordinateSystemResolution( const css::uno::Sequence< sal_Int32 >& rCoordinateSystemResolution )
281
0
{
282
0
    m_nXResolution = 1000;
283
0
    m_nYResolution = 1000;
284
0
    m_nZResolution = 1000;
285
0
    if( rCoordinateSystemResolution.getLength() > 0 )
286
0
        m_nXResolution = rCoordinateSystemResolution[0];
287
0
    if( rCoordinateSystemResolution.getLength() > 1 )
288
0
        m_nYResolution = rCoordinateSystemResolution[1];
289
0
    if( rCoordinateSystemResolution.getLength() > 2 )
290
0
        m_nZResolution = rCoordinateSystemResolution[2];
291
0
}
292
293
bool PlottingPositionHelper::isSameForGivenResolution( double fX, double fY, double fZ
294
                                , double fX2, double fY2, double fZ2 /*these values are all expected tp be scaled already*/ )
295
0
{
296
0
    if( !std::isfinite(fX) || !std::isfinite(fY) || !std::isfinite(fZ)
297
0
        || !std::isfinite(fX2) || !std::isfinite(fY2) || !std::isfinite(fZ2) )
298
0
        return false;
299
300
0
    double fScaledMinX = getLogicMinX();
301
0
    double fScaledMinY = getLogicMinY();
302
0
    double fScaledMinZ = getLogicMinZ();
303
0
    double fScaledMaxX = getLogicMaxX();
304
0
    double fScaledMaxY = getLogicMaxY();
305
0
    double fScaledMaxZ = getLogicMaxZ();
306
307
0
    doLogicScaling( &fScaledMinX, &fScaledMinY, &fScaledMinZ );
308
0
    doLogicScaling( &fScaledMaxX, &fScaledMaxY, &fScaledMaxZ);
309
310
0
    bool bSameX = ( static_cast<sal_Int32>(m_nXResolution*(fX - fScaledMinX)/(fScaledMaxX-fScaledMinX))
311
0
                == static_cast<sal_Int32>(m_nXResolution*(fX2 - fScaledMinX)/(fScaledMaxX-fScaledMinX)) );
312
313
0
    bool bSameY = ( static_cast<sal_Int32>(m_nYResolution*(fY - fScaledMinY)/(fScaledMaxY-fScaledMinY))
314
0
                == static_cast<sal_Int32>(m_nYResolution*(fY2 - fScaledMinY)/(fScaledMaxY-fScaledMinY)) );
315
316
0
    bool bSameZ = ( static_cast<sal_Int32>(m_nZResolution*(fZ - fScaledMinZ)/(fScaledMaxZ-fScaledMinZ))
317
0
                == static_cast<sal_Int32>(m_nZResolution*(fZ2 - fScaledMinZ)/(fScaledMaxZ-fScaledMinZ)) );
318
319
0
    return (bSameX && bSameY && bSameZ);
320
0
}
321
322
bool PlottingPositionHelper::isStrongLowerRequested( sal_Int32 nDimensionIndex ) const
323
0
{
324
0
    if( m_aScales.empty() )
325
0
        return false;
326
0
    if( 0==nDimensionIndex )
327
0
        return m_bAllowShiftXAxisPos && m_aScales[nDimensionIndex].m_bShiftedCategoryPosition;
328
0
    else if( 2==nDimensionIndex )
329
0
        return m_bAllowShiftZAxisPos && m_aScales[nDimensionIndex].m_bShiftedCategoryPosition;
330
0
    return false;
331
0
}
332
333
bool PlottingPositionHelper::isLogicVisible(
334
    double fX, double fY, double fZ ) const
335
0
{
336
0
    return fX >= m_aScales[0].Minimum && ( isStrongLowerRequested(0) ? fX < m_aScales[0].Maximum : fX <= m_aScales[0].Maximum )
337
0
        && fY >= m_aScales[1].Minimum && fY <= m_aScales[1].Maximum
338
0
        && fZ >= m_aScales[2].Minimum && ( isStrongLowerRequested(2) ? fZ < m_aScales[2].Maximum : fZ <= m_aScales[2].Maximum );
339
0
}
340
341
void PlottingPositionHelper::doLogicScaling( double* pX, double* pY, double* pZ ) const
342
0
{
343
0
    if(pX)
344
0
    {
345
0
        if( m_aScales[0].Scaling.is())
346
0
            *pX = m_aScales[0].Scaling->doScaling(*pX);
347
0
        if( m_bAllowShiftXAxisPos && m_aScales[0].m_bShiftedCategoryPosition )
348
0
            (*pX) += m_fScaledCategoryWidth/2.0;
349
0
    }
350
0
    if(pY && m_aScales[1].Scaling.is())
351
0
        *pY = m_aScales[1].Scaling->doScaling(*pY);
352
0
    if(pZ)
353
0
    {
354
0
        if( m_aScales[2].Scaling.is())
355
0
            *pZ = m_aScales[2].Scaling->doScaling(*pZ);
356
0
        if( m_bAllowShiftZAxisPos && m_aScales[2].m_bShiftedCategoryPosition)
357
0
            (*pZ) += 0.5;
358
0
    }
359
0
}
360
361
void PlottingPositionHelper::doUnshiftedLogicScaling( double* pX, double* pY, double* pZ ) const
362
0
{
363
0
    if(pX && m_aScales[0].Scaling.is())
364
0
        *pX = m_aScales[0].Scaling->doScaling(*pX);
365
0
    if(pY && m_aScales[1].Scaling.is())
366
0
        *pY = m_aScales[1].Scaling->doScaling(*pY);
367
0
    if(pZ && m_aScales[2].Scaling.is())
368
0
        *pZ = m_aScales[2].Scaling->doScaling(*pZ);
369
0
}
370
371
void PlottingPositionHelper::doLogicScaling( css::drawing::Position3D& rPos ) const
372
0
{
373
0
    doLogicScaling( &rPos.PositionX, &rPos.PositionY, &rPos.PositionZ );
374
0
}
375
376
void PlottingPositionHelper::clipLogicValues( double* pX, double* pY, double* pZ ) const
377
0
{
378
0
    if(pX)
379
0
    {
380
0
        if( *pX < m_aScales[0].Minimum )
381
0
            *pX = m_aScales[0].Minimum;
382
0
        else if( *pX > m_aScales[0].Maximum )
383
0
            *pX = m_aScales[0].Maximum;
384
0
    }
385
0
    if(pY)
386
0
    {
387
0
        if( *pY < m_aScales[1].Minimum )
388
0
            *pY = m_aScales[1].Minimum;
389
0
        else if( *pY > m_aScales[1].Maximum )
390
0
            *pY = m_aScales[1].Maximum;
391
0
    }
392
0
    if(pZ)
393
0
    {
394
0
        if( *pZ < m_aScales[2].Minimum )
395
0
            *pZ = m_aScales[2].Minimum;
396
0
        else if( *pZ > m_aScales[2].Maximum )
397
0
            *pZ = m_aScales[2].Maximum;
398
0
    }
399
0
}
400
401
inline bool PlottingPositionHelper::clipYRange( double& rMin, double& rMax ) const
402
0
{
403
    //returns true if something remains
404
0
    if( rMin > rMax )
405
0
        std::swap( rMin, rMax );
406
0
    if( rMin > getLogicMaxY() )
407
0
        return false;
408
0
    if( rMax < getLogicMinY() )
409
0
        return false;
410
0
    if( rMin < getLogicMinY() )
411
0
        rMin = getLogicMinY();
412
0
    if( rMax > getLogicMaxY() )
413
0
        rMax = getLogicMaxY();
414
0
    return true;
415
0
}
416
417
inline double PlottingPositionHelper::getLogicMinX() const
418
0
{
419
0
    return m_aScales[0].Minimum;
420
0
}
421
inline double PlottingPositionHelper::getLogicMinY() const
422
0
{
423
0
    return m_aScales[1].Minimum;
424
0
}
425
inline double PlottingPositionHelper::getLogicMinZ() const
426
0
{
427
0
    return m_aScales[2].Minimum;
428
0
}
429
430
inline double PlottingPositionHelper::getLogicMaxX() const
431
0
{
432
0
    return m_aScales[0].Maximum;
433
0
}
434
inline double PlottingPositionHelper::getLogicMaxY() const
435
0
{
436
0
    return m_aScales[1].Maximum;
437
0
}
438
inline double PlottingPositionHelper::getLogicMaxZ() const
439
0
{
440
0
    return m_aScales[2].Maximum;
441
0
}
442
inline bool PlottingPositionHelper::isMathematicalOrientationX() const
443
0
{
444
0
    return css::chart2::AxisOrientation_MATHEMATICAL == m_aScales[0].Orientation;
445
0
}
446
inline bool PlottingPositionHelper::isMathematicalOrientationY() const
447
0
{
448
0
    return css::chart2::AxisOrientation_MATHEMATICAL == m_aScales[1].Orientation;
449
0
}
450
inline bool PlottingPositionHelper::isMathematicalOrientationZ() const
451
0
{
452
0
    return css::chart2::AxisOrientation_MATHEMATICAL == m_aScales[2].Orientation;
453
0
}
454
inline bool PlottingPositionHelper::isSwapXAndY() const
455
0
{
456
0
    return m_bSwapXAndY;
457
0
}
458
inline bool PlottingPositionHelper::maySkipPointsInRegressionCalculation() const
459
0
{
460
0
    return m_bMaySkipPointsInRegressionCalculation;
461
0
}
462
463
} //namespace chart
464
465
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */