views:

677

answers:

5

Hi, I would like to write a C# program that generates a 2D image from a rendered 3D object(s) by "slicing" the 3D object or through a cut-plane. The desired output of the 2D image should be data that can be displayed using a CAD. For example:

A 3D image is defined by its vertices, these vertices is contained within Point3DList(). A method is then called taking Point3DList as its parameter e.g: Cut2D(Point3DList).The method then generates the 2D vertices and saved it inside Point2DList() and these vertices can be read through a CAD program which display it in 2D form.

My question therefore is whether there is a previous implementation of this in C#(.NET compatible) or is there any suggestion on third-party components/algorithms to solve this problem.

Thanks in advance.

A: 

This is more opengl specific rather than c# specific, but what i'd do:

Rotate and transform by a 3d matrix, so that the 'slice' you want is 1 metre in 'front' of the camera.

Then set the near and far horizon limits to 1m and 1.001m, respectively.

-update- Are you even using opengl? If not, you could perform your matrix arithmetic yourself somehow.

Chris
Hey Chris,This is a cool idea, but it has a couple of minor wrinkles. I'm sure Chris is aware of them, but just for clarity:1) The output from this technique would presumably be the output from then rendering the model. This gives a bitmap, which isn't exactly what the question asks for.2) The finite size of the slice from 1m to 1.001m means that features on your 3D object at 1.0005m would be included in the cross-section, when ideally they shouldn't be. Not a biggie, but irks the geometric purist in me.Can these (especially the first one) be fixed? Hugs!
Tartley
The best option, with this approach, would be if one of the two edges is a <=. In that case, you can set the near and far horizon limits to 1m and get exactly and only the correct plane. There would be no way for anything else to fit. This assumes that near and far limits will correctly render the exact point where they are equal.
TheJacobTaylor
A: 

It sounds like you want to get the 2D representation of the points of intersection of a plane with a three-dimensional surface or object. While I don't know the algorithm to produce such a thing off hand (I have done very little with 3D modeling applications), I think that is what you are asking about.

I encountered such an algorithm a number of years ago in either a Graphics Gems or GPU Gems or similar book. I could not find anything through a few Bing searches, but hopefully this will give you some ideas.

jrista
+2  A: 

You pose an interesting question, in part, by not including a full definition of a 3D shape. You need to specify either the vertices and edges, or an algorithm to obtain the edges from the vertex list. Since an algorithm to obtain the edges from the vertex list devolves into specifying the vertices and edges, I will only cover that case here. My description also works best when the vertices and edges are transformed into a list of flat polygons. To break a vertex list down into polygons, you have to find cycles in the undirected graph that is created by the vertices and edges. For a triangular polygon with vertices A, B, and C you will end up with edges AB, BC, and AC.

The easiest algorithm that I can think of is:

  1. Transform all points so that your 2D plane where the Z axis is 0. (rotate, twist, and move as required to transform the desired 2D plane to line up with the XY plane where Z=0).

  2. For each flat polygon:
    a. For each edge, check to see if the vertices have opposite sign on the Z axis (or if one is 0). If Z0 * Z1 <= 0 then this is the case
    b. Use the definition of a line and solve for the point where Z=0. This will give you the X,Y of the intersection.
    c. You now have a dot, line, or polygon that represents the intersection of your the original flat polygon from step 1 intersecting the 2D plane.
    d. Fill in the polygon formed by the shapes (if desired). If your 2D rendering package will not create a polygon from the list of vertices,you need to start rendering pixels using scanlines.

Each of the individual algorithms should be in "Algorithms in C" or similar.

Graphics programs can be quite rewarding when they start to work.

Have Fun,

Jacob

TheJacobTaylor
Hey,I think this would work, but:1) In the first step where you say the 'Z axis', I think you mean something like 'the XY plane'2) I'm hazy on the middle part. After you detect all the edges which cross from +ve Z to -ve Z in step 3, you presumably find the point at which the edge crosses z==0. You then have a collection of such points. How do you tell what order to join up these points to make your 2D line in the 4th step? I think I'm asking how to you determine the 'adjacent edges' you mention in the 4th step.3) Do you want to consider numbering the steps for ease of discussion?Thanks!
Tartley
1: yes, I was talking about the XY plane where Z=0. I have tweaked the wording. It is the plane where Z=0.2. Yeah, missed writing that down.4. Once you have the location where any edges of a polygon intersect with the plane, you can fill them in. Since this is a single polygon, all edges are connected by the (assumed) flat face of the polygon. Any open space contained within edges can be filled in as well. I will tweak this description and number the steps. Thanks for your excellent feedback! +1
TheJacobTaylor
A: 

if its a 3d texture cant you just specify 3d tex coords (into the texture) for each vertex of a quad? wouldnt that auto-interpolate the texels?

fusi
A: 

If you are looking for a 3rd party implementation, maybe you should explore Coin3d. Capable of such things as you require, though I am not sure of its exact database format or input requirements. I find your description lacking in that you do not specify the direction from which you want to project the 3d image on to a 2d plane.

Shaihi