SkPaint makePaint() { SkPaint paint; bool antiAlias = fRand.nextBool(); paint.setAntiAlias(antiAlias); SkPaint::Style style = fStrokeOnly ? SkPaint::kStroke_Style : (SkPaint::Style) fRand.nextRangeU(SkPaint::kFill_Style, SkPaint::kStrokeAndFill_Style); paint.setStyle(style); SkColor color = (SkColor) fRand.nextU(); paint.setColor(color); SkScalar width = fRand.nextRangeF(0, 10); paint.setStrokeWidth(width); SkScalar miter = makeScalar(); paint.setStrokeMiter(miter); SkPaint::Cap cap = (SkPaint::Cap) fRand.nextRangeU(SkPaint::kButt_Cap, SkPaint::kSquare_Cap); paint.setStrokeCap(cap); SkPaint::Join join = (SkPaint::Join) fRand.nextRangeU(SkPaint::kMiter_Join, SkPaint::kBevel_Join); paint.setStrokeJoin(join); return paint; }
SkRRect makeRRect() { SkRRect rrect; RandomSetRRect rrectType = makeSetRRectType(); if (fPrintName) { SkDebugf("%.*s%s\n", fPathDepth * 3, fTab, gRandomSetRRectNames[rrectType]); } switch (rrectType) { case kSetEmpty: rrect.setEmpty(); break; case kSetRect: { SkRect rect = makeRect(); rrect.setRect(rect); } break; case kSetOval: { SkRect oval = makeRect(); rrect.setOval(oval); } break; case kSetRectXY: { SkRect rect = makeRect(); SkScalar xRad = makeScalar(); SkScalar yRad = makeScalar(); rrect.setRectXY(rect, xRad, yRad); } break; case kSetNinePatch: { SkRect rect = makeRect(); SkScalar leftRad = makeScalar(); SkScalar topRad = makeScalar(); SkScalar rightRad = makeScalar(); SkScalar bottomRad = makeScalar(); rrect.setNinePatch(rect, leftRad, topRad, rightRad, bottomRad); SkDebugf(""); // keep locals in scope } break; case kSetRectRadii: { SkRect rect = makeRect(); SkVector radii[4]; makeVectorArray(SK_ARRAY_COUNT(radii), radii); rrect.setRectRadii(rect, radii); } break; } return rrect; }
void makeScalarArray(size_t arrayCount, SkScalar* array) { for (size_t index = 0; index < arrayCount; ++index) { array[index] = makeScalar(); } }
SkPath makePath() { SkPath path; for (uint32_t cIndex = 0; cIndex < fPathContourCount; ++cIndex) { uint32_t segments = makeSegmentCount(); for (uint32_t sIndex = 0; sIndex < segments; ++sIndex) { RandomAddPath addPathType = makeAddPathType(); ++fAddCount; if (fPrintName) { SkDebugf("%.*s%s\n", fPathDepth * 3, fTab, gRandomAddPathNames[addPathType]); } switch (addPathType) { case kAddArc: { SkRect oval = makeRect(); SkScalar startAngle = makeAngle(); SkScalar sweepAngle = makeAngle(); path.addArc(oval, startAngle, sweepAngle); validate(path); } break; case kAddRoundRect1: { SkRect rect = makeRect(); SkScalar rx = makeScalar(), ry = makeScalar(); SkPath::Direction dir = makeDirection(); path.addRoundRect(rect, rx, ry, dir); validate(path); } break; case kAddRoundRect2: { SkRect rect = makeRect(); SkScalar radii[8]; makeScalarArray(SK_ARRAY_COUNT(radii), radii); SkPath::Direction dir = makeDirection(); path.addRoundRect(rect, radii, dir); validate(path); } break; case kAddRRect: { SkRRect rrect = makeRRect(); SkPath::Direction dir = makeDirection(); path.addRRect(rrect, dir); validate(path); } break; case kAddPoly: { SkTDArray<SkPoint> points; makePointArray(&points); bool close = makeBool(); path.addPoly(&points[0], points.count(), close); validate(path); } break; case kAddPath1: if (fPathDepth < fPathDepthLimit) { ++fPathDepth; SkPath src = makePath(); validate(src); SkScalar dx = makeScalar(); SkScalar dy = makeScalar(); SkPath::AddPathMode mode = makeAddPathMode(); path.addPath(src, dx, dy, mode); --fPathDepth; validate(path); } break; case kAddPath2: if (fPathDepth < fPathDepthLimit) { ++fPathDepth; SkPath src = makePath(); validate(src); SkPath::AddPathMode mode = makeAddPathMode(); path.addPath(src, mode); --fPathDepth; validate(path); } break; case kAddPath3: if (fPathDepth < fPathDepthLimit) { ++fPathDepth; SkPath src = makePath(); validate(src); SkMatrix matrix = makeMatrix(); SkPath::AddPathMode mode = makeAddPathMode(); path.addPath(src, matrix, mode); --fPathDepth; validate(path); } break; case kReverseAddPath: if (fPathDepth < fPathDepthLimit) { ++fPathDepth; SkPath src = makePath(); validate(src); path.reverseAddPath(src); --fPathDepth; validate(path); } break; case kMoveToPath: { SkScalar x = makeScalar(); SkScalar y = makeScalar(); path.moveTo(x, y); validate(path); } break; case kRMoveToPath: { SkScalar x = makeScalar(); SkScalar y = makeScalar(); path.rMoveTo(x, y); validate(path); } break; case kLineToPath: { SkScalar x = makeScalar(); SkScalar y = makeScalar(); path.lineTo(x, y); validate(path); } break; case kRLineToPath: { SkScalar x = makeScalar(); SkScalar y = makeScalar(); path.rLineTo(x, y); validate(path); } break; case kQuadToPath: { SkPoint pt[2]; makePointArray(SK_ARRAY_COUNT(pt), pt); path.quadTo(pt[0], pt[1]); validate(path); } break; case kRQuadToPath: { SkPoint pt[2]; makePointArray(SK_ARRAY_COUNT(pt), pt); path.rQuadTo(pt[0].fX, pt[0].fY, pt[1].fX, pt[1].fY); validate(path); } break; case kConicToPath: { SkPoint pt[2]; makePointArray(SK_ARRAY_COUNT(pt), pt); SkScalar weight = makeScalar(); path.conicTo(pt[0], pt[1], weight); validate(path); } break; case kRConicToPath: { SkPoint pt[2]; makePointArray(SK_ARRAY_COUNT(pt), pt); SkScalar weight = makeScalar(); path.rConicTo(pt[0].fX, pt[0].fY, pt[1].fX, pt[1].fY, weight); validate(path); } break; case kCubicToPath: { SkPoint pt[3]; makePointArray(SK_ARRAY_COUNT(pt), pt); path.cubicTo(pt[0], pt[1], pt[2]); validate(path); } break; case kRCubicToPath: { SkPoint pt[3]; makePointArray(SK_ARRAY_COUNT(pt), pt); path.rCubicTo(pt[0].fX, pt[0].fY, pt[1].fX, pt[1].fY, pt[2].fX, pt[2].fY); validate(path); } break; case kArcToPath: { SkPoint pt[2]; makePointArray(SK_ARRAY_COUNT(pt), pt); SkScalar radius = makeScalar(); path.arcTo(pt[0], pt[1], radius); validate(path); } break; case kArcTo2Path: { SkRect oval = makeRect(); SkScalar startAngle = makeAngle(); SkScalar sweepAngle = makeAngle(); bool forceMoveTo = makeBool(); path.arcTo(oval, startAngle, sweepAngle, forceMoveTo); validate(path); } break; case kClosePath: path.close(); validate(path); break; } } } return path; }
SkMatrix makeMatrix() { SkMatrix matrix; matrix.reset(); RandomSetMatrix setMatrix = (RandomSetMatrix) fRand.nextRangeU(0, kRandomSetMatrix_Last); if (fPrintName) { SkDebugf("%.*s%s\n", fPathDepth * 3, fTab, gRandomSetMatrixNames[setMatrix]); } switch (setMatrix) { case kSetIdentity: break; case kSetTranslateX: matrix.setTranslateX(makeScalar()); break; case kSetTranslateY: matrix.setTranslateY(makeScalar()); break; case kSetTranslate: matrix.setTranslate(makeScalar(), makeScalar()); break; case kSetScaleX: matrix.setScaleX(makeScalar()); break; case kSetScaleY: matrix.setScaleY(makeScalar()); break; case kSetScale: matrix.setScale(makeScalar(), makeScalar()); break; case kSetScaleTranslate: matrix.setScale(makeScalar(), makeScalar(), makeScalar(), makeScalar()); break; case kSetSkewX: matrix.setSkewX(makeScalar()); break; case kSetSkewY: matrix.setSkewY(makeScalar()); break; case kSetSkew: matrix.setSkew(makeScalar(), makeScalar()); break; case kSetSkewTranslate: matrix.setSkew(makeScalar(), makeScalar(), makeScalar(), makeScalar()); break; case kSetRotate: matrix.setRotate(makeScalar()); break; case kSetRotateTranslate: matrix.setRotate(makeScalar(), makeScalar(), makeScalar()); break; case kSetPerspectiveX: matrix.setPerspX(makeScalar()); break; case kSetPerspectiveY: matrix.setPerspY(makeScalar()); break; case kSetAll: matrix.setAll(makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar(), makeScalar()); break; } return matrix; }
/* Call matlab's fmincon */ int matlab_fmincon( double* x, /* result: optimum point */ double* fval, /* result: optimum function value */ int ndim, /* number of dimensions (variables) */ double (*objfun)(double *), /* objective function */ double* x0, /* starting point */ int nIneq, /* number of linear inequalities */ double* A, /* linear inequalities A*X <= B*/ double* B, int nEq, /* number of linear equalities */ double* Aeq, /* linear equalities Aeq*X = B*/ double* Beq, bool hasBounds, /* are there any bounds */ double* LB, /* lower bounds */ double* UB, /* upper bounds */ int nC, /* number of constraints */ double* (*C)(double *), /* non linear constraints */ int nCeq, /* number of equality constraints */ double* (*Ceq)(double *) /* non linear equality constraints */ ){ /** Intialize MCR and library **/ const char* ops = "-nodesktop"; /*const char* ops = "";*/ if (!mclInitializeApplication(&ops, 1)) { printf("Error Initializing Application\n"); return -1; } if (!liboptimizeInitialize()) { printf("Error Initializing Library\n"); return -1; } printf("Setting up matlab optimizer for a %i var problem\n",ndim); /** The objective function **/ /* Create a numeric datatype large enough to hold a pointer */ mxArray *of = mxCreateNumericMatrix(1,1,mxUINT64_CLASS,mxREAL); /* Save the pointer to the objective function as numeric */ *((mwSize*)mxGetData(of)) = (mwSize) objfun; printf(" Objective function set\n"); /** The starting point **/ mxArray *x0arr = makeArray(x0,ndim,1); printf(" Starting point set x0[0]=%f x0[1]=%f\n",x0[0],x0[1]); /** linear constraints **/ mxArray *Aarr = makeArray(A,nIneq,ndim); mxArray *Barr = makeArray(B,nIneq,1); printf(" %i inequality constraints set\n",nIneq); /** linear equality constraints **/ mxArray *Aeqarr = makeArray(Aeq,nEq,ndim); mxArray *Beqarr = makeArray(Beq,nEq,1); printf(" %i equality constraints set\n",nEq); /** variable bounds **/ int nb = ndim; if (!hasBounds) nb = 0; mxArray *LBarr = makeArray(LB,nb,1); mxArray *UBarr = makeArray(UB,nb,1); printf(" %i variable bounds set\n",nb); /** nonlinear constraints **/ mxArray *cf; mxArray *ncf = makeScalar(nC); if (nC > 0) { cf = mxCreateNumericMatrix(1,1,mxUINT64_CLASS,mxREAL); *((mwSize*)mxGetData(cf)) = (mwSize) C; }else{ cf = mxCreateNumericMatrix(0, 1, mxDOUBLE_CLASS, mxREAL); } printf(" %i nonlinear constraints set\n",nC); /** nonlinear equality constraints **/ mxArray *ecf; mxArray *necf = makeScalar(nCeq); if (nCeq > 0) { ecf = mxCreateNumericMatrix(1,1,mxUINT64_CLASS,mxREAL); *((mwSize*)mxGetData(ecf)) = (mwSize) Ceq; }else{ ecf = mxCreateNumericMatrix(0, 1, mxDOUBLE_CLASS, mxREAL); } printf(" %i nonlinear equality constraints set\n",nCeq); /** Call the matlab function **/ mxArray *xarr=NULL; mxArray *fvalarr=NULL; printf("Calling the matlab library\n"); mlfDoOptim(2,&xarr,&fvalarr,of,x0arr,Aarr,Barr,Aeqarr,Beqarr,LBarr,UBarr,ncf,cf,necf,ecf); printf("Returning results\n"); /** Get/display the results **/ double* xa = mxGetPr(xarr); double* fvala = mxGetPr(fvalarr); int i; for(i=0;i<ndim;++i) x[i] = xa[i]; for(i=0;i<1;++i) fval[i] = fvala[i]; printf("Cleaning up\n"); /** Cleanup **/ mxDestroyArray(of); mxDestroyArray(x0arr); mxDestroyArray(Aarr); mxDestroyArray(Barr); mxDestroyArray(Aeqarr); mxDestroyArray(Beqarr); mxDestroyArray(LBarr); mxDestroyArray(UBarr); mxDestroyArray(cf); mxDestroyArray(ecf); mxDestroyArray(ncf); mxDestroyArray(necf); mxDestroyArray(xarr); mxDestroyArray(fvalarr); liboptimizeTerminate(); mclTerminateApplication(); return 0; }