<?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>Real-Time Rendering &#187; loop</title>
	<atom:link href="http://www.realtimerendering.com/blog/tag/loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.realtimerendering.com/blog</link>
	<description>Tracking the latest developments in interactive rendering techniques</description>
	<lastBuildDate>Sun, 12 May 2013 00:21:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Looping Through Polygon Edges</title>
		<link>http://www.realtimerendering.com/blog/looping-through-polygon-edges/</link>
		<comments>http://www.realtimerendering.com/blog/looping-through-polygon-edges/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 21:28:51 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[polygon]]></category>

		<guid isPermaLink="false">http://www.realtimerendering.com/blog/?p=377</guid>
		<description><![CDATA[We mostly avoid coding issues in our book, as our focus is on algorithms, not syntax and compiler vagaries. There&#8217;s a coding trick that I want to pass on, as it&#8217;s handy. Graphics programmers appear to be divided into two groups with this method: those who think it&#8217;s intuitively obvious and learnt it on their [...]]]></description>
			<content:encoded><![CDATA[<p>We mostly avoid coding issues in our book, as our focus is on algorithms, not syntax and compiler vagaries. There&#8217;s a coding trick that I want to pass on, as it&#8217;s handy. Graphics programmers appear to be divided into two groups with this method: those who think it&#8217;s intuitively obvious and learnt it on their pappy&#8217;s knee, and those who never saw it before and are glad to find out.</p>
<p>You want to loop through the edges of a polygon. The vertex data is stored in some array <em>vertexData[count]</em>, an array of <em>count </em>of some sort of <em>Vertex </em>data structure. The headache is attaching the last and first vertices together to make the connecting edge. There are plenty of weak ways to walk through the edges and connect last and first:</p>
<ul>
<li>Double the beginning vertex so it&#8217;s added to the end of array; the final edge is then just another pair of adjacent points. This is perhaps even fastest to actually execute but is generally a hideous solution, adding a copy of a vertex to the array.</li>
<li>Form the last edge explicitly, outside the loop. Poor for maintenance, as you then need to copy whatever other code is inside the loop to be called one more time.</li>
<li>Use an &#8220;if&#8221; statement to know if you&#8217;re at the end of the loop; if so, then connect the first and last vertices for the last edge. The &#8220;if&#8221; special case is needed for only one vertex, which is wasteful and we&#8217;d like to avoid &#8220;ifs&#8221;.</li>
<li>Use modulo arithmetic on the counter for one of the vertices, so that it loops back to the start.</li>
</ul>
<p>Modulo isn&#8217;t terrible, but is overkill and costs processing speed, as the modulo operation is truly needed for only the very last iteration:</p>
<pre>for ( int v = 0; v &lt; count; v++ ) {
   // access vertexData[v] and vertexData[(v+1)%count] for the edge
}</pre>
<p>Here&#8217;s the solution I prefer:</p>
<pre>for ( int v1 = count-1, int v2 = 0; v2 &lt; count; v1 = v2++ ) {
   // access vertexData[v1] and vertexData[v2] for the edge
}</pre>
<p>The simple trick is that <em>v1</em> starts at the end of polygon, so dealing with the tough &#8220;bridge&#8221; case immediately; <em>v2</em> counts through the vertices, and <em>v1</em> follows behind. You can, similarly, make a pointer-based version, updating the <em>pV1</em> pointer by copying from <em>pV2</em>. If register space is at a premium, then modulo might be a better fit, but otherwise this loop strikes me as the cleanest solution.</p>
<p>This copy approach can be extended to access any number of neighboring vertices per iteration. For example, if you wanted the two vertices <em>vp </em>and <em>vn</em>, previous and next to a given vertex, it&#8217;s simply:</p>
<pre>int vp, v, vn;
for ( vp = count-2, v = count-1, vn = 0; vn &lt; count; vp = v, v = vn++ ) {
   // access vertexData[vp], [v], [vn] for the middle vertex v.
}</pre>
<p>I&#8217;ve seen this type of trick in code in <em><a href="http://www.amazon.com/Geometric-Computer-Graphics-Morgan-Kaufmann/dp/1558605940?tag=realtimerenderin">Geometric Tools</a></em>, and <a href="http://jgt.akpeters.com/papers/Barrett05/">Barrett formally presents it in </a><em><a href="http://jgt.akpeters.com/papers/Barrett05/">jgt</a></em>. I mention it here because I think it&#8217;s a technique every computer graphics person should know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.realtimerendering.com/blog/looping-through-polygon-edges/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>