Skip to content

Commit

Permalink
Do more face work
Browse files Browse the repository at this point in the history
  • Loading branch information
dzervas committed Jun 14, 2024
1 parent 318bc21 commit 74c2ece
Show file tree
Hide file tree
Showing 20 changed files with 186 additions and 155 deletions.
17 changes: 9 additions & 8 deletions applications/web/src/components/Face.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,25 @@
} else if (wire.Segments) {
let points = []
for (let segment of wire.Segments) {
if (segment.type === "Line") {
let start = pointsById[segment.start]
let end = pointsById[segment.end]
console.log("segment", segment)
if ("Line" in segment) {
let start = pointsById[segment.Line.start]
let end = pointsById[segment.Line.end]
if (points.length === 0) {
points.push(new Vector2(start.x, start.y))
}
points.push(new Vector2(end.x, end.y))
} else if (segment.type === "Arc") {
let center = pointsById[segment.center]
let start = pointsById[segment.start]
let end = pointsById[segment.end]
} else if ("Arc" in segment) {
let center = pointsById[segment.Arc.center]
let start = pointsById[segment.Arc.start]
let end = pointsById[segment.Arc.end]
let arcPoints = arcToPoints(
new Vector2(center.x, center.y),
new Vector2(start.x, start.y),
new Vector2(end.x, end.y),
segment.clockwise,
segment.Arc.clockwise,
)
if (points.length !== 0) {
Expand Down
4 changes: 2 additions & 2 deletions applications/web/src/components/FeatureHistory.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
{#if isPointStep(step)}
<PointFeature name={step.name} index={featureIndex} />
{:else if isPlaneStep(step)}
<PlaneFeature name={step.name} index={featureIndex} plane={step.result.Plane} {setCameraFocus} />
<PlaneFeature name={step.name} index={featureIndex} plane={step.result} {setCameraFocus} />
{:else if isSketchStep(step)}
<SketchFeature name={step.name} index={featureIndex} hash={step.hash} plane_desc={step.data.WorkbenchSketchAdd.plane_description} />
<SketchFeature name={step.name} index={featureIndex} hash={step.hash} plane_desc={step.data.plane_description} />
{:else if isExtrusionStep(step)}
<ExtrusionFeature name={step.name} index={featureIndex} hash={step.hash} data={step.data} />
{:else}
Expand Down
31 changes: 16 additions & 15 deletions applications/web/src/components/PassiveSketch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import debounce from "just-debounce-it"
import {isSketchArcStep, isSketchCircleStep, isSketchLineStep, isSketchPointStep} from "shared/stepTypeGuards"
import {isSketchActionStep, isSketchArcStep, isSketchCircleStep, isSketchLineStep, isSketchPointStep} from "shared/stepTypeGuards"
import {hiddenSketches, previewGeometry, sketchTool, workbench} from "shared/stores"
import type {IDictionary, PreviewGeometry} from "shared/types"
import type {Plane, Point2, StepHash} from "cadmium"
Expand All @@ -29,7 +29,6 @@
export let name: string,
plane: Plane,
hash: StepHash,
faces: Face[],
editing: boolean = false
const {size, dpr} = useThrelte()
Expand Down Expand Up @@ -91,7 +90,7 @@
$: hidden = $hiddenSketches.includes(hash) && !editing
$: $hiddenSketches, log("[$hiddenSketches]", hidden)
$: pointsById = history.reduce((acc, step) => {
if (isSketchPointStep(step)) acc[step.hash] = step.result.Primitive.Point2
if (isSketchPointStep(step)) acc[step.hash] = step.result.action
return acc
}, {} as IDictionary<Point2>)
Expand Down Expand Up @@ -124,9 +123,9 @@
if ($sketchTool === "line") {
newLineTool.click(e, projectToPlane(e.point))
} else if ($sketchTool === "circle") {
newCircleTool.click(e, {twoD: projectToPlane(e.point), threeD: e.point})
newCircleTool.click(e, projectToPlane(e.point))
} else if ($sketchTool === "rectangle") {
newRectangleTool.click(e, {twoD: projectToPlane(e.point), threeD: e.point})
newRectangleTool.click(e, projectToPlane(e.point))
} else if ($sketchTool === "select") {
selectTool.click(e, projectToPlane(e.point))
}
Expand Down Expand Up @@ -168,11 +167,11 @@

{#each history as step}
{#if isSketchPointStep(step)}
<Point2D x={step.result.Primitive.Point2.x} y={step.result.Primitive.Point2.y} hidden={step.result.Primitive.Point2.hidden} id={step.hash} {collisionLineMaterial} />
<Point2D x={step.result.action.x} y={step.result.action.y} hidden={step.result.action.hidden} id={step.hash} {collisionLineMaterial} />
{:else if isSketchCircleStep(step)}
<Circle
center={pointsById[step.result.Primitive.Circle2.center]}
radius={step.result.Primitive.Circle2.radius}
center={pointsById[step.result.action.center]}
radius={step.result.action.radius}
id={step.hash}
{solidLineMaterial}
{solidHoveredMaterial}
Expand All @@ -184,7 +183,7 @@
{:else if isSketchArcStep(step)}
<!-- TODO: Use start & end angle instead of points -->
<Arc
center={pointsById[step.result.Primitive.Arc2.center]}
center={pointsById[step.result.action.center]}
start={pointsById[0]}
end={pointsById[1]}
id={step.hash}
Expand All @@ -197,8 +196,8 @@
/>
{:else if isSketchLineStep(step)}
<Line
start={pointsById[step.result.Primitive.Line2.start]}
end={pointsById[step.result.Primitive.Line2.end]}
start={pointsById[step.result.action.start]}
end={pointsById[step.result.action.end]}
id={step.hash}
{solidLineMaterial}
{solidHoveredMaterial}
Expand All @@ -208,6 +207,12 @@
{collisionLineMaterial}
/>
{/if}

{#if isSketchActionStep(step)}
{#each step.result.faces as face, faceId}
<Face face={face} id={`${hash}-${faceId}`} {pointsById} />
{/each}
{/if}
{/each}

{#each $previewGeometry as geom (geom.uuid)}
Expand Down Expand Up @@ -239,9 +244,5 @@
<Point2D x={geom.x!} y={geom.y!} hidden={false} id={geom.uuid} isPreview {collisionLineMaterial} />
{/if}
{/each}

{#each faces as face, faceId}
<Face face={face} id={`${hash}-${faceId}`} {pointsById} />
{/each}
</T.Group>
{/if}
21 changes: 10 additions & 11 deletions applications/web/src/components/Scene.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@
{#if isPointStep(step)}
<Point3D
id={step.hash}
x={step.result.Point.x}
y={step.result.Point.y}
z={step.result.Point.z}
hidden={step.result.Point.hidden}
x={step.result.x}
y={step.result.y}
z={step.result.z}
hidden={step.result.hidden}
{collisionLineMaterial}
/>
{:else if isPlaneStep(step)}
Expand All @@ -141,17 +141,16 @@
id={step.hash}
height={100}
width={100}
origin={step.result.Plane.origin}
primary={step.result.Plane.primary}
secondary={step.result.Plane.secondary}
tertiary={step.result.Plane.tertiary}
origin={step.result.origin}
primary={step.result.primary}
secondary={step.result.secondary}
tertiary={step.result.tertiary}
/>
{:else if isSketchStep(step)}
<Sketch
hash={step.hash}
name={step.name}
sketch={step.result.Sketch.sketch}
faces={step.result.Sketch.faces}
sketch={step.result}
editing={$sketchBeingEdited === step.hash}
{solidLineMaterial}
{solidHoveredMaterial}
Expand All @@ -161,7 +160,7 @@
{collisionLineMaterial}
/>
{:else if isSolidStep(step)}
{#each step.result.Solid as solid}
{#each step.result as solid}
<Solid
name={step.name}
indices={solid.indices}
Expand Down
4 changes: 1 addition & 3 deletions applications/web/src/components/Sketch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import type {LineMaterial} from "three/examples/jsm/lines/LineMaterial.js"
import {currentlySelected, previewGeometry, sketchTool} from "shared/stores"
import type {Face} from "shared/types"
import type {ISketch} from "cadmium"
// @ts-ignore
const log = (function () { const context = "[Sketch.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() // prettier-ignore
export let hash: string, name: string, sketch: ISketch, faces: Face[], editing: boolean = false
export let hash: string, name: string, sketch: ISketch, editing: boolean = false
export let dashedLineMaterial: LineMaterial,
dashedHoveredMaterial: LineMaterial,
Expand Down Expand Up @@ -50,7 +49,6 @@
{name}
{hash}
plane={sketch.plane}
{faces}
editing
{solidLineMaterial}
{solidHoveredMaterial}
Expand Down
3 changes: 2 additions & 1 deletion applications/web/src/components/features/Extrusion.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import X from "phosphor-svelte/lib/X"
import {base} from "../../base"
import type {FeatureExtrusionAdd} from "shared/cadmium-api"
import type { StepHash } from "cadmium"
// @ts-ignore
const log = (function () { const context = "[ExtrusionFeature.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() // prettier-ignore
export let name: string, index: number, hash: number, data: FeatureExtrusionAdd
export let name: string, index: number, hash: StepHash, data: FeatureExtrusionAdd
// $: data, log("[props]", "name:", name, "index:", index, "id:", id, "data:", data)
// $: data, log("[props]", "typeof id:", typeof id, "id:", id)
Expand Down
57 changes: 23 additions & 34 deletions applications/web/src/components/tools/NewCircle.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import {snapPoints, sketchTool, previewGeometry, currentlyMousedOver} from "shared/stores"
import {bench} from "shared/projectUtils"
import {Vector3, type Vector2Like, type Vector3Like} from "three"
import type {PointLikeById, Point2D, ProjectToPlane, IDictionary, PointById} from "shared/types"
import {Vector2, Vector3, type Vector2Like} from "three"
import type {PointLikeById, Point2D, ProjectToPlane, IDictionary, Point2WithID} from "shared/types"
import type { Point2 } from "cadmium"
// @ts-ignore
Expand All @@ -14,39 +14,32 @@
export let projectToPlane: ProjectToPlane
let centerPoint: Point2 | null = null
let stack: Point2[] = []
let stack: Point2WithID[] = []
$: if ($sketchTool !== "circle") clearStack()
function pushToStack(point: PointById) {
function pushToStack(point: Point2WithID) {
if (!point) return
point.id = point.id ?? bench.sketchAddPoint(sketchId, point.twoD.x, point.threeD.y).data
point.id = point.id ?? bench.sketchAddPoint(sketchId, point.x, point.y).data
stack.push(point)
}
function processPoint(point: PointById) {
function processPoint(point: Point2WithID) {
pushToStack(point)
centerPoint = point
switch (stack.length) {
case 0: // nothing to do, the stack is empty
break
case 1: // can't create a circle with only one point!
break
default:
const circumference = popFromStack()
const center = popFromStack()
bench.sketchAddCircle(sketchId, center!.id!, circumference!.id!)
clearStack()
break
}
if (stack.length < 2) return
const circumference = popFromStack()
const center = popFromStack()
bench.sketchAddCircle(sketchId, center!.id!, circumference!.id!)
clearStack()
}
export function click(_event: Event, projected: {twoD: Vector2Like; threeD: Vector3Like}) {
export function click(_event: Event, projected: Vector2) {
if ($snapPoints.length > 0) processPoint($snapPoints[0])
else {
let pt: PointLikeById = {twoD: projected.twoD, threeD: projected.threeD, id: null}
processPoint(pt)
processPoint({x: projected.x, y: projected.y, hidden: false} as Point2WithID)
}
}
Expand All @@ -57,19 +50,15 @@
// TODO: in the future, we should also snap to the midpoints of lines
// and to the perimeters of circles and so on
// so these snap points do not necessarily correspond to actual points in the sketch
let snappedTo = null
let snappedTo: Point2WithID | null = null
for (const geom of $currentlyMousedOver) {
if (geom.type === "point3D") {
const twoD = projectToPlane(new Vector3(geom.x, geom.y, geom.z))
snappedTo = {
twoD: {x: twoD.x, y: twoD.y},
threeD: {x: geom.x, y: geom.y, z: geom.z},
id: null,
}
const point = projectToPlane(new Vector3(geom.x, geom.y, geom.z))
snappedTo = {x: point.x, y: point.y, hidden: false} as Point2WithID
}
if (geom.type === "point") {
const point = pointsById[geom.id]
if (point && geom.id) snappedTo = {point: point, id: geom.id}
if (point && geom.id) snappedTo = {x: point.x, y: point.y, hidden: false, id: geom.id} as Point2WithID
break // If there is a 2D point, prefer to use it rather than the 3D point
}
}
Expand All @@ -84,20 +73,20 @@
const dy = a.y - b?.y!
return Math.hypot(dx, dy)
}
const radius = snappedTo ? calcDeltas(snappedTo.point!, centerPoint.twoD) : calcDeltas(projected, centerPoint.twoD)
const radius = snappedTo ? calcDeltas(snappedTo!, centerPoint) : calcDeltas(projected, centerPoint)
previewGeometry.set([
{
type: "circle",
center: centerPoint,
radius,
uuid: `circle-${centerPoint.twoD?.x}-${centerPoint.twoD?.y}-${radius}`,
uuid: `circle-${centerPoint.x}-${centerPoint.y}-${radius}`,
},
{
type: "point",
x: centerPoint.twoD?.x,
y: centerPoint.twoD?.y,
uuid: `point-${centerPoint.twoD?.x}-${centerPoint.twoD?.y}`,
x: centerPoint.x,
y: centerPoint.y,
uuid: `point-${centerPoint.x}-${centerPoint.y}`,
},
])
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/cadmium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"dev": "pnpm build:dev && onchange 'src/**/*.rs' -- pnpm build:dev",
"build:dev": "cross-env cargo check && wasm-pack build --no-pack --target web --dev",
"build": "cross-env cargo run --bin gen-types && wasm-pack build --target web --no-pack",
"build": "cross-env cargo run --release --bin gen-types && wasm-pack build --target web --no-pack",
"clean": "rimraf target pkg node_modules",
"test": "cargo test",
"postinstall": "pnpm build"
Expand Down
1 change: 1 addition & 0 deletions packages/cadmium/src/archetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ pub struct Line2 {

#[derive(Tsify, Debug, Clone, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
#[serde(tag = "type")]
pub enum WrappedPrimitive {
Point2(Point2),
Line2(Line2),
Expand Down
4 changes: 2 additions & 2 deletions packages/cadmium/src/bin/gen-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
r#"
export interface {name} {{ {iface_fields} }}
export function {fn_name}({fn_params}): MessageResult {{
const message: Message = {{ {name}: {{ {iface_params} }} }};
const message: Message = {{ type: "{name}", {iface_params} }};
return sendWasmMessage(message);
}}"#
)
Expand All @@ -45,7 +45,7 @@ export function {fn_name}({fn_params}): MessageResult {{

let message_variants = all_defs
.iter()
.map(|(name, _)| format!("{{ {name}: {name} }}"))
.map(|(name, _)| format!("{{ type: \"{name}\" }} & {name}"))
.collect::<Vec<_>>()
.join(" | ");

Expand Down
3 changes: 2 additions & 1 deletion packages/cadmium/src/isketch/compound_rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
use tsify_next::Tsify;

use crate::message::MessageHandler;
use crate::step::sketch_action::IntoSketchActionResult;
use crate::step::StepResult;
use crate::IDType;

Expand Down Expand Up @@ -103,6 +104,6 @@ impl MessageHandler for Add {
isketch.compounds.insert(rectangle_id, compound.clone());
isketch.compounds_next_id += 1;

Ok(Some((rectangle_id, StepResult::Compound(compound))))
Ok(Some((rectangle_id, compound.into_result(&isketch))))
}
}
Loading

0 comments on commit 74c2ece

Please sign in to comment.