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(id: String);
const polyline = new Polyline(options?: IPolylineOptions);
Rust Constructor Parameters
Unique identifier for the polyline instance
TypeScript Constructor Parameters
Configuration options for the polyline
Optional unique identifier (auto-generated if not provided)
Array of points defining the polyline path
Polyline color as a hexadecimal number (e.g., 0x00ff00 for green)
Enable thick line rendering using Line2
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.polyline.setConfig(options: IPolylineOptions);
Updates the polyline configuration and regenerates geometry.
add_point
polyline.add_point(point: Vector3);
polyline.addPoint(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();
// Called automatically by setConfig and addPoint
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();
// Used internally for rendering
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
);
const result = polyline.getOffset(
distance: number,
acuteThresholdDegrees: number = 35.0,
bevel: boolean = true
): IOffsetResult;
Generates an offset path parallel to the polyline. The behavior differs for open vs. closed polylines.
Distance to offset the polyline (positive or negative)
Angle threshold for beveling acute corners
Enable beveling at acute angles
is_closed
let closed: bool = polyline.is_closed();
const isClosed = polyline.isClosed;
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();
// Available through internal API
Returns the complete BREP representation.
Properties
Unique identifier for the polyline instance
Array of points defining the polyline path
Whether the polyline forms a closed loop (automatically detected)
Internal BREP representation containing vertices, edges, and optionally a face for closed polylines
Indicates if the polyline is closed (TypeScript property)
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();
import { Polyline, Vector3 } from 'opengeometry-three';
const polyline = new Polyline({
points: [
new Vector3(0, 0, 0),
new Vector3(5, 0, 2),
new Vector3(10, 0, 1),
new Vector3(15, 0, 4)
],
color: 0x00ff00
});
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());
const triangle = new Polyline({
points: [
new Vector3(0, 0, 0),
new Vector3(10, 0, 0),
new Vector3(5, 0, 8.66),
new Vector3(0, 0, 0) // Closes the triangle
],
color: 0xff0000
});
console.log(triangle.isClosed); // true
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
const polyline = new Polyline({ points: [], color: 0x0000ff });
polyline.addPoint(new Vector3(0, 0, 0));
polyline.addPoint(new Vector3(5, 0, 0));
polyline.addPoint(new Vector3(5, 0, 5));
polyline.addPoint(new Vector3(0, 0, 5));
polyline.addPoint(new Vector3(0, 0, 0)); // Close the square
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());
const offsetResult = polyline.getOffset(2.0, 35.0, true);
console.log(offsetResult.points); // Vector3[]
console.log(offsetResult.beveledVertexIndices); // Indices of beveled vertices
console.log(offsetResult.isClosed); // true if original was closed
// Create a new polyline from the offset
const offsetPolyline = new Polyline({
points: offsetResult.points,
color: 0xff00ff
});
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);