Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rectangle tool to sketch mode #2005

Merged
merged 34 commits into from
Apr 19, 2024
Merged

Add rectangle tool to sketch mode #2005

merged 34 commits into from
Apr 19, 2024

Conversation

franknoirot
Copy link
Collaborator

@franknoirot franknoirot commented Apr 3, 2024

Resolves #1500. Adds a rectangle tool to sketch mode.

Demo

Screenshare.-.2024-04-17.8_42_11.PM-compressed.mp4

@franknoirot franknoirot added enhancement New feature or request sketch labels Apr 3, 2024
@franknoirot franknoirot self-assigned this Apr 3, 2024
Copy link

vercel bot commented Apr 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
modeling-app ✅ Ready (Inspect) Visit Preview Apr 19, 2024 3:56pm

@Irev-Dev
Copy link
Collaborator

Irev-Dev commented Apr 3, 2024

Sweet!
I'll take a look and start with the brittle problem.

@Irev-Dev
Copy link
Collaborator

Irev-Dev commented Apr 3, 2024

Pretty sure I was able to resolve the conflicts.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, https://github.com/KittyCAD/modeling-app/pull/1995/files#r1549121189 hasn't been merged, so maybe this will be enough 🤷

@franknoirot
Copy link
Collaborator Author

Alright I've taken a bit more of a look at this and I think I've got my head around it enough to ask you some questions if you have some spare time to pair soon @Irev-Dev.

I think that I need to make use of truncatedAst in my onMove handler because I'm using the whole AST which isn't as efficient anyway and is maybe causing an issue? I've tried to replace it in there, but my THREEjs segments break, I suspect because their IDs aren't mapping correctly when I use truncatedAst.

Or if it would save us time I'm happy for you to wrap this up and I'll learn your approach, you probably know just what to look for.

@franknoirot
Copy link
Collaborator Author

Alright I got this working, the only thing that I need to talk to you about now is how I should handle if a user tries to use the Rectangle tool in a sketch with content already. Because we make everything in sketches one PipeExpression this is problematic, so maybe for now we only allow use of the Rectangle tool if you have an empty sketch?

@franknoirot
Copy link
Collaborator Author

I don't like that though

@Irev-Dev
Copy link
Collaborator

Sorry I think you need to rebase, there seems to unrelated changes in here.
Sorry to slow things down but I don't want to get confused as to what are the changes I need to be looking at.
Ping me again when it's ready.

Copy link
Collaborator Author

@franknoirot franknoirot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I think I've removed anything unrelated and commented on the gnarliest part of the diff to explain what's changing, but let me know if I've misunderstood what you mean.

Comment on lines -806 to +1058
sketchGroup
)
const originalPathToNodeStr = JSON.stringify(segPathToNode)
segPathToNode[1][0] = varDecIndex
const pathToNodeStr = JSON.stringify(segPathToNode)
// more hacks to hopefully be solved by proper pathToNode info in memory/sketchGroup segments
const group =
this.activeSegments[pathToNodeStr] ||
this.activeSegments[originalPathToNodeStr]
// const prevSegment = sketchGroup.slice(index - 1)[0]
const type = group?.userData?.type
const factor =
(sceneInfra.camControls.camera instanceof OrthographicCamera
? orthoFactor
: perspScale(sceneInfra.camControls.camera, group)) /
sceneInfra._baseUnitMultiplier
if (type === TANGENTIAL_ARC_TO_SEGMENT) {
this.updateTangentialArcToSegment({
prevSegment: sgPaths[index - 1],
from: segment.from,
to: segment.to,
group: group,
scale: factor,
})
} else if (type === STRAIGHT_SEGMENT) {
this.updateStraightSegment({
from: segment.from,
to: segment.to,
group: group,
scale: factor,
})
} else if (type === PROFILE_START) {
group.position.set(segment.from[0], segment.from[1], 0)
group.scale.set(factor, factor, factor)
}
}
updateSegment(sketchGroup.start, 0)
sgPaths.forEach(updateSegment)
)
})()
}

/**
* Update the THREEjs sketch entities with new segment data
* mapping them back to the AST
* @param segment
* @param index
* @param varDecIndex
* @param modifiedAst
* @param orthoFactor
* @param sketchGroup
*/
updateSegment = (
segment: Path | SketchGroup['start'],
index: number,
varDecIndex: number,
modifiedAst: Program,
orthoFactor: number,
sketchGroup: SketchGroup
) => {
const segPathToNode = getNodePathFromSourceRange(
modifiedAst,
segment.__geoMeta.sourceRange
)
const sgPaths = sketchGroup.value
const originalPathToNodeStr = JSON.stringify(segPathToNode)
segPathToNode[1][0] = varDecIndex
const pathToNodeStr = JSON.stringify(segPathToNode)
// more hacks to hopefully be solved by proper pathToNode info in memory/sketchGroup segments
const group =
this.activeSegments[pathToNodeStr] ||
this.activeSegments[originalPathToNodeStr]
// const prevSegment = sketchGroup.slice(index - 1)[0]
const type = group?.userData?.type
const factor =
(sceneInfra.camControls.camera instanceof OrthographicCamera
? orthoFactor
: perspScale(sceneInfra.camControls.camera, group)) /
sceneInfra._baseUnitMultiplier
if (type === TANGENTIAL_ARC_TO_SEGMENT) {
this.updateTangentialArcToSegment({
prevSegment: sgPaths[index - 1],
from: segment.from,
to: segment.to,
group: group,
scale: factor,
})
} else if (type === STRAIGHT_SEGMENT) {
this.updateStraightSegment({
from: segment.from,
to: segment.to,
group,
scale: factor,
})
} else if (type === PROFILE_START) {
group.position.set(segment.from[0], segment.from[1], 0)
group.scale.set(factor, factor, factor)
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an annoying diff to read but what happened here is updateSegment was a little helper function inside setupSketchIdleCallbacks, but in order to make use of it up in setupDraftRectangle we had to pull it out and make it a top-level method on the class.

Comment on lines 525 to 543
await expect(page.locator('.cm-content')).toHaveText(
`const part001 = startSketchOn('-XZ')
|> startProfileAt([18.2, 5.98], %)
|> angledLine([180, 9.14], %, 'rectangleSegmentA001')
|> angledLine([
segAng('rectangleSegmentA001', %) + 90,
18.2
], %, 'rectangleSegmentB001')
|> angledLine([
segAng('rectangleSegmentA001', %),
-segLen('rectangleSegmentA001', %)
], %, 'rectangleSegmentC001')
|> close(%)`
)

await expect(page).toHaveScreenshot({
maxDiffPixels: 100,
})
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to think about the philosophy of when we use snap shots, and I think it should be, "verify things visually when we can verify it in other ways".

And to that end asserting that the code here is something very specific makes me think that we should not need a screen shot, since we should be pretty confident that a simple sketch code like this will show up correctly in the app.

HOWEVER,

The draft lines before the second click might need verifying visually because there's no code in the app for that, and a check for "is the draft rectangle animating correctly?"

So maybe a better test is

await page.mouse.click(startXPx + PUR * 20, 500 - PUR * 30)
await page.mouse.move(startXPx + PUR * 10, 500 - PUR * 10, {steps: 5})
// take screenshot here!
await page.mouse.click(startXPx + PUR * 10, 500 - PUR * 10)

await expect(page.locator('.cm-content')).toHaveText( //...

Food for thought, up to you.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point, I'll make this change

Copy link
Collaborator

@Irev-Dev Irev-Dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick bug explanation for 7c35109

Screenshare.-.2024-04-19.7_28_24.AM.mp4

Potential changes are

  • should rectangle tool be selectable without deselecting line tool? (mentioned in the vid above)
  • A suggestion about the snapshot test

But nothing I feel so strongly about that should block the merge.

Really happy with the both the UI and the code!

@franknoirot
Copy link
Collaborator Author

quick bug explanation for 7c35109

lol I had reverted that bitbecause I thought it was what you meant by unrelated code 😆. Sorry I should have tested after that change!

@franknoirot franknoirot enabled auto-merge (squash) April 19, 2024 15:55
@franknoirot franknoirot merged commit 6450622 into main Apr 19, 2024
12 checks passed
@franknoirot franknoirot deleted the add-rectangle-tool branch April 19, 2024 15:56
@franknoirot franknoirot mentioned this pull request Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sketch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add rectangle from corner tool
2 participants