#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy
[docs]class Transformation:
"""
The transformation module allow to calculate global or local coordinates while an XML export.
Let's define a matrix representing the transformation (translation and rotation) of a location in an environment or building.
:arguments:
- x (type integer)
- y (type integer)
- z (type integer)
- angle (type float)
rotation between local and global coordinates
**exemple**
>>> x = 20
>>> y = 12
>>> z = 3
>>> angle = 90
>>> T = Transformation(x,y,z,angle)"
To calculate global coordinates, the local ones are multiplicated by the transformation matrix.Pour calculer les coordonnées (X', Y', Z') dans le repère global, on multiplie les coordonées (X, Y, Z) dans le repère local par la matrice de transformation associée.
cos a -sin a 0 x X X'
sin a cos a 0 y * Y = Y'
0 0 1 z Z Z'
0 0 0 1 1 1
x, y, z are the global coordinates of the location and a the angle of the rotation.
For a local point (a, b, c) the global transformation function is called that way :
>>> coor_globales = T.trans(a,b,c)
"""
def __init__(self, x_arg, y_arg, z_arg, angle_arg):
"""
Initialisation of the transformation matrix from local coordinates to global ones
"""
self.matrice = numpy.eye(4)
self.matrice[0,3] = x_arg
self.matrice[1,3] = y_arg
self.matrice[2,3] = z_arg
self.matrice[0,0] = numpy.cos(angle_arg*2*numpy.pi/360)
self.matrice[0,1] = -numpy.sin(angle_arg*2*numpy.pi/360)
self.matrice[1,0] = numpy.sin(angle_arg*2*numpy.pi/360)
self.matrice[1,1] = numpy.cos(angle_arg*2*numpy.pi/360)
""" Correction of the approximation due to the division by 360 ==> all values below 1e-15 are fixed to 0."""
for i in xrange(4):
for j in xrange(4):
if abs(self.matrice[i,j]) < 1E-15: self.matrice[i,j] = 0
# print self.matrice
[docs] def localToGlobal(self, x_coor_place, y_coor_place, z_coor_place):
"""
The function 'localeVersGlobale' enables to get global coordinates from local coordinates. the "coor_place" arguments are global coordinates of the place.
It returns a 3x1 vector, global coordinates of the point.
"""
#vector_coor = numpy.matrix('1; 2; 0; 1')
vector_coor = numpy.matrix("'" + str(x_coor_place) + "; " + str(y_coor_place) + "; " + str(z_coor_place) + "; 1'")
global_coor = self.matrice*vector_coor
return global_coor[:3]
[docs] def globalToLocal(self, x_coor_area, y_coor_area, z_coor_area):
"""
The function 'globaleVersLocale' enables to get local coordinates from global coordinates. the "coor_area" arguments are global coordinates of the place.
It returns a 3x1 vector, local coordinates of the point.
"""
vector_coor = numpy.matrix("'" + str(x_coor_area) + "; " + str(y_coor_area) + "; " + str(z_coor_area) + "; 1'")
local_coor = numpy.linalg.inv(self.matrice)*vector_coor
return local_coor[:3]
if __name__ == '__main__':
trans_g = Transformation(10,30,6, 180)
o = trans_g.localeVersGlobale(1, 2, 0)
print o
print trans_g.globaleVersLocale(int(o[0]),int(o[1]),int(o[2]))