def transformNormals(normals: numpy.ndarray, transformation: Matrix) -> numpy.ndarray: """Transform an array of normals using a matrix :param normals: :type{numpy.ndarray} array of 3D normals :param transformation: a 4x4 matrix :return: :type{numpy.ndarray} the transformed normals :note This assumes the normals are untranslated unit normals, and returns the same. """ data = numpy.pad(normals, ((0, 0), (0, 1)), "constant", constant_values=(0.0, 0.0)) # Get the translation from the transformation so we can cancel it later. translation = transformation.getTranslation() # Transform the normals so they get the proper rotation data = data.dot(transformation.getTransposed().getData()) data += transformation.getData()[:, 3] data = data[:, 0:3] # Cancel the translation since normals should always go from origin to a point on the unit sphere. data[:] -= translation.getData() # Re-normalize the normals, since the transformation can contain scaling. lengths = numpy.linalg.norm(data, axis=1) lengths[lengths == 0] = 1 data[:, 0] /= lengths data[:, 1] /= lengths data[:, 2] /= lengths return data
def transformNormals(normals: numpy.ndarray, transformation: Matrix) -> numpy.ndarray: data = numpy.pad(normals, ((0, 0), (0, 1)), "constant", constant_values=(0.0, 0.0)) # Get the translation from the transformation so we can cancel it later. translation = transformation.getTranslation() # Transform the normals so they get the proper rotation data = data.dot(transformation.getTransposed().getData()) data += transformation.getData()[:, 3] data = data[:, 0:3] # Cancel the translation since normals should always go from origin to a point on the unit sphere. data[:] -= translation.getData() # Re-normalize the normals, since the transformation can contain scaling. lengths = numpy.linalg.norm(data, axis = 1) data[:, 0] /= lengths data[:, 1] /= lengths data[:, 2] /= lengths return data
def test_translate(self): matrix = Matrix() matrix.translate(Vector(1, 1, 1)) assert matrix.getTranslation() == Vector(1, 1, 1) matrix.translate(Vector(2, 3, 4)) assert matrix.getTranslation() == Vector(3, 4, 5)