]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur-scoring.cc
(struct Slur_score_state): new
[lilypond.git] / lily / slur-scoring.cc
1 /*
2   slur-quanting.cc -- Score based slur formatting
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include <math.h>
11
12 #include "accidental-interface.hh"
13 #include "beam.hh"
14 #include "directional-element-interface.hh"
15 #include "group-interface.hh"
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
18 #include "slur.hh"
19 #include "note-column.hh"
20 #include "output-def.hh"
21 #include "pitch.hh"
22 #include "bezier.hh"
23 #include "spanner.hh"
24 #include "staff-symbol-referencer.hh"
25 #include "staff-symbol.hh"
26 #include "stem.hh"
27 #include "warn.hh"
28 #include "paper-column.hh"
29
30 /*
31   TODO:
32
33   - curve around flag for y coordinate
34
35   - this file is a big mess, clean it up
36
37   - short-cut: try a smaller region first.
38
39   - handle non-visible stems better.
40
41   - try to prune number of scoring criteria
42
43   - take encompass-objects more into account when determining
44   slur shape
45
46   - calculate encompass scoring directly after determining slur shape.
47
48   - optimize.
49
50 */
51
52 struct Slur_score
53 {
54   Drul_array<Offset> attachment_;
55   Real score_;
56   Bezier curve_;
57
58 #if DEBUG_SLUR_QUANTING
59   String score_card_;
60 #endif
61
62   Slur_score ()
63   {
64     score_ = 0.0;
65   }
66 };
67
68 struct Slur_score_parameters
69 {
70   int region_size_;
71   Real head_encompass_penalty_;
72   Real stem_encompass_penalty_;
73   Real closeness_factor_;
74   Real edge_attraction_factor_;
75   Real same_slope_penalty_;
76   Real steeper_slope_factor_;
77   Real non_horizontal_penalty_;
78   Real max_slope_;
79   Real max_slope_factor_;
80   Real extra_object_collision_;
81   Real accidental_collision_;
82   Real free_slur_distance_;
83   Real free_head_distance_;
84   Real extra_encompass_free_distance_;
85   Real edge_slope_exponent_;
86   Real head_slur_distance_max_ratio_;
87   Real head_slur_distance_factor_;
88 };
89
90
91
92 struct Extra_collision_info
93 {
94   Real idx_;
95   Box extents_;
96   Real penalty_;
97   Grob * grob_;
98
99   Extra_collision_info (Grob *g, Real idx, Interval x, Interval y, Real p)
100   {
101     idx_ = idx;
102     extents_[X_AXIS] = x;
103     extents_[Y_AXIS] = y;
104     penalty_ = p;
105     grob_ = g;
106   }
107   Extra_collision_info ()
108   {
109     idx_ = 0.0;
110     penalty_ = 0.;
111     grob_ = 0;
112   }
113 };
114
115
116 struct Encompass_info
117 {
118   Real x_;
119   Real stem_;
120   Real head_;
121   Encompass_info ()
122   {
123     x_ = 0.0;
124     stem_ = 0.0;
125     head_ = 0.0;
126   }
127   Real get_point (Direction dir)
128   {
129     Interval y;
130     y.add_point (stem_);
131     y.add_point (head_);
132     return y[dir];
133   }
134 };
135
136 struct Bound_info
137 {
138   Box stem_extent_;
139   Direction stem_dir_;
140   Item *bound_;
141   Grob *note_column_;
142   Grob *slur_head_;
143   Grob *staff_;
144   Grob *stem_;
145   Interval slur_head_extent_;
146   Real neighbor_y_;
147   Real staff_space_;
148
149   Bound_info ()
150   {
151     stem_ = 0;
152     neighbor_y_ = 0;
153     staff_ = 0;
154     slur_head_ = 0;
155     stem_dir_ = CENTER;
156     note_column_ = 0;
157   }
158 };
159
160 struct Slur_score_state
161 {
162   Spanner *slur_;
163   Grob *common_[NO_AXES];
164   bool valid_;
165   
166   Link_array<Grob> columns_;
167   Array<Encompass_info> encompass_infos_;
168   Direction dir_;
169   Slur_score_parameters parameters_;
170   Drul_array<Bound_info> extremes_;
171   Drul_array<Offset> base_attachments_;
172   Array<Slur_score> *scores_;
173   Real staff_space_;
174   Real thickness_;
175   
176   Slur_score_state();
177   ~Slur_score_state();
178 };
179
180 Slur_score_state::Slur_score_state()
181 {
182   valid_ = false;
183   dir_ = CENTER;
184   slur_ = 0;
185   common_[X_AXIS] = 0;
186   common_[Y_AXIS] = 0;
187   scores_ = 0;
188 }
189
190 Slur_score_state::~Slur_score_state ()
191 {
192   delete scores_;
193 }
194
195 static void score_extra_encompass (Slur_score_state const&);
196 static void score_slopes  (Slur_score_state const&);
197 static void score_edges (Slur_score_state const&);
198 static void score_encompass (Slur_score_state const&);
199 static Bezier avoid_staff_line  (Slur_score_state const&,
200                                 Bezier bez);
201 static Encompass_info get_encompass_info (Slur_score_state const&, Grob *col);
202 static Bezier get_bezier (Slur_score_state const&,
203                           Drul_array<Offset>,
204                           Real r_0, Real h_inf);
205 static Direction get_default_dir (Grob *me);
206
207 static void set_end_points (Grob *);
208 static Real broken_trend_y (Slur_score_state const&, Direction dir);
209 static Drul_array<Bound_info> get_bound_info (Slur_score_state const&);
210
211 static void generate_curves (Slur_score_state const&);
212 static Array<Slur_score> *enumerate_attachments (Slur_score_state const&,
213                                                 Drul_array<Real> end_ys);
214 static Drul_array<Offset> get_base_attachments(Slur_score_state const&);
215 static Drul_array<Real> get_y_attachment_range(Slur_score_state const&);
216
217
218 Real
219 get_detail (SCM alist, SCM sym)
220 {
221   SCM entry = scm_assq (sym, alist);
222   return robust_scm2double (scm_is_pair (entry)
223                             ? ly_cdr (entry)
224                             : SCM_EOL,
225                             0.0);
226 }
227
228 void
229 init_score_param (Grob *me,
230                   Slur_score_parameters *score_param)
231 {
232   SCM details = me->get_property ("slur-details");
233
234   score_param->region_size_
235     = (int) get_detail (details, ly_symbol2scm ("region-size"));
236   score_param->head_encompass_penalty_
237     = get_detail (details, ly_symbol2scm ("head-encompass-penalty"));
238   score_param->stem_encompass_penalty_
239     = get_detail (details, ly_symbol2scm ("stem-encompass-penalty"));
240   score_param->closeness_factor_
241     = get_detail (details, ly_symbol2scm ("closeness-factor"));
242   score_param->edge_attraction_factor_
243     = get_detail (details, ly_symbol2scm ("edge-attraction-factor"));
244   score_param->same_slope_penalty_
245     = get_detail (details, ly_symbol2scm ("same-slope-penalty"));
246   score_param->steeper_slope_factor_
247     = get_detail (details, ly_symbol2scm ("steeper-slope-factor"));
248   score_param->non_horizontal_penalty_
249     = get_detail (details, ly_symbol2scm ("non-horizontal-penalty"));
250   score_param->max_slope_
251     = get_detail (details, ly_symbol2scm ("max-slope"));
252   score_param->max_slope_factor_
253     = get_detail (details, ly_symbol2scm ("max-slope-factor"));
254   score_param->free_head_distance_
255     = get_detail (details, ly_symbol2scm ("free-head-distance"));
256   score_param->extra_object_collision_
257     = get_detail (details, ly_symbol2scm ("extra-object-collision"));
258   score_param->accidental_collision_
259     = get_detail (details, ly_symbol2scm ("accidental-collision"));
260   score_param->extra_encompass_free_distance_
261     = get_detail (details, ly_symbol2scm ("extra-encompass-free-distance"));
262   score_param->head_slur_distance_factor_
263     = get_detail (details, ly_symbol2scm ("head-slur-distance-factor"));
264   score_param->head_slur_distance_max_ratio_
265     = get_detail (details, ly_symbol2scm ("head-slur-distance-max-ratio"));
266   score_param->free_slur_distance_
267     = get_detail (details, ly_symbol2scm ("free-slur-distance"));
268   score_param->edge_slope_exponent_
269     = get_detail (details, ly_symbol2scm ("edge-slope-exponent"));
270 }
271
272
273
274 /* HDIR indicates which side (left or right) we are processing here.  */
275 Real
276 broken_trend_y (Slur_score_state const &state, Direction hdir)
277 {
278   /* A broken slur should maintain the same vertical trend
279      the unbroken slur would have had.  */
280   Real by = 0.0;
281   if (Spanner *mother = dynamic_cast<Spanner*> (state.slur_->original_))
282     {
283       int k = broken_spanner_index (state.slur_);
284       int j = k + hdir;
285       if (j < 0 || j >= mother->broken_intos_.size ())
286         return by;
287
288       Grob *neighbor = mother->broken_intos_[j];
289       if (hdir == RIGHT)
290         neighbor->set_property ("direction", scm_from_int (state.dir_));
291
292       Spanner *common_mother
293         = dynamic_cast<Spanner*> (state.common_[Y_AXIS]->original_);
294       int common_k
295         = broken_spanner_index (dynamic_cast<Spanner*> (state.common_[Y_AXIS]));
296       int common_j = common_k + hdir;
297
298       if (common_j < 0 || common_j >= common_mother->broken_intos_.size ())
299         return by;
300
301       Grob *common_next_system = common_mother->broken_intos_[common_j];
302       Link_array<Grob> neighbor_cols
303         = Pointer_group_interface__extract_grobs (neighbor, (Grob *)0,
304                                                   "note-columns");
305
306       Grob *neighbor_col
307         = (hdir == RIGHT) ? neighbor_cols[0] : neighbor_cols.top ();
308       Grob *neighbor_common
309         = common_next_system->common_refpoint (neighbor_col, Y_AXIS);
310
311       Direction vdir = state.dir_;
312       Real neighbor_y
313         = neighbor_col->extent (neighbor_common, Y_AXIS)
314         .linear_combination (int (vdir))
315         - common_next_system->relative_coordinate (neighbor_common, Y_AXIS);
316
317       Grob *extreme_col = (hdir == RIGHT) ? state.columns_.top () : state.columns_[0];
318       Real y = extreme_col->extent (state.common_[Y_AXIS], Y_AXIS)
319         .linear_combination (int ((state.columns_.size () == 1) ? CENTER : vdir));
320       by = (y*neighbor_cols.size () + neighbor_y*state.columns_.size ()) /
321          (state.columns_.size () + neighbor_cols.size ());
322     }
323   return by;
324 }
325
326 Encompass_info
327 get_encompass_info (Slur_score_state const &state,
328                     Grob *col)
329 {
330   Grob *stem = unsmob_grob (col->get_property ("stem"));
331   Encompass_info ei;
332
333   if (!stem)
334     {
335       programming_error ("No stem for note column?");
336       ei.x_ = col->relative_coordinate (state.common_[X_AXIS], X_AXIS);
337       ei.head_ = ei.stem_ = col->extent (state.common_[Y_AXIS],
338                                          Y_AXIS)[state.dir_];
339       return ei;
340     }
341   Direction stem_dir = get_grob_direction (stem);
342
343   if (Grob *head = Note_column::first_head (col))
344     ei.x_ = head->extent (state.common_[X_AXIS], X_AXIS).center ();
345   else
346     ei.x_ = col->extent (state.common_[X_AXIS], X_AXIS).center ();
347
348   Grob *h = Stem::extremal_heads (stem)[Direction (state.dir_)];
349   if (!h)
350     {
351       ei.head_ = ei.stem_ = col->extent (state.common_[Y_AXIS], Y_AXIS)[state.dir_];
352       return ei;
353     }
354
355   ei.head_ = h->extent (state.common_[Y_AXIS], Y_AXIS)[state.dir_];
356
357   if ((stem_dir == state.dir_)
358       && !stem->extent (stem, Y_AXIS).is_empty ())
359     {
360       ei.stem_ = stem->extent (state.common_[Y_AXIS], Y_AXIS)[state.dir_];
361       if (Grob *b = Stem::get_beam (stem))
362         ei.stem_ += stem_dir * 0.5 * Beam::get_thickness (b);
363
364       Interval x = stem->extent (state.common_[X_AXIS], X_AXIS);
365       ei.x_ = x.is_empty ()
366         ? stem->relative_coordinate (state.common_[X_AXIS], X_AXIS)
367         : x.center ();
368     }
369   else
370     ei.stem_ = ei.head_;
371
372   return ei;
373 }
374
375
376 Direction
377 get_default_dir (Grob*me)
378 {
379   Link_array<Grob> encompasses
380     = Pointer_group_interface__extract_grobs (me, (Grob*) 0, "note-columns");
381
382   Direction d = DOWN;
383   for (int i= 0; i < encompasses.size (); i ++)
384     {
385       if (Note_column::dir (encompasses[i]) < 0)
386         {
387           d = UP;
388           break;
389         }
390     }
391   return d;
392 }
393
394
395
396 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking,1);
397 SCM
398 Slur::after_line_breaking (SCM smob)
399 {
400   Spanner *me = dynamic_cast<Spanner*> (unsmob_grob (smob));
401   if (!scm_ilength (me->get_property ("note-columns")))
402     {
403       me->suicide ();
404       return SCM_UNSPECIFIED;
405     }
406
407   if (!get_grob_direction (me))
408     set_grob_direction (me, get_default_dir (me));
409
410   if (scm_ilength (me->get_property ("control-points")) < 4)
411     set_end_points (me);
412
413   return SCM_UNSPECIFIED;
414 }
415
416 Drul_array<Bound_info>
417 get_bound_info (Slur_score_state const &state)
418 {
419   Drul_array<Bound_info> extremes;
420
421   Direction d = RIGHT;
422   Direction dir = state.dir_;
423
424   do
425     {
426       extremes[d].bound_ = state.slur_->get_bound (d);
427       if (Note_column::has_interface (extremes[d].bound_))
428         {
429           extremes[d].note_column_ = extremes[d].bound_;
430           extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
431           extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
432           extremes[d].stem_extent_[X_AXIS]
433             = extremes[d].stem_->extent (state.common_[X_AXIS], X_AXIS);
434           extremes[d].stem_extent_[Y_AXIS]
435             = extremes[d].stem_->extent (state.common_[Y_AXIS], Y_AXIS);
436           extremes[d].slur_head_
437             = Stem::extremal_heads (extremes[d].stem_)[dir];
438           if (!extremes[d].slur_head_
439               && Note_column::has_rests (extremes[d].bound_))
440             {
441               extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
442             }
443
444           if (extremes[d].slur_head_)
445             extremes[d].slur_head_extent_
446               = extremes[d].slur_head_->extent (state.common_[X_AXIS], X_AXIS);
447
448           extremes[d].staff_ = Staff_symbol_referencer
449             ::get_staff_symbol (extremes[d].stem_);
450           extremes[d].staff_space_ = Staff_symbol_referencer
451             ::staff_space (extremes[d].stem_);
452         }
453       else if (d == RIGHT)
454         /*
455           right side anticipates on the next note.
456         */
457         extremes[d].neighbor_y_ = broken_trend_y (state, d);
458     }
459   while (flip (&d) != RIGHT);
460   return extremes;
461 }
462
463 void
464 fill_scoring_state (Grob *me, Slur_score_state *state_ptr)
465 {
466   Slur_score_state &state = *state_ptr;
467   state.slur_ = dynamic_cast<Spanner*> (me);
468   state.columns_ 
469     = Pointer_group_interface__extract_grobs (me, (Grob *) 0, "note-columns");
470   
471   if (state.columns_.is_empty ())
472     {
473       me->suicide ();
474       return ;
475     }
476
477   state.staff_space_ = Staff_symbol_referencer::staff_space (me);
478   Real lt = me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
479   state.thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) *  lt;
480   
481   state.dir_ = get_grob_direction (me);
482   init_score_param (me, &state.parameters_);
483   
484   SCM eltlist = me->get_property ("note-columns");
485   SCM extra_list = me->get_property ("encompass-objects");
486   Spanner *sp = dynamic_cast<Spanner*> (me);
487
488   for (int i = X_AXIS; i < NO_AXES; i++)
489     {
490       Axis a = (Axis)i;
491       state.common_[a] = common_refpoint_of_list (eltlist, me, a);
492       state.common_[a] = common_refpoint_of_list (extra_list, state.common_[a], a);
493
494       Direction d = LEFT;
495       do {
496         state.common_[a] = state.common_[a]->common_refpoint (sp->get_bound (d), a);
497       }
498       while (flip (&d) != LEFT);
499     }
500
501   state.extremes_ = get_bound_info (state);
502   state.base_attachments_ = get_base_attachments (state);
503
504   Drul_array<Real> end_ys
505     = get_y_attachment_range (state);
506
507   state.scores_ = enumerate_attachments (state, end_ys);
508   for (int i = 0; i < state.columns_.size (); i++)
509     state.encompass_infos_.push (get_encompass_info (state, state.columns_[i]));
510
511   state.valid_ = true;
512 }
513
514 void
515 set_end_points (Grob *me)
516 {
517   Slur_score_state state;
518   fill_scoring_state (me, &state);
519
520   if (!state.valid_)
521     return;
522   
523   generate_curves (state);
524   score_edges (state);
525   score_slopes (state);
526   score_encompass (state);
527   score_extra_encompass (state);
528   
529   Real opt = 1e6;
530   int opt_idx = -1;
531   for (int i = 0; i < state.scores_->size (); i++)
532     {
533       if ((*state.scores_)[i].score_ < opt)
534         {
535           opt = (*state.scores_)[i].score_;
536           opt_idx = i;
537         }
538     }
539
540 #if DEBUG_SLUR_QUANTING
541   SCM inspect_quants = me->get_property ("inspect-quants");
542   if (to_boolean (me->get_paper ()
543                   ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
544       && scm_is_pair (inspect_quants))
545     {
546       Drul_array<Real> ins = ly_scm2interval (inspect_quants);
547       Real mindist = 1e6;
548       for (int i = 0; i < state.scores_->size (); i ++)
549         {
550           Real d =fabs ((*state.scores_)[i].attachment_[LEFT][Y_AXIS] - ins[LEFT])
551             + fabs ((*state.scores_)[i].attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
552           if (d < mindist)
553             {
554               opt_idx = i;
555               mindist= d;
556             }
557         }
558       if (mindist > 1e5)
559         programming_error ("Could not find quant.");
560     }
561   (*state.scores_)[opt_idx].score_card_ += to_string ("i%d", opt_idx);
562
563   // debug quanting
564   me->set_property ("quant-score",
565                     scm_makfrom0str ((*state.scores_)[opt_idx].score_card_.to_str0 ()));
566 #endif
567
568   Bezier b = (*state.scores_)[opt_idx].curve_;
569   SCM controls = SCM_EOL;
570   for (int i = 4; i--;)
571     {
572       Offset o = b.control_[i]
573         - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
574                   me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
575       controls = scm_cons (ly_offset2scm (o), controls);
576     }
577   me->set_property ("control-points", controls);
578 }
579
580 /*
581   TODO: should analyse encompasses to determine sensible region, and
582   should limit slopes available.
583  */
584
585 Drul_array<Real>
586 get_y_attachment_range (Slur_score_state const &state)
587 {
588   Drul_array<Real> end_ys;
589   Direction d = LEFT;
590   do
591     {
592       if (state.extremes_[d].note_column_)
593         {
594           end_ys[d] = state.dir_
595             * ((state.dir_ * (state.base_attachments_[d][Y_AXIS] +  state.parameters_.region_size_* state.dir_))
596                >? (state.dir_ * (state.dir_ + state.extremes_[d].note_column_->extent (state.common_[Y_AXIS],
597                                                                  Y_AXIS)[state.dir_]))
598                >? (state.dir_ * state.base_attachments_[-d][Y_AXIS]));
599         }
600       else
601         end_ys[d] = state.base_attachments_[d][Y_AXIS] + state.parameters_.region_size_ * state.dir_;
602     }
603   while (flip (&d) != LEFT);
604
605   return end_ys;
606 }
607
608 bool
609 spanner_less (Spanner *s1, Spanner* s2)
610 {
611   Slice b1, b2;
612   Direction d  = LEFT;
613   do
614     {
615       b1[d] = s1->get_bound (d)->get_column ()->rank_;
616       b2[d] = s2->get_bound (d)->get_column ()->rank_;
617     } while (flip (&d) != LEFT);
618
619   return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
620     && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
621 }
622
623
624 Drul_array<Offset>
625 get_base_attachments (Slur_score_state const &state)
626 {
627   Drul_array<Offset> base_attachment;
628   Direction d = RIGHT;
629   do
630     {
631       Grob *stem = state.extremes_[d].stem_;
632       Grob *head = state.extremes_[d].slur_head_;
633
634       Real x, y;
635       if (!state.extremes_[d].note_column_)
636         {
637           if (d == RIGHT)
638             {
639               y = state.extremes_[d].neighbor_y_;
640               x = state.extremes_[d].bound_->extent (state.common_[X_AXIS], X_AXIS)[d];
641             }
642           else
643             {
644               x = state.slur_->get_broken_left_end_align ();
645               if (state.extremes_[RIGHT].bound_ == state.columns_[0])
646                 {
647                   y = base_attachment[RIGHT][Y_AXIS];
648                 }
649               else
650                 {
651                   y = state.columns_[0]->extent (state.common_[Y_AXIS], Y_AXIS)[state.dir_];
652                 }
653             }
654         }
655       else
656         {
657           bool same_beam =
658          (state.extremes_[d].stem_ && state.extremes_[-d].stem_
659              && Stem::get_beam (state.extremes_[d].stem_) == Stem::get_beam (state.extremes_[-d].stem_));
660
661           /*
662             fixme: X coord should also be set in this case.
663            */
664           if (stem
665               && state.extremes_[d].stem_dir_ == state.dir_
666               && Stem::get_beaming (stem, -d)
667               && (!spanner_less (state.slur_, Stem::get_beam (stem))
668                   || same_beam))
669             y = state.extremes_[d].stem_extent_[Y_AXIS][state.dir_];
670           else if (head)
671             y = head->extent (state.common_[Y_AXIS], Y_AXIS)[state.dir_];
672           y += state.dir_ * 0.5 * state.staff_space_;
673
674           Real pos
675             = (y - state.extremes_[d].staff_->relative_coordinate (state.common_[Y_AXIS],
676                                                             Y_AXIS))
677             * 2.0 / state.staff_space_;
678
679           /* start off staffline. */
680           if (fabs (pos - my_round (pos)) < 0.2
681               && Staff_symbol_referencer::on_staffline (head, (int) rint (pos))
682               && Staff_symbol_referencer::line_count (head) - 1 >= rint (pos)
683               )
684             // TODO: calc from slur thick & line thick, parameter.      
685             y += 1.5 * state.staff_space_ * state.dir_ / 10;
686
687           Grob * fh = Note_column::first_head (state.extremes_[d].note_column_);
688           x =
689          (fh ? fh->extent (state.common_[X_AXIS], X_AXIS)
690              : state.extremes_[d].bound_->extent (state.common_[X_AXIS], X_AXIS))
691             .linear_combination (CENTER);
692         }
693       base_attachment[d] = Offset (x, y);
694
695     } while (flip (&d) != RIGHT);
696
697   return base_attachment;
698 }
699
700 void
701 generate_curves (Slur_score_state const &state)
702 {
703   Real r_0 = robust_scm2double (state.slur_->get_property ("ratio"), 0.33);
704   Real h_inf = state.staff_space_ *scm_to_double (state.slur_->get_property ("height-limit"));
705   for (int i = 0; i < state.scores_->size (); i++)
706     {
707       Bezier bez = get_bezier (state,
708                                (*state.scores_)[i].attachment_, r_0, h_inf);
709
710       bez = avoid_staff_line (state, bez);
711       (*state.scores_)[i].attachment_[LEFT] = bez.control_[0];
712       (*state.scores_)[i].attachment_[RIGHT] = bez.control_[3];
713       (*state.scores_)[i].curve_ = bez;
714     }
715 }
716
717 Bezier
718 avoid_staff_line (Slur_score_state const &state, 
719                   Bezier bez)
720 {
721   Offset horiz (1,0);
722   Array<Real> ts = bez.solve_derivative (horiz);
723
724   /* TODO: handle case of broken slur.  */
725   if (!ts.is_empty ()
726       && (state.extremes_[LEFT].staff_ == state.extremes_[RIGHT].staff_)
727       && state.extremes_[LEFT].staff_ && state.extremes_[RIGHT].staff_)
728     {
729       Real y = bez.curve_point (ts[0])[Y_AXIS];
730
731       Grob *staff = state.extremes_[LEFT].staff_;
732
733       Real p = 2 * (y - staff->relative_coordinate (state.common_[Y_AXIS], Y_AXIS))
734         / state.staff_space_;
735
736       Real distance = fabs (my_round (p) - p);  //  in halfspaces
737       if (distance < 4 * state.thickness_
738           && (int) fabs (my_round (p))
739           <= 2 * Staff_symbol_referencer::staff_radius (staff) + 0.1
740           && (int (fabs (my_round (p))) % 2
741               != Staff_symbol_referencer::line_count (staff) % 2))
742         {
743           Direction resolution_dir =
744          (distance ?  state.dir_ : Direction (sign (p - my_round (p))));
745
746           // TODO: parameter
747           Real newp = my_round (p) + resolution_dir
748             * 5 * state.thickness_;
749         
750           Real dy = (newp - p) * state.staff_space_ / 2.0;
751         
752           bez.control_[1][Y_AXIS] += dy;
753           bez.control_[2][Y_AXIS] += dy;
754         }
755     }
756   return bez;
757 }
758
759 Array<Slur_score> *
760 enumerate_attachments (Slur_score_state const &state, Drul_array<Real> end_ys)
761 {
762   /*ugh.   */
763   Array<Slur_score> scores;
764
765
766   Drul_array<Offset> os;
767   os[LEFT] = state.base_attachments_[LEFT];
768   Real minimum_length = state.staff_space_
769     * robust_scm2double (state.slur_->get_property ("minimum-length"), 2.0);
770
771   for (int i = 0; state.dir_ * os[LEFT][Y_AXIS] <= state.dir_ * end_ys[LEFT]; i++)
772     {
773       os[RIGHT] = state.base_attachments_[RIGHT];
774       for (int j = 0; state.dir_ * os[RIGHT][Y_AXIS] <= state.dir_ * end_ys[RIGHT]; j++)
775         {
776           Slur_score s;
777           Direction d = LEFT;
778           Drul_array<bool> attach_to_stem (false, false);
779           do
780             {
781               os[d][X_AXIS] = state.base_attachments_[d][X_AXIS];
782               if (state.extremes_[d].stem_
783                   && !Stem::is_invisible (state.extremes_[d].stem_)
784                   && state.extremes_[d].stem_dir_ == state.dir_)
785                 {
786                   Interval stem_y = state.extremes_[d].stem_extent_[Y_AXIS];
787                   stem_y.widen (0.25 * state.staff_space_);
788                   if (state.dir_ == -d
789                       && stem_y.contains (os[d][Y_AXIS]))
790                     {
791                       os[d][X_AXIS] =  state.extremes_[d].slur_head_extent_[-d]
792                         - d * 0.3;
793                       attach_to_stem[d] = true;
794                     }
795                   else if (state.dir_ *state.extremes_[d].stem_extent_[Y_AXIS][state.dir_]
796                              < state.dir_ * os[d][Y_AXIS]
797                            && !state.extremes_[d].stem_extent_[X_AXIS].is_empty ()
798                            )
799                 
800                     os[d][X_AXIS] = state.extremes_[d].stem_extent_[X_AXIS].center ();
801                 }
802             }
803           while (flip (&d) != LEFT);
804
805           Offset dz;    
806           dz = os[RIGHT] - os[LEFT];
807           if (dz[X_AXIS] < minimum_length
808               || fabs (dz[Y_AXIS] / dz[X_AXIS]) > state.parameters_.max_slope_
809               )
810             {
811               do
812                 {
813                   if (state.extremes_[d].slur_head_)
814                     {
815                       os[d][X_AXIS] = state.extremes_[d].slur_head_extent_.center ();
816                       attach_to_stem[d] = false;
817                     }
818                 }
819               while (flip (&d) != LEFT);
820             }
821
822           dz = os[RIGHT] - os[LEFT];
823           do
824             {
825               if (state.extremes_[d].slur_head_
826                   && !attach_to_stem[d])
827                 {
828                   /* Horizontally move tilted slurs a little.  Move
829                      more for bigger tilts.
830                 
831                      TODO: parameter */
832                   os[d][X_AXIS]
833                     -= state.dir_ * state.extremes_[d].slur_head_extent_.length ()
834                     * sin (dz.arg ()) / 3;
835                 }
836             }
837           while (flip (&d) != LEFT);
838         
839           s.attachment_ = os;
840           scores.push (s);
841         
842           os[RIGHT][Y_AXIS] += state.dir_ * state.staff_space_ / 2;
843         }
844
845       os[LEFT][Y_AXIS] += state.dir_ * state.staff_space_ / 2;
846     }
847
848   assert (scores.size () > 0);
849   return new Array<Slur_score> (scores);
850 }
851
852 inline Real
853 linear_interpolate (Real x, Real x1, Real x2,  Real y1, Real  y2)
854 {
855   return (x2 - x) / (x2 - x1) * y1 +
856  (x - x1) / (x2 - x1) * y2 ;
857 }
858
859
860 void
861 score_encompass (Slur_score_state const &state)
862 {
863   Array<Encompass_info> infos;
864
865
866   for (int i = 0; i < state.scores_->size (); i++)
867     {
868       Slur_score &configuration = state.scores_->elem_ref (i);
869       Bezier const &bez (configuration.curve_);
870       Real demerit = 0.0;
871
872       /*
873         Distances for heads that are between slur and line between
874         attachment points.
875        */
876       Array<Real> convex_head_distances;
877       Array<Real> edge_distances;
878       for (int j = 0; j < infos.size (); j++)
879         {
880           Real x = infos[j].x_;
881
882           bool l_edge = j==0;
883           bool r_edge = j==infos.size ()-1;
884           bool edge =  l_edge || r_edge;
885
886
887           if (edge)
888           {
889             edge_distances.push (fabs (configuration.attachment_[l_edge ? LEFT : RIGHT][Y_AXIS]
890                                        - infos[j].get_point (state.dir_)));
891           }
892         
893         
894           if (! (x < configuration.attachment_[RIGHT][X_AXIS]
895                 && x > configuration.attachment_[LEFT][X_AXIS]))
896             continue;
897         
898           Real y = bez.get_other_coordinate (X_AXIS, x);
899           if (!edge)
900             {
901               Real head_dy = (y - infos[j].head_);
902               if (state.dir_ * head_dy < 0)
903                 {
904                   demerit += state.parameters_.head_encompass_penalty_;
905                   convex_head_distances.push (0.0);
906                 }
907               else
908                 {
909                   Real hd = (head_dy)
910                     ? (1 / fabs (head_dy) - 1 / state.parameters_.free_head_distance_)
911                     : state.parameters_.head_encompass_penalty_;
912                   hd = (hd >? 0)<? state.parameters_.head_encompass_penalty_;
913
914                   demerit += hd;
915                 }
916
917               Real line_y = linear_interpolate (x,
918                                                 configuration.attachment_[RIGHT][X_AXIS],
919                                                 configuration.attachment_[LEFT][X_AXIS],
920                                                 configuration.attachment_[RIGHT][Y_AXIS],
921                                                 configuration.attachment_[LEFT][Y_AXIS]);
922
923               if ( 1 ) // state.dir_ * infos[j].get_point (state.dir_) > state.dir_ *line_y )
924                 {
925                 
926                   Real closest =
927                     state.dir_ * (state.dir_ * infos[j].get_point (state.dir_)
928                            >? state.dir_ *line_y
929                            );
930                   Real d = fabs (closest - y);
931         
932                   convex_head_distances.push (d);
933                 }
934             }
935         
936         
937
938           if (state.dir_ * (y - infos[j].stem_) < 0)
939             {
940               Real stem_dem =state.parameters_.stem_encompass_penalty_ ;
941               if ((l_edge && state.dir_ == UP)
942                   || (r_edge && state.dir_ == DOWN))
943                 stem_dem /= 5;
944
945               demerit +=  stem_dem;
946             }
947           else if (!edge)
948             {
949               Interval ext;
950               ext.add_point (infos[j].stem_);
951               ext.add_point (infos[j].head_);
952
953               // ?
954               demerit += -state.parameters_.closeness_factor_
955                 * (state.dir_
956                    * (y - (ext[state.dir_] + state.dir_ * state.parameters_.free_head_distance_))
957                    <? 0)
958                 / infos.size ();
959             }
960         }
961
962       Real variance_penalty = 0.0;
963
964       if (convex_head_distances.size ())
965         {
966           Real avg_distance = 0.0;
967           Real min_dist = infinity_f;
968           for (int j = 0; j < convex_head_distances.size (); j++)
969             {
970               min_dist = min_dist <? convex_head_distances[j];
971               avg_distance += convex_head_distances[j];
972             }
973
974           /*
975             For slurs over 3 or 4 heads, the average distance is not a
976             good normalizer.
977            */
978           int n =  convex_head_distances.size ();
979           if (convex_head_distances.size () <= 2)
980             {
981               //              Real min_edge_dist = 1e6;
982               for (int j = 0; j < edge_distances.size (); j++)
983                 {
984                   avg_distance += edge_distances[j];
985                   n++;
986                 }
987
988             }
989
990           /*
991             TODO: maybe it's better to use (avgdist - mindist)*factor
992             as penalty.
993            */
994           avg_distance /= n;
995           variance_penalty = state.parameters_.head_slur_distance_max_ratio_;
996           if (min_dist > 0.0)
997             variance_penalty = ((avg_distance / (min_dist  +state.parameters_.free_head_distance_)) - 1.0)
998               <? variance_penalty;
999
1000           variance_penalty *= state.parameters_.head_slur_distance_factor_;
1001         }
1002 #if DEBUG_SLUR_QUANTING
1003  (*state.scores_)[i].score_card_ += to_string ("C%.2f", demerit);
1004  (*state.scores_)[i].score_card_ += to_string ("D%.2f", variance_penalty);
1005 #endif
1006
1007  (*state.scores_)[i].score_ += demerit + variance_penalty;
1008     }
1009 }
1010 void
1011 score_extra_encompass (Slur_score_state const &state)
1012 {
1013   Link_array<Grob> encompasses
1014     = Pointer_group_interface__extract_grobs (state.slur_, (Grob *)0,
1015                                               "encompass-objects");
1016   Array<Extra_collision_info> collision_infos;
1017   for (int i = encompasses.size (); i--; )
1018     {
1019       if (Slur::has_interface (encompasses[i]))
1020         {
1021           Spanner * small_slur = dynamic_cast<Spanner*> (encompasses[i]);
1022           Bezier b = Slur::get_curve (small_slur);
1023
1024           Offset relative (small_slur->relative_coordinate (state.common_[X_AXIS], X_AXIS),
1025                            small_slur->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
1026
1027           for (int k = 0; k < 3; k++)
1028           {
1029             Direction hdir =  Direction (k /2 - 1);
1030
1031             /*
1032               Only take bound into account if small slur starts
1033               together with big slur.
1034              */
1035             if (hdir && small_slur->get_bound (hdir) != state.slur_->get_bound (hdir))
1036               continue;
1037         
1038
1039             Offset z = b.curve_point ( k / 2.0);
1040             z += relative;
1041
1042             Interval yext;
1043             yext.set_full ();
1044             yext[state.dir_] = z[Y_AXIS] + state.dir_ * state.thickness_ * 1.0;
1045
1046             Interval xext (-1, 1);
1047             xext = xext * (state.thickness_*2) + z[X_AXIS];
1048             Extra_collision_info info (small_slur,
1049                                        k - 1.0,
1050                                        xext,
1051                                        yext,
1052                                        state.parameters_.extra_object_collision_);
1053             collision_infos.push (info);
1054           }
1055         }
1056       else
1057         {
1058           Grob *g = encompasses [i];
1059           Interval xe = g->extent (state.common_[X_AXIS], X_AXIS);
1060           Interval ye = g->extent (state.common_[Y_AXIS], Y_AXIS);
1061
1062           Real xp = 0.0;
1063           Real penalty = state.parameters_.extra_object_collision_;
1064           if (Accidental_interface::has_interface (g))
1065             {
1066               penalty = state.parameters_.accidental_collision_;
1067               /* Begin copy accidental.cc */
1068               bool parens = false;
1069               if (to_boolean (g->get_property ("cautionary")))
1070                 {
1071                   SCM cstyle = g->get_property ("cautionary-style");
1072                   parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
1073                 }
1074         
1075               SCM accs = g->get_property ("accidentals");
1076               SCM scm_style = g->get_property ("style");
1077               if (!scm_is_symbol (scm_style)
1078                   && !parens
1079                   && scm_ilength (accs) == 1)
1080                 {
1081                   /* End copy accidental.cc */
1082                   switch (scm_to_int (ly_car (accs)))
1083                     {
1084                     case FLAT:
1085                     case DOUBLE_FLAT:
1086                       xp = LEFT;
1087                       break ;
1088                     case SHARP:
1089                       xp = 0.5 * state.dir_;
1090                       break ;
1091                     case NATURAL:
1092                       xp = -state.dir_;
1093                       break;
1094                     }
1095                 }
1096             }
1097
1098           ye.widen (state.thickness_ * 0.5);
1099           xe.widen (state.thickness_ * 1.0);
1100           Extra_collision_info info (g, xp, xe, ye,  penalty);
1101           collision_infos.push (info);
1102         }
1103     }
1104   for (int i = 0; i < state.scores_->size (); i++)
1105     {
1106       Real demerit = 0.0;
1107       for (int j = 0; j < collision_infos.size (); j++)
1108         {
1109           Drul_array<Offset> attachment = state.scores_->elem (i).attachment_;
1110           Interval slur_wid (attachment[LEFT][X_AXIS], attachment[RIGHT][X_AXIS]);
1111
1112           /*
1113             to prevent numerical inaccuracies in
1114             Bezier::get_other_coordinate ().
1115           */
1116           Direction d = LEFT;
1117           bool found = false;
1118           Real y = 0.0;
1119         
1120           do
1121             {
1122               /*
1123                 We need to check for the bound explicitly, since the
1124                 slur-ending can be almost vertical, making the Y
1125                 coordinate a bad approximation of the object-slur
1126                 distance.               
1127                */
1128               Item * as_item =  dynamic_cast<Item*> (collision_infos[j].grob_);
1129               if ((as_item
1130                    && as_item->get_column ()
1131                    == state.extremes_[d] .bound_->get_column ())
1132                   || collision_infos[j].extents_[X_AXIS].contains (attachment[d][X_AXIS]))
1133                 {
1134                   y = attachment[d][Y_AXIS];
1135                   found = true;
1136                 }
1137             }
1138           while (flip (&d) != LEFT);
1139
1140           if (!found)
1141             {
1142               Real x = collision_infos[j].extents_[X_AXIS]
1143                 .linear_combination (collision_infos[j].idx_);
1144
1145               if (!slur_wid.contains (x))
1146                 continue;
1147         
1148               y = state.scores_->elem (i).curve_.get_other_coordinate (X_AXIS, x);
1149             }
1150
1151           Real dist = collision_infos[j].extents_[Y_AXIS].distance (y);
1152           demerit +=
1153             fabs (0 >? (state.parameters_.extra_encompass_free_distance_ - dist)) /
1154             state.parameters_.extra_encompass_free_distance_
1155             * collision_infos[j].penalty_;
1156         }
1157 #if DEBUG_SLUR_QUANTING
1158  (*state.scores_)[i].score_card_ += to_string ("X%.2f", demerit);
1159 #endif
1160  (*state.scores_)[i].score_ += demerit;
1161     }
1162 }
1163
1164 void
1165 score_edges (Slur_score_state const &state)
1166 {
1167   for (int i = 0; i < state.scores_->size (); i++)
1168     {
1169       Direction d = LEFT;
1170       Slur_score &config = state.scores_->elem_ref (i);
1171       Offset dz = config.attachment_[RIGHT] - config.attachment_[LEFT];
1172       Real slope = dz[Y_AXIS] / dz[X_AXIS];
1173       do
1174         {
1175           Real y = config.attachment_[d][Y_AXIS];
1176           Real dy = fabs (y - state.base_attachments_[d][Y_AXIS]);
1177         
1178           Real factor = state.parameters_.edge_attraction_factor_;
1179           Real demerit = factor * dy;
1180           if (state.extremes_[d].stem_
1181               && state.extremes_[d].stem_dir_ == state.dir_
1182               && !Stem::get_beaming (state.extremes_[d].stem_, -d)
1183               )
1184             demerit /= 5;
1185
1186           demerit *= exp (state.dir_ * d * slope
1187                           * state.parameters_.edge_slope_exponent_ );
1188         
1189          (*state.scores_)[i].score_ += demerit;
1190 #if DEBUG_SLUR_QUANTING
1191          (*state.scores_)[i].score_card_ += to_string ("E%.2f", demerit);
1192 #endif
1193         }
1194       while (flip (&d) != LEFT);
1195     }
1196 }
1197
1198 void
1199 score_slopes (Slur_score_state const &state)
1200 {
1201
1202   Drul_array<Real> ys;
1203   Direction d = LEFT;
1204   do
1205     {
1206       if (state.extremes_[d].slur_head_)
1207         ys[d] = state.extremes_[d].slur_head_->relative_coordinate (state.common_[Y_AXIS],
1208                                                               Y_AXIS);
1209       else
1210         ys[d] = state.extremes_[d].neighbor_y_;
1211     }
1212   while (flip (&d) != LEFT);
1213
1214   bool has_beams
1215     = (state.extremes_[LEFT].stem_ && Stem::get_beam (state.extremes_[LEFT].stem_))
1216     || (state.extremes_[RIGHT].stem_ && Stem::get_beam (state.extremes_[RIGHT].stem_));
1217
1218   Real dy = ys[RIGHT] - ys[LEFT];
1219   for (int i = 0; i < state.scores_->size (); i++)
1220     {
1221       Offset slur_dz = (*state.scores_)[i].attachment_[RIGHT]
1222         - (*state.scores_)[i].attachment_[LEFT];
1223       Real slur_dy = slur_dz[Y_AXIS];
1224       Real demerit = 0.0;
1225
1226       demerit += ((fabs (slur_dy / slur_dz[X_AXIS])
1227                    - state.parameters_.max_slope_) >? 0)
1228         * state.parameters_.max_slope_factor_;
1229
1230       /* 0.2: account for staffline offset. */
1231       Real max_dy = (fabs (dy) + 0.2);
1232       if (has_beams)
1233         max_dy += 1.0;
1234
1235       demerit += state.parameters_.steeper_slope_factor_
1236         * ((fabs (slur_dy) -max_dy) >? 0);
1237
1238       demerit += ((fabs (slur_dy/slur_dz[X_AXIS])
1239                    - state.parameters_.max_slope_) >? 0)
1240         * state.parameters_.max_slope_factor_;
1241
1242       if (sign (dy) == 0
1243           && sign (slur_dy) != 0)
1244         demerit += state.parameters_.non_horizontal_penalty_;
1245
1246       if (sign (dy)
1247           && sign (slur_dy)
1248           && sign (slur_dy) != sign (dy))
1249         demerit += has_beams
1250           ? state.parameters_.same_slope_penalty_ / 10
1251           : state.parameters_.same_slope_penalty_;
1252
1253 #if DEBUG_SLUR_QUANTING
1254       (*state.scores_)[i].score_card_ += to_string ("S%.2f", d);
1255 #endif
1256       (*state.scores_)[i].score_ += demerit;
1257     }
1258
1259
1260
1261 }
1262
1263
1264 Real
1265 fit_factor (Offset dz_unit, Offset dz_perp,
1266             Bezier curve, Direction d,  Array<Offset> const &avoid)
1267 {
1268   Real fit_factor = 0.0;
1269   Offset x0 = curve.control_[0];
1270   curve.translate (-x0);
1271   curve.rotate (-dz_unit.arg ());
1272   curve.scale (1, d);
1273
1274   Interval curve_xext;
1275   curve_xext.add_point (curve.control_[0][X_AXIS]);
1276   curve_xext.add_point (curve.control_[3][X_AXIS]);
1277
1278   for (int i = 0; i < avoid.size (); i++)
1279     {
1280       Offset z = (avoid[i] - x0) ;
1281       Offset p (dot_product (z, dz_unit),
1282                 d* dot_product (z, dz_perp));
1283       if (!curve_xext.contains (p[X_AXIS]))
1284         continue;
1285
1286       Real y = curve.get_other_coordinate (X_AXIS, p[X_AXIS]);
1287       if (y)
1288         {
1289           fit_factor = fit_factor >? (p[Y_AXIS] / y);
1290         }
1291     }
1292   return fit_factor;
1293 }
1294         
1295
1296 Bezier
1297 get_bezier (Slur_score_state const &state,
1298             Drul_array<Offset> attachments,
1299             Real r_0, Real h_inf
1300             )
1301 {
1302   Link_array<Grob> encompasses = state.columns_;
1303
1304   Array<Offset> avoid;
1305   for (int i = 0; i < encompasses.size (); i++)
1306     {
1307       if (state.extremes_[LEFT].note_column_ == encompasses[i]
1308           || state.extremes_[RIGHT].note_column_ == encompasses[i])
1309         continue;
1310
1311       Encompass_info inf (get_encompass_info (state, encompasses[i]));
1312
1313       Real y = state.dir_ * ((state.dir_ * inf.head_) >? (state.dir_ *inf.stem_));
1314
1315       avoid.push (Offset (inf.x_,  y + state.dir_ * state.parameters_.free_head_distance_));
1316     }
1317
1318   Link_array<Grob> extra_encompasses
1319     = Pointer_group_interface__extract_grobs (state.slur_, (Grob *)0, "encompass-objects");
1320   for (int i = 0;  i < extra_encompasses.size (); i++)
1321     if (Slur::has_interface (extra_encompasses[i]))
1322       {
1323         Grob * small_slur = extra_encompasses[i];
1324         Bezier b = Slur::get_curve (small_slur);
1325
1326         Offset z = b.curve_point (0.5);
1327         z += Offset (small_slur->relative_coordinate (state.common_[X_AXIS], X_AXIS),
1328                      small_slur->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
1329
1330         z[Y_AXIS] += state.dir_ * state.parameters_.free_slur_distance_;
1331         avoid.push (z);
1332       }
1333
1334   Offset dz = attachments[RIGHT]- attachments[LEFT];;
1335   Offset dz_unit = dz;
1336   dz_unit *= 1 / dz.length ();
1337   Offset dz_perp = dz_unit * Offset (0, 1);
1338
1339   Real indent, height;
1340   get_slur_indent_height (&indent, &height, dz.length (), h_inf, r_0);
1341
1342   Real excentricity = robust_scm2double (state.slur_->get_property ("excentricity"), 0);
1343   Bezier curve;
1344
1345   Real x1 = (excentricity + indent);
1346   Real x2 = (excentricity - indent);
1347   curve.control_[0] = attachments[LEFT];
1348   curve.control_[1] = attachments[LEFT] + dz_perp * height * state.dir_ + dz_unit * x1;
1349   curve.control_[2] = attachments[RIGHT] + dz_perp * height * state.dir_
1350     + dz_unit * x2;
1351   curve.control_[3] = attachments[RIGHT];
1352
1353   Real ff = fit_factor (dz_unit, dz_perp, curve, state.dir_, avoid);
1354   Real len = dz.length ();
1355
1356   /* This condition,
1357
1358      len^2 > 4h^2 +  3 (i 1/3len)^2  - 1/3 len^2
1359
1360      is equivalent to:
1361
1362      |bez' (0)| < | bez' (.5)|
1363
1364      when (control2 - control1) has the same direction as
1365     (control3 - control0).  */
1366
1367   Real a1 = sqr (len) / 3.0;
1368   Real a2 = 0.75 * sqr (indent + len / 3.0);
1369   Real max_h;
1370   if (a1 >= a2)
1371     max_h = sqrt (a1 - a2);
1372   else
1373     {
1374       programming_error ("FIXME: max_h is broken; setting to length / 3");
1375       max_h = len / 3.0;
1376     }
1377   height = height >? ((height * ff) <? max_h);
1378
1379   curve.control_[0] = attachments[LEFT];
1380   curve.control_[1] = attachments[LEFT] + dz_perp * height * state.dir_ + dz_unit * x1;
1381   curve.control_[2] = attachments[RIGHT] + dz_perp * height * state.dir_ + dz_unit * x2;
1382   curve.control_[3] = attachments[RIGHT];
1383
1384   return curve;
1385 }