]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer-scheme.cc
replace SCM_ASSERT_TYPE with LY_ASSERT_TYPE and friends
[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} springs, and an arbitrary number of rods "
20            "Springs have the format (ideal, hooke) and rods (idx1, idx2, distance) "
21            "@var{length} is a number, @var{ragged} a boolean "
22            "Return: a list containing the force (positive for stretching, "
23            "negative for compressing and #f for non-satisfied constraints) "
24            "followed by the @var{spring-count}+1 positions of the objects. ")
25 {
26   LY_FUNC_NOTE_FIRST_ARG(springs);
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_ARG1, __FUNCTION__, "list of rods");
34   LY_ASSERT_TYPE(scm_is_number, 3);
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.solve (scm_to_double (length), is_ragged);
58
59   vector<Real> posns = spacer.spring_positions ();
60
61   SCM force_return = spacer.fits () ? scm_from_double (spacer.force ()) : SCM_BOOL_F;
62
63   SCM retval = SCM_EOL;
64   for (vsize i = posns.size (); i--;)
65     retval = scm_cons (scm_from_double (posns[i]), retval);
66
67   retval = scm_cons (force_return, retval);
68   return retval;
69 }