<commit_before>package org.glob3.mobile.generated; 
public class TileTextureBuilder extends RCObject
{
  private MultiLayerTileTexturizer _texturizer;
  private Tile _tile;

  private java.util.ArrayList<Petition> _petitions = new java.util.ArrayList<Petition>();
  private int _petitionsCount;
  private int _stepsDone;

  private IFactory _factory; // FINAL WORD REMOVE BY CONVERSOR RULE
  private TexturesHandler _texturesHandler;
  private TextureBuilder _textureBuilder;
  private GL _gl;

  private final Vector2I _tileTextureResolution;
  private final Vector2I _tileMeshResolution;
  private final boolean _mercator;

  private IDownloader _downloader;

  private final Mesh _tessellatorMesh;

  private final TileTessellator _tessellator;

  private final int _firstLevel;

  private java.util.ArrayList<TileTextureBuilder_PetitionStatus> _status = new java.util.ArrayList<TileTextureBuilder_PetitionStatus>();
  private java.util.ArrayList<Long> _requestsIds = new java.util.ArrayList<Long>();


  private boolean _finalized;
  private boolean _canceled;
  private boolean _anyCanceled;
  private boolean _alreadyStarted;

  private long _texturePriority;


  private java.util.ArrayList<Petition> cleanUpPetitions(java.util.ArrayList<Petition> petitions)
  {
    final int petitionsSize = petitions.size();
    if (petitionsSize <= 1)
    {
      return petitions;
    }

    java.util.ArrayList<Petition> result = new java.util.ArrayList<Petition>();
    for (int i = 0; i < petitionsSize; i++)
    {
      Petition currentPetition = petitions.get(i);
      final Sector currentSector = currentPetition.getSector();

      boolean coveredByFollowingPetition = false;
      for (int j = i+1; j < petitionsSize; j++)
      {
        Petition followingPetition = petitions.get(j);

        // only opaque petitions can cover
        if (!followingPetition.isTransparent())
        {
          if (followingPetition.getSector().fullContains(currentSector))
          {
            coveredByFollowingPetition = true;
            break;
          }
        }
      }

      if (coveredByFollowingPetition)
      {
        if (currentPetition != null)
           currentPetition.dispose();
      }
      else
      {
        result.add(currentPetition);
      }
    }

    return result;
  }


  public LeveledTexturedMesh _mesh;

  public TileTextureBuilder(MultiLayerTileTexturizer texturizer, G3MRenderContext rc, LayerSet layerSet, IDownloader downloader, Tile tile, Mesh tessellatorMesh, TileTessellator tessellator, long texturePriority)
  {
     _texturizer = texturizer;
     _factory = rc.getFactory();
     _texturesHandler = rc.getTexturesHandler();
     _textureBuilder = rc.getTextureBuilder();
     _gl = rc.getGL();
     _tileTextureResolution = layerSet.getLayerTilesRenderParameters()._tileTextureResolution;
     _tileMeshResolution = layerSet.getLayerTilesRenderParameters()._tileMeshResolution;
     _mercator = layerSet.getLayerTilesRenderParameters()._mercator;
     _firstLevel = layerSet.getLayerTilesRenderParameters()._firstLevel;
     _downloader = downloader;
     _tile = tile;
     _tessellatorMesh = tessellatorMesh;
     _stepsDone = 0;
     _anyCanceled = false;
     _mesh = null;
     _tessellator = tessellator;
     _finalized = false;
     _canceled = false;
     _alreadyStarted = false;
     _texturePriority = texturePriority;
    _petitions = cleanUpPetitions(layerSet.createTileMapPetitions(rc, tile));

    _petitionsCount = _petitions.size();

    for (int i = 0; i < _petitionsCount; i++)
    {
      _status.add(TileTextureBuilder_PetitionStatus.STATUS_PENDING);
    }

    _mesh = createMesh();
  }

  public final void start()
  {
    if (_canceled)
    {
      return;
    }
    if (_alreadyStarted)
    {
      return;
    }
    _alreadyStarted = true;

    if (_tile == null)
    {
      return;
    }

    for (int i = 0; i < _petitionsCount; i++)
    {
      final Petition petition = _petitions.get(i);

      //      const long long priority =  (_parameters->_incrementalTileQuality
      //                                   ? 1000 - _tile->getLevel()
      //                                   : _tile->getLevel());

      final long priority = _texturePriority + _tile.getLevel();

      //      printf("%s\n", petition->getURL().getPath().c_str());

      final long requestId = _downloader.requestImage(new URL(petition.getURL()), priority, petition.getTimeToCache(), new BuilderDownloadStepDownloadListener(this, i), true);
      if (requestId >= 0)
      {
        _requestsIds.add(requestId);
      }
    }
  }

  public void dispose()
  {
    if (!_finalized && !_canceled)
    {
      cancel();
    }

    deletePetitions();
  }

  public final RectangleF getImageRectangleInTexture(Sector wholeSector, Sector imageSector)
  {

    final IMathUtils mu = IMathUtils.instance();

    final Vector2D lowerFactor = wholeSector.getUVCoordinates(imageSector.lower());

    final double widthFactor = imageSector.getDeltaLongitude().div(wholeSector.getDeltaLongitude());
    final double heightFactor = imageSector.getDeltaLatitude().div(wholeSector.getDeltaLatitude());

    final int textureWidth = _tileTextureResolution._x;
    final int textureHeight = _tileTextureResolution._y;

    return new RectangleF((float) mu.round(lowerFactor._x * textureWidth), (float) mu.round(lowerFactor._y * textureHeight), (float) mu.round(widthFactor * textureWidth), (float) mu.round(heightFactor * textureHeight));
  }

  public final RectangleF getImageRectangle(IImage wholeImage, Sector wholeSector, Sector imageSector)
  {
    final IMathUtils mu = IMathUtils.instance();

    double minWholeLat = wholeSector.lower().latitude().degrees();
    double minWholeLon = wholeSector.lower().longitude().degrees();
    double maxWholeLat = wholeSector.upper().latitude().degrees();
    double maxWholeLon = wholeSector.upper().longitude().degrees();

    double minImageLat = imageSector.lower().latitude().degrees();
    double minImageLon = imageSector.lower().longitude().degrees();

    double lowerFactorX = (minImageLon - minWholeLon) / (maxWholeLon - minWholeLon);
    double lowerFactorY = (minImageLat - minWholeLat) / (maxWholeLat - minWholeLat);
    final Vector2D lowerFactor = new Vector2D(lowerFactorX, lowerFactorY);

    final double widthFactor = imageSector.getDeltaLongitude().div(wholeSector.getDeltaLongitude());
    final double heightFactor = imageSector.getDeltaLatitude().div(wholeSector.getDeltaLatitude());

    final int textureWidth = wholeImage.getWidth();
    final int textureHeight = wholeImage.getHeight();
<commit_msg>Generated<commit_after>package org.glob3.mobile.generated; 
public class TileTextureBuilder extends RCObject
{
  private MultiLayerTileTexturizer _texturizer;
  private Tile _tile;

  private java.util.ArrayList<Petition> _petitions = new java.util.ArrayList<Petition>();
  private int _petitionsCount;
  private int _stepsDone;

  private IFactory _factory; // FINAL WORD REMOVE BY CONVERSOR RULE
  private TexturesHandler _texturesHandler;
  private TextureBuilder _textureBuilder;
  private GL _gl;

  private final Vector2I _tileTextureResolution;
  private final Vector2I _tileMeshResolution;
  private final boolean _mercator;

  private IDownloader _downloader;

  private final Mesh _tessellatorMesh;

  private final TileTessellator _tessellator;

  private final int _firstLevel;

  private java.util.ArrayList<TileTextureBuilder_PetitionStatus> _status = new java.util.ArrayList<TileTextureBuilder_PetitionStatus>();
  private java.util.ArrayList<Long> _requestsIds = new java.util.ArrayList<Long>();


  private boolean _finalized;
  private boolean _canceled;
  private boolean _anyCanceled;
  private boolean _alreadyStarted;

  private long _texturePriority;


  private java.util.ArrayList<Petition> cleanUpPetitions(java.util.ArrayList<Petition> petitions)
  {
    final int petitionsSize = petitions.size();
    if (petitionsSize <= 1)
    {
      return petitions;
    }

    java.util.ArrayList<Petition> result = new java.util.ArrayList<Petition>();
    for (int i = 0; i < petitionsSize; i++)
    {
      Petition currentPetition = petitions.get(i);
      final Sector currentSector = currentPetition.getSector();

      boolean coveredByFollowingPetition = false;
      for (int j = i+1; j < petitionsSize; j++)
      {
        Petition followingPetition = petitions.get(j);

        // only opaque petitions can cover
        if (!followingPetition.isTransparent())
        {
          if (followingPetition.getSector().fullContains(currentSector))
          {
            coveredByFollowingPetition = true;
            break;
          }
        }
      }

      if (coveredByFollowingPetition)
      {
        if (currentPetition != null)
           currentPetition.dispose();
      }
      else
      {
        result.add(currentPetition);
      }
    }

    return result;
  }


  public LeveledTexturedMesh _mesh;

  public TileTextureBuilder(MultiLayerTileTexturizer texturizer, G3MRenderContext rc, LayerSet layerSet, IDownloader downloader, Tile tile, Mesh tessellatorMesh, TileTessellator tessellator, long texturePriority)
  {
     _texturizer = texturizer;
     _factory = rc.getFactory();
     _texturesHandler = rc.getTexturesHandler();
     _textureBuilder = rc.getTextureBuilder();
     _gl = rc.getGL();
     _tileTextureResolution = layerSet.getLayerTilesRenderParameters()._tileTextureResolution;
     _tileMeshResolution = layerSet.getLayerTilesRenderParameters()._tileMeshResolution;
     _mercator = layerSet.getLayerTilesRenderParameters()._mercator;
     _firstLevel = layerSet.getLayerTilesRenderParameters()._firstLevel;
     _downloader = downloader;
     _tile = tile;
     _tessellatorMesh = tessellatorMesh;
     _stepsDone = 0;
     _anyCanceled = false;
     _mesh = null;
     _tessellator = tessellator;
     _finalized = false;
     _canceled = false;
     _alreadyStarted = false;
     _texturePriority = texturePriority;
    _petitions = cleanUpPetitions(layerSet.createTileMapPetitions(rc, tile));

    _petitionsCount = _petitions.size();

    for (int i = 0; i < _petitionsCount; i++)
    {
      _status.add(TileTextureBuilder_PetitionStatus.STATUS_PENDING);
    }

    _mesh = createMesh();
  }

  public final void start()
  {
    if (_canceled)
    {
      return;
    }
    if (_alreadyStarted)
    {
      return;
    }
    _alreadyStarted = true;

    if (_tile == null)
    {
      return;
    }

    for (int i = 0; i < _petitionsCount; i++)
    {
      final Petition petition = _petitions.get(i);

      //      const long long priority =  (_parameters->_incrementalTileQuality
      //                                   ? 1000 - _tile->getLevel()
      //                                   : _tile->getLevel());

      final long priority = _texturePriority + _tile.getLevel();

      //      printf("%s\n", petition->getURL().getPath().c_str());

      final long requestId = _downloader.requestImage(new URL(petition.getURL()), priority, petition.getTimeToCache(), new BuilderDownloadStepDownloadListener(this, i), true);
      if (requestId >= 0)
      {
        _requestsIds.add(requestId);
      }
    }
  }

  public void dispose()
  {
    if (!_finalized && !_canceled)
    {
      cancel();
    }

    deletePetitions();
  }

  public final RectangleF getImageRectangleInTexture(Sector wholeSector, Sector imageSector)
  {

    final IMathUtils mu = IMathUtils.instance();

    final Vector2D lowerFactor = wholeSector.getUVCoordinates(imageSector.lower());

    final double widthFactor = imageSector.getDeltaLongitude().div(wholeSector.getDeltaLongitude());
    final double heightFactor = imageSector.getDeltaLatitude().div(wholeSector.getDeltaLatitude());

    final int textureWidth = _tileTextureResolution._x;
    final int textureHeight = _tileTextureResolution._y;

    return new RectangleF((float) mu.round(lowerFactor._x * textureWidth), (float) mu.round((1.0 - (lowerFactor._y + heightFactor)) * textureHeight), (float) mu.round(widthFactor * textureWidth), (float) mu.round(heightFactor * textureHeight));
  }

  public final RectangleF getImageRectangle(IImage wholeImage, Sector wholeSector, Sector imageSector)
  {
    final IMathUtils mu = IMathUtils.instance();

    double minWholeLat = wholeSector.lower().latitude().degrees();
    double minWholeLon = wholeSector.lower().longitude().degrees();
    double maxWholeLat = wholeSector.upper().latitude().degrees();
    double maxWholeLon = wholeSector.upper().longitude().degrees();

    double minImageLat = imageSector.lower().latitude().degrees();
    double minImageLon = imageSector.lower().longitude().degrees();

    double lowerFactorX = (minImageLon - minWholeLon) / (maxWholeLon - minWholeLon);
    double lowerFactorY = (minImageLat - minWholeLat) / (maxWholeLat - minWholeLat);
    final Vector2D lowerFactor = new Vector2D(lowerFactorX, lowerFactorY);

    final double widthFactor = imageSector.getDeltaLongitude().div(wholeSector.getDeltaLongitude());
    final double heightFactor = imageSector.getDeltaLatitude().div(wholeSector.getDeltaLatitude());

    final int textureWidth = wholeImage.getWidth();
    final int textureHeight = wholeImage.getHeight();

{'fraction_non_alphanumeric': 0.09696923890862369, 'fraction_numerical': 0.002872042929483788, 'mean_word_length': 3.4567194341529133, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5760467', 'n_tokens_mistral': 4018, 'n_tokens_neox': 3781, 'n_words': 822}