Very Strange: Every other time an array is updated its values are screwed up - arrays

You can see the code there: http://jsfiddle.net/jocose/CkL5F/901/
(double click on the box and move your mouse)
NOTE: This is a simplified example that is part of a larger system. My ultimate goal is to manipulate individual vertices of a path.
Update: I crunched the numbers and the math actually apears to be correct. What I want to do is calculate the offset from each point to the mouse, and then move that point to the mouses position + the offset.
So if I have a mouse of 224 then 224-103 = 121 then I add: 121+224=345
These creates a cycle of ups and downs that I am seeing. I don't know why these is stumping me so badly, any help would be much appreciated.
I need to manually update a Raphael path element.
To do this I convert an absolute path into an array using Raphael great built in function "parsePathString"
I then loop through that array and modify the values based off the mouse position.
The update is done to the X values only, and is in real time; called each time the mouse moves.
When the element moves it flickers back and forth between the correct position and some anomalous one.
I have no clue why its doing this. I have spent almost 5 hours trying to figure this out and I'm officially stuck.
Here is a sample of the result where you can see the values jumping around:
MOUSE224
M,103.676287
MOUSE225
M,346.323713
MOUSE227
M,107.676287
MOUSE228
M,348.323713 12
MOUSE228
M,107.676287
MOUSE229
M,350.323713
MOUSE231
M,111.67S287
MOUSE232
M,3S2.323713
MOUSE233
M,113.676287
MOUSE233
M,3S2.323713

Here's my version of your fiddle modified to do what I think you need. At least, it seems to work. It's the same type of problem I had to fix for the Raphael 2 transformations here.
Basically, in your mousemove, I've changed mx to be a calculation of the offset between where your mouse is now and where it was the last time mousemove was called. Your move() function now only has to add this value to the x-coords.
Hope this helps you out somewhat

Related

ARKit: Reproducing the Project Point function

I'm attempting to reproduce the ARCamera's project point function, but for some reason the values are not matching up properly. I am taking the ARCamera's projection matrix and view matrix and applying basic CG perspective transform math, (PV) * p, but the NDC values do not match the pixel values given from the ARCamera's project point function. Any ideas? Am I forgetting something?
Some more detail:
Basically, I'm trying to take an ARFrame a the click of a button, and then trying to replicate the functionality of https://developer.apple.com/documentation/arkit/arcamera/2923538-projectpoint. I'm attempting to do this with https://developer.apple.com/documentation/arkit/arcamera/2887458-projectionmatrix and https://developer.apple.com/documentation/arkit/arcamera/2921672-viewmatrix, making sure all of the inputs match for both parts. CG size is used to transform the coordinates from NDC space to image space.
EDIT: Solution found, check comments below.
The problem turned out to be projection_matrix sometimes does not correctly find the device orientation. The correct approach is to use projectionMatrix(for:viewportSize:zNear:zFar:).

Compare changes among an array of hashes

I have an array where each element is a hash representing a simplified version of an entire Chess Board. I am trying to implement the fifty-move draw rule which states a draw can be claimed if in fifty moves, no piece has been captured and no pawn has moved.
In doing so, I'm trying to keep DRY and use code that I've already implemented for another draw scenario, which is currently working properly.
A new "snapshot" of the board is saved after each turn and looks like the following (after a pawn has moved from "a2" to "a4" on the first turn):
board_snapshot = [{
"a1"=>"Rook", "a2"=>nil, "a3"=>nil, "a4"=>"Pawn", "a5"=>nil, "a6"=>nil,
"a7"=>"Pawn", "a8"=>"Rook", "b1"=>"Knight", "b2"=>"Pawn", "b3"=>nil,
"b4"=>nil, "b5"=>nil, "b6"=>nil, "b7"=>"Pawn", "b8"=>"Knight",
"c1"=>"Bishop", "c2"=>"Pawn", "c3"=>nil, "c4"=>nil, "c5"=>nil, "c6"=>nil,
"c7"=>"Pawn", "c8"=>"Bishop", "d1"=>"Queen", "d2"=>"Pawn", "d3"=>nil,
"d4"=>nil, "d5"=>nil, "d6"=>nil, "d7"=>"Pawn", "d8"=>"Queen", "e1"=>"King",
"e2"=>"Pawn", "e3"=>nil, "e4"=>nil, "e5"=>nil, "e6"=>nil, "e7"=>"Pawn",
"e8"=>"King", "f1"=>"Bishop", "f2"=>"Pawn", "f3"=>nil, "f4"=>nil,
"f5"=>nil, "f6"=>nil, "f7"=>"Pawn", "f8"=>"Bishop", "g1"=>"Knight",
"g2"=>"Pawn", "g3"=>nil, "g4"=>nil, "g5"=>nil, "g6"=>nil, "g7"=>"Pawn",
"g8"=>"Knight", "h1"=>"Rook", "h2"=>"Pawn", "h3"=>nil, "h4"=>nil,
"h5"=>nil, "h6"=>nil, "h7"=>"Pawn", "h8"=>"Rook"
}]
In pseudocode, I'm thinking of implementing this fifty move rule check by creating a method which looks at the previous fifty board snapshots to see if the amount of nil values are the same (no piece captured) and if so, somehow looking to see that each of the Pawns are on the same square.
I've found a way to compare two boards to see if the nil values are the same:
board_snapshot[index].values.count(nil) == board_snapshot[index + 1].values.count(nil)
However, I'm still having trouble coming up with a way to iterate over 50 board "snapshots" to run this test on each one. Also not sure how to iterate over the 50 "snapshots" to ensure that no Pawn has moved.
If it would just be easier to implement this rule by creating a "counter" which resets when a piece is captured and when a Pawn is moved let me know, I was trying to be efficient and utilize code that was already around.
I think #sawa has the right idea in the comments. You only need to check that the Pawns are in the same position as they were 50 moves ago (since pawns can't move backwards, they can't move in one snapshot and be returned in the next)
board_snapshot.last.delete_if{|_,v| v != "Pawn"} == board_snapshot[-50].delete_if{|_,v| v != "Pawn"}
Similarly (and using your suggested code)
board_snapshotlast.values.count(nil) == board_snapshot[-50].values.count(nil)
Since pieces can't be added to the board, you don't need to worry about a piece disappearing in one move and reappearing in the next move.

How to center a map on something other than the prime meridian in d3.js?

This is a similar question to this one (which was answered for ggplot2), this one (which was answered for R) and is a follow up question to this one (which is still looking for an answer).
How could I use this recent Constrained Zoom plot by Mike Bostock (http://bl.ocks.org/mbostock/4987520) but have the starting position with the pacific in the center rather than Africa?
Like this...
Obviously just adjusting the .translate([0, 0]) values in the code moves the map, but there is no 'wrapping' that would allow the map to be presented as above.
I am convinced that there must be a simple way to accomplish this, as it seems like a fundamental capability, I just can't see or find a solution.
OK, The answers was pretty obvious in the end and many thanks to the guys at Hashbang whose post set me on the right path.
The problem I was having was assuming that I needed to use the .translate() function to shift the map to the correct location, when in fact the .translate() function just moves the points on the returned map. So in other words it literally translates what you have to another location (duh!).
What I should have done is use the .rotate function to rotate the map about its longitude by using the function like so;
var projection = d3.geo.mercator()
.translate([0, 0])
.scale(width)
.rotate([-180,0]);
This simply wraps the map around and gives full control as desired.
A fully functioning example is here.

Image-processing basics

I have to do some image processing but I don't know where to start. My problem is as follows :-
I have a 2D fiber image (attached with this post), in which the fiber edges are denoted by white color and the inside of the fiber is black. I want to choose any black pixel inside the fiber, and travel from it along the length of the fiber. This will involve comparing the contrast with the surrounding pixels and then travelling in the desired direction. My main aim is to find the length of the fiber
So can someone please tell me atleast where to start? I have made a rough algorithm in my mind on how to approach my problem but I don't know even which software/library to use.
Regards
Adi
EDIT1 - Instead of OpenCV, I started using MATLAB since I found it much easier. I applied the Hough Transform and then Houghpeaks function with max no. of peaks = 100 so that all fibers are included. After that I got the following image. How do I find the length now?
EDIT2 - I found a research article on how to calculate length using Hough Transform but I'm not able to implement it in MATLAB. Someone please help
If your images are all as clean as the one you posted, it's quite an easy problem.
The very first technique I'd try is using a Hough Transform to estimate the line parameters, and there is a good implementation of the algorithm in OpenCV. After you have them, you can estimate their length any way you want, based on whatever other constraints you have.
Problem is two-fold as I see it:
1) locate start and end point from your starting position.
2) decide length between start and end points
Since I don't know your input data I assume it's pixel data with a 0..1 data on each pixel representing it's "whiteness".
In order to find end points I would do some kind of WALKER/AI that tries to walk in different locations, knowing original pos and last traversed direction then continuing along that route until "forward arc" is all white. This assumes fiber is somewhat straight (is it?).
Once you got start and end points you can input these into a a* path finding algorithm and give black pixels a low value and white very high. Then find shortest distance between start and end point, that is the length of the fiber.
Kinda hard to give more detail since I have no idea what techniques you gonna use and some example input data.
Assumptions:
-This image can be considered a binary image where there are only 0s(black) and 1s(white).
-all the fibers are straight and their starting and ending points are on borders.
-we can come up with a limit for thickness in fiber(thickness of white lines).
Under these assumptions:
start scanning the image border(start from wherever you want in whichever direction you want...just be consistent) until you encounter with the first white pixel.At this point your program will understand that this is definitely a starting point. By knowing this, you will gather all the white pixels until you reach a certain limit(or a threshold). The idea here is, if there is a fiber,you will get the angle between the fiber and the border the starting point is on...of course the more pixels you get(the inner you get)the surer you will be in the end. This is the trickiest part. after somehow ending up with a line...you need to calculate the angle(basic trigonometry). Since you know the starting point, the width/height of the image and the angle(or cos/sin of those) you will have the exact coordinate of the end point. Be advised...the exactness here is not really what you might have understood because we may(the thing is we will) have calculation errors in cos/sin values. So you need to hold the threshold as long as possible. So your end point will not be a point actually but rather an area indicating possibility that the ending point is somewhere inside that area. The rest is just simple maths.
Obviously you can put too much detail in this method like checking the both white lines that makes the fiber and deciding which one is longer or you can allow some margin for error since those lines will not be straight properly...this is where a conceptual thickness comes to the stage etc.
Programming:
C# has nice stuff and easy for you to use...I'll put some code here...
newBitmap = new Bitmap(openFileDialog1.FileName);
for (int x = 0; x < newBitmap.Width; x++)
{
for (int y = 0; y < newBitmap.Height; y++)
{
Color originalColor = newBitmap.GetPixel(x, y);//gets the pixel value...
//things go here...
}
}
you'll get the image from a openfiledialog and bitmap the image. inside the nested for loop this code scans the image left-to-right however you can change this...
Since you know C++ and C, I would recommend OpenCV
. It is open-source so if you don't trust anyone like me, you won't have a problem ;). Also if you want to use C# like #VictorS. Mentioned I would use EmguCV which is the C# equivilant of OpenCV. Tutorials for OpenCV are included and for EmguCV can be found on their website. Hope this helps!
Download and install the latest version of 3Dslicer,
Load your data and go the the package>EM segmenter without Atlas>
Choose your anatomical tree in 2 different labels, the back one which is your purpose, the white edges.
The choose the whole 2D image as your ROI and click on segment.
Here is the result, I labeled the edges in green and the black area in white
You can modify your tree and change the structures you define.
You can give more samples to your segmentation to make it more accurate.

Chart optimization: More than million points

I have custom control - chart with size, for example, 300x300 pixels and more than one million points (maybe less) in it. And its clear that now he works very slowly. I am searching for algoritm which will show only few points with minimal visual difference.
I have a link to the component which have functionallity exactly what i need
(2 million points demo):
I will be grateful for any matherials, links or thoughts how to realize such functionallity.
If I understand your question correctly, then you are looking to plot a graph of a dataset where you have ~1M points, but the chart's horizontal resolution is much smaller? If so, you can down-sample your dataset to get about the number of available x values. If your data is sorted in equal intervals, you can extract every N'th point and plot it. Choose N such that the number of points is, say, double the resolution (in this case, N=2000 will give you 500 points to display).
If the intervals are very different from eachother (not regularly spaced), you can approximate your graph with a polynomial, or spline or any other method that fits, and then interpolate 300-600 points from that approximation.
EDIT:
Depending on the nature of the data, you may end up with aliasing artifacts when you simply sample every N't point. There are probably better methods for coping with this problem, but again - it depends on what exactly you want to plot.
You could always buy the control - it is for sale!
John-Daniel Trask (Co-founder of Mindscape ;-)

Resources