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