/libs/ObjectAL/Actions/OALFunction.m
http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 189 lines · 93 code · 68 blank · 28 comment · 2 complexity · e2c9ed1a45bf8ee33233346e0761fee9 MD5 · raw file
- //
- // OALFunction.m
- // ObjectAL
- //
- // Created by Karl Stenerud on 10-08-22.
- //
- // Copyright 2010 Karl Stenerud
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- // Note: You are NOT required to make the license available from within your
- // iOS application. Including it in your project is sufficient.
- //
- // Attribution is not required, but appreciated :)
- //
- #import "OALFunction.h"
- #pragma mark OALLinearFunction
- SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALLinearFunction);
- @implementation OALLinearFunction
- #pragma mark Object Management
- SYNTHESIZE_SINGLETON_FOR_CLASS(OALLinearFunction);
- + (id) function
- {
- return [self sharedInstance];
- }
- #pragma mark Function
- - (float) valueForInput:(float) inputValue
- {
- return inputValue;
- }
- @end
- #pragma mark -
- #pragma mark OALSCurveFunction
- SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALSCurveFunction);
- @implementation OALSCurveFunction
- #pragma mark Object Management
- SYNTHESIZE_SINGLETON_FOR_CLASS(OALSCurveFunction);
- + (id) function
- {
- return [self sharedInstance];
- }
- #pragma mark Function
- - (float) valueForInput:(float) inputValue
- {
- // x^2 * (3-2x)
- return inputValue * inputValue * (3.0f - 2.0f * inputValue);
- }
- @end
- #pragma mark -
- #pragma mark OALExponentialFunction
- SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALExponentialFunction);
- @implementation OALExponentialFunction
- #pragma mark Object Management
- SYNTHESIZE_SINGLETON_FOR_CLASS(OALExponentialFunction);
- + (id) function
- {
- return [self sharedInstance];
- }
- #pragma mark Function
- - (float) valueForInput:(float) inputValue
- {
- // (10^(x-1) - 10^-1) * (1 / (1 - 10^-1))
- return (powf(10.f,inputValue-1.0f) - 0.1f) * (1.0f/0.9f);
- }
- @end
- #pragma mark -
- #pragma mark OALLogarithmicFunction
- SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALLogarithmicFunction);
- @implementation OALLogarithmicFunction
- #pragma mark Object Management
- SYNTHESIZE_SINGLETON_FOR_CLASS(OALLogarithmicFunction);
- + (id) function
- {
- return [self sharedInstance];
- }
- #pragma mark Function
- - (float) valueForInput:(float) inputValue
- {
- // log10(x * (1 - 10^-1) + 10^-1) + 1
- return log10f(inputValue * 0.9f + 0.1f) + 1.0f;
- }
- @end
- #pragma mark -
- #pragma mark OALReverseFunction
- @implementation OALReverseFunction
- #pragma mark Object Management
- + (id) functionWithFunction:(id<OALFunction, NSObject>) function
- {
- return [[[self alloc] initWithFunction:function] autorelease];
- }
- - (id) initWithFunction:(id<OALFunction, NSObject>) functionIn
- {
- if(nil != (self = [super init]))
- {
- function = [functionIn retain];
- }
- return self;
- }
- - (void) dealloc
- {
- [function release];
- [super dealloc];
- }
- #pragma mark Properties
- @synthesize function;
- #pragma mark Function
- - (float) valueForInput:(float) inputValue
- {
- return [function valueForInput:1.0f - inputValue];
- }
- @end