Skip to content

Commit

Permalink
update demos, fix circuit equivalence test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcl committed Sep 14, 2024
1 parent e64cdc0 commit 7b4dfd3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
32 changes: 31 additions & 1 deletion src/Renderer/Common/CommonTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ These are types used throughout the application

module CommonTypes

module Constants =
let equalityCheckTolerance = 0.0001
let labelPosTolerance = 0.00001


open Fable.Core
open Optics
#if FABLE_COMPILER
Expand Down Expand Up @@ -372,6 +377,8 @@ type SymbolInfo = {
VScale: float option
}



let portOrder_ = Lens.create (fun c -> c.PortOrder) (fun n c -> {c with PortOrder = n})
let portOrientation_ = Lens.create (fun c -> c.PortOrientation) (fun n c -> {c with PortOrientation = n})

Expand Down Expand Up @@ -405,8 +412,31 @@ type Component = {
SymbolInfo : SymbolInfo option
}

with member this.getPort (PortId portId: PortId) =
with
member this.getPort (PortId portId: PortId) =
List.tryFind (fun (port:Port) -> port.Id = portId ) (this.InputPorts @ this.OutputPorts)

/// Equality function for components, includes all geometry except component position
member c1.isSame(c2: Component) =
c1.Id = c2.Id && c1.Type = c2.Type && c1.Label = c2.Label &&
c1.InputPorts = c2.InputPorts && c1.OutputPorts = c2.OutputPorts &&
match c1.SymbolInfo, c2.SymbolInfo with
| Some s1, Some s2->
let dx = c1.X - c2.X
let dy = c1.Y - c2.Y
// check if label positions are equal
let labelPosEq =
match s1.LabelBoundingBox, s2.LabelBoundingBox with
| Some l1, Some l2 -> (l1.TopLeft.X - l2.TopLeft.X - dx)**2. + (l1.TopLeft.Y - l2.TopLeft.Y - dy)**2. < Constants.labelPosTolerance
| None, None -> true
| _ -> false
s1.HScale = s2.HScale && s1.VScale = s2.VScale && s1.LabelRotation = s2.LabelRotation &&
s1.PortOrder = s2.PortOrder &&
s1.PortOrientation = s2.PortOrientation && s1.ReversedInputPorts = s2.ReversedInputPorts &&
labelPosEq
| None, None -> true
| _ -> false



let type_ = Lens.create (fun c -> c.Type) (fun n c -> {c with Type = n})
Expand Down
33 changes: 17 additions & 16 deletions src/Renderer/Simulator/CanvasExtractor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -169,33 +169,34 @@ let compareConns tolerance conns1 conns2 =
false
| _ -> true



/// Are two lists of components identical
let compareComps tolerance comps1 comps2 =
let isClose a b = float ((a - b) * (a - b)) < tolerance
let compIdA (comps: Component List) = comps |> sortQBy (fun comp -> comp.Id)
let compsA1 = compIdA comps1
let compsA2 = compIdA comps2
let byId (comps: Component List) = comps |> sortQBy (fun comp -> comp.Id)

match byId comps1, byId comps2 with
| [], [] -> true
| l1, l2 when l1.Length <> l2.Length -> false
| (c1 :: _) as c1L, ((c2 :: _) as c2L) ->
// if whole ckt has been translated this will be the offset.
let dx = c1.X - c2.X
let dy = c1.Y - c2.Y
let isClose c1 c2 = (c1.X - c2.X - dx)**2 + (c1.Y - c2.Y - dy)**2 < tolerance
List.forall2 (fun (c1: Component) (c2: Component) -> isClose c1 c2 && (c1.isSame c2)) c1L c2L
| _ -> false // NB this cannot happen

compsA1.Length = compsA2.Length
&& List.forall2 (fun (c1: Component) (c2: Component) -> isClose c1.X c2.X && isClose c1.Y c2.Y) compsA1 compsA2

/// Robust comparison of two schematics. Tolerance determines how similar
/// counts as equal.
/// cannot use equality because float vertices may not be identical
/// use to detemine whether schematic needs to be saved
/// NB for electrical circuit comparison use extractReducedState.
let compareCanvas (tolerance: float) ((comps1, conns1): CanvasState) ((comps2, conns2): CanvasState) =
let reduce (comps: Component list) = comps |> sortQBy (fun comp -> comp.Id)
let compsOk = reduce comps1 = reduce comps2
let compsSamePos = compareComps tolerance comps1 comps2
let compsOk = compareComps tolerance comps1 comps2
let connsOk = compareConns tolerance conns1 conns2
let comparesEqual = compsOk && compsSamePos && connsOk
//if not comparesEqual then
//printf "%s" $"comps:{compsOk}, compsSamePos:{compsSamePos}, connsOk:{connsOk}"
comparesEqual

let comparesEqual = compsOk && connsOk
if not comparesEqual then
printf "%s" $"comps:{compsOk}, connsOk:{connsOk}" //>
comparesEqual

/// Compare the name and IOs of two sheets as loadedcomponents
/// For backups, if these change something major has happened
Expand Down
6 changes: 3 additions & 3 deletions src/Renderer/UI/UIPopups.fs
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ let viewWaveInfoPopup dispatch feature =
li [] [ str "Hover mouse over a waveform name in the viewer to see it highlighted on the current schematic sheet."]
li [] [ str "Change schematic sheet to view or alter components on subsheets."]
li [] [ str "Drag names to reorder waveforms, use delete icon to delete, use "; bSpan "Select Waves"; str " to add or delete."]
li [] [ str "Scroll or use " ; bSpan "scrollbar arrows" ; str " and "; DiagramStyle.zoomOutSVG; DiagramStyle.zoomInSVG;
li [] [ str "Scroll or use " ; bSpan "Scrollbar arrows" ; str " and "; DiagramStyle.zoomOutSVG; DiagramStyle.zoomInSVG;
str " controls to show which cycles to display."]
li [] [ str "The "; bSpan "cursor cycle"; str " is colored and can be moved by "; bSpan "clicking on the waveforms,";
str " altering the number in the "; bSpan "cursor box,"; bSpan " clicking box arrows,"; str " or using the "; bSpan "Left and Right Arrow Keys";
li [] [ str "Move the coloured"; bSpan " cursor clock cycle"; str " using "; bSpan "a click on the waveforms,";
str " the "; bSpan "cursor box number,"; bSpan " box controls,"; str " or "; bSpan "Left/Right Arrow Keys";
str " when the mouse is on the righthand side of the grey divider." ]
li [] [ str "The column to the right of the waveforms shows signal values in the cursor cycle" ]
li [] [ str "Drag the"; bSpan " grey vertical divider "; str "to alter the screen space used by waveforms"]
Expand Down
2 changes: 1 addition & 1 deletion static/demos/5eratosthenes/datapath.dgm

Large diffs are not rendered by default.

Loading

0 comments on commit 7b4dfd3

Please sign in to comment.