Welcome to mirror list, hosted at ThFree Co, Russian Federation.

feature_helpers.hpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0fe9bb5ec858f89ac1693e97b5adccd418488238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once

#include "coding/geometry_coding.hpp"

#include "geometry/parametrized_segment.hpp"
#include "geometry/point2d.hpp"
#include "geometry/simplification.hpp"

#include "indexer/feature_data.hpp"
#include "indexer/feature_visibility.hpp"
#include "indexer/scales.hpp"

#include "base/assert.hpp"
#include "base/math.hpp"

#include <cmath>
#include <cstdint>
#include <functional>
#include <limits>
#include <utility>
#include <vector>

namespace feature
{
class FeatureBuilder;

class CalculateMidPoints
{
public:
  using CellAndOffset = std::pair<uint64_t, uint64_t>;
  using MinDrawableScalePolicy = std::function<int(TypesHolder const & types, m2::RectD limitRect)>;

  CalculateMidPoints();
  CalculateMidPoints(MinDrawableScalePolicy const & minDrawableScalePolicy);

  void operator()(FeatureBuilder const & ft, uint64_t pos);
  bool operator()(m2::PointD const & p);

  m2::PointD GetCenter() const;
  std::vector<CellAndOffset> const & GetVector() const { return m_vec; }

  void Sort();

private:
  m2::PointD m_midLoc;
  m2::PointD m_midAll;
  size_t m_locCount = 0;
  size_t m_allCount = 0;
  uint8_t m_coordBits = serial::GeometryCodingParams().GetCoordBits();
  MinDrawableScalePolicy m_minDrawableScalePolicy;
  std::vector<CellAndOffset> m_vec;
};

template <typename Point>
inline bool ArePointsEqual(Point const & p1, Point const & p2)
{
  return p1 == p2;
}

template <>
inline bool ArePointsEqual<m2::PointD>(m2::PointD const & p1, m2::PointD const & p2)
{
  return AlmostEqualULPs(p1, p2);
}

class DistanceToSegmentWithRectBounds
{
public:
  explicit DistanceToSegmentWithRectBounds(m2::RectD const & rect) : m_rect(rect) {}

  // Returns squared distance from the segment [a, b] to the point p unless
  // p is close to the borders of |m_rect|, in which case returns a very large number.
  double operator()(m2::PointD const & a, m2::PointD const & b, m2::PointD const & p) const
  {
    if (base::AlmostEqualAbs(p.x, m_rect.minX(), m_eps) ||
        base::AlmostEqualAbs(p.x, m_rect.maxX(), m_eps) ||
        base::AlmostEqualAbs(p.y, m_rect.minY(), m_eps) ||
        base::AlmostEqualAbs(p.y, m_rect.maxY(), m_eps))
    {
      // Points near rect should be in a result simplified vector.
      return std::numeric_limits<double>::max();
    }

    return m2::SquaredDistanceFromSegmentToPoint<m2::PointD>()(a, b, p);
  }

  double GetEpsilon() const { return m_eps; }

private:
  m2::RectD const & m_rect;

  // 5.0E-7 is near with minimal epsilon when integer points are different
  // PointDToPointU(x, y) != PointDToPointU(x + m_eps, y + m_eps)
  double m_eps = 5.0E-7;
};

template <typename DistanceFn, typename PointsContainer>
void SimplifyPoints(DistanceFn distFn, int level, PointsContainer const & in, PointsContainer & out)
{
  if (in.size() < 2)
    return;

  double const eps = std::pow(scales::GetEpsilonForSimplify(level), 2);

  SimplifyNearOptimal(20, in.begin(), in.end(), eps, distFn,
                      AccumulateSkipSmallTrg<DistanceFn, m2::PointD>(distFn, out, eps));

  CHECK_GREATER(out.size(), 1, ());
  CHECK(ArePointsEqual(in.front(), out.front()), ());
  CHECK(ArePointsEqual(in.back(), out.back()), ());
}
}  // namespace feature