]> git.donarmstrong.com Git - lilypond.git/blob - flower/offset.cc
Issue 4961/2: Add offset_directed (Real)
[lilypond.git] / flower / offset.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "offset.hh"
21
22 #ifndef STANDALONE
23 string
24 Offset::to_string () const
25 {
26   string s;
27   s = string (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", "
28       + ::to_string (coordinate_a_[Y_AXIS]) + ")";
29   return s;
30 }
31 #endif
32
33 /*
34   free bsd fix by John Galbraith
35 */
36
37 Offset
38 complex_multiply (Offset z1, Offset z2)
39 {
40   Offset z;
41   if (!isinf (z2[Y_AXIS]))
42     {
43       z[X_AXIS] = z1[X_AXIS] * z2[X_AXIS] - z1[Y_AXIS] * z2[Y_AXIS];
44       z[Y_AXIS] = z1[X_AXIS] * z2[Y_AXIS] + z1[Y_AXIS] * z2[X_AXIS];
45     }
46   return z;
47 }
48
49 Offset
50 complex_conjugate (Offset o)
51 {
52   o[Y_AXIS] = -o[Y_AXIS];
53   return o;
54 }
55
56 Offset
57 complex_divide (Offset z1, Offset z2)
58 {
59   z2 = complex_conjugate (z2);
60   Offset z = complex_multiply (z1, z2);
61   z *= 1 / z2.length ();
62   return z;
63 }
64
65 Offset
66 complex_exp (Offset o)
67 {
68   Real s = sin (o[Y_AXIS]);
69   Real c = cos (o[Y_AXIS]);
70
71   Real r = exp (o[X_AXIS]);
72
73   return Offset (r * c, r * s);
74 }
75
76 Real
77 Offset::arg () const
78 {
79   return atan2 (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
80 }
81
82 static inline Real
83 atan2d (Real y, Real x)
84 {
85   return atan2 (y, x) * (180.0 / M_PI);
86 }
87
88 Real
89 Offset::angle_degrees () const
90 {
91   Real x = coordinate_a_ [X_AXIS];
92   Real y = coordinate_a_ [Y_AXIS];
93
94   // We keep in the vicinity of multiples of 45 degrees here: this is
95   // where straightforward angles for straightforward angular
96   // relations are most expected.  The factors of 2 employed in the
97   // comparison are not really perfect for that: sqrt(2)+1 would be
98   // the factor giving exact windows of 45 degrees rather than what we
99   // have here.  It's just that 2 is likely to generate nicer code
100   // than 2.4 and the exact handover does not really matter.
101   //
102   // Comparisons here are chosen appropriately to let infinities end
103   // up in their "exact" branch.  As opposed to the normal atan2
104   // function behavior, this makes "competing" infinities result in
105   // NAN angles.
106   if (y < 0.0)
107     {
108       if (2*x < -y)
109         if (-x > -2*y)          // x < 0, y < 0, |x| > |2y|
110           return -180 + atan2d (-y, -x);
111         else if (-2*x >= -y)    // x < 0, y < 0, |y| < |2x| <= |4y|
112           return -135 + atan2d (x - y, -y - x);
113         else                    // y < 0, |y| >= |2x|
114           return -90 + atan2d (x, -y);
115       else if (x <= -2*y)       // x > 0, y < 0, |y| <= |2x| < |4y|
116         return -45 + atan2d (x + y, x - y);
117       // Drop through for y < 0, x > |2y|
118     }
119   else if (y > 0.0)
120     {
121       if (2*x < y)
122         if (-x > 2*y)           // x < 0, y >= 0, |x| > |2y|
123           return 180 - atan2d (y, -x);
124         else if (-2*x >= y)     // x < 0, y >= 0, |y| < |2x| <= |4y|
125           return 135 - atan2d (x + y, y - x);
126         else                    // y >= 0, |y| >= |2x|
127           return 90 - atan2d (x, y);
128       else if (x <= 2*y)        // x >= 0, y >= 0, |y| < |2x| < |4y|
129         return 45 - atan2d (x - y, x + y);
130       // Drop through for y > 0, x > |2y|
131     }
132   else
133     // we return 0 for (0,0).  NAN would be an option but is a
134     // nuisance for getting back to rectangular coordinates.  Strictly
135     // speaking, this argument would be just as valid for (+inf.0,
136     // +inf.0), but then infinities are already an indication of a
137     // problem in LilyPond.
138     return (x < 0.0) ? 180 : 0;
139   return atan2d (y, x);
140 }
141
142
143 /**
144    euclidian vector length / complex modulus
145 */
146 Real
147 Offset::length () const
148 {
149   return hypot (coordinate_a_[X_AXIS], coordinate_a_[Y_AXIS]);
150 }
151
152 bool
153 Offset::is_sane () const
154 {
155   return !isnan (coordinate_a_[X_AXIS])
156          && !isnan (coordinate_a_ [Y_AXIS])
157          && !isinf (coordinate_a_[X_AXIS])
158          && !isinf (coordinate_a_[Y_AXIS]);
159 }
160
161 Offset
162 Offset::direction () const
163 {
164   Offset d = *this;
165   if (isinf (d[X_AXIS]))
166     {
167       if (!isinf (d[Y_AXIS]))
168         return Offset ((d[X_AXIS] > 0.0 ? 1.0 : -1.0), 0.0);
169     }
170   else if (isinf (d[Y_AXIS]))
171     return Offset (0.0, (d[Y_AXIS] > 0.0 ? 1.0 : -1.0));
172   else if (d[X_AXIS] == 0.0 && d[Y_AXIS] == 0.0)
173     return d;
174   // The other cases propagate or produce NaN as appropriate.
175
176   d /= length ();
177   return d;
178 }
179
180 Offset
181 Offset::swapped () const
182 {
183   return Offset (coordinate_a_[Y_AXIS], coordinate_a_[X_AXIS]);
184 }
185
186 Offset
187 offset_directed (Real angle)
188 {
189   if (angle <= -360.0 || angle >= 360.0)
190     angle = fmod (angle, 360.0);
191   // Now |angle| < 360.0, and the absolute size is not larger than
192   // before, so we haven't lost precision.
193   if (angle <= -180.0)
194     angle += 360.0;
195   else if (angle > 180.0)
196     angle -= 360.0;
197   // Now -180.0 < angle <= 180.0 and we still haven't lost precision.
198   // We don't work with angles greater than 45 degrees absolute in
199   // order to minimize how rounding errors of M_PI/180 affect the
200   // result.  That way, at least angles that are a multiple of 90
201   // degree deliver the expected results.
202   //
203   // Sign of the sine is chosen to avoid -0.0 in results.  This
204   // version delivers exactly equal magnitude on x/y for odd multiples
205   // of 45 degrees at the cost of losing some less obvious invariants.
206
207   if (angle > 0)
208     if (angle > 90)
209       return Offset (sin ((90 - angle) * M_PI/180.0),
210                      sin ((180 - angle) * M_PI/180.0));
211     else
212       return Offset (sin ((90 - angle) * M_PI/180.0),
213                      sin (angle * M_PI/180.0));
214   else if (angle < -90)
215     return Offset (sin ((90 + angle) * M_PI/180.0),
216                    sin ((-180 - angle) * M_PI/180.0));
217   else
218     return Offset (sin ((90 + angle) * M_PI/180.0),
219                    sin (angle * M_PI/180.0));
220 }