meshlabxml/files.py
def measure_aabb(fbasename=None, log=None, coord_system='CARTESIAN'):
    """ Measure the axis aligned bounding box (aabb) of a mesh
    in multiple coordinate systems.

    Args:
        fbasename (str): filename of input model
        log (str): filename of log file
        coord_system (enum in ['CARTESIAN', 'CYLINDRICAL']
            Coordinate system to use:
                'CARTESIAN': lists contain [x, y, z]
                'CYLINDRICAL': lists contain [r, theta, z]
    Returns:
        dict: dictionary with the following aabb properties
            min (3 element list): minimum values
            max (3 element list): maximum values
            center (3 element list): the center point
            size (3 element list): size of the aabb in each coordinate (max-min)
            diagonal (float): the diagonal of the aabb
    """
    # TODO: add center point, spherical coordinate system
    fext = os.path.splitext(fbasename)[1][1:].strip().lower()
    if fext != 'xyz':
        fin = 'TEMP3D_aabb.xyz'
        run(log=log, file_in=fbasename, file_out=fin, script=None)
    else:
        fin = fbasename
    fread = open(fin, 'r')
    aabb = {'min': [999999.0, 999999.0, 999999.0], 'max': [-999999.0, -999999.0, -999999.0]}
    for line in fread:
        x_co, y_co, z_co = line.split()
        x_co = util.to_float(x_co)
        y_co = util.to_float(y_co)
        z_co = util.to_float(z_co)
        if coord_system == 'CARTESIAN':
            if x_co < aabb['min'][0]:
                aabb['min'][0] = x_co
            if y_co < aabb['min'][1]:
                aabb['min'][1] = y_co
            if z_co < aabb['min'][2]:
                aabb['min'][2] = z_co
            if x_co > aabb['max'][0]:
                aabb['max'][0] = x_co
            if y_co > aabb['max'][1]:
                aabb['max'][1] = y_co
            if z_co > aabb['max'][2]:
                aabb['max'][2] = z_co
        elif coord_system == 'CYLINDRICAL':
            radius = math.sqrt(x_co**2 + y_co**2)
            theta = math.degrees(math.atan2(y_co, x_co))
            if radius < aabb['min'][0]:
                aabb['min'][0] = radius
            if theta < aabb['min'][1]:
                aabb['min'][1] = theta
            if z_co < aabb['min'][2]:
                aabb['min'][2] = z_co
            if radius > aabb['max'][0]:
                aabb['max'][0] = radius
            if theta > aabb['max'][1]:
                aabb['max'][1] = theta
            if z_co > aabb['max'][2]:
                aabb['max'][2] = z_co
    fread.close()
    try:
        aabb['center'] = [(aabb['max'][0] + aabb['min'][0]) / 2,
                          (aabb['max'][1] + aabb['min'][1]) / 2,
                          (aabb['max'][2] + aabb['min'][2]) / 2]
        aabb['size'] = [aabb['max'][0] - aabb['min'][0], aabb['max'][1] - aabb['min'][1],
                        aabb['max'][2] - aabb['min'][2]]
        aabb['diagonal'] = math.sqrt(
            aabb['size'][0]**2 +
            aabb['size'][1]**2 +
            aabb['size'][2]**2)
    except UnboundLocalError:
        print('Error: aabb input file does not contain valid data. Exiting ...')
        sys.exit(1)
    for key, value in aabb.items():
        if log is None:
            print('{:10} = {}'.format(key, value))
        else:
            log_file = open(log, 'a')
            log_file.write('{:10} = {}\n'.format(key, value))
            log_file.close()
    """
    if log is not None:
        log_file = open(log, 'a')
        #log_file.write('***Axis Aligned Bounding Results for file "%s":\n' % fbasename)
        log_file.write('min = %s\n' % aabb['min'])
        log_file.write('max = %s\n' % aabb['max'])
        log_file.write('center = %s\n' % aabb['center'])
        log_file.write('size = %s\n' % aabb['size'])
        log_file.write('diagonal = %s\n\n' % aabb['diagonal'])
        log_file.close()
    # print(aabb)
    """
    return aabb
def measure_aabb(fbasename=None, log=None, coord_system='CARTESIAN'):
    """ Measure the axis aligned bounding box (aabb) of a mesh
    in multiple coordinate systems.

    Args:
        fbasename (str): filename of input model
        log (str): filename of log file
        coord_system (enum in ['CARTESIAN', 'CYLINDRICAL']
            Coordinate system to use:
                'CARTESIAN': lists contain [x, y, z]
                'CYLINDRICAL': lists contain [r, theta, z]
    Returns:
        dict: dictionary with the following aabb properties
            min (3 element list): minimum values
            max (3 element list): maximum values
            center (3 element list): the center point
            size (3 element list): size of the aabb in each coordinate (max-min)
            diagonal (float): the diagonal of the aabb
    """
    # TODO: add center point, spherical coordinate system
    fext = os.path.splitext(fbasename)[1][1:].strip().lower()
    if fext != 'xyz':
        fin = 'TEMP3D_aabb.xyz'
        run(log=log, file_in=fbasename, file_out=fin, script=None)
    else:
        fin = fbasename
    fread = open(fin, 'r')
    aabb = {'min': [999999.0, 999999.0, 999999.0], 'max': [-999999.0, -999999.0, -999999.0]}
    for line in fread:
        x_co, y_co, z_co = line.split()
        x_co = util.to_float(x_co)
        y_co = util.to_float(y_co)
        z_co = util.to_float(z_co)
        if coord_system == 'CARTESIAN':
            if x_co < aabb['min'][0]:
                aabb['min'][0] = x_co
            if y_co < aabb['min'][1]:
                aabb['min'][1] = y_co
            if z_co < aabb['min'][2]:
                aabb['min'][2] = z_co
            if x_co > aabb['max'][0]:
                aabb['max'][0] = x_co
            if y_co > aabb['max'][1]:
                aabb['max'][1] = y_co
            if z_co > aabb['max'][2]:
                aabb['max'][2] = z_co
        elif coord_system == 'CYLINDRICAL':
            radius = math.sqrt(x_co**2 + y_co**2)
            theta = math.degrees(math.atan2(y_co, x_co))
            if radius < aabb['min'][0]:
                aabb['min'][0] = radius
            if theta < aabb['min'][1]:
                aabb['min'][1] = theta
            if z_co < aabb['min'][2]:
                aabb['min'][2] = z_co
            if radius > aabb['max'][0]:
                aabb['max'][0] = radius
            if theta > aabb['max'][1]:
                aabb['max'][1] = theta
            if z_co > aabb['max'][2]:
                aabb['max'][2] = z_co
    fread.close()
    try:
        aabb['center'] = [(aabb['max'][0] + aabb['min'][0]) / 2,
                          (aabb['max'][1] + aabb['min'][1]) / 2,
                          (aabb['max'][2] + aabb['min'][2]) / 2]
        aabb['size'] = [aabb['max'][0] - aabb['min'][0], aabb['max'][1] - aabb['min'][1],
                        aabb['max'][2] - aabb['min'][2]]
        aabb['diagonal'] = math.sqrt(
            aabb['size'][0]**2 +
            aabb['size'][1]**2 +
            aabb['size'][2]**2)
    except UnboundLocalError:
        print('Error: aabb input file does not contain valid data. Exiting ...')
        sys.exit(1)
    for key, value in aabb.items():
        if log is None:
            print('{:10} = {}'.format(key, value))
        else:
            log_file = open(log, 'a')
            log_file.write('{:10} = {}\n'.format(key, value))
            log_file.close()
    """
    if log is not None:
        log_file = open(log, 'a')
        #log_file.write('***Axis Aligned Bounding Results for file "%s":\n' % fbasename)
        log_file.write('min = %s\n' % aabb['min'])
        log_file.write('max = %s\n' % aabb['max'])
        log_file.write('center = %s\n' % aabb['center'])
        log_file.write('size = %s\n' % aabb['size'])
        log_file.write('diagonal = %s\n\n' % aabb['diagonal'])
        log_file.close()
    # print(aabb)
    """
    return aabb
def measure_aabb ( fbasename = None , log = None , coord_system = 'CARTESIAN' ) : # TODO: add center point, spherical coordinate system fext = os . path . splitext ( fbasename ) [ 1 ] [ 1 : ] . strip ( ) . lower ( ) if fext != 'xyz' : fin = 'TEMP3D_aabb.xyz' run ( log = log , file_in = fbasename , file_out = fin , script = None ) else : fin = fbasename fread = open ( fin , 'r' ) aabb = { 'min' : [ 999999.0 , 999999.0 , 999999.0 ] , 'max' : [ - 999999.0 , - 999999.0 , - 999999.0 ] } for line in fread : x_co , y_co , z_co = line . split ( ) x_co = util . to_float ( x_co ) y_co = util . to_float ( y_co ) z_co = util . to_float ( z_co ) if coord_system == 'CARTESIAN' : if x_co < aabb [ 'min' ] [ 0 ] : aabb [ 'min' ] [ 0 ] = x_co if y_co < aabb [ 'min' ] [ 1 ] : aabb [ 'min' ] [ 1 ] = y_co if z_co < aabb [ 'min' ] [ 2 ] : aabb [ 'min' ] [ 2 ] = z_co if x_co > aabb [ 'max' ] [ 0 ] : aabb [ 'max' ] [ 0 ] = x_co if y_co > aabb [ 'max' ] [ 1 ] : aabb [ 'max' ] [ 1 ] = y_co if z_co > aabb [ 'max' ] [ 2 ] : aabb [ 'max' ] [ 2 ] = z_co elif coord_system == 'CYLINDRICAL' : radius = math . sqrt ( x_co ** 2 + y_co ** 2 ) theta = math . degrees ( math . atan2 ( y_co , x_co ) ) if radius < aabb [ 'min' ] [ 0 ] : aabb [ 'min' ] [ 0 ] = radius if theta < aabb [ 'min' ] [ 1 ] : aabb [ 'min' ] [ 1 ] = theta if z_co < aabb [ 'min' ] [ 2 ] : aabb [ 'min' ] [ 2 ] = z_co if radius > aabb [ 'max' ] [ 0 ] : aabb [ 'max' ] [ 0 ] = radius if theta > aabb [ 'max' ] [ 1 ] : aabb [ 'max' ] [ 1 ] = theta if z_co > aabb [ 'max' ] [ 2 ] : aabb [ 'max' ] [ 2 ] = z_co fread . close ( ) try : aabb [ 'center' ] = [ ( aabb [ 'max' ] [ 0 ] + aabb [ 'min' ] [ 0 ] ) / 2 , ( aabb [ 'max' ] [ 1 ] + aabb [ 'min' ] [ 1 ] ) / 2 , ( aabb [ 'max' ] [ 2 ] + aabb [ 'min' ] [ 2 ] ) / 2 ] aabb [ 'size' ] = [ aabb [ 'max' ] [ 0 ] - aabb [ 'min' ] [ 0 ] , aabb [ 'max' ] [ 1 ] - aabb [ 'min' ] [ 1 ] , aabb [ 'max' ] [ 2 ] - aabb [ 'min' ] [ 2 ] ] aabb [ 'diagonal' ] = math . sqrt ( aabb [ 'size' ] [ 0 ] ** 2 + aabb [ 'size' ] [ 1 ] ** 2 + aabb [ 'size' ] [ 2 ] ** 2 ) except UnboundLocalError : print ( 'Error: aabb input file does not contain valid data. Exiting ...' ) sys . exit ( 1 ) for key , value in aabb . items ( ) : if log is None : print ( '{:10} = {}' . format ( key , value ) ) else : log_file = open ( log , 'a' ) log_file . write ( '{:10} = {}\n' . format ( key , value ) ) log_file . close ( ) """
    if log is not None:
        log_file = open(log, 'a')
        #log_file.write('***Axis Aligned Bounding Results for file "%s":\n' % fbasename)
        log_file.write('min = %s\n' % aabb['min'])
        log_file.write('max = %s\n' % aabb['max'])
        log_file.write('center = %s\n' % aabb['center'])
        log_file.write('size = %s\n' % aabb['size'])
        log_file.write('diagonal = %s\n\n' % aabb['diagonal'])
        log_file.close()
    # print(aabb)
    """ return aabb
Measure the axis aligned bounding box (aabb) of a mesh
    in multiple coordinate systems.

    Args:
        fbasename (str): filename of input model
        log (str): filename of log file
        coord_system (enum in ['CARTESIAN', 'CYLINDRICAL']
            Coordinate system to use:
                'CARTESIAN': lists contain [x, y, z]
                'CYLINDRICAL': lists contain [r, theta, z]
    Returns:
        dict: dictionary with the following aabb properties
            min (3 element list): minimum values
            max (3 element list): maximum values
            center (3 element list): the center point
            size (3 element list): size of the aabb in each coordinate (max-min)
            diagonal (float): the diagonal of the aabb
Measure the axis aligned bounding box ( aabb ) of a mesh in multiple coordinate systems .