Skip to content

Commit

Permalink
Fix the damn line
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitris Zervas <[email protected]>
  • Loading branch information
dzervas committed Jun 14, 2024
1 parent 03ac4a3 commit d8229ae
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 120 deletions.
11 changes: 6 additions & 5 deletions applications/web/src/components/Arc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import {T} from "@threlte/core"
import {flatten, arcToPoints, promoteTo3} from "shared/projectUtils"
import {currentlySelected, currentlyMousedOver, sketchTool} from "shared/stores"
import type {EntityType, SketchPoint} from "shared/types"
import type {EntityType} from "shared/types"
import {isEntity} from "shared/typeGuards"
import type { Point2 } from "cadmium"
// @ts-ignore
const log = (function () { const context = "[Arc.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() // prettier-ignore
const type: EntityType = "arc"
export let id: string, center: SketchPoint, start: SketchPoint, end: SketchPoint
export let id: string, center: Point2, start: Point2, end: Point2
export let dashedLineMaterial: LineMaterial,
dashedHoveredMaterial: LineMaterial,
Expand All @@ -26,9 +27,9 @@
$: selected = $currentlySelected.some(e => isEntity(e) && e.id === id && e.type === type) ? true : false
const center2 = new Vector2(center.twoD.x, center.twoD.y)
const start2 = new Vector2(start.twoD.x, start.twoD.y)
const end2 = new Vector2(end.twoD.x, end.twoD.y)
const center2 = new Vector2(center.x, center.y)
const start2 = new Vector2(start.x, start.y)
const end2 = new Vector2(end.x, end.y)
const points = flatten(promoteTo3(arcToPoints(center2, start2, end2 /** implicit false */))) // defaulted false in function todo ask Matt
Expand Down
7 changes: 4 additions & 3 deletions applications/web/src/components/Circle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import {T} from "@threlte/core"
import {flatten, circleToPoints, promoteTo3} from "shared/projectUtils"
import {currentlySelected, currentlyMousedOver, sketchTool} from "shared/stores"
import type {CircleTuple, EntityType} from "shared/types"
import type {EntityType} from "shared/types"
import type { Point2 } from "cadmium"
// @ts-ignore
const log = (function () { const context = "[Circle.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() // prettier-ignore
const type: EntityType = "circle"
export let id: string, center: CircleTuple["center"], radius: number
export let id: string, center: Point2, radius: number
// log("[props]", "id:", id, "center:", center, "radius:", radius)
Expand All @@ -26,7 +27,7 @@
$: selected = $currentlySelected.some(e => e.id === id && e.type === type) ? true : false
// array of x,y,z points
const points = flatten(promoteTo3(circleToPoints(center.twoD, radius)))
const points = flatten(promoteTo3(circleToPoints(center, radius)))
const lineGeometry = new LineGeometry()
lineGeometry.setPositions(points)
Expand Down
18 changes: 5 additions & 13 deletions applications/web/src/components/Line.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import {T} from "@threlte/core"
import {flatten, promoteTo3} from "shared/projectUtils"
import {currentlySelected, currentlyMousedOver, sketchTool} from "shared/stores"
import type {EntityType, PointById} from "shared/types"
import type {EntityType} from "shared/types"
import {isEntity} from "shared/typeGuards"
import type { Point2 } from "cadmium"
// @ts-ignore
const log = (function () { const context = "[Line.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() // prettier-ignore
export let id: string, start: PointById, end: PointById
export let id: string, start: Point2, end: Point2
export let dashedLineMaterial: LineMaterial,
dashedHoveredMaterial: LineMaterial,
Expand All @@ -23,19 +24,10 @@
const type: EntityType = "line"
let hovered = false
$: selected = $currentlySelected.some(e => checkIsEntity(e) && e.id === id && e.type === type) ? true : false
function checkIsEntity(e: unknown) {
// log("[checkIsEntity]", isEntity(e), e)
return isEntity(e)
}
// $: selected, log("[selected] an entity has been selected:", selected, "Line.id:", id)
// prettier-ignore
// $: $currentlySelected, ()=> {if (selected && !$currentlySelected.every((e) => isEntity(e))) log("ERROR [currentlySelected] are not all isEntity", $currentlySelected)}
$: selected = $currentlySelected.some(e => isEntity(e) && e.id === id && e.type === type) ? true : false
const points = flatten(
promoteTo3([new Vector2(start.twoD.x, start.twoD.y), new Vector2(end.twoD.x, end.twoD.y)])
promoteTo3([new Vector2(start.x, start.y), new Vector2(end.x, end.y)])
)
const lineGeometry = new LineGeometry()
Expand Down
62 changes: 33 additions & 29 deletions applications/web/src/components/PassiveSketch.svelte
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
<script lang="ts">
import {Matrix4, Euler, MeshStandardMaterial, Vector2, Vector3} from "three"
import {T, useThrelte} from "@threlte/core"
import {Text, Suspense} from "@threlte/extras"
import {hiddenSketches, previewGeometry, sketchTool, workbench} from "shared/stores"
import Point2D from "./Point2D.svelte"
import Line from "./Line.svelte"
import Circle from "./Circle.svelte"
import Arc from "./Arc.svelte"
import Face from "./Face.svelte"
import {LineMaterial} from "three/addons/lines/LineMaterial.js"
import {LineGeometry} from "three/addons/lines/LineGeometry.js"
// import Face from "./Face.svelte"
import NewLineTool from "./tools/NewLine.svelte"
import NewCircleTool from "./tools/NewCircle.svelte"
import NewRectangleTool from "./tools/NewRectangle.svelte"
import SelectTool from "./tools/Select.svelte"
import type {FaceTuple, IDictionary, PreviewGeometry, SketchPoint} from "shared/types"
import {T, useThrelte} from "@threlte/core"
import {Text, Suspense} from "@threlte/extras"
import {Matrix4, Euler, MeshStandardMaterial, Vector2, Vector3} from "three"
import {LineMaterial} from "three/addons/lines/LineMaterial.js"
import {LineGeometry} from "three/addons/lines/LineGeometry.js"
import debounce from "just-debounce-it"
import type {ISketch, Plane} from "cadmium"
import {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"
// @ts-ignore
const log = (function () { const context = "[PassiveSketch.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,
sketch: ISketch,
plane: Plane,
uniqueId: string,
uniqueId: StepHash,
editing = false
const {size, dpr} = useThrelte()
Expand All @@ -41,9 +44,6 @@
let newLineTool: NewLineTool, newCircleTool: NewCircleTool, newRectangleTool: NewRectangleTool, selectTool: SelectTool
let faceTuples: FaceTuple[] = []
let pointsById: IDictionary<SketchPoint> = {}
// Build some Three.js vectors from the props
const origin_point = new Vector3(plane.origin.x, plane.origin.y, plane.origin.z)
const primary = new Vector3(plane.primary.x, plane.primary.y, plane.primary.z)
Expand Down Expand Up @@ -88,7 +88,11 @@
lineGeometry.setPositions(points)
$: hidden = $hiddenSketches.includes(uniqueId) && !editing
// $: $hiddenSketches, log("[$hiddenSketches]", $hiddenSketches)
$: $hiddenSketches, log("[$hiddenSketches]", hidden)
$: pointsById = history.reduce((acc, step) => {
if (isSketchPointStep(step)) acc[step.hash] = step.result.Primitive.Point2
return acc
}, {} as IDictionary<Point2>)
$: if (editing) $sketchTool = "select"
Expand Down Expand Up @@ -117,7 +121,7 @@
on:click={e => {
if (editing) {
if ($sketchTool === "line") {
newLineTool.click(e, {twoD: projectToPlane(e.point), threeD: e.point})
newLineTool.click(e, projectToPlane(e.point))
} else if ($sketchTool === "circle") {
newCircleTool.click(e, {twoD: projectToPlane(e.point), threeD: e.point})
} else if ($sketchTool === "rectangle") {
Expand Down Expand Up @@ -163,11 +167,11 @@

{#each history as step}
{#if isSketchPointStep(step)}
<Point2D x={step.result.Point2.x} y={step.result.Point2.y} hidden={step.result.Point2.hidden} id={step.hash} {collisionLineMaterial} />
<Point2D x={step.result.Primitive.Point2.x} y={step.result.Primitive.Point2.y} hidden={step.result.Primitive.Point2.hidden} id={step.hash} {collisionLineMaterial} />
{:else if isSketchCircleStep(step)}
<Circle
center={pointsById[step.result.Circle2.center]}
radius={step.result.Circle2.radius}
center={pointsById[step.result.Primitive.Circle2.center]}
radius={step.result.Primitive.Circle2.radius}
id={step.hash}
{solidLineMaterial}
{solidHoveredMaterial}
Expand All @@ -179,7 +183,7 @@
{:else if isSketchArcStep(step)}
<!-- TODO: Use start & end angle instead of points -->
<Arc
center={pointsById[step.result.Arc2.center]}
center={pointsById[step.result.Primitive.Arc2.center]}
start={pointsById[0]}
end={pointsById[1]}
id={step.hash}
Expand All @@ -192,8 +196,8 @@
/>
{:else if isSketchLineStep(step)}
<Line
start={pointsById[step.result.Line2.start]}
end={pointsById[step.result.Line2.end]}
start={pointsById[step.result.Primitive.Line2.start]}
end={pointsById[step.result.Primitive.Line2.end]}
id={step.hash}
{solidLineMaterial}
{solidHoveredMaterial}
Expand All @@ -208,8 +212,8 @@
{#each $previewGeometry as geom (geom.uuid)}
{#if isGeomType(geom, "line")}
<Line
start={geom.start}
end={geom.end}
start={geom.start!}
end={geom.end!}
id={geom.uuid}
{solidLineMaterial}
{solidHoveredMaterial}
Expand All @@ -220,8 +224,8 @@
/>
{:else if isGeomType(geom, "circle")}
<Circle
center={geom.center}
radius={geom.radius}
center={geom.center!}
radius={geom.radius!}
id={geom.uuid}
{solidLineMaterial}
{solidHoveredMaterial}
Expand All @@ -231,12 +235,12 @@
{collisionLineMaterial}
/>
{:else if isGeomType(geom, "point")}
<Point2D x={geom.x} y={geom.y} hidden={false} id={geom.uuid} isPreview {collisionLineMaterial} />
<Point2D x={geom.x!} y={geom.y!} hidden={false} id={geom.uuid} isPreview {collisionLineMaterial} />
{/if}
{/each}

{#each faceTuples as face (`${faceTuples.length}-${face.id}`)}
<!-- {#each faceTuples as face (`${faceTuples.length}-${face.id}`)}
<Face face={face.face} id={face.id} {pointsById} />
{/each}
{/each} -->
</T.Group>
{/if}
2 changes: 1 addition & 1 deletion applications/web/src/components/Point2D.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import type {EntityType} from "shared/types"
import {base} from "../base"
export let x, y, hidden: boolean, id: string
export let x: number, y: number, hidden: boolean, id: string
export let isPreview = false
export let collisionLineMaterial: LineMaterial
Expand Down
6 changes: 3 additions & 3 deletions applications/web/src/components/Scene.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
{#each history as step}
{#if isPointStep(step)}
<Point3D
id={`${step.hash}`}
id={step.hash}
x={step.result.Point.x}
y={step.result.Point.y}
z={step.result.Point.z}
Expand All @@ -138,7 +138,7 @@
{:else if isPlaneStep(step)}
<Plane
name={step.name}
id={`${step.hash}`}
id={step.hash}
height={100}
width={100}
origin={step.result.Plane.origin}
Expand All @@ -148,7 +148,7 @@
/>
{:else if isSketchStep(step)}
<Sketch
uniqueId={`${step.hash}`}
uniqueId={step.hash}
name={step.name}
sketch={step.result.Sketch.sketch}
editing={$sketchBeingEdited === step.hash}
Expand Down
2 changes: 0 additions & 2 deletions applications/web/src/components/Sketch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<PassiveSketch
{name}
{uniqueId}
sketch={sketch.sketch}
plane={sketch.plane}
editing
{solidLineMaterial}
Expand All @@ -61,7 +60,6 @@
<PassiveSketch
{name}
{uniqueId}
sketch={sketch.sketch}
plane={sketch.plane}
{solidLineMaterial}
{solidHoveredMaterial}
Expand Down
19 changes: 10 additions & 9 deletions applications/web/src/components/tools/NewCircle.svelte
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<script lang="ts">
import {snapPoints, sketchTool, previewGeometry, currentlyMousedOver} from "shared/stores"
import {addCircleBetweenPoints, addPointToSketch, bench} from "shared/projectUtils"
import {bench} from "shared/projectUtils"
import {Vector3, type Vector2Like, type Vector3Like} from "three"
import type {PointLikeById, Point2D, PointsLikeById, ProjectToPlane} from "shared/types"
import type {PointLikeById, Point2D, ProjectToPlane, IDictionary, PointById} from "shared/types"
import type { Point2 } from "cadmium"
// @ts-ignore
const log = (function () { const context = "[NewCircleTool.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() // prettier-ignore
export let pointsById: PointsLikeById
export let pointsById: IDictionary<Point2>
export let sketchId: string
export let active: boolean
export let projectToPlane: ProjectToPlane
let centerPoint: PointLikeById | null = null
let stack: PointLikeById[] = []
let centerPoint: Point2 | null = null
let stack: Point2[] = []
$: if ($sketchTool !== "circle") clearStack()
function pushToStack(point: PointLikeById) {
function pushToStack(point: PointById) {
if (!point) return
point.id = point.id ?? bench.sketchAddPoint(sketchId, point.twoD.x, point.threeD.y).data
stack.push(point)
}
function processPoint(point: PointLikeById) {
function processPoint(point: PointById) {
pushToStack(point)
centerPoint = point
Expand Down Expand Up @@ -68,7 +69,7 @@
}
if (geom.type === "point") {
const point = pointsById[geom.id]
if (point.twoD && point.threeD && geom.id) snappedTo = {twoD: point.twoD, threeD: point.threeD, id: geom.id}
if (point && geom.id) snappedTo = {point: point, id: geom.id}
break // If there is a 2D point, prefer to use it rather than the 3D point
}
}
Expand All @@ -83,7 +84,7 @@
const dy = a.y - b?.y!
return Math.hypot(dx, dy)
}
const radius = snappedTo ? calcDeltas(snappedTo.twoD, centerPoint.twoD) : calcDeltas(projected, centerPoint.twoD)
const radius = snappedTo ? calcDeltas(snappedTo.point!, centerPoint.twoD) : calcDeltas(projected, centerPoint.twoD)
previewGeometry.set([
{
Expand Down
Loading

0 comments on commit d8229ae

Please sign in to comment.