38 lines
1.4 KiB
C
38 lines
1.4 KiB
C
#include "kernels.h"
|
|
#include <math.h>
|
|
|
|
double native_signal(double t, double start, int stages, int cyclic, const double *data) {
|
|
if (t < start) return data[0];
|
|
double elapsed = t-start, duration = 0, offset = 0;
|
|
for (int i=0;i<stages;i++) duration += data[16+i];
|
|
if (cyclic && duration > 0) elapsed = fmod(elapsed,duration);
|
|
else if (elapsed >= duration) return data[8+stages-1];
|
|
for (int i=0;i<stages;i++) {
|
|
double d = data[16+i];
|
|
if (elapsed < offset+d || i == stages-1)
|
|
return d <= 0 ? data[8+i] : data[i] + (elapsed-offset)/d*(data[8+i]-data[i]);
|
|
offset += d;
|
|
}
|
|
return data[8+stages-1];
|
|
}
|
|
|
|
double native_signal_break(double t, double end, double start, int stages,
|
|
int cyclic, const double *data) {
|
|
double duration=0, offset=0, result=end;
|
|
for (int i=0;i<stages;i++) duration += data[16+i];
|
|
/* A cyclic endpoint is already the next cycle's start. Listing both can
|
|
create two nearly equal floating-point break times for one event. */
|
|
for (int i=0;i<stages+(cyclic?0:1);i++) {
|
|
double event=start+offset;
|
|
if (cyclic && duration > 0 && event <= t) {
|
|
double cycle=fmax(0,floor((t-event)/duration)+1);
|
|
event += cycle*duration;
|
|
if (event <= t) event += duration;
|
|
}
|
|
if (event > t) result=fmin(result,event);
|
|
if (i < stages) offset += data[16+i];
|
|
}
|
|
return result;
|
|
}
|
|
|