Applying transformation matrix to 3D vertex coordinates

PhilC pcooke at philc.net
Mon Sep 3 14:17:54 EDT 2007


'''
################################################
Task:-
to apply a translation array to an array
of 3D vertex coordinates to produce the
resulting location of each vertices.

Translation array and vertex coordinates taken from
a Truespace COB file.

The text in the COB file looks like this:-

Transform
-2.11786 -0.817596 0.946387 0.864939
0.405189 1.25484 2.11894 0.434915
-1.16679 1.9198 -0.981965 1.13894
0 0 0 1
World Vertices 8
-0.200000 -0.029486 -0.236313
0.200000 0.029486 -0.163687
-0.200000 0.370514 -0.282911
-0.200000 0.370514 0.117089
-0.200000 -0.029486 0.163687
0.200000 0.029486 0.236313
0.200000 0.429486 -0.210286
0.200000 0.429486 0.189714

###############################################
'''
# script start 

import Numeric
 
transArray = Numeric.array((
(-2.11786, -0.817596, 0.946387, 0.864939),
(0.405189, 1.25484, 2.11894, 0.434915),
(-1.16679, 1.9198, -0.981965, 1.1389),
(0, 0, 0, 1)
))

# a "1" added to the end of each set of vertix coordinates
vertArray = Numeric.array((
(-0.200000, -0.029486, -0.236313, 1),
(0.200000, 0.029486, -0.163687, 1),
(-0.200000, 0.370514, -0.282911, 1),
(-0.200000, 0.370514, 0.117089, 1),
(-0.200000, -0.029486, 0.163687, 1),
(0.200000, 0.029486, 0.236313, 1),
(0.200000, 0.429486, -0.210286, 1),
(0.200000, 0.429486, 0.189714, 1)
))

print transArray
print ""
print vertArray
print ""

transArray = Numeric.reshape(transArray,(4,4))
vertArray = Numeric.reshape(vertArray,(4,8))

print Numeric.matrixmultiply(transArray,vertArray)

# script end
'''
##################################################################
Result:-

[[ 0.5708016   0.10309048  0.70481144 -1.12413     0.1022124
0.03400637  0.63866924 -1.12413   ]
 [-0.6688108   0.57729922 -0.19537307  4.213884    0.3408408
0.72615216  0.66384632  4.213884  ]
 [ 0.273571    1.26381257 -0.66763452  0.909945   -0.585931
1.13709619  0.39979     0.909945  ]
 [ 0.2         0.429486   -0.210286    1.          0.2
0.429486    0.189714    1.        ]]

Am I going in the right direction?
What do I do with the result? :)

Thanks,

PhilC
'''



More information about the Python-list mailing list