Skip to content

Commit

Permalink
adding more coverage to linear
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineH committed Apr 14, 2016
1 parent bd26a2b commit 3dc6a29
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 3 deletions.
182 changes: 182 additions & 0 deletions pyglet_helper/test/util_tests/test_linear.py
Expand Up @@ -185,3 +185,185 @@ def test_vertex_init():
assert ver1[0] == 0.0
assert ver1[1] == 1.0
assert ver1[2] == 2.0


def test_vertex_project():
from pyglet_helper.util import Vertex
ver1 = Vertex([-1.0, 0, -2.0, .50])
ver2 = ver1.project()
assert ver2[0] == -2.0
assert ver2[1] == 0.0
assert ver2[2] == -4.0


@patch('pyglet_helper.util.linear.gl', new=pyglet_helper.test.fake_gl)
def test_vertex_render():
from pyglet_helper.util import Vertex
vec1 = Vertex([1.0, 0, 2.0])
vec1.gl_render()


@raises(IndexError)
def test_vertex_get_error():
from pyglet_helper.util import Vertex
vec1 = Vertex()
vec1[4]


@raises(IndexError)
def test_vertex_set_error():
from pyglet_helper.util import Vertex
vec1 = Vertex()
vec1[4] = 2


def test_vertex_str():
from pyglet_helper.util import Vertex
vec1 = Vertex([0.0, 1.0, -1.0, 0.5])
_str = str(vec1)
assert _str == "Vertex(0.0,1.0,-1.0,0.5)"


def test_tmatrix_init():
from pyglet_helper.util import Tmatrix
_tmatrix1 = Tmatrix()
_tmatrix1.matrix[2, 3] = 1.0
_tmatrix2 = Tmatrix(_tmatrix1)
assert _tmatrix2.matrix[2, 3] == 1.0


def test_tmatrix_mult():
from pyglet_helper.util import Tmatrix, Vertex
_tmatrix1 = Tmatrix()
_tmatrix1.matrix[0, 1] = 1.0
_tmatrix1.matrix[1, 0] = 1.0
_tmatrix1.matrix[0, 0] = 0.0
_tmatrix1.matrix[1, 1] = 0.0
_result = _tmatrix1*_tmatrix1
assert _result.matrix[0, 0] == 1.0
assert _result.matrix[1, 1] == 1.0
assert _result.matrix[0, 1] == 0.0
assert _result.matrix[1, 0] == 0.0
_result = 2.0*_tmatrix1
assert _result.matrix[0, 0] == 0.0
assert _result.matrix[1, 1] == 0.0
assert _result.matrix[0, 1] == 2.0
assert _result.matrix[1, 0] == 2.0
_result = _tmatrix1*2.0
assert _result.matrix[0, 0] == 0.0
assert _result.matrix[1, 1] == 0.0
assert _result.matrix[0, 1] == 2.0
assert _result.matrix[1, 0] == 2.0
_vert = Vertex([0, 1.0, 0, 1])
_result = _tmatrix1*_vert
assert _result[0] == 1.0
assert _result[1] == 0.0


def test_tmatrix_inverse():
from pyglet_helper.util import Tmatrix
_tmatrix1 = Tmatrix()
_tmatrix1.matrix[0, 1] = 1.0
_tmatrix1.matrix[1, 0] = 0.0
_tmatrix1.matrix[0, 0] = 0.50
_tmatrix1.matrix[1, 1] = -0.50
_tmatrix1.inverse()
assert _tmatrix1.matrix[0, 1] == 4.0
assert _tmatrix1.matrix[0, 0] == 2.0
assert _tmatrix1.matrix[1, 0] == 0.0
assert _tmatrix1.matrix[1, 1] == -2.0


def test_tmatrix_project():
from pyglet_helper.util import Tmatrix, Vertex
_tmatrix1 = Tmatrix()
_tmatrix1.matrix[0, 0] = 0.0
_tmatrix1.matrix[1, 1] = 0.0
_tmatrix1.matrix[1, 0] = 1.0
_tmatrix1.matrix[0, 1] = 1.0
_vert = Vertex([0, 1.0, 0, 0.5])
_outvert = _tmatrix1.project(_vert)
assert _outvert[0] == 1.0
assert _outvert[1] == 0.0


def test_tmatrix_scale():
from pyglet_helper.util import Tmatrix, Vertex
_tmatrix1 = Tmatrix()
_tmatrix1.matrix[0, 0] = 0.0
_tmatrix1.matrix[1, 1] = 0.0
_tmatrix1.matrix[1, 0] = 1.0
_tmatrix1.matrix[0, 1] = 1.0
_vert = Vertex([0, 1.0, 0, 0.5])
_tmatrix1.scale(_vert)
assert _tmatrix1.matrix[0, 0] == 0.0
assert _tmatrix1.matrix[1, 1] == 0.0
assert _tmatrix1.matrix[0, 1] == 0.0
assert _tmatrix1.matrix[3, 3] == 0.5


def test_tmatrix_times_inv():
from pyglet_helper.util import Tmatrix, Vertex, Vector
# test with a vector
_tmatrix1 = Tmatrix()
_vect = Vector([0.0, 1.0, 0.0])
_tmatrix1.matrix[0, 0] = 0.0
_tmatrix1.matrix[1, 1] = 0.0
_tmatrix1.matrix[1, 0] = 1.0
_tmatrix1.matrix[0, 1] = 1.0
_outvect = _tmatrix1.times_inv(_vect)
assert _outvect[0] == 1.0
assert _outvect[1] == 0.0
# test with a vertex
_vert = Vertex([0, 1.0, 0, 0.5])
_outvect = _tmatrix1.times_inv(_vert)
assert _outvect[0] == 1.0
assert _outvect[1] == 0.0


def test_tmatrix_times_v():
from pyglet_helper.util import Tmatrix, Vertex, Vector
# test with a vector
_tmatrix1 = Tmatrix()
_vect = Vector([0.0, 1.0, 0.0])
_tmatrix1.matrix[0, 0] = 0.0
_tmatrix1.matrix[1, 1] = 0.0
_tmatrix1.matrix[1, 0] = 1.0
_tmatrix1.matrix[0, 1] = 1.0
_outvect = _tmatrix1.times_v(_vect)
assert _outvect[0] == 1.0
assert _outvect[1] == 0.0
# test with a vertex
_vert = Vertex([0, 1.0, 0, 0.5])
_outvect = _tmatrix1.times_v(_vert)
assert _outvect[0] == 1.0
assert _outvect[1] == 0.0


def test_tmatrix_origin():
from pyglet_helper.util import Tmatrix
# test with a vector
_tmatrix1 = Tmatrix()
_tmatrix1.matrix[0, 0] = 0.50
_tmatrix1.matrix[1, 1] = 0.0
_tmatrix1.matrix[1, 0] = 2.0
_tmatrix1.matrix[0, 1] = 1.0
_tmatrix1.matrix[3, 0] = 1.0
_outvect = _tmatrix1.origin()
assert _outvect[0] == 1.0
assert _outvect[1] == 0.0
assert _outvect[2] == 0.0


def test_tmatrix_str():
from pyglet_helper.util import Tmatrix
# test with a vector
_tmatrix1 = Tmatrix()
_tmatrix1.matrix[0, 0] = 0.50
_tmatrix1.matrix[1, 1] = 0.0
_tmatrix1.matrix[1, 0] = 2.0
_tmatrix1.matrix[0, 1] = 1.0
_tmatrix1.matrix[3, 0] = 1.0
_out_str = str(_tmatrix1)
assert _out_str == "| 0.5 2.0 0.0 1.0|\n| 1.0 0.0 0.0 0.0|\n| 0.0 0.0 " \
"1.0 0.0|\n| 0.0 0.0 0.0 1.0|\n"
38 changes: 35 additions & 3 deletions pyglet_helper/util/linear.py
Expand Up @@ -558,6 +558,27 @@ def __getitem__(self, i):
if i > 3 or i < -4:
raise IndexError("index not available")

def __setitem__(self, i, value):
"""
set the component of the vertex by an integer address
:param i: the integer, with a value from -4 to 3
:type i: int
:param value: the new value to set the component to
:type value: float
:return:
"""
if i == 0 or i == -4:
self.x_component = value
if i == 1 or i == -3:
self.y_component = value
if i == 2 or i == -2:
self.z_component = value
if i == 3 or i == -1:
self.w_component = value
if i > 3 or i < -4:
raise IndexError("index not available")


def __repr__(self):
"""
Format the vertex into a string
Expand Down Expand Up @@ -614,7 +635,20 @@ def __mul__(self, input_data):
out_vect = self.project(input_data)
return out_vect
else:
return self.matrix * input_data
tmp = Tmatrix()
tmp.matrix = self.matrix * input_data
return tmp

def __rmul__(self, input_data):
"""
Multiply the data in the input by the current matrix
:param input_data: the data to be multiplied
:type input_data: float or int
:return: self.matrix*input_data
"""
tmp = Tmatrix()
tmp.matrix = self.matrix * input_data
return tmp

def inverse(self):
"""
Expand Down Expand Up @@ -830,8 +864,6 @@ def w_column(self, vector=None):
tuple or list
"""
if vector is not None:
if type(vector) is not Vector and type(vector) is not Vertex:
vector = Vector(vector)
self.matrix[3, 0] = vector.x_component
self.matrix[3, 1] = vector.y_component
self.matrix[3, 2] = vector.z_component
Expand Down

0 comments on commit 3dc6a29

Please sign in to comment.