Skip to main content

Overview

The Polyline primitive represents a connected sequence of line segments in 3D space. It is defined by a series of points and can be either open or closed. When the first and last points coincide, the polyline automatically detects and creates a closed loop. Use Cases:
  • Creating complex paths and contours
  • Defining irregular shapes
  • Profile curves for extrusion
  • Offset operations for parallel paths
  • Closed polygons and open paths

Constructor

let polyline = OGPolyline::new("poly-1".to_string());

Rust Constructor Parameters

id
String
required
Unique identifier for the polyline instance

TypeScript Constructor Parameters

options
IPolylineOptions
Configuration options for the polyline
options.ogid
string
Optional unique identifier (auto-generated if not provided)
options.points
Vector3[]
required
Array of points defining the polyline path
options.color
number
required
Polyline color as a hexadecimal number (e.g., 0x00ff00 for green)
options.fatLines
boolean
default:"false"
Enable thick line rendering using Line2
options.width
number
default:"20"
Line width when fatLines is enabled

Methods

set_config

polyline.set_config(points: Vec<Vector3>);
Updates the polyline’s points and automatically checks if it’s closed.

add_point

polyline.add_point(point: Vector3);
Adds a single point to the polyline and regenerates the geometry.

add_multiple_points

polyline.add_multiple_points(points: Vec<Vector3>);
Replaces all points with the provided array.

generate_geometry

polyline.generate_geometry();
Generates vertices and edges from the points. For closed polylines, creates a face and the closing edge.

get_geometry_serialized

let geometry: String = polyline.get_geometry_serialized();
Returns serialized vertex buffer data. For closed polylines, includes the first vertex repeated at the end.

get_offset_serialized

let offset_data = polyline.get_offset_serialized(
    distance: f64,
    acute_threshold_degrees: f64,
    bevel: bool
);
Generates an offset path parallel to the polyline. The behavior differs for open vs. closed polylines.
distance
number
required
Distance to offset the polyline (positive or negative)
acuteThresholdDegrees
number
default:"35.0"
Angle threshold for beveling acute corners
bevel
boolean
default:"true"
Enable beveling at acute angles

is_closed

let closed: bool = polyline.is_closed();
Returns whether the polyline forms a closed loop.

get_points

let points_json: String = polyline.get_points();
Returns serialized points as JSON.

get_brep_serialized

let brep: String = polyline.get_brep_serialized();
Returns the complete BREP representation.

Properties

id
String
Unique identifier for the polyline instance
points
Vec<Vector3>
Array of points defining the polyline path
is_closed
bool
Whether the polyline forms a closed loop (automatically detected)
brep
Brep
Internal BREP representation containing vertices, edges, and optionally a face for closed polylines
isClosed
boolean
Indicates if the polyline is closed (TypeScript property)
color
number
Polyline color (TypeScript only, settable)

Code Examples

Creating an Open Polyline

use opengeometry::primitives::OGPolyline;
use openmaths::Vector3;

let mut polyline = OGPolyline::new("poly-1".to_string());
let points = vec![
    Vector3::new(0.0, 0.0, 0.0),
    Vector3::new(5.0, 0.0, 2.0),
    Vector3::new(10.0, 0.0, 1.0),
    Vector3::new(15.0, 0.0, 4.0),
];
polyline.set_config(points);
polyline.generate_geometry();

Creating a Closed Polygon

// Create a triangle by repeating the first point
let points = vec![
    Vector3::new(0.0, 0.0, 0.0),
    Vector3::new(10.0, 0.0, 0.0),
    Vector3::new(5.0, 0.0, 8.66),
    Vector3::new(0.0, 0.0, 0.0),  // Close the loop
];
polyline.set_config(points);
assert!(polyline.is_closed());

Adding Points Incrementally

let mut polyline = OGPolyline::new("poly-2".to_string());
polyline.add_point(Vector3::new(0.0, 0.0, 0.0));
polyline.add_point(Vector3::new(5.0, 0.0, 0.0));
polyline.add_point(Vector3::new(5.0, 0.0, 5.0));
polyline.add_point(Vector3::new(0.0, 0.0, 5.0));
polyline.add_point(Vector3::new(0.0, 0.0, 0.0));  // Close it

Generating Offset Polylines

let offset_data = polyline.get_offset_serialized(2.0, 35.0, true);
let result: OffsetResult = serde_json::from_str(&offset_data).unwrap();

println!("Offset is closed: {}", result.is_closed);
println!("Number of points: {}", result.points.len());

Using Fat Lines

const thickPolyline = new Polyline({
  points: [
    new Vector3(0, 0, 0),
    new Vector3(10, 0, 5),
    new Vector3(20, 0, 2),
    new Vector3(30, 0, 8)
  ],
  color: 0xffff00,
  fatLines: true,
  width: 50
});

Complex Path Example

// Create a zigzag pattern
const zigzag = new Polyline({
  points: [
    new Vector3(0, 0, 0),
    new Vector3(5, 0, 5),
    new Vector3(10, 0, 0),
    new Vector3(15, 0, 5),
    new Vector3(20, 0, 0),
    new Vector3(25, 0, 5)
  ],
  color: 0x00ffff
});

// Generate parallel zigzags
const offset1 = zigzag.getOffset(2);
const offset2 = zigzag.getOffset(-2);

Live Demo

Polyline Demo

Try the Polyline primitive in the browser

See Also

Line

Straight line segments

Arc

Circular arc segments

Curve

Smooth curves through control points
Last modified on March 14, 2026