initial setup of thesis repository
[cluster_expansion_thesis.git] / little_helpers / tikz / sketch-0.2.161 / Doc / sketch / A-hierarchical-model.html
blobdd76a6d4dccd1f24c31f99a81d07d37dacbd47d9
1 <html lang="en">
2 <head>
3 <title>A hierarchical model - Sketch</title>
4 <meta http-equiv="Content-Type" content="text/html">
5 <meta name="description" content="Sketch">
6 <meta name="generator" content="makeinfo 4.7">
7 <link title="Top" rel="start" href="index.html#Top">
8 <link rel="up" href="Building-a-drawing.html#Building-a-drawing" title="Building a drawing">
9 <link rel="prev" href="A-technical-drawing.html#A-technical-drawing" title="A technical drawing">
10 <link rel="next" href="Caveats.html#Caveats" title="Caveats">
11 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12 <!--
13 Copyright (C) 2005, 2006, 2007, 2008 Eugene K. Ressler.
15 This manual is for `sketch', version 0.2 (build 161),
16 Tuesday, September 08, 2009, a program that converts descriptions of simple
17 three-dimensional scenes into static drawings. This version generates
18 `PSTricks' or `PGF/TikZ' code suitable for use with the
19 TeX document processing system.
21 `Sketch' is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 3, or (at your option)
24 any later version.
26 Sketch is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with `sketch'; see the file COPYING.txt. If not, see
33 http://www.gnu.org/copyleft.-->
34 <meta http-equiv="Content-Style-Type" content="text/css">
35 <style type="text/css"><!--
36 pre.display { font-family:inherit }
37 pre.format { font-family:inherit }
38 pre.smalldisplay { font-family:inherit; font-size:smaller }
39 pre.smallformat { font-family:inherit; font-size:smaller }
40 pre.smallexample { font-size:smaller }
41 pre.smalllisp { font-size:smaller }
42 span.sc { font-variant:small-caps }
43 span.roman { font-family: serif; font-weight: normal; }
44 --></style>
45 </head>
46 <body>
47 <div class="node">
48 <p>
49 <a name="A-hierarchical-model"></a>Next:&nbsp;<a rel="next" accesskey="n" href="Caveats.html#Caveats">Caveats</a>,
50 Previous:&nbsp;<a rel="previous" accesskey="p" href="A-technical-drawing.html#A-technical-drawing">A technical drawing</a>,
51 Up:&nbsp;<a rel="up" accesskey="u" href="Building-a-drawing.html#Building-a-drawing">Building a drawing</a>
52 <hr><br>
53 </div>
55 <!-- node-name, next, previous, up -->
56 <h3 class="section">4.3 A hierarchical model</h3>
58 <p><a name="index-hierarchical-model-472"></a>While <code>sketch</code> was never meant to be a geometric modeling
59 language, it comes fairly close. The following example puts all we
60 have seen to work in a very simple model of the human hand. Start by
61 sweeping a line to make a truncated cone, which will be copied over
62 and over again to make the segments of fingers.
63 <a name="index-def-473"></a><a name="index-sweep-474"></a><a name="index-rotate-475"></a><a name="index-line-476"></a>
64 <pre class="verbatim">
65 def O (0,0,0) % origin
66 def I [1,0,0] def J [0,1,0] def K [0,0,1] % canonical unit vectors
67 def segment {
68 def n_faces 8
69 sweep { n_faces&lt;>, rotate(360 / n_faces, [J]) }
70 line(proximal_rad, 0)(distal_rad, distal_len)
72 </pre>
73 In hand anatomy, <em>distal</em> is &ldquo;at the tip&rdquo; and <em>proximal</em>
74 is &ldquo;in the area of the palm.&rdquo; We have omitted all the scalar
75 constants. You can find them in <span class="file">hand.sk</span>, which is provided
76 in the <code>sketch</code> distribution.
78 <p>We also need a prototypical sphere to use for the joints themselves.
79 <a name="index-def-477"></a><a name="index-sweep-478"></a><a name="index-rotate-479"></a>
80 <pre class="verbatim">
81 def joint_sphere {
82 def n_joint_faces 8
83 sweep [fillcolor=red] { n_joint_faces, rotate(360 / n_joint_faces, [J]) }
84 sweep { n_joint_faces, rotate(180 / n_joint_faces) }
85 (0, -joint_rad)
87 </pre>
89 <p>We'll now design the index finger (number&nbsp;<!-- /@w -->1 in our notational
90 convention; finger&nbsp;<!-- /@w -->0 is the thumb). The distal rotation for the
91 finger applies only to the tip, so we define the following.
92 <a name="index-def-480"></a><a name="index-put-481"></a><a name="index-translate-482"></a><a name="index-rotate-483"></a><a name="index-scale-484"></a>
93 <pre class="verbatim">
94 def distal_1 {
95 put { translate(joint_gap * joint_rad * [J])
96 then rotate(distal_1_rot, [I])
97 then translate((distal_len + joint_gap * joint_rad) * [J]) }
98 {segment}
99 put { rotate(distal_1_rot / 2, [I])
100 then translate((distal_len + joint_gap * joint_rad) * [J]) }
101 {joint_sphere}
102 put { scale( [J] + proximal_distal_ratio * ([I]+[K]) ) }
103 {segment}
105 </pre>
106 The identifiers here are for size and location constants. The
107 exception is <code>distal_rot_1</code>. This rotation parameter models the
108 flexing of the finger tip. The first <code>put</code> makes a copy of the
109 finger segment that is translated upward
110 <a name="index-translation-transform-485"></a><a name="index-transform_002c-translation-486"></a>just far enough to make room
111 for the spherical joint. Then it applies the distal rotation.
112 <a name="index-rotation-transform-487"></a><a name="index-transform_002c-rotation-488"></a>Finally it translates the whole assembly upward again to make room for
113 the middle phlanges (the next bone toward the palm). The second
114 <code>put</code> positions the sphere. There is a rotation to place the
115 grid on the sphere surface at an nice angle, then a translation to the
116 base of the distal phlanges, which is also center of its rotation.
117 Finally, the last <code>put</code> positions the middle segment itself.
119 <p>The middle joint is the next one down, with rotation angle
120 <code>middle_rot_1</code>. When this angle changes, we need all the objects
121 in <code>distal_1</code> to rotate as a unit.
122 <a name="index-rotation-transform-489"></a><a name="index-transform_002c-rotation-490"></a>This is the reasoning behind
123 the next definition.
124 <pre class="verbatim">
125 def finger_1 {
126 put { translate(joint_gap * joint_rad * [J])
127 then rotate(middle_1_rot, [I])
128 then translate((middle_ratio * distal_len +
129 joint_gap * joint_rad) * [J]) }
130 {distal_1}
131 put { scale(proximal_distal_ratio)
132 then rotate(middle_1_rot / 2, [I])
133 then translate((middle_ratio * distal_len +
134 joint_gap * joint_rad) * [J]) }
135 {joint_sphere}
136 put { scale( middle_ratio * [J] +
137 proximal_distal_ratio^2 * ([I]+[K]) ) }
138 {segment}
140 </pre>
141 This looks very similar to the previous definition, and it is. The
142 important difference is that rather than positioning and rotating a
143 single segment, we position and rotate the entire &ldquo;assembly&rdquo; defined
144 as <code>distal_1</code>.
145 <a name="index-rotation-transform-491"></a><a name="index-transform_002c-rotation-492"></a>The rest is just arithmetic to compute sizes and
146 positions that look nice. The last <code>put</code> places an appropriately
147 shaped segment that is the <em>proximal phlanges</em>, the bone that
148 joins the palm of the hand. This completes the finger itself.
150 <p>All the other fingers are described identically to this one. We
151 account for the fact that real fingers are different sizes in the next
152 step, which is to build the entire hand.
154 <p>The <code>hand</code> definition that follows includes a section for each
155 finger. We'll continue with finger&nbsp;<!-- /@w -->1 and omit all the others.
156 (Of note is that the thumb needs slightly special treatment&mdash;an extra
157 rotation to account for its opposing angle. This is clear in the full
158 source code.) Not surprisingly, the <code>hand</code> definition looks very
159 much like the previous two. It should be no surprise that when the
160 rotation parameter <code>meta_1_rot</code> changes, the entire finger
161 rotates!
162 <a name="index-rotation-transform-493"></a><a name="index-transform_002c-rotation-494"></a>There is an additional rotation that allows the fingers to spread
163 laterally. We say these joints of the proximal phlanges have two
164 <em>degrees of freedom</em>. The joints higher on the finger have only
165 one. Finally, each finger is scaled by a factor to lend it proportion.
166 <pre class="verbatim">
167 def hand {
168 % finger 1 [all other fingers omitted]
169 def scale_1 .85
170 put { scale(scale_1)
171 then translate((joint_gap * joint_rad) * [J])
172 then rotate(meta_1_rot, [I])
173 then rotate(-spread_rot, [K])
174 then translate((proximal_1_loc) - (O)) }
175 {finger_1}
176 put { scale(scale_1 * proximal_distal_ratio^2)
177 then rotate(meta_1_rot / 2, [I])
178 then rotate(-spread_rot, [K])
179 then translate((proximal_1_loc) - (O)) }
180 {joint_sphere}
182 % palm
183 sweep { 1, rotate(6, (0,15,0), [I]) }
184 put { rotate(-3, (0,15,0), [I]) } {
185 polygon(proximal_1_loc)(proximal_2_loc)
186 (proximal_3_loc)(proximal_4_loc)
187 (h5)(h6)(h6a)(h9)(h10)
188 polygon(h6a)(h7)(h8)(h9)
190 </pre>
191 The last section of the definition creates the polytope for the palm
192 of the hand by <code>sweep</code>ing
193 <a name="index-swept-polygon-495"></a><a name="index-polygon-sweep-496"></a>a 10-sided polygon through a very short
194 arc (9&nbsp;<!-- /@w -->degrees). This provides a wedge-shaped profile when viewed
195 from the side. The thick end of the wedge is the wrist. Because the
196 polygon is concave, it is split into into two convex shapes with nine
197 and four vertices.
199 <p>We can now have fun positioning the hand by adjusting the various
200 rotation angles. The complete source includes definitions with
201 alternatives that include the following views and more.
203 <div align="center"><img src="ex210.png" alt="ex210.png"><img src="ex220.png" alt="ex220.png"><img src="ex230.png" alt="ex230.png"><img src="ex240.png" alt="ex240.png"></div>
205 </body></html>