def __init__(self, p, k, wv=0.588): self.__check(p, k) inp = np.array(p) ink = np.array(k) innormk = ink / mag(ink) # normalised direction vector self._log = [[inp, innormk]] self._wv = wv self._terminated = False
def __init__(self, p, k, wv=0.588): self.__check(p,k) inp = np.array(p) ink = np.array(k) innormk = ink / mag(ink) # normalised direction vector self._log = [[inp, innormk]] self._wv = wv self._terminated = False
def disperse_ray(self, ray): if ray._terminated: return None q = self.intercept(ray) if q is None: ray._terminated = True return None unitk = ray.k() / mag(ray.k()) normal = self._normal unitnormal = normal / mag(normal) wv = ray._wv n2 = np.sqrt(1 + (1.03961212*wv**2)/(wv**2-0.00600069867) + (0.231792344*wv**2)/(wv**2-0.0200179144) + (1.01046945*wv**2)/(wv**2-103.560653)) newdirection = self.snellrefract(unitk, unitnormal, 1, n2) if newdirection is None: ray._terminated = True return None ray.append(q.tolist(), newdirection.tolist())
def disperse_ray(self, ray): if ray._terminated: return None q = self.intercept(ray) if q is None: ray._terminated = True return None unitk = ray.k() / mag(ray.k()) normal = self._normal unitnormal = normal / mag(normal) wv = ray._wv n2 = np.sqrt(1 + (1.03961212 * wv**2) / (wv**2 - 0.00600069867) + (0.231792344 * wv**2) / (wv**2 - 0.0200179144) + (1.01046945 * wv**2) / (wv**2 - 103.560653)) newdirection = self.snellrefract(unitk, unitnormal, 1, n2) if newdirection is None: ray._terminated = True return None ray.append(q.tolist(), newdirection.tolist())
def append(self, p, k): "Adds a new point and direction to the log." self.__check(p,k) inp = np.array(p) if k is None: self._log.append([inp, None]) else: ink = np.array(k) innormk = ink / mag(ink) self._log.append([inp,innormk])
def append(self, p, k): "Adds a new point and direction to the log." self.__check(p, k) inp = np.array(p) if k is None: self._log.append([inp, None]) else: ink = np.array(k) innormk = ink / mag(ink) self._log.append([inp, innormk])
def snellrefract(self, incident, normal, n1, n2): ndotk = dotpr(normal, incident) if ndotk < 0: # we require positive n dot k ndotk = dotpr(-normal, incident) ratio = n1 / float(n2) sinincident = np.sqrt(1-(ndotk**2)) if sinincident > n2/float(n1): # discard total internal reflection return None refracted = ratio*incident + (ratio*ndotk - np.sqrt(1-((ratio**2)*(1-ndotk**2))))*normal normrefracted = refracted / mag(refracted) return normrefracted
def propagate_ray(self, ray): if ray._terminated: return None q = self.intercept(ray) if q is None: ray._terminated = True return None unitk = ray.k() / mag(ray.k()) if self._curvature == 0: normal = np.array([0,0,-1]) elif self._curvature < 0: normal = self._origin - q else: normal = q - self._origin unitnormal = normal / mag(normal) newdirection = self.reflect(unitk, unitnormal) if newdirection is None: ray._terminated = True return None ray.append(q.tolist(), newdirection.tolist())
def propagate_ray(self, ray): if ray._terminated: # do not propagate if previously terminated return None q = self.intercept(ray) if q is None: ray._terminated = True return None unitk = ray.k() / mag(ray.k()) if self._curvature == 0: # set normal for plane surface normal = np.array([0, 0, -1]) elif self._curvature < 0: # we require the normal to be normal = self._origin - q # opposite the ray direction else: normal = q - self._origin unitnormal = normal / mag(normal) newdirection = self.snellrefract(unitk, unitnormal, self._n1, self._n2) if newdirection is None: ray._terminated = True return None ray.append(q.tolist(), newdirection.tolist())
def propagate_ray(self, ray): if ray._terminated: # do not propagate if previously terminated return None q = self.intercept(ray) if q is None: ray._terminated = True return None unitk = ray.k() / mag(ray.k()) if self._curvature == 0: # set normal for plane surface normal = np.array([0,0,-1]) elif self._curvature < 0: # we require the normal to be normal = self._origin - q # opposite the ray direction else: normal = q - self._origin unitnormal = normal / mag(normal) newdirection = self.snellrefract(unitk, unitnormal, self._n1, self._n2) if newdirection is None: ray._terminated = True return None ray.append(q.tolist(), newdirection.tolist())
def propagate_ray(self, ray): if ray._terminated: return None q = self.intercept(ray) if q is None: ray._terminated = True return None unitk = ray.k() / mag(ray.k()) if self._curvature == 0: normal = np.array([0, 0, -1]) elif self._curvature < 0: normal = self._origin - q else: normal = q - self._origin unitnormal = normal / mag(normal) newdirection = self.reflect(unitk, unitnormal) if newdirection is None: ray._terminated = True return None ray.append(q.tolist(), newdirection.tolist())
def snellrefract(self, incident, normal, n1, n2): ndotk = dotpr(normal, incident) if ndotk < 0: # we require positive n dot k ndotk = dotpr(-normal, incident) ratio = n1 / float(n2) sinincident = np.sqrt(1 - (ndotk**2)) if sinincident > n2 / float(n1): # discard total internal reflection return None refracted = ratio * incident + (ratio * ndotk - np.sqrt(1 - ( (ratio**2) * (1 - ndotk**2)))) * normal normrefracted = refracted / mag(refracted) return normrefracted
def intercept(self, ray): if self._curvature == 0: return self.zerocurveintercept(ray) r = ray.p() - self._origin k = ray.k() unitk = k / mag(k) rdotk = dotpr(r, unitk) if (rdotk)**2 - ((mag(r))**2 - self._R**2) < 0: return None root = np.sqrt((rdotk)**2 - ((mag(r))**2 - self._R**2)) toreturn = [] plusl = -(rdotk) + root if plusl > 0: # need forward direction of ray plusq = ray.p() + (plusl * unitk) plusqzdist = mag(plusq[0:2]) - self._apradius if plusqzdist <= 0: # if intercept is within aperture radius toreturn.append(plusq) minusl = -(rdotk) - root if minusl > 0: minusq = ray.p() + (minusl * unitk) minusqzdist = mag(minusq[0:2]) - self._apradius if minusqzdist <= 0: toreturn.append(minusq) if len(toreturn) == 0: return None elif len(toreturn) == 1: return toreturn[0] elif len(toreturn) == 2: if self._curvature > 0: if plusl <= minusl: # for +ve curv. we require smaller l return plusq else: return minusq else: if plusl >= minusl: # for -ve curv. we require larger l return plusq else: return minusq
def intercept(self, ray): if self._curvature == 0: return self.zerocurveintercept(ray) r = ray.p() - self._origin k = ray.k() unitk = k / mag(k) rdotk = dotpr(r, unitk) if (rdotk)**2-((mag(r))**2-self._R**2) < 0: return None root = np.sqrt((rdotk)**2-((mag(r))**2-self._R**2)) toreturn = [] plusl = -(rdotk) + root if plusl > 0: # need forward direction of ray plusq = ray.p() + (plusl * unitk) plusqzdist = mag(plusq[0:2])-self._apradius if plusqzdist <= 0: # if intercept is within aperture radius toreturn.append(plusq) minusl = -(rdotk) - root if minusl > 0: minusq = ray.p() + (minusl * unitk) minusqzdist = mag(minusq[0:2])-self._apradius if minusqzdist <= 0: toreturn.append(minusq) if len(toreturn) == 0: return None elif len(toreturn) == 1: return toreturn[0] elif len(toreturn) == 2: if self._curvature > 0: if plusl <= minusl: # for +ve curv. we require smaller l return plusq else: return minusq else: if plusl >= minusl: # for -ve curv. we require larger l return plusq else: return minusq
def cosine_similarity(a, b) -> float: """Calculates the angle between two vectors. If used on two word embedding vectors, the effect is to return the similarity of their texts.""" similarity = dot(a, b) / (mag(a) * mag(b)) return similarity
def reflect(self, incident, normal): ndotk = dotpr(normal, incident) reflected = incident - 2*(ndotk)*normal normreflected = reflected / mag(reflected) return normreflected
def reflect(self, incident, normal): ndotk = dotpr(normal, incident) reflected = incident - 2 * (ndotk) * normal normreflected = reflected / mag(reflected) return normreflected