def photon(self): photon = Photon() photon.source = self.source_id photon.id = self.throw self.throw = self.throw + 1 intphi = np.random.randint(1, self.spacing+1) inttheta = np.random.randint(1, self.spacing+1) phi = intphi*(self.phimax-self.phimin)/self.spacing if self.thetamin == self.thetamax: theta = self.thetamin else: theta = inttheta*(self.thetamax-self.thetamin)/self.spacing x = np.cos(phi)*np.sin(theta) y = np.sin(phi)*np.sin(theta) z = np.cos(theta) direction = (x,y,z) transform = tf.translation_matrix((0,0,0)) point = transform_point(self.center, transform) photon.direction = direction photon.position = point if self.spectrum != None: photon.wavelength = self.spectrum.wavelength_at_probability(np.random.uniform()) else: photon.wavelength = self.wavelength photon.active = True return photon
def photon(self): photon = Photon() photon.source = self.source_id photon.id = self.throw self.throw = self.throw + 1 phi = np.random.uniform(self.phimin, self.phimax) theta = np.random.uniform(self.thetamin, self.thetamax) x = np.cos(phi) * np.sin(theta) y = np.sin(phi) * np.sin(theta) z = np.cos(theta) direction = (x, y, z) transform = tf.translation_matrix((0, 0, 0)) point = transform_point(self.center, transform) photon.direction = direction photon.position = point if self.spectrum != None: photon.wavelength = self.spectrum.wavelength_at_probability( np.random.uniform()) else: photon.wavelength = self.wavelength photon.active = True return photon
def coil_transform_pos(pos): pos[1] = -pos[1] a, b, g = np.radians(pos[3:]) r_ref = tf.euler_matrix(a, b, g, 'sxyz') t_ref = tf.translation_matrix(pos[:3]) m_img = tf.concatenate_matrices(t_ref, r_ref) return m_img
def _draw(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glClearColor(0.0, 0.0, 0.0, 1.0) view_mat = translation_matrix((0, 0, -self.dist)) view_mat = view_mat.dot(self.ball.matrix()) view_mat = view_mat.dot(scale_matrix(self.zoom)) self.VMatrix = view_mat self.draw_hook() OpenGL.GLUT.glutSwapBuffers()
def photon(self): photon = Photon() photon.source = self.source_id photon.id = self.throw self.throw = self.throw + 1 #This does not work, since the area element scales with Sin(theta) d theta d phi #See http://mathworld.wolfram.com/SpherePointPicking.html #Reimplementing the Randomizer phi = np.random.uniform(self.phimin, self.phimax) #theta = np.random.uniform(self.thetamin, self.thetamax) theta = -1 while theta > self.thetamax or theta < self.thetamin: theta = np.arccos(2 * np.random.uniform(0, 1) - 1) x = np.cos(phi) * np.sin(theta) y = np.sin(phi) * np.sin(theta) z = np.cos(theta) direction = (x, y, z) transform = tf.translation_matrix((0, 0, 0)) point = transform_point(self.center, transform) photon.direction = direction photon.position = point if self.spectrum != None: photon.wavelength = self.spectrum.wavelength_at_probability( np.random.uniform()) else: photon.wavelength = self.wavelength photon.active = True return photon
def photon(self): photon = Photon() photon.source = self.source_id photon.id = self.throw self.throw = self.throw + 1 #This does not work, since the area element scales with Sin(theta) d theta d phi #See http://mathworld.wolfram.com/SpherePointPicking.html #Reimplementing the Randomizer phi = np.random.uniform(self.phimin, self.phimax) #theta = np.random.uniform(self.thetamin, self.thetamax) theta = -1 while theta > self.thetamax or theta < self.thetamin : theta = np.arccos(2* np.random.uniform(0,1)-1) x = np.cos(phi)*np.sin(theta) y = np.sin(phi)*np.sin(theta) z = np.cos(theta) direction = (x,y,z) transform = tf.translation_matrix((0,0,0)) point = transform_point(self.center, transform) photon.direction = direction photon.position = point if self.spectrum != None: photon.wavelength = self.spectrum.wavelength_at_probability(np.random.uniform()) else: photon.wavelength = self.wavelength photon.active = True return photon
def translate(self, translation): self.shape.append_transform(tf.translation_matrix(translation))
def intersection(self, ray): """ Returns all forward intersection points with ray and the capped cylinder. The intersection algoithm is taken from, "Intersecting a Ray with a Cylinder" Joseph M. Cychosz and Warren N. Waggenspack, Jr., in "Graphics Gems IV", Academic Press, 1994. >>> cld = Cylinder(1.0, 1.0) >>> cld.intersection(Ray([0.0,0.0,0.5], [1,0,0])) [array([ 1. , 0. , 0.5])] >>> cld.intersection(Ray([-5,0.0,0.5], [1,0,0])) [array([-1. , 0. , 0.5]), array([ 1. , 0. , 0.5])] >>> cld.intersection(Ray([.5,.5,-1], [0,0,1])) [array([ 0.5, 0.5, 1. ]), array([ 0.5, 0.5, 0. ])] >>> cld.intersection( Ray([0.0,0.0,2.0], [0,0,-1])) [array([ 0., 0., 1.]), array([ 0., 0., 0.])] >>> cld.intersection(Ray([-0.2, 1.2,0.5],[0.75498586, -0.53837322, 0.37436697])) [array([ 0.08561878, 0.99632797, 0.64162681]), array([ 0.80834999, 0.48095523, 1. ])] >>> cld.intersection(Ray(position=[ 0.65993112596983427575736414, -0.036309587083015459896273569, 1. ], direction=[ 0.24273873128664008591570678, -0.81399482405912471083553328, 0.52772183462341881732271531])) [array([ 0.65993113, -0.03630959, 1. ])] >>> cld.transform = tf.translation_matrix([0,0,1]) >>> cld.intersection(Ray([-5,0.0,1.5], [1,0,0])) [array([-1. , 0. , 1.5]), array([ 1. , 0. , 1.5])] >>> cld.transform = tf.identity_matrix() >>> cld.transform = tf.rotation_matrix(0.25*np.pi, [1,0,0]) >>> cld.intersection(Ray([-5,-.5,-0.25], [1,0,0])) [array([-0.84779125, -0.5 , -0.25 ]), array([ 0.84779125, -0.5 , -0.25 ])] """ # Inverse transform the ray to get it into the cylinders local frame inv_transform = tf.inverse_matrix(self.transform) rpos = transform_point(ray.position, inv_transform) rdir = transform_direction(ray.direction, inv_transform) direction = np.array([0,0,1]) normal = np.cross(rdir, direction) normal_magnitude = magnitude(normal) #print normal_magnitude, "Normal magnitude" if cmp_floats(normal_magnitude, .0): # Ray parallel to cylinder direction normal = norm(normal) #d = abs(np.dot(rpos, direction)) #D = rpos - d * np.array(direction) #if magnitude(D) <= self.radius: # Axis aligned ray inside the cylinder volume only hits caps #print "Inside axis aligned ray only hits caps" bottom = Plane() top = Plane() top.transform = tf.translation_matrix([0,0,self.length]) p0 = top.intersection(Ray(rpos, rdir)) p1 = bottom.intersection(Ray(rpos, rdir)) cap_intersections = [] if p0 != None: cap_intersections.append(p0) if p1 != None: cap_intersections.append(p1) points = [] for point in cap_intersections: if point[0] != None: point = point[0] point_radius = np.sqrt(point[0]**2 + point[1]**2) if point_radius <= self.radius: #print "Hit cap at point:" #print point #print "" points.append(point) if len(points) > 0: world_points = [] for pt in points: world_points.append(transform_point(pt, self.transform)) #print "Local points", points #print "World points", world_points return world_points return None # finish axis parallel branch #print "Not parallel to cylinder axis." #print "" normal = norm(normal) d = abs(np.dot(rpos, normal)) if d <= self.radius: #Hit quadratic surface O = np.cross(rpos, direction) t = - np.dot(O,normal) / normal_magnitude O = np.cross(normal, direction) O = norm(O) s = abs(np.sqrt(self.radius**2 - d**2) / np.dot(rdir, O)) t0 = t - s p0 = rpos + t0 * rdir t1 = t + s p1 = rpos + t1 * rdir points = [] if (t0 >= 0.0) and (.0 <= p0[2] <= self.length): points.append(p0) if (t1 >= 0.0) and (.0 <= p1[2] <= self.length): points.append(p1) #print "Hits quadratic surface with t0 and t1, ", t0, t1 #print "" #print "Intersection points:" #p0 = rpos + t0 * rdir #p1 = rpos + t1 * rdir # Check that hit quadratic surface in the length range #points = [] #if (.0 <= p0[2] <= self.length) and not Ray(rpos, rdir).behind(p0): # points.append(p0) # #if (.0 <= p1[2] <= self.length) and not Ray(rpos, rdir).behind(p1): # points.append(p1) #print points #Now compute intersection with end caps #print "Now to calculate caps intersections" bottom = Plane() top = Plane() top.transform = tf.translation_matrix([0,0,self.length]) p2 = top.intersection(Ray(rpos, rdir)) p3 = bottom.intersection(Ray(rpos, rdir)) cap_intersections = [] if p2 != None: cap_intersections.append(p2) if p3 != None: cap_intersections.append(p3) for point in cap_intersections: if point[0] != None: point = point[0] point_radius = np.sqrt(point[0]**2 + point[1]**2) if point_radius <= self.radius: #print "Hit cap at point:" #print point #print "" points.append(point) #print points if len(points) > 0: world_points = [] for pt in points: world_points.append(transform_point(pt, self.transform)) return world_points return None