Skip to content

Commit

Permalink
larger grid size when connected with opencv, see #142
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed May 10, 2022
1 parent 9486a9e commit 7ff37b3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
10 changes: 7 additions & 3 deletions js/quadrilateral/model/QuadrilateralModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class QuadrilateralModel {
public static MAJOR_GRID_SPACING = 0.05;
public static MINOR_GRID_SPACING: number = QuadrilateralModel.MAJOR_GRID_SPACING / 4;

// When controlling with the device the vertex positions are constrained to this resolution
// of grid spacing - a bit larger than the MINOR_GRID_SPACING to prevent small motions that
// may come from noise.
public static DEVICE_GRID_SPACING: number = QuadrilateralModel.MAJOR_GRID_SPACING / 2;

constructor( shapeIdentificationEnabledProperty: BooleanProperty, tandem: Tandem ) {

// The bounds in model space. The bounds will change depending on available screen bounds so that
Expand Down Expand Up @@ -230,10 +235,9 @@ class QuadrilateralModel {

/**
* Returns the closest position in the model from the point provided that will be constrained to the minor lines
* of the model "grid".
* of the model "grid". By default it uses the following minor grid spacing but a different spacing may be necessary.
*/
public static getClosestMinorGridPosition( position: Vector2 ): Vector2 {
const interval = QuadrilateralModel.MINOR_GRID_SPACING;
public static getClosestGridPosition( position: Vector2, interval = QuadrilateralModel.MINOR_GRID_SPACING ): Vector2 {
return new Vector2( Utils.roundToInterval( position.x, interval ), Utils.roundToInterval( position.y, interval ) );
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/quadrilateral/model/QuadrilateralShapeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ class QuadrilateralShapeModel {
const scaledProposedPositions: VertexWithProposedPosition[] = proposedPositions.map( vertexWithProposedPosition => {

const virtualPosition = this.model.physicalToVirtualTransform!.modelToViewPosition( vertexWithProposedPosition.proposedPosition );
const constrainedPosition = QuadrilateralModel.getClosestMinorGridPosition( virtualPosition );
const constrainedPosition = QuadrilateralModel.getClosestGridPosition( virtualPosition, QuadrilateralModel.DEVICE_GRID_SPACING );

return {
vertex: vertexWithProposedPosition.vertex,
Expand Down
10 changes: 5 additions & 5 deletions js/quadrilateral/view/SideNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ class SideNode extends Voicing( Path, 1 ) {
const modelVertex2Position = modelPoint.plus( vectorToVertex2! );

// constrain each to the model grid
const proposedVertex1Position = QuadrilateralModel.getClosestMinorGridPosition( modelVertex1Position );
const proposedVertex2Position = QuadrilateralModel.getClosestMinorGridPosition( modelVertex2Position );
const proposedVertex1Position = QuadrilateralModel.getClosestGridPosition( modelVertex1Position );
const proposedVertex2Position = QuadrilateralModel.getClosestGridPosition( modelVertex2Position );

// only update positions if both are allowed
if ( quadrilateralModel.areVertexPositionsAllowed( side.vertex1, proposedVertex1Position, side.vertex2, proposedVertex2Position ) ) {
Expand Down Expand Up @@ -296,8 +296,8 @@ class SideNode extends Voicing( Path, 1 ) {
let proposedVertex2Position = this.side.vertex2.positionProperty.get().plus( deltaVector );

// constrain positions to the "grid" of the model
proposedVertex1Position = QuadrilateralModel.getClosestMinorGridPosition( proposedVertex1Position );
proposedVertex2Position = QuadrilateralModel.getClosestMinorGridPosition( proposedVertex2Position );
proposedVertex1Position = QuadrilateralModel.getClosestGridPosition( proposedVertex1Position );
proposedVertex2Position = QuadrilateralModel.getClosestGridPosition( proposedVertex2Position );

// if the positions are outside of model bounds, the shape is not allowed
// TODO: I am not sure how to put this in the isQuadrilateralShapeAllowed, because to set the shape
Expand Down Expand Up @@ -339,7 +339,7 @@ class SideNode extends Voicing( Path, 1 ) {
*/
private rotateVertexAroundOther( anchorVertex: Vertex, armVertex: Vertex, modelDelta: Vector2 ): void {
const modelPosition = armVertex.positionProperty.get().plus( modelDelta );
const proposedPosition = QuadrilateralModel.getClosestMinorGridPosition( modelPosition );
const proposedPosition = QuadrilateralModel.getClosestGridPosition( modelPosition );
if ( this.quadrilateralModel.isVertexPositionAllowed( armVertex, proposedPosition ) ) {
armVertex.positionProperty.value = proposedPosition;
}
Expand Down
2 changes: 1 addition & 1 deletion js/quadrilateral/view/VertexNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class VertexNode extends Voicing( Circle, 1 ) {
const modelPoint = modelViewTransform.viewToModelPosition( parentPoint );

// constrain to the allowable positions in the model along the grid
const constrainedPosition = QuadrilateralModel.getClosestMinorGridPosition( modelPoint );
const constrainedPosition = QuadrilateralModel.getClosestGridPosition( modelPoint );

if ( model.isVertexPositionAllowed( vertex, constrainedPosition ) ) {
vertex.positionProperty.value = constrainedPosition;
Expand Down

0 comments on commit 7ff37b3

Please sign in to comment.