Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Vector.h
Go to the documentation of this file.
1 
31 #pragma once
32 
33 #ifdef OGDF_INCLUDE_CGAL
34 
36 
37 # include <iostream>
38 # include <limits>
39 
40 # include <CGAL/Aff_transformation_2.h>
41 # include <CGAL/Cartesian.h>
42 # include <CGAL/Vector_2.h>
43 # include <CGAL/aff_transformation_tags.h>
44 # include <math.h>
45 
46 namespace ogdf {
47 namespace internal {
48 namespace gcm {
49 namespace geometry {
50 using namespace tools;
51 
52 template<typename kernel>
53 using Vector_t = CGAL::Vector_2<kernel>;
54 
55 template<typename kernel>
56 inline bool is_the_same(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
57  return v1.x() == v2.x() && v1.y() == v2.y();
58 }
59 
60 template<typename kernel>
61 inline Vector_t<kernel> rotate(const Vector_t<kernel>& v, const double angle) {
62  CGAL::Aff_transformation_2<kernel> rotation(CGAL::ROTATION, std::sin(angle), std::cos(angle));
63  return std::move(rotation(v));
64 }
65 
66 template<typename kernel>
67 inline typename kernel::FT dot(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
68  return v1 * v2;
69 }
70 
71 template<typename kernel>
72 inline typename kernel::FT cross(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
73  return CGAL::determinant(v1, v2);
74 }
75 
76 template<typename kernel>
77 inline bool turn(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
78  return cross(v1, v2) >= 0;
79 }
80 
81 template<typename kernel>
82 inline bool left_turn(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
83  return CGAL::orientation(v1, v2) == CGAL::LEFT_TURN;
84 }
85 
86 template<typename kernel>
87 inline bool right_turn(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
88  return CGAL::orientation(v1, v2) == CGAL::RIGHT_TURN;
89 }
90 
91 template<typename kernel>
92 inline bool parallel(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
93  return CGAL::orientation(v1, v2) == CGAL::COLLINEAR;
94 }
95 
96 template<typename kernel>
97 inline Vector_t<kernel> normalize(const Vector_t<kernel>& v) {
98  OGDF_ASSERT(v.squared_length() > 0);
99  return v * CGAL::inverse(tools::approx_sqrt(v.squared_length()));
100 }
101 
102 template<typename kernel>
103 inline Vector_t<kernel> normal(const Vector_t<kernel>& v) {
104  return v.perpendicular(CGAL::POSITIVE);
105 }
106 
107 template<typename kernel>
108 inline Vector_t<kernel> bisect(const Vector_t<kernel> v1, const Vector_t<kernel>& v2) {
109  if ((-v1).direction() == v2.direction()) {
110  return normalize(normal(v1));
111  }
112  return (normalize(v1) + normalize(v2)) * 0.5;
113 }
114 
115 template<typename kernel>
116 inline typename kernel::FT cos_angle(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
117  OGDF_ASSERT(!isZero(v1.squared_length()) && !isZero(v2.squared_length()));
118  typename kernel::FT v = v1 * v2;
119  return v * CGAL::inverse(tools::approx_sqrt(v1.squared_length() * v2.squared_length()));
120 }
121 
122 template<typename kernel>
123 inline typename kernel::FT angle(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
124  return tools::acos(cos_angle(v1, v2));
125 }
126 
127 //ccw
128 template<typename kernel>
129 inline typename kernel::FT full_angle(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
130  using type = typename kernel::FT;
131  const type alpha = geometry::angle(v1, v2);
132  const bool left = left_turn(v1, v2);
133  return (type)left * alpha + (type)(1.0 - left) * ((type)2.0 * tools::const_pi<type>() - alpha);
134 }
135 
136 template<typename kernel>
137 inline bool is_valid(const Vector_t<kernel>& v) {
138  return !isinf(v.x()) && !isinf(v.y()) && !isnan(v.x()) && !isnan(v.y());
139 }
140 
141 template<typename kernel>
142 void serialize(const Vector_t<kernel>& v, std::ostream& os) {
143  os.write(reinterpret_cast<const char*>(&v.x()), sizeof(v.x()));
144  os.write(reinterpret_cast<const char*>(&v.y()), sizeof(v.y()));
145 }
146 
147 template<typename kernel>
148 void deserialize(Vector_t<kernel>& v, std::istream& in) {
149  in.read(reinterpret_cast<char*>(&v.x()), sizeof(v.x()));
150  in.read(reinterpret_cast<char*>(&v.y()), sizeof(v.y()));
151 }
152 
153 template<typename Vector>
154 struct VectorExactLess {
155  bool operator()(const Vector& a, const Vector& b) const {
156  return a.x() < b.x() || (a.x() == b.x() && a.y() < b.y());
157  }
158 };
159 
160 } //namespace
161 }
162 }
163 }
164 
165 #endif
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
math.h
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
backward::Color::type
type
Definition: backward.hpp:1716
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:243
ogdf::isinf
bool isinf(T value)
Definition: PivotMDS.h:44
ogdf::orientation
int orientation(const DPoint &p, const DPoint &q, const DPoint &r)