Debian Codegraph @

src/lib/ZMFTypes.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is a part of the libzmf project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "ZMFTypes.h"
11
12 #include <boost/math/constants/constants.hpp>
13
14 namespace libzmf
15 {
16
17 bool operator==(const Point &lhs, const Point &rhs)
18 {
19 return lhs.x == rhs.x && lhs.y == rhs.y;
20 }
21
22 bool operator!=(const Point &lhs, const Point &rhs)
23 {
24 return !(lhs == rhs);
25 }
26
27 Point Point::move(double dx, double dy) const
28 {
29 return Point(x + dx, y + dy);
30 }
31
32 Point Point::rotate(double rotation, const Point &center) const
33 {
34 double rotatedX = (x - center.x) * std::cos(rotation) - (y - center.y) * std::sin(rotation) + center.x;
35 double rotatedY = (y - center.y) * std::cos(rotation) + (x - center.x) * std::sin(rotation) + center.y;
36 return Point(rotatedX, rotatedY);
37 }
38
39 double Point::distance(const Point &p2) const
40 {
41 return std::hypot(p2.x - x, p2.y - y);
42 }
43
44
45 BoundingBox::BoundingBox(const std::vector<Point> &points_)
46 : m_points(points_)
47 , m_width(0.0)
48 , m_height(0.0)
49 , m_center()
50 , m_rotation(0)
51 , m_p1Quadrant(0)
52 , m_p2Quadrant(0)
53 , m_mirrorHorizontal(false)
54 , m_mirrorVertical(false)
55 {
56 using namespace boost::math::double_constants;
57
58 if (m_points.size() != 4)
59 throw GenericException();
60
61 // rectangle center is middle point of diagonal
62 m_center = Point((m_points[0].x + m_points[2].x) / 2.0, (m_points[0].y + m_points[2].y) / 2.0);
63
64 m_rotation = std::atan2(m_points[1].y - m_points[0].y, m_points[1].x - m_points[0].x);
65 if (m_rotation < 0)
66 m_rotation += two_pi;
67
68 std::vector<Point> originalPoints;
69 if (ZMF_ALMOST_ZERO(m_rotation))
70 {
71 originalPoints = m_points;
72 }
73 else
74 {
75 originalPoints.reserve(m_points.size());
76 for (auto p : m_points)
77 {
78 originalPoints.push_back(p.rotate(-m_rotation, m_center));
79 }
80 }
81
82 double dist1 = m_points[0].distance(m_points[1]);
83 double dist2 = m_points[0].distance(m_points[3]);
84
85 if (std::abs(originalPoints[0].x - originalPoints[1].x) > std::abs(originalPoints[0].x - originalPoints[3].x))
86 {
87 m_width = dist1;
88 m_height = dist2;
89 }
90 else
91 {
92 m_width = dist2;
93 m_height = dist1;
94 }
95
96 m_p1Quadrant = quadrant(originalPoints[0]);
97 m_p2Quadrant = quadrant(originalPoints[1]);
98
99 if (p1Quadrant() == 1 || p1Quadrant() == 4)
100 m_rotation -= pi;
101 if (m_rotation < 0)
102 m_rotation += two_pi;
103
104 m_mirrorHorizontal = m_p1Quadrant == 1 || m_p1Quadrant == 4;
105 m_mirrorVertical = m_p1Quadrant == 3 || m_p1Quadrant == 4;
106 }
107
108 const std::vector<Point> &BoundingBox::points() const
109 {
110 return m_points;
111 }
112
113 double BoundingBox::width() const
114 {
115 return m_width;
116 }
117
118 double BoundingBox::height() const
119 {
120 return m_height;
121 }
122
123 Point BoundingBox::center() const
124 {
125 return m_center;
126 }
127
128 Point BoundingBox::topLeft() const
129 {
130 return center().move(-width() / 2.0, -height() / 2.0);
131 }
132
133 double BoundingBox::rotation() const
134 {
135 return m_rotation;
136 }
137
138 int BoundingBox::p1Quadrant() const
139 {
140 return m_p1Quadrant;
141 }
142
143 int BoundingBox::p2Quadrant() const
144 {
145 return m_p2Quadrant;
146 }
147
148 bool BoundingBox::mirrorHorizontal() const
149 {
150 return m_mirrorHorizontal;
151 }
152
153 bool BoundingBox::mirrorVertical() const
154 {
155 return m_mirrorVertical;
156 }
157
158 int BoundingBox::quadrant(const Point &p) const
159 {
160 if (p.x > m_center.x)
161 {
162 if (p.y < m_center.y)
163 return 1;
164 else
165 return 4;
166 }
167 else
168 {
169 if (p.y < m_center.y)
170 return 2;
171 else
172 return 3;
173 }
174 }
175
176
177 librevenge::RVNGString Color::toString() const
178 {
179 librevenge::RVNGString colorStr;
180 colorStr.sprintf("#%.2x%.2x%.2x", red, green, blue);
181 return colorStr;
182 }
183
184 }
185
186 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */