2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2015 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"
29 LY_DEFINE (ly_solve_spring_rod_problem, "ly:solve-spring-rod-problem",
30 4, 1, 0, (SCM springs, SCM rods, SCM length, SCM ragged),
31 "Solve a spring and rod problem for @var{count} objects, that"
32 " are connected by @var{count}-1 @var{springs}, and an arbitrary"
33 " number of @var{rods}. @var{count} is implicitly given by"
34 " @var{springs} and @var{rods}. The @var{springs} argument has"
35 " the format @code{(ideal, inverse_hook)} and @var{rods} is of"
36 " the form @code{(idx1, idx2, distance)}.\n"
38 "@var{length} is a number, @var{ragged} a boolean.\n"
40 "The function returns a list containing the force (positive for"
41 " stretching, negative for compressing and @code{#f} for"
42 " non-satisfied constraints) followed by @var{spring-count}+1"
43 " positions of the objects.")
45 int len = scm_ilength (springs);
47 return scm_list_2 (scm_from_double (0.0), scm_from_double (0.0));
49 SCM_ASSERT_TYPE (len >= 0, springs, SCM_ARG1, __FUNCTION__, "list of springs");
50 SCM_ASSERT_TYPE (scm_ilength (rods) > 0, rods, SCM_ARG1, __FUNCTION__, "list of rods");
51 LY_ASSERT_TYPE (scm_is_number, length, 3);
53 bool is_ragged = to_boolean (ragged);
55 for (SCM s = springs; scm_is_pair (s); s = scm_cdr (s))
57 Real ideal = scm_to_double (scm_caar (s));
58 Real inv_hooke = scm_to_double (scm_cadar (s));
60 Spring sp (ideal, 0.0);
61 sp.set_inverse_compress_strength (inv_hooke);
62 sp.set_inverse_stretch_strength (inv_hooke);
64 spacer.add_spring (sp);
67 for (SCM s = rods; scm_is_pair (s); s = scm_cdr (s))
69 SCM entry = scm_car (s);
70 int l = scm_to_int (scm_car (entry));
71 int r = scm_to_int (scm_cadr (entry));
72 entry = scm_cddr (entry);
74 Real distance = scm_to_double (scm_car (entry));
75 spacer.add_rod (l, r, distance);
78 spacer.solve (scm_to_double (length), is_ragged);
80 vector<Real> posns = spacer.spring_positions ();
82 SCM force_return = spacer.fits () ? scm_from_double (spacer.force ()) : SCM_BOOL_F;
85 for (vsize i = posns.size (); i--;)
86 retval = scm_cons (scm_from_double (posns[i]), retval);
88 retval = scm_cons (force_return, retval);