Python_2  1.0
Functions
_12a_poly Namespace Reference

Draws a 3D polyhedron and allows a user to rotate it (mouse left button and wheel). More...

Functions

def matMul (m1, m2)
 Return m1 x m2 (m1 multiplied by m2). More...
 
def vecSub (v1, v2)
 Return vector v2 - v1. More...
 
def vecAdd (v1, v2)
 Return vector v2 + v1. More...
 
def vecScale (v, s)
 Return vector v * s. More...
 
def veclen (p, q)
 Length of vector from point p = (x1,y1,...,x1n) to q = (x,y,...,xn). More...
 
def normalize (p)
 Normalize a vector. More...
 
def transpose (M)
 Transpose a matrix. More...
 
def bbox (p)
 Bounding Box of a polyhedron. More...
 
def translate (poly, t)
 Translate a polyhedron. More...
 
def centroid (vtx)
 Centroid of a convex face. More...
 
def crossProd (U, V)
 Return cross product U x V. More...
 
def dotProd (u, v)
 Return the dot product of two vectors. More...
 
def getNormal2 (p)
 Face normal vector. More...
 
def ROT_X (x)
 counter-clockwise rotation about the X axis More...
 
def ROT_Y (y)
 counter-clockwise rotation about the Y axis More...
 
def ROT_Z (z)
 counter-clockwise rotation about the Z axis More...
 
def EPS (d)
 angular displacement as a function of a number of pixels. More...
 
def eps (d)
 angular displacement as a function of the sign of a number of pixels. More...
 
def getNormal (poly)
 Polygon normal by Newell's method. More...
 
def drawAxes ()
 Draw coordinate axes. More...
 
def makeBox (box)
 Make a bounding box. More...
 
def drawBbox ()
 Draw the current polygon bounding box. More...
 
def DrawObj (obj, col)
 Draw a polyhedron. More...
 
def getNormalLine (p, C, col)
 Return a line corresponding to a face normal. More...
 
def drawFace (p, pc, cent, col1, col)
 Draw a face of the polyhedron, if it is visible. More...
 
def init ()
 
def cbClicked (event)
 
def cbMotion (event)
 
def wheelUp (event)
 
def wheelDown (event)
 
def wheel (event)
 
def resize (event)
 
def toggleFill (event)
 
def toggleNormal (event)
 
def toggleObj (event)
 
def toggleDebug (event)
 
def help (event)
 
def exit (event)
 
def main ()
 

Detailed Description

Draws a 3D polyhedron and allows a user to rotate it (mouse left button and wheel).

The polyhedron is represented by a 3 x number_of_vertices matrix. Each column represents a 3D vertex.

note: a m x n matrix is represented by a list of lines: [[l_1] [l_2] .. [l_m]]. m = len(mat), n = len(mat[0]), mat(i,j) = mat[i][j]

Author
Paulo Roma
Since
11/06/2019
See also
http://www.orimosenzon.com/wiki/index.php/Python_samples
http://mathworld.wolfram.com/RotationMatrix.html
https://code.activestate.com/recipes/578876-rotatable-tetrahedron/

Function Documentation

◆ bbox()

def _12a_poly.bbox (   p)

Bounding Box of a polyhedron.

Referenced by toggleObj().

◆ cbClicked()

def _12a_poly.cbClicked (   event)
Save current mouse position.

Referenced by cbMotion().

◆ cbMotion()

def _12a_poly.cbMotion (   event)
Map mouse displacements in Y direction to rotations about X axis,
   and mouse displacements in X direction to rotations about Y axis.

References cbClicked(), DrawObj(), EPS(), matMul(), ROT_X(), and ROT_Y().

◆ centroid()

def _12a_poly.centroid (   vtx)

Centroid of a convex face.

Referenced by DrawObj(), getNormalLine(), and toggleObj().

◆ crossProd()

def _12a_poly.crossProd (   U,
  V 
)

Return cross product U x V.

◆ dotProd()

def _12a_poly.dotProd (   u,
  v 
)

Return the dot product of two vectors.

Referenced by getNormalLine().

◆ drawAxes()

def _12a_poly.drawAxes ( )

Draw coordinate axes.

References transpose().

Referenced by DrawObj().

◆ drawBbox()

def _12a_poly.drawBbox ( )

Draw the current polygon bounding box.

References transpose().

Referenced by DrawObj().

◆ drawFace()

def _12a_poly.drawFace (   p,
  pc,
  cent,
  col1,
  col 
)

Draw a face of the polyhedron, if it is visible.

Parameters
pworld coordinate points.
pcscreen coordinate points.
centcentroid of the polyhedron.
col1fill color.
coloutline color.
See also
https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal
Draw a face of the polyhedron.

References getNormalLine().

Referenced by DrawObj().

◆ DrawObj()

def _12a_poly.DrawObj (   obj,
  col 
)

Draw a polyhedron.

Not all faces are visible at the same time. Use back face culling to eliminate invisible faces from camera point of view.

Parameters
objvertex coordinate matrix.
colcolor of the edges.
Draw a polyhedron.

References centroid(), drawAxes(), drawBbox(), drawFace(), and transpose().

Referenced by cbMotion(), _12a_poly_interface.draw(), main(), resize(), toggleFill(), toggleNormal(), wheel(), wheelDown(), and wheelUp().

◆ EPS()

def _12a_poly.EPS (   d)

angular displacement as a function of a number of pixels.

Referenced by cbMotion(), _12_tet.cbMottion(), _12_tet.init(), _12_tet.wheel(), wheel(), _12_tet.wheelDown(), wheelDown(), _12_tet.wheelUp(), and wheelUp().

◆ eps()

def _12a_poly.eps (   d)

angular displacement as a function of the sign of a number of pixels.

Referenced by _12_tet.init().

◆ exit()

def _12a_poly.exit (   event)
Finishes this program.

Referenced by _15_julia.keyboard(), mplayerdvd.playdvd(), and data.readSheet().

◆ getNormal()

def _12a_poly.getNormal (   poly)

Polygon normal by Newell's method.

Parameters
polya polygon vertex list.
Returns
face normal vector.
See also
https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal

References vecAdd(), and vecSub().

Referenced by getNormalLine().

◆ getNormal2()

def _12a_poly.getNormal2 (   p)

Face normal vector.

References vecSub().

◆ getNormalLine()

def _12a_poly.getNormalLine (   p,
  C,
  col 
)

Return a line corresponding to a face normal.

The circulation of the face in relation to the centroid of the polyhedron is used to figure out the normal direction. Therefore, even if faces are not consistently oriented, the normal will be right.

Parameters
pface vertex list.
Ccentroid of the polyhedron.
colnormal vector color.
Returns
normal line in screen coordinates and Z component of the normal for culling.

References centroid(), dotProd(), getNormal(), normalize(), vecAdd(), vecScale(), and vecSub().

Referenced by drawFace().

◆ help()

def _12a_poly.help (   event)
Usage instructions.

◆ init()

def _12a_poly.init ( )
Initialize global variables.

References toggleObj().

Referenced by main().

◆ main()

def _12a_poly.main ( )

References DrawObj(), and init().

◆ makeBox()

def _12a_poly.makeBox (   box)

Make a bounding box.

Parameters
boxlower left and upper right corners of the bounding box.
Returns
a 3 x 8 matrix with the eight vertices of the bounding box.

Referenced by toggleObj().

◆ matMul()

def _12a_poly.matMul (   m1,
  m2 
)

Return m1 x m2 (m1 multiplied by m2).

Referenced by cbMotion(), wheel(), wheelDown(), and wheelUp().

◆ normalize()

def _12a_poly.normalize (   p)

Normalize a vector.

References veclen().

Referenced by getNormalLine().

◆ resize()

def _12a_poly.resize (   event)
Redraw the polyhedron, in case of a window change due to user resizing it.

References DrawObj().

Referenced by toggleObj().

◆ ROT_X()

def _12a_poly.ROT_X (   x)

counter-clockwise rotation about the X axis

Referenced by cbMotion(), _12_tet.cbMottion(), and _12_tet.init().

◆ ROT_Y()

def _12a_poly.ROT_Y (   y)

counter-clockwise rotation about the Y axis

Referenced by cbMotion(), _12_tet.cbMottion(), and _12_tet.init().

◆ ROT_Z()

def _12a_poly.ROT_Z (   z)

counter-clockwise rotation about the Z axis

Referenced by _12_tet.init(), _12_tet.wheel(), wheel(), _12_tet.wheelDown(), wheelDown(), _12_tet.wheelUp(), and wheelUp().

◆ toggleDebug()

def _12a_poly.toggleDebug (   event)
Toggle debugging mode.

◆ toggleFill()

def _12a_poly.toggleFill (   event)
Toggle outline mode.

References DrawObj().

◆ toggleNormal()

def _12a_poly.toggleNormal (   event)
Toggle normal mode.

References DrawObj().

◆ toggleObj()

def _12a_poly.toggleObj (   event)
Toggle displayed object.

References bbox(), centroid(), makeBox(), resize(), translate(), transpose(), and vecScale().

Referenced by init(), and _12a_poly_interface.setSolid().

◆ translate()

def _12a_poly.translate (   poly,
  t 
)

Translate a polyhedron.

Parameters
polypolyhedron.
ttranslation vector.

References vecSub().

Referenced by toggleObj().

◆ transpose()

def _12a_poly.transpose (   M)

Transpose a matrix.

Referenced by drawAxes(), drawBbox(), DrawObj(), and toggleObj().

◆ vecAdd()

def _12a_poly.vecAdd (   v1,
  v2 
)

Return vector v2 + v1.

Referenced by getNormal(), and getNormalLine().

◆ veclen()

def _12a_poly.veclen (   p,
  q 
)

Length of vector from point p = (x1,y1,...,x1n) to q = (x,y,...,xn).

Referenced by normalize().

◆ vecScale()

def _12a_poly.vecScale (   v,
  s 
)

Return vector v * s.

Referenced by getNormalLine(), and toggleObj().

◆ vecSub()

def _12a_poly.vecSub (   v1,
  v2 
)

Return vector v2 - v1.

Referenced by getNormal(), getNormal2(), getNormalLine(), and translate().

◆ wheel()

def _12a_poly.wheel (   event)
Map mouse wheel displacements to rotations about Z axis.

References DrawObj(), EPS(), matMul(), and ROT_Z().

◆ wheelDown()

def _12a_poly.wheelDown (   event)
Map mouse wheel down displacements to rotations about Z axis.

References DrawObj(), EPS(), matMul(), and ROT_Z().

◆ wheelUp()

def _12a_poly.wheelUp (   event)
Map mouse wheel up displacements to rotations about Z axis.

References DrawObj(), EPS(), matMul(), and ROT_Z().