omdl  v0.9.5
OpenSCAD Mechanical Design Library
utility.scad
Go to the documentation of this file.
1 //! Miscellaneous mathematical utilities.
2 /***************************************************************************//**
3  \file
4  \author Roy Allen Sutton
5  \date 2017-2019
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \amu_define group_name (Utilities)
31  \amu_define group_brief (Miscellaneous mathematical utilities.)
32 
33  \amu_include (include/amu/pgid_path_pstem_pg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // group.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/group_in_parent_start.amu)
42  \amu_include (include/amu/includes_required.amu)
43 *******************************************************************************/
44 
45 //----------------------------------------------------------------------------//
46 
47 //! Return facets number for the given arc radius.
48 /***************************************************************************//**
49  \param r <decimal> The arc radius.
50 
51  \returns <integer> The number of facets.
52 
53  \details
54 
55  This function approximates the \c get_fragments_from_r() code that
56  is used by OpenSCAD to calculates the number of fragments in a
57  circle or arc. The arc facets are controlled by the special
58  variables \p $fa, \p $fs, and \p $fn.
59 *******************************************************************************/
60 function get_fn
61 (
62  r
63 ) = (r < grid_fine) ? 3
64  : ($fn > 0.0) ? ($fn >= 3) ? $fn : 3
65  : ceil( max( min(360/$fa, r*tau/$fs), 5 ) );
66 
67 //! Generate a histogram for the elements of a list of values.
68 /***************************************************************************//**
69  \param v <data> A list of values.
70  \param m <integer> The output mode (a 5-bit encoded integer).
71 
72  \param cs <string-list-4> A list of strings [s1, s2, s3, fs]
73  (for custom field formatting).
74 
75  \returns <list | string> with the occurrence frequency of the elements
76  of \p v.
77 
78  \details
79 
80  The custom formatting strings are inserted in the output stream as
81  follows:
82 
83  \verbatim
84  s1, value, s2, value-frequency, s3, fs
85  \endverbatim
86 
87  See strl_html() for description of the html formatting parameters
88  \p cb, \p cp, \p ca, \p cf, and \p d.
89 
90  Output mode selection:
91 
92  | bit | Description | 0 | 1 |
93  |:---:|:------------------------|:------------|:------------|
94  | 0 | output mode | numerical | string |
95  | 1-3 | string mode format | see table | see table |
96  | 4 | field separator mode | not at end | all |
97 
98  String output modes:
99 
100  | | B3 | B2 | B1 | B0 | Description |
101  |:---:|:---:|:---:|:---:|:---:|:------------------|
102  | 1 | 0 | 0 | 0 | 1 | list of strings |
103  | 3 | 0 | 0 | 1 | 1 | text format 1 |
104  | 9 | 1 | 0 | 0 | 1 | html format 1 |
105  | 15 | 1 | 1 | 1 | 1 | custom formating |
106 
107 *******************************************************************************/
108 function histogram
109 (
110  v,
111  m = 0,
112  cs,
113  cb,
114  cp,
115  ca,
116  cf,
117  d = false
118 ) = let
119  (
120  hv = [for (i=unique(v)) [i, len(find(i, v, 0))]]
121  )
122  binary_bit_is(m, 0, 0) ? hv
123  : let
124  (
125  sm = binary_iw2i(m, 1, 3),
126  fm = binary_bit_is(m, 4, 1) ? true : false
127  )
128  (sm == 0) ? [for (i=hv) str(first(i), "x", second(i))]
129  : let
130  (
131  s1 = (sm==1) ? empty_str
132  : (sm==4) ? empty_str
133  : (len(cs) > 0 ? cs[0] : empty_str),
134  s2 = (sm==1) ? "<"
135  : (sm==4) ? "x"
136  : (len(cs) > 1 ? cs[1] : empty_str),
137  s3 = (sm==1) ? ">"
138  : (sm==4) ? empty_str
139  : (len(cs) > 2 ? cs[2] : empty_str),
140  fs = (sm==1) ? " "
141  : (sm==4) ? ", "
142  : (len(cs) > 3 ? cs[3] : empty_str),
143  fb = (sm==1) ? undef
144  : (sm==4) ? undef
145  : cb,
146  fp = (sm==1) ? undef
147  : (sm==4) ? [undef, undef, ["i", "sup"], ["b", "sup"], undef]
148  : cp,
149  fa = (sm==1) ? undef
150  : (sm==4) ? [undef, undef, undef, undef, undef, ["br"]]
151  : ca,
152  ff = (sm==1) ? undef
153  : (sm==4) ? undef
154  : cf
155  )
156  strl
157  ([
158  for (i=[0:len(hv)-1])
159  strl_html
160  (
161  [s1, first(hv[i]), s2, second(hv[i]), s3, if ((i<(len(hv)-1)) || fm) fs],
162  b=fb, p=fp, a=fa, f=ff, d=d
163  ),
164  ]);
165 
166 //! @}
167 //! @}
168 
169 //----------------------------------------------------------------------------//
170 // end of file
171 //----------------------------------------------------------------------------//
tau
<decimal> The ratio of a circle's circumference to its radius.
Definition: constants.scad:201
empty_str
<string> A string with no characters (the empty string).
Definition: constants.scad:301
grid_fine
OpenSCAD fine grid limit.
Definition: constants.scad:310
function binary_iw2i(v, s, w)
Decode the binary bits of a bit window to an integer value.
function binary_bit_is(v, b, t=1)
Test if a binary bit position of an integer value equals a test bit.
function unique(v)
Return a list of the unique elements of an iterable value.
function find(mv, v, c=1, i, i1=0, i2)
Find the occurrences of a match value in an iterable value.
function second(v)
Return the second element of an iterable value.
function first(v)
Return the first element of an iterable value.
function strl(v)
Convert a list of values to a concatenated string.
function strl_html(v, b, p, a, f, d=false)
Convert a list of values to a concatenated HTML-formatted string.
function histogram(v, m=0, cs, cb, cp, ca, cf, d=false)
Generate a histogram for the elements of a list of values.
function get_fn(r)
Return facets number for the given arc radius.