# Perp maker

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
```#
# \$Id\$
#

#
# perp_maker.py: Nothing to do with cops.  Given a simple PyMol scene, attempts to
# create a CGO background triangle perpendicular to the vector created - which is
# parallel to the line segment drawn through the camera point and current center of
# mass - as obtained by "get_position," or "get_view."
#

#
# To use: Load your scene.  Orient the scene as you wish.  Run the script.
# Could it be any simpler?!
#

# The TTT Matrix has to be the identity, do achieve this result.  So,
# run the following:
#   -- 'reset'
#   -- then orient your molecule as desired using the EDITING features!
#     -- before running this script, make sure 'get_view' shows the identity
#     -- matrix for the first 9 elements.
#   -- then run the script
#

import pymol
import math
import sys
import random
from pymol.cgo import *
from pymol.vfont import plain

############################################################
#
# Methods
#
############################################################

#
# Given the viewVector and center, creates a random sized plane
# perpendicular to the viewVector through the origin.  It is then
# the next step's responsibility to move the plane back some so
# it dosen't cut the molecule/scene in half.
#
def getPPlane( viewVector, center, side_length=100 ):
"""Returns a 3-tuple of 3D points representing the perp. plane."""

# for reproduceable testing
#random.seed(10)
#
# The formula for a plane with our chacteristics is defined by
#
# A(x - x') + B(y - y') + C(y - y') + D = 0, where
# A, B and C are not all zero coefficients in the vector
# Ai + Bj + Ck such that the plane is perpendicular to this
# vector; x, y, and z are points on the plane; x', y', and z'
# are the coordiates through which the plane shall run.
#

# This is fool-ass.  Gotta' be a better way to do this.
# Declaring that rVal is a 3-Tuple.
rVal = [ [], [], [], [], [], [] ]

# Compose two triangles into a square.
# Never learned any GFX coding, so I'm sure there's something
# better than this; but, this works.
for i in range(0, 6):
if ( i == 0 ) or ( i == 5 ):
x = -side_length + center
y = -side_length + center
elif (i == 1):
x = -side_length + center
y =  side_length + center
elif ( i == 2 ) or ( i == 3 ):
x = side_length + center
y = side_length + center
elif ( i == 4):
x = side_length + center
y = -side_length + center

if ( viewVector != 0 ):
z = -(((viewVector*(x - center)) - (viewVector*(y - center))) /
viewVector) + center

else:
print "Z-component of viewVector is zero.  Now, I need a nonzero value here
so I'm just making one up. :)"
z = random.randint(-200, 200)

rVal[i] = [x, y, z]

return rVal

############################################################
#
# End methods
#
############################################################
#
# First, get the center and camera locations
#
view = cmd.get_view()
camera = [ view, view, view ]
center = [ view, view, view ]

#
# Sanity check

#
print "Camera is: " + str(camera)
print "Center is: " + str(center)

#
# Create the vector through the two points directed
# from the camera to the center - the viewVector
#
viewVector = [ center - camera,
center - camera,
center - camera ]

print "ViewVector is: " + str(viewVector)

#
# Create the plane perpendicular to the viewVector
# running through the origin
#

pPlane = getPPlane( viewVector, center, side_length=100 )
print "Plane points calculated as: " + str(pPlane)

#
# Now translate the plane down away from the camera along the viewVector axis
#

# now create the CGO and load from the points
obj = [
BEGIN, TRIANGLES,
COLOR, 0.2, 0.4, 1,

VERTEX, pPlane, pPlane, pPlane,
VERTEX, pPlane, pPlane, pPlane,
VERTEX, pPlane, pPlane, pPlane,

VERTEX, pPlane, pPlane, pPlane,
VERTEX, pPlane, pPlane, pPlane,
VERTEX, pPlane, pPlane, pPlane,

END
]