]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer-scheme.cc
Web-ja: update introduction
[lilypond.git] / lily / simple-spacer-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include <cstdio>
21
22 #include "paper-column.hh"
23 #include "spring.hh"
24 #include "warn.hh"
25 #include "simple-spacer.hh"
26
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"
35            "\n"
36            "@var{length} is a number, @var{ragged} a boolean.\n"
37            "\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.")
42 {
43   int len = scm_ilength (springs);
44   if (len == 0)
45     return scm_list_2 (scm_from_double (0.0), scm_from_double (0.0));
46
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);
50
51   bool is_ragged = to_boolean (ragged);
52   Simple_spacer spacer;
53   for (SCM s = springs; scm_is_pair (s); s = scm_cdr (s))
54     {
55       Real ideal = scm_to_double (scm_caar (s));
56       Real inv_hooke = scm_to_double (scm_cadar (s));
57
58       Spring sp (ideal, 0.0);
59       sp.set_inverse_compress_strength (inv_hooke);
60       sp.set_inverse_stretch_strength (inv_hooke);
61
62       spacer.add_spring (sp);
63     }
64
65   for (SCM s = rods; scm_is_pair (s); s = scm_cdr (s))
66     {
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);
71
72       Real distance = scm_to_double (scm_car (entry));
73       spacer.add_rod (l, r, distance);
74     }
75
76   spacer.solve (scm_to_double (length), is_ragged);
77
78   vector<Real> posns = spacer.spring_positions ();
79
80   SCM force_return = spacer.fits () ? scm_from_double (spacer.force ()) : SCM_BOOL_F;
81
82   SCM retval = SCM_EOL;
83   for (vsize i = posns.size (); i--;)
84     retval = scm_cons (scm_from_double (posns[i]), retval);
85
86   retval = scm_cons (force_return, retval);
87   return retval;
88 }