Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 27 Dec 2009
Offline Last Active Jul 21 2014 09:57 PM

Topics I've Started

Hash Distance & Angle To Produce Unique Value

20 July 2014 - 11:50 PM



I have many vertices drawn on my Unity3d C# application window (thousands). I am attempting to come up with a fast way to detect which vertex has been clicked.


My idea is to use a sorted list. The list will be ordered by a vector/vertices distance to (0,0,0):

List <KeyValuePair<double, GameObject>> vectorMap = new List <KeyValuePair<double, GameObject>>();

I use a binary search to order, search and add to the list.


Add/Insertion Method:

public void recordElementVertices(Element ele) {
	// Element is a Sub-Class of GameObject

	foreach (Vector3d v in ele.WorldVertices) {
		double distFromZero = Vector3d.Distance(Vector3d.zero, v);

		int index = -1;
		if (Algorithms.LowerBounds(vertexDistanceList, distFromZero, out index, DEF_PRECISION) != Algorithms.SearchResult.FOUND_TARGET)
			vertexDistanceList.Insert(index, new KeyValuePair<double, List<GameObject>>(distFromZero, new List<GameObject>()));
		vertexDistanceList[index].Value.Add (ele.gameObject);

Hit Test (Search) Method:

public List<GameObject> hits(Vector3d mousePos) {

	int 	index 			= -1;
	double 	distFromZero 	= Vector3d.Distance(Vector3d.zero, mousePos);

	// Binary search will search for an exact hit or find the closest vertex to the mouse pos
	if (Algorithms.BinarySearch(vertexDistanceList, distFromZero, out index, DEF_PRECISION) == Algorithms.SearchResult.FAIL || index < 0)
		return null;

	return vertexDistanceList[index].Value; // return all game objects that occupies that vector position

This all works nicely and is fast but there is a major flaw. The algorithm doesn't take into account the vertices angle from (0,0,0). So 2 points that are the exact same distance from (0,0,0) but have different angles will be considered the same vertex when they are not.


For example; these 2 points lie 5 metres from (0,0,0) but have different angles. My algorithm will consider these 2 points as occupying the same place in space when they are in a completely different position.




Do you have any suggestions how I can hash a distance and angle to produce a unique result that describes that point in space? Some simple like doing Distance ^ Angle could produce a unique result but they would also produce huge numbers. A restriction is that my list sorting algorithm requires that two points close to each other should produce a hash that is similar in order to find points close to the mouse position. Hope that makes sense.


I've heard about Locatily Sensitve Hashing but it looks like implementing this algorithm would be very tricky.


Any advice would be greatly appreciated.

Determine Mesh Vertex Index Given Another Mesh Index

19 June 2014 - 11:51 PM



Please bare with me as I explain what I am trying to accomplish tongue.png


Each 'X' below represents a mesh vertex:




|      |      |     |     |      |


|      |      |     |     |      |


|      |      |     |     |      |



For my Unity3d GameObject (G1); the mesh vertices are stored in a C# List<Vector3> object in the following order. *Note the mesh index 'A'. This is a value I know - in this case A=16 (mesh index 16).



|      |      |     |     |      |


|      |      |     |     |      |


|      |      |     |     |      |




I have another GameObject (G2) whose shape is the same (a simple rectangle) but the mesh vertices are ordered differently.*Note the mesh index 'B'. This is a value I dont know and I want to find this value (mesh index) - in this case B=17 (mesh index 17).



|      |      |     |     |      |


|      |      |     |     |      |


|      |      |     |     |      |




As you can see the mesh arrangement is pretty much the same except rotated 90 degrees. If I know the 'mesh vertex index' of A how can I find the 'mesh vertex index' of B?


Whats an algorithm that can calculate the mesh index of B? My attempt is not working at all:

uint A     = 16;
uint nCols = 6;
uint nRows = 4;

// Technically both B and A sit on Row 3, Column 5 so if I can find A's row and column maybe I am there?
uint aCol = 16 / nCols;        // nup not right
uint aRow = (16 % nCols) - 1;  // gives 3

// Once I've calculated aCol and aRow its easy
uint B    = (aRow * nRows) + aCol;  // ie, (3 * 4) + 5 = 17

Determine the 3d position of this vector

13 March 2014 - 10:57 PM



I have a rather simple mathematic problem to solve. However I am not that mathematically inclined so I'd appreciate if someone could provide advice on how I solve this mathematic problem:


For the following triangle I wish to find the location (X,Y & Z) of the point/vector C. How can I find this position?




Using simple Pythagoras I can find the length of side Z. But how can I find the position of C? I am attempting to offset/parallel a straight line segment (side X) so I need to calculate the position of C.

Given a set of Vertices: Determine Boundary Vertices and Connection Order

02 February 2014 - 03:36 AM


I am looking for suggestions on algorithms I can use to determine the boundary points/vertices of a list of vertices. I also need to determine which boundary point connects to which other boundary point.



I have my own solution but it involves alot of steps and is more procedurally based rather than mathematically based. Can you suggest any algorithms (both procedural and mathematic)?

My approach:

  •    - Organise the vertices into a Dictionary <int, List<Triangle>> vertexTriangleMap. Where the keys are the indexes of the vertices and the values are lists of the triangles that involve that vertex
  •    - Pick a random vertex(RV) then pick a random triangle involving that vertex. For eg; vertexTriangleMap[9][0]
  •    - Of the 2 other vertices in this triangle: Determine which involves the least triangles (the point w the least triangles = OV)
  •        - Search through OV's triangles looking for another triangle that involves the vertices OV AND RV:
  •            - if there is more than 1 triangle that involves these 2 points then that means that OV is NOT a boundary point else OV is a boundary point AND I know it connects to RV. OV now becomes RV and I can now repeat the process and I will determine which boundary points connect to which. I'll know when to stop when I reach the beginning again (the first RV).



Insert Image into SQLite3 Database: Unsure whats causing the error

24 November 2013 - 09:05 PM



Does anyone have experience inserting binary data into a SQlite3 database using the SQLite3 C API? Note I am using the SQLite3 C API functions imported into a C# application so I am writting C#. I am attempting to write a png to a SQLite3 database in C#. I have managed to correctly import the external DLL function sqlite3_bind_blob. But now I am getting an error when I write the image to my SQLite3 database. When I call the function sqlite3_bind_blob I get the error:


#define SQLITE_MISUSE      21   /* Library used incorrectly */


I am unsure how I am causing this error and what I can do to fix it. Can you tell me what I am doing wrong?

Heres my code:

// Include dll function (unsure if the parameter types are correct)
[DllImport("sqlite3", EntryPoint = "sqlite3_bind_blob", CallingConvention = CallingConvention.Cdecl)]
private static extern int sqlite3_bind_blob (IntPtr stmHandle, int iIndex, byte[] iParam, int iBytes, IntPtr iOperation);

// Code inside my function: Insert image into database
string query = "INSERT OR REPLACE INTO myTable(lat, lon, image) VALUES(-33.555, 151.457, ?1);";
if (sqlite3_prepare_v2 (_connection, query, query.Length, out stmHandle, IntPtr.Zero) != SQLITE_OK) {
    IntPtr errorMsg = sqlite3_errmsg (_connection);
    throw new SqliteException (Marshal.PtrToStringAnsi (errorMsg));

IntPtr SQLITE_TRANSIENT = new IntPtr(-1); // Represents SQLITE_TRANSIENT
int res = sqlite3_bind_blob (stmHandle, 1, blob, blob.Length, SQLITE_TRANSIENT);
// res always equals 21

From my debugging I know that the blob correctly contains valid png data because I can write it out to a file and open that file. I also know that the length of the blob is correct aswell.