PageRenderTime 55ms CodeModel.GetById 19ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1//
  2//  OALFunction.m
  3//  ObjectAL
  4//
  5//  Created by Karl Stenerud on 10-08-22.
  6//
  7// Copyright 2010 Karl Stenerud
  8//
  9// Licensed under the Apache License, Version 2.0 (the "License");
 10// you may not use this file except in compliance with the License.
 11// You may obtain a copy of the License at
 12//
 13// http://www.apache.org/licenses/LICENSE-2.0
 14//
 15// Unless required by applicable law or agreed to in writing, software
 16// distributed under the License is distributed on an "AS IS" BASIS,
 17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18// See the License for the specific language governing permissions and
 19// limitations under the License.
 20//
 21// Note: You are NOT required to make the license available from within your
 22// iOS application. Including it in your project is sufficient.
 23//
 24// Attribution is not required, but appreciated :)
 25//
 26
 27#import "OALFunction.h"
 28
 29
 30#pragma mark OALLinearFunction
 31
 32SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALLinearFunction);
 33
 34@implementation OALLinearFunction
 35
 36
 37#pragma mark Object Management
 38
 39SYNTHESIZE_SINGLETON_FOR_CLASS(OALLinearFunction);
 40
 41+ (id) function
 42{
 43	return [self sharedInstance];
 44}
 45
 46
 47#pragma mark Function
 48
 49- (float) valueForInput:(float) inputValue
 50{
 51	return inputValue;
 52}
 53
 54@end
 55
 56
 57
 58#pragma mark -
 59#pragma mark OALSCurveFunction
 60
 61SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALSCurveFunction);
 62
 63@implementation OALSCurveFunction
 64
 65
 66#pragma mark Object Management
 67
 68SYNTHESIZE_SINGLETON_FOR_CLASS(OALSCurveFunction);
 69
 70+ (id) function
 71{
 72	return [self sharedInstance];
 73}
 74
 75
 76#pragma mark Function
 77
 78- (float) valueForInput:(float) inputValue
 79{
 80	// x^2 * (3-2x)
 81	return inputValue * inputValue * (3.0f - 2.0f * inputValue);
 82}
 83
 84@end
 85
 86
 87
 88#pragma mark -
 89#pragma mark OALExponentialFunction
 90
 91SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALExponentialFunction);
 92
 93@implementation OALExponentialFunction
 94
 95
 96#pragma mark Object Management
 97
 98SYNTHESIZE_SINGLETON_FOR_CLASS(OALExponentialFunction);
 99
100+ (id) function
101{
102	return [self sharedInstance];
103}
104
105
106#pragma mark Function
107
108- (float) valueForInput:(float) inputValue
109{
110	// (10^(x-1) - 10^-1) * (1 / (1 - 10^-1))
111	return (powf(10.f,inputValue-1.0f) - 0.1f) * (1.0f/0.9f);
112}
113
114@end
115
116
117
118#pragma mark -
119#pragma mark OALLogarithmicFunction
120
121SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALLogarithmicFunction);
122
123@implementation OALLogarithmicFunction
124
125
126#pragma mark Object Management
127
128SYNTHESIZE_SINGLETON_FOR_CLASS(OALLogarithmicFunction);
129
130+ (id) function
131{
132	return [self sharedInstance];
133}
134
135
136#pragma mark Function
137
138- (float) valueForInput:(float) inputValue
139{
140	// log10(x * (1 - 10^-1) + 10^-1) + 1
141	return log10f(inputValue * 0.9f + 0.1f) + 1.0f;
142}
143
144@end
145
146
147
148#pragma mark -
149#pragma mark OALReverseFunction
150
151@implementation OALReverseFunction
152
153
154#pragma mark Object Management
155
156+ (id) functionWithFunction:(id<OALFunction, NSObject>) function
157{
158	return [[[self alloc] initWithFunction:function] autorelease];
159}
160
161- (id) initWithFunction:(id<OALFunction, NSObject>) functionIn
162{
163	if(nil != (self = [super init]))
164	{
165		function = [functionIn retain];
166	}
167	return self;
168}
169
170- (void) dealloc
171{
172	[function release];
173	[super dealloc];
174}
175
176
177#pragma mark Properties
178
179@synthesize function;
180
181
182#pragma mark Function
183
184- (float) valueForInput:(float) inputValue
185{
186	return [function valueForInput:1.0f - inputValue];
187}
188
189@end