Actually compile the Future work chapter.
[matthijs/master-project/report.git] / Utils / Metapost.tex
1 % Some setup for metapost
2 \startMPinclusions 
3
4 % Use metaobj for drawing objects
5 input metaobj; 
6
7 % Set some defaults for pretty pictures
8 setObjectDefaultOption("Box")("framewidth")(.75mm);
9 setObjectDefaultOption("Circle")("framewidth")(.75mm);
10 setObjectDefaultOption("Circle")("circmargin")(3mm);
11 setObjectDefaultOption("Mux")("framewidth")(.75mm);
12 %ahlength := 1.5mm;
13 ahangle := 60;
14
15 setCurveDefaultOption("linewidth",.75mm);
16 % Add a nodesep that's equal to the linewidth, since the linewidth isn't
17 % included in the bounding box for line cutoff.
18 setCurveDefaultOption("nodesep",curve_linewidth_default);
19
20
21 % Define a register.
22 % |v| is either a picture, a string or an object given by its number which
23 % will be contained inside the register. Supports the same options as Box,
24 % plus:
25 %   reflect    Reflect the register vertically, so input is right and output
26 %              is left. (default: false)
27 %   labels     Show labels for the input and output ports. (default: false)
28 vardef newReg@#(expr v) text options=
29   ExecuteOptions(@#)(options);
30   assignObj(@#,"Reg");
31   StandardInterface;
32   
33   % We contain a box (that contains the passed object in turn)
34   SubObject(box,obj(newobjstring_));
35   newBox.obj(@#box)(v) options;
36  
37   % Some points for ports
38   ObjPoint ck,d,out;
39
40   ObjCode StandardEquations,
41     % Align our bounding box with the enclosed box
42     "obj(@#box).c = @#c",
43     "obj(@#box).se = @#se",
44     "obj(@#box).nw = @#nw",
45     % Put the clock, data and output ports at the right spots
46     if OptionValue@#("reflect"):
47       "xpart @#out = xpart @#w",
48       "xpart @#d = xpart @#ck = xpart @#e",
49     else:
50       "xpart @#out = xpart @#e",
51       "xpart @#d = xpart @#ck = xpart @#w",
52     fi
53     "ypart @#out = ypart midpoint(@#n, @#s)",
54     "ypart @#d = ypart (@#sw * .25 + @#nw * .75)",
55     "ypart @#ck = ypart (@#sw * .75 + @#nw * .25)";
56
57   StandardTies;
58 enddef;
59
60 % Draw a register
61 def drawReg(suffix reg)=
62   save clkwh, refl;
63   % Draw the box
64   drawObj(obj(reg.box));
65
66   % Fill refl with 1 or -1, to invert coordinates when reflected.
67   if OptionValue.reg("reflect"):
68     refl = -1;
69   else:
70     refl = 1;
71   fi;
72
73   % Calculate (half of) the height and width of the triangle
74   clkwh = ypart((reg.n - reg.s)) * .1;
75   % Draw the "clock" triangle
76   draw ((0, clkwh) -- (refl * clkwh, 0) -- (0, -clkwh)) shifted reg.ck;
77
78   % Draw input and output labels
79   if OptionValue.reg("labels"):
80     if OptionValue.reg("reflect"):
81       label.lft(\sometxt{D}, reg.d);
82       label.rt(\sometxt{O}, reg.out);
83     else:
84       label.rt(\sometxt{D}, reg.d);
85       label.lft(\sometxt{O}, reg.out);
86     fi;
87   fi
88 enddef;
89
90 % Bounding path for a register
91 def BpathReg(suffix n)=BpathObj(obj(n.box)) enddef;
92
93 % Define options
94 define_global_boolean_option("reflect");
95 define_global_boolean_option("labels");
96 setObjectDefaultOption("Reg")("reflect")(false);
97 setObjectDefaultOption("Reg")("labels")(false);
98
99 % Define a Multiplexer.
100 vardef newMux@# text options=
101   ExecuteOptions(@#)(options);
102   assignObj(@#,"Mux");
103   StandardInterface;
104   % Add ports
105   ObjPoint inpa, inpb, out, sel;
106  
107   ObjCode MinimumStandardEquations,
108     % Make it a parallel trapezium
109     "xpart(@#ne - @#nw) = xpart(@#se - @#sw)",
110     "ypart(@#ne - @#nw) = -1 * ypart(@#se - @#sw)",
111     % Use a 22.5 degree angle for the sides
112     "ypart(@#nw) - ypart(@#ne) = xpart(@#ne-@#nw) / 2",
113     % With a specific width and height
114     "@#e-@#w = (" & decimal OptionValue@#("width") & ", 0mm)",
115     "@#n-@#s = (0mm, " & decimal OptionValue@#("height") & ")",
116     % And space the input ports evenly
117     "@#inpa = midpoint(@#nw, @#w)",
118     "@#inpb = midpoint(@#w, @#sw)",
119     "@#out = @#e", 
120     "@#sel = @#n"; 
121
122   StandardTies;
123 enddef;
124
125 % Draw a multiplexer
126 def drawMux(suffix mux)=
127   draw mux.n -- mux.s;
128   % Use our bounding path to draw
129   drawFramedOrFilledObject_(mux);
130 enddef;
131
132 % Bounding path for a multiplexer
133 def BpathMux(suffix n)=StandardBpath(n) enddef;
134
135 % Define options
136 define_local_numeric_option("width");
137 define_local_numeric_option("height");
138 setObjectDefaultOption("Mux")("width")(5mm);
139 setObjectDefaultOption("Mux")("height")(15mm);
140 setObjectDefaultOption("Mux")("framed")(true);
141 % Overidden above
142 %setObjectDefaultOption("Mux")("framewidth")(.5bp);
143 setObjectDefaultOption("Mux")("framecolor")(black);
144 setObjectDefaultOption("Mux")("framestyle")("");
145 setObjectDefaultOption("Mux")("shadow")(false);
146 setObjectDefaultOption("Mux")("shadowcolor")(black);
147 setObjectDefaultOption("Mux")("filled")(false);
148 setObjectDefaultOption("Mux")("fillcolor")(black);
149
150
151 def midpoint(expr a, b) = ((a + b) / 2) enddef;
152
153 \stopMPinclusions
154
155 % Make \overrightarrow "unexpanded", to make it work within metapost.
156 % http://www.ntg.nl/pipermail/ntg-context/2009/043620.html. Will be fixed in
157 % context.
158 \let\normaloverrightarrow\overrightarrow                                                                                                                     
159 \unexpanded\def\overrightarrow{\normaloverrightarrow}