-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrayons_01.py
More file actions
325 lines (231 loc) · 8.58 KB
/
Copy pathrayons_01.py
File metadata and controls
325 lines (231 loc) · 8.58 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
##### Mathgame #####
### Lancer de rayons ###
import numpy as np
# import sys
# sys.path.append('...')
from outils_mathgame import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
#######################################
## Lancer de rayon sur un plan/triangle
def intersection_plan(S, v, A, B, C):
""" Calcule l'intersection du rayon S + tv avec le plan (ABC). Renvoie t et coord. alpha, beta """
u = vecteur(A, B)
uu = vecteur(A, C)
n = produit_vectoriel(u, uu) # vecteur orthogonal au plan
p = produit_scalaire(v, n)
if p == 0: # pas d'intersection
return None
else:
t = produit_scalaire(vecteur(S, A), n) / p
alpha = - produit_mixte(vecteur(S, A), vecteur(A, C), v) / p
beta = - produit_mixte(vecteur(A, B), vecteur(S, A), n) / p
return t, alpha, beta
def intersection_triangle(S, v, A, B, C):
""" Calcule l'intersection du rayon S + tv avec le triangle (ABC). Renvoie t et coord. alpha, beta """
inter = intersection_plan(S, v, A, B, C)
if not inter: # Pas d'intersection avec le plan
return None
else:
t, alpha, beta = inter
if (t >= 0) and (alpha >= 0) and (beta >=0) and (alpha+beta <= 1):
return inter # intersection dans le triangle
else: # intersection en dehors du triangle
return None
def affichage_triangle(S, v, A, B, C):
""" Affichage de l'intersection rayon avec triangle """
fig = plt.figure()
ax = plt.axes(projection='3d')
# ax = plt.axes(projection='3d', proj_type = 'ortho')
ax.set_xlabel('axe x')
ax.set_ylabel('axe y')
ax.set_zlabel('axe z')
ax.view_init(15, -120)
ax.scatter(*S, color = 'green') # origine du rayon
ax.text(*S, "S")
ax.quiver(*S, *v, color = 'green') # vecteur
ax.scatter(*A, color = 'blue') # sommets du triangle
ax.scatter(*B, color = 'blue')
ax.scatter(*C, color = 'blue')
ax.text(*A, "A")
ax.text(*B, "B")
ax.text(*C, "C")
ax.plot([A[0],B[0],C[0],A[0]], [A[1],B[1],C[1],A[1]], [A[2],B[2],C[2],A[2]], color = 'blue')
lx, ly, lz = [A[0],B[0],C[0]], [A[1],B[1],C[1]], [A[2],B[2],C[2]]
verts = [list(zip(lx,ly,lz))]
ax.add_collection3d(Poly3DCollection(verts, alpha=0.2, color='blue'))
inter = intersection_plan(S, v, A, B, C)
if inter:
t, alpha, beta = inter
P = addition(S, multiplication_scalaire(t, v)) # P = S + t*v
if (t >= 0) and (alpha >= 0) and (beta >=0) and (alpha+beta <= 1):
ax.scatter(*P, color = 'red')
ax.text(*P, "P in")
else:
ax.scatter(*P, color = 'red')
ax.text(*P, "P out")
ax.plot([S[0],P[0]], [S[1],P[1]], [S[2],P[2]], color = 'red', lw=0.5) # vecteur
print(inter)
plt.tight_layout()
# plt.savefig('nomfichier.png')
plt.show()
return
# Test
S = (0, 0, 0)
v = (1, 1/2, 1)
A = (1, 2, 3)
B = (1, 0, 2)
C = (3, 1, 1)
# affichage_triangle(S, v, A, B, C)
#######################################
## Lancer de rayon sur une sphère
def intersection_sphere(S, v, O, R):
""" Calcule l'intersection du rayon S + tv avec une sphère centrée en O de rayon R """
vecSO = vecteur(S,O)
d2 = norme2(vecSO) - produit_scalaire(vecSO, v)**2 / norme2(v)
if R**2 - d2 >= 0: # intersection existe
tH = produit_scalaire(vecSO, v) / norme2(v)
h = np.sqrt(R**2 - d2)
n = norme(v)
t1 = tH - h/n
t2 = tH + h/n
return t1, t2, tH
else:
return None
def affichage_sphere(S, v, O, R):
""" Affichage de l'intersection rayon avec sphère """
fig = plt.figure()
# ax = plt.axes(projection='3d')
ax = plt.axes(projection='3d', proj_type = 'ortho')
ax.set_xlabel('axe x')
ax.set_ylabel('axe y')
ax.set_zlabel('axe z')
ax.view_init(15, -120)
ax.scatter(*S, color = 'green') # origine du rayon
ax.text(*S, "S")
ax.quiver(*S, *v, color = 'green') # vecteur
ax.scatter(*O, color = 'blue') # centre de la sphère
ax.text(*O, "O")
# Sphere
uu, vv = np.mgrid[0:2*np.pi:50j, 0:np.pi:50j]
x = O[0] + R*np.cos(uu)*np.sin(vv)
y = O[1] + R*np.sin(uu)*np.sin(vv)
z = O[2] + R*np.cos(vv)
# ax.plot_wireframe(x, y, z, color="r")
ax.plot_surface(x, y, z, linewidth=0.0, cstride=1, rstride=1, alpha=0.5)
inter = intersection_sphere(S, v, O, R)
if inter:
t1, t2, tH = inter
P1 = addition(S, multiplication_scalaire(t1, v)) # P = S + t*v
P2 = addition(S, multiplication_scalaire(t2, v))
H = addition(S, multiplication_scalaire(tH, v))
ax.scatter(*P1, color = 'red')
ax.text(*P1, "P1")
ax.scatter(*P2, color = 'red')
ax.text(*P2, "P2")
ax.scatter(*H, color = 'blue')
ax.text(*H, "H")
ax.plot([S[0],P1[0]], [S[1],P1[1]], [S[2],P1[2]], color = 'red', lw=0.5)
ax.plot([S[0],P2[0]], [S[1],P2[1]], [S[2],P2[2]], color = 'red', lw=0.5)
ax.plot([O[0],H[0]], [O[1],H[1]], [O[2],H[2]], color = 'blue', lw=0.5)
print(inter)
plt.tight_layout()
# plt.savefig('nomfichier.png')
plt.show()
return
# Test
S = (0, 0, 0)
v = (1, 1, 1)
O = (1, 2, 3)
R = 2
# affichage_sphere(S, v, O, R)
#######################################
## Lancer de rayon sur une boite
def intersection_boite(S, v, A, B):
""" Calcule l'intersection du rayon S + tv avec une boite délmitée par deux sommets opposés A et B """
tin = -np.inf
tout = +np.inf
xS, yS, zS = S
xv, yv, zv = v
xA, yA, zA = A
xB, yB, zB = B
if xv != 0:
txA = (xA - xS)/xv
txB = (xB - xS)/xv
tin = max(tin, min(txA, txB))
tout = min(tout, max(txA, txB))
if yv != 0:
tyA = (yA - yS)/yv
tyB = (yB - yS)/yv
tin = max(tin, min(tyA, tyB))
tout = min(tout, max(tyA, tyB))
if zv != 0:
tzA = (zA - zS)/zv
tzB = (zB - zS)/zv
tin = max(tin, min(tzA, tzB))
tout = min(tout, max(tzA, tzB))
if tin <= tout:
return tin, tout
else:
return None
def affichage_boite(S, v, A, B):
""" Affichage de l'intersection rayon avec boite """
fig = plt.figure()
ax = plt.axes(projection='3d')
# ax = plt.axes(projection='3d', proj_type = 'ortho')
ax.set_xlabel('axe x')
ax.set_ylabel('axe y')
ax.set_zlabel('axe z')
ax.view_init(15, -120)
ax.scatter(*S, color = 'green') # origine du rayon
ax.text(*S, "S")
ax.quiver(*S, *v, color = 'green') # vecteur
# Boite
xA, yA, zA = A
xB, yB, zB = B
ax.scatter(*A, color = 'blue') # sommets du triangle
ax.scatter(*B, color = 'blue')
ax.text(*A, "A")
ax.text(*B, "B")
# Les 6 faces à la main !!! What a shame!
lx, ly, lz = [xA,xB,xB,xA], [yA,yA,yB,yB], [zA,zA,zA,zA]
verts = [list(zip(lx,ly,lz))]
ax.add_collection3d(Poly3DCollection(verts, alpha=0.2, color='red'))
lx, ly, lz = [xA,xB,xB,xA], [yA,yA,yB,yB], [zB,zB,zB,zB]
verts = [list(zip(lx,ly,lz))]
ax.add_collection3d(Poly3DCollection(verts, alpha=0.2, color='red'))
lx, ly, lz = [xA,xB,xB,xA], [yA,yA,yA,yA], [zA,zA,zB,zB]
verts = [list(zip(lx,ly,lz))]
ax.add_collection3d(Poly3DCollection(verts, alpha=0.2, color='green'))
lx, ly, lz = [xA,xB,xB,xA], [yB,yB,yB,yB], [zA,zA,zB,zB]
verts = [list(zip(lx,ly,lz))]
ax.add_collection3d(Poly3DCollection(verts, alpha=0.2, color='green'))
lx, ly, lz = [xA,xA,xA,xA], [yA,yB,yB,yA], [zA,zA,zB,zB]
verts = [list(zip(lx,ly,lz))]
ax.add_collection3d(Poly3DCollection(verts, alpha=0.2, color='blue'))
lx, ly, lz = [xB,xB,xB,xB], [yA,yB,yB,yA], [zA,zA,zB,zB]
verts = [list(zip(lx,ly,lz))]
ax.add_collection3d(Poly3DCollection(verts, alpha=0.2, color='blue'))
inter = intersection_boite(S, v, A, B)
if inter:
t1, t2 = inter
P1 = addition(S, multiplication_scalaire(t1, v)) # P = S + t*v
P2 = addition(S, multiplication_scalaire(t2, v))
ax.scatter(*P1, color = 'red')
ax.text(*P1, "Pin")
ax.scatter(*P2, color = 'red')
ax.text(*P2, "Pout")
ax.plot([S[0],P1[0]], [S[1],P1[1]], [S[2],P1[2]], color = 'red', lw=0.5)
ax.plot([S[0],P2[0]], [S[1],P2[1]], [S[2],P2[2]], color = 'red', lw=0.5)
print(inter)
plt.tight_layout()
# plt.savefig('nomfichier.png')
plt.show()
return
# Test
S = (0, 0, 0)
v = (1, 1, 2)
A = (1, 2, 1)
B = (4, 5, 6)
affichage_boite(S, v, A, B)