def bezier3dCrv(crv3d, t): dim = crv3d.dim degree = crv3d.degree pntLst = crv3d.controlPnt type = crv3d.curveType if dim != 3: return None if len(pntLst) != degree + 1: return None for i in range(degree): tempPnt = np.array([]) for j in range(len(pntLst) - 1): x = t * pntLst[j + 1].x + (1 - t) * pntLst[j].x y = t * pntLst[j + 1].y + (1 - t) * pntLst[j].y z = t * pntLst[j + 1].z + (1 - t) * pntLst[j].z pnt = point(x, y, z) tempPnt = np.append(tempPnt, pnt) pntLst = tempPnt return pntLst[0]
def extendBezierCrv(curveInfo, t): pntLst = curveInfo.controlPnt if t < 0: pntLst = pntLst[::-1] t = np.fabs(t) degree = curveInfo.degree extendCrv = np.array([]) if degree + 1 != len(pntLst): return None for i in range(degree): if i == 0: x = pntLst[0].x y = pntLst[0].y pnt = point(x, y) extendCrv = np.append(extendCrv, pnt) tempPnt = np.array([]) for j in range(len(pntLst) - 1): x = pntLst[j].x + 1 / (t - 1) * (pntLst[j + 1].x - pntLst[j].x) y = pntLst[j].y + 1 / (t - 1) * (pntLst[j + 1].y - pntLst[j].y) pnt = point(x, y) tempPnt = np.append(tempPnt, pnt) if j == 0: extendCrv = np.append(extendCrv, pnt) pntLst = tempPnt return extendCrv
def upgradeBezierDegree(curveInfo): pntLst = curveInfo.controlPnt degree = curveInfo.degree crvPntLst = np.array([]) crvPntLst = np.append(crvPntLst, pntLst[0]) for i in range(len(pntLst) - 1): alpha = (i + 1) / (degree + 1) x = (pntLst[i].x * alpha + pntLst[i + 1].x) / (1 + alpha) y = (pntLst[i].y * alpha + pntLst[i + 1].y) / (1 + alpha) pnt = point(x, y) crvPntLst = np.append(crvPntLst, pnt) crvPntLst = np.append(crvPntLst, pntLst[len(pntLst) - 1]) newCrveInfo = nurbCurve(crvPntLst, degree + 1) return newCrveInfo
tempPnt = np.array([]) for j in range(len(pntLst) - 1): x = t * pntLst[j + 1].x + (1 - t) * pntLst[j].x y = t * pntLst[j + 1].y + (1 - t) * pntLst[j].y z = t * pntLst[j + 1].z + (1 - t) * pntLst[j].z pnt = point(x, y, z) tempPnt = np.append(tempPnt, pnt) pntLst = tempPnt return pntLst[0] if __name__ == "__main__": pnt1 = point(-41.88438, 41.26041, 49.91586) pnt2 = point(-73.06837, 13.91201, 87.07949) pnt3 = point(-39.28571, -33.41261, 46.81889) controlPnt = np.array([pnt1, pnt2, pnt3]) crv3d = nurbCurve(controlPnt, 2, 3) pnt = bezier3dCrv(crv3d, 0.1) print(pnt.x, ',', pnt.y, ',', pnt.z) pnt = bezier3dCrv(crv3d, 0.2) print(pnt.x, ',', pnt.y, ',', pnt.z) pnt = bezier3dCrv(crv3d, 0.3) print(pnt.x, ',', pnt.y, ',', pnt.z)
def decasteljaus(curveInfo, t): pntLst = curveInfo.controlPnt n = curveInfo.degree if n + 1 != len(pntLst): return None if t < 0: return None tempPnt = np.array([]) firstDer = None pnt = None firstCurve = np.array([]) secondCurve = np.array([]) firstCurve = np.append(firstCurve, pntLst[0]) secondCurve = np.append(secondCurve, pntLst[len(pntLst) - 1]) secondDer = None for i in range(n): if i == n - 1: firstDer_x = pntLst[1].x - pntLst[0].x firstDer_y = pntLst[1].y - pntLst[0].y firstDer_x = firstDer_x * n firstDer_y = firstDer_y * n firstDer = point(firstDer_x, firstDer_y) x = t * pntLst[1].x + (1 - t) * pntLst[0].x y = t * pntLst[1].y + (1 - t) * pntLst[0].y pnt = point(x, y) firstCurve = np.append(firstCurve, pnt) secondCurve = np.append(secondCurve, pnt) else: if n >= 2 and i == n - 2: secondDer_x = pntLst[2].x - 2 * pntLst[1].x + pntLst[0].x secondDer_y = pntLst[2].y - 2 * pntLst[1].y + pntLst[0].y secondDer_x = n * (n - 1) * secondDer_x secondDer_y = n * (n - 1) * secondDer_y secondDer = point(secondDer_x, secondDer_y) for j in range(len(pntLst) - 1): x = t * pntLst[j + 1].x + (1 - t) * pntLst[j].x y = t * pntLst[j + 1].y + (1 - t) * pntLst[j].y pnt = point(x, y) tempPnt = np.append(tempPnt, pnt) if j == 0: firstCurve = np.append(firstCurve, pnt) if j == len(pntLst) - 2: secondCurve = np.append(secondCurve, pnt) pntLst = tempPnt tempPnt = np.array([]) secondCurve = secondCurve[::-1] return pnt, firstDer, secondDer, firstCurve, secondCurve
for i in range(len(pntLst) - 1): alpha = (i + 1) / (degree + 1) x = (pntLst[i].x * alpha + pntLst[i + 1].x) / (1 + alpha) y = (pntLst[i].y * alpha + pntLst[i + 1].y) / (1 + alpha) pnt = point(x, y) crvPntLst = np.append(crvPntLst, pnt) crvPntLst = np.append(crvPntLst, pntLst[len(pntLst) - 1]) newCrveInfo = nurbCurve(crvPntLst, degree + 1) return newCrveInfo if __name__ == "__main__": pnt1 = point(0, 0) pnt2 = point(0, 100) pnt3 = point(100, 100) pnt4 = point(100, 0) controlPnt = np.array([pnt1, pnt2, pnt3, pnt4]) curveInfo = bezier(3, controlPnt) extendCrv = extendBezierCrv(curveInfo, 1.5) print(extendCrv) extendCrv = extendBezierCrv(curveInfo, -1.5) print(extendCrv) upgradeCrv = upgradeBezierDegree(curveInfo)