LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
geovector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "cad/const.h"
4 #include "geocoordinate.h"
5 #include "geobase.h"
6 #include <vector>
7 #include "cad/base/visitor.h"
8 #include "cad/math/equation.h"
9 
10 namespace lc {
11  namespace geo {
12  class Vector : public Base, virtual public Visitable {
13  public:
14  // TODO: Test for validity?
15  explicit Vector(const Coordinate& start, const Coordinate& end) : _start(start), _end(end) {}
16 
17  // TODO: Test for validity?
18  Vector(const Vector& v) : _start(v._start), _end(v._end) {}
19 
20  inline const Coordinate start() const {
21  return _start;
22  }
23  inline const Coordinate end() const {
24  return _end;
25  }
26 
27  inline Vector& operator = (const Vector& v) {
28  if (this != &v) {
29  _start = v._start;
30  _end = v._end;
31  }
32 
33  return *this;
34  }
35 
39  inline const Coordinate nearestPointOnPath(const Coordinate& coord) const {
40  Coordinate direction = this->end() - this->start();
41  Coordinate vpc = coord - this->start();
42  vpc = start() + direction * Coordinate(vpc).dot(direction) / direction.squared();
43  return vpc;
44  }
45 
49  inline const Coordinate nearestPointOnEntity(const Coordinate& coord) const {
50  const auto vpc = nearestPointOnPath(coord);
51  // Note, I could use Area for this but didn't want that dependency here
52  const geo::Coordinate minP(Coordinate(std::min(_start.x()- LCTOLERANCE, _end.x()- LCTOLERANCE), std::min(_start.y()-
53  LCTOLERANCE, _end.y()-
54  LCTOLERANCE)));
55  const geo::Coordinate maxP(Coordinate(std::max(_start.x()+ LCTOLERANCE, _end.x()+ LCTOLERANCE), std::max(_start.y()+
56  LCTOLERANCE, _end.y()+
57  LCTOLERANCE)));
58 
59  bool inArea = (vpc.x() >= minP.x() && vpc.x() <= maxP.x() && vpc.y() >= minP.y() && vpc.y() <= maxP.y());
60  if (inArea) {
61  return vpc;
62  } else {
63  const double dStart = coord.distanceTo(start());
64  const double dEnd = coord.distanceTo(end());
65  return dStart < dEnd?start():end();
66  }
67  }
68  /*
69  inline bool isCoordinateOnPath(const Coordinate& coord) const {
70  geo::Area area(_start, _end);
71  if (area.inArea(coord) && ((nearestPointOnPath(coord) - coord).squared() < 1.0e-8)) {
72  return true;
73  }
74 
75  return false;
76  }*/
77 
104  double Angle1() const {
105  return _start.angleTo(_end);
106  }
107 
111  double Angle2() const {
112  return _end.angleTo(_start);
113  }
114 
115  const maths::Equation equation() const {
116  auto&& dvp = this->_end - this->_start;
117  geo::Coordinate normal(-dvp.y(), dvp.x());
118  return maths::Equation(0,0,0,normal.x(),normal.y(),- normal.dot(this->end()));
119  }
120 
121  virtual void accept(GeoEntityVisitor &v) const override { v.visit(*this); }
122 
123  private:
124  friend std::ostream& operator<<(std::ostream& os, const Vector& e) {
125  os << "Vector(start=" << e._start << " end=" << e._end << ")";
126  return os;
127  }
128  private:
131 
132  };
133  }
134 }
135 
136 // GEOVECTOR_H
double squared() const
Vector & operator=(const Vector &v)
Definition: geovector.h:27
const Coordinate end() const
Definition: geovector.h:23
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
double angleTo(const Coordinate &v) const
Returns angle To the coordinate.
Definition: geocoordinate.h:77
virtual void accept(GeoEntityVisitor &v) const override
Definition: geovector.h:121
const Coordinate nearestPointOnEntity(const Coordinate &coord) const
Definition: geovector.h:49
double Angle1() const
Definition: geovector.h:104
double y() const
Returns y of Coordinate.
Definition: geocoordinate.h:34
friend std::ostream & operator<<(std::ostream &os, const Vector &e)
Definition: geovector.h:124
Definition: cadentity.h:12
Coordinate _end
Definition: geovector.h:130
const Coordinate nearestPointOnPath(const Coordinate &coord) const
Definition: geovector.h:39
#define LCTOLERANCE
Definition: const.h:6
double Angle2() const
Definition: geovector.h:111
Coordinate _start
Definition: geovector.h:129
Vector(const Coordinate &start, const Coordinate &end)
Definition: geovector.h:15
double dot(const Coordinate &coord) const
const Coordinate start() const
Definition: geovector.h:20
double distanceTo(const geo::Coordinate &c) const
Vector(const Vector &v)
Definition: geovector.h:18
const maths::Equation equation() const
Definition: geovector.h:115