argiope/mesh.py
def centroids_and_volumes(self, sort_index = True):
    """
    Returns a dataframe containing volume and centroids of all the elements.
    """
    elements = self.elements
    out = []
    for etype, group in self.elements.groupby([("type", "argiope", "")]):
      etype_info = ELEMENTS[etype]
      simplices_info = etype_info.simplices
      index = group.index
      simplices_data = self.split(into = "simplices", 
                             loc = index,
                             at = "coords") 
      simplices = simplices_data.values.reshape(
                  index.size, 
                  simplices_info.shape[0], 
                  simplices_info.shape[1], 
                  3) 
      edges = simplices[:,:,1:] - simplices[:,:,:1] 
      simplices_centroids = simplices.mean(axis = 2)
      if etype_info.space == 2:
        simplices_volumes = np.linalg.norm(
                  np.cross(edges[:,:,0], 
                           edges[:,:,1], 
                           axis = 2),
                  axis = 2)/2.
      elif etype_info.space == 3:          
        simplices_volumes =  (np.cross(edges[:,:,0], 
                                       edges[:,:,1], axis = 2) 
                             * edges[:,:, 2]).sum(axis = 2) / 6.
      elements_volumes = simplices_volumes.sum(axis = 1)
      elements_centroids = ((simplices_volumes.reshape(*simplices_volumes.shape, 1) 
                          * simplices_centroids).sum(axis = 1) 
                          / elements_volumes.reshape(*elements_volumes.shape,1))
      volumes_df = pd.DataFrame(index = index,
                                data = elements_volumes,
                                columns = pd.MultiIndex.from_product(
                                [["volume"], [""]]))
      centroids_df = pd.DataFrame(index = index,
                                data = elements_centroids,
                                columns = pd.MultiIndex.from_product(
                                [["centroid"], ["x", "y", "z"]]))                          
      out.append(pd.concat([volumes_df, centroids_df], axis = 1))             
    out = pd.concat(out)  
    if sort_index: out.sort_index(inplace = True)
    return out.sort_index(axis= 1)
def centroids_and_volumes(self, sort_index = True):
    elements = self.elements
    out = []
    for etype, group in self.elements.groupby([("type", "argiope", "")]):
      etype_info = ELEMENTS[etype]
      simplices_info = etype_info.simplices
      index = group.index
      simplices_data = self.split(into = "simplices", 
                             loc = index,
                             at = "coords") 
      simplices = simplices_data.values.reshape(
                  index.size, 
                  simplices_info.shape[0], 
                  simplices_info.shape[1], 
                  3) 
      edges = simplices[:,:,1:] - simplices[:,:,:1] 
      simplices_centroids = simplices.mean(axis = 2)
      if etype_info.space == 2:
        simplices_volumes = np.linalg.norm(
                  np.cross(edges[:,:,0], 
                           edges[:,:,1], 
                           axis = 2),
                  axis = 2)/2.
      elif etype_info.space == 3:          
        simplices_volumes =  (np.cross(edges[:,:,0], 
                                       edges[:,:,1], axis = 2) 
                             * edges[:,:, 2]).sum(axis = 2) / 6.
      elements_volumes = simplices_volumes.sum(axis = 1)
      elements_centroids = ((simplices_volumes.reshape(*simplices_volumes.shape, 1) 
                          * simplices_centroids).sum(axis = 1) 
                          / elements_volumes.reshape(*elements_volumes.shape,1))
      volumes_df = pd.DataFrame(index = index,
                                data = elements_volumes,
                                columns = pd.MultiIndex.from_product(
                                [["volume"], [""]]))
      centroids_df = pd.DataFrame(index = index,
                                data = elements_centroids,
                                columns = pd.MultiIndex.from_product(
                                [["centroid"], ["x", "y", "z"]]))                          
      out.append(pd.concat([volumes_df, centroids_df], axis = 1))             
    out = pd.concat(out)  
    if sort_index: out.sort_index(inplace = True)
    return out.sort_index(axis= 1)
def centroids_and_volumes ( self , sort_index = True ) : elements = self . elements out = [ ] for etype , group in self . elements . groupby ( [ ( "type" , "argiope" , "" ) ] ) : etype_info = ELEMENTS [ etype ] simplices_info = etype_info . simplices index = group . index simplices_data = self . split ( into = "simplices" , loc = index , at = "coords" ) simplices = simplices_data . values . reshape ( index . size , simplices_info . shape [ 0 ] , simplices_info . shape [ 1 ] , 3 ) edges = simplices [ : , : , 1 : ] - simplices [ : , : , : 1 ] simplices_centroids = simplices . mean ( axis = 2 ) if etype_info . space == 2 : simplices_volumes = np . linalg . norm ( np . cross ( edges [ : , : , 0 ] , edges [ : , : , 1 ] , axis = 2 ) , axis = 2 ) / 2. elif etype_info . space == 3 : simplices_volumes = ( np . cross ( edges [ : , : , 0 ] , edges [ : , : , 1 ] , axis = 2 ) * edges [ : , : , 2 ] ) . sum ( axis = 2 ) / 6. elements_volumes = simplices_volumes . sum ( axis = 1 ) elements_centroids = ( ( simplices_volumes . reshape ( * simplices_volumes . shape , 1 ) * simplices_centroids ) . sum ( axis = 1 ) / elements_volumes . reshape ( * elements_volumes . shape , 1 ) ) volumes_df = pd . DataFrame ( index = index , data = elements_volumes , columns = pd . MultiIndex . from_product ( [ [ "volume" ] , [ "" ] ] ) ) centroids_df = pd . DataFrame ( index = index , data = elements_centroids , columns = pd . MultiIndex . from_product ( [ [ "centroid" ] , [ "x" , "y" , "z" ] ] ) ) out . append ( pd . concat ( [ volumes_df , centroids_df ] , axis = 1 ) ) out = pd . concat ( out ) if sort_index : out . sort_index ( inplace = True ) return out . sort_index ( axis = 1 )
Returns a dataframe containing volume and centroids of all the elements.
Returns a dataframe containing volume and centroids of all the elements .