Alpha Shape proplem (Draw outer line around the outside point Cloud at the same plane by ignoring any points that would fall inside.)

Draw outer line around the outside points by ignoring any points that would fall inside.
I used Alpha Shape algorethem alphashape · PyPI by install alphashape in Python3

Pointcloud Plane Detection using RANSAC_Alpha Shape.dyn (103.1 KB)

  • First select point cloud form revit project by using @Julien_Benoit1 Julien Benoit python script
  • Second use point-cloud-plane-detection-using-ransac @Ewan_Opie to detecte the planes of the point cloud.
  • Final use alphashape python code to Draw outer line around the points (at the same plane detected by ransac Algorithm )
  • create polyline and surface for each groub of points
# Phython-Standard- und DesignScript-Bibliotheken laden
#Created By Ramiz Mohareb
import sys
import clr

import descartes
import alphashape
import matplotlib.pyplot as plt
from Autodesk.DesignScript.Geometry import *
from System.IO import MemoryStream
import numpy as np
from descartes import PolygonPatch

from shapely.geometry import Polygon

import matplotlib.pyplot as plt
from PIL import Image
import alphashape
import io
import sys
import clr
import System

import pandas as pd
from Autodesk.DesignScript.Geometry import *
dirAppLoc = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) 
sys.path.append(dirAppLoc + r'\python-3.8.3-embed-amd64\Lib\site-packages')

import System.Drawing
from System.Drawing import *
from System.Drawing.Imaging import *
from System.IO import MemoryStream

import matplotlib.pyplot as plt
from PIL import Image
import math
# Die Eingaben für diesen Block werden in Form einer Liste in den IN-Variablen gespeichert.

def convertToBitmap2(npImgArray):
    bitmap_ = None
    # remove alpha
    if npImgArray.ndim == 3 and npImgArray.shape[-1] == 4:
        npImgArray = npImgArray[:, :, :-1]
    # convert to PIL Image
    if npImgArray.ndim == 3:
        image = Image.fromarray(npImgArray, "RGB")
        image = Image.fromarray(npImgArray, "L")
    # convert to Python ByteArray
    byteIO = io.BytesIO(), format='BMP')
    byteArr = byteIO.getvalue()
    # convert to Net ByteArray
    netBytes = System.Array[System.Byte](byteArr)
    with MemoryStream(netBytes) as ms:
        bitmap_ = Bitmap(ms)
    return bitmap_    
def plt2arr(fig):
    need to draw if figure is not drawn yet
    rgba_buf = fig.canvas.buffer_rgba()
    (w,h) = fig.canvas.get_width_height()
    rgba_arr = np.frombuffer(rgba_buf, dtype=np.uint8).reshape((h,w,4))
    return rgba_arr

fig, ax = plt.subplots()
df_4d = pd.DataFrame(points_3d, columns=['x', 'y', 'z'])

alpha_shape = alphashape.alphashape(points_3d, 0.)

fig, ax = plt.subplots()
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))

image_from_plot = plt2arr(fig)
bitmap1 = convertToBitmap2(image_from_plot)

OUT =bitmap1,alpha_shape.exterior.coords

Good Job.


Hi @RMohareb ,

Very interesting!
How many points did you test and how long did it take?


Hi @Daan
I tested with 50000 points. It take about 1 min

1 Like

Nice work :slight_smile: