13 static std::vector<SeqTrack> tracks;
16 static Track *load_track(struct ts_node *tnode);
17 static InterpMode str2interp(const char *str);
18 static ExtrapMode str2extrap(const char *str);
28 int ntrk = tracks.size();
29 for(int i=0; i<ntrk; i++) {
30 delete tracks[i].track;
31 delete [] tracks[i].name;
36 int add_seq_track(const char *name, InterpMode inmode, ExtrapMode exmode, float defval)
38 int idx = find_seq_track(name);
39 if(idx >= 0) return idx;
42 st.name = new char[strlen(name) + 1];
43 strcpy(st.name, name);
45 st.track->defval = defval;
46 st.track->interp = inmode;
47 st.track->extrap = exmode;
49 return tracks.size() - 1;
52 int find_seq_track(const char *name)
54 int ntrk = tracks.size();
55 for(int i=0; i<ntrk; i++) {
56 if(strcmp(tracks[i].name, name) == 0) {
63 Track *get_seq_track(int idx)
65 return tracks[idx].track;
68 void clear_seq_track(int idx)
70 tracks[idx].track->clear();
73 void clear_seq_track(const char *name)
75 int idx = find_seq_track(name);
77 tracks[idx].track->clear();
81 void set_seq_value(int idx, long tm, float val)
83 tracks[idx].track->set_value(tm, val);
86 void set_seq_value(const char *name, long tm, float val)
88 int idx = find_seq_track(name);
90 tracks[idx].track->set_value(tm, val);
94 float get_seq_value(int idx, long tm)
96 return (*tracks[idx].track)(tm);
99 float get_seq_value(const char *name, long tm)
101 int idx = find_seq_track(name);
105 return (*tracks[idx].track)(tm);
108 bool load_seq(const char *fname)
110 struct ts_node *tree;
111 if(!(tree = ts_load(fname))) {
114 if(strcmp(tree->name, "faros") != 0) {
115 fprintf(stderr, "invalid sequence file: %s\n", fname);
120 struct ts_node *node = tree->child_list;
122 if(strcmp(node->name, "track") == 0) {
123 Track *track = load_track(node);
124 const char *name = ts_get_attr_str(node, "name", 0);
128 sprintf(buf, "unnamed%03d", foo++);
132 int idx = find_seq_track(name);
136 st.name = new char[strlen(name) + 1];
137 strcpy(st.name, name);
138 tracks.push_back(st);
140 delete tracks[idx].track;
141 tracks[idx].track = track;
152 static Track *load_track(struct ts_node *tnode)
154 Track *track = new Track;
155 track->defval = ts_get_attr_num(tnode, "default", 0.0f);
156 track->interp = str2interp(ts_get_attr_str(tnode, "interpolation", ""));
157 track->extrap = str2extrap(ts_get_attr_str(tnode, "extrapolation", ""));
159 struct ts_node *keynode = tnode->child_list;
161 long tm = ts_get_attr_int(keynode, "time", 0);
162 float val = ts_get_attr_num(keynode, "value", 0.0f);
163 track->set_value(tm, val);
164 keynode = keynode->next;
170 static InterpMode str2interp(const char *str)
172 if(strcmp(str, "step") == 0) {
174 } else if(strcmp(str, "sigmoid") == 0) {
175 return INTERP_SIGMOID;
177 return INTERP_LINEAR;
180 static ExtrapMode str2extrap(const char *str)
182 if(strcmp(str, "repeat") == 0) {
183 return EXTRAP_REPEAT;
188 bool dump_seq(const char *fname)
190 FILE *fp = fopen(fname, "w");
192 perror("failed to open sequence dump file");
196 fprintf(fp, "faros {\n");
197 int ntrk = tracks.size();
198 for(int i=0; i<ntrk; i++) {
199 fprintf(fp, " track {\n");
200 fprintf(fp, " name = \"%s\"\n", tracks[i].name);
202 int nkeys = tracks[i].track->get_num_keys();
203 for(int j=0; j<nkeys; j++) {
204 TrackKey key = (*tracks[i].track)[j];
205 fprintf(fp, " key {\n");
206 fprintf(fp, " time = %ld\n", key.time);
207 fprintf(fp, " value = %g\n", key.value);
210 fprintf(fp, " }\n\n");