fix some gesture's crash

This commit is contained in:
侯歌 2024-07-26 10:45:03 +08:00
parent e6bd3f6bc2
commit 529ac6b6d2
6 changed files with 46 additions and 17 deletions

View File

@ -137,8 +137,9 @@ Bitmap Gesture::toBitmap(int width, int height, int edge, int numSample, int col
canvas.set_color(color);
for (auto stroke:mStrokes) {
Path* path = stroke->toPath(width - 2 * edge, height - 2 * edge, numSample);
LOGD("TODO");//canvas.drawPath(path);
//Path* path = stroke->toPath(width - 2 * edge, height - 2 * edge, numSample);
//canvas.drawPath(path);
stroke->draw(canvas);
}
return bitmap;
@ -175,9 +176,10 @@ Bitmap Gesture::toBitmap(int width, int height, int inset, int color) {
canvas.translate(offsetX + inset, offsetY + inset);
canvas.scale(scale, scale);
LOGD("TODO");//canvas.drawPath(path);
//canvas.drawPath(path);
for (auto stroke:mStrokes){
stroke->draw(canvas);
}
return bitmap;
}

View File

@ -18,7 +18,7 @@ private:
static std::atomic<int> sGestureCount;
RectF mBoundingBox;
// the same as its instance ID
long mGestureID;
unsigned long mGestureID;
std::vector<GestureStroke*> mStrokes;
public:
Gesture();

View File

@ -81,7 +81,7 @@ std::vector<std::string> GestureStore::getGestureEntries() {
* @return a list of predictions of possible entries for a given gesture
*/
std::vector<Prediction> GestureStore::recognize(const Gesture& gesture) {
Instance* instance = Instance::createInstance(mSequenceType, mOrientationStyle, gesture, nullptr);
Instance* instance = Instance::createInstance(mSequenceType, mOrientationStyle, gesture,"");
return mClassifier->classify(mSequenceType, mOrientationStyle, instance->vector);
}

View File

@ -44,17 +44,47 @@ GestureStroke::GestureStroke(const RectF& bbx, float len,const std::vector<float
timestamps = times;
}
static void quad_to(Canvas&c,double x1, double y1, double x2, double y2){
double x0, y0;
c.get_current_point(x0,y0);
//Control points for cubic bezier curve
double cp1x = x0 + 2.f / 3.f * (x1 - x0);
double cp1y = y0 + 2.f / 3.f * (y1 - y0);
double cp2x = cp1x + (x2 - x0) / 3.f;
double cp2y = cp1y + (y2 - y0) / 3.f;
c.curve_to(cp1x, cp1y, cp2x, cp2y, x2, y2);
}
/**
* Draws the stroke with a given canvas and paint.
*
* @param canvas
*/
void GestureStroke::draw(Canvas& canvas) {
if (1/*mCachedPath == null*/) {
/*if (mCachedPath == nullptr) {
makePath();LOGD("TODO");
}
canvas.drawPath(mCachedPath);*/
std::vector<float>& localPoints = points;
const int count = localPoints.size();
//canvas.drawPath(mCachedPath);
float mX = 0, mY = 0;
for (int i = 0; i < count; i += 2) {
const float x = localPoints[i];
const float y = localPoints[i + 1];
if (i==0) {
canvas.move_to(x, y);
mX = x; mY = y;
} else {
float dx = std::abs(x - mX);
float dy = std::abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
quad_to(canvas,mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x; mY = y;
}
}
}
}
cdroid::Path GestureStroke::getPath() {
@ -71,23 +101,20 @@ void GestureStroke::makePath() {
Path path;
float mX = 0;
float mY = 0;
float mX = 0, mY = 0;
for (int i = 0; i < count; i += 2) {
float x = localPoints[i];
float y = localPoints[i + 1];
if (i==0/*path == null*/) {
path.move_to(x, y);
mX = x;
mY = y;
mX = x; mY = y;
} else {
float dx = std::abs(x - mX);
float dy = std::abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
path.quad_to(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
mX = x; mY = y;
}
}
}

View File

@ -89,7 +89,7 @@ std::vector<float> GestureUtils::spatialSampling(const Gesture& gesture, int bit
float preDy = -rect.centerY();
float postDx = targetPatchSize / 2;
float postDy = targetPatchSize / 2;
std::vector<GestureStroke*> strokes = gesture.getStrokes();
const std::vector<GestureStroke*>& strokes = gesture.getStrokes();
const int count = strokes.size();
int size;
float xpos;

View File

@ -5,7 +5,7 @@
namespace cdroid{
Instance::Instance(long id,const std::vector<float>& sample, const std::string& sampleName)
:id(id),vector(vector),label(sampleName){
:id(id),vector(sample),label(sampleName){
}
void Instance::normalize() {