Friday, January 24, 2025

Bezier Curve - my exploration of Computer Graphics goes on...

A Bézier curve is a parametric curve frequently used in computer graphics and related fields to model smooth and scalable shapes. It is defined by control points, where the curve starts at the first control point and ends at the last control point. The intermediate control points influence the curvature but do not lie on the curve itself.

The mathematical representation of a Bézier curve is:

are the control points.

where the polynomials

  • Bézier curves are widely used in:

  • Computer-aided design (CAD).
  • Font design.
  • Animation paths.
Here's the Python script to create a simple application using freeCAD...

import FreeCAD
import FreeCADGui
import Part
from PySide2 import QtWidgets, QtCore


class BezierCurveApp(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Bézier Curve in FreeCAD")

        # UI Layout
        layout = QtWidgets.QVBoxLayout()

        # Input for control points
        self.control_points_input = QtWidgets.QTextEdit()
        self.control_points_input.setPlaceholderText(
            "Enter control points as x,y,z (one per line):\nExample:\n0,0,0\n10,15,0\n20,5,0\n30,10,0"
        )
        layout.addWidget(self.control_points_input)

        # Button to create Bézier curve
        self.create_curve_btn = QtWidgets.QPushButton("Create Bézier Curve")
        self.create_curve_btn.clicked.connect(self.create_bezier_curve)
        layout.addWidget(self.create_curve_btn)

        # Button to clear the workspace
        self.clear_workspace_btn = QtWidgets.QPushButton("Clear Workspace")
        self.clear_workspace_btn.clicked.connect(self.clear_workspace)
        layout.addWidget(self.clear_workspace_btn)

        self.setLayout(layout)

    def parse_control_points(self):
        """Parse control points from the input text."""
        points = []
        lines = self.control_points_input.toPlainText().strip().split("\n")
        for line in lines:
            try:
                x, y, z = map(float, line.split(","))
                points.append(FreeCAD.Vector(x, y, z))
            except ValueError:
                QtWidgets.QMessageBox.warning(self, "Invalid Input", "Invalid control point format!")
                return None
        return points

    def create_bezier_curve(self):
      """Create a Bézier curve based on the input control points."""
      # Ensure an active document exists
      if not FreeCAD.ActiveDocument:
          FreeCAD.newDocument("BezierCurveDemo")
  
      points = self.parse_control_points()
      if not points:
          return
  
      if len(points) < 2:
          QtWidgets.QMessageBox.warning(self, "Invalid Input", "At least two control points are required!")
          return
  
      # Create the Bézier curve
      bezier_curve = Part.BezierCurve()
      bezier_curve.setPoles(points)
      curve_object = FreeCAD.ActiveDocument.addObject("Part::Feature", "BezierCurve")
      curve_object.Shape = bezier_curve.toShape()
      FreeCAD.ActiveDocument.recompute()
  
      QtWidgets.QMessageBox.information(self, "Success", "Bézier curve created!")


    def clear_workspace(self):
        """Clear all objects from the workspace."""
        if FreeCAD.ActiveDocument:
            FreeCAD.ActiveDocument.clearAllObjects()
            FreeCAD.ActiveDocument.recompute()
            QtWidgets.QMessageBox.information(self, "Workspace Cleared", "All objects have been removed.")


def show_bezier_curve_app():
    """Display the Bézier Curve UI application."""
    app = BezierCurveApp()

    # Wrap the app in a QDockWidget
    dock_widget = QtWidgets.QDockWidget("Bézier Curve App", FreeCADGui.getMainWindow())
    dock_widget.setWidget(app)
    FreeCADGui.getMainWindow().addDockWidget(QtCore.Qt.RightDockWidgetArea, dock_widget)


# Run the application
show_bezier_curve_app()

And here's my today's recording of Bezier curve of Computer Graphics...



And here's the experimentation of my young son Ridit using Bezier curve to animate a path of a slithering snake in Blender.



Enjoy...


No comments: