(Using Python) My code reads through a text file and biases the point to make a perfect circle. When it is reading through the points, it is taking 10-15 minutes to run. I have multiple different text files of similar size to go through so waiting that long is impractical. Does anybody know of ways I can edit my code to make it run faster? It has to read through roughly 430 points each file.
import numpy as np
import math
import matplotlib.pyplot as plt
from circle_fitting_3d import Circle3D
#Read the text file with all the points
lines = open(text file goes here)
#create a 2D array of all the (x,y,z) points in the file
points = np.array([[float(n) for n in ln.split(",")] for ln in lines])
#find the center position (x,y,z) of all the points in the file
center = points.mean (axis = 0)
#find circle of best fit
circle_3d = Circle3D(points)
print(circle_3d._projected_points)
#calculate all the distances between the points and the center and get the mean
#print("Radius = " + str(circle_3d.radius) + "mm")
#move the circle center to (0,0,0)
translateTuple = circle_3d.center
print("Translation Values: " + str(translateTuple))
transPoint = np.empty((0,3))
for p in points:
tx = p[0] - translateTuple[0]
ty = p[1] - translateTuple[1]
tz = p[2] - translateTuple[2]
#print("Diff x: " + str(tx-p[0]))
transPoint = np.append(transPoint,[[tx,ty,tz]], axis=0)
#Calculating new points
def pointOnLine(pointA, pointB, dis):
h = math.sqrt((pointA[0]-pointB[0])**2 + (pointA[1]-pointB[1])**2 + (pointA[2]-pointB[2])**2)
cx = pointA[0] - ((dis*(pointA[0]-pointB[0])/h))
cy = pointA[1] - ((dis*(pointA[1]-pointB[1])/h))
cz = pointA[2] - ((dis*(pointA[2]-pointB[2])/h))
pointC = np.array([cx,cy,cz])
return pointC
#print(transPoint)
new_circle = Circle3D(transPoint)
#resize the circle
resizeValue = new_circle.radius - 100.00
print(resizeValue)
resizePoints = np.empty((0,3))
for p in transPoint:
resized = pointOnLine(p, new_circle.center, resizeValue)
resizePoints = np.append(resizePoints,[resized], axis=0)
#print(resizePoints)
resizePoints = Circle3D
resize_circle = Circle3D(resizePoints)
print("Old Radius = " + str(circle_3d.radius) + "mm")
print("New Radius = " + str(new_circle.radius) + "mm")
print("Resized Radius = " + str(resize_circle.radius) + "mm")
print(circle_3d.center)
print(new_circle.center)
print(resize_circle.center)
#write new points to .txt file
with open('poly_out_test_new_points.txt', 'w') as testfile:
for row in reversed(resizePoints._projected_points):
testfile.write(' '.join([str(a) for a in row]) + '\n')
#plot in 3D space
fig = plt.figure()
# syntax for 3-D projection
ax = plt.axes(projection ='3d')
ax.plot3D(points[:,0], points[:,1], points[:,2], 'green')
ax.plot3D(circle_3d._projected_points[:,0], circle_3d._projected_points[:,1], circle_3d._projected_points[:,2], 'purple')
resize_circle.plot(ax)
#circle_3d.plot(ax)
plt.show()
print("Complete")`
I have tried using array broadcasting, but this is my first time using that, so it kept throwing me errors and I wasn't sure where I was going wrong. The code I posted is the solution I have that works but runs slow.