Ejemplo n.º 1
0
 def add_child(self, child, modify=False):
     """ Adds an object as a child in the scene graph. With modify=True, model_matrix_transform gets change from identity and prevents the changes of the coordinates of the child"""
     SceneGraph.add_child(self, child)
     self.notify()
     if modify:
         child._model_matrix_transform[:] = trans.inverse_matrix(
             self.model_matrix_global)
         child._normal_matrix_transform[:] = trans.inverse_matrix(
             self.normal_matrix_global)
Ejemplo n.º 2
0
    def update(self):
        """Calculate model, normal, and view matrices from position, rotation, and scale data."""
        to_update = super(Physical, self).update()
        if to_update:
            # Update Model, View, and Normal Matrices
            self.model_matrix = np.dot(self.position.to_matrix(),
                                       self.rotation.to_matrix())
            self.view_matrix = trans.inverse_matrix(self.model_matrix)
            self.model_matrix = np.dot(self.model_matrix,
                                       self.scale.to_matrix())
            self.normal_matrix = trans.inverse_matrix(self.model_matrix.T)

            self.notify_observers()
        return to_update
Ejemplo n.º 3
0
 def on_change(self):
     Physical.on_change(self)
     if self.parent:
         self.model_matrix_global = np.dot(self.parent.model_matrix_global, self._model_matrix)
         self.normal_matrix_global = np.dot(self.parent.normal_matrix_global, self._normal_matrix)
         self.view_matrix_global = trans.inverse_matrix(self._model_matrix_global)
         # self.view_matrix_global = np.dot(self.parent.view_matrix_global, self._view_matrix)
     else:
         self.model_matrix_global = self._model_matrix
         self.normal_matrix_global = self._normal_matrix
         self.view_matrix_global = self._view_matrix
Ejemplo n.º 4
0
    def on_change(self):
        Physical.on_change(self)
        mm_pg = self.parent.model_matrix_global if self.parent else np.identity(
            4, dtype=np.float32)
        nn_pg = self.parent.normal_matrix_global if self.parent else np.identity(
            4, dtype=np.float32)
        mm, mm_t = self._model_matrix, self._model_matrix_transform
        nn, nn_t = self._normal_matrix, self._normal_matrix_transform

        self.model_matrix_global = mm_pg.dot(mm_t).dot(mm)
        self.normal_matrix_global = nn_pg.dot(nn_t).dot(nn)
        self.view_matrix_global = trans.inverse_matrix(
            self._model_matrix_global)
Ejemplo n.º 5
0
 def on_change(self):
     self.model_matrix = np.dot(self.position.to_matrix(),
                                self.rotation.to_matrix())
     self.view_matrix = trans.inverse_matrix(self._model_matrix)
     self.model_matrix = np.dot(self._model_matrix, self.scale.to_matrix())
     self.normal_matrix = trans.inverse_matrix(self._model_matrix.T)
Ejemplo n.º 6
0
def rotate_and_print(facet, normal, axis, precision=10,plot=False,fibre_rad=0.02):
    import _transformations as tr
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.spatial import ConvexHull

    throat_area = 0.0
    throat_centroid = []
    offset_verts_3D = []
    output_offset = []
    " For boundaries some facets will already be aligned with the axis - if this is the case a rotation is unnecessary and could also cause problems "
    angle = tr.angle_between_vectors(normal,axis)
    if (angle==0.0)or(angle==np.pi):
        "We are already aligned"
        output = np.around(facet,precision)
        rotate_input = False
    else:
        rotate_input = True
        M = tr.rotation_matrix(tr.angle_between_vectors(normal,axis),tr.vector_product(normal,axis))
        rotated_facet = np.dot(facet,M[:3,:3].T)
        #output = np.around(tr.unit_vector(rotated_facet),precision)
        output = np.around(rotated_facet,precision)
    

    x = output[:,0]
    y = output[:,1]
    z = output[:,2]
    if (np.around(z.std(),3)!=0.000):
        print "Rotation failed"
    facet_coords_2D = np.column_stack((x,y)) ##THIS NEEDS REVISING IF WE WANT TO GENERALISE TO ANY AXIS BUT IT DOESN'T REALLY MATTER
    hull = ConvexHull(facet_coords_2D)
    
    " Work out span of points and set axes scales to cover this and be equal in both dimensions "
    x_range = x.max() - x.min()
    y_range = y.max() - y.min()
    if (x_range > y_range):
        my_range = x_range
    else:
        my_range = y_range
           
    lower_bound_x = x.min() - my_range*0.1
    upper_bound_x = x.min() + my_range*1.1
    lower_bound_y = y.min() - my_range*0.1
    upper_bound_y = y.min() + my_range*1.1

    " Now we want to effectively erode the facet the fibre radius to simulate the fibre" 
    " We need to check whether any vertices lie very close together and merge them if they do otherwise offsetting will not work "
    " Also if the range in values of the facet is less than the fibre diameter the facet is two small and should be ignored "
    tolerance = my_range*0.1
    verts_2D = facet_coords_2D[hull.vertices]
    fused_verts = fuse(verts_2D,tolerance)
    if (len(fused_verts) <3):
        print "Error: Fused Too Many Verts"
    elif(my_range < fibre_rad*2):
        print "Error: Facet Too small to Erode"
    else:
        offset = []
        for i,vert in enumerate(fused_verts):
            " Collect three adjacent points and compute the offset of the first "
            triplet = (vert, np.roll(fused_verts,-1,axis=0)[i],np.roll(fused_verts,1,axis=0)[i])
            offset.append(offset_vertex(triplet,fibre_rad))
        offset = np.asarray(offset)
        " At this point if everything went well the offset points should lie within the original convex hull "
        " If they don't then the offset value may be greater than the spread of points or we may still "
        " have some points very close together "
        " Make another convex hull including offset points and see whether the area has increased "
        " If this happens the throat is fully eroded and will have zero area "
            
        original_area = PolyArea2D(verts_2D)            
        all_points = np.concatenate((verts_2D,offset),axis=0)
        total_hull = ConvexHull(all_points)            
        total_area = PolyArea2D(all_points[total_hull.vertices])
        if (total_area>original_area):
            print "Error: Offset area larger than original"
        else:
            offset_hull = ConvexHull(offset)
            offset_verts_2D = offset[offset_hull.vertices]
            throat_area = PolyArea2D(offset_verts_2D)
            print "Throat Area: "+str(throat_area)
            " Make 3D again in rotated plane "
            offset_verts_3D = np.column_stack((offset_verts_2D,z[0:len(offset_verts_2D)]))
            " Get matrix to un-rotate the co-ordinates back to the original orientation if we rotated in the first place"
            if (rotate_input):
                M1 = tr.inverse_matrix(M)
                " Unrotate the offset coordinates "
                output_offset = np.dot(offset_verts_3D,M1[:3,:3].T)
            else:
                output_offset = offset_verts_3D
            " Calculate the centroid of the facet to calculate pore to pore distance later "
            throat_centroid = centroid(output_offset)
    
    if (plot):
        temp_fig = plt.figure()
        plt.axis((lower_bound_x,upper_bound_x,lower_bound_y,upper_bound_y))
        " Plot the convex Hull of the original points "
        for simplex in hull.simplices:
            plt.plot(output[simplex,0], output[simplex,1], 'k-',linewidth=2)
        plt.scatter(facet_coords_2D[:,0], facet_coords_2D[:,1])
        " Plot the convex Hull of the offset points "
        if (throat_area>0.0):       
            for offset_simplex in offset_hull.simplices:
                plt.plot(offset[offset_simplex,0], offset[offset_simplex,1], 'g-',linewidth=2)
            plt.scatter(offset[:,0], offset[:,1])

        temp_fig.show()
        
    return output,output_offset,throat_area,throat_centroid