// This function runs a brush "simulation" step. Usually it is // called once or twice per dab. In theory the precision of the // "simulation" gets better when it is called more often. In // practice this only matters if there are some highly nonlinear // mappings in critical places or extremely few events per second. // // note: parameters are is dx/ddab, ..., dtime/ddab (dab is the number, 5.0 = 5th dab) void update_states_and_setting_values (MyPaintBrush *self, float step_ddab, float step_dx, float step_dy, float step_dpressure, float step_declination, float step_ascension, float step_dtime) { float pressure; float inputs[MYPAINT_BRUSH_INPUTS_COUNT]; if (step_dtime < 0.0) { printf("Time is running backwards!\n"); step_dtime = 0.001; } else if (step_dtime == 0.0) { // FIXME: happens about every 10th start, workaround (against division by zero) step_dtime = 0.001; } self->states[MYPAINT_BRUSH_STATE_X] += step_dx; self->states[MYPAINT_BRUSH_STATE_Y] += step_dy; self->states[MYPAINT_BRUSH_STATE_PRESSURE] += step_dpressure; self->states[MYPAINT_BRUSH_STATE_DECLINATION] += step_declination; self->states[MYPAINT_BRUSH_STATE_ASCENSION] += step_ascension; float base_radius = expf(mypaint_mapping_get_base_value(self->settings[MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC])); // FIXME: does happen (interpolation problem?) if (self->states[MYPAINT_BRUSH_STATE_PRESSURE] <= 0.0) self->states[MYPAINT_BRUSH_STATE_PRESSURE] = 0.0; pressure = self->states[MYPAINT_BRUSH_STATE_PRESSURE]; { // start / end stroke (for "stroke" input only) if (!self->states[MYPAINT_BRUSH_STATE_STROKE_STARTED]) { if (pressure > mypaint_mapping_get_base_value(self->settings[MYPAINT_BRUSH_SETTING_STROKE_THRESHOLD]) + 0.0001) { // start new stroke //printf("stroke start %f\n", pressure); self->states[MYPAINT_BRUSH_STATE_STROKE_STARTED] = 1; self->states[MYPAINT_BRUSH_STATE_STROKE] = 0.0; } } else { if (pressure <= mypaint_mapping_get_base_value(self->settings[MYPAINT_BRUSH_SETTING_STROKE_THRESHOLD]) * 0.9 + 0.0001) { // end stroke //printf("stroke end\n"); self->states[MYPAINT_BRUSH_STATE_STROKE_STARTED] = 0; } } } // now follows input handling float norm_dx, norm_dy, norm_dist, norm_speed; norm_dx = step_dx / step_dtime / base_radius; norm_dy = step_dy / step_dtime / base_radius; norm_speed = hypotf(norm_dx, norm_dy); norm_dist = norm_speed * step_dtime; inputs[MYPAINT_BRUSH_INPUT_PRESSURE] = pressure * expf(mypaint_mapping_get_base_value(self->settings[MYPAINT_BRUSH_SETTING_PRESSURE_GAIN_LOG])); inputs[MYPAINT_BRUSH_INPUT_SPEED1] = log(self->speed_mapping_gamma[0] + self->states[MYPAINT_BRUSH_STATE_NORM_SPEED1_SLOW])*self->speed_mapping_m[0] + self->speed_mapping_q[0]; inputs[MYPAINT_BRUSH_INPUT_SPEED2] = log(self->speed_mapping_gamma[1] + self->states[MYPAINT_BRUSH_STATE_NORM_SPEED2_SLOW])*self->speed_mapping_m[1] + self->speed_mapping_q[1]; inputs[MYPAINT_BRUSH_INPUT_RANDOM] = rng_double_next(self->rng); inputs[MYPAINT_BRUSH_INPUT_STROKE] = MIN(self->states[MYPAINT_BRUSH_STATE_STROKE], 1.0); inputs[MYPAINT_BRUSH_INPUT_DIRECTION] = fmodf (atan2f (self->states[MYPAINT_BRUSH_STATE_DIRECTION_DY], self->states[MYPAINT_BRUSH_STATE_DIRECTION_DX])/(2*M_PI)*360 + 180.0, 180.0); inputs[MYPAINT_BRUSH_INPUT_TILT_DECLINATION] = self->states[MYPAINT_BRUSH_STATE_DECLINATION]; inputs[MYPAINT_BRUSH_INPUT_TILT_ASCENSION] = fmodf(self->states[MYPAINT_BRUSH_STATE_ASCENSION] + 180.0, 360.0) - 180.0; inputs[MYPAINT_BRUSH_INPUT_CUSTOM] = self->states[MYPAINT_BRUSH_STATE_CUSTOM_INPUT]; if (self->print_inputs) { printf("press=% 4.3f, speed1=% 4.4f\tspeed2=% 4.4f\tstroke=% 4.3f\tcustom=% 4.3f\n", (double)inputs[MYPAINT_BRUSH_INPUT_PRESSURE], (double)inputs[MYPAINT_BRUSH_INPUT_SPEED1], (double)inputs[MYPAINT_BRUSH_INPUT_SPEED2], (double)inputs[MYPAINT_BRUSH_INPUT_STROKE], (double)inputs[MYPAINT_BRUSH_INPUT_CUSTOM]); } // FIXME: this one fails!!! //assert(inputs[MYPAINT_BRUSH_INPUT_SPEED1] >= 0.0 && inputs[MYPAINT_BRUSH_INPUT_SPEED1] < 1e8); // checking for inf int i=0; for (i=0; i<MYPAINT_BRUSH_SETTINGS_COUNT; i++) { self->settings_value[i] = mypaint_mapping_calculate(self->settings[i], (inputs)); } { float fac = 1.0 - exp_decay (self->settings_value[MYPAINT_BRUSH_SETTING_SLOW_TRACKING_PER_DAB], step_ddab); self->states[MYPAINT_BRUSH_STATE_ACTUAL_X] += (self->states[MYPAINT_BRUSH_STATE_X] - self->states[MYPAINT_BRUSH_STATE_ACTUAL_X]) * fac; self->states[MYPAINT_BRUSH_STATE_ACTUAL_Y] += (self->states[MYPAINT_BRUSH_STATE_Y] - self->states[MYPAINT_BRUSH_STATE_ACTUAL_Y]) * fac; } { // slow speed float fac; fac = 1.0 - exp_decay (self->settings_value[MYPAINT_BRUSH_SETTING_SPEED1_SLOWNESS], step_dtime); self->states[MYPAINT_BRUSH_STATE_NORM_SPEED1_SLOW] += (norm_speed - self->states[MYPAINT_BRUSH_STATE_NORM_SPEED1_SLOW]) * fac; fac = 1.0 - exp_decay (self->settings_value[MYPAINT_BRUSH_SETTING_SPEED2_SLOWNESS], step_dtime); self->states[MYPAINT_BRUSH_STATE_NORM_SPEED2_SLOW] += (norm_speed - self->states[MYPAINT_BRUSH_STATE_NORM_SPEED2_SLOW]) * fac; } { // slow speed, but as vector this time // FIXME: offset_by_speed should be removed. // Is it broken, non-smooth, system-dependent math?! // A replacement could be a directed random offset. float time_constant = expf(self->settings_value[MYPAINT_BRUSH_SETTING_OFFSET_BY_SPEED_SLOWNESS]*0.01)-1.0; // Workaround for a bug that happens mainly on Windows, causing // individual dabs to be placed far far away. Using the speed // with zero filtering is just asking for trouble anyway. if (time_constant < 0.002) time_constant = 0.002; float fac = 1.0 - exp_decay (time_constant, step_dtime); self->states[MYPAINT_BRUSH_STATE_NORM_DX_SLOW] += (norm_dx - self->states[MYPAINT_BRUSH_STATE_NORM_DX_SLOW]) * fac; self->states[MYPAINT_BRUSH_STATE_NORM_DY_SLOW] += (norm_dy - self->states[MYPAINT_BRUSH_STATE_NORM_DY_SLOW]) * fac; } { // orientation (similar lowpass filter as above, but use dabtime instead of wallclock time) float dx = step_dx / base_radius; float dy = step_dy / base_radius; float step_in_dabtime = hypotf(dx, dy); // FIXME: are we recalculating something here that we already have? float fac = 1.0 - exp_decay (exp(self->settings_value[MYPAINT_BRUSH_SETTING_DIRECTION_FILTER]*0.5)-1.0, step_in_dabtime); float dx_old = self->states[MYPAINT_BRUSH_STATE_DIRECTION_DX]; float dy_old = self->states[MYPAINT_BRUSH_STATE_DIRECTION_DY]; // use the opposite speed vector if it is closer (we don't care about 180 degree turns) if (SQR(dx_old-dx) + SQR(dy_old-dy) > SQR(dx_old-(-dx)) + SQR(dy_old-(-dy))) { dx = -dx; dy = -dy; } self->states[MYPAINT_BRUSH_STATE_DIRECTION_DX] += (dx - self->states[MYPAINT_BRUSH_STATE_DIRECTION_DX]) * fac; self->states[MYPAINT_BRUSH_STATE_DIRECTION_DY] += (dy - self->states[MYPAINT_BRUSH_STATE_DIRECTION_DY]) * fac; } { // custom input float fac; fac = 1.0 - exp_decay (self->settings_value[MYPAINT_BRUSH_SETTING_CUSTOM_INPUT_SLOWNESS], 0.1); self->states[MYPAINT_BRUSH_STATE_CUSTOM_INPUT] += (self->settings_value[MYPAINT_BRUSH_SETTING_CUSTOM_INPUT] - self->states[MYPAINT_BRUSH_STATE_CUSTOM_INPUT]) * fac; } { // stroke length float frequency; float wrap; frequency = expf(-self->settings_value[MYPAINT_BRUSH_SETTING_STROKE_DURATION_LOGARITHMIC]); self->states[MYPAINT_BRUSH_STATE_STROKE] += norm_dist * frequency; // can happen, probably caused by rounding if (self->states[MYPAINT_BRUSH_STATE_STROKE] < 0) self->states[MYPAINT_BRUSH_STATE_STROKE] = 0; wrap = 1.0 + self->settings_value[MYPAINT_BRUSH_SETTING_STROKE_HOLDTIME]; if (self->states[MYPAINT_BRUSH_STATE_STROKE] > wrap) { if (wrap > 9.9 + 1.0) { // "inifinity", just hold stroke somewhere >= 1.0 self->states[MYPAINT_BRUSH_STATE_STROKE] = 1.0; } else { self->states[MYPAINT_BRUSH_STATE_STROKE] = fmodf(self->states[MYPAINT_BRUSH_STATE_STROKE], wrap); // just in case if (self->states[MYPAINT_BRUSH_STATE_STROKE] < 0) self->states[MYPAINT_BRUSH_STATE_STROKE] = 0; } } } // calculate final radius float radius_log; radius_log = self->settings_value[MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC]; self->states[MYPAINT_BRUSH_STATE_ACTUAL_RADIUS] = expf(radius_log); if (self->states[MYPAINT_BRUSH_STATE_ACTUAL_RADIUS] < ACTUAL_RADIUS_MIN) self->states[MYPAINT_BRUSH_STATE_ACTUAL_RADIUS] = ACTUAL_RADIUS_MIN; if (self->states[MYPAINT_BRUSH_STATE_ACTUAL_RADIUS] > ACTUAL_RADIUS_MAX) self->states[MYPAINT_BRUSH_STATE_ACTUAL_RADIUS] = ACTUAL_RADIUS_MAX; // aspect ratio (needs to be caluclated here because it can affect the dab spacing) self->states[MYPAINT_BRUSH_STATE_ACTUAL_ELLIPTICAL_DAB_RATIO] = self->settings_value[MYPAINT_BRUSH_SETTING_ELLIPTICAL_DAB_RATIO]; self->states[MYPAINT_BRUSH_STATE_ACTUAL_ELLIPTICAL_DAB_ANGLE] = self->settings_value[MYPAINT_BRUSH_SETTING_ELLIPTICAL_DAB_ANGLE]; }
float calculate (float * data) { return mypaint_mapping_calculate(c_mapping, data); }