xref: /aoo41x/main/chart2/source/tools/Scaling.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_chart2.hxx"
30*cdf0e10cSrcweir #include "Scaling.hxx"
31*cdf0e10cSrcweir #include <rtl/math.hxx>
32*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir namespace
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Logarithmic(
38*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LogarithmicScaling" ));
39*cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Exponential(
40*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.ExponentialScaling" ));
41*cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Linear(
42*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LinearScaling" ));
43*cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Power(
44*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.PowerScaling" ));
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Logarithmic(
47*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LogarithmicScaling" ));
48*cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Exponential(
49*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.ExponentialScaling" ));
50*cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Linear(
51*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LinearScaling" ));
52*cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Power(
53*cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.PowerScaling" ));
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir //.............................................................................
57*cdf0e10cSrcweir namespace chart
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir //.............................................................................
60*cdf0e10cSrcweir using namespace ::com::sun::star;
61*cdf0e10cSrcweir using namespace ::com::sun::star::chart2;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir LogarithmicScaling::LogarithmicScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
64*cdf0e10cSrcweir         m_fBase( 10.0 ),
65*cdf0e10cSrcweir         m_fLogOfBase( log( 10.0 ) ),
66*cdf0e10cSrcweir         m_xContext( xContext )
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir LogarithmicScaling::LogarithmicScaling( double fBase ) :
71*cdf0e10cSrcweir         m_fBase( fBase ),
72*cdf0e10cSrcweir         m_fLogOfBase( log( fBase ) )
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir LogarithmicScaling::~LogarithmicScaling()
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     double SAL_CALL
81*cdf0e10cSrcweir LogarithmicScaling::doScaling( double value )
82*cdf0e10cSrcweir     throw (uno::RuntimeException)
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir     double fResult;
85*cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
86*cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
87*cdf0e10cSrcweir     else
88*cdf0e10cSrcweir         fResult = log( value ) / m_fLogOfBase;
89*cdf0e10cSrcweir     return fResult;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     uno::Reference< XScaling > SAL_CALL
93*cdf0e10cSrcweir LogarithmicScaling::getInverseScaling()
94*cdf0e10cSrcweir     throw (uno::RuntimeException)
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     return new ExponentialScaling( m_fBase );
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
100*cdf0e10cSrcweir LogarithmicScaling::getServiceName()
101*cdf0e10cSrcweir     throw (uno::RuntimeException)
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir     return lcl_aServiceName_Logarithmic;
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > LogarithmicScaling::getSupportedServiceNames_Static()
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Logarithmic, 1 );
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
112*cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( LogarithmicScaling, lcl_aServiceName_Logarithmic )
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir // ----------------------------------------
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir ExponentialScaling::ExponentialScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
117*cdf0e10cSrcweir         m_fBase( 10.0 ),
118*cdf0e10cSrcweir         m_xContext( xContext )
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir ExponentialScaling::ExponentialScaling( double fBase ) :
123*cdf0e10cSrcweir         m_fBase( fBase )
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir ExponentialScaling::~ExponentialScaling()
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     double SAL_CALL
132*cdf0e10cSrcweir ExponentialScaling::doScaling( double value )
133*cdf0e10cSrcweir     throw (uno::RuntimeException)
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir     double fResult;
136*cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
137*cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
138*cdf0e10cSrcweir     else
139*cdf0e10cSrcweir         fResult = pow( m_fBase, value );
140*cdf0e10cSrcweir     return fResult;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     uno::Reference< XScaling > SAL_CALL
144*cdf0e10cSrcweir ExponentialScaling::getInverseScaling()
145*cdf0e10cSrcweir     throw (uno::RuntimeException)
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir     return new LogarithmicScaling( m_fBase );
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
151*cdf0e10cSrcweir ExponentialScaling::getServiceName()
152*cdf0e10cSrcweir     throw (uno::RuntimeException)
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir     return lcl_aServiceName_Exponential;
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > ExponentialScaling::getSupportedServiceNames_Static()
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Exponential, 1 );
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
163*cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( ExponentialScaling, lcl_aServiceName_Exponential )
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir // ----------------------------------------
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir LinearScaling::LinearScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
168*cdf0e10cSrcweir         m_fSlope( 1.0 ),
169*cdf0e10cSrcweir         m_fOffset( 0.0 ),
170*cdf0e10cSrcweir         m_xContext( xContext )
171*cdf0e10cSrcweir {}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir LinearScaling::LinearScaling( double fSlope, double fOffset ) :
174*cdf0e10cSrcweir         m_fSlope( fSlope ),
175*cdf0e10cSrcweir         m_fOffset( fOffset )
176*cdf0e10cSrcweir {}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir LinearScaling::~LinearScaling()
179*cdf0e10cSrcweir {}
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir double SAL_CALL LinearScaling::doScaling( double value )
182*cdf0e10cSrcweir     throw (uno::RuntimeException)
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir     double fResult;
185*cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
186*cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
187*cdf0e10cSrcweir     else
188*cdf0e10cSrcweir         fResult = m_fOffset + m_fSlope * value;
189*cdf0e10cSrcweir     return fResult;
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir uno::Reference< XScaling > SAL_CALL
193*cdf0e10cSrcweir     LinearScaling::getInverseScaling()
194*cdf0e10cSrcweir     throw (uno::RuntimeException)
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     // ToDo: ApproxEqual ?
197*cdf0e10cSrcweir     if( m_fSlope == 0 )
198*cdf0e10cSrcweir         throw uno::RuntimeException();
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     return new LinearScaling( 1.0 / m_fSlope, m_fOffset / m_fSlope );
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
204*cdf0e10cSrcweir LinearScaling::getServiceName()
205*cdf0e10cSrcweir     throw (uno::RuntimeException)
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir     return lcl_aServiceName_Linear;
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > LinearScaling::getSupportedServiceNames_Static()
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Linear, 1 );
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
216*cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( LinearScaling, lcl_aServiceName_Linear )
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir // ----------------------------------------
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir PowerScaling::PowerScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
221*cdf0e10cSrcweir         m_fExponent( 10.0 ),
222*cdf0e10cSrcweir         m_xContext( xContext )
223*cdf0e10cSrcweir {}
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir PowerScaling::PowerScaling( double fExponent ) :
226*cdf0e10cSrcweir         m_fExponent( fExponent )
227*cdf0e10cSrcweir {}
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir PowerScaling::~PowerScaling()
230*cdf0e10cSrcweir {}
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir double SAL_CALL PowerScaling::doScaling( double value )
233*cdf0e10cSrcweir     throw (uno::RuntimeException)
234*cdf0e10cSrcweir {
235*cdf0e10cSrcweir     double fResult;
236*cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
237*cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
238*cdf0e10cSrcweir     else
239*cdf0e10cSrcweir         fResult = pow( value, m_fExponent );
240*cdf0e10cSrcweir     return fResult;
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir uno::Reference< XScaling > SAL_CALL
244*cdf0e10cSrcweir     PowerScaling::getInverseScaling()
245*cdf0e10cSrcweir     throw (uno::RuntimeException)
246*cdf0e10cSrcweir {
247*cdf0e10cSrcweir     // ToDo: ApproxEqual ?
248*cdf0e10cSrcweir     if( m_fExponent == 0 )
249*cdf0e10cSrcweir         throw uno::RuntimeException();
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     return new PowerScaling( 1.0 / m_fExponent );
252*cdf0e10cSrcweir }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
255*cdf0e10cSrcweir PowerScaling::getServiceName()
256*cdf0e10cSrcweir     throw (uno::RuntimeException)
257*cdf0e10cSrcweir {
258*cdf0e10cSrcweir     return lcl_aServiceName_Power;
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > PowerScaling::getSupportedServiceNames_Static()
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Power, 1 );
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
267*cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( PowerScaling, lcl_aServiceName_Power )
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir //.............................................................................
270*cdf0e10cSrcweir } //namespace chart
271*cdf0e10cSrcweir //.............................................................................
272