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