Ejemplo n.º 1
0
  /**
   * Check that each staff begins with a clef.
   *
   * @return the number of clefs rebuilt
   */
  @Override
  public int runPattern() {
    int successNb = 0;
    int staffId = 0;

    for (StaffInfo staff : system.getStaves()) {
      staffId++;

      // Define the inner box to intersect clef glyph(s)
      int left = (int) Math.rint(staff.getAbscissa(HorizontalSide.LEFT));
      Rectangle inner =
          new Rectangle(
              left + (2 * xOffset) + (clefWidth / 2),
              staff.getFirstLine().yAt(left) + (staff.getHeight() / 2),
              0,
              0);
      inner.grow((clefWidth / 2) - xOffset, (staff.getHeight() / 2) - yOffset);

      // Remember the box, for visual debug
      staff.addAttachment("  ci", inner);

      // We must find a clef out of these glyphs
      Collection<Glyph> glyphs = system.lookupIntersectedGlyphs(inner);
      logger.debug("{}{}", staffId, Glyphs.toString(" int", glyphs));

      // We assume than there can't be any alien among them, so we should
      // rebuild the larger glyph which the alien had wrongly segmented
      Set<Glyph> impacted = new HashSet<>();

      for (Glyph glyph : glyphs) {
        if (glyph.getShape() == Shape.STEM) {
          logger.debug("Clef: Removed stem#{}", glyph.getId());

          impacted.addAll(glyph.getConnectedNeighbors());
          impacted.add(glyph);
        }
      }

      if (!impacted.isEmpty()) {
        // Rebuild the larger glyph
        Glyph larger = system.buildCompound(impacted);
        if (larger != null) {
          logger.debug("Rebuilt stem-segmented {}", larger.idString());
        }

        // Recompute the set of intersected glyphs
        glyphs = system.lookupIntersectedGlyphs(inner);
      }

      if (checkClef(glyphs, staff)) {
        successNb++;
      }
    }

    return successNb;
  }
Ejemplo n.º 2
0
    @Override
    protected double getValue(GlyphContext context) {
      Glyph stick = context.stick;
      Rectangle box = getBox(stick);

      int aliens = getAlienPixelsIn(stick, box);
      int area = box.width * box.height;

      // Normalize the ratio with stick length
      double ratio = (1000 * aliens) / ((double) area * stick.getLength(Orientation.VERTICAL));

      logger.debug(
          "{} {} aliens:{} area:{} ratio:{}",
          stick.idString(),
          getName(),
          aliens,
          area,
          (float) ratio);

      return ratio;
    }
Ejemplo n.º 3
0
  /**
   * From the list of vertical sticks, this method uses several tests to provide the initial
   * collection of good barlines candidates.
   *
   * @param sticks the collection of candidate sticks
   */
  public void checkCandidates(Collection<? extends Glyph> sticks) {
    //        // Sort candidates according to their abscissa
    //        List<Glyph> sortedSticks = new ArrayList<Glyph>(sticks);
    //        Collections.sort(sortedSticks, Glyph.midPosComparator);
    double minResult = constants.minCheckResult.getValue();

    // Check each candidate stick in turn
    for (Glyph stick : sticks) {
      // Allocate the candidate context, and pass the whole check suite
      GlyphContext context = new GlyphContext(stick);

      double res = suite.pass(context);

      if (logger.isDebugEnabled() || stick.isVip()) {
        logger.info(
            "suite => {}{} for {}",
            (float) res,
            (stick.getResult() != null) ? (" " + stick.getResult()) : "",
            stick);
      }

      if ((stick.isBar() && stick.isManualShape()) || res >= minResult) {
        // OK, we flag this candidate with proper barline shape
        contexts.put(stick, context);
        if ((!stick.isBar() || !stick.isManualShape())) {
          stick.setShape(isThickBar(stick) ? Shape.THICK_BARLINE : Shape.THIN_BARLINE);
        }

        // Additional processing for Bars that define a system or a part
        // (they start AND end with precise staves horizontal limits)
        if ((context.topStaff != -1) && (context.botStaff != -1)) {
          // Here, we have both part & system defining bars
          // System bars occur first
          // (since glyphs are sorted by increasing abscissa)
          stick.setResult(BAR_PART_DEFINING);

          logger.debug(
              "Part-defining Barline from staff {} to staff {} {}",
              context.topStaff,
              context.botStaff,
              stick);
        } else {
          if (logger.isDebugEnabled()) {
            logger.debug(
                "Non-Part-defining Bar line {}{}",
                (context.topStaff != -1) ? (" topIdx=" + context.topStaff) : "",
                (context.botStaff != -1) ? (" botIdx=" + context.botStaff) : "");
          }

          stick.setResult(BAR_NOT_PART_DEFINING);
        }
      } else {
        if (stick.isBar()) {
          if (logger.isDebugEnabled() || stick.isVip()) {
            logger.info("Purged {} {}", stick.idString(), stick.getShape());
          }

          stick.setShape(null);
        }
      }
    }
  }
Ejemplo n.º 4
0
  /**
   * Try to recognize a clef in the compound of the provided glyphs.
   *
   * @param glyphs the parts of a clef candidate
   * @param staff the containing staff
   * @return true if successful
   */
  private boolean checkClef(Collection<Glyph> glyphs, StaffInfo staff) {
    if (glyphs.isEmpty()) {
      return false;
    }

    // Check if we already have a clef among the intersected glyphs
    Set<Glyph> clefs = Glyphs.lookupGlyphs(glyphs, clefGlyphPredicate);
    Glyph orgClef = null;

    if (!clefs.isEmpty()) {
      if (Glyphs.containsManual(clefs)) {
        return false; // Respect user decision
      } else {
        // Remember grade of the best existing clef
        for (Glyph glyph : clefs) {
          if ((orgClef == null) || (glyph.getGrade() > orgClef.getGrade())) {
            orgClef = glyph;
          }
        }
      }
    }

    // Remove potential aliens
    Glyphs.purgeManuals(glyphs);

    Glyph compound = system.buildTransientCompound(glyphs);

    // Check if a clef appears in the top evaluations
    Evaluation vote =
        GlyphNetwork.getInstance().vote(compound, system, Grades.clefMinGrade, clefShapePredicate);

    if ((vote != null) && ((orgClef == null) || (vote.grade > orgClef.getGrade()))) {
      // We now have a clef!
      // Look around for an even better result...
      logger.debug("{} built from {}", vote.shape, Glyphs.toString(glyphs));

      // Look for larger stuff
      Rectangle outer = compound.getBounds();
      outer.grow(xMargin, yMargin);

      // Remember the box, for visual debug
      staff.addAttachment("co", outer);

      List<Glyph> outerGlyphs = system.lookupIntersectedGlyphs(outer);
      outerGlyphs.removeAll(glyphs);
      Collections.sort(outerGlyphs, Glyph.byReverseWeight);

      final double minWeight = constants.minWeight.getValue();

      for (Glyph g : outerGlyphs) {
        // Consider only glyphs with a minimum weight
        if (g.getNormalizedWeight() < minWeight) {
          break;
        }

        logger.debug("Considering {}", g);

        Glyph newCompound = system.buildTransientCompound(Arrays.asList(compound, g));
        final Evaluation newVote =
            GlyphNetwork.getInstance()
                .vote(newCompound, system, Grades.clefMinGrade, clefShapePredicate);

        if ((newVote != null) && (newVote.grade > vote.grade)) {
          logger.debug("{} better built with {}", vote, g.idString());

          compound = newCompound;
          vote = newVote;
        }
      }

      // Register the last definition of the clef
      compound = system.addGlyph(compound);
      compound.setShape(vote.shape, Evaluation.ALGORITHM);

      logger.debug("{} rebuilt as {}", vote.shape, compound.idString());

      return true;
    } else {
      return false;
    }
  }