-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayToSTL_TEST2.py
84 lines (45 loc) · 1.83 KB
/
ArrayToSTL_TEST2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
########################################################################
# ArrayToSTL #
########################################################################
# This script is designed to take a given array (allPoints) and
# convert it to an STL file
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from stl import mesh
import os
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial import ConvexHull
# Import the array-creation functions
from ImagesToArray import *
#------------------------ FUNCTIONS -----------------------------#
def arrayToConvexHull(pointArray):
# Array to STL
# Vertices = points in array
# Faces are defined by the corner points, listed in simplices
vertices = np.array(pointArray)
hull = ConvexHull(vertices)
faces = np.array(hull.simplices)
stlModel = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
stlModel.vectors[i][j] = vertices[f[j],:]
# Write the mesh to file "ScanData.stl"
stlModel.save(DIRECTORY + 'ScanData.stl')
print ('Saved to directory as "ScanData.stl"')
def arrayToXYZ(pointArray):
# Writes all points to local .xyz file
# Open (and write) new file in local directory
file = open("ScanData.xyz", "w")
for point in pointArray:
xVal, yVal, zVal = point
file.write(str(xVal)+' '+str(yVal)+' '+str(zVal)+'\n')
file.close()
print('Saved to directory as "ScanData.xyz"')
if __name__ == '__main__':
# For testing purposes
pathToImages = DIRECTORY + 'TestPhotos/'
print ('Extracting points from images...')
filteredPoints = getPoints(pathToImages)
arrayToConvexHull(filteredPoints)
arrayToXYZ(filteredPoints)