# 3D graphics: finding optimal view of a scene with constraints

1. Jul 14, 2004

### Polverone

Pardon me if this should go elsewhere, but it seems like a problem with a linear algebra solution.

I have a series of PDB files from a molecular dynamics simulation of modified DNA strands. I use the program Pymol (http://www.pymol.org) to visualize them as a "movie." But now I want to make a real movie - that is, create a series of raster images that I can encode as a video file. I know how to do that with Pymol, by loading a PDB file, adjusting viewing parameters, and rendering/saving a raster image (repeat x1000). The problem is that the molecule shifts and twists over time, so that the region I want to view becomes hard to see unless I make adjustments by rotating the view. When I'm making raster frames I cannot use the interactive mouse controls like I would when viewing on my desktop machine; I must programmatically determine the compensation-rotations to keep my region of interest unobscured and level.

My idea is to pick two carbon atoms from opposite sides of the strand but located on the same level and two on the same side but located on different levels like so:

Imagine a measurement of the vertical distance between the two left green points or the two right green points (I will just choose left or right and stick with it, ultimately), as seen by the viewing camera. I want to rotate the model so that this vertical distance is maximized (constraint 1). Now imagine a measurement of the horizontal distance between the two magenta points as seen by the viewing camera. I want to maximize this distance (constraint 2) with rotation while still honoring constraint 1. I think this constraint should naturally produce a line that appears perfectly horizontal, but if doesn't, consider "the line must appear perfectly horizontal" as an additional constraint. There are multiple "best" views possible, since mirrored views are going to be the same, but the chosen view should be the best view that differs the least from the best view of the previous frame (constraint 3). This is a long-winded way of saying that I want to always be orienting the model so that I have a clear head-on view of these 3 base pairs.

Here's what I have to work with in modifying the view:
Suppose that everything but the rotation matrix will remain static over the course of the animation, but any of these values can be retrieved. I know that these constraints *can* be satisfied, but I'm at a loss how to go about it. I've done only a little with 3D graphics before, and the other systems I used let me use quaternions or Euler angles to control rotations. Here I just have a matrix. I've spent some time reading Computer Graphics: Principles and Practice as well as the comp.graphics.algorithms FAQ, and neither got me very far. I have a feeling that this would be a pretty simple problem for someone well-versed in linear algebra or 3D graphics, but I am not yet that someone and I need to make these animations before next week

I would very much appreciate it if someone can help me with this, keeping in mind that I did once study linear algebra but it was a while ago, and that I am familiar with most graphics terms (if not the actual implementations).

Edit: I botched one of my constraints. The "maximize vertical distance as seen by the camera" constraint doesn't work because of the twisting of the strand. This constraint will lead to terrible viewing angles. Maybe I should have said "as seen by an orthographic camera" or somesuch... along with additional constraints on what sort of rotation is allowed? Meh, this is even trickier than I thought. I always want to view this 3 base pair region head-on, however that can be stated formally. Any suggestions for dealing with my problem are welcome, even if they're "download this great molecular visualization program BlahViz, it'll do what you want out of the box."

Last edited: Jul 14, 2004
2. Jul 14, 2004

### enigma

Staff Emeritus
Let me try some rambling here: hopefully some of this will help you.

If you want to orient the green points so they're vertical, one algorithm is this:

Find the [X,Y,Z] direction vector which points from Green point 1 (GP1) to GP2. We'll call this GP12. Normalize this vector. Then find the rotation matrix to turn GP12 into [0,0,1]. Apply that rotation matrix to all of the points. That will rotate your entire molecule and you'll have the greens aligned N-S, and the screen distance between them will be maximized.

For the two magenta points (direction vector called MP12), you now will have a problem. If a constraint is that MP12 must appear horizontal, then GP12 (although keeping it N-S is still possible) cannot be maximized as viewed from the screen. This is, of course, assuming that MP12 isn't exactly horizontal. If you rotate GP12 about an arbitrary vector normal to both the viewscreen and GP12, then you could get the magentas to line up, but the magnitude of GP12 as viewed from the screen will decrease.

I have an idea how to do what you need. I took a geometry for computer graphics course last semester, but unfortunately, it was the class which I spent the least time on, and I can only really remember the concepts... not the implementation. Additionally, I don't know how you would be able to import the data into the software you're using.

Take a look Here It is the lecture notes for the class I took. It's got a link to download MathReader which is the free viewer for Mathematica. Unfortunately, you won't be able to play around with any of the stuff, but you will at least see the notes.

Vector Rotations about a non-origin point are covered in section 1.3

Viewing angle is covered somewhere in chapter 4.

I'm afraid that this will probably be too nitty-gritty and less "how do I do it" for you at this point.

3. Jul 17, 2004

### Polverone

Thanks for trying to help with my problem. All my geometry data is available to me as x,y,z triples (there's connectivity information too, but I just care about the points). All I want is to automatically rotate the model so that I always have a level, head-on view of the region of interest (which consists of the 3 base pairs I showed in my screenshots). It seems like somebody must have solved this problem before with an algorithm or decent heuristic, but I can't recall having come across such info in my readings on graphics and I'm having a hard time figuring it out now.

4. Jul 21, 2004

### robphy

Recently, I've been using VPython http://vpython.org/ for my 3D-visualization needs.

I wonder if VPython can satisfactorily render your dataset for you. You can easily create an animation by moving the camera position or changing the center of the view. (See http://vpython.org/webdoc/visual/display.html )

If you must render in your software, maybe VPython can interface with your program and "steer the camera". VPython supports vector and matrix calculations (via Numeric) and may help simplify your calculations.

Here are some other tools that may be of interest
http://vrmlworks.crispen.org/tools.html