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

path_info.hpp « render - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70679ee655afa599c78d6151a7ba539fcc393973 (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
#pragma once

#include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp"

#include "std/vector.hpp"
#include "std/algorithm.hpp"

namespace di
{
  class PathInfo
  {
    double m_fullL;
    double m_offset;

  public:
    vector<m2::PointD> m_path;

    // -1.0 means "not" initialized
    PathInfo(double offset = -1.0) : m_fullL(-1.0), m_offset(offset) {}

    void swap(PathInfo & r)
    {
      m_path.swap(r.m_path);
      std::swap(m_fullL, r.m_fullL);
      std::swap(m_offset, r.m_offset);
    }

    void push_back(m2::PointD const & p)
    {
      m_path.push_back(p);
    }

    size_t size() const { return m_path.size(); }

    void SetFullLength(double len) { m_fullL = len; }
    double GetFullLength() const
    {
      ASSERT ( m_fullL > 0.0, (m_fullL) );
      return m_fullL;
    }

    double GetLength() const
    {
      double sum = 0.0;
      for (size_t i = 1; i < m_path.size(); ++i)
      {
        double const l = m_path[i-1].Length(m_path[i]);
        sum += l;
      }
      return sum;
    }

    double GetOffset() const
    {
      ASSERT ( m_offset >= 0.0, (m_offset) );
      return m_offset;
    }

    bool GetSmPoint(double part, m2::PointD & pt) const
    {
      double sum = -GetFullLength() * part + m_offset;
      if (sum > 0.0) return false;

      for (size_t i = 1; i < m_path.size(); ++i)
      {
        double const l = m_path[i-1].Length(m_path[i]);
        sum += l;
        if (sum >= 0.0)
        {
          double const factor = (l - sum) / l;
          ASSERT_GREATER_OR_EQUAL ( factor, 0.0, () );

          pt.x = factor * (m_path[i].x - m_path[i-1].x) + m_path[i-1].x;
          pt.y = factor * (m_path[i].y - m_path[i-1].y) + m_path[i-1].y;
          return true;
        }
      }

      return false;
    }

    m2::RectD GetLimitRect() const
    {
      m2::RectD rect;
      for (size_t i = 0; i < m_path.size(); ++i)
        rect.Add(m_path[i]);
      return rect;
    }
  };
}

inline void swap(di::PathInfo & p1, di::PathInfo & p2)
{
  p1.swap(p2);
}