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
let curve = OGCurve::new(id: String);
const curve = new Curve(options?: ICurveOptions);
Rust Constructor Parameters
Unique identifier for the curve instance
TypeScript Constructor Parameters
Configuration options for the curve
Optional unique identifier (auto-generated if not provided)
Array of control points defining the curve path
Curve color as a hexadecimal number (e.g., 0x00aa55)
Methods
set_config
curve.set_config(control_points: Vec<Vector3>);
Updates the curve’s control points and regenerates geometry.curve.setConfig(options: ICurveOptions);
Updates the curve configuration and regenerates geometry.
generate_geometry
curve.generate_geometry();
// Called automatically by setConfig
Generates vertices and edges connecting the control points sequentially.
get_geometry_serialized
let geometry: String = curve.get_geometry_serialized();
// Used internally for rendering
Returns serialized vertex buffer data as a JSON string.
get_offset_serialized
let offset_data = curve.get_offset_serialized(
distance: f64,
acute_threshold_degrees: f64,
bevel: bool
);
const result = curve.getOffset(
distance: number,
acuteThresholdDegrees: number = 35.0,
bevel: boolean = true
): ICurveOffsetResult;
Generates an offset curve parallel to the original at the specified distance. The curve is treated as an open path.
Distance to offset the curve (positive or negative)
Angle threshold for beveling acute corners
Enable beveling at acute angles
get_brep_serialized
let brep: String = curve.get_brep_serialized();
// Available through internal API
Returns the complete BREP representation including vertices and edges.
dispose
Clears the BREP and control points to free memory.
Properties
Unique identifier for the curve instance
Array of control points defining the curve
Internal BREP representation containing vertices and edges
Curve color (TypeScript only, settable)
Code Examples
Creating a Simple Curve
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);
import { Curve, Vector3 } from 'opengeometry-three';
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
});
Creating a Wave Pattern
// 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
});
Generating Offset Curves
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
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
});
Creating Multiple Parallel Curves
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
});
});
Updating Curve Control Points
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;
Bezier-like Curve Approximation
// 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
});