> ## Documentation Index
> Fetch the complete documentation index at: https://docs.opengeometry.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Curve

> A curve primitive represented through control points

## Overview

The Curve primitive represents a curve in 3D space defined by a series of control points. Currently, the curve is represented as a polyline through these control points. It is an open path (non-closed) and is useful for creating smooth or complex curved paths.

**Use Cases:**

* Creating curved paths and contours
* Spline-like representations
* Profile curves for sweep operations
* Offset operations for parallel curves
* Complex path definitions

## Constructor

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    let curve = OGCurve::new("curve-1".to_string());
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    const curve = new Curve(options?: ICurveOptions);
    ```
  </Tab>
</Tabs>

### Rust Constructor Parameters

<ParamField path="id" type="String" required>
  Unique identifier for the curve instance
</ParamField>

### TypeScript Constructor Parameters

<ParamField path="options" type="ICurveOptions">
  Configuration options for the curve
</ParamField>

<ParamField path="options.ogid" type="string">
  Optional unique identifier (auto-generated if not provided)
</ParamField>

<ParamField path="options.controlPoints" type="Vector3[]" required>
  Array of control points defining the curve path
</ParamField>

<ParamField path="options.color" type="number" required>
  Curve color as a hexadecimal number (e.g., 0x00aa55)
</ParamField>

## Methods

### set\_config

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    curve.set_config(control_points: Vec<Vector3>);
    ```

    Updates the curve's control points and regenerates geometry.
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    curve.setConfig(options: ICurveOptions);
    ```

    Updates the curve configuration and regenerates geometry.
  </Tab>
</Tabs>

### generate\_geometry

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    curve.generate_geometry();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    // Called automatically by setConfig
    ```
  </Tab>
</Tabs>

Generates vertices and edges connecting the control points sequentially.

### get\_geometry\_serialized

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    let geometry: String = curve.get_geometry_serialized();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    // Used internally for rendering
    ```
  </Tab>
</Tabs>

Returns serialized vertex buffer data as a JSON string.

### get\_offset\_serialized

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    let offset_data = curve.get_offset_serialized(
        distance: f64,
        acute_threshold_degrees: f64,
        bevel: bool
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    const result = curve.getOffset(
        distance: number,
        acuteThresholdDegrees: number = 35.0,
        bevel: boolean = true
    ): ICurveOffsetResult;
    ```
  </Tab>
</Tabs>

Generates an offset curve parallel to the original at the specified distance. The curve is treated as an open path.

<ParamField path="distance" type="number" required>
  Distance to offset the curve (positive or negative)
</ParamField>

<ParamField path="acuteThresholdDegrees" type="number" default="35.0">
  Angle threshold for beveling acute corners
</ParamField>

<ParamField path="bevel" type="boolean" default="true">
  Enable beveling at acute angles
</ParamField>

### get\_brep\_serialized

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    let brep: String = curve.get_brep_serialized();
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    // Available through internal API
    ```
  </Tab>
</Tabs>

Returns the complete BREP representation including vertices and edges.

### dispose

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    curve.dispose();
    ```

    Clears the BREP and control points to free memory.
  </Tab>
</Tabs>

## Properties

<ResponseField name="id" type="String">
  Unique identifier for the curve instance
</ResponseField>

<ResponseField name="control_points" type="Vec<Vector3>">
  Array of control points defining the curve
</ResponseField>

<ResponseField name="brep" type="Brep">
  Internal BREP representation containing vertices and edges
</ResponseField>

<ResponseField name="color" type="number">
  Curve color (TypeScript only, settable)
</ResponseField>

## Code Examples

### Creating a Simple Curve

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    use opengeometry::primitives::OGCurve;
    use openmaths::Vector3;

    let mut curve = OGCurve::new("curve-1".to_string());
    let control_points = vec![
        Vector3::new(0.0, 0.0, 0.0),
        Vector3::new(5.0, 0.0, 3.0),
        Vector3::new(10.0, 0.0, 2.0),
        Vector3::new(15.0, 0.0, 5.0),
    ];
    curve.set_config(control_points);
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import { Curve, Vector3 } from "opengeometry";

    const curve = new Curve({
      controlPoints: [
        new Vector3(0, 0, 0),
        new Vector3(5, 0, 3),
        new Vector3(10, 0, 2),
        new Vector3(15, 0, 5)
      ],
      color: 0x00aa55
    });
    ```
  </Tab>
</Tabs>

### Creating a Wave Pattern

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    // Generate a sine wave using control points
    const controlPoints = [];
    for (let i = 0; i <= 20; i++) {
      const x = i * 2;
      const z = Math.sin(i * 0.5) * 5;
      controlPoints.push(new Vector3(x, 0, z));
    }

    const wave = new Curve({
      controlPoints,
      color: 0x0088ff
    });
    ```
  </Tab>
</Tabs>

### Generating Offset Curves

<Tabs>
  <Tab title="Rust">
    ```rust theme={null}
    let offset_data = curve.get_offset_serialized(2.0, 35.0, true);
    let result: OffsetResult = serde_json::from_str(&offset_data).unwrap();

    println!("Offset points: {}", result.points.len());
    println!("Is closed: {}", result.is_closed);  // false for curves
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    const offsetResult = curve.getOffset(2.0, 35.0, true);

    console.log(offsetResult.points);              // Vector3[]
    console.log(offsetResult.beveledVertexIndices); // number[]
    console.log(offsetResult.isClosed);            // false (curves are open)

    // Create a parallel curve
    const parallelCurve = new Curve({
      controlPoints: offsetResult.points,
      color: 0xff0088
    });
    ```
  </Tab>
</Tabs>

### Creating Multiple Parallel Curves

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    const baseCurve = new Curve({
      controlPoints: [
        new Vector3(0, 0, 0),
        new Vector3(10, 0, 5),
        new Vector3(20, 0, 3),
        new Vector3(30, 0, 8)
      ],
      color: 0xff0000
    });

    // Create offset curves on both sides
    const distances = [2, 4, 6];
    const upperCurves = distances.map(d => {
      const offset = baseCurve.getOffset(d);
      return new Curve({
        controlPoints: offset.points,
        color: 0x0000ff
      });
    });

    const lowerCurves = distances.map(d => {
      const offset = baseCurve.getOffset(-d);
      return new Curve({
        controlPoints: offset.points,
        color: 0x00ff00
      });
    });
    ```
  </Tab>
</Tabs>

### Updating Curve Control Points

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    curve.setConfig({
      controlPoints: [
        new Vector3(0, 0, 0),
        new Vector3(8, 0, 4),
        new Vector3(16, 0, 1),
        new Vector3(24, 0, 6)
      ],
      color: 0xffff00
    });

    // Change color
    curve.color = 0xff00ff;
    ```
  </Tab>
</Tabs>

### Bezier-like Curve Approximation

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    // Approximate a quadratic bezier with control points
    function quadraticBezier(
      p0: Vector3, 
      p1: Vector3, 
      p2: Vector3, 
      segments: number
    ): Vector3[] {
      const points = [];
      for (let i = 0; i <= segments; i++) {
        const t = i / segments;
        const x = (1-t)*(1-t)*p0.x + 2*(1-t)*t*p1.x + t*t*p2.x;
        const y = (1-t)*(1-t)*p0.y + 2*(1-t)*t*p1.y + t*t*p2.y;
        const z = (1-t)*(1-t)*p0.z + 2*(1-t)*t*p1.z + t*t*p2.z;
        points.push(new Vector3(x, y, z));
      }
      return points;
    }

    const bezierPoints = quadraticBezier(
      new Vector3(0, 0, 0),
      new Vector3(10, 0, 10),
      new Vector3(20, 0, 0),
      20
    );

    const bezierCurve = new Curve({
      controlPoints: bezierPoints,
      color: 0xff00ff
    });
    ```
  </Tab>
</Tabs>

## Live Demo

<Card title="Curve Demo" icon="play" href="https://demos.opengeometry.io/primitives/curve.html">
  Try the Curve primitive in the browser
</Card>

## See Also

<CardGroup cols={2}>
  <Card title="Arc" icon="circle-notch" href="/OpenGeometry/api/primitives/arc">
    Circular arc segments
  </Card>

  <Card title="Line" icon="minus" href="/OpenGeometry/api/primitives/line">
    Straight line segments
  </Card>

  <Card title="Polyline" icon="timeline" href="/OpenGeometry/api/primitives/polyline">
    Connected line segments
  </Card>
</CardGroup>
