<commit_before>#include "cartographer/io/probability_grid_points_processor.h"

#include "Eigen/Core"
#include "cartographer/common/lua_parameter_dictionary.h"
#include "cartographer/common/make_unique.h"
#include "cartographer/common/math.h"
#include "cartographer/io/image.h"
#include "cartographer/io/points_batch.h"

namespace cartographer {
namespace io {
namespace {

void WriteGrid(const mapping_2d::ProbabilityGrid& probability_grid,
               FileWriter* const file_writer) {
  Eigen::Array2i offset;
  mapping_2d::CellLimits cell_limits;
  probability_grid.ComputeCroppedLimits(&offset, &cell_limits);
  if (cell_limits.num_x_cells == 0 || cell_limits.num_y_cells == 0) {
    LOG(WARNING) << "Not writing output: empty probability grid";
    return;
  }
  const auto grid_index_to_pixel = [cell_limits](const Eigen::Array2i& index) {
    return Eigen::Array2i(cell_limits.num_y_cells - index(1) - 1,
                          cell_limits.num_x_cells - index(0) - 1);
  };
  const auto compute_color_value = [&probability_grid](
                                       const Eigen::Array2i& index) {
    if (probability_grid.IsKnown(index)) {
      const float probability = 1.f - probability_grid.GetProbability(index);
      return static_cast<uint8_t>(
          255 * ((probability - mapping::kMinProbability) /
                 (mapping::kMaxProbability - mapping::kMinProbability)));
    } else {
      constexpr uint8_t kUnknownValue = 128;
      return kUnknownValue;
    }
  };
  int width = cell_limits.num_y_cells;
  int height = cell_limits.num_x_cells;
  Image image(width, height);
  for (auto xy_index :
       cartographer::mapping_2d::XYIndexRangeIterator(cell_limits)) {
    auto index = xy_index + offset;
    uint8 value = compute_color_value(index);
    const Eigen::Array2i pixel = grid_index_to_pixel(xy_index);
    image.SetPixel(pixel.x(), pixel.y(), {{value, value, value}});
  }
  image.WritePng(file_writer);
  CHECK(file_writer->Close());
}

mapping_2d::ProbabilityGrid CreateProbabilityGrid(const double resolution) {
  constexpr int kInitialProbabilityGridSize = 100;
  Eigen::Vector2d max =
      0.5 * kInitialProbabilityGridSize * resolution * Eigen::Vector2d::Ones();
  return mapping_2d::ProbabilityGrid(cartographer::mapping_2d::MapLimits(
      resolution, max,
      mapping_2d::CellLimits(kInitialProbabilityGridSize,
                             kInitialProbabilityGridSize)));
}

}  // namespace

ProbabilityGridPointsProcessor::ProbabilityGridPointsProcessor(
    const double resolution,
    const mapping_2d::proto::RangeDataInserterOptions&
        range_data_inserter_options,
    std::unique_ptr<FileWriter> file_writer, PointsProcessor* const next)
    : next_(next),<commit_msg>Fix ProbabilityPointsProcessor mirroring output (#419)<commit_after>#include "cartographer/io/probability_grid_points_processor.h"

#include "Eigen/Core"
#include "cartographer/common/lua_parameter_dictionary.h"
#include "cartographer/common/make_unique.h"
#include "cartographer/common/math.h"
#include "cartographer/io/image.h"
#include "cartographer/io/points_batch.h"

namespace cartographer {
namespace io {
namespace {

void WriteGrid(const mapping_2d::ProbabilityGrid& probability_grid,
               FileWriter* const file_writer) {
  Eigen::Array2i offset;
  mapping_2d::CellLimits cell_limits;
  probability_grid.ComputeCroppedLimits(&offset, &cell_limits);
  if (cell_limits.num_x_cells == 0 || cell_limits.num_y_cells == 0) {
    LOG(WARNING) << "Not writing output: empty probability grid";
    return;
  }
  const auto grid_index_to_pixel = [cell_limits](const Eigen::Array2i& index) {
    return Eigen::Array2i(index(0), index(1));
  };
  const auto compute_color_value = [&probability_grid](
                                       const Eigen::Array2i& index) {
    if (probability_grid.IsKnown(index)) {
      const float probability = 1.f - probability_grid.GetProbability(index);
      return static_cast<uint8_t>(
          255 * ((probability - mapping::kMinProbability) /
                 (mapping::kMaxProbability - mapping::kMinProbability)));
    } else {
      constexpr uint8_t kUnknownValue = 128;
      return kUnknownValue;
    }
  };
  int width = cell_limits.num_x_cells;
  int height = cell_limits.num_y_cells;
  Image image(width, height);
  for (auto xy_index :
       cartographer::mapping_2d::XYIndexRangeIterator(cell_limits)) {
    auto index = xy_index + offset;
    uint8 value = compute_color_value(index);
    const Eigen::Array2i pixel = grid_index_to_pixel(xy_index);
    image.SetPixel(pixel.x(), pixel.y(), {{value, value, value}});
  }
  image.WritePng(file_writer);
  CHECK(file_writer->Close());
}

mapping_2d::ProbabilityGrid CreateProbabilityGrid(const double resolution) {
  constexpr int kInitialProbabilityGridSize = 100;
  Eigen::Vector2d max =
      0.5 * kInitialProbabilityGridSize * resolution * Eigen::Vector2d::Ones();
  return mapping_2d::ProbabilityGrid(cartographer::mapping_2d::MapLimits(
      resolution, max,
      mapping_2d::CellLimits(kInitialProbabilityGridSize,
                             kInitialProbabilityGridSize)));
}

}  // namespace

ProbabilityGridPointsProcessor::ProbabilityGridPointsProcessor(
    const double resolution,
    const mapping_2d::proto::RangeDataInserterOptions&
        range_data_inserter_options,
    std::unique_ptr<FileWriter> file_writer, PointsProcessor* const next)
    : next_(next),
{'fraction_non_alphanumeric': 0.10800367985280589, 'fraction_numerical': 0.01343146274149034, 'mean_word_length': 4.201913875598086, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7054132', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1675, 'n_words': 301}