xref: /aoo41x/main/chart2/source/tools/Scaling.cxx (revision cde9e8dc)
1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19*cde9e8dcSAndrew Rist  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "Scaling.hxx"
27cdf0e10cSrcweir #include <rtl/math.hxx>
28cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Logarithmic(
34cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LogarithmicScaling" ));
35cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Exponential(
36cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.ExponentialScaling" ));
37cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Linear(
38cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LinearScaling" ));
39cdf0e10cSrcweir static const ::rtl::OUString lcl_aServiceName_Power(
40cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.PowerScaling" ));
41cdf0e10cSrcweir 
42cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Logarithmic(
43cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LogarithmicScaling" ));
44cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Exponential(
45cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.ExponentialScaling" ));
46cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Linear(
47cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LinearScaling" ));
48cdf0e10cSrcweir static const ::rtl::OUString lcl_aImplementationName_Power(
49cdf0e10cSrcweir     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.PowerScaling" ));
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //.............................................................................
53cdf0e10cSrcweir namespace chart
54cdf0e10cSrcweir {
55cdf0e10cSrcweir //.............................................................................
56cdf0e10cSrcweir using namespace ::com::sun::star;
57cdf0e10cSrcweir using namespace ::com::sun::star::chart2;
58cdf0e10cSrcweir 
LogarithmicScaling(const uno::Reference<uno::XComponentContext> & xContext)59cdf0e10cSrcweir LogarithmicScaling::LogarithmicScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
60cdf0e10cSrcweir         m_fBase( 10.0 ),
61cdf0e10cSrcweir         m_fLogOfBase( log( 10.0 ) ),
62cdf0e10cSrcweir         m_xContext( xContext )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
LogarithmicScaling(double fBase)66cdf0e10cSrcweir LogarithmicScaling::LogarithmicScaling( double fBase ) :
67cdf0e10cSrcweir         m_fBase( fBase ),
68cdf0e10cSrcweir         m_fLogOfBase( log( fBase ) )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
~LogarithmicScaling()72cdf0e10cSrcweir LogarithmicScaling::~LogarithmicScaling()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     double SAL_CALL
doScaling(double value)77cdf0e10cSrcweir LogarithmicScaling::doScaling( double value )
78cdf0e10cSrcweir     throw (uno::RuntimeException)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     double fResult;
81cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
82cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
83cdf0e10cSrcweir     else
84cdf0e10cSrcweir         fResult = log( value ) / m_fLogOfBase;
85cdf0e10cSrcweir     return fResult;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     uno::Reference< XScaling > SAL_CALL
getInverseScaling()89cdf0e10cSrcweir LogarithmicScaling::getInverseScaling()
90cdf0e10cSrcweir     throw (uno::RuntimeException)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     return new ExponentialScaling( m_fBase );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
getServiceName()96cdf0e10cSrcweir LogarithmicScaling::getServiceName()
97cdf0e10cSrcweir     throw (uno::RuntimeException)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     return lcl_aServiceName_Logarithmic;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
getSupportedServiceNames_Static()102cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > LogarithmicScaling::getSupportedServiceNames_Static()
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Logarithmic, 1 );
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
APPHELPER_XSERVICEINFO_IMPL(LogarithmicScaling,lcl_aServiceName_Logarithmic)108cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( LogarithmicScaling, lcl_aServiceName_Logarithmic )
109cdf0e10cSrcweir 
110cdf0e10cSrcweir // ----------------------------------------
111cdf0e10cSrcweir 
112cdf0e10cSrcweir ExponentialScaling::ExponentialScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
113cdf0e10cSrcweir         m_fBase( 10.0 ),
114cdf0e10cSrcweir         m_xContext( xContext )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
ExponentialScaling(double fBase)118cdf0e10cSrcweir ExponentialScaling::ExponentialScaling( double fBase ) :
119cdf0e10cSrcweir         m_fBase( fBase )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
~ExponentialScaling()123cdf0e10cSrcweir ExponentialScaling::~ExponentialScaling()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     double SAL_CALL
doScaling(double value)128cdf0e10cSrcweir ExponentialScaling::doScaling( double value )
129cdf0e10cSrcweir     throw (uno::RuntimeException)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     double fResult;
132cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
133cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
134cdf0e10cSrcweir     else
135cdf0e10cSrcweir         fResult = pow( m_fBase, value );
136cdf0e10cSrcweir     return fResult;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     uno::Reference< XScaling > SAL_CALL
getInverseScaling()140cdf0e10cSrcweir ExponentialScaling::getInverseScaling()
141cdf0e10cSrcweir     throw (uno::RuntimeException)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     return new LogarithmicScaling( m_fBase );
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
getServiceName()147cdf0e10cSrcweir ExponentialScaling::getServiceName()
148cdf0e10cSrcweir     throw (uno::RuntimeException)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir     return lcl_aServiceName_Exponential;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
getSupportedServiceNames_Static()153cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > ExponentialScaling::getSupportedServiceNames_Static()
154cdf0e10cSrcweir {
155cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Exponential, 1 );
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
APPHELPER_XSERVICEINFO_IMPL(ExponentialScaling,lcl_aServiceName_Exponential)159cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( ExponentialScaling, lcl_aServiceName_Exponential )
160cdf0e10cSrcweir 
161cdf0e10cSrcweir // ----------------------------------------
162cdf0e10cSrcweir 
163cdf0e10cSrcweir LinearScaling::LinearScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
164cdf0e10cSrcweir         m_fSlope( 1.0 ),
165cdf0e10cSrcweir         m_fOffset( 0.0 ),
166cdf0e10cSrcweir         m_xContext( xContext )
167cdf0e10cSrcweir {}
168cdf0e10cSrcweir 
LinearScaling(double fSlope,double fOffset)169cdf0e10cSrcweir LinearScaling::LinearScaling( double fSlope, double fOffset ) :
170cdf0e10cSrcweir         m_fSlope( fSlope ),
171cdf0e10cSrcweir         m_fOffset( fOffset )
172cdf0e10cSrcweir {}
173cdf0e10cSrcweir 
~LinearScaling()174cdf0e10cSrcweir LinearScaling::~LinearScaling()
175cdf0e10cSrcweir {}
176cdf0e10cSrcweir 
doScaling(double value)177cdf0e10cSrcweir double SAL_CALL LinearScaling::doScaling( double value )
178cdf0e10cSrcweir     throw (uno::RuntimeException)
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     double fResult;
181cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
182cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
183cdf0e10cSrcweir     else
184cdf0e10cSrcweir         fResult = m_fOffset + m_fSlope * value;
185cdf0e10cSrcweir     return fResult;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir uno::Reference< XScaling > SAL_CALL
getInverseScaling()189cdf0e10cSrcweir     LinearScaling::getInverseScaling()
190cdf0e10cSrcweir     throw (uno::RuntimeException)
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     // ToDo: ApproxEqual ?
193cdf0e10cSrcweir     if( m_fSlope == 0 )
194cdf0e10cSrcweir         throw uno::RuntimeException();
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     return new LinearScaling( 1.0 / m_fSlope, m_fOffset / m_fSlope );
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
getServiceName()200cdf0e10cSrcweir LinearScaling::getServiceName()
201cdf0e10cSrcweir     throw (uno::RuntimeException)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     return lcl_aServiceName_Linear;
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
getSupportedServiceNames_Static()206cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > LinearScaling::getSupportedServiceNames_Static()
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Linear, 1 );
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
APPHELPER_XSERVICEINFO_IMPL(LinearScaling,lcl_aServiceName_Linear)212cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( LinearScaling, lcl_aServiceName_Linear )
213cdf0e10cSrcweir 
214cdf0e10cSrcweir // ----------------------------------------
215cdf0e10cSrcweir 
216cdf0e10cSrcweir PowerScaling::PowerScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
217cdf0e10cSrcweir         m_fExponent( 10.0 ),
218cdf0e10cSrcweir         m_xContext( xContext )
219cdf0e10cSrcweir {}
220cdf0e10cSrcweir 
PowerScaling(double fExponent)221cdf0e10cSrcweir PowerScaling::PowerScaling( double fExponent ) :
222cdf0e10cSrcweir         m_fExponent( fExponent )
223cdf0e10cSrcweir {}
224cdf0e10cSrcweir 
~PowerScaling()225cdf0e10cSrcweir PowerScaling::~PowerScaling()
226cdf0e10cSrcweir {}
227cdf0e10cSrcweir 
doScaling(double value)228cdf0e10cSrcweir double SAL_CALL PowerScaling::doScaling( double value )
229cdf0e10cSrcweir     throw (uno::RuntimeException)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     double fResult;
232cdf0e10cSrcweir     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
233cdf0e10cSrcweir         ::rtl::math::setNan( & fResult );
234cdf0e10cSrcweir     else
235cdf0e10cSrcweir         fResult = pow( value, m_fExponent );
236cdf0e10cSrcweir     return fResult;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir uno::Reference< XScaling > SAL_CALL
getInverseScaling()240cdf0e10cSrcweir     PowerScaling::getInverseScaling()
241cdf0e10cSrcweir     throw (uno::RuntimeException)
242cdf0e10cSrcweir {
243cdf0e10cSrcweir     // ToDo: ApproxEqual ?
244cdf0e10cSrcweir     if( m_fExponent == 0 )
245cdf0e10cSrcweir         throw uno::RuntimeException();
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     return new PowerScaling( 1.0 / m_fExponent );
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     ::rtl::OUString SAL_CALL
getServiceName()251cdf0e10cSrcweir PowerScaling::getServiceName()
252cdf0e10cSrcweir     throw (uno::RuntimeException)
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     return lcl_aServiceName_Power;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir 
getSupportedServiceNames_Static()257cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > PowerScaling::getSupportedServiceNames_Static()
258cdf0e10cSrcweir {
259cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Power, 1 );
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
263cdf0e10cSrcweir APPHELPER_XSERVICEINFO_IMPL( PowerScaling, lcl_aServiceName_Power )
264cdf0e10cSrcweir 
265cdf0e10cSrcweir //.............................................................................
266cdf0e10cSrcweir } //namespace chart
267cdf0e10cSrcweir //.............................................................................
268