/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. #import "OALFunction.h"
  27. #pragma mark OALLinearFunction
  28. SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALLinearFunction);
  29. @implementation OALLinearFunction
  30. #pragma mark Object Management
  31. SYNTHESIZE_SINGLETON_FOR_CLASS(OALLinearFunction);
  32. + (id) function
  33. {
  34. return [self sharedInstance];
  35. }
  36. #pragma mark Function
  37. - (float) valueForInput:(float) inputValue
  38. {
  39. return inputValue;
  40. }
  41. @end
  42. #pragma mark -
  43. #pragma mark OALSCurveFunction
  44. SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALSCurveFunction);
  45. @implementation OALSCurveFunction
  46. #pragma mark Object Management
  47. SYNTHESIZE_SINGLETON_FOR_CLASS(OALSCurveFunction);
  48. + (id) function
  49. {
  50. return [self sharedInstance];
  51. }
  52. #pragma mark Function
  53. - (float) valueForInput:(float) inputValue
  54. {
  55. // x^2 * (3-2x)
  56. return inputValue * inputValue * (3.0f - 2.0f * inputValue);
  57. }
  58. @end
  59. #pragma mark -
  60. #pragma mark OALExponentialFunction
  61. SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALExponentialFunction);
  62. @implementation OALExponentialFunction
  63. #pragma mark Object Management
  64. SYNTHESIZE_SINGLETON_FOR_CLASS(OALExponentialFunction);
  65. + (id) function
  66. {
  67. return [self sharedInstance];
  68. }
  69. #pragma mark Function
  70. - (float) valueForInput:(float) inputValue
  71. {
  72. // (10^(x-1) - 10^-1) * (1 / (1 - 10^-1))
  73. return (powf(10.f,inputValue-1.0f) - 0.1f) * (1.0f/0.9f);
  74. }
  75. @end
  76. #pragma mark -
  77. #pragma mark OALLogarithmicFunction
  78. SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(OALLogarithmicFunction);
  79. @implementation OALLogarithmicFunction
  80. #pragma mark Object Management
  81. SYNTHESIZE_SINGLETON_FOR_CLASS(OALLogarithmicFunction);
  82. + (id) function
  83. {
  84. return [self sharedInstance];
  85. }
  86. #pragma mark Function
  87. - (float) valueForInput:(float) inputValue
  88. {
  89. // log10(x * (1 - 10^-1) + 10^-1) + 1
  90. return log10f(inputValue * 0.9f + 0.1f) + 1.0f;
  91. }
  92. @end
  93. #pragma mark -
  94. #pragma mark OALReverseFunction
  95. @implementation OALReverseFunction
  96. #pragma mark Object Management
  97. + (id) functionWithFunction:(id<OALFunction, NSObject>) function
  98. {
  99. return [[[self alloc] initWithFunction:function] autorelease];
  100. }
  101. - (id) initWithFunction:(id<OALFunction, NSObject>) functionIn
  102. {
  103. if(nil != (self = [super init]))
  104. {
  105. function = [functionIn retain];
  106. }
  107. return self;
  108. }
  109. - (void) dealloc
  110. {
  111. [function release];
  112. [super dealloc];
  113. }
  114. #pragma mark Properties
  115. @synthesize function;
  116. #pragma mark Function
  117. - (float) valueForInput:(float) inputValue
  118. {
  119. return [function valueForInput:1.0f - inputValue];
  120. }
  121. @end