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



![image|638x499](upload://8JnHvCiJDujl
Pointcloud Plane Detection using RANSAC_Alpha Shape.dyn (103.1 KB)
NwjwfcAuhGcpyVW.png)

  • 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
#enramiz@yahoo.com
#https://www.linkedin.com/in/ramiz-mohareb-3362b095/
import sys
import clr

import descartes
import alphashape
import matplotlib.pyplot as plt
clr.AddReference('ProtoGeometry')
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
clr.AddReference('ProtoGeometry')
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')

clr.AddReference('System.Drawing')
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")
    else:
        image = Image.fromarray(npImgArray, "L")
    # convert to Python ByteArray
    byteIO = io.BytesIO()
    image.save(byteIO, 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
    """
    fig.canvas.draw()
    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
    

points_3d=IN[0]
fig, ax = plt.subplots()
ax.scatter(*zip(*points_3d))
plt.show()
df_4d = pd.DataFrame(points_3d, columns=['x', 'y', 'z'])

alpha_shape = alphashape.alphashape(points_3d, 0.)
#alpha_shape

fig, ax = plt.subplots()
ax.scatter(*zip(*points_3d))
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
5 Likes

Good Job.

2 Likes

Hi @RMohareb ,

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

2 Likes

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

1 Like

Nice work :slight_smile:

2 Likes