开发者

2d projection of Graphics3D object

开发者 https://www.devze.com 2023-03-20 23:53 出处:网络
I have a Graphics3D object. I want to export it as a bitmap, and to calculate the bitmap-pixel coordinates of certain 3D points.

I have a Graphics3D object. I want to export it as a bitmap, and to calculate the bitmap-pixel coordinates of certain 3D points.

Mathematica obviously does a projection from 3D objects to 2D pixel coordinates when it draws the 3D graphic. How can I find out what this projection is?

I'd rather avoid doing lots of tricky geometrical calculations based on ViewVector and ViewAng开发者_如何学JAVAle and ImageSize and BoundingBox. Is there a shortcut?

Damon.


You could GeometricTransform using the option "Transformation" -> "Perspective". Suppose your projected chess board looks something like this

img = Image@
  Plot3D[0, {x, -1, 1}, {y, -1, 1}, Mesh -> 7, 
   MeshShading -> {{Black, White}, {White, Black}}, Boxed -> False, 
   AxesEdge -> {{-1, -1}, {-1, -1}, None}, AxesOrigin -> {-1, -1, 0}]

2d projection of Graphics3D object

To find the projection you will need the coordinates of at least 4 control points in img for which you know the {x,y}-coordinates. There probably are methods to have Mathematica find these coordinates automatically but you can select them manually by right-clicking on img and choosing "Get Coordinates". Click on the control points of your choice (in this case I chose the 4 corners of the chessboard) and copy/paste their coordinates to a new line. You should get something like

controls = {{13.5`, 151.5`}, {235.5`, 68.5`}, 
   {332.5`, 206.5`}, {139.5`, 262.5`}};

The projection function and matrix then become

transform = FindGeometricTransform[controls,
   {{0, 0}, {8, 0}, {8, 8}, {0, 8}}, 
   "Transformation" -> "Perspective"][[2]]
transfMat = TranformationMatrix[transform]

Note that I chose the chessboard to be an 8x8 square centred at {4,4}, but you can choose any square.

A point {x,y} on the chessboard will now correspond to the point in img with pixel coordinates transform[{x,y}] or, using the projection matrix, (transfMat[[{1,2}]].{x,y,1})/(transfMat[[3]].{x,y,1}). So for example, to put a marker on D6, which would be at position {x,y}={4-1/2,6-1/2} in my 8x8 square, you could do something like

ImageCompose[img, Image[BoxMatrix[2]], Round[transform[{4 - 1/2, 6 - 1/2}]]]

2d projection of Graphics3D object


When you render your Graphics3D object, you can specify the ViewMatrix option. You can set the transformation (such as a rotation) and then the projection.

This way you can use your explicit knowledge of the projection used to calculate the correct planar coordinates.

0

精彩评论

暂无评论...
验证码 换一张
取 消