-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransforms.py
172 lines (151 loc) · 5.47 KB
/
transforms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (C) 2013 Nicolas P. Rougier. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of Nicolas P. Rougier.
# -----------------------------------------------------------------------------
import math
import numpy
import numpy as np
def translate(M, x, y=None, z=None):
"""
translate produces a translation by (x, y, z) .
Parameters
----------
x, y, z
Specify the x, y, and z coordinates of a translation vector.
"""
if y is None: y = x
if z is None: z = x
T = [[ 1, 0, 0, x],
[ 0, 1, 0, y],
[ 0, 0, 1, z],
[ 0, 0, 0, 1]]
T = np.array(T, dtype=np.float32).T
M[...] = np.dot(M,T)
def scale(M, x, y=None, z=None):
"""
scale produces a non uniform scaling along the x, y, and z axes. The three
parameters indicate the desired scale factor along each of the three axes.
Parameters
----------
x, y, z
Specify scale factors along the x, y, and z axes, respectively.
"""
if y is None: y = x
if z is None: z = x
S = [[ x, 0, 0, 0],
[ 0, y, 0, 0],
[ 0, 0, z, 0],
[ 0, 0, 0, 1]]
S = np.array(S,dtype=np.float32).T
M[...] = np.dot(M,S)
def xrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
[[ 1.0, 0.0, 0.0, 0.0 ],
[ 0.0, cosT,-sinT, 0.0 ],
[ 0.0, sinT, cosT, 0.0 ],
[ 0.0, 0.0, 0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)
def yrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
[[ cosT, 0.0, sinT, 0.0 ],
[ 0.0, 1.0, 0.0, 0.0 ],
[-sinT, 0.0, cosT, 0.0 ],
[ 0.0, 0.0, 0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)
def zrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
[[ cosT,-sinT, 0.0, 0.0 ],
[ sinT, cosT, 0.0, 0.0 ],
[ 0.0, 0.0, 1.0, 0.0 ],
[ 0.0, 0.0, 0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)
def rotate(M, angle, x, y, z, point=None):
"""
rotate produces a rotation of angle degrees around the vector (x, y, z).
Parameters
----------
M
Current transformation as a numpy array
angle
Specifies the angle of rotation, in degrees.
x, y, z
Specify the x, y, and z coordinates of a vector, respectively.
"""
angle = math.pi*angle/180
c,s = math.cos(angle), math.sin(angle)
n = math.sqrt(x*x+y*y+z*z)
x /= n
y /= n
z /= n
cx,cy,cz = (1-c)*x, (1-c)*y, (1-c)*z
R = numpy.array([[ cx*x + c , cy*x - z*s, cz*x + y*s, 0],
[ cx*y + z*s, cy*y + c , cz*y - x*s, 0],
[ cx*z - y*s, cy*z + x*s, cz*z + c, 0],
[ 0, 0, 0, 1]]).T
M[...] = np.dot(M,R)
def ortho( left, right, bottom, top, znear, zfar ):
assert( right != left )
assert( bottom != top )
assert( znear != zfar )
M = np.zeros((4,4), dtype=np.float32)
M[0,0] = +2.0/(right-left)
M[3,0] = -(right+left)/float(right-left)
M[1,1] = +2.0/(top-bottom)
M[3,1] = -(top+bottom)/float(top-bottom)
M[2,2] = -2.0/(zfar-znear)
M[3,2] = -(zfar+znear)/float(zfar-znear)
M[3,3] = 1.0
return M
def frustum( left, right, bottom, top, znear, zfar ):
assert( right != left )
assert( bottom != top )
assert( znear != zfar )
M = np.zeros((4,4), dtype=np.float32)
M[0,0] = +2.0*znear/(right-left)
M[2,0] = (right+left)/(right-left)
M[1,1] = +2.0*znear/(top-bottom)
M[3,1] = (top+bottom)/(top-bottom)
M[2,2] = -(zfar+znear)/(zfar-znear)
M[3,2] = -2.0*znear*zfar/(zfar-znear)
M[2,3] = -1.0
return M
def perspective(fovy, aspect, znear, zfar):
assert( znear != zfar )
h = np.tan(fovy / 360.0 * np.pi) * znear
w = h * aspect
return frustum( -w, w, -h, h, znear, zfar )