2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "paper-column.hh"
25 #include "simple-spacer.hh"
27 LY_DEFINE (ly_solve_spring_rod_problem
, "ly:solve-spring-rod-problem",
28 4, 1, 0, (SCM springs
, SCM rods
, SCM length
, SCM ragged
),
29 "Solve a spring and rod problem for @var{count} objects, that"
30 " are connected by @var{count}-1 @var{springs}, and an arbitrary"
31 " number of @var{rods}. @var{count} is implicitly given by"
32 " @var{springs} and @var{rods}. The @var{springs} argument has"
33 " the format @code{(ideal, inverse_hook)} and @var{rods} is of"
34 " the form @code{(idx1, idx2, distance)}.\n"
36 "@var{length} is a number, @var{ragged} a boolean.\n"
38 "The function returns a list containing the force (positive for"
39 " stretching, negative for compressing and @code{#f} for"
40 " non-satisfied constraints) followed by @var{spring-count}+1"
41 " positions of the objects.")
43 int len
= scm_ilength (springs
);
45 return scm_list_2 (scm_from_double (0.0), scm_from_double (0.0));
47 SCM_ASSERT_TYPE (len
>= 0, springs
, SCM_ARG1
, __FUNCTION__
, "list of springs");
48 SCM_ASSERT_TYPE (scm_ilength (rods
) > 0, rods
, SCM_ARG1
, __FUNCTION__
, "list of rods");
49 LY_ASSERT_TYPE (scm_is_number
, length
, 3);
51 bool is_ragged
= ragged
== SCM_BOOL_T
;
53 for (SCM s
= springs
; scm_is_pair (s
); s
= scm_cdr (s
))
55 Real ideal
= scm_to_double (scm_caar (s
));
56 Real inv_hooke
= scm_to_double (scm_cadar (s
));
58 Spring
sp (ideal
, 0.0);
59 sp
.set_inverse_compress_strength (inv_hooke
);
60 sp
.set_inverse_stretch_strength (inv_hooke
);
62 spacer
.add_spring (sp
);
65 for (SCM s
= rods
; scm_is_pair (s
); s
= scm_cdr (s
))
67 SCM entry
= scm_car (s
);
68 int l
= scm_to_int (scm_car (entry
));
69 int r
= scm_to_int (scm_cadr (entry
));
70 entry
= scm_cddr (entry
);
72 Real distance
= scm_to_double (scm_car (entry
));
73 spacer
.add_rod (l
, r
, distance
);
76 spacer
.solve (scm_to_double (length
), is_ragged
);
78 vector
<Real
> posns
= spacer
.spring_positions ();
80 SCM force_return
= spacer
.fits () ? scm_from_double (spacer
.force ()) : SCM_BOOL_F
;
83 for (vsize i
= posns
.size (); i
--;)
84 retval
= scm_cons (scm_from_double (posns
[i
]), retval
);
86 retval
= scm_cons (force_return
, retval
);