summaryrefslogtreecommitdiff
path: root/lily/bezier-bow.cc
blob: e049bd734c244f78c8411324296514e8de219cc1 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
  This file is part of LilyPond, the GNU music typesetter.

  Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>

  LilyPond is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LilyPond is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "misc.hh"
#include "bezier.hh"

static Real
F0_1 (Real x)
{
  return 2 / M_PI * atan (M_PI * x / 2);
}

Real
slur_height (Real width, Real h_inf, Real r_0)
{
  return F0_1 (width * r_0 / h_inf) * h_inf;
}

/*
  ^                x                    x
  |
  height   <indent>
  |
  v      x                                       x



  For small w, the height should be proportional to w, for w ->
  infinity, the height should rise to a limit asymptotically.

  Hence we take F (x) such that
  F (0) = 0 , F' (0) = 1, and F (infty) = 1

  and use

  h = h_infinity * F (x * r_0 / h_infinity)


  Examples:

  * F (x) = 2/pi * atan (pi x/2)

  * F (x) = 1/alpha * x^alpha / (1 + x^alpha)

  * (etc.)

  [with the 2nd recipe you can determine how quickly the conversion from
  `small' slurs to `big' slurs occurs.]

  Although this might seem cand_idates to SCM-ify, it is not all clear
  which parameters (ie. h_inf, r_0, F (.)) should be candidates for
  this.  At present h_inf and r_0 come from layout settings, but we did
  no experiments for determining the best combinations of F, h_inf and
  r_0.


  The indent is proportional to the height of the slur for small
  slurs.  For large slurs, this gives a certain hookiness at the end,
  so we increase the indent.

  indent = G (w)

  w -> 0,  G (w) -> .33 w


  (due to derivative constraints, we cannot have indent > len/3)

  w -> inf, G (w) -> 2*h_inf

  i.e.


  G (0) = 0 , G'(0) 1/3, G (infty) = 2h_inf

  solve from

  G (w) = r  + p/(w+q)

  yields

  G (w) = 2 h_inf - max_fraction * q^2/ (w + q)

  with q = 2 h_inf
*/

void
get_slur_indent_height (Real *indent, Real *height,
                        Real width, Real h_inf, Real r_0)
{
  Real max_fraction = 1.0 / 3.1;
  *height = slur_height (width, h_inf, r_0);

  Real q = 2 * h_inf / max_fraction;
  *indent = 2 * h_inf - sqr (q) * max_fraction / (width + q);
}

Bezier
slur_shape (Real width, Real h_inf, Real r_0)
{
  Real indent;
  Real height;

  get_slur_indent_height (&indent, &height,
                          width, h_inf, r_0);

  Bezier curve;
  curve.control_[0] = Offset (0, 0);
  curve.control_[1] = Offset (indent, height);
  curve.control_[2] = Offset (width - indent, height);
  curve.control_[3] = Offset (width, 0);
  return curve;
}