How to find all distinct closed paths from a group of points - actionscript-3

Is it possible to find out all possible closed paths with intersection. I have a set of points in an array. I need to process the points in the array and plot the closed areas.
This is building designing project. I know all the points (Shown as green dots in the images attached) x and y values. Here i need to know each rooms separately as shown in image2(room are labeled from 1-11). How can i find out the room's boundary points.
The points may vary this is just a sample.
http://demo.enfintech.net/BuildingImage/1.jpg
http://demo.enfintech.net/BuildingImage/2.jpg

What I suggest is that since all your connections are either vertical or horizontal, you can do the following:
Extend all connection lines in both directions so that you end up with a matrix of small cells.
Initially your number of rooms shall be the number of these small cells. Number them accordingly.
Now for each cell, check whether the boundary with its neighbour exists or not (whether the boundary line is an actual connection line or just an extrapolation).
If it is just an extrapolation, then join these cells into a single cell.
Keep repeating this for all the cells until all extrapolated lines disappear.

Related

Confused about automatic instances and moving sprites in AS3

I created big Sprite called Space and big number of smaller sprites and keep them in array Cells.
Question 1.
When I trace Cell's names (like Cells[i].name) - it shows 'instance' and and even numbers 2,4,6,...
When I trace children of Space names for (...Space.numChildren; i++){trace(Space.getChildAt(i).name) it shows odd numbers 'instance1' 'instance3','instance5'
The question is why there are two sets of different sprites and with which one I need to work further - to change coordinates, colors and so on. How the two related to each other?
Question 2.
Both sets show x and y coordinates of all cells set to 0 although when I created them I set it to different values and I see them placed correctly on screen.
When I move (by mouse) one of the cells, I want to move the same way all of the group of cells w/o changing relative locations between them. I could do it with the second set of instances only, but strange way. I don't set new coordinates as one would expect - c.x += dx; c.y +=dy,
but c.x =dx; c.y = dy;
I need clarification on that.
Thank you all cared to answer.
I already figured it out. The problem was that I did not understand that in array I saved addresses of my as3 objects returned by new operator, but addresses of graphic objects has to be saved inside the class objects and I had to use them to access the images.

Anchor Boxes in YOLO : How are they decided

I have gone through a couple of YOLO tutorials but I am finding it some what hard to figure if the Anchor boxes for each cell the image is to be divided into is predetermined. In one of the guides I went through, The image was divided into 13x13 cells and it stated each cell predicts 5 anchor boxes(bigger than it, ok here's my first problem because it also says it would first detect what object is present in the small cell before the prediction of the boxes).
How can the small cell predict anchor boxes for an object bigger than it. Also it's said that each cell classifies before predicting its anchor boxes how can the small cell classify the right object in it without querying neighbouring cells if only a small part of the object falls within the cell
E.g. say one of the 13 cells contains only the white pocket part of a man wearing a T-shirt how can that cell classify correctly that a man is present without being linked to its neighbouring cells? with a normal CNN when trying to localize a single object I know the bounding box prediction relates to the whole image so at least I can say the network has an idea of what's going on everywhere on the image before deciding where the box should be.
PS: What I currently think of how the YOLO works is basically each cell is assigned predetermined anchor boxes with a classifier at each end before the boxes with the highest scores for each class is then selected but I am sure it doesn't add up somewhere.
UPDATE: Made a mistake with this question, it should have been about how regular bounding boxes were decided rather than anchor/prior boxes. So I am marking #craq's answer as correct because that's how anchor boxes are decided according to the YOLO v2 paper
I think there are two questions here. Firstly, the one in the title, asking where the anchors come from. Secondly, how anchors are assigned to objects. I'll try to answer both.
Anchors are determined by a k-means procedure, looking at all the bounding boxes in your dataset. If you're looking at vehicles, the ones you see from the side will have an aspect ratio of about 2:1 (width = 2*height). The ones viewed from in front will be roughly square, 1:1. If your dataset includes people, the aspect ratio might be 1:3. Foreground objects will be large, background objects will be small. The k-means routine will figure out a selection of anchors that represent your dataset. k=5 for yolov3, but there are different numbers of anchors for each YOLO version.
It's useful to have anchors that represent your dataset, because YOLO learns how to make small adjustments to the anchor boxes in order to create an accurate bounding box for your object. YOLO can learn small adjustments better/easier than large ones.
The assignment problem is trickier. As I understand it, part of the training process is for YOLO to learn which anchors to use for which object. So the "assignment" isn't deterministic like it might be for the Hungarian algorithm. Because of this, in general, multiple anchors will detect each object, and you need to do non-max-suppression afterwards in order to pick the "best" one (i.e. highest confidence).
There are a couple of points that I needed to understand before I came to grips with anchors:
Anchors can be any size, so they can extend beyond the boundaries of
the 13x13 grid cells. They have to be, in order to detect large
objects.
Anchors only enter in the final layers of YOLO. YOLO's neural network makes 13x13x5=845 predictions (assuming a 13x13 grid and 5 anchors). The predictions are interpreted as offsets to anchors from which to calculate a bounding box. (The predictions also include a confidence/objectness score and a class label.)
YOLO's loss function compares each object in the ground truth with one anchor. It picks the anchor (before any offsets) with highest IoU compared to the ground truth. Then the predictions are added as offsets to the anchor. All other anchors are designated as background.
If anchors which have been assigned to objects have high IoU, their loss is small. Anchors which have not been assigned to objects should predict background by setting confidence close to zero. The final loss function is a combination from all anchors. Since YOLO tries to minimise its overall loss function, the anchor closest to ground truth gets trained to recognise the object, and the other anchors get trained to ignore it.
The following pages helped my understanding of YOLO's anchors:
https://medium.com/#vivek.yadav/part-1-generating-anchor-boxes-for-yolo-like-network-for-vehicle-detection-using-kitti-dataset-b2fe033e5807
https://github.com/pjreddie/darknet/issues/568
I think that your statement about the number of predictions of the network could be misleading. Assuming a 13 x 13 grid and 5 anchor boxes the output of the network has, as I understand it, the following shape: 13 x 13 x 5 x (2+2+nbOfClasses)
13 x 13: the grid
x 5: the anchors
x (2+2+nbOfClasses): (x, y)-coordinates of the center of the bounding box (in the coordinate system of each cell), (h, w)-deviation of the bounding box (deviation to the prior anchor boxes) and a softmax activated class vector indicating a probability for each class.
If you want to have more information about the determination of the anchor priors you can take a look at the original paper in the arxiv: https://arxiv.org/pdf/1612.08242.pdf.

SSRS : Overlapping Line in Line Chart

I have created a line chart which has the date as X-axis and Y-axis as calculated median value and its grouped according to "FileName". Problem is that some of the "FileName" has same median values which makes line overlap thus not able to see all the lines. Attached image shows only 5 lines but there are total 10 lines. After running query I found out other 5 has 50 as the median which makes it overlap with one of the line.
I tried using transparency and secondary axis but wasn't able to achieve the desired result. Is there any other solution to try out ? Thanks!
This is more of a data presentation issue than something specific to SSRS. If you are stuck on using a line chart, then I've only used two options:
1) Increment lines to different widths. For example, in a chart with 3 lines, the width is set to 5,3,1 pts.
2) Change the values insignificantly to offset the lines. Obviously this depends on the data being visualized as shifting the line slight (multiply by 0.1) may be allowable or highly discouraged depending on your situation.
Trying to do either option with 10 lines (and up to 5+ stacking) is not going to be very good.
I think Viking is right and you might want to check out other visualization options. Grouped column charts perhaps or just split your chart into multiple charts on the page (i.e. four separate trend charts)

Is it possible to layout this HTML table + customized dendrogram using MATLAB's publish command?

I would like to put together a more publication-worthy (and more easily produced) version of the diagram that I've hacked together below:
Basically, it's a two part illustration with an HTML table on the left (showing different cluster assignments from varying parameters) and a modified dengrogram plot on the right.
I'm guessing it's apparent from the snapshot what I'm trying to add to the plot… Basically it could be a set of horizontal, stacked bars that show how the different parameters grouped the observations into different clusters. I used brackets with the cluster numbers when drawing these by hand, but anything that underlines the right observation numbers would be acceptable.
I realize there are two parts to this question: How to get the two-up layout when one of the panels is HTML instead of a figure, and how to modify the plot. [If only one part gets answered here, I can ask for the other in a separate question.]
You can label your plot using line and text. You need to set the clipping property to off in order for lines to show up outside of your axes. Here's an example:
data = [1,3,4,5; 2,6,7,8; 9,3,7,4;3,8,5,2];
tree = linkage(data,'average');
figure()
dendrogram(tree)
set(gca,'Position',[0.13, 0.3,0.775, 0.65])
h1 = line([1,1],[4,4.8],'Color','k');
h2 = line([1,3],[4,4],'Color','k');
h3 = line([3,3],[4,4.8],'Color','k');
set(h1,'Clipping','off')
set(h2,'Clipping','off')
set(h3,'Clipping','off')
ht = text(1.7,3.9,'Label');

Finding a free area in the stage

I'm drawing rectangles at random positions on the stage, and I don't want them to overlap.
So for each rectangle, I need to find a blank area to place it.
I've thought about trying a random position, verify if it is free with
private function containsRect(r:Rectangle):Boolean {
var free:Boolean = true;
for (var i:int = 0; i < numChildren; i++)
free &&= getChildAt(i).getBounds(this).containsRect(r);
return free;
}
and in case it returns false, to try with another random position.
The problem is that if there is no free space, I'll be stuck trying random positions forever.
There is an elegant solution to this?
Let S be the area of the stage. Let A be the area of the smallest rectangle we want to draw. Let N = S/A
One possible deterministic approach:
When you draw a rectangle on an empty stage, this divides the stage into at most 4 regions that can fit your next rectangle. When you draw your next rectangle, one or two regions are split into at most 4 sub-regions (each) that can fit a rectangle, etc. You will never create more than N regions, where S is the area of your stage, and A is the area of your smallest rectangle. Keep a list of regions (unsorted is fine), each represented by its four corner points, and each labeled with its area, and use weighted-by-area reservoir sampling with a reservoir size of 1 to select a region with probability proportional to its area in at most one pass through the list. Then place a rectangle at a random location in that region. (Select a random point from bottom left portion of the region that allows you to draw a rectangle with that point as its bottom left corner without hitting the top or right wall.)
If you are not starting from a blank stage then just build your list of available regions in O(N) (by re-drawing all the existing rectangles on a blank stage in any order, for example) before searching for your first point to draw a new rectangle.
Note: You can change your reservoir size to k to select the next k rectangles all in one step.
Note 2: You could alternatively store available regions in a tree with each edge weight equaling the sum of areas of the regions in the sub-tree over the area of the stage. Then to select a region in O(logN) we recursively select the root with probability area of root region / S, or each subtree with probability edge weight / S. Updating weights when re-balancing the tree will be annoying, though.
Runtime: O(N)
Space: O(N)
One possible randomized approach:
Select a point at random on the stage. If you can draw one or more rectangles that contain the point (not just one that has the point as its bottom left corner), then return a randomly positioned rectangle that contains the point. It is possible to position the rectangle without bias with some subtleties, but I will leave this to you.
At worst there is one space exactly big enough for our rectangle and the rest of the stage is filled. So this approach succeeds with probability > 1/N, or fails with probability < 1-1/N. Repeat N times. We now fail with probability < (1-1/N)^N < 1/e. By fail we mean that there is a space for our rectangle, but we did not find it. By succeed we mean we found a space if one existed. To achieve a reasonable probability of success we repeat either Nlog(N) times for 1/N probability of failure, or N² times for 1/e^N probability of failure.
Summary: Try random points until we find a space, stopping after NlogN (or N²) tries, in which case we can be confident that no space exists.
Runtime: O(NlogN) for high probability of success, O(N²) for very high probability of success
Space: O(1)
You can simplify things with a transformation. If you're looking for a valid place to put your LxH rectangle, you can instead grow all of the previous rectangles L units to the right, and H units down, and then search for a single point that doesn't intersect any of those. This point will be the lower-right corner of a valid place to put your new rectangle.
Next apply a scan-line sweep algorithm to find areas not covered by any rectangle. If you want a uniform distribution, you should choose a random y-coordinate (assuming you sweep down) weighted by free area distribution. Then choose a random x-coordinate uniformly from the open segments in the scan line you've selected.
I'm not sure how elegant this would be, but you could set up a maximum number of attempts. Maybe 100?
Sure you might still have some space available, but you could trigger the "finish" event anyway. It would be like when tween libraries snap an object to the destination point just because it's "close enough".
HTH
One possible check you could make to determine if there was enough space, would be to check how much area the current set of rectangels are taking up. If the amount of area left over is less than the area of the new rectangle then you can immediately give up and bail out. I don't know what information you have available to you, or whether the rectangles are being laid down in a regular pattern but if so you may be able to vary the check to see if there is obviously not enough space available.
This may not be the most appropriate method for you, but it was the first thing that popped into my head!
Assuming you define the dimensions of the rectangle before trying to draw it, I think something like this might work:
Establish a grid of possible centre points across the stage for the candidate rectangle. So for a 6x4 rectangle your first point would be at (3, 2), then (3 + 6 * x, 2 + 4 * y). If you can draw a rectangle between the four adjacent points then a possible space exists.
for (x = 0, x < stage.size / rect.width - 1, x++)
for (y = 0, y < stage.size / rect.height - 1, y++)
if can_draw_rectangle_at([x,y], [x+rect.width, y+rect.height])
return true;
This doesn't tell you where you can draw it (although it should be possible to build a list of the possible drawing areas), just that you can.
I think that the only efficient way to do this with what you have is to maintain a 2D boolean array of open locations. Have the array of sufficient size such that the drawing positions still appear random.
When you draw a new rectangle, zero out the corresponding rectangular piece of the array. Then checking for a free area is constant^H^H^H^H^H^H^H time. Oops, that means a lookup is O(nm) time, where n is the length, m is the width. There must be a range based solution, argh.
Edit2: Apparently the answer is here but in my opinion this might be a bit much to implement on Actionscript, especially if you are not keen on the geometry.
Here's the algorithm I'd use
Put down N number of random points, where N is the number of rectangles you want
iteratively increase the dimensions of rectangles created at each point N until they touch another rectangle.
You can constrain the way that the initial points are put down if you want to have a minimum allowable rectangle size.
If you want all the space covered with rectangles, you can then incrementally add random points to the remaining "free" space until there is no area left uncovered.