Edge.cpp
1 #include "Edge.hpp"
2 #include <cmath>
3 #include "../misc/universal_error.hpp"
4 
5 Edge::Edge(void):
6 vertices(Vector2D(), Vector2D()),
7  neighbors(0,0) {}
8 
9 Edge::~Edge(void){}
10 
11 Edge::Edge(Edge const& other):
12 vertices(other.vertices),
13  neighbors(other.neighbors) {}
14 
15 Edge& Edge::operator=(const Edge& other)
16 {
17  vertices = other.vertices;
18  neighbors = other.neighbors;
19  return *this;
20 }
21 
22 Edge::Edge(Vector2D const& p1, Vector2D const& p2,
23  int neighbor1, int neighbor2):
24 vertices(p1,p2), neighbors(neighbor1, neighbor2) {}
25 
26 double Edge::GetLength(void) const
27 {
28  return abs(vertices.second-vertices.first);
29 }
30 
31 double DistanceToEdge(Vector2D const& point,Edge const& edge)
32 {
33  Vector2D v=edge.vertices.second-edge.vertices.first;
34  Vector2D w=point-edge.vertices.first;
35  double c1,c2;
36  c1=ScalarProd(v,w);
37  if(c1<=0)
38  return point.distance(edge.vertices.first);
39  c2=ScalarProd(v,v);
40  if(c2<=c1)
41  return point.distance(edge.vertices.second);
42  return point.distance(edge.vertices.first+(c1/c2)*v);
43 }
44 
45 bool SegmentIntersection(Edge const&edge1,Edge const&edge2,
46  Vector2D &Intersection,double eps)
47 {
48  bool res=true;
49  const double areascale=std::min((edge1.vertices.first.x-edge1.vertices.second.x)*
50  (edge1.vertices.first.x-edge1.vertices.second.x)+(edge1.vertices.first.y-
51  edge1.vertices.second.y)*(edge1.vertices.first.y-edge1.vertices.second.y),
52  (edge2.vertices.first.x-edge2.vertices.second.x)*
53  (edge2.vertices.first.x-edge2.vertices.second.x)+(edge2.vertices.first.y-
54  edge2.vertices.second.y)*(edge2.vertices.first.y-edge2.vertices.second.y));
55  if(std::min(edge1.vertices.second.x,edge1.vertices.first.x)>std::max(edge2.vertices.second.x,edge2.vertices.first.x)||
56  std::min(edge2.vertices.second.x,edge2.vertices.first.x)>std::max(edge1.vertices.second.x,edge1.vertices.first.x)||
57  std::min(edge1.vertices.second.y,edge1.vertices.first.y)>std::max(edge2.vertices.second.y,edge2.vertices.first.y)||
58  std::min(edge2.vertices.second.y,edge2.vertices.first.y)>std::max(edge1.vertices.second.y,edge1.vertices.first.y))
59  res=false;
60  double d=(edge1.vertices.first.x-edge1.vertices.second.x)*(edge2.vertices.first.y-edge2.vertices.second.y)
61  -(edge2.vertices.first.x-edge2.vertices.second.x)*(edge1.vertices.first.y-edge1.vertices.second.y);
62  if(fabs(d)<1e-8*areascale)
63  return false;
64  double xi=((edge2.vertices.first.x-edge2.vertices.second.x)*(edge1.vertices.first.x*edge1.vertices.second.y-
65  edge1.vertices.second.x*edge1.vertices.first.y)-(edge1.vertices.first.x-edge1.vertices.second.x)*
66  (edge2.vertices.first.x*edge2.vertices.second.y-edge2.vertices.second.x*edge2.vertices.first.y))/d;
67  double yi=((edge2.vertices.first.y-edge2.vertices.second.y)*(edge1.vertices.first.x*edge1.vertices.second.y-
68  edge1.vertices.second.x*edge1.vertices.first.y)-(edge1.vertices.first.y-edge1.vertices.second.y)*
69  (edge2.vertices.first.x*edge2.vertices.second.y-edge2.vertices.second.x*edge2.vertices.first.y))/d;
70  Intersection.Set(xi,yi);
71  eps=eps*sqrt(areascale);
72  if((xi+eps)<std::min(edge1.vertices.first.x,edge1.vertices.second.x)||(xi-eps)>std::max(edge1.vertices.first.x,edge1.vertices.second.x))
73  return false;
74  if((xi+eps)<std::min(edge2.vertices.first.x,edge2.vertices.second.x)||(xi-eps)>std::max(edge2.vertices.first.x,edge2.vertices.second.x))
75  return false;
76  if((yi+eps)<std::min(edge1.vertices.first.y,edge1.vertices.second.y)||(yi-eps)>std::max(edge1.vertices.first.y,edge1.vertices.second.y))
77  return false;
78  if((yi+eps)<std::min(edge2.vertices.first.y,edge2.vertices.second.y)||(yi-eps)>std::max(edge2.vertices.first.y,edge2.vertices.second.y))
79  return false;
80  return res;
81 }
82 
83 Vector2D Parallel(Edge const& edge)
84 {
85  return (edge.vertices.second - edge.vertices.first);
86 }
87 
89 {
90  return 0.5*(edge.vertices.first+edge.vertices.second);
91 }
double distance(Vector2D const &v1) const
Caluclates the distance from the Vector to v1.
Definition: geometry.cpp:13
bool SegmentIntersection(Edge const &edge1, Edge const &edge2, Vector2D &Intersection, double eps=1e-8)
Calculates the intersection of two edges.
Definition: Edge.cpp:45
Vector2D Parallel(Edge const &edge)
Calculates a unit vector parallel to an edge.
Definition: Edge.cpp:83
Edge between cells.
Edge(Vector2D const &p1, Vector2D const &p2, int neighbor1, int neighbor2)
Class constructor.
Definition: Edge.cpp:22
Interface between two cells.
Definition: Edge.hpp:13
double max(vector< double > const &v)
returns the maximal term in a vector
Definition: utils.cpp:52
Edge & operator=(const Edge &other)
copy operator
Definition: Edge.cpp:15
double ScalarProd(Vector3D const &v1, Vector3D const &v2)
Scalar product of two vectors.
Definition: Vector3D.cpp:185
std::pair< Vector2D, Vector2D > vertices
Points at the ends of the edge.
Definition: Edge.hpp:18
double min(vector< double > const &v)
Returns the minimal term in a vector.
Definition: utils.cpp:44
void Set(double ix, double iy)
Set vector components.
Definition: geometry.cpp:39
std::pair< int, int > neighbors
Neighboring cells.
Definition: Edge.hpp:21
Vector3D calc_centroid(const Face &face)
Calculates the centroid of aa face.
Definition: Face.cpp:32
double DistanceToEdge(Vector2D const &point, Edge const &edge)
Calculates the distance of a point to an edge.
Definition: Edge.cpp:31
double abs(Vector3D const &v)
Norm of a vector.
Definition: Vector3D.cpp:44
2D Mathematical vector
Definition: geometry.hpp:15
double GetLength(void) const
Returns the length of the edge.
Definition: Edge.cpp:26