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

168
LINES

< > BotCompany Repo | #1035062 // G22MeshMapping - map one mesh to another one (bijectively mapping anchors and curves)

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

Transpiled version (11403L) is out of date.

1  
// For users of this class: Don't fill the maps directly, call
2  
// mapAnchor + mapCurve.
3  
4  
srecord noeq G22MeshMapping(G22Mesh mesh1, G22Mesh mesh2) {
5  
  delegate Anchor, Curve to G22Mesh.
6  
  
7  
  // A mapping of a curve to another curve may be "flipped"
8  
  // (start becomes end and end becomes start)
9  
  srecord MappedCurve(Curve curve, bool flipped) {
10  
    Curve get() { ret curve; }
11  
    
12  
    Anchor start() { ret curve.anchor(flipped); }
13  
    Anchor end()   { ret curve.anchor(!flipped); }
14  
  }
15  
16  
  // For both maps: keys are from mesh1, values are from mesh2
17  
  BijectiveMap<Anchor> anchorMap = new BijectiveMap(true);
18  
  
19  
  // forward (mesh1 -> mesh2) and backward (mesh2 -> mesh1)
20  
  new LinkedHashMap<Curve, MappedCurve> curveMap;
21  
  new LinkedHashMap<Curve, MappedCurve> curveBackwardMap;
22  
  
23  
  // General validity check that should cover all bases
24  
  
25  
  void validityCheck() {
26  
    if (mesh1 == mesh2)
27  
      fail("Mesh1 and mesh2 must not be the same object");
28  
      
29  
    // For anchors, we only need to check that they come from the
30  
    // right meshes. Bijectiveness is ensured by anchorMap itself.
31  
    
32  
    for (a1, a2 : anchorMap) {
33  
      assertAnchorInMesh(a1, +mesh1);
34  
      assertAnchorInMesh(a2, +mesh2);
35  
    }
36  
    
37  
    for (c1, c2 : curveMap) {
38  
      assertTrue(c1 + " is in mesh1", mesh1.containsCurve(c1));
39  
      assertTrue(c2 + " is in mesh2", mesh2.containsCurve(c2!));
40  
      
41  
      // Check compatibility with anchor mappings
42  
      
43  
      assertEquals("Start point", c2.start(), getAnchorMapping(c1.start));
44  
      assertEquals("End point", c2.end(), getAnchorMapping(c1.end));
45  
    }
46  
  }
47  
  
48  
  S validityError() {
49  
    try {
50  
      validityCheck();
51  
      null;
52  
    } catch e {
53  
      ret "Validity error: " + e.getMessage();
54  
    }
55  
  }
56  
  
57  
  bool isValid() { ret validityError() == null; }
58  
  
59  
  // A mapping is complete iff all anchors and curves from both
60  
  // meshes are covered.
61  
  
62  
  bool isComplete() {
63  
    ret allEq(l(anchorMap), l(mesh1.anchors()), l(mesh2.anchors()))
64  
      && allEq(l(curveMap), l(mesh1.curves()), l(mesh2.curves()));
65  
  }
66  
  
67  
  void assertAnchorInMesh(Anchor a, S meshName, G22Mesh mesh) {
68  
  assertTrue(a + " is in " + meshName, mesh.containsAnchor(a));
69  
  }
70  
  
71  
  AutoCloseable tempMapAnchor(Anchor a1, Anchor a2) {
72  
    assertAnchorInMesh(a1, +mesh1);
73  
    assertAnchorInMesh(a2, +mesh2);
74  
    ret tempMapPut(anchorMap, a1, a2);
75  
  }
76  
 
77  
  void mapAnchor(Anchor a1, Anchor a2) {
78  
    assertAnchorInMesh(a1, +mesh1);
79  
    assertAnchorInMesh(a2, +mesh2);
80  
    anchorMap.put(a1, a2);
81  
  }
82  
  
83  
  // Note: doesn't remove the curve mappings
84  
  void unmapAnchor(Anchor a1) {
85  
    anchorMap.remove(a1);
86  
  }
87  
  
88  
  AutoCloseable tempMapCurve(Curve c1, Curve c2, bool flipped) {
89  
    new L<AutoCloseable> closers;
90  
    
91  
    var forwardMapping = new MappedCurve(c2, flipped);
92  
    var backwardMapping = new MappedCurve(c1, flipped);
93  
    closers.add(tempMapPut(curveMap, c1, forwardMapping));
94  
    closers.add(tempMapPut(curveBackwardMap, c2, backwardMapping));
95  
    
96  
    // Automatically map the anchors
97  
    closers.add(tempMapAnchor(c1.start, forwardMapping.start());
98  
    closers.add(tempMapAnchor(c1.end, forwardMapping.end());
99  
    ret combineAutoCloseables(closers);
100  
  }
101  
  
102  
  void mapCurve(Curve c1, Curve c2, bool flipped) {
103  
    var forwardMapping = new MappedCurve(c2, flipped);
104  
    var backwardMapping = new MappedCurve(c1, flipped);
105  
    curveMap.put(c1, forwardMapping);
106  
    curveBackwardMap.put(c2, backwardMapping);
107  
    
108  
    // Automatically map the anchors
109  
    mapAnchor(c1.start, forwardMapping.start());
110  
    mapAnchor(c1.end, forwardMapping.end());
111  
  }
112  
  
113  
  // The following functions take anchors and curves from either mesh
114  
  
115  
  Anchor getAnchorMapping aka get(Anchor a) {
116  
    try object anchorMap.get(a);
117  
    ret anchorMap.inverseGet(a);
118  
  }
119  
  
120  
  MappedCurve getCurveMapping aka get(Curve c) {
121  
    try object curveMap.get(c);
122  
    ret curveBackwardMap.get(c);
123  
  }
124  
  
125  
  bool isMapped(Anchor a) {
126  
    ret get(a) != null;
127  
  }
128  
  
129  
  bool isMapped(Curve c) {
130  
    ret get(c) != null;
131  
  }
132  
  
133  
  L<Int> anchorMappingIndices() {
134  
    var idx = mapItemsToListIndex(mesh2.anchorList());
135  
    ret map(mesh1.anchorList(), a -> idx.get(get(a)));
136  
  }
137  
  
138  
  L<Int> curveMappingIndices() {
139  
    var idx = mapItemsToListIndex(mesh2.curveList());
140  
    ret map(mesh1.curveList(), a -> {
141  
      var c = get(a);
142  
      ret c == null ?: idx.get(c!);
143  
    });
144  
  }
145  
  
146  
  toString {
147  
    ret "Mapped anchors: " + anchorMappingIndices()
148  
      + ", mapped curves: " + curveMappingIndices();
149  
  }
150  
  
151  
  void drawMappedPartOfMesh1(Graphics2D g) {
152  
    for (anchor : keys(anchorMap))
153  
      new G22VisualizeMeshes().drawAnchor(g, anchor.pt);
154  
    for (curve : keys(curveMap))
155  
      new G22VisualizeMeshes().drawCurve(g, curve);
156  
  }
157  
  
158  
  void drawMappedPartOfMesh2(Graphics2D g) {
159  
    for (anchor : values(anchorMap))
160  
      new G22VisualizeMeshes().drawAnchor(g, anchor.pt);
161  
    for (curve : values(curveMap)) {
162  
      new G22VisualizeMeshes vm;
163  
      if (curve.flipped)
164  
        vm.curveColor(Color.green);
165  
      vm.drawCurve(g, curve!);
166  
    }
167  
  }
168  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035062
Snippet name: G22MeshMapping - map one mesh to another one (bijectively mapping anchors and curves)
Eternal ID of this version: #1035062/39
Text MD5: 21f1d489130e875e86744cc54c2229d5
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-06 20:44:00
Source code size: 5170 bytes / 168 lines
Pitched / IR pitched: No / No
Views / Downloads: 153 / 357
Version history: 38 change(s)
Referenced in: [show references]