1 // Copyright (c) 2009 Yanagi Asakura
3 // This software is provided 'as-is', without any express or implied
4 // warranty. In no event will the authors be held liable for any damages
5 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
11 // 1. The origin of this software must not be misrepresented; you must not
12 // claim that you wrote the original software. If you use this software
13 // in a product, an acknowledgment in the product documentation would be
14 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
19 // 3. This notice may not be removed or altered from any source
26 // Created by 柳 on 09/09/15.
27 // Copyright 2009 __MyCompanyName__. All rights reserved.
30 #import "ElisKeyframe.h"
32 static float convertQTTimeToSecond(QTTime t)
34 return (float)t.timeValue/t.timeScale;
37 @implementation ElisKeyframe
42 timesAndValues = [[NSMutableDictionary alloc] init];
45 #ifdef __SNOW_LEOPARD_GCD__
46 diq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
51 - (void)setValueForTime:(float)value time:(QTTime)time
56 [timesAndValues setObject:[NSNumber numberWithFloat:value] forKey:[NSNumber numberWithFloat:convertQTTimeToSecond(time)]];
57 sortedTimes = [[timesAndValues allKeys] sortedArrayUsingSelector:@selector(compare:)];
58 // 常識的に考えて、NSArrayである必要なくね? Cの配列でよくね?
63 // for(i = 0; i < index-1; i++){
64 // if(times[i].timeValue == time.timeValue){
68 // if(times[i].timeValue <= time.timeValue && time.timeValue <= times[i+1].timeValue){
69 // for(j = index; j > i+1; j--){
70 // times[j] = times[j-1];
71 // values[j] = values[j-1];
74 // values[i+1] = value;
78 // if(times[index-1].timeValue == time.timeValue) values[index-1] = value;
79 // else if(times[index-1].timeValue <= time.timeValue){
80 // times[index] = time;
81 // values[index++] = value;
83 // times[index] = times[index-1];
84 // values[index] = values[index-1];
85 // times[index-1] = time;
86 // values[index-1] = value;
92 // いろいろすさまじい。ボトルネックになりそうだなあここ。
93 // 描画を実行する度に2回呼ばれる。キャッシュするか。
94 - (float)getValueForTime:(QTTime)time
96 // long long now = time.timeValue;
98 // for(i = 0; i < index-1; i++){
99 // if(times[i].timeValue <= now && now <= times[i+1].timeValue){
100 // return ((float)(times[i+1].timeValue - now) * values[i] +
101 // (float)(now - times[i].timeValue) * values[i+1])/(times[i+1].timeValue - times[i].timeValue);
104 // if(now <= times[0].timeValue) return values[0];
105 // if(times[index-1].timeValue <= now) return values[index-1];
106 float now = convertQTTimeToSecond(time);
108 if(cacheTime == now) return cacheValue;
111 int i, size = [sortedTimes count];
114 cacheValue = [[timesAndValues objectForKey:[sortedTimes objectAtIndex:0]] floatValue];
118 if([[sortedTimes lastObject] floatValue] <= now){
119 cacheValue = [[timesAndValues objectForKey:[sortedTimes lastObject]] floatValue];
123 //#ifdef __SNOW_LEOPARD_GCD__
124 // dispatch_apply(size-1, diq, ^(size_t i) {
125 // if([[sortedTimes objectAtIndex:i] floatValue] <= now &&
126 // now <= [[sortedTimes objectAtIndex:i+1] floatValue]){
127 // cacheValue = (([[sortedTimes objectAtIndex:i+1] floatValue] - now) *
128 // [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i]] floatValue] +
129 // (now - [[sortedTimes objectAtIndex:i] floatValue]) *
130 // [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i+1]] floatValue]) /
131 // ([[sortedTimes objectAtIndex:i+1] floatValue] - [[sortedTimes objectAtIndex:i] floatValue]);
134 // return cacheValue;
136 for(i = 0; i < size-1; i++){
137 if([[sortedTimes objectAtIndex:i] floatValue] <= now &&
138 now <= [[sortedTimes objectAtIndex:i+1] floatValue]){
139 cacheValue = (([[sortedTimes objectAtIndex:i+1] floatValue] - now) *
140 [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i]] floatValue] +
141 (now - [[sortedTimes objectAtIndex:i] floatValue]) *
142 [[timesAndValues objectForKey:[sortedTimes objectAtIndex:i+1]] floatValue]) /
143 ([[sortedTimes objectAtIndex:i+1] floatValue] - [[sortedTimes objectAtIndex:i] floatValue]);
152 - (void)encodeWithCoder:(NSCoder*)encoder
154 [encoder encodeObject:timesAndValues forKey:@"timesAndValues"];
155 [encoder encodeObject:sortedTimes forKey:@"sortedTimes"];
158 - (id)initWithCoder:(NSCoder*)coder
160 timesAndValues = [coder decodeObjectForKey:@"timesAndValues"];
161 sortedTimes = [coder decodeObjectForKey:@"sortedTimes"];