]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer-scheme.cc
* lily/tie-column.cc (set_manual_tie_configuration): new function.
[lilypond.git] / lily / simple-spacer-scheme.cc
1 /*
2   simple-spacer-scheme.cc -- implement Simple_spacer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <cstdio>
10 #include <cmath>
11 using namespace std;
12
13 #include "paper-column.hh"
14 #include "spring.hh"
15 #include "warn.hh"
16 #include "simple-spacer.hh"
17
18 LY_DEFINE (ly_solve_spring_rod_problem, "ly:solve-spring-rod-problem",
19            4, 1, 0, (SCM springs, SCM rods, SCM length, SCM ragged),
20            "Solve a spring and rod problem for @var{count} objects, that "
21            "are connected by @var{count-1} springs, and an arbitrary number of rods "
22            "Springs have the format (ideal, hooke) and rods (idx1, idx2, distance) "
23            "@var{length} is a number, @var{ragged} a boolean "
24            "Return: a list containing the force (positive for stretching, "
25            "negative for compressing and #f for non-satisfied constraints) "
26            "followed by the @var{spring-count}+1 positions of the objects. ")
27 {
28   int len = scm_ilength (springs);
29   if (len == 0)
30     return scm_list_2 (scm_from_double (0.0), scm_from_double (0.0));
31
32   SCM_ASSERT_TYPE (len >= 0, springs, SCM_ARG1, __FUNCTION__, "list of springs");
33   SCM_ASSERT_TYPE (scm_ilength (rods) >= 0, rods, SCM_ARG2, __FUNCTION__, "list of rods");
34   SCM_ASSERT_TYPE (scm_is_number (length) || length == SCM_BOOL_F,
35                    length, SCM_ARG3, __FUNCTION__, "number or #f");
36
37   bool is_ragged = ragged == SCM_BOOL_T;
38   Simple_spacer spacer;
39   for (SCM s = springs; scm_is_pair (s); s = scm_cdr (s))
40     {
41       Real ideal = scm_to_double (scm_caar (s));
42       Real hooke = scm_to_double (scm_cadar (s));
43
44       spacer.add_spring (ideal, 1 / hooke);
45     }
46
47   for (SCM s = rods; scm_is_pair (s); s = scm_cdr (s))
48     {
49       SCM entry = scm_car (s);
50       int l = scm_to_int (scm_car (entry));
51       int r = scm_to_int (scm_cadr (entry));
52       entry = scm_cddr (entry);
53
54       Real distance = scm_to_double (scm_car (entry));
55       spacer.add_rod (l, r, distance);
56     }
57
58   spacer.line_len_ = scm_to_double (length);
59
60   if (is_ragged)
61     spacer.my_solve_natural_len ();
62   else
63     spacer.my_solve_linelen ();
64
65   Array<Real> posns;
66   posns.push (0.0);
67   for (int i = 0; i < spacer.springs_.size (); i++)
68     {
69       Real l = spacer.springs_[i].length ((is_ragged) ? 0.0 : spacer.force_);
70       posns.push (posns.top () + l);
71     }
72
73   SCM force_return = SCM_BOOL_F;
74   if (!isinf (spacer.force_)
75       && (spacer.is_active () || is_ragged))
76     force_return = scm_from_double (spacer.force_);
77
78   if (is_ragged
79       && posns.top () > spacer.line_len_)
80     force_return = SCM_BOOL_F;
81
82   SCM retval = SCM_EOL;
83   for (int i = posns.size (); i--;)
84     retval = scm_cons (scm_from_double (posns[i]), retval);
85
86   retval = scm_cons (force_return, retval);
87   return retval;
88 }