<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 13: Scissors and Glue">
<setdocpatch name="13jScissorsAndGlue" patch="13jScissorsAndGlue.maxpat"/>

<previous name="jitterchapter12">Color Lookup Tables</previous>
<next name="jitterchapter14">Matrix Positioning</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 13: Scissors and Glue</h1>
<p>In this tutorial we'll learn how to use two simple objects to slice and combine rectangular regions of two-dimensional Jitter matrices.</p>


<p>The tutorial patch shows two Jitter objects that neatly complement each other: <o>jit.scissors</o>, which cuts a matrix into equally sized smaller matrices, and <o>jit.glue</o>, which pastes multiple matrices into one matrix. We'll also take a brief look at a Max object called <o>router</o>, which lets you easily route Max messages from multiple sources to multiple destinations.</p>

<illustration><img src="images/jitterchapter13a.png"/>Read the movie</illustration>
<p>The top left of the patch is straightforward enough. The <o>loadbang</o> object automatically sends the <m>read traffic.mov</m> message to the <o>jit.movie</o> object, which then loads our movie of traffic footage.</p>

<bullet>Start the <o>metro</o> by clicking the <o>toggle</o> box at the top of the patch. You will see the traffic appear in the large <o>jit.pwindow</o> at the bottom of the patch. More interestingly, you will see the traffic image cut into quadrants, each of which appears in a separate <o>jit.pwindow</o> object off to the right side.</bullet>
<h2>Cut it Up</h2>
<p>The <o>jit.scissors</o> object is responsible for splitting the Jitter matrix containing the traffic footage into four smaller matrices:</p>


<illustration><img src="images/jitterchapter13b.png"/>The <o>jit.scissors</o> object</illustration>
<p>The <o>jit.scissors</o> object cuts a Jitter matrix of any size, type, or planecount into smaller Jitter matrices that are then sent out independent outlets of the object. The <m>rows</m> and <m>columns</m> attributes specify how many smaller matrices are created each time the object receives a new matrix in its inlet. In our tutorial patch, the <o>jit.scissors</o> object is splitting the image into four smaller matrices (2 <m>columns</m> and 2 <m>rows</m>). These separate matrices come out individual outlets of the object in <i>column-major</i> order (i.e. the object assigns outlets to the smaller matrices from left-to-right and then from top-to-bottom). </p>

<div>
<techdetail>Two very important things you should know about <o>jit.scissors</o>:</techdetail>
<techdetail>1) The number of outlets that <o>jit.scissors</o> has is determined at object creation. Therefore the <m>rows</m> and <m>columns</m> attributes will only create outlets when they are specified in the object box. For example, typing <o>jit.scissors</o> <m>@rows 10 @columns 2</m> will create an instance of <o>jit.scissors</o> with 20 matrix outlets (plus the usual right outet for attribute queries), but simply making a <o>jit.scissors</o> object with no arguments will only give you one matrix outlet. You can change the <m>rows</m> and <m>columns</m> attributes with Max messages to the object, but you won't be able to add outlets beyond what those initially created by the object.</techdetail>
<techdetail>2) The size (<m>dim</m>) of the matrices put out by <o>jit.scissors</o> is equal to the size of the slices of the matrix, not the entire original matrix. For example, the four smaller matrices in our tutorial patch are each 160x120 cells, not 320x240.</techdetail>
</div>
<h2>Routing the Matrices</h2>
<p>The four smaller matrices output by <o>jit.scissors</o> in our patch are each sent to two different places: to <o>jit.pwindow</o> objects so we can see what's going on, and to a Max object in the middle of the patch called <o>router</o>. The colored patchcords illustrate where each smaller matrix is sent.</p>

<illustration><img src="images/jitterchapter13c.png"/>The Max <o>router</o> object</illustration>
<p>The <o>router</o> object is a combination of the Max <o>gate</o> and <o>switch</o> objects. It takes two arguments (the number of routeable inlets and the number of routable outlets) and is controlled by messages sent to the leftmost inlet. Most of the messages that <o>router</o> understands are identical to the MSP object <o>matrix~</o>. As a result you can use <o>router</o> with the <o>matrixctrl</o> object with ease.</p>

<p>The four inlets to the right of the <o>router</o> object take their input from the four matrix outlets of our <o>jit.scissors</o> object. A <o>receive</o> object assigned to the symbol <m>routeit</m> gets messages from the lower-right of the tutorial patch, which controls our <o>router</o> object. The four leftmost outlets of the router object are connected to a <o>jit.glue</o> object, which we'll talk about in a moment.</p>

<illustration><img src="images/jitterchapter13d.png"/>Controlling the <o>router</o></illustration>
<p>Sending the message <m>patch</m> followed by an inlet number and an outlet number to a <o>router</o> object will make a virtual connection between that inlet and that outlet in the object. Any message arriving at that inlet will be instantly forwarded to the relevant outlet. If an inlet was previously connected to that outlet, a <m>patch</m> message will sever that connection in favor of the new one.</p>

<p>The <o>radiogroup</o> objects in this patch control which outlets of the <o>router</o> our four small Jitter matrices (arriving at the inlets) are sent to. The inlets and outlets number up from <m>0</m>, so the message <m>patch 2 1</m> makes a connection between the third routeable inlet and the second outlet of the <o>router</o> object.</p>

<bullet>Click on some of the <o>radiogroup</o> controls, and watch how the output image in the lower jit.pwindow changes. Notice how with the <o>router</o> object you can make the matrices cut from the traffic image appear in any of the four quadrants of the composite image at the bottom.</bullet>
<h2>The Glue That Keeps It Together</h2>
<p>The <o>jit.glue</o> object at the bottom of the patch does the effective opposite of <o>jit.scissors</o>. The <m>rows</m> and <m>columns</m> attributes specify inlets, not outlets, and a composite matrix is output which is made up of the incoming matrices laid out in a grid.</p>


<illustration><img src="images/jitterchapter13e.png"/>Sending the same matrix to all four inlets of <o>jit.glue</o></illustration>
<div>
<techdetail>Important Note: As with <o>jit.scissors</o>, <o>jit.glue</o> can only create new inlets and outlets when the object is created, so the <m>rows</m> and <m>columns</m> attributes present in the object box will determine how many inlets the object has. Also, the size (<m>dim</m>) of the output matrix generated by <o>jit.glue</o> will be equal to the size of all the smaller matrices put together (e.g. our four 160x120 matrices in this patch will yield one 320x240 matrix).</techdetail>
</div><br/>
<p>One final point worth making about <o>jit.glue</o> is that its default behavior is to only output a composite matrix when a new matrix arrives it its <i>leftmost</i> inlet. If we were to disconnect the leftmost inlet of our <o>jit.glue</o> object, we would no longer get any new output matrices from the object. The <m>syncinlet</m> attribute lets you make <o>jit.glue</o> sent its output in response to a different inlet. A <m>syncinlet</m> value of <m>-1</m> will cause <o>jit.glue</o> to output new composite matrices when it gets new matrices at <i>any</i> inlet. While this sounds like a good idea in theory, it can quickly bog down the frame rate of your Jitter processes with lots of redundant work.</p>

<h2>Summary</h2>
<p>The <o>jit.scissors</o> object cuts a matrix into smaller, equal-sized rectangular matrices. The <o>jit.glue</o> object takes equal-sized rectangular matrices and pastes them back together into a composite matrix. The <m>rows</m> and <m>columns</m> attributes of both objects determine their number of outlets or inlets, respectively, when given at object creation, as well as the way in which the matrix is sliced up or composited. The <o>router</o> object lets you arbitrarily connect Max messages from multiple inlets to multiple outlets in a similar fashion to the MSP <o>matrix~</o> object.</p>

	<seealsolist>
		<seealso name="jit.glue">Glue many matrices into one</seealso>
		<seealso name="jit.pwindow">In-Patcher Window</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="jit.scissors">Cut up a matrix into evenly spaced sub matrices</seealso>
		<seealso name="metro">send bangs at regular intervals</seealso>
		<seealso name="radiogroup">Radio button/check box user interface object</seealso>
		<seealso name="router">Matrix-compatible Max message router</seealso>
		<seealso name="toggle">Switch between on and off (1 and 0)</seealso>
	</seealsolist>
	</chapter>
