A Tepid Raster To Vector Algorithm That Probably Doesn't Really Do All The Things You Want It To Do So That's Your Tough Luck.
2006sep: An email corrects a problem with the code. Jump to the end of this article for details.
This article details a bitmap to vector algorithm I created for a larger program (not discussed here). This algorithm may already exist in a purer state elsewhere, it may be flawed, incomplete, or otherwise mutated. I make no claim to its functionality or efficiency; other algorithms probably exist that are better, faster, stronger. If you are stupid enough to put this algorithm into some sort of medical care device without checking the math and it kills someone, it's your problem, not mine. Also this applies to transportation, the military, and anything that could generate a lawsuit with me sitting on the wrong end of the barrel. It's not going to happen, cholly. Your frivolous "wrongful death" suit has been disclaimed.
If you want something that estimates curves, creates splines, etc, then you should definitely go look somewhere else. Maybe I will need to add this in the future, like a year from now. I doubt it.
But, if you are looking for something that encodes a bitmap into a straight-line vector table, then this algorithm will maybe help you.
I wouldn't count on this page being here forever, even though I have no intention of removing it. If you are remotely interested, grab it now and store it on your little hard drive. There's also a note at the end, someone sent in an email correcting a problem. I had heard there was a problem with the algorithm years ago, but I never got around to checking it. So I'm guessing this email fixes that problem. I haven't checked it either.
There are other algorithms for bitmap-to-vector processing. I have not used them because they are either patented or oddly useless. An example of the former, Patent #4,740,904, is available via either the IBM patent server or the U.S. patent server. Perhaps you would like to license this from Mr. Nagle or Autodesk. Perhaps it is more robust for your computational needs. More "macho". I cannot say at this time.
Sample code is included which looks suspiciously like Delphi Pascal with a lot of the variable declarations and frilly shit removed. There is also a full version of the sample code with declarations and all that so you don't have to cut and paste from this page.
The vectors are encoded as an array of records; there are better ways to do this (this is completely obvious, but so I can avoid answering any questions about this, a "better way" and probably the "best" way would be "linked lists"), but with this method it is easier to understand and follow along in your own songbook.
There are three main procedures.
The first is "Create Vector". The bitmap is translated to vector notation as soon as possible. That is, each single bit is converted into four directional vectors, joined as a square.
The second is "Simplify Vector". The vector field is simplified by checking for duplicates and removing the vectors that are lying on top of each other (these would be adjacent bits in the bitmap). This is done by referring to the links to the identical vectors and merging these, much like one would remove an item from a linked list.
The third and last is "Lengthen Vector". The algorithm goes through the modified vector field looking for joined parallel vectors and turns these multiple vectors into a single vector.
Hell, you could probably write your own program now.
We (you and I) will now consider each of these procedures, and their
associated subroutines, in turn.
The bitmap is represented as an array "a" that is MAXSIZEX by MAXSIZEY in dimension.
For each bit in the bitmap that is turned on, you will want to
create a representational square vector for it.
V is declared as an array of records, each representing a vector. Each
record has fields for the starting (sx,sy) and ending (ex,ey) points,
and pointers to both the previous (prev) and next (next) vectors. Each
vector also has a status field indicating whether the vector is alive
(0) or dead (negative). Vnum stands for the the total number of vectors.
Walk through the vector list. For each vector, look at the remainder of the vector list, and if there are any duplicates, remove them.
Equalvectors. Check the current vector to make sure it's not dead. If not, compare the points of the vector. If the vectors are exactly the same, or if the first vector is the exact reverse of the second vector, the vectors are equal.
Equalpoints. Determine if two pairs of points passed are equal.
Removevectors. In Simplifyvector, if the vectors are equal, this procedure is called. Because we have two squares next to each other and we want to remove the common line between them, we need to process each endpoint of the common vector. After this is done, we set each vector's status to "dead".
Removevector. This is the sticky bit of the whole Simplifyvector step. To remove the common vectors, we work around them. For vector mm, we hold the vector just before it (p), and then change p's next vector to be mm2's next vector. Then, we hold mm2's next vector (n), and make n's previous vector to be p. This would be much easier to explain with a diagram, perhaps I'll draw one someday.
Lengthenvector. What we have now is a series of joined vectors, but the
problem is that any line with a length greater than one "unit" on a side
is represented by a series of vectors and not one vector. This procedure
examines where two joined vectors (the current vector and the previous
vector) lie on the plane. If they are in a line,
the link between them is removed and the extraneous vector is set "dead".
2006sep. Here is the email I receievd.
This is the end. You should use the sample code if you are going to cut and paste any of this.
[ home | archive | contact | computer | sample code ]