]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer-scheme.cc
48f127bfca5ae710b5778c73b343e6d08e994839
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <cstdio>
10
11 #include "paper-column.hh"
12 #include "spring.hh"
13 #include "warn.hh"
14 #include "simple-spacer.hh"
15
16 LY_DEFINE (ly_solve_spring_rod_problem, "ly:solve-spring-rod-problem",
17            4, 1, 0, (SCM springs, SCM rods, SCM length, SCM ragged),
18            "Solve a spring and rod problem for @var{count} objects, that"
19            " are connected by @var{count}-1 @var{springs}, and an arbitrary"
20            " number of @var{rods}.  @var{count} is implicitly given by"
21            " @var{springs} and @var{rods}.  The @var{springs} argument has"
22            " the format @code{(ideal, inverse_hook)} and @var{rods} is of"
23            " the form @code{(idx1, idx2, distance)}.\n"
24            "\n"
25            "@var{length} is a number, @var{ragged} a boolean.\n"
26            "\n"
27            "The function returns a list containing the force (positive for"
28            " stretching, negative for compressing and @code{#f} for"
29            " non-satisfied constraints) followed by @var{spring-count}+1"
30            " positions of the objects.")
31 {
32   int len = scm_ilength (springs);
33   if (len == 0)
34     return scm_list_2 (scm_from_double (0.0), scm_from_double (0.0));
35
36   SCM_ASSERT_TYPE (len >= 0, springs, SCM_ARG1, __FUNCTION__, "list of springs");
37   SCM_ASSERT_TYPE (scm_ilength (rods)  > 0, rods, SCM_ARG1, __FUNCTION__, "list of rods");
38   LY_ASSERT_TYPE (scm_is_number, length, 3);
39
40   bool is_ragged = ragged == SCM_BOOL_T;
41   Simple_spacer spacer;
42   for (SCM s = springs; scm_is_pair (s); s = scm_cdr (s))
43     {
44       Real ideal = scm_to_double (scm_caar (s));
45       Real inv_hooke = scm_to_double (scm_cadar (s));
46
47       Spring sp (ideal, 0.0);
48       sp.set_inverse_compress_strength (inv_hooke);
49       sp.set_inverse_stretch_strength (inv_hooke);
50
51       spacer.add_spring (sp);
52     }
53
54   for (SCM s = rods; scm_is_pair (s); s = scm_cdr (s))
55     {
56       SCM entry = scm_car (s);
57       int l = scm_to_int (scm_car (entry));
58       int r = scm_to_int (scm_cadr (entry));
59       entry = scm_cddr (entry);
60
61       Real distance = scm_to_double (scm_car (entry));
62       spacer.add_rod (l, r, distance);
63     }
64
65   spacer.solve (scm_to_double (length), is_ragged);
66
67   vector<Real> posns = spacer.spring_positions ();
68
69   SCM force_return = spacer.fits () ? scm_from_double (spacer.force ()) : SCM_BOOL_F;
70
71   SCM retval = SCM_EOL;
72   for (vsize i = posns.size (); i--;)
73     retval = scm_cons (scm_from_double (posns[i]), retval);
74
75   retval = scm_cons (force_return, retval);
76   return retval;
77 }