def areas_from_polygons(verts, polygons, sum_faces=False): ''' returns pols area as [float, float,...] vertices: list as [vertex, vertex, ...], being each vertex [float, float, float]. faces: list as [polygon, polygon,..], being each polygon [int, int, ...]. sum_faces if True it will return the sum of the areas as [float] ''' areas = [] concat_area = areas.append for polygon in polygons: num = len(polygon) if num == 3: concat_area( area(verts[polygon[0]], verts[polygon[1]], verts[polygon[2]])) elif num == 4: area_1 = area(verts[polygon[0]], verts[polygon[1]], verts[polygon[2]]) area_2 = area(verts[polygon[0]], verts[polygon[2]], verts[polygon[3]]) concat_area(area_1 + area_2) elif num > 4: ngon_area = 0.0 subcoords = [Vector(verts[idx]) for idx in polygon] for tri in tessellate([subcoords]): ngon_area += area(*[verts[polygon[i]] for i in tri]) concat_area(ngon_area) else: concat_area(0) if sum_faces: areas = [sum(areas)] return areas
def areas_from_polygons(verts, polygons, sum_faces=False): areas = [] concat_area = areas.append for polygon in polygons: num = len(polygon) if num == 3: concat_area( area(verts[polygon[0]], verts[polygon[1]], verts[polygon[2]])) elif num == 4: area_1 = area(verts[polygon[0]], verts[polygon[1]], verts[polygon[2]]) area_2 = area(verts[polygon[0]], verts[polygon[2]], verts[polygon[3]]) concat_area(area_1 + area_2) elif num > 4: ngon_area = 0.0 subcoords = [Vector(verts[idx]) for idx in polygon] for tri in tessellate([subcoords]): ngon_area += area(*[verts[polygon[i]] for i in tri]) concat_area(ngon_area) else: concat_area(0) if sum_faces: areas = [sum(areas)] return areas