<commit_before>#pragma once
#include "../geometry/point2d.hpp"
#include "../base/assert.hpp"
#include "../base/base.hpp"
#include "../base/buffer_vector.hpp"
#include "../base/logging.hpp"
#include "../base/math.hpp"
#include "../std/algorithm.hpp"

namespace covering
{

enum CellObjectIntersection
{
  CELL_OBJECT_NO_INTERSECTION = 0,
  CELL_OBJECT_INTERSECT = 1,
  CELL_INSIDE_OBJECT = 2,
  OBJECT_INSIDE_CELL = 3
};

template <class CellIdT>
inline CellObjectIntersection IntersectCellWithLine(CellIdT const cell,
                                                    m2::PointD const & a,
                                                    m2::PointD const & b)
{
  pair<uint32_t, uint32_t> const xy = cell.XY();
  uint32_t const r = cell.Radius();
  m2::PointD const cellCorners[4] =
  {
    m2::PointD(xy.first - r, xy.second - r),
    m2::PointD(xy.first - r, xy.second + r),
    m2::PointD(xy.first + r, xy.second + r),
    m2::PointD(xy.first + r, xy.second - r)
  };
  for (int i = 0; i < 4; ++i)
    if (m2::SegmentsIntersect(a, b, cellCorners[i], cellCorners[i == 0 ? 3 : i - 1]))
      return CELL_OBJECT_INTERSECT;
  if (xy.first - r <= a.x && a.x <= xy.first + r && xy.second - r <= a.y && a.y <= xy.second + r)
    return OBJECT_INSIDE_CELL;
  return CELL_OBJECT_NO_INTERSECTION;
}

template <class CellIdT>
CellObjectIntersection IntersectCellWithTriangle(
    CellIdT const cell, m2::PointD const & a, m2::PointD const & b, m2::PointD const & c)
{
  CellObjectIntersection const i1 = IntersectCellWithLine(cell, a, b);
  if (i1 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  CellObjectIntersection const i2 = IntersectCellWithLine(cell, b, c);
  if (i2 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  CellObjectIntersection const i3 = IntersectCellWithLine(cell, c, a);
  if (i3 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  ASSERT_EQUAL(i1, i2, (cell, a, b, c));
  ASSERT_EQUAL(i2, i3, (cell, a, b, c));
  ASSERT_EQUAL(i3, i1, (cell, a, b, c));
  return i1;
}

template <class CellIdT, class CellIdContainerT, typename IntersectF>
void CoverObject(IntersectF const & intersect, uint64_t cellPenaltyArea, CellIdContainerT & out,
                 CellIdT cell)
{
  uint64_t const cellArea = my::sq(uint64_t(1 << (CellIdT::DEPTH_LEVELS - 1 - cell.Level())));
  CellObjectIntersection const intersection = intersect(cell);

  if (intersection == CELL_OBJECT_NO_INTERSECTION)
    return;
  if (intersection == CELL_INSIDE_OBJECT ||
      cell.Level() == CellIdT::DEPTH_LEVELS - 1 ||
      cellPenaltyArea >= cellArea)
  {
    out.push_back(cell);
    return;
  }

  buffer_vector<CellIdT, 32> subdiv;
  for (uint8_t i = 0; i < 4; ++i)
    CoverObject(intersect, cellPenaltyArea, subdiv, cell.Child(i));

  uint64_t subdivArea = 0;
  for (size_t i = 0; i < subdiv.size(); ++i)
    subdivArea += my::sq(uint64_t(1 << (CellIdT::DEPTH_LEVELS - 1 - subdiv[i].Level())));

  ASSERT(!subdiv.empty(), (cellPenaltyArea, out, cell));

  if (subdiv.empty() || cellPenaltyArea * (int(subdiv.size()) - 1) >= cellArea - subdivArea)
    out.push_back(cell);
  else
    for (size_t i = 0; i < subdiv.size(); ++i)
      out.push_back(subdiv[i]);
}


} // namespace covering
<commit_msg>Fix bug in IntersectCellWithTriangle()!! It was detected by a unit test, restored in commit 647b4bf4.<commit_after>#pragma once
#include "../geometry/point2d.hpp"
#include "../base/assert.hpp"
#include "../base/base.hpp"
#include "../base/buffer_vector.hpp"
#include "../base/logging.hpp"
#include "../base/math.hpp"
#include "../std/algorithm.hpp"

namespace covering
{

enum CellObjectIntersection
{
  CELL_OBJECT_NO_INTERSECTION = 0,
  CELL_OBJECT_INTERSECT = 1,
  CELL_INSIDE_OBJECT = 2,
  OBJECT_INSIDE_CELL = 3
};

template <class CellIdT>
inline CellObjectIntersection IntersectCellWithLine(CellIdT const cell,
                                                    m2::PointD const & a,
                                                    m2::PointD const & b)
{
  pair<uint32_t, uint32_t> const xy = cell.XY();
  uint32_t const r = cell.Radius();
  m2::PointD const cellCorners[4] =
  {
    m2::PointD(xy.first - r, xy.second - r),
    m2::PointD(xy.first - r, xy.second + r),
    m2::PointD(xy.first + r, xy.second + r),
    m2::PointD(xy.first + r, xy.second - r)
  };
  for (int i = 0; i < 4; ++i)
    if (m2::SegmentsIntersect(a, b, cellCorners[i], cellCorners[i == 0 ? 3 : i - 1]))
      return CELL_OBJECT_INTERSECT;
  if (xy.first - r <= a.x && a.x <= xy.first + r && xy.second - r <= a.y && a.y <= xy.second + r)
    return OBJECT_INSIDE_CELL;
  return CELL_OBJECT_NO_INTERSECTION;
}

template <class CellIdT>
CellObjectIntersection IntersectCellWithTriangle(
    CellIdT const cell, m2::PointD const & a, m2::PointD const & b, m2::PointD const & c)
{
  CellObjectIntersection const i1 = IntersectCellWithLine(cell, a, b);
  if (i1 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  CellObjectIntersection const i2 = IntersectCellWithLine(cell, b, c);
  if (i2 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  CellObjectIntersection const i3 = IntersectCellWithLine(cell, c, a);
  if (i3 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  // At this point either:
  // 1. Triangle is inside cell.
  // 2. Cell is inside triangle.
  // 3. Cell and triangle do not intersect.
  ASSERT_EQUAL(i1, i2, (cell, a, b, c));
  ASSERT_EQUAL(i2, i3, (cell, a, b, c));
  ASSERT_EQUAL(i3, i1, (cell, a, b, c));
  if (i1 == OBJECT_INSIDE_CELL || i2 == OBJECT_INSIDE_CELL || i3 == OBJECT_INSIDE_CELL)
    return OBJECT_INSIDE_CELL;
  pair<uint32_t, uint32_t> const xy = cell.XY();
  if (m2::IsPointStrictlyInsideTriangle(m2::PointD(xy.first, xy.second), a, b, c))
    return CELL_INSIDE_OBJECT;
  return CELL_OBJECT_NO_INTERSECTION;
}

template <class CellIdT, class CellIdContainerT, typename IntersectF>
void CoverObject(IntersectF const & intersect, uint64_t cellPenaltyArea, CellIdContainerT & out,
                 CellIdT cell)
{
  uint64_t const cellArea = my::sq(uint64_t(1 << (CellIdT::DEPTH_LEVELS - 1 - cell.Level())));
  CellObjectIntersection const intersection = intersect(cell);

  if (intersection == CELL_OBJECT_NO_INTERSECTION)
    return;
  if (intersection == CELL_INSIDE_OBJECT ||
      cell.Level() == CellIdT::DEPTH_LEVELS - 1 ||
      cellPenaltyArea >= cellArea)
  {
    out.push_back(cell);
    return;
  }

  buffer_vector<CellIdT, 32> subdiv;
  for (uint8_t i = 0; i < 4; ++i)
    CoverObject(intersect, cellPenaltyArea, subdiv, cell.Child(i));

  uint64_t subdivArea = 0;
  for (size_t i = 0; i < subdiv.size(); ++i)
    subdivArea += my::sq(uint64_t(1 << (CellIdT::DEPTH_LEVELS - 1 - subdiv[i].Level())));

  ASSERT(!subdiv.empty(), (cellPenaltyArea, out, cell));

  if (subdiv.empty() || cellPenaltyArea * (int(subdiv.size()) - 1) >= cellArea - subdivArea)
    out.push_back(cell);
  else
    for (size_t i = 0; i < subdiv.size(); ++i)
      out.push_back(subdiv[i]);
}


} // namespace covering

{'fraction_non_alphanumeric': 0.14038351459645107, 'fraction_numerical': 0.020892959358900974, 'mean_word_length': 3.440279542566709, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5600710', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2565, 'n_words': 595}