Add brush and grid snapping functionality
This commit is contained in:
47
src/geometry/box-brush.ts
Normal file
47
src/geometry/box-brush.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { Vec3 } from "../core/vector";
|
||||
import type { BoxBrush } from "../document/brushes";
|
||||
|
||||
export interface BoxBrushBounds {
|
||||
min: Vec3;
|
||||
max: Vec3;
|
||||
}
|
||||
|
||||
export function getBoxBrushHalfSize(brush: BoxBrush): Vec3 {
|
||||
return {
|
||||
x: brush.size.x * 0.5,
|
||||
y: brush.size.y * 0.5,
|
||||
z: brush.size.z * 0.5
|
||||
};
|
||||
}
|
||||
|
||||
export function getBoxBrushBounds(brush: BoxBrush): BoxBrushBounds {
|
||||
const halfSize = getBoxBrushHalfSize(brush);
|
||||
|
||||
return {
|
||||
min: {
|
||||
x: brush.center.x - halfSize.x,
|
||||
y: brush.center.y - halfSize.y,
|
||||
z: brush.center.z - halfSize.z
|
||||
},
|
||||
max: {
|
||||
x: brush.center.x + halfSize.x,
|
||||
y: brush.center.y + halfSize.y,
|
||||
z: brush.center.z + halfSize.z
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function getBoxBrushCornerPositions(brush: BoxBrush): Vec3[] {
|
||||
const bounds = getBoxBrushBounds(brush);
|
||||
|
||||
return [
|
||||
{ x: bounds.min.x, y: bounds.min.y, z: bounds.min.z },
|
||||
{ x: bounds.max.x, y: bounds.min.y, z: bounds.min.z },
|
||||
{ x: bounds.min.x, y: bounds.max.y, z: bounds.min.z },
|
||||
{ x: bounds.max.x, y: bounds.max.y, z: bounds.min.z },
|
||||
{ x: bounds.min.x, y: bounds.min.y, z: bounds.max.z },
|
||||
{ x: bounds.max.x, y: bounds.min.y, z: bounds.max.z },
|
||||
{ x: bounds.min.x, y: bounds.max.y, z: bounds.max.z },
|
||||
{ x: bounds.max.x, y: bounds.max.y, z: bounds.max.z }
|
||||
];
|
||||
}
|
||||
48
src/geometry/grid-snapping.ts
Normal file
48
src/geometry/grid-snapping.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { Vec3 } from "../core/vector";
|
||||
|
||||
export const DEFAULT_GRID_SIZE = 1;
|
||||
|
||||
function assertGridSize(gridSize: number): number {
|
||||
if (!Number.isFinite(gridSize) || gridSize <= 0) {
|
||||
throw new Error("Grid size must be a positive finite number.");
|
||||
}
|
||||
|
||||
return gridSize;
|
||||
}
|
||||
|
||||
export function snapValueToGrid(value: number, gridSize = DEFAULT_GRID_SIZE): number {
|
||||
const step = assertGridSize(gridSize);
|
||||
|
||||
if (!Number.isFinite(value)) {
|
||||
throw new Error("Grid-snapped values must be finite numbers.");
|
||||
}
|
||||
|
||||
return Math.round(value / step) * step;
|
||||
}
|
||||
|
||||
function snapPositiveSizeValue(value: number, gridSize: number): number {
|
||||
if (!Number.isFinite(value)) {
|
||||
throw new Error("Box brush size values must be finite numbers.");
|
||||
}
|
||||
|
||||
const snappedSize = Math.round(Math.abs(value) / gridSize) * gridSize;
|
||||
return snappedSize > 0 ? snappedSize : gridSize;
|
||||
}
|
||||
|
||||
export function snapVec3ToGrid(vector: Vec3, gridSize = DEFAULT_GRID_SIZE): Vec3 {
|
||||
return {
|
||||
x: snapValueToGrid(vector.x, gridSize),
|
||||
y: snapValueToGrid(vector.y, gridSize),
|
||||
z: snapValueToGrid(vector.z, gridSize)
|
||||
};
|
||||
}
|
||||
|
||||
export function snapPositiveSizeToGrid(size: Vec3, gridSize = DEFAULT_GRID_SIZE): Vec3 {
|
||||
const step = assertGridSize(gridSize);
|
||||
|
||||
return {
|
||||
x: snapPositiveSizeValue(size.x, step),
|
||||
y: snapPositiveSizeValue(size.y, step),
|
||||
z: snapPositiveSizeValue(size.z, step)
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user