Starting from API version 26.0.0, FAST Kit provides the mathPrediction math prediction module. It models and predicts sequence data based on historical sampling data and is suitable for real-time prediction scenarios such as gesture tracking, animation curve prediction, and motion trajectory estimation.
The mathPrediction math prediction module provides intelligent sequence prediction (predictIndex) capabilities. It receives an array of sampling points containing indexes and timestamps, and uses built-in FAST Kit algorithms to predict the index value at the next time step. The core principle is to use time series analysis techniques to perform trend fitting and extrapolation on input sampling points, thereby predicting future states.
The mathPrediction module is applicable to the following scenarios:
Gesture tracking: predicts the index position of the touch point in the next frame based on the historical positions and timestamps of touch points, reducing touch tracking latency.
Animation curve prediction: predicts the trend of subsequent frame index changes based on existing keyframe index-time samples of animation curves, enabling smoother animation transitions.
Motion trajectory estimation: predicts the subsequent position of an object based on sampled historical motion trajectories in games or interactive apps, improving response speed.
Scroll inertia prediction: predicts the final stop position during deceleration based on sampled historical scroll offsets in list scrolling scenarios.
Before calling the predictIndex function, ensure that the mathPrediction module has been correctly imported and the passed** IndexSample** array contains at least two valid sampling points.
Import the mathPrediction module.
Construct the IndexSample API array and add at least two sampling points.
Call the predictIndex function to perform prediction.
Obtain the returned prediction result.
Handle exceptions. For example, throw an exception and log the error when the number of sampling points is less than two.
The timestamps of sampling points must be monotonically increasing. Otherwise, the prediction result may be inaccurate.
import { hilog } from '@kit.PerformanceAnalysisKit';
import { mathPrediction } from '@kit.FASTKit';
const DOMAIN = 0x0000;
function mathPredictionTest(): void {
// Construct an array that contains at least two sampling points to predict the next index value.
let samples: mathPrediction.IndexSample[] = [
// First sampling point: The initial index is 0 and the timestamp is 0.
{ index: 0, timestamp: 0 },
// Second sampling point: The index is 10 and the timestamp is 100.
{ index: 10, timestamp: 100 },
// Third sampling point: The index is 20 and the timestamp is 200.
{ index: 20, timestamp: 200 }
];
try {
// Call the prediction function to obtain the next index value.
const predicted = mathPrediction.predictIndex(samples);
hilog.info(DOMAIN, 'testTag', 'PredictionFunction predicted=%{public}d', predicted);
} catch (err) {
// Capture and record the error code and error information when the prediction fails.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
hilog.error(DOMAIN, 'testTag', 'PredictionFunction failed, code: %{public}d, message: %{public}s', code, message);
}
}