Samchon Framework for CPP  1.0.0
GeometryPoint.hpp
1 #pragma once
2 #include <samchon/protocol/Entity.hpp>
3 
4 #include <random>
5 #include <cmath>
6 #include <samchon/library/Math.hpp>
7 #include <samchon/library/StringUtil.hpp>
8 #include <samchon/library/XML.hpp>
9 
10 namespace samchon
11 {
12 namespace examples
13 {
14 namespace tsp
15 {
16  using namespace std;
17 
18  using namespace library;
19  using namespace protocol;
20 
31  : public Entity<int>
32  {
33  private:
34  typedef Entity super;
35 
36  protected:
40  int uid;
41 
45  double longitude;
46 
50  double latitude;
51 
52  public:
53  /* -----------------------------------------------------------
54  CONSTRUCTORS
55  ----------------------------------------------------------- */
60  : super()
61  {
62  };
63 
71  GeometryPoint(int uid)
72  : super()
73  {
74  this->uid = uid;
75 
76  this->longitude = Math::random() * 180.0;
77  this->latitude = Math::random() * 180 - 90.0;
78  };
79 
86  GeometryPoint(int uid, double longitude, double latitude)
87  : super()
88  {
89  this->uid = uid;
90  this->longitude = longitude;
91  this->latitude = latitude;
92  };
93 
94  virtual ~GeometryPoint() = default;
95 
96  virtual void construct(shared_ptr<XML> xml) override
97  {
98  uid = xml->getProperty<int>("uid");
99  longitude = xml->getProperty<double>("longitude");
100  latitude = xml->getProperty<double>("latitude");
101  };
102 
103  /* -----------------------------------------------------------
104  GETTERS
105  ----------------------------------------------------------- */
106  virtual auto key() const -> int override
107  {
108  return uid;
109  };
110 
117  auto calcDistance(const GeometryPoint &point) const -> double
118  {
119  if (longitude == point.longitude && latitude == point.latitude)
120  return 0.0;
121 
122  double latitude_radian1 = Math::degree_to_radian(this->latitude);
123  double latitude_radian2 = Math::degree_to_radian(point.latitude);
124  double theta = this->longitude - point.longitude;
125 
126  double val =
127  sin(latitude_radian1) * sin(latitude_radian2)
128  + cos(latitude_radian1) * cos(latitude_radian2) * cos(Math::degree_to_radian(theta));
129 
130  val = acos(val);
131  val = Math::radian_to_degree(val);
132  val = val * 60 * 1.1515;
133  val = val * 1.609344;
134 
135  return val;
136  };
137 
138  /* -----------------------------------------------------------
139  EXPORTER
140  ----------------------------------------------------------- */
141  virtual auto TAG() const -> string
142  {
143  return "point";
144  };
145 
146  auto toXML() const -> shared_ptr<XML> override
147  {
148  shared_ptr<XML> &xml = super::toXML();
149  xml->setProperty("uid", uid);
150  xml->setProperty("longitude", longitude);
151  xml->setProperty("latitude", latitude);
152 
153  return xml;
154  };
155 
164  auto toString() const -> string
165  {
167  (
168  "{1}\t{2}\t{3}",
169  uid, longitude, latitude
170  );
171  };
172  };
173 };
174 };
175 };
GeometryPoint(int uid)
Construct from uid.
An entity, a standard data class.
Definition: Entity.hpp:115
static auto radian_to_degree(double val) -> double
Convert radian to degree.
Definition: Math.hpp:157
virtual auto key() const -> int override
Get a key that can identify the Entity uniquely.
int uid
An unique id for uniqueness.
GeometryPoint()
Default Constructor.
auto calcDistance(const GeometryPoint &point) const -> double
Calculate distance between target Branch.
double latitude
The latitude, coordinates Y.
GeometryPoint(int uid, double longitude, double latitude)
Construct from uid and geometry coordinates.
double longitude
The longitude; coordinates X.
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
Definition: StringUtil.hpp:54
auto toString() const -> string
Convert the Branch to String.
A geometry coordinates (point)
static auto random() -> double
Get a random value.
Definition: Math.hpp:135
static auto degree_to_radian(double val) -> double
Convert degree to radian.
Definition: Math.hpp:149