Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

68
LINES

< > BotCompany Repo | #1035441 // G22Network - a network of objects that together create a recognition

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (53008L) is out of date.

1  
concept G22Network > ConceptWithChangeListeners {
2  
  delegate Port to G22NetworkElement.
3  
  
4  
  settableWithVar S description;
5  
  Cl<G22NetworkElement> elements = syncL();
6  
  settableWithVar int magneticDistance = 100;
7  
  
8  
  // Is the network safe to auto-calculate?
9  
  settableWithVar bool autoCalculate = true;
10  
  
11  
  toString { ret or2(description, super.toString()); }
12  
  
13  
  // return new connections made
14  
  Cl<G22NetworkCable> doMagneticConnections() {
15  
    //deleteMagneticConnections();
16  
17  
    new L<G22NetworkCable> newCables;
18  
    new L<Port> inputPorts;
19  
    new L<Port> outputPorts;
20  
    
21  
    for (element : elements)
22  
      for (port : element.ports())
23  
        (port.isOutput() ? outputPorts : inputPorts).add(port);
24  
        
25  
    for (port1 : inputPorts) {
26  
      if (port1.isConnected()) continue;
27  
      Rect bounds1 = port1.bounds();
28  
      new Lowest<G22NetworkElement.Port> best;
29  
      
30  
      for (port2 : outputPorts) {
31  
        double distance = rectDistance(bounds1, port2.bounds());
32  
        
33  
        // too far away? skip
34  
        if (distance >= magneticDistance) continue;
35  
        
36  
        // wrong type? skip
37  
        if (!isSubclassOf(port2.dataType(), port1.dataType())) continue;
38  
        
39  
        // candidate found - store with distance
40  
        best.put(port2, distance);
41  
      }
42  
      
43  
      var port2 = best!;
44  
      if (port2 != null) {
45  
        var cable = new G22NetworkCable().from(port2).to(port1);
46  
        newCables.add(cable);
47  
        cable.connect();
48  
        print("Made cable: " + cable);
49  
      }
50  
    }
51  
    
52  
    ret newCables;
53  
  }
54  
  
55  
  void deleteMagneticConnections {
56  
    for (cable : allCables())
57  
      if (cable.isAutomatic())
58  
        cable.remove();
59  
  }
60  
  
61  
  Set<G22NetworkCable> allCables() {
62  
    new Set<G22NetworkCable> set;
63  
    for (element : elements)
64  
      for (port : element.ports())
65  
        addIfNotNull(set, port.cable);
66  
    ret set;
67  
  }
68  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035441
Snippet name: G22Network - a network of objects that together create a recognition
Eternal ID of this version: #1035441/21
Text MD5: 1711970151c99b425a160ec90947fc64
Author: stefan
Category: javax / gazelle 2
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-30 17:37:27
Source code size: 1934 bytes / 68 lines
Pitched / IR pitched: No / No
Views / Downloads: 181 / 283
Version history: 20 change(s)
Referenced in: [show references]