Exemple #1
0
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);

}
Exemple #2
0
void SVGPathNormalizer::emitSegment(const PathSegmentData& segment) {
  PathSegmentData normSeg = segment;

  // Convert relative points to absolute points.
  switch (segment.command) {
    case PathSegCurveToQuadraticRel:
      normSeg.point1 += m_currentPoint;
      normSeg.targetPoint += m_currentPoint;
      break;
    case PathSegCurveToCubicRel:
      normSeg.point1 += m_currentPoint;
    /* fall through */
    case PathSegCurveToCubicSmoothRel:
      normSeg.point2 += m_currentPoint;
    /* fall through */
    case PathSegMoveToRel:
    case PathSegLineToRel:
    case PathSegLineToHorizontalRel:
    case PathSegLineToVerticalRel:
    case PathSegCurveToQuadraticSmoothRel:
    case PathSegArcRel:
      normSeg.targetPoint += m_currentPoint;
      break;
    case PathSegLineToHorizontalAbs:
      normSeg.targetPoint.setY(m_currentPoint.y());
      break;
    case PathSegLineToVerticalAbs:
      normSeg.targetPoint.setX(m_currentPoint.x());
      break;
    case PathSegClosePath:
      // Reset m_currentPoint for the next path.
      normSeg.targetPoint = m_subPathPoint;
      break;
    default:
      break;
  }

  // Update command verb, handle smooth segments and convert quadratic curve
  // segments to cubics.
  switch (segment.command) {
    case PathSegMoveToRel:
    case PathSegMoveToAbs:
      m_subPathPoint = normSeg.targetPoint;
      normSeg.command = PathSegMoveToAbs;
      break;
    case PathSegLineToRel:
    case PathSegLineToAbs:
    case PathSegLineToHorizontalRel:
    case PathSegLineToHorizontalAbs:
    case PathSegLineToVerticalRel:
    case PathSegLineToVerticalAbs:
      normSeg.command = PathSegLineToAbs;
      break;
    case PathSegClosePath:
      normSeg.command = PathSegClosePath;
      break;
    case PathSegCurveToCubicSmoothRel:
    case PathSegCurveToCubicSmoothAbs:
      if (!isCubicCommand(m_lastCommand))
        normSeg.point1 = m_currentPoint;
      else
        normSeg.point1 = reflectedPoint(m_currentPoint, m_controlPoint);
    /* fall through */
    case PathSegCurveToCubicRel:
    case PathSegCurveToCubicAbs:
      m_controlPoint = normSeg.point2;
      normSeg.command = PathSegCurveToCubicAbs;
      break;
    case PathSegCurveToQuadraticSmoothRel:
    case PathSegCurveToQuadraticSmoothAbs:
      if (!isQuadraticCommand(m_lastCommand))
        normSeg.point1 = m_currentPoint;
      else
        normSeg.point1 = reflectedPoint(m_currentPoint, m_controlPoint);
    /* fall through */
    case PathSegCurveToQuadraticRel:
    case PathSegCurveToQuadraticAbs:
      // Save the unmodified control point.
      m_controlPoint = normSeg.point1;
      normSeg.point1 = blendPoints(m_currentPoint, m_controlPoint);
      normSeg.point2 = blendPoints(normSeg.targetPoint, m_controlPoint);
      normSeg.command = PathSegCurveToCubicAbs;
      break;
    case PathSegArcRel:
    case PathSegArcAbs:
      if (!decomposeArcToCubic(m_currentPoint, normSeg)) {
        // On failure, emit a line segment to the target point.
        normSeg.command = PathSegLineToAbs;
      } else {
        // decomposeArcToCubic() has already emitted the normalized
        // segments, so set command to PathSegArcAbs, to skip any further
        // emit.
        normSeg.command = PathSegArcAbs;
      }
      break;
    default:
      ASSERT_NOT_REACHED();
  }

  if (normSeg.command != PathSegArcAbs)
    m_consumer->emitSegment(normSeg);

  m_currentPoint = normSeg.targetPoint;

  if (!isCubicCommand(segment.command) && !isQuadraticCommand(segment.command))
    m_controlPoint = m_currentPoint;

  m_lastCommand = segment.command;
}