Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
46namespace ogdf {
47namespace internal {
48namespace gcm {
49namespace geometry {
50using namespace tools;
51
52template<typename kernel>
53using Vector_t = CGAL::Vector_2<kernel>;
54
55template<typename kernel>
56inline 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
60template<typename kernel>
61inline 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
66template<typename kernel>
67inline typename kernel::FT dot(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
68 return v1 * v2;
69}
70
71template<typename kernel>
72inline typename kernel::FT cross(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
73 return CGAL::determinant(v1, v2);
74}
75
76template<typename kernel>
77inline bool turn(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
78 return cross(v1, v2) >= 0;
79}
80
81template<typename kernel>
82inline bool left_turn(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
83 return CGAL::orientation(v1, v2) == CGAL::LEFT_TURN;
84}
85
86template<typename kernel>
87inline bool right_turn(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
88 return CGAL::orientation(v1, v2) == CGAL::RIGHT_TURN;
89}
90
91template<typename kernel>
92inline bool parallel(const Vector_t<kernel>& v1, const Vector_t<kernel>& v2) {
93 return CGAL::orientation(v1, v2) == CGAL::COLLINEAR;
94}
95
96template<typename kernel>
97inline 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
102template<typename kernel>
103inline Vector_t<kernel> normal(const Vector_t<kernel>& v) {
104 return v.perpendicular(CGAL::POSITIVE);
105}
106
107template<typename kernel>
109 if ((-v1).direction() == v2.direction()) {
110 return normalize(normal(v1));
111 }
112 return (normalize(v1) + normalize(v2)) * 0.5;
113}
114
115template<typename kernel>
116inline 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
122template<typename kernel>
123inline 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
128template<typename kernel>
129inline 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
136template<typename kernel>
137inline bool is_valid(const Vector_t<kernel>& v) {
138 return !isinf(v.x()) && !isinf(v.y()) && !isnan(v.x()) && !isnan(v.y());
139}
140
141template<typename kernel>
142void 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
147template<typename kernel>
148void 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
153template<typename Vector>
154struct 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
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.
bool isinf(T value)
Definition PivotMDS.h:44