| | 48 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 49 | #~ Constants / Variables / Etc. |
|---|
| | 50 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 51 | |
|---|
| | 52 | Projection = None |
|---|
| | 53 | _BoxTraversal = IndexedCollectionTraversal(GL.GL_LINES, [[0,1, 1,2, 2,3, 3,0, 0,4, 1,5, 2,6, 3,7, 4,5, 5,6, 6,7, 7,4]], Numeric.UInt8) |
|---|
| | 54 | |
|---|
| | 55 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 56 | #~ Public Methods |
|---|
| | 57 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 58 | |
|---|
| | 59 | def __init__(self, projection=None): |
|---|
| | 60 | self.Projection = projection |
|---|
| | 61 | self._Vertices= VertexArray(Numeric.zeros((8,3), Numeric.Float32)) |
|---|
| | 62 | |
|---|
| | 63 | def Execute(self, context): |
|---|
| | 64 | self._UpdateBoxVertices() |
|---|
| | 65 | self._Vertices.Select(context) |
|---|
| | 66 | self._BoxTraversal.Execute(context) |
|---|
| | 67 | self._Vertices.Deselect(context) |
|---|
| | 68 | |
|---|
| | 69 | def _UpdateBoxVertices(self): |
|---|
| | 70 | l,r,b,t,n,f = (-1.,1.,-1.,1.,-1.,1.) |
|---|
| | 71 | self._Vertices.data[:4] = [(l,b,n), (r,b,n), (r,t,n), (l,t,n)] |
|---|
| | 72 | self._Vertices.data[4:] = [(l,b,f), (r,b,f), (r,t,f), (l,t,f)] |
|---|
| | 73 | return self._Vertices.data |
|---|
| | 74 | |
|---|
| | 75 | Draw = Execute |
|---|
| | 76 | |
|---|
| | 77 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 78 | |
|---|
| | 79 | class OrthographicBox(ProjectionBox): |
|---|
| | 80 | """ |
|---|
| | 81 | >>> from RBRapier.Tools.Projections import Orthographic |
|---|
| | 82 | >>> OrthographicBox(Orthographic(-1, 1, -1, 1, -1, 1))._UpdateBoxVertices() |
|---|
| | 83 | array([[-1., -1., -1.], |
|---|
| | 84 | [ 1., -1., -1.], |
|---|
| | 85 | [ 1., 1., -1.], |
|---|
| | 86 | [-1., 1., -1.], |
|---|
| | 87 | [-1., -1., 1.], |
|---|
| | 88 | [ 1., -1., 1.], |
|---|
| | 89 | [ 1., 1., 1.], |
|---|
| | 90 | [-1., 1., 1.]],'f') |
|---|
| | 91 | """ |
|---|
| | 92 | |
|---|
| | 93 | def _UpdateBoxVertices(self): |
|---|
| | 94 | l,r,b,t,n,f = [float(e) for e in self.Projection.asTuple] |
|---|
| | 95 | self._Vertices.data[:4] = [(l,b,n), (r,b,n), (r,t,n), (l,t,n)] |
|---|
| | 96 | self._Vertices.data[4:] = [(l,b,f), (r,b,f), (r,t,f), (l,t,f)] |
|---|
| | 97 | return self._Vertices.data |
|---|
| | 98 | |
|---|
| | 99 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 100 | |
|---|
| | 101 | class FrustumBox(ProjectionBox): |
|---|
| | 102 | """ |
|---|
| | 103 | >>> from RBRapier.Tools.Projections import Frustum |
|---|
| | 104 | >>> FrustumBox(Frustum(-1, 1, -1, 1, 1, 2))._UpdateBoxVertices() |
|---|
| | 105 | array([[-1., -1., 1.], |
|---|
| | 106 | [ 1., -1., 1.], |
|---|
| | 107 | [ 1., 1., 1.], |
|---|
| | 108 | [-1., 1., 1.], |
|---|
| | 109 | [-2., -2., 2.], |
|---|
| | 110 | [ 2., -2., 2.], |
|---|
| | 111 | [ 2., 2., 2.], |
|---|
| | 112 | [-2., 2., 2.]],'f') |
|---|
| | 113 | >>> FrustumBox(Frustum(1, 2, 1, 2, 2, 4))._UpdateBoxVertices() |
|---|
| | 114 | array([[ 1., 1., 2.], |
|---|
| | 115 | [ 2., 1., 2.], |
|---|
| | 116 | [ 2., 2., 2.], |
|---|
| | 117 | [ 1., 2., 2.], |
|---|
| | 118 | [ 2., 2., 4.], |
|---|
| | 119 | [ 4., 2., 4.], |
|---|
| | 120 | [ 4., 4., 4.], |
|---|
| | 121 | [ 2., 4., 4.]],'f') |
|---|
| | 122 | """ |
|---|
| | 123 | |
|---|
| | 124 | def _UpdateBoxVertices(self): |
|---|
| | 125 | l,r,b,t,n,f = [float(e) for e in self.Projection.asTuple] |
|---|
| | 126 | self._Vertices.data[:4] = [(l,b,n), (r,b,n), (r,t,n), (l,t,n)] |
|---|
| | 127 | scale = f/n |
|---|
| | 128 | l = l*scale |
|---|
| | 129 | r = r*scale |
|---|
| | 130 | b = b*scale |
|---|
| | 131 | t = t*scale |
|---|
| | 132 | self._Vertices.data[4:] = [(l,b,f), (r,b,f), (r,t,f), (l,t,f)] |
|---|
| | 133 | return self._Vertices.data |
|---|
| | 134 | |
|---|
| | 135 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 136 | #~ Testing |
|---|
| | 137 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 138 | |
|---|
| | 139 | if __name__=='__main__': |
|---|
| | 140 | print "Testing..." |
|---|
| | 141 | import doctest |
|---|
| | 142 | import ProjectionBoxes as _testmod |
|---|
| | 143 | doctest.testmod(_testmod) |
|---|
| | 144 | print "Test complete." |
|---|
| | 145 | |
|---|
| | 146 | |
|---|