-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflection.py
236 lines (227 loc) · 6.72 KB
/
reflection.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
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
#!/usr/bin/env python3
# Keith Briggs 2017-02-02
# Hayley Wragg 2017-03-28
# Hayley Wragg 2017-04-12
# Hayley Wragg 2017-05-15
''' Code to Reflect a line in an edge without using Shapely '''
from math import atan2,hypot,sqrt,copysign
import numpy as np
import matplotlib.pyplot as mp
import HayleysPlotting as hp
import intersection as inter
from numpy import *
import linefunctions as lf
def try_reflect_ray(ray,edge):
''' Reflection of a ray which goes through the points source or
previous intersect and next intersect.ray.get_origin '''
# Find the distances which need to be translated
trdist=-ray[1]
direc=lf.Direction(ray)
# Translate the points before the reflection
o=trdist
ray+=trdist
edge+=trdist
# Find the image of the ray in the edge
Image=ray[1]+direc
# Use the unit edge instead of the exact edge for the reflection
unitedge=np.array([ray[1],edge[1]])
unitedge=(1.0/lf.length(unitedge))*unitedge
# Find the normal to the edge
normedge=np.array([[unitedge[1][1],-unitedge[1][0]],ray[1]])
# Find the reflection using the Image
ReflPt=Image-2*np.dot(normedge[0],Image)*normedge[0]
#Translate Back
ray-=trdist
edge-=trdist
normedge-=trdist
ReflPt-=trdist
o-=trdist
return np.array([ray[1], ReflPt]), normedge
def try_3D_reflect_ray(ray,plane):
''' Take a ray and find it's reflection in a plane. '''
# Find the distances which need to be translated
trdist=-ray[1]
direc=lf.Direction3D(ray)
# Translate the points before the reflection
o=trdist
ray+=trdist
plane[0]+=trdist
# Find the image of the ray in the edge
Image=ray[1]+direc
# Find the normal to the edge
normedge=plane[1]
# Find the reflection using the Image
ReflPt=Image-2*np.dot(normedge,Image)*normedge
#Translate Back
ray-=trdist
plane[0]-=trdist
normedge-=trdist
ReflPt-=trdist
o-=trdist
return np.array([ray[1], ReflPt]), normedge
def errorcheck(err,ray,ref,normedge):
''' Take the input ray and output ray and the normal to the edge,
check that both vectors have the same angle to the normal'''
# Convert to the Direction vectors
ray=lf.Direction(ray)
normedge=lf.Direction(normedge)
ref=lf.Direction(ref)
# Find the angles
#FIXME check the cosine rule
theta1=np.arccos(abs(np.dot(ray,normedge))/(np.linalg.norm(ray)*np.linalg.norm(normedge)))
theta2=np.arccos(abs(np.dot(ref,normedge))/(np.linalg.norm(ref)*np.linalg.norm(normedge)))
if (abs(theta1-theta2)>1.0E-7):
err+=1.0
return err
def errorcheck3D(err,ray,ref,normedge):
''' Take the input ray and output ray and the normal to the edge,
check that both vectors have the same angle to the normal'''
# Convert to the Direction vectors
ray=lf.Direction3D(ray)
#normedge=lf.Direction3D(normedge)
ref=lf.Direction3D(ref)
# Find the angles
#FIXME check the cosine rule
theta1=np.arccos(abs(np.dot(ray,normedge))/(np.linalg.norm(ray)*np.linalg.norm(normedge)))
theta2=np.arccos(abs(np.dot(ref,normedge))/(np.linalg.norm(ref)*np.linalg.norm(normedge)))
if (abs(theta1-theta2)>1.0E-7):
err+=1.0
return err
def test3D(err):
l1=np.array([[0.0,0.0,0.0],[0.0,0.0,1.0]])
plane=np.array([[0.0,1.0,2.0],[0.0,0.0,1.0]])
interp=inter.intersection3D(l1,plane)
ray=np.array([l1[0],interp])
ref,norm=try_3D_reflect_ray(ray,plane)
err=errorcheck3D(err,ray,ref,norm)
return err
def test():
# Set Error term to zero
err=0
i=0
#Test1
l1=np.array([[0.0,0.0],[1.0,1.0]])
e1=np.array([[0.0,0.5],[2.0,0.5]])
#mp.figure(i+1)
#i=i+1
#hp.Plotedge(e1,'b')
interPoint=inter.intersection(l1,e1)
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref,normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
#hp.Plotedge(ref,'g')
#hp.Plotedge(e1,'b')
#hp.Plotedge(ray,'r')
#Test2
l1=np.array([[1.0,0.0],[-1.0,3.0]])
e1=np.array([[0.0,0.5],[2.0,0.5]])
interPoint=inter.intersection(l1,e1)
#mp.figure(i+1)
#i=i+1
#hp.Plotedge(e1,'b')
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref, normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
#hp.Plotedge(ref,'g')
#hp.Plotedge(e1,'b')
#hp.Plotedge(ray,'r')
#Test3
l1=np.array([[0.0, 1.0],[3.0,2.0]])
e1=np.array([[0.0,2.0],[2.0,0.0]])
#mp.figure(i+1)
#i=i+1
#hp.Plotedge(e1,'b')
interPoint=inter.intersection(l1,e1)
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref, normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
#hp.Plotedge(ref,'g')
#hp.Plotedge(ray,'r')
#Test4
l1=np.array([[0.0, 1.0],[3.0,-5.0]])
e1=np.array([[0.0,-2.0],[5.0, -2.0]])
interPoint=inter.intersection(l1,e1)
mp.figure(i+1)
i=i+1
hp.Plotedge(e1,'b')
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref,normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
hp.Plotedge(ref,'g')
hp.Plotedge(ray,'r')
#Test5
l1=np.array([[0.0, 1.0],[3.0,-5.0]])
e1=np.array([[0.0,-2.0],[5.0, -1.0]])
mp.figure(i+1)
i=i+1
hp.Plotedge(e1,'b')
interPoint=inter.intersection(l1,e1)
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref,normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
hp.Plotedge(ref,'g')
hp.Plotedge(ray,'r')
#Test6
l1=np.array([[0.0, 1.0],[3.0,-5.0]])
e1=np.array([[0.0,-2.0],[5.0, -3.0]])
mp.figure(i+1)
i=i+1
hp.Plotedge(e1,'b')
interPoint=inter.intersection(l1,e1)
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref,normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
hp.Plotedge(ref,'g')
hp.Plotedge(ray,'r')
##Test7
l1=np.array([[0.0, 1.0],[7.0,-5.0]])
e1=np.array([[0.0,-2.0],[10.0, -4.0]])
mp.figure(i+1)
i=i+1
hp.Plotedge(e1,'b')
interPoint=inter.intersection(l1,e1)
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref,normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
hp.Plotedge(ref,'g')
hp.Plotedge(ray,'r')
else:
hp.Plotline(l1,5,'r')
#Test8
l1=np.array([[0.0, 1.0],[-7.0/5.0,-1.0]])
e1=np.array([[0.0,-2.0],[-5.0, -3.0]])
interPoint=inter.intersection(l1,e1)
mp.figure(i+1)
i=i+1
hp.Plotedge(e1,'b')
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref,normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
hp.Plotedge(ref,'g')
hp.Plotedge(ray,'r')
else:
hp.Plotline(l1,5,'r')
#Test9
l1=np.array([[0.0, 1.0],[-7.0,-5.0]])
e1=np.array([[-5.0,0.0],[5.0, 0.0]])
interPoint=inter.intersection(l1,e1)
mp.figure(i+1)
hp.Plotedge(e1,'b')
if interPoint[0]:
ray=np.array([l1[0],interPoint])
ref,normedge=try_reflect_ray(ray,e1)
err=errorcheck(err,ray,ref,normedge)
hp.Plotedge(ref,'g')
hp.Plotedge(ray,'r')
else:
hp.Plotline(l1,5,'r')
#mp.show(5)
return err