]> git.donarmstrong.com Git - lilypond.git/blob - lily/simple-spacer-scheme.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[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 using std::vector;
28
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"
37            "\n"
38            "@var{length} is a number, @var{ragged} a boolean.\n"
39            "\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.")
44 {
45   int len = scm_ilength (springs);
46   if (len == 0)
47     return scm_list_2 (scm_from_double (0.0), scm_from_double (0.0));
48
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);
52
53   bool is_ragged = to_boolean (ragged);
54   Simple_spacer spacer;
55   for (SCM s = springs; scm_is_pair (s); s = scm_cdr (s))
56     {
57       Real ideal = scm_to_double (scm_caar (s));
58       Real inv_hooke = scm_to_double (scm_cadar (s));
59
60       Spring sp (ideal, 0.0);
61       sp.set_inverse_compress_strength (inv_hooke);
62       sp.set_inverse_stretch_strength (inv_hooke);
63
64       spacer.add_spring (sp);
65     }
66
67   for (SCM s = rods; scm_is_pair (s); s = scm_cdr (s))
68     {
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);
73
74       Real distance = scm_to_double (scm_car (entry));
75       spacer.add_rod (l, r, distance);
76     }
77
78   spacer.solve (scm_to_double (length), is_ragged);
79
80   vector<Real> posns = spacer.spring_positions ();
81
82   SCM force_return = spacer.fits () ? scm_from_double (spacer.force ()) : SCM_BOOL_F;
83
84   SCM retval = SCM_EOL;
85   for (vsize i = posns.size (); i--;)
86     retval = scm_cons (scm_from_double (posns[i]), retval);
87
88   retval = scm_cons (force_return, retval);
89   return retval;
90 }