Esempio n. 1
0
std::size_t Manager::OutputAlignmentNBest(
    Alignments &retAlign,
    const KBestExtractor::Derivation &derivation,
    std::size_t startTarget) const
{
  const SHyperedge &shyperedge = derivation.edge->shyperedge;

  std::size_t totalTargetSize = 0;
  std::size_t startSource = shyperedge.head->pvertex->span.GetStartPos();

  const TargetPhrase &tp = *(shyperedge.translation);

  std::size_t thisSourceSize = CalcSourceSize(derivation);

  // position of each terminal word in translation rule, irrespective of
  // alignment if non-term, number is undefined
  std::vector<std::size_t> sourceOffsets(thisSourceSize, 0);
  std::vector<std::size_t> targetOffsets(tp.GetSize(), 0);

  const AlignmentInfo &aiNonTerm = shyperedge.translation->GetAlignNonTerm();
  std::vector<std::size_t> sourceInd2pos = aiNonTerm.GetSourceIndex2PosMap();
  const AlignmentInfo::NonTermIndexMap &targetPos2SourceInd =
      aiNonTerm.GetNonTermIndexMap();

  UTIL_THROW_IF2(sourceInd2pos.size() != derivation.subderivations.size(),
      "Error");

  std::size_t targetInd = 0;
  for (std::size_t targetPos = 0; targetPos < tp.GetSize(); ++targetPos) {
    if (tp.GetWord(targetPos).IsNonTerminal()) {
      UTIL_THROW_IF2(targetPos >= targetPos2SourceInd.size(), "Error");
      std::size_t sourceInd = targetPos2SourceInd[targetPos];
      std::size_t sourcePos = sourceInd2pos[sourceInd];

      const KBestExtractor::Derivation &subderivation =
        *derivation.subderivations[sourceInd];

      // calc source size
      std::size_t sourceSize =
        subderivation.edge->head->svertex.pvertex->span.GetNumWordsCovered();
      sourceOffsets[sourcePos] = sourceSize;

      // calc target size.
      // Recursively look thru child hypos
      std::size_t currStartTarget = startTarget + totalTargetSize;
      std::size_t targetSize = OutputAlignmentNBest(retAlign, subderivation,
          currStartTarget);
      targetOffsets[targetPos] = targetSize;

      totalTargetSize += targetSize;
      ++targetInd;
    } else {
      ++totalTargetSize;
    }
  }

  // convert position within translation rule to absolute position within
  // source sentence / output sentence
  ShiftOffsets(sourceOffsets, startSource);
  ShiftOffsets(targetOffsets, startTarget);

  // get alignments from this hypo
  const AlignmentInfo &aiTerm = shyperedge.translation->GetAlignTerm();

  // add to output arg, offsetting by source & target
  AlignmentInfo::const_iterator iter;
  for (iter = aiTerm.begin(); iter != aiTerm.end(); ++iter) {
    const std::pair<std::size_t, std::size_t> &align = *iter;
    std::size_t relSource = align.first;
    std::size_t relTarget = align.second;
    std::size_t absSource = sourceOffsets[relSource];
    std::size_t absTarget = targetOffsets[relTarget];

    std::pair<std::size_t, std::size_t> alignPoint(absSource, absTarget);
    std::pair<Alignments::iterator, bool> ret = retAlign.insert(alignPoint);
    UTIL_THROW_IF2(!ret.second, "Error");
  }

  return totalTargetSize;
}
Esempio n. 2
0
void GeometryClipmaps::update( const Pnt3f& localViewerPos )
{
    assert( heightSource_ );
    
    // todo: transform world to modelposition (inverse transform)
    Pnt2i viewerSamplePos = Pnt2i( int(localViewerPos[0]),
                                   int(localViewerPos[2]) );
    
    // for debugging:
    static Pnt2i oldViewerPos;
    
    if( viewerSamplePos != oldViewerPos && !notFinishedLastFrame_ )
    {
        oldViewerPos = viewerSamplePos;
    }
    
    notFinishedLastFrame_ = false;
    
    cpuRenderer_.updateRenderData( localViewerPos );
    
    const int levelCount = int(levels_.size());
    
    // update clip rect:
    int updatedSampleCount = 0;
    
    // from coarse to fine:
    for( int levelIdx = 0; levelIdx < levelCount; ++levelIdx )
    {
        GeometryClipmapLevel& level = levels_[ levelIdx ];
        GeometryClipmapLevel* parentLevel = 0;
        
        if( levelIdx > 0 )
        {
            parentLevel = &levels_[ levelIdx - 1 ];
        }
        
        const int halfSampleCoverage = level.getSampleCoverage() / 2;
        
        // set the desired origin of the level: (aligned to the the sample spacing of this level )
        Pnt2i newOrigin = alignPoint( viewerSamplePos - Pnt2i( halfSampleCoverage, halfSampleCoverage ), level.sampleSpacing, false );
        
        if( parentLevel )
        {
            // align to parent sample spacing:
            newOrigin = alignPoint( newOrigin, parentLevel->sampleSpacing, false );
        }
        else
        {
            // todo: Level 0 needs to cover the whole terrain: so it needs to
            // start at 0,0
            newOrigin = Pnt2i( 0, 0 );
        }
        
        // do the update:
        updatedSampleCount += updateLevel( level, parentLevel, newOrigin );
        
        if( updatedSampleCount >= sampleUpdateBudget_ )
        {
            // continue next frame
            SLOG << "Geometry Clipmaps: update sample limit reached!\n";
            
            for( int i = levelIdx + 1; i < levelCount; ++i )
            {
                GeometryClipmapLevel& levelIA = getLevel( i );
                
                levelIA.isActive = false;
            }
            // and stop updating here.
            notFinishedLastFrame_ = true;
            break;
        }           
    }       
    
    //todo: update statistics/profiler
    //lastUpdateSampleCount_ = updatedSampleCount;
}