LibreOffice Module tools (master) 1
degree.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice 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#pragma once
10
11#include <sal/config.h>
12
14#include <sal/types.h>
15#include <o3tl/strong_int.hxx>
17#include <cstdlib>
18#include <math.h>
19#include <numeric>
20
21template <int N> struct FractionTag;
22// 1/Nth fraction of a degree
23template <typename I, int N> using Degree = o3tl::strong_int<I, FractionTag<N>>;
24
25template <typename I, int N> char (&NofDegree(Degree<I, N>))[N]; // helper
26// Nof<DegreeN> gives compile-time constant N, needed in templates
27template <class D> constexpr int Nof = sizeof(NofDegree(std::declval<D>()));
28
31
33constexpr Degree10 operator""_deg10(unsigned long long n) { return Degree10{ n }; }
34
37
38// Android has trouble calling the correct overload of std::abs
39#ifdef ANDROID
40inline Degree100 abs(Degree100 x) { return Degree100(std::abs(static_cast<int>(x.get()))); }
41#else
42inline Degree100 abs(Degree100 x) { return Degree100(std::abs(x.get())); }
43#endif
44
46constexpr Degree100 operator""_deg100(unsigned long long n) { return Degree100{ n }; }
47
50template <class To, typename IofFrom, int NofFrom> inline To to(Degree<IofFrom, NofFrom> x)
51{
52 constexpr sal_Int64 m = Nof<To> / std::gcd(Nof<To>, NofFrom);
53 constexpr sal_Int64 d = NofFrom / std::gcd(Nof<To>, NofFrom);
54 return To{ o3tl::convert(x.get(), m, d) };
55}
56
57template <class D> inline double toRadians(D x) { return basegfx::deg2rad<Nof<D>>(x.get()); }
58template <class D> inline double toDegrees(D x) { return x.get() / static_cast<double>(Nof<D>); }
59
60/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
double d
Degree< sal_Int32, 100 > Degree100
hundredths of a Degree, normally rotation
Definition: degree.hxx:36
Degree< sal_Int16, 10 > Degree10
tenths of a Degree, normally rotation
Definition: degree.hxx:30
double toRadians(D x)
Definition: degree.hxx:57
Degree100 abs(Degree100 x)
Definition: degree.hxx:42
char(& NofDegree(Degree< I, N >))[N]
double toDegrees(D x)
Definition: degree.hxx:58
constexpr int Nof
Definition: degree.hxx:27
To to(Degree< IofFrom, NofFrom > x)
conversion functions
Definition: degree.hxx:50
float x
sal_Int64 n
m
constexpr auto convert(N n, sal_Int64 mul, sal_Int64 div)
#define N