-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3D_Rotation.py
More file actions
148 lines (118 loc) · 3.89 KB
/
3D_Rotation.py
File metadata and controls
148 lines (118 loc) · 3.89 KB
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
#Python Project
#Submitted to ICFOSS
#pip install plotly==4.8.1(install needed for running the program )
import numpy as np
from plotly.offline import plot
import plotly.graph_objects as go
ch='y' #initialisation of varaiable ch
v = np.array([[0,0,0],[0,0.5,0],[0.25,0.5,0],[0.25,0,0],
[0,0,1],[0,0.5,1],[0.25,0.5,1],[0.25,0,1]]) #Array used to specify the coordinates of the cuboid.
v1 = np.zeros((22,3),float) #Array used for scatter plot
for i in range(11):
if i == 4:
v1[i]=v[i-4]
elif i == 9:
v1[i]=v[i-5]
elif i == 8:
v1[i]=v[7]
elif i < 4:
v1[i]=v[i]
elif i > 4 and i <= 7:
v1[i]=v[i-1]
v1[10]=v[0]
v1[11]=v[1]
v1[12]=v[5]
v1[13]=v[4]
v1[14]=v[0]
v1[15]=v[3]
v1[16]=v[2]
v1[17]=v[6]
v1[18]=v[5]
v1[19]=v[3]
v1[20]=v[3]
v1[21]=v[7] #inputting values into array v1
x,y,z=v.T
#storing x comps in x, y comps in y and z comps in z using array unpacking.
x1,y1,z1=v1.T
print("The 3D object is a Rectangular Cuboid (The output will be displayed in the browser)")
fig=go.Figure() #for plotting the 3D object
fig.add_trace(go.Scatter3d(x=x1,y=y1,z=z1,mode='lines+markers'))
fig.add_trace(
go.Mesh3d(x=x, y=y, z=z,
alphahull=1,
opacity=1,
color='orange')
)
plot(fig) #command to display the cuboid
while(ch=='y' or ch=='Y'): #loop condition
print("\nEnter rotation angles \n" )
a=float(input("Enter angle 'a' in terms of degrees (rot along x-axis) : "))
b=float(input("Enter angle 'b' in terms of degrees (rot along y axis) : "))
c=float(input("Enter angle 'c' in terms of degrees (rot along z axis) : "))
#Creation and initialisation of rotation matrices
X=np.zeros((3,3),float)
Y=np.zeros((3,3),float)
Z=np.zeros((3,3),float)
p=np.zeros((8,3),float)
X[0,0]=1
X[1,1]=np.cos(a*(np.pi/180))
X[1,2]=np.sin(a*(np.pi/180))
X[2,1]=-np.sin(a*(np.pi/180))
X[2,2]=np.cos(a*(np.pi/180))
print("\n Rotation matrix about x axis; X = \n",X)
Y[1,1]=1
Y[0,0]=np.cos(b*(np.pi/180))
Y[0,2]=np.sin(b*(np.pi/180))
Y[2,0]=-np.sin(b*(np.pi/180))
Y[2,2]=np.cos(b*(np.pi/180))
print("\n Rotation matrix about y axis; Y = \n",Y)
Z[2,2]=1
Z[0,0]=np.cos(c*(np.pi/180))
Z[0,1]=np.sin(c*(np.pi/180))
Z[1,0]=-np.sin(c*(np.pi/180))
Z[1,1]=np.cos(c*(np.pi/180))
print("\n Rotation matrix about z axis; Z = \n",Z)
#Matrix multiplication
R=np.dot(X,np.dot(Y,Z))
print("\nRotational Matrix = X*Y*Z = \n",R)
print("\n Old Coordinates = \n",v)
p=np.dot(v,R)
print("\n New coordinates = R*X_old = \n",p)
v1 = np.zeros((22,3),float) #Array used for scatter plot
for i in range(11):
if i == 4:
v1[i]=p[i-4]
elif i == 9:
v1[i]=p[i-5]
elif i == 8:
v1[i]=p[7]
elif i < 4:
v1[i]=p[i]
elif i > 4 and i <= 7:
v1[i]=p[i-1]
v1[10]=p[0]
v1[11]=p[1]
v1[12]=p[5]
v1[13]=p[4]
v1[14]=p[0]
v1[15]=p[3]
v1[16]=p[2]
v1[17]=p[6]
v1[18]=p[5]
v1[19]=p[3]
v1[20]=p[3]
v1[21]=p[7] #inputting values into array v1
x,y,z=p.T
#storing x comps in x, y comps in y and z comps in z using unpacking.
x1,y1,z1=v1.T
print("The 3D cuboid after rotation (The output will be displayed in the browser)")
fig=go.Figure() #for plotting the 3D object
fig.add_trace(go.Scatter3d(x=x1,y=y1,z=z1,mode='lines+markers'))
fig.add_trace(
go.Mesh3d(x=x, y=y, z=z,
alphahull=1,
opacity=1,
color='orange')
)
plot(fig) #result output
ch=input("Want to rotate again? (Y/N) : ")