<?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>mim.Armand &#187; HashGrid</title>
	<atom:link href="http://armand.eu/blog/tag/hashgrid/feed/" rel="self" type="application/rss+xml" />
	<link>http://armand.eu/blog</link>
	<description>Artist, inventor and developer!</description>
	<lastBuildDate>Wed, 24 Jun 2015 12:02:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>A Simple Spatial Hashing (HashGrid) for processing-2</title>
		<link>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/</link>
		<comments>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/#comments</comments>
		<pubDate>Mon, 23 Jun 2014 12:54:44 +0000</pubDate>
		<dc:creator><![CDATA[mim]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[#Grid]]></category>
		<category><![CDATA[buckets]]></category>
		<category><![CDATA[HashGrid]]></category>
		<category><![CDATA[HashGrid class]]></category>
		<category><![CDATA[OPP]]></category>
		<category><![CDATA[performance improvement]]></category>
		<category><![CDATA[Spatial grid]]></category>
		<category><![CDATA[Spatial Hashing]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://armand.eu/blog/?p=52</guid>
		<description><![CDATA[download, contribute or fork this project here! Continuing from our last post, We were just ready to create our grid &#8220;buckets&#8221;! Buckets are small rectangles forming the grid; objects corresponding to each bucket location will be saved (a reference to them) in that bucket. To create buckets (and our grid) we need to divide the &#8230; <a href="http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/" class="more-link">Continue reading <span class="screen-reader-text">A Simple Spatial Hashing (HashGrid) for processing-2</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><small>download, contribute or fork this project <a title="spatial hash grid" href="https://github.com/mim-Armand/HashGrid" target="_blank">here</a>!</small></p>
<figure id="attachment_57" style="width: 474px;" class="wp-caption aligncenter"><a href="http://armand.eu/blog/wp-content/uploads/2014/06/Spatial_HashGrid_02.jpg"><img class="size-large wp-image-57" src="http://armand.eu/blog/wp-content/uploads/2014/06/Spatial_HashGrid_02-1024x455.jpg" alt="SPatial Hash Grid System" width="474" height="210" /></a><figcaption class="wp-caption-text">Testing the functionality of the class</figcaption></figure>
<p>Continuing from our <a title="Simple Spatial Hashing for Processing (Part 1)" href="http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-1/" target="_blank">last post</a>, We were just ready to create our grid &#8220;buckets&#8221;!<br />
Buckets are small rectangles forming the grid; objects corresponding to each bucket location will be saved (a reference to them) in that bucket.<br />
To create buckets (and our grid) we need to divide the size of our target canvas (it can be our stage size or a virtual stage) to create smaller rectangles, then we will need just to check each object against other objects only in the same bucket or neighbor buckets which will dramatically reduce the required number of iterations. but it&#8217;s important to decide about the bucket sizes according to our needs (number of objects, desired distance to check&#8230;) and since variables affecting or decision here aren&#8217;t constant, it&#8217;s better to make it flexible so we can decide about the bucket size later! we will start with defining a float variable that will dictate desired size of our bucket to the class, if it&#8217;s not defined when calling the class it can be created automatically according to number of objects and requested distance check etc. but for now &#8230; let&#8217;s keep simple!<br />
Also, we would want (I imagine at this point) two methods:<br />
1. obj.set() &#8211;&gt; Objects will declare their position to class and get referenced in corresponding buckets.<br />
2. obj.get() &#8211;&gt; which will return an array of references in all the neighbors in the  requested distance.</p>
<p>Here to check the functionality of our Spatial hash grid class I&#8217;ll use a <a title="Auto generated cover page using Processing" href="http://armand.eu/blog/auto_cover_processing_01/" target="_blank">simple class I wrote before</a>, which just generates bunch of random point objects on the stage, so we can put our class under some definable load, the<a title="Points class gist" href="https://gist.github.com/mim-Armand/5169c185aecc43ab927e" target="_blank"> Points class</a> is simple (and anyway not the subject of this topic, but let me know if you want know more about it!):</p>
<p><script src="https://gist.github.com/mim-Armand/5169c185aecc43ab927e.js"></script>So! here is the structure of our HashGrid class:<script src="https://gist.github.com/mim-Armand/b89a1846075d984b282c.js"></script><br />
Here we just declared the class and bunch of variables and methods we know we&#8217;re gonna need.<br />
There is also a displayGrid() function that will just draw our grid for us on the display so it&#8217;d be easier to understand and to debug.<br />
But we have defined another class inside the HashGrid class as well (a <a title="Nested Classes (Oracle Reference pages)" href="http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html" target="_blank">nested class</a>) near the end of this code snippet, Buckets class. that&#8217;s because in Java you should define the array type in declaration time and since our array will keep reference to Objects and there is not such a primitive called &#8220;Object&#8221; you need to define the object to be able to declare an array of that kine of objects, so we create a nested class called buckets to keep our bucket arrays and an array of our buckets in it!<br />
Also you may notice that I hard coded another object type in my HashGrid class (The Points) which is not the best practice since we want to be able to use this class everywhere with no modifications, but for now and to keep it as simple as possible we are gonna leave it like that, but later on (in another post hopefully if I could) we will make it more abstract by using an <a title="Oracle Java interface reference page" href="http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html" target="_blank">interface </a>or some other techniques.</p>
<p>Now we need to create an array of buckets, create an array in each bucket (to keep our object references) and populate our set() and get() functions:<br />
<script src="https://gist.github.com/mim-Armand/a7c907f84dbbdd8af772.js"></script>Now our HashGrid class is all functional! awesome! but there is a few things still we need to do, some of them are relatively easy to implement, like adding the functionality to be able to define the desired distance to check (checking neighbors as well as the bucket itself) and also the Interface, we are gonna work on these later-on (hopefully!) in another post. But now to test our class we can create a new processing file (tab) and put <a title="test the class" href="https://gist.github.com/mim-Armand/3dfab04834484ae2f34f" target="_blank">this</a> code on it:<script src="https://gist.github.com/mim-Armand/3dfab04834484ae2f34f.js"></script><br />
Yep! it works! you can download, contribute or fork the whole project <a title="An spatial hash grid implementation" href="https://github.com/mim-Armand/HashGrid" target="_blank">here </a>at Github:<br />
https://github.com/mim-Armand/HashGrid</p>
]]></content:encoded>
			<wfw:commentRss>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple Spatial Hashing (HashGrid) for processing-1</title>
		<link>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-1/</link>
		<comments>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-1/#comments</comments>
		<pubDate>Thu, 19 Jun 2014 11:25:07 +0000</pubDate>
		<dc:creator><![CDATA[mim]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[#Grid]]></category>
		<category><![CDATA[HashGrid]]></category>
		<category><![CDATA[HashGrid class]]></category>
		<category><![CDATA[OPP]]></category>
		<category><![CDATA[performance improvement]]></category>
		<category><![CDATA[Spatial grid]]></category>
		<category><![CDATA[Spatial Hashing]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://armand.eu/blog/?p=39</guid>
		<description><![CDATA[While working on bunch of sketches to create visuals for one of my documents, I was thinking to implement it with a simple Spatial Hash grid, but since performance wasn&#8217;t that important in that particular project, I just did it the simple way (checking every node against all the others in each iteration which means &#8230; <a href="http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-1/" class="more-link">Continue reading <span class="screen-reader-text">A Simple Spatial Hashing (HashGrid) for processing-1</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<figure id="attachment_50" style="width: 1000px;" class="wp-caption aligncenter"><a href="http://armand.eu/blog/wp-content/uploads/2014/06/spatial_hashGrid.jpg"><img class="wp-image-50 size-full" src="http://armand.eu/blog/wp-content/uploads/2014/06/spatial_hashGrid.jpg" alt="Spatial HashGrid projected on a sphere" width="1000" height="525" /></a><figcaption class="wp-caption-text">Image Source: Wikimedia.</figcaption></figure>
<p style="text-align: justify;">While working on bunch of sketches to create visuals for one of my documents, I was thinking to implement it with a simple Spatial Hash grid, but since performance wasn&#8217;t that important in that particular project, I just did it the simple way (checking every node against all the others in each iteration which means &#8212;&gt; n² checks / frame (90K check for 300 prticle points for example!)) and it worked just fine! but strangely enaugh it was bothering me that I didn&#8217;t do it the right way! and since I&#8217;m gonna to reuse the code in another projects for sure and since I&#8217;ll need to implement this technique anyway sooner or later in one of my &#8220;things&#8221;! I tried to give it a try and do it the &#8220;right way&#8221;.<br />
I started by just thinking and trying to analyze the way I&#8217;d imagine it works, I wanted something simple and minimal, so I did bunch of sketches on paper like these one (which probably make sense just to myself!):</p>
<figure id="attachment_49" style="width: 474px;" class="wp-caption aligncenter"><a href="http://armand.eu/blog/wp-content/uploads/2014/06/WP_20140619_004.jpg"><img class="size-large wp-image-49" src="http://armand.eu/blog/wp-content/uploads/2014/06/WP_20140619_004-1024x574.jpg" alt="Spatial hash first sketches" width="474" height="265" /></a><figcaption class="wp-caption-text">primitive sketches on paper to define spatial grid I wanted to have to myself!</figcaption></figure>
<p style="text-align: justify;">The concept behind spatial grids is simple: you keep track of each target object by keeping a reference to that object in the grid bucket beneath it (in a 2D scenario (which is expandable to a 3D one respectivly easy)), so each bucket is an array of references to objects located on that bucket&#8217;s position (on the grid), of course it can be further developed I think to a more efficient algorithm combining a quad-tree system for scenarios where objects are placed mostly not evenly on the grid ( buckets will have a limit size (let&#8217;s say 3) and then (if there is more than 3 objects located there) they&#8217;ll divide to 4 buckets&#8230;), but we don&#8217;t need that here since my objects will be scattered on space by a random perlin noise algorithm (almost evenly placed on enough large spaces). Doing a bit of research I found out some interesting libraries which would do the job just fine, like <a title="HashGrid class from giCentre Utile library" href="http://www.gicentre.net/utils/hashgrid" target="_blank">HashGrid class</a> in <a title="giCentre!" href="http://www.gicentre.net/" target="_blank">giCentre</a> <a title="giCentre Util's library for processing" href="http://www.gicentre.net/utils" target="_blank">Utiles </a>library:</p>
<pre><code>hashGrid = new HashGrid(width,height,10);</code></pre>
<p style="text-align: justify;">But first of all, I wanted it to be simple and just do a few things that I need it to do and secondly (and more important for me) I wanted to do it myself so I&#8217;d understand it and I&#8217;d be able to use the technique whenever and wherever (let&#8217;s say in my simple JS Canvas game) I wanted to, and all in all it looked like easy to implement! (surly enough, just to do the basic and probably not in the best way possible?!) So anyway I started to write the class:<br />
<small>(Ah btw, unfortunately, their source code is not available to public! (just a broken link on their <a title="giCentre utils on Google code (not accessible!)" href="https://code.google.com/p/gicentreutils/" target="_blank">Google repo site</a>) and I didn&#8217;t find any other essay to do this on internet so I was (or am) pretty much on myself to figure it out! wish me luck!)</small></p>
<pre><code>class HashGrid{
// It is just a simple class declaration so far!
 HashGrid(/*defined vars*/){
// constructor
 }
}</code></pre>
<p style="text-align: justify;">To keep reference to objects there is ArrayList class/ interface available in <a title="Class ArrayList&lt;E&gt; in JAVA Docs" href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html" target="_blank">JAVA </a>(and <a title="ArrayList class reference in Processing Docs." href="https://www.processing.org/reference/ArrayList.html" target="_blank">Processing</a>) which is very convenient since it resizes dynamically according to the number of objects in it, but is slower than a simple <a href="https://www.processing.org/reference/Array" target="_blank">array</a> of objects, so it&#8217;d be better if we can have simple arrays but then we should think about a mechanism to <a title="Append" href="https://www.processing.org/reference/append_.html" target="_blank">add</a> AND <a title="Shorten()" href="https://www.processing.org/reference/shorten_.html" target="_blank">remove </a>(There is a confusion for me though since in Java these methods <a title="Once an array object is created, its length never changes." href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html" target="_blank">doesn&#8217;t exist</a>! but apparently they exist in Processing (as it explain in the reference page: Decreases an array by one element and <strong>returns the shortened array</strong>. (Also look at <a title="Array length change works in processing!!" href="https://gist.github.com/mim-Armand/42e8900a5aa3dca12f54" target="_blank">this snippet</a>))) (<strong><span style="color: #ff0000;">EDIT</span></strong>: <small>I further investigated this issue and <a title="Why use ArrayList instead of array with append()?" href="http://wiki.processing.org/w/Why_use_ArrayList_instead_of_array_with_append%28%29%3F" target="_blank">found out</a> that (as I guessed) the append and shorten methodes in processing are pretty ineficient and actually just create a new array, with one member more or less and copy the old one to it, and don&#8217;t respect Java specs, all in all, seems better to avoid them (after all we can do the same thing if we need and keep the code more reusable in another Java envirenment)</small>) items manually to and from the array (which happens automatically using <a title="ArrayList reference page in processing Documentations." href="https://www.processing.org/reference/ArrayList.html" target="_blank">ArrayList</a>) but due to Java Array limitations the length of Array can not be changed, &#8230; since our concern is performance here (that&#8217;s why we want to create a hashGrid in the first place!) we should obviously go with standard arrays. (I&#8217;ll make this decision later on, be cause although Arrays are faster but: 1.we need to allocate a considerable amount of memory to them either we use that space or not, and 2. it may bring more and in-neccesary complexity to the process when we need to itterate each bucket array to get references to objects&#8230; (<strong><span style="color: #ff0000;">EDIT</span></strong>: <small>it turned out that it wasn&#8217;t that complicated! so I used simple Arrays finally, Although I guess other implementations, including the <a href="http://www.gicentre.net/" target="_blank">GiCentre Utiles</a>, are using arrayLists, so I guess my methode should be a lot faster than other ones specially when the number of elements is higher)</small> ), let&#8217;s go and start by creating out buckets:</p>
<p style="text-align: justify;">. . .<br />
TO BE CONTINUED IN THE NEXT POST!</p>
]]></content:encoded>
			<wfw:commentRss>http://armand.eu/blog/a-simple-spatial-hashing-hashgrid-for-processing-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
