<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-spot &#187; C++</title>
	<atom:link href="http://code-spot.co.za/category/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://code-spot.co.za</link>
	<description>a programming blog</description>
	<lastBuildDate>Wed, 25 Aug 2010 10:59:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Region Quadtrees in C++</title>
		<link>http://code-spot.co.za/2010/06/15/region-quadtrees-in-c/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=region-quadtrees-in-c</link>
		<comments>http://code-spot.co.za/2010/06/15/region-quadtrees-in-c/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 11:14:14 +0000</pubDate>
		<dc:creator>herman.tulleken</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Image Processing]]></category>
		<category><![CDATA[image partitioning]]></category>
		<category><![CDATA[quadtree]]></category>
		<category><![CDATA[spatial partitioning]]></category>

		<guid isPermaLink="false">http://code-spot.co.za/?p=983</guid>
		<description><![CDATA[
(Original image by GoAwayStupidAI).
Below are four C++ implementations of the region quadtree (the kind used for image compression, for example). The different implementations were made in an attempt to optimise construction of quadtrees. (For a tutorial on implementing region quadtrees, see Issue 26 [6.39 MB zip] of Dev.Mag).

NaiveQuadtree is the straightforward implementation.
AreaSumTableQuadtree uses a summed [...]


Related posts:<ol><li><a href='http://code-spot.co.za/2008/10/06/quadtrees/' rel='bookmark' title='Permanent Link: Quadtrees'>Quadtrees</a></li>
<li><a href='http://code-spot.co.za/2008/11/15/quadtrees-2/' rel='bookmark' title='Permanent Link: Quadtrees'>Quadtrees</a></li>
<li><a href='http://code-spot.co.za/2008/12/15/a-simple-texture-algorithm-faster-code-and-more-results/' rel='bookmark' title='Permanent Link: A simple texture algorithm – faster code and more results'>A simple texture algorithm – faster code and more results</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img style="display: inline; border: 0px;" title="quadtree" src="http://code-spot.co.za/blog/wp-content/uploads/2010/06/quadtree.png" border="0" alt="quadtree" width="491" height="375" /></p>
<p>(<a href="http://www.flickr.com/photos/goawaystupidai/3106598088/">Original image</a> by <a href="http://www.flickr.com/photos/goawaystupidai/">GoAwayStupidAI</a>).</p>
<p>Below are four C++ implementations of the <a href="http://www.voronoi.com/wiki/index.php?title=Quadtree">region quadtree</a> (the kind used for image compression, for example). The different implementations were made in an attempt to optimise <em>construction </em>of quadtrees. (For a tutorial on implementing region quadtrees, see <a href="http://www.devmag.org.za/uploads/DevMag_Issue26.zip">Issue 26</a> [6.39 MB zip] of <a href="http://www.devmag.org.za/">Dev.Mag</a>).</p>
<ul>
<li><strong>NaiveQuadtree </strong>is the straightforward implementation.</li>
<li><strong>AreaSumTableQuadtree </strong>uses a <a href="http://en.wikipedia.org/wiki/Summed_area_table">summed area table</a> to perform fast calculations of the mean and variance of regions in the data grid.</li>
<li><strong>AugmentedAreaSumTableQuadtree </strong>is the same, except that the area sum table has an extra row and column of zeros to prevents if-then logic that slows it down and makes it tricky to understand.</li>
<li><strong>SimpleQuadtree </strong>is the same as AugmentedAreaSumTableQuadtree , except that no distinction is made (at a class level) between different node types.</li>
</ul>
<p><span id="more-983"></span>The interfaces of all quadtrees are the same, but I did not want to extend from a base class. (Instead, a compile time check is performed on the classes, using <a href="http://www.boost.org/doc/libs/1_43_0/libs/concept_check/concept_check.htm">boost concepts</a>).</p>
<p>The results of the performance (on my machine!) of the trees are as follows:</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td style="text-align: left;" width="100" valign="top">(milliseconds)</td>
<td style="text-align: right;" width="100" valign="top"><strong>N</strong></td>
<td style="text-align: right;" width="100" valign="top"><strong>S</strong></td>
<td style="text-align: right;" width="100" valign="top"><strong>AST</strong></td>
<td style="text-align: right;" width="100" valign="top"><strong>AAST</strong></td>
</tr>
<tr>
<td style="text-align: right;" width="100" valign="top"></td>
<td style="text-align: right;" width="100" valign="top">3</td>
<td style="text-align: right;" width="100" valign="top">4</td>
<td style="text-align: right;" width="100" valign="top">4</td>
<td style="text-align: right;" width="100" valign="top">3</td>
</tr>
<tr>
<td width="100" valign="top"><strong>64×64</strong></td>
<td style="text-align: right;" width="100" valign="top">14</td>
<td style="text-align: right;" width="100" valign="top">14</td>
<td style="text-align: right;" width="100" valign="top">14</td>
<td style="text-align: right;" width="100" valign="top">13</td>
</tr>
<tr>
<td width="100" valign="top"><strong>128×128</strong></td>
<td style="text-align: right;" width="100" valign="top">55</td>
<td style="text-align: right;" width="100" valign="top">52</td>
<td style="text-align: right;" width="100" valign="top">55</td>
<td style="text-align: right;" width="100" valign="top">52</td>
</tr>
<tr>
<td width="100" valign="top"><strong>256×256</strong></td>
<td style="text-align: right;" width="100" valign="top">229</td>
<td style="text-align: right;" width="100" valign="top">233</td>
<td style="text-align: right;" width="100" valign="top">218</td>
<td style="text-align: right;" width="100" valign="top">214</td>
</tr>
<tr>
<td width="100" valign="top"><strong>512×512</strong></td>
<td style="text-align: right;" width="100" valign="top">950</td>
<td style="text-align: right;" width="100" valign="top">1036</td>
<td style="text-align: right;" width="100" valign="top">937</td>
<td style="text-align: right;" width="100" valign="top">922</td>
</tr>
<tr>
<td width="100" valign="top"><strong>1024×1024</strong></td>
<td style="text-align: right;" width="100" valign="top">4064</td>
<td style="text-align: right;" width="100" valign="top">4459</td>
<td style="text-align: right;" width="100" valign="top">5396</td>
<td style="text-align: right;" width="100" valign="top">3891</td>
</tr>
</tbody>
</table>
<p>As it turns out, the different implementations do not differ significantly. Constructing the nodes takes long, and not so much the calculations necessary to determine whether a node should split or not, and what data should be in the node. Had I profiled properly before I started I would not have gone through this exercise…</p>
<p>Between these four implementations, the NaiveQuadtree is the one I recommend; I left in the other implementations for anyone interested.</p>
<p>The one good thing that came from this experiment is that I found out that using a zero-augmented summed area table can increase performance quite a bit. This is useful for <a href="http://code-spot.co.za/2010/04/16/simple-fast-approximate-minimum-maximum-filters/">max-filters</a> and other algorithms that use these tables.</p>
<p>You can download the source code:</p>
<p><a href="http://www.code-spot.co.za/downloads/cpp/Quadtree.zip">Quadtree.zip</a> (44 KB, Visual Studio).</p>
<p>(It requires the <a href="http://www.boost.org/users/download/">boost library</a> for concept checking, but nothing else. Everything will still work if you remove all references to the boost library. If you already have boost, just hook up to the include path in Visual Studio).</p>
<p>&#8230;or read the <a href="http://www.code-spot.co.za/downloads/cpp/quadtrees/html/index.html">online documentation</a>.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcode-spot.co.za%2F2010%2F06%2F15%2Fregion-quadtrees-in-c%2F&amp;linkname=Region%20Quadtrees%20in%20C%2B%2B"><img src="http://code-spot.co.za/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://code-spot.co.za/2008/10/06/quadtrees/' rel='bookmark' title='Permanent Link: Quadtrees'>Quadtrees</a></li>
<li><a href='http://code-spot.co.za/2008/11/15/quadtrees-2/' rel='bookmark' title='Permanent Link: Quadtrees'>Quadtrees</a></li>
<li><a href='http://code-spot.co.za/2008/12/15/a-simple-texture-algorithm-faster-code-and-more-results/' rel='bookmark' title='Permanent Link: A simple texture algorithm – faster code and more results'>A simple texture algorithm – faster code and more results</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://code-spot.co.za/2010/06/15/region-quadtrees-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple texture algorithm – faster code and more results</title>
		<link>http://code-spot.co.za/2008/12/15/a-simple-texture-algorithm-faster-code-and-more-results/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-simple-texture-algorithm-faster-code-and-more-results</link>
		<comments>http://code-spot.co.za/2008/12/15/a-simple-texture-algorithm-faster-code-and-more-results/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 11:22:29 +0000</pubDate>
		<dc:creator>herman.tulleken</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Image Processing]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[blend animation]]></category>
		<category><![CDATA[blending]]></category>
		<category><![CDATA[optimisation]]></category>
		<category><![CDATA[Perlin noise]]></category>
		<category><![CDATA[procedural texture]]></category>
		<category><![CDATA[prototyping]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://code-spot.co.za/?p=396</guid>
		<description><![CDATA[
Faster Code
A while back I wrote about a simple texture algorithm that I have been exploring. The Python implementation was very slow – so much, that I decided to implement it in C++ to see what performance gain I would get. Surprisingly, the C++ version is about 100 faster, if not more. I expected a [...]


Related posts:<ol><li><a href='http://code-spot.co.za/2008/11/14/a-simple-procedural-texture-algorithm-more-results-and-code/' rel='bookmark' title='Permanent Link: A Simple Procedural Texture Algorithm &#8211; More Results and Code'>A Simple Procedural Texture Algorithm &#8211; More Results and Code</a></li>
<li><a href='http://code-spot.co.za/2008/11/07/a-simple-procedural-texture-algorithm/' rel='bookmark' title='Permanent Link: A Simple Procedural Texture Algorithm'>A Simple Procedural Texture Algorithm</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/header.png" alt="header" width="500" height="348" /></p>
<h2>Faster Code</h2>
<p>A while back I wrote about a <a href="http://code-spot.co.za/2008/11/07/a-simple-procedural-texture-algorithm/">simple texture algorithm</a> that I have been exploring. The <a href="http://code-spot.co.za/2008/11/14/a-simple-procedural-texture-algorithm-more-results-and-code/">Python implementation</a> was <a href="http://code-spot.co.za/2008/11/11/5-tips-for-prototyping-slow-algorithms/">very slow</a> – so much, that I decided to implement it in C++ to see what performance gain I would get. Surprisingly, the C++ version is about 100 faster, if not more. I expected a decent increase, but what once took several hours can now be done in a minute!</p>
<p><span id="more-396"></span></p>
<p>Not all of the performance gain is because of using C++ instead of Python. The Python version uses a data structure (an <a href="http://www.code-spot.co.za/downloads/python_image_code/doc/html/namespaceenhanced__grid.html">“enhanced” grid</a>) that provides a lot of fancy syntax for operating on a grid. For instance:</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td width="97" valign="top">grid[0, ...]</td>
<td width="400" valign="top">First column (1D)</td>
</tr>
<tr>
<td width="97" valign="top">grid[..., 1]</td>
<td width="400" valign="top">Second row (1D)</td>
</tr>
<tr>
<td width="97" valign="top">grid[0, ::2]</td>
<td width="400" valign="top">Every second item of the first column (1D)</td>
</tr>
<tr>
<td width="97" valign="top">grid[1:3,::2]</td>
<td width="400" valign="top">Every second item of the second and third column (2D)</td>
</tr>
<tr>
<td width="97" valign="top">grid[3, 4]</td>
<td width="400" valign="top">The item at the fourth column and fifth row (0D)</td>
</tr>
</tbody>
</table>
<p>The “enhanced” grid also provides several iterators that allow easy processing, for example:</p>
<ul>
<li>a cell iterator, that iterates over all values in the grid;</li>
<li>an index iterator, that iterates over all possible tuples that can be used to index the grid; and</li>
<li>a window iterator, that iterates over all cells in a rectangular portion of the grid.</li>
</ul>
<p>The speed problem is caused by the implementation that supports the above features – an implementation that I thought was rather elegant. I knew there was a performance penalty, but after seeing to what extent I realise that I must have a hard look at how these features are implemented.</p>
<p>I am a big fan of late optimisation, especially for experimental code. But this incident has made me re-evaluate my position. After I had the fast C++ version running, I could do hordes of experiments very quickly – which meant I got a much better intuition of how parameters influence the resulting texture.</p>
<p>It also made me realise how important it is to check the performance of supporting data structures, even if they are merely convenience structures in non-production code (as is the “enhanced” grid).</p>
<h2>More results</h2>
<p>One of the things that struck me about the textures that this algorithm typically produces is the shapes of blotches of similar colour. I have been trying to enhance (and take advantage) these shapes by applying various filters on the texture; below are the results.</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/original.png" alt="original" width="160" height="160" /></td>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/water.png" alt="water" width="160" height="160" /></td>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/brown.png" alt="brown" width="160" height="160" /></td>
</tr>
<tr>
<td width="166" valign="top">Original (generated from a random 5&#215;5 matrix)</td>
<td width="166" valign="top">Dilation</td>
<td width="166" valign="top">Posterisation</td>
</tr>
<tr>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/sobel.png" alt="sobel" width="160" height="160" /></td>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/sobel-median.png" alt="sobel_median" width="160" height="160" /></td>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/edge.png" alt="edge" width="160" height="160" /></td>
</tr>
<tr>
<td width="166" valign="top">Sobel edge detection</td>
<td width="166" valign="top">Median filter and Sobel edge detection</td>
<td width="166" valign="top">Median filter, differential edge detection</td>
</tr>
<tr>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/emboss.png" alt="emboss" width="160" height="160" /></td>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/alien.png" alt="alien" width="160" height="160" /></td>
<td width="166" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/green.png" alt="green" width="160" height="160" /></td>
</tr>
<tr>
<td width="166" valign="top">Emboss</td>
<td width="166" valign="top">Alien (Gimp filter)</td>
<td width="166" valign="top">Median filter.</td>
</tr>
</tbody>
</table>
<p>The texture is also suitable for organic blend animations (compare with organic blending using <a href="http://www.luma.co.za/labs/2008/01/20/perlin-noise/">Perlin noise</a>).</p>
<table border="0" cellspacing="0" cellpadding="0" width="500">
<tbody>
<tr>
<td width="83" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/t1.png" alt="t1" width="80" height="80" /></td>
<td width="83" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/t2.png" alt="t2" width="80" height="80" /></td>
<td width="83" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/t3.png" alt="t3" width="80" height="80" /></td>
<td width="83" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/t4.png" alt="t4" width="80" height="80" /></td>
<td width="83" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/t5.png" alt="t5" width="80" height="80" /></td>
<td width="83" valign="top"><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/t6.png" alt="t6" width="80" height="80" /></td>
</tr>
</tbody>
</table>
<p><img src="http://code-spot.co.za/blog/wp-content/uploads/2008/12/t1.gif" alt="t1" width="160" height="160" /></p>
<h2>Download</h2>
<h3>C++ Implementation</h3>
<p><a href="http://www.code-spot.co.za/downloads/cpp_examples/cpp_texture.zip">cpp_texture.zip</a> (1.78 MB) (Visual Studio Solution)</p>
<p>The example uses <a href="http://pngwriter.sourceforge.net/">PNG Writer</a> , <a href="http://www.libpng.org/">libpng</a> and <a href="http://www.zlib.net/">zlib</a> (library files included with the download).</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcode-spot.co.za%2F2008%2F12%2F15%2Fa-simple-texture-algorithm-faster-code-and-more-results%2F&amp;linkname=A%20simple%20texture%20algorithm%20%E2%80%93%20faster%20code%20and%20more%20results"><img src="http://code-spot.co.za/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://code-spot.co.za/2008/11/14/a-simple-procedural-texture-algorithm-more-results-and-code/' rel='bookmark' title='Permanent Link: A Simple Procedural Texture Algorithm &#8211; More Results and Code'>A Simple Procedural Texture Algorithm &#8211; More Results and Code</a></li>
<li><a href='http://code-spot.co.za/2008/11/07/a-simple-procedural-texture-algorithm/' rel='bookmark' title='Permanent Link: A Simple Procedural Texture Algorithm'>A Simple Procedural Texture Algorithm</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://code-spot.co.za/2008/12/15/a-simple-texture-algorithm-faster-code-and-more-results/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
