Algorithm for filling container with elements, knowing their vertical position - algorithm

My problem is a bit similar to this topic :
Fit elements into box
Although in my case I have a container of some size, and some predefined elements with know vertical positions. The elements are to be allocated inside the container without overlapping, and taking full available width. If two or more would overlap, they should have the same width.
Anyone knows any algorithms that may be helpful here ?

Well one way would be to model this problem as a convex optimization problem and then use a solver to solve it (tons of solvers are available online). You can find more info about this approach in floor planning chapter (page 438) of Boyds convex optimization book. They have a matlab implementation of it in the examples folder of cvx software. I hope I haven't made it more complicated, it would be interesting to see other solutions.

Related

Subdividing a polygon into boxes of varying size

I would like to be pointed to information / resources for creating algorithms like the one illustrated on this blog, which is a subdivision of a polygon (in my case a voronoi cell) into several boxes of varying size:
http://procworld.blogspot.nl/2011/07/city-lots.html
In the comments a paper by among others the author of the blog can be found, however the only formula listed is about candidate location suitability:
http://www.groenewegen.de/delft/thesis-final/ProceduralCityLayoutGeneration-Preprint.pdf
Any language will do, but if examples can be given Javascript is preferred (as it is the language i am currently working with)
A similar question is this one: https://gamedev.stackexchange.com/questions/27055/what-is-an-efficient-packing-algorithm-for-packing-rectangles-into-a-polygon
[edit]: I have found something to start with, but it is not what i was looking for entirely:
http://www2.stetson.edu/~efriedma/squintri/
I have solved my problem in a completely different, easier way.
As i was looking for my problem, it turned out to be a fairly complex one, both measured in difficulty to implement as algorithm (my opinion) and algorithm complexity class(es).
If anyone is having a similar problem, these problems are classified as 'packing problems' in general, with specific problems like the 'pallet loading problem'.
The problem i was interested in, is illustrated at the bottom of this page:
https://www.ime.usp.br/~egbirgin/packing/
and a paper about this problem, with algorithm descriptions of how to solve the packing problem for convex polygons and curved shapes:
http://www.ime.usp.br/~egbirgin/publications/bmnr.pdf
Some more information on these kinds of problems:
http://lagrange.ime.usp.br/~lobato/utdc/
http://mathworld.wolfram.com/SquarePacking.html

Is there some well-known algorithm which turns user's drawings into smoothed shapes?

My requirements:
A user should be able to draw something by hand. Then after he takes off his pen (or finger) an algorithm smooths and transforms it into some basic shapes.
To get started I want to transform a drawing into a rectangle which resembles the original as much as possible. (Naturally this won't work if the user intentionally draws something else.) Right now I'm calculating an average x and y position, and I'm distinguishing between horizontal and vertical lines. But it's not yet a rectangle but some kind of orthogonal lines.
I wondered if there is some well-known algorithm for that, because I saw it a few times at some touchscreen applications. Do you have some reading tip?
Update: Maybe a pattern recognition algorithm would help me. There are some phones which request the user to draw a pattern to unlock it's keys.
P.S.: I think this question is not related to a particular programming language, but if you're interested, I will build a web application with RaphaelGWT.
The Douglas-Peucker algorithm is used in geography (to simplify a GPS track for instance) I guess it could be used here as well.
Based on your description I guess you're looking for a vectorization algorithm. Here are some pointers that might help you:
https://en.wikipedia.org/wiki/Image_tracing
http://outliner.codeplex.com/ - open source vectorizer of the edges in the raster pictures.
http://code.google.com/p/shapelogic/wiki/vectorization - describes different vectorization algorithm implementations
http://cardhouse.com/computer/vector.htm
There are a lot of resources on vectorization algorithms, I'm sure you'll be able to find something that fits your needs. I don't know how complex these algorithms are to implement them, though,

Fastest method to search for a specified item on an image?

Imagine we have a simple 2D drawing, filled it with lots of non-overlapping circles and only a few stars.
If we are to find all the stars among all these circles, I can think of very few methods. Brute force is one of them. Another one is possibly reduce the image size (to the optimal point where you can still distinguish the objects apart) and then apply brute force and map to the original image. The drawback of brute force is of course, it is very time consuming. I am looking for faster methods, possibly the fastest one.
What is the fastest image processing method to search for the specified item on a simple 2D image?
One typical way of looking for an object in an image is through cross correlation. Basically, you look for the position where the cross-correlation between a mask (the object you're attempting to find) and the image is the highest. That position is the likely location of the object you're trying to find.
For the sake of simplicity, I will refer to the object you're attempting to find as a star, but in general it can be any shape.
Some problems with the above approach:
The size of the mask has to match the size of the star. If you don't know the size of the star, then you will have to try different size masks. Image pyramids are more effective than just iteratively trying different size masks, but still require extra effort.
Similarly, the orientations of the mask and the star have to match. If they don't, the cross-correlation won't work.
For these reasons, the more you know about your problem, the simpler it becomes. This is the reason why people have asked you for more information in the comments. A general purpose solution doesn't really exist, to the best of my knowledge. Maybe someone more knowledgeable can correct me on this.
As you've mentioned, reducing the size of the image will help you reduce the computational time of your approach. In my opinion, it's hardly the core element of a solution -- it's just an optional optimization step.
If the shapes are easy to segment from the background, you might be able to compute distinguishing shape/color descriptors. Depending on your problem you could choose descriptors that are invariant to scale, translation or rotation (e.g. compactness, if it is unique to each shape). I do not know if this will be faster, though.
If you already know the exact shape and have an idea about the size, you might want to have a look at the Generalized Hough Transform, which is basically a formalized description of your "brute force algorithm"
As you list a property that the shapes are not overlapping then I assume an efficient algorithm would be able to
cut out all the shapes by scanning the image in some way (I can imagine relatively efficient and simple algorithm for convex shapes)
when you are left with cut out shapes you could use cross relation misha mentioned
You should describe the problem a bit better
can the shapes be rotated or scaled (or some other transform?)
is the background uniform colour
are the shapes uniform colour
are the shapes filled
Depending on the answer on the above questions you might have more less or more simple solutions.
Also, maybe this article might be interesting.
If the shapes are very regular maybe turning them into vectors could fit your needs nicely, but it might be an overkill, really depends what you want to do later.
Step 1: Thresholding - reduce the image to 1 bit (black or white) if the general image set permits it. [For the type of example you cite, my guess is thresholding would work nicely - leaving enough details to find objects].
Step 2: Optionally do some smoothing/noise removal.
Step 3: Use some clustering approach to gather the foreground objects.
Step 4: Use an appropriate heuristic to identify the objects.
The parameters in steps 1/2 will depend a lot on the type of images as well as experimentation/observation. 3 is usually straightforward if you have worked out 1/2 correctly. 4 will depend very much on the problem (for example, in your case identifying stars - which would depend on what is the actual shape of the stars expected in the images).

Algorithm to determine (x,y) coordinates for rectangles so the area of the surrounding rectangle is minimal?

I hope my title makes sense, but here is what I a trying to do:
I have n rectangles, each with widths of Wn and heights of Hn that I need to arrange so on a two dimensional (x,y) plane the rectangle that they all fit in takes up the least area. I also need to be able to establish what (x,y) corresponds to what rectangle.
I would prefer something in psuedo-code, but can work with many languages.
Thanks for helping.
This is a difficult problem to be solved optimally, however there are some solutions that are not too hard to implement and that represent a good approximations for many uses (e.g. for textures). Try googling for "rect(angle) packing" ... you will find a lot of code that solves the problem.
Sounds NP-complete to me. Kinda like the knapsack problem. Which means there is no real solution. Only good approximations.
It's a variant on 2D bin packing. The fact that your container is flexible, allows for more optimization as well making it more challenging (as in difficult). Drools Planner (open source java) can handle this. At least one implementation (see user mailing list) with Drools Planner exists (not open source). If you need an open source example to start from, probably the cloud balance in drools-planner-examples is a good example to start from.
For an overview of the algorithms you can use, see my answer on a similar question.
Your problem is known as the 2D packing problem. Even the 1D problem is NP-hard. See here for a good article on some approaches along with sample C# code.
Also, see the following questions:
How can I programmatically determine how to fit smaller boxes into a larger package?
Where can I find open source 2d bin packing algorithms?

Position elements without overlap

I have a number of rectangular elements that I want to position in a 2D space. I calculate an ideal position for each element. Now my problem is that many elements overlap as very often the ideal positions are concentrated in one region. I want to avoid overlap as much as possible (doesn't have to be perfect, though). How can I do this?
I've heard physics simulations are suitable for this - is that correct? And can anyone provide an example/tutorial?
By the way: I'm using XNA, if you know any .NET library that does exactly this job - tell me!
One way the physics engine can be used:
Put positive electric charges (or some kind of repulsive force) on each rectangles and simulate the forces and movements. Also, as Eyal was kind enough to point out, you also need some attractive forces to keep them from drifting away. This can be modelled by springs (again as Eyal points out). They will hopefully end up in some sort of equilibrium which might involve non-overlapping rectangles.
I believe similar ideas (force based heuristics) are used in determining nice looking layouts of graphs (the nodes and edges one).
Disclaimer: I haven't used this myself.
Hope that helps!
Box2D is a widly used (free) physics library that can achieve the needed task: Link
The algorithm that you are looking for is linear interpolation. XNA has its own lerp function.

Resources