DXF2TRI DXF polyline/polygon to triangle converter by Tim Riley Aug 1993 based on POLY2TRI - a polygon to triangle converter by Steve Anger Jan 1993 This program is commited to the public domain. Feel free to use all or part of this code in your own programs. This is a utility that extracts polyline shapes from a DXF file and breaks them down into individual triangles. The polylines are assumed to be closed, planar (2D) polygons. This program only deals with POLYLINE entities and ignores all other entities in the DXF file. All properties (color, line type) are ignored. The input DXF file consists of one or more polylines with no more than 2000 vertices. This limit is rather arbitrary and can be changed if necessary by changing POLYMAX. The program uses the following syntax: DXF2TRI infile[.DXF] [outfile[.RAW]] [-e] [-b] with two options: -e: extrude the object one unit in the z direction (use SCALE in the scene descriptor file to modify this). The extrusion is only applied to the edge of the object, not to the in- dividual triangles. -b: generate a back to the extruded object. This is useful if the back will be visible (ex: in a reflection). This option can be used without the -e option for whatever reason. The output file consists of one triangle per line in the following format: x1 y1 z1 x2 y2 z2 x3 y3 z3 and can be fed directly into RAW2POV to create an INC file with calculated bounding shapes (which speeds up tracing) and a POV file which gives limits of the total object to aid in positioning. RAW2POV converts some triangles (the extrusions) to smooth traingles, which gives a nice look to the sides. NOTE: the resultant object is a shell, not a solid, so take this into account when applying textures, particularly wood and marble. Also, the object won't work with CSG. So much for the boilerplate. The reason this came about was the result of a quest for ray-tracing's holy grail: neat-o text in a scene. I struggled along for a while until I came across Steve Anger's POLY2TRI program. The seas parted, the clouds lifted and my sinuses cleared. Thanks Steve. This archive should contain the following: DXF2TRI.C The source DXF2TRI.EXE The executable, compiled with Borland C++ 2.1, large memory model (if I remember correctly). No extenders, no memory tricks, so you don't have to worry about EMM386 and it's ilk and it runs fine from within Windows. DXF2TRI.DOC Duh, guess. TEST.JPG A sample 640x480 image. It took 9m19s to trace on a HP9000/710 file server (UNIX, with everybody else doing their thing as well). To keep things straight forward, here is how I put text in a scene (NOTE: I am assuming a reasonable knowledge of CorelDraw. This method should be possible with other Windows draw (NOT paint) programs such as Micrographix, Adobe Draw (Designer?) and others, none of which I have any experience with, assuming they have the following capabilities: * can export a DXF file * converts curves to line segments (POLYLINES) otherwise, you're out of luck): 1. Use Corel (4.0) to generate the initial text. Overall size and position is not important since you will scale and position it later on. Line properties (type, thickness or color) are irrelevant since they will be ignored, as well as fill properties. 2. Dealing with holes: re-entrant (concave) portions of a letter appear to convert with no problems (the m and & of the test image seem to prove this). Holes are another matter. In the simplest view, they are another object/curve. Since the objects will be converted to 2D triangles, CSG cannot be used to subtract them from the main object. The easiest solution I have found is to make all holes part of the main object's outline. This is accomplished in two steps: a. Convert the letter from text to a curve. Do this after you have done all of the transformations you care to do on your text. You cannot convert from curves back to text (well you can, in a way, but font creation is beyond to scope of this babble). b. Using the node edit tool, cut a channel of zero width between the hole and the outside of the curve/letter. This can be done anywhere. As long as the sides of the channel touch, there will be no visible effect on the finished product. For example: Turn: into: *** *** *** *** *** *** ** ** ** ** ** *** ** ** *** ** * ** ** * * ** ** * * ** ** * * ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** * * ** ** * * ** ** * * ** ** * ** *** ** ** * * ** ** ** ** * * ** *** *** **** **** *** * * These segements overlap --->| |<--- If you want to be artistic, you can cut the channels in obvious places, where a curve intersects a straight segment, for example. I broke the ampersand in the following places: *** * * * * * \ \* * * * * * * * * * * * and then removed the unnecessary nodes on the straight line segments. This has the added effect of reducing the number of vertices of the polyline, as well as the number of triangles and the complexity of the final object. 3. Export the text as a DXF file. The Corel 4.0 DXF filter converts all curves to line segments. It allows you to choose the number of line segments a curve is converted to. I chose 32 (the minimum value) to minimize the complexity of the object. There is no visible degradation to the object when rendered at 1024x768 (and, I suspect, greater). Individual letters may be exported to separate DXF files if you want to texture them separately. The COREL-DXF-RAW-POV conversion chain retains the relative position of each letter, but they become separate objects and inter-letter spacing becomes a problem unless you first combine them as a composite in the scene file before performing any transformation. If you are going to texture the text uniformly, it is easier to export the text to a single DXF file and treat it as a single object. 4. Run DXF2TRI on the DXF file to generate a RAW file. If you want to extrude the letters, use the -e switch. If the back of the letters will be visible (due to reflections or camera position), use the -b switch to generate solid backs to the letters. 5. Run RAW2POV on the RAW file. RAW2POV will generate a POV file giving the physical limits of the object(s) and an INC file consisting of triangles, smooth triangles and bounding shapes. The rest is up to you. No, its not overly easy or straight forward, but it works well and the results aren't bad. Before anyone has a chance to ask or comment, no it won't bevel, but I am thinking of it. A straight bevel should be possible, but a curved (routed) bevel will take some doing, since CSG won't work with triangles. Don't let me stop anyone else from working on the problem, since I have a lot of other things to keep me busy. I assume NO responsibility for this program or its effects, but if you need to contact me, for whatever reason, I check in occasionally to The Graphics Alternative and my Internet address is: triley@its.bldrdoc.gov. Doo dah.