CurvePrimitive
常用的見下
LineSegment3d | 直線段 | 兩點直線 | 邊、桿件、骨架 |
LineString3d | 折線 | 多點連續直線 | 輪廓線、路徑 |
Arc3d | 圓弧 / 橢圓弧 | 圓心 + 半徑 + 起止角 | 圓孔、圓角、弧段 |
BezierCurve3d | 貝塞爾曲線 | 端點 + 控制點 | 平滑過渡、動畫軌跡 |
BSplineCurve3d | B 樣條 / NURBS | 控制點 + 節點矢量 | 車身曲面、流線 |
LineSegment3d
示例代碼?LineSegment3d
核心部分,如何創建線段,及如何根據比例取點
-
一條
LineSegment3d
,由起點pointA
和終點pointB
組成。 -
fraction:一個 歸一化參數
-
0.0
→ 起點 -
1.0
→ 終點 -
0.5
→ 線段中點 -
可超范圍:
-0.2
、1.3
等會得到延長線上的點。
-
import { LineSegment3d, Point3d } from "@itwin/core-geometry";export default class SimpleLineApi {public static createLineSegmentFromXY(point1X: number, point1Y: number, point2X: number, point2Y: number): LineSegment3d {const pointA = Point3d.create(point1X, point1Y, 0);const pointB = Point3d.create(point2X, point2Y, 0);return LineSegment3d.create(pointA, pointB);}public static createPointsAlongLine(lineSegment: LineSegment3d, fractionsAlongLine: number[]): Point3d[] {const points: Point3d[] = [];for (const fractionAlongLine of fractionsAlongLine) {const pointAlongLine = lineSegment.fractionToPoint(fractionAlongLine);points.push(pointAlongLine);}return points;}
}
LineString3d
連續線段集合
import { LineString3d, Point3d } from "@itwin/core-geometry";// 1. 從點數組
const ls = LineString3d.create([Point3d.create(0, 0, 0),Point3d.create(1, 2, 3),Point3d.create(4, 5, 6)
]);// 2. 兩點起止
const ls2 = LineString3d.create(Point3d.create(0,0), Point3d.create(10,0), Point3d.create(10,10));// 3. 空折線再 push
const ls3 = LineString3d.create();
ls3.pushXYZ(1,2,3);
ls3.pushXYZ(4,5,6);
Arc
Arc3d.create(center, vectorU, vectorV, AngleSweep.createStartEndDegrees(0, 90))
把圓心、半徑向量、垂直向量及起止角度交給 Arc3d.create
,即可得到一條位于任意 3D 平面中的圓弧。
參數 | 作用 | 典型取值 |
---|---|---|
center | 圓心坐標 | {x:0, y:0, z:0} |
vectorX | 半徑向量?+?X 軸方向 | 長度 = 半徑,例如?Vector3d.create(5,0,0) |
vectorY | 與 vectorX 垂直,定義平面 | 例如?Vector3d.create(0,5,0) |
sweepStartEnd | 兩元素的數組?[startDeg, endDeg] | [0, 90] ?表示 0° 到 90° 的圓弧 |
?
其余的一些對外函數,所以iTwin并不是只能去獲取幾何,還有大量的函數
類別 | 函數簽名(簡寫) | 一句話作用 |
---|---|---|
構造函數 | Arc3d.create(center, vector0, vector90, sweep) | 用圓心 + 兩正交向量 + 角度范圍建圓弧 |
Arc3d.createXY(center, radius, sweep?) | 快速建 X-Y 平面圓弧 | |
Arc3d.createXYZ(center, radius, sweep?) | 快速建 X-Y-Z 空間圓弧 | |
Arc3d.createUnitCircle() | 單位圓(半徑 1,0-360°) | |
Arc3d.fromJSON(json) | 反序列化恢復圓弧 | |
Arc3d.createCircularStartMiddleEnd(start, middle, end) | 三點定圓弧 | |
讀取屬性 | arc.center : Point3d | 圓心坐標 |
arc.vector0 : Vector3d | 0° 方向半徑向量 | |
arc.vector90 : Vector3d | 90° 方向半徑向量 | |
arc.sweep : AngleSweep | 起止角對象 | |
arc.radiusXY : number | 平均半徑 | |
arc.curveLength() | 弧長 | |
arc.fractionToPoint(f) | 比例 0-1 取點 | |
arc.fractionToPointAndDerivative(f) | 取點 + 切線 | |
arc.closestPoint(spacePt) | 空間點到圓弧最近點 | |
arc.isCircular : boolean | 是否整圓(360°) | |
變換 / 復制 | arc.clone() | 深拷貝 |
arc.cloneTransformed(transform) | 變換后拷貝 | |
arc.reverseInPlace() | 就地反轉起止角 | |
幾何操作 | arc.constructOffset(distance, method?) | 生成等距圓弧 |
arc.constructCircularArcChainApproximation(options) | 圓弧 → 多段小圓弧近似 | |
arc.extendToStartEnd(start, end) | 延伸/裁剪到起止點 | |
arc.getRange() | 返回包圍盒?Range3d | |
交互 / 判斷 | arc.isAlmostEqual(other) | 幾何相等比較 |
arc.intersectRay3d(ray) | 與射線求交 | |
arc.intersectPlane(plane) | 與平面求交 | |
arc.intersectRange(range) | 與包圍盒求交 |
BezierCurve3d
Bezier示例
不好意思這個例子很多函數名沒改。。。。但是我不想改了。。
伯恩斯坦多項式
成員 | 返回值 | 一句話作用 |
---|---|---|
curve.controlPoints : Point3d[] | 控制點數組 | 直接拿到當前控制點 |
curve.order : number | 階數 | 2=二次,3=立方 |
curve.curveLength() | 近似弧長 | 快速積分長度 |
curve.fractionToPoint(f) | Point3d | 比例 0-1 取點 |
curve.fractionToPointAndDerivative(f) | {point,derivative} | 取點 + 切線 |
curve.closestPoint(spacePt) | {point,fraction} | 空間點到曲線最近 |
BSplineCurve3d
B樣條曲線示例
Loop
是由折線(或任意曲線鏈)首尾閉合形成的“環”,比如面域邊界、孔洞、截面輪廓
把折線變 Loop(強制閉合)
import { Loop, LineString3d } from "@itwin/core-geometry";const ls = LineString3d.create([p0, p1, p2, p0]); // 首尾同點
const loop = Loop.create(ls); // 生成閉合環
從 Loop 取回折線
const lsAgain = loop.children[0] as LineString3d;
Path
Path 是一個可以混裝任意曲線類型,不強制閉合的 開放曲線鏈(poly-curve),比如布線、掃描路徑、拉伸軌跡。它只表示一條“路徑”,不包圍任何區域;若要圍成區域,請用 Loop。
會把多種線類型,如線段和弧,拼接成一個Path,然后用Decorator來渲染,用作路徑展示之類
import { LineString3d, Arc3d, Path } from "@itwin/core-geometry";const ls = LineString3d.create([p0, p1]);
const arc = Arc3d.createXY(Point3d.create(2,0), 3);const path = Path.create(ls, arc); // 折線 + 圓弧 連成開放鏈
console.log(path.curveLength());
其他的例子
幾何的例子