#NEWLINE# def clip_matrix( left , right , bottom , top , near , far , perspective = False ) : #NEWLINE# #INDENT# 'Return matrix to obtain normalized device coordinates from frustrum.\n\n    The frustrum bounds are axis-aligned along x (left, right),\n    y (bottom, top) and z (near, far).\n\n    Normalized device coordinates are in range [-1, 1] if coordinates are\n    inside the frustrum.\n\n    If perspective is True the frustrum is a truncated pyramid with the\n    perspective point at origin and direction along z axis, otherwise an\n    orthographic canonical view volume (a box).\n\n    Homogeneous coordinates transformed by the perspective clip matrix\n    need to be dehomogenized (devided by w coordinate).\n\n    >>> frustrum = numpy.random.rand(6)\n    >>> frustrum[1] += frustrum[0]\n    >>> frustrum[3] += frustrum[2]\n    >>> frustrum[5] += frustrum[4]\n    >>> M = clip_matrix(*frustrum, perspective=False)\n    >>> numpy.dot(M, [frustrum[0], frustrum[2], frustrum[4], 1.0])\n    array([-1., -1., -1.,  1.])\n    >>> numpy.dot(M, [frustrum[1], frustrum[3], frustrum[5], 1.0])\n    array([ 1.,  1.,  1.,  1.])\n    >>> M = clip_matrix(*frustrum, perspective=True)\n    >>> v = numpy.dot(M, [frustrum[0], frustrum[2], frustrum[4], 1.0])\n    >>> v / v[3]\n    array([-1., -1., -1.,  1.])\n    >>> v = numpy.dot(M, [frustrum[1], frustrum[3], frustrum[4], 1.0])\n    >>> v / v[3]\n    array([ 1.,  1., -1.,  1.])\n\n    ' #NEWLINE# if ( ( left >= right ) or ( right >= top ) or ( near >= far ) ) : #NEWLINE# #INDENT# raise ValueError ( 'invalid frustrum' ) #NEWLINE# #UNINDENT# if perspective : #NEWLINE# #INDENT# if ( near <= _EPS ) : #NEWLINE# #INDENT# raise ValueError ( 'invalid frustrum: near <= 0' ) #NEWLINE# #UNINDENT# t = ( 2.0 * near ) #NEWLINE# M = ( ( ( ( - t ) / ( right - left ) ) , 0.0 , ( ( right + left ) / ( right - left ) ) , 0.0 ) , ( 0.0 , ( ( - t ) / ( top - bottom ) ) , ( ( top + bottom ) / ( top - bottom ) ) , 0.0 ) , ( 0.0 , 0.0 , ( ( - ( far + near ) ) / ( far - near ) ) , ( ( t * far ) / ( far - near ) ) ) , ( 0.0 , 0.0 , ( - 1.0 ) , 0.0 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# M = ( ( ( 2.0 / ( right - left ) ) , 0.0 , 0.0 , ( ( right + left ) / ( left - right ) ) ) , ( 0.0 , ( 2.0 / ( top - bottom ) ) , 0.0 , ( ( top + bottom ) / ( bottom - top ) ) ) , ( 0.0 , 0.0 , ( 2.0 / ( far - near ) ) , ( ( far + near ) / ( near - far ) ) ) , ( 0.0 , 0.0 , 0.0 , 1.0 ) ) #NEWLINE# #UNINDENT# return numpy . array ( M , dtype = numpy . float64 )