JDKFontInfo* createJDKFontInfo(JNIEnv *env, jobject font2D, jobject fontStrike, jfloat ptSize, jlong pNativeFont, jfloatArray matrix, jboolean aat) { JDKFontInfo *fi = (JDKFontInfo*)malloc(sizeof(JDKFontInfo)); if (!fi) { return NULL; } fi->env = env; // this is valid only for the life of this JNI call. fi->font2D = font2D; fi->fontStrike = fontStrike; fi->nativeFont = pNativeFont; fi->aat = aat; (*env)->GetFloatArrayRegion(env, matrix, 0, 4, fi->matrix); fi->ptSize = ptSize; fi->xPtSize = euclidianDistance(fi->matrix[0], fi->matrix[1]); fi->yPtSize = euclidianDistance(fi->matrix[2], fi->matrix[3]); if (!aat && (getenv("HB_NODEVTX") != NULL)) { fi->devScale = fi->xPtSize / fi->ptSize; } else { fi->devScale = 1.0f; } return fi; }
FontInstanceAdapter::FontInstanceAdapter(JNIEnv *theEnv, jobject theFont2D, jobject theFontStrike, float *matrix, le_int32 xRes, le_int32 yRes, le_int32 theUPEM, TTLayoutTableCache *ltables) : env(theEnv), font2D(theFont2D), fontStrike(theFontStrike), xppem(0), yppem(0), xScaleUnitsToPoints(0), yScaleUnitsToPoints(0), xScalePixelsToUnits(0), yScalePixelsToUnits(0), upem(theUPEM), layoutTables(ltables) { xPointSize = euclidianDistance(matrix[0], matrix[1]); yPointSize = euclidianDistance(matrix[2], matrix[3]); txMat[0] = matrix[0]/xPointSize; txMat[1] = matrix[1]/xPointSize; txMat[2] = matrix[2]/yPointSize; txMat[3] = matrix[3]/yPointSize; xppem = ((float) xRes / 72) * xPointSize; yppem = ((float) yRes / 72) * yPointSize; xScaleUnitsToPoints = xPointSize / upem; yScaleUnitsToPoints = yPointSize / upem; xScalePixelsToUnits = upem / xppem; yScalePixelsToUnits = upem / yppem; };
QPoint Ghost::clydeChaseTarget() { QPoint clydeTarget = pacman->position(); if(euclidianDistance(clyde->position(), clydeTarget) < 8) clydeTarget = clyde->scatterCorner(); return clydeTarget; }
void SOM::adjustWeights(int index, std::vector<double>& inputData) { long double sigma; long double lateralDistance; long double h; long double eta; int xWinner = winnerNeuron / dimension; int yWinner = winnerNeuron % dimension; for (unsigned int x = 0; x < neurons.size(); x++) { for (unsigned int y = 0; y < neurons[x].size(); y++) { sigma = sigma0 * exp(-index / tau); lateralDistance = euclidianDistance(xWinner, yWinner, x, y); h = exp( -lateralDistance / (2 * pow(sigma, 2)) ); eta = eta0 * exp( -index /tau ); if(eta <= 0.01) eta = 0.02; for (int w = 0; w < neurons[x][y].numSynapses; w++) { neurons[x][y].weights[w] += eta * h * (inputData[w] - neurons[x][y].weights[w]); } } } }
GLdouble computeDimension (struct attractor *at) { /* An estimate of the correlation dimension: accumulate the values of the distances between point p and one of its predecessors, ignoring the points right before p */ GLdouble n1 = 0.0, n2 = 0.0, d2; GLdouble d2max = 4 * at->r * at->r; /* Square of the attractor radius */ int twod = 1 << at->dimension; int i, j; if (at->numPoints <= DIM_DEPTH) { return -1; } for (i = DIM_DEPTH; i < at->numPoints; i++) { j = i - DIM_IGNORE - (rand () % (DIM_DEPTH - DIM_IGNORE)); d2 = euclidianDistance (at->array[i], at->array[j]); if (d2 < DIM_RADIUS1 * twod * d2max) n2++; if (d2 > DIM_RADIUS2 * twod * d2max) continue; n1++; } at->correlationDimension = log10 (n2 / n1); return at->correlationDimension; }
void clarke_wright(std::vector<Vertices> vec, float** distances ){ Vertice hub = vec.front(); std::vector<Vertices> vh(vec); vh.pop_front(); std::vector<Vertice> savings; std::vector<Vertices>::iterator i; std::vector<Vertices>::iterator j; float a,b,ij; for (i= vec.begin(); i < vec.end(); ++i){ for (j = veg.begin(); j < vec.end(); ++j){ a = euclidianDistance(hub, vh[i], distances); b = euclidianDistance(hub, vh[j], distances); ij = euclidianDistance(vh[i], vh[j], distances); Vertice vi; vi.x = i; vi.y = j; } } }
// Takes contours and finds circles // Theory: find the circle and ellipse that fit around a contour. If the contour is circular, the circle and ellipse should be similar // Also, if the contour is circular, the fit circle should be a good approximation of it void findCircles(std::vector<std::vector<cv::Point> > contours, std::vector<struct ColouredCircle> &circles, double centerEpsilon, double radiusEpsilon) { // Iterate over each contour for(size_t i = 0; i < contours.size(); i++) { std::vector<cv::Point> contour = contours[i]; // Discard is less than five points in contour // Both as can't fit ellipse otherwise and is unlikely to be a circle with five points if(contour.size() < 5) { continue; } // Find minimum enclosing circle cv::Point2f center; float radius; cv::minEnclosingCircle(contour, center, radius); // Fit an ellipse around the contour cv::RotatedRect ellipse = cv::fitEllipse(contour); cv::Point2f eCenter = ellipse.center; cv::Point2f eAxesRadius = ellipse.size; // Need to half to as currently axes (diameter) eAxesRadius.x = eAxesRadius.x / 2; eAxesRadius.y = eAxesRadius.y / 2; // Compare the circle and ellipse // Uses Euclidian distance. Better alternatives? double centerDistance = euclidianDistance(center, eCenter); double radiusDistance = euclidianDistance(cv::Point2f(radius, radius), eAxesRadius); if(centerDistance > centerEpsilon || radiusDistance > radiusEpsilon) { continue; } // Add the found circle to the output vector struct ColouredCircle cc; cc.center = cv::Point2f(center.x, center.y); cc.radius = radius; circles.push_back(cc); } }
FontInstanceAdapter::FontInstanceAdapter(fontObject *theFontObject, Strike *theStrike, FontTransform *tx, le_int32 xRes, le_int32 yRes) : fFontObject(theFontObject), fStrike(theStrike), xppem(0), yppem(0), xScaleUnitsToPoints(0), yScaleUnitsToPoints(0), xScalePixelsToUnits(0), yScalePixelsToUnits(0) { float upem = (float) fFontObject->GetUnitsPerEM(); float matrix[4]; float xPointSize, yPointSize; tx->getMatrixInto(matrix, 4); xPointSize = euclidianDistance(matrix[0], matrix[1]); yPointSize = euclidianDistance(matrix[2], matrix[3]); xppem = ((float) xRes / 72) * xPointSize; yppem = ((float) yRes / 72) * yPointSize; xScaleUnitsToPoints = xPointSize / upem; yScaleUnitsToPoints = yPointSize / upem; xScalePixelsToUnits = upem / xppem; yScalePixelsToUnits = upem / yppem; };
int Ghost::findNextdirection(QPoint tileTarget, bool ignorePreviousDirection) { QList<int> availableDirections; QList<QPoint> availableMoves; QPoint tmpPoint; DoublePosition tmpPosition; int x,y, collision; for(int i=0; i<4; i++) { x = (i/2)*(-1 + 2*(i%2)); y = (1 - (i/2))*(-1 + 2*(i%2)); tmpPosition = m_position; tmpPosition.increaseX(m_speed * x); tmpPosition.increaseY(m_speed * y); tmpPoint = m_position.integerPosition() + QPoint(x,y); collision = m_grid->collisionAt(tmpPoint); if((collision != Grid::CollisionOn)&&((i != m_previousDirection)||(ignorePreviousDirection))) { availableDirections.append(i); availableMoves.append(tmpPoint); } } if(availableDirections.size() == 1) return availableDirections.first(); int minIdx = -1; qreal dist, min = 10000.0; for(int i=0; i<availableDirections.size(); i++) { dist = euclidianDistance(tileTarget, availableMoves.at(i)); if(min>dist) { min = dist; minIdx = i; } } if(minIdx == -1) return -1; return availableDirections.at(minIdx); }
JNIEXPORT jlong JNICALL Java_sun_font_FreetypeFontScaler_createScalerContextNative( JNIEnv *env, jobject scaler, jlong pScaler, jdoubleArray matrix, jint aa, jint fm, jfloat boldness, jfloat italic) { double dmat[4], ptsz; FTScalerContext *context = (FTScalerContext*) calloc(1, sizeof(FTScalerContext)); FTScalerInfo *scalerInfo = (FTScalerInfo*) jlong_to_ptr(pScaler); if (context == NULL) { invalidateJavaScaler(env, scaler, NULL); return (jlong) 0; } (*env)->GetDoubleArrayRegion(env, matrix, 0, 4, dmat); ptsz = euclidianDistance(dmat[2], dmat[3]); //i.e. y-size if (ptsz < 1.0) { //text can not be smaller than 1 point ptsz = 1.0; } context->ptsz = (int)(ptsz * 64); context->transform.xx = FloatToFTFixed((float)dmat[0]/ptsz); context->transform.yx = -FloatToFTFixed((float)dmat[1]/ptsz); context->transform.xy = -FloatToFTFixed((float)dmat[2]/ptsz); context->transform.yy = FloatToFTFixed((float)dmat[3]/ptsz); context->aaType = aa; context->fmType = fm; /* If using algorithmic styling, the base values are * boldness = 1.0, italic = 0.0. */ context->doBold = (boldness != 1.0); context->doItalize = (italic != 0); return ptr_to_jlong(context); }
float getLength(sf::Vector3f x) { return euclidianDistance(sf::Vector3f(0.f, 0.f, 0.f), x); }
GLdouble getRadius (struct attractor *a) { return 0.5 * sqrt (euclidianDistance (a->bound[1], a->bound[0])); }
int main(const int argc, const char* argv[]) { // Ensure OpenCV is using optimized code cv::setUseOptimized(true); if(argc < 2) { std::cerr << "Named of named pipe required" << std::endl; return 1; } std::ofstream namedPipe(argv[1], std::ofstream::out); vision::Config config("config/config.xml"); vision::Camera camera(0, config.getPitch(0)); //vision::Camera camera("/home/euan/Downloads/pitch (1).avi", config.getPitch(0)); cv::Point2f lastBallPos; bool seenBall = false; bool seenYellowGreen = false; cv::Point2f lastYGPos; double lastYGO = 0; bool seenYellowPink = false; cv::Point2f lastYPPos; double lastYPO = 0; bool seenBlueGreen = false; cv::Point2f lastBGPos; double lastBGO = 0; bool seenBluePink = false; cv::Point2f lastBPPos; double lastBPO = 0; // Main processing loop // Grabs, decodes and stores a frame from capture each iteration while(camera.update()) { cv::UMat frame = camera.getFrame(); std::vector<std::vector<struct ColouredCircle> > circles = findColouredCirclesInFrame(frame, camera.getBackgroundImage()); for(size_t r=0;r<circles[0].size();r++) { for(size_t c=1;c<circles.size();c++) { for(size_t i=0;i<circles[c].size();i++) { double dist = euclidianDistance(circles[0][r].center, circles[c][i].center); if(dist < 20) { circles[0][r].radius = 0; break; } } } } cv::Point2f ball; double ballSize = 2; bool ballFound = false; for(size_t i = 0; i < circles[0].size(); i++) { if(circles[0][i].radius > ballSize) { ballFound = true; ball = circles[0][i].center; ballSize = circles[0][i].radius; } } std::vector<struct ColouredCircle> pinkAndGreen = circles[3]; pinkAndGreen.insert(pinkAndGreen.end(), circles[4].begin(), circles[4].end()); bool ygFound = false; bool ypFound = false; bool bgFound = false; bool bpFound = false; std::vector<struct Robot> robots; for(size_t blue = 0; blue < circles[1].size(); blue++) { sort(pinkAndGreen.begin(), pinkAndGreen.end(), [circles, blue](struct ColouredCircle x, struct ColouredCircle y) { double distX = euclidianDistance(circles[1][blue].center, x.center); double distY = euclidianDistance(circles[1][blue].center, y.center); return (distX < distY); }); size_t len = 4; if(pinkAndGreen.size() < len) { continue; } std::vector<struct ColouredCircle> markers; int numG = 0, numP = 0; for(size_t i = 0; i < len; i++) { markers.push_back(pinkAndGreen[i]); if(pinkAndGreen[i].colour == 3) { numP++; } else { numG++; } } bool shouldContinue = true; for(size_t i = 0; i < markers.size(); i++) { double dist = euclidianDistance(circles[1][blue].center, markers[i].center); if(dist > 50) { shouldContinue = false; break; } } if(!shouldContinue) { continue; } struct Robot r; if(numP < numG) { r.pos = circles[1][blue].center; size_t pindex = 0; for(size_t i = 0; i < markers.size(); i++) { if(markers[i].colour == 3) { pindex = i; } } cv::Point2f botLeftOrigin = markers[pindex].center - r.pos; r.orientation = -atan2(botLeftOrigin.x, botLeftOrigin.y) - 0.4; r.team = 0; r.colour = 0; r.markers = markers; bgFound = true; } else { r.pos = circles[1][blue].center; size_t pindex = 0; for(size_t i = 0; i < markers.size(); i++) { if(markers[i].colour == 4) { pindex = i; } } cv::Point2f botLeftOrigin = markers[pindex].center - r.pos; r.orientation = -atan2(botLeftOrigin.x, botLeftOrigin.y) - 0.4; r.team = 0; r.colour = 1; r.markers = markers; bpFound = true; } robots.push_back(r); } for(size_t yellow = 0; yellow < circles[2].size(); yellow++) { sort(pinkAndGreen.begin(), pinkAndGreen.end(), [circles, yellow](struct ColouredCircle x, struct ColouredCircle y) { double distX = euclidianDistance(circles[2][yellow].center, x.center); double distY = euclidianDistance(circles[2][yellow].center, y.center); return (distX < distY); }); size_t len = 4; if(pinkAndGreen.size() < len) { continue; } std::vector<struct ColouredCircle> markers; int numG = 0, numP = 0; for(size_t i = 0; i < len; i++) { markers.push_back(pinkAndGreen[i]); if(pinkAndGreen[i].colour == 3) { numP++; } else { numG++; } } bool shouldContinue = true; for(size_t i = 0; i < markers.size(); i++) { double dist = euclidianDistance(circles[2][yellow].center, markers[i].center); if(dist > 50) { shouldContinue = false; break; } } if(!shouldContinue) { continue; } struct Robot r; if(numP < numG) { r.pos = circles[2][yellow].center; size_t pindex = 0; for(size_t i = 0; i < markers.size(); i++) { if(markers[i].colour == 3) { pindex = i; } } cv::Point2f botLeftOrigin = markers[pindex].center - r.pos; r.orientation = -atan2(botLeftOrigin.x, botLeftOrigin.y) - 0.4; r.team = 1; r.colour = 0; r.markers = markers; ygFound = true; } else { r.pos = circles[2][yellow].center; size_t pindex = 0; for(size_t i = 0; i < markers.size(); i++) { if(markers[i].colour == 4) { pindex = i; } } cv::Point2f botLeftOrigin = markers[pindex].center - r.pos; r.orientation = -atan2(botLeftOrigin.x, botLeftOrigin.y) - 0.4; r.team = 1; r.colour = 1; r.markers = markers; ypFound = true; } robots.push_back(r); } std::vector<std::string> jsonKeyObjects; if(!ballFound) { ball = lastBallPos; } else { std::stringstream jsonBall; jsonBall << "\"b\":{\"x\":" << ball.x << ",\"y\":" << ball.y << "}"; jsonKeyObjects.push_back(jsonBall.str()); } cv::arrowedLine(frame, ball, ball + (ball-lastBallPos)*10, cv::Scalar(255,255,255), 3); cv::circle(frame, ball, 10, cv::Scalar(0, 0, 255), 3); lastBallPos = ball; for(size_t i = 0; i < robots.size(); i++) { if(robots[i].team == 0 && robots[i].colour == 0) { bgFound = true; lastBGPos = robots[i].pos; lastBGO = robots[i].orientation; } else if(robots[i].team == 0 && robots[i].colour == 1) { bpFound = true; lastBPPos = robots[i].pos; lastBPO = robots[i].orientation; } else if(robots[i].team == 1 && robots[i].colour == 0) { ygFound = true; lastYGPos = robots[i].pos; lastYGO = robots[i].orientation; } else if(robots[i].team == 1 && robots[i].colour == 1) { ypFound = true; lastYPPos = robots[i].pos; lastYPO = robots[i].orientation; } } if(bgFound) { seenBlueGreen = true; } else if(seenBlueGreen) { seenBlueGreen = false; cv::circle(frame, lastBGPos, 10, cv::Scalar(255, 0, 0), 3); cv::circle(frame, lastBGPos, 50, cv::Scalar(0, 255, 0), 3); cv::Point2f newPoint( float(0 * cos(lastBGO) - (-20 * sin(lastBGO))), float(0 * sin(lastBGO) + (-20 * cos(lastBGO))) ); cv::line(frame, lastBGPos, lastBGPos + newPoint, cv::Scalar(0, 255, 0), 2); } if(bpFound) { seenBluePink = true; } else if(seenBluePink) { seenBluePink = false; cv::circle(frame, lastBPPos, 10, cv::Scalar(255, 0, 0), 3); cv::circle(frame, lastBPPos, 50, cv::Scalar(255, 0, 255), 3); cv::Point2f newPoint( float(0 * cos(lastBPO) - (-20 * sin(lastBPO))), float(0 * sin(lastBPO) + (-20 * cos(lastBPO))) ); cv::line(frame, lastBPPos, lastBPPos + newPoint, cv::Scalar(0, 255, 0), 2); } if(ygFound) { seenYellowGreen = true; } else if(seenYellowGreen) { seenYellowGreen = false; cv::circle(frame, lastYGPos, 10, cv::Scalar(0, 255, 255), 3); cv::circle(frame, lastYGPos, 50, cv::Scalar(0, 255, 0), 3); cv::Point2f newPoint( float(0 * cos(lastYGO) - (-20 * sin(lastYGO))), float(0 * sin(lastYGO) + (-20 * cos(lastYGO))) ); cv::line(frame, lastYGPos, lastYGPos + newPoint, cv::Scalar(0, 255, 0), 2); } if(ypFound) { seenYellowPink = true; } else if(seenYellowPink) { seenYellowPink = false; cv::circle(frame, lastYPPos, 10, cv::Scalar(0, 255, 255), 3); cv::circle(frame, lastYPPos, 50, cv::Scalar(255, 0, 255), 3); cv::Point2f newPoint( float(0 * cos(lastYPO) - (-20 * sin(lastYPO))), float(0 * sin(lastYPO) + (-20 * cos(lastYPO))) ); cv::line(frame, lastYPPos, lastYPPos + newPoint, cv::Scalar(0, 255, 0), 2); } for(size_t i = 0; i < robots.size(); i++) { struct Robot r = robots[i]; cv::circle(frame, r.pos, 10, (r.team == 0 ? cv::Scalar(255, 0, 0) : cv::Scalar(0, 255, 255)), 3); cv::circle(frame, r.pos, 50, (r.colour == 0 ? cv::Scalar(0, 255, 0) : cv::Scalar(255, 0, 255)), 3); cv::Point2f newPoint( float(0 * cos(r.orientation) - (-20 * sin(r.orientation))), float(0 * sin(r.orientation) + (-20 * cos(r.orientation))) ); cv::line(frame, r.pos, r.pos + newPoint, cv::Scalar(0, 255, 0), 2); std::stringstream jsonRobot; jsonRobot << '"'; if(r.team == 0) { jsonRobot << 'b'; } else { jsonRobot << 'y'; } if(r.colour == 0) { jsonRobot << 'g'; } else { jsonRobot << 'p'; } jsonRobot << "\":{\"x\":" << r.pos.x << ",\"y\":" << r.pos.y << ",\"f\":" << r.orientation * 180 / M_PI << "}"; jsonKeyObjects.push_back(jsonRobot.str()); } namedPipe << '{'; for(size_t j = 0; j < jsonKeyObjects.size(); j++) { namedPipe << jsonKeyObjects[j]; if(j != jsonKeyObjects.size() - 1) { namedPipe << ','; } } namedPipe << "}\n"; std::flush(namedPipe); cv::imshow("Frame", frame); // Update windows and check if a key has been pressed char key = char(cv::waitKey(1)); // Check if user wants to quit if(key == 'q' || key == 'Q' || key == ESC_KEY) { break; } } }