const Vector2& Geom::GetCentroid() const { if (dirtyCentroid) calculateCentroid(); return centroid; }
void Gesture::vectorize(bool angleSensitive) { assert(resampled_points.size()); rotated_points.clear(); Vec2 center = calculateCentroid(); translate(-center); float indicative_angle = atan2(resampled_points[0].y, resampled_points[0].x); float delta = -indicative_angle; if(angleSensitive) { float base_orientation = (PI * 0.25f) * floor( (indicative_angle + PI / 8.0f) / (PI / 4.0f)); delta = base_orientation - indicative_angle; } Vec2 rotated_point; float cosa = cos(delta); float sina = sin(delta); float sum = 0.0f; for(std::vector<Vec2>::iterator it = resampled_points.begin(); it != resampled_points.end(); ++it) { Vec2& p = *it; rotated_point.x = p.x * cosa - p.y * sina; rotated_point.y = p.y * cosa + p.x * sina; rotated_points.push_back(rotated_point); sum += (rotated_point.x * rotated_point.x) + (rotated_point.y * rotated_point.y); } sum = sqrt(sum); for(std::vector<Vec2>::iterator it = rotated_points.begin(); it != rotated_points.end(); ++it) { (*it) /= sum; } }
void Mesh::suavize(Vertex (*gradient)(Vertex)){ copyVertices(0); Vertex normal, baricentro, desloc; Vertex *toBeChanged; double alpha = 0.1; for (int i = 0; i < vcount; ++i) { normal = gradient( *verts[i] ); baricentro = calculateCentroid(i); desloc = baricentro - *verts[i]; // printf("#Normal: "); normal.printvert(); printf(" -> %lf ", sqrt(normal.dot(normal))); printf("\n"); // printf("#Baricentro: "); baricentro.printvert(); printf(" -> %lf ", sqrt(baricentro.dot(baricentro))); printf("\n"); // printf("#Deslocamento: "); desloc.printvert(); printf(" -> %lf ", sqrt(desloc.dot(desloc))); printf("\n"); // printf("\n"); normal = normal / sqrt(normal.dot(normal)); // baricentro = baricentro / sqrt(baricentro.dot(baricentro)); // desloc = desloc / sqrt(desloc.dot(desloc)); toBeChanged = vcopy[i]; *toBeChanged = (*verts[i]) + ( desloc - normal*(desloc.dot(normal)) )*alpha; } copyVertices(1); }
void NelderMead::process() { int count = 0; int maxCount = FileParser::getKey("NELDER_MEAD_CYCLES", 100); while ((!converged() && count < maxCount) || unlimited) { sendLog(LogLevelDebug); std::vector<double> centroid = calculateCentroid(); count++; logged << "Evaluation of best point: " << testPoints[0].second << std::endl; sendLog(LogLevelDebug); TestPoint reflected = reflectedPoint(centroid); if (reflected.second < testPoints[1].second) { logged << "Reflecting" << std::endl; setWorstTestPoint(reflected); continue; } if (reflected.second < testPoints[0].second) { TestPoint expanded = expandedPoint(centroid); bool expandedBetter = (expanded.second < reflected.second); setWorstTestPoint(expandedBetter ? expanded : reflected); logged << (expandedBetter ? "Expanding" : "Reflecting") << std::endl; continue; } TestPoint contracted = contractedPoint(centroid); TestPoint *worstPoint = worstTestPoint(); if (contracted.second < worstPoint->second) { logged << "Contracting" << std::endl; setWorstTestPoint(contracted); continue; } else { logged << "Reducing" << std::endl; reduction(); } } orderTestPoints(); setTestPointParameters(&testPoints[0]); logged << "Evaluation of best point: " << testPoints[0].second << std::endl; sendLog(LogLevelDetailed); }
void ofxFace::addVertex(ofxVertex *vertex) { vertex->addFace(this); verts.push_back(vertex); if(verts.size() == 3) { calculateCentroid(); calculateNormal(); } }
Mesh(const VertsRangeT& verts, int coordsPerVertex, GLenum renderStyle, const ColorsRangeT& colors, int componentsPerColor) : coordsPerVertex_(coordsPerVertex) , componentsPerColor_(componentsPerColor) , renderStyle_(renderStyle) , radius_(0) { boost::range::push_back(vertexes_, verts); boost::range::push_back(colors_, colors); calculateCentroid(); calculateRadius(); }
void movePointUntilAdditionalConstraintBeFulfilled(Point points[], int pointIndex) { Point centroid; int i; centroid = calculateCentroid(points, pointIndex); i = 0; while(!isAdditionalConstraintFulfilled(points[pointIndex].coordinators) && i < MAX_TRY_OF_MOVE_POINT) { movePointHalfWayToCentroid(&points[pointIndex], centroid); i++; } }
Point reflectPoint(Point points[], int numberOfPoints) { Point centroid, reflectedPoint; bool isReflectedPointAccepted; double alpha; int indexOfLastPoint, i; indexOfLastPoint = numberOfPoints - 1; alpha = ALPHA; moveMaxValuePointAtLastPosition(points, numberOfPoints); centroid = calculateCentroid(points, indexOfLastPoint); reflectedPoint = generateReflectedPoint(points[indexOfLastPoint], centroid, alpha); isReflectedPointAccepted = false; i = 0; while(!isReflectedPointAccepted && i < MAX_TRY_OF_REFLECTED_POINT) { reflectedPoint.objectiveFunctionValue = calculateObjectiveFunctionValue(reflectedPoint.coordinators); if(isReflectedPointFulfillConstraints(reflectedPoint)) { if(isValueOfReflectedPointLessThanMaxValue(reflectedPoint, points, indexOfLastPoint)) { replaceMaxValuePointWithReflectedPoint(reflectedPoint, points, indexOfLastPoint); isReflectedPointAccepted = true; } else { alpha = alpha / 2; if(alpha > ALPHA_ACCURACY) { reflectedPoint = generateReflectedPoint(points[indexOfLastPoint], centroid, alpha); } else { printf("Ups cant generate reflected point for max value point"); exit(0); } } } else { movePointUntilAllConstraintsBeFulfilled(&reflectedPoint, centroid); } i++; } return reflectedPoint; }
//---------------------------------------- void ofxBox2dPolygon::updateShape() { calculateArea(); calculateCentroid(); bounds = getBoundingBox(); }
//---------------------------------------- void ofxBox2dPolygon::updateShape() { calculateArea(); calculateCentroid(); }
NelderMead::NelderMead(std::vector<double *> newParamPtrs, std::vector<double> expectedRanges, void *object, double (*score)(void *object)) { alpha = 1; gamma = 2; rho = -0.5; sigma = 0.5; unlimited = false; std::vector<double *>streamlinedPtrs; for (int i = 0; i < newParamPtrs.size(); i++) { if (newParamPtrs[i] != NULL) streamlinedPtrs.push_back(newParamPtrs[i]); else { expectedRanges.erase(expectedRanges.begin() + i); newParamPtrs.erase(newParamPtrs.begin() + i); i--; } } paramPtrs = streamlinedPtrs; evaluationFunction = score; evaluatingObject = object; int testPointCount = (int)paramCount() + 1; testPoints.resize(testPointCount); if (paramCount() == 0) return; assert(paramCount() > 1); for (int i = 0; i < testPoints.size(); i++) { testPoints[i].second = 0; testPoints[i].first.resize(paramCount()); logged << "Test point parameter " << i << ": "; for (int j = 0; j < paramCount(); j++) { if (i == 0) { testPoints[i].first[j] = *paramPtrs[j]; } if (i > 0) { int minJ = i - 1; double scale = 2; testPoints[i].first[j] = testPoints[0].first[j] + (j == minJ) * scale * expectedRanges[j]; } logged << testPoints[i].first[j] << ", " << std::endl; } } logged << "Test point evaluations: "; for (int i = 0; i < testPoints.size(); i++) { evaluateTestPoint(i); logged << testPoints[i].second << ", "; } logged << std::endl; std::vector<double> centroid = calculateCentroid(); logged << "Starting centroid: " << std::endl; for (int i = 0; i < centroid.size(); i++) { logged << centroid[i] << ", "; } logged << std::endl; sendLog(LogLevelDebug); }