Skip to content

Commit b695266

Browse files
committed
56_6 - working on neighbours leaving container
1 parent b91be3d commit b695266

File tree

14 files changed

+140
-118
lines changed

14 files changed

+140
-118
lines changed

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/AbstractC2CompactionStep.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,6 @@ abstract class AbstractC2CompactionStep(val cd: CompleteDisplayer) : C2Compactio
143143
f(r)
144144
}
145145

146-
147-
fun getNonDoneVersion(c2Slideable: C2Slideable): C2Slideable {
148-
return c2Slideable.getNotDoneVersion()
149-
}
150-
151146
companion object {
152147

153148
fun getPadding(d: Dimension, r: Rectangular) : Pair<Int, Int> {

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/C2Compaction.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ interface C2Compaction {
3636

3737
/**
3838
* Duplicates neighbours arriving on one slideable to the other
39+
* @param toIsOrbit set to true to link all the neighbours together the same as they are on from
3940
*/
40-
fun copyNeighbourMap(from: C2Slideable?, to: C2Slideable?)
41+
fun copyNeighbourMap(from: C2Slideable?, to: C2Slideable?, toIsOrbit: Boolean)
4142
}

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/C2CompactionImpl.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,23 @@ class C2CompactionImpl(private val diagram: Diagram) : C2Compaction {
228228
}
229229
}
230230

231-
override fun copyNeighbourMap(from: C2Slideable?, to: C2Slideable?) {
231+
override fun copyNeighbourMap(from: C2Slideable?, to: C2Slideable?, toIsOrbit: Boolean) {
232232
if ((from != null) && (to != null)) {
233233
val sets = getNeighbourSetsOn(from)
234234
sets.flatMap { it }.forEach {
235235
addNeighbour(it, from, to)
236236
}
237237

238-
sets.forEach { s ->
239-
var last: C2Slideable? = null
240-
s.forEach {
241-
if (last != null) {
242-
addNeighbour(to, last, it)
243-
}
238+
if (toIsOrbit) {
239+
sets.forEach { s ->
240+
var last: C2Slideable? = null
241+
s.forEach {
242+
if (last != null) {
243+
addNeighbour(to, last, it)
244+
}
244245

245-
last = it
246+
last = it
247+
}
246248
}
247249
}
248250
}

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/C2SlackOptimisation.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ class C2SlackOptimisation(val compaction: C2CompactionImpl, val dimension: Dimen
177177
} else if (s1 == s2) {
178178
return s1
179179
} else {
180+
val s1o = s1.getOrbitAnchors().isNotEmpty()
181+
val s1r = s1.getRectAnchors().isNotEmpty()
182+
val s2o = s2.getOrbitAnchors().isNotEmpty()
183+
val s2r = s2.getRectAnchors().isNotEmpty()
184+
if ((s1o != s2o) && (s1r != s2r)) {
185+
throw LogicException("Should only merge rects or orbits, not a combination")
186+
}
187+
180188
val sNew = s1.merge(s2)
181189
// now we need to replace s1 and s2 in their containers
182190
val containsS1 = slideableMap.remove(s1) ?: mutableSetOf()
@@ -217,9 +225,10 @@ class C2SlackOptimisation(val compaction: C2CompactionImpl, val dimension: Dimen
217225
val new = when (side) {
218226
Side.START -> {
219227
val newLeft = if (containerRoutable != null) {
220-
compaction.copyNeighbourMap(latestLeft!!, containerRoutable.bl!!)
228+
compaction.copyNeighbourMap(latestLeft!!, containerRoutable.bl!!, true)
221229
containerRoutable.bl!!
222230
} else {
231+
compaction.copyNeighbourMap(latestLeft!!, container.l, false)
223232
container.l
224233
}
225234
if (latestRight != null) {
@@ -229,9 +238,10 @@ class C2SlackOptimisation(val compaction: C2CompactionImpl, val dimension: Dimen
229238
}
230239
Side.END -> {
231240
val newRight = if (containerRoutable != null) {
232-
compaction.copyNeighbourMap(latestRight, containerRoutable.br!!)
241+
compaction.copyNeighbourMap(latestRight, containerRoutable.br!!, true)
233242
containerRoutable.br!!
234243
} else {
244+
compaction.copyNeighbourMap(latestRight, container.r, false)
235245
container.r
236246
}
237247
if (latestRight != null) {

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/C2Slideable.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ class C2Slideable(
207207
}
208208

209209
fun addRectAnchor(a: RectAnchor) {
210+
if (anchors.filterIsInstance<OrbitAnchor>().isNotEmpty()) {
211+
throw LogicException("You shouldn't have orbits and rects on the same slideable")
212+
}
210213
anchors.add(a)
211214
}
212215

@@ -234,7 +237,7 @@ class C2Slideable(
234237
}
235238

236239
fun getNotDoneVersion() : C2Slideable {
237-
return getNonDoneVersion(this)
240+
return getNonDoneVersion(this)!!
238241
}
239242

240243
override fun addMinimumForwardConstraint(to: Slideable, dist: Int) {
@@ -258,9 +261,13 @@ class C2Slideable(
258261
@Suppress("UNCHECKED_CAST") return (anchors as Set<Anchor<Any>>).toMutableSet()
259262
}
260263

261-
fun getNonDoneVersion(c2Slideable: C2Slideable): C2Slideable {
264+
fun getNonDoneVersion(c2Slideable: C2Slideable?): C2Slideable? {
265+
if (c2Slideable == null) {
266+
return null
267+
}
268+
262269
while (c2Slideable.isDone()) {
263-
return getNonDoneVersion(c2Slideable.mergedInto!!)
270+
return getNonDoneVersion(c2Slideable.mergedInto)
264271
}
265272

266273
return c2Slideable

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/hierarchy/AbstractC2BuilderCompactionStep.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ abstract class AbstractC2BuilderCompactionStep(cd: CompleteDisplayer, val gp: Gr
4848
fun ensureNoOldGridOrbitSlideables() {
4949
for ((k, v) in gridOrbitSlideables) {
5050
if (v.isDone()) {
51-
val v2 = getNonDoneVersion(v)
51+
val v2 = v.getNotDoneVersion()
5252
gridOrbitSlideables[k] = v2
5353
}
5454
}
@@ -200,6 +200,12 @@ abstract class AbstractC2BuilderCompactionStep(cd: CompleteDisplayer, val gp: Gr
200200
val ic = createOrReuseIntersectionSlideable(gridMidpoint, purpose)
201201
val bl = createOrReuseOrbitSlideable(gridMidpoint, Side.START)
202202
val br = createOrReuseOrbitSlideable(gridMidpoint, Side.END)
203+
if (bl != null) {
204+
cso.ensureMinimumDistance(bl, ic, 1)
205+
}
206+
if (br != null) {
207+
cso.ensureMinimumDistance(ic, br, 1)
208+
}
203209
val out2 = RoutableSlideableSetImpl(ic, bl, br)
204210
cso.add(g.connected as PlacementPositioned, out2)
205211
out2

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/hierarchy/AbstractC2ContainerCompactionStep.kt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,29 @@ abstract class AbstractC2ContainerCompactionStep(cd: CompleteDisplayer, val rr:
102102
dimension: Dimension,
103103
topGroup: Group) {
104104

105+
log.send("Applying container edge ${c.getID()} $s to $to")
106+
107+
// slideable we are wrapping with
108+
val outer = checkCreateElement(c, dimension, so, null, topGroup)
109+
val isGridCell = (c.getParent() as Container?)?.getLayout() == Layout.GRID
110+
val useOrbit = !isGridCell
111+
112+
// inner orbits we can merge together
105113
val allMergableRoutables = mutableSetOf<C2Slideable>()
106-
var hub : TemporaryContainerHub? = null
107114

115+
// should contain only the orbit of outer, or outer side itself if a grid
116+
val allAddedSlideables = mutableSetOf<C2Slideable>()
117+
118+
// set if we're adding sides to an empty grid square
119+
var hub : TemporaryContainerHub? = null
108120

109121
to.forEach { lg ->
110122
if (lg.connected is TemporaryContainerHub) {
111123
hub = lg.connected as TemporaryContainerHub
112124
}
113125
val routables = map[lg]!!
114-
val outer = checkCreateElement(c, dimension, so, null, topGroup)
115126
val theRSS = if (dimension == Dimension.H) routables.first else routables.second
116-
val otherRSS = if (dimension == Dimension.V) routables.first else routables.second
117127
if (theRSS != null) {
118-
val isGridCell = (c.getParent() as Container?)?.getLayout() == Layout.GRID
119-
val useOrbit = !isGridCell
120128
val padding = getPadding(c, s, dimension)
121129
val newRSS = so.addSide(outer, theRSS, s, useOrbit, padding)
122130
if (s == Side.START) {
@@ -127,24 +135,18 @@ abstract class AbstractC2ContainerCompactionStep(cd: CompleteDisplayer, val rr:
127135
if (bl != null) {
128136
allMergableRoutables.add(bl)
129137
}
138+
allAddedSlideables.add(newRSS.bl!!)
130139
} else {
131140
val br = theRSS.br?.getNotDoneVersion()
132141
if ((br != null) && (br != outer.r)) {
133-
so.ensureMinimumDistance(br!!, outer.r, padding)
142+
so.ensureMinimumDistance(br, outer.r, padding)
134143
}
135144
if (br != null) {
136-
allMergableRoutables.add(br!!)
145+
allMergableRoutables.add(br)
137146
}
147+
allAddedSlideables.add(newRSS.br!!)
138148
}
139149

140-
/*if (otherRSS?.c != null) {
141-
if (s == Side.START) {
142-
so.compaction.addNeighbour(otherRSS!!.c!!, outer.l, newRSS!!.bl)
143-
} else {
144-
so.compaction.addNeighbour(otherRSS!!.c!!, outer.r, newRSS!!.br)
145-
}
146-
}*/
147-
148150
if (dimension == Dimension.H) {
149151
map[lg] = Pair(newRSS, routables.second)
150152
} else {
@@ -158,6 +160,7 @@ abstract class AbstractC2ContainerCompactionStep(cd: CompleteDisplayer, val rr:
158160
}
159161

160162
if (c.getContainer()?.getLayout() == Layout.GRID) {
163+
// this handles merging in the temporary hub elements of a grid
161164
if (hub == null) {
162165
throw LogicException("grid cell without hub")
163166
}
@@ -174,6 +177,10 @@ abstract class AbstractC2ContainerCompactionStep(cd: CompleteDisplayer, val rr:
174177
throw LogicException("was expecting orbit slideable for grid element")
175178
}
176179
}
180+
181+
if (allAddedSlideables.size != 1) {
182+
throw LogicException("Should be one added slideable for a container")
183+
}
177184
}
178185

179186
private fun completeContainers(

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/hierarchy/C2HierarchicalCompactionStep.kt

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,72 @@ class C2HierarchicalCompactionStep(cd: CompleteDisplayer, rr: RoutableReader, g
6060
gridOrbitSlideables.keys.forEach {
6161
if (it.s == Side.START) {
6262
val counterpart = Quad(it.c, it.i-1, it.d, Side.END)
63-
val slideableA = gridOrbitSlideables[counterpart]
64-
val slideableB = gridOrbitSlideables[it]
65-
c.copyNeighbourMap(slideableB, slideableA)
66-
c.copyNeighbourMap(slideableA, slideableB)
63+
val slideableA = C2Slideable.getNonDoneVersion(gridOrbitSlideables[counterpart])
64+
val slideableB = C2Slideable.getNonDoneVersion(gridOrbitSlideables[it])
65+
c.copyNeighbourMap(slideableB, slideableA, true)
66+
c.copyNeighbourMap(slideableA, slideableB, true)
6767
} else {
6868
val counterpart = Quad(it.c, it.i+1, it.d, Side.START)
69-
val slideableA = gridOrbitSlideables[counterpart]
70-
val slideableB = gridOrbitSlideables[it]
71-
c.copyNeighbourMap(slideableB, slideableA)
72-
c.copyNeighbourMap(slideableA, slideableB)
69+
val slideableA = C2Slideable.getNonDoneVersion(gridOrbitSlideables[counterpart])
70+
val slideableB = C2Slideable.getNonDoneVersion(gridOrbitSlideables[it])
71+
c.copyNeighbourMap(slideableB, slideableA, true)
72+
c.copyNeighbourMap(slideableA, slideableB, true)
7373
}
7474

7575
}
7676
}
7777

7878
fun mergeSide(leaves: Map<Double, Set<RoutableSlideableSet>>, s: Side, so: C2SlackOptimisation) : Map<Double, C2Slideable?> {
79-
return leaves.mapValues { (k, r) ->
80-
r.map {
81-
if (s == Side.START) {
82-
it.bl
79+
80+
fun ensureOrbitSlideableSet(s: RoutableSlideableSet, side: Side) : RoutableSlideableSet {
81+
val slideable = if (side == Side.START) s.bl else s.br
82+
83+
if (slideable == null) {
84+
return s;
85+
}
86+
87+
val slideableToCheck = slideable.getNotDoneVersion()
88+
if (slideableToCheck.getRectAnchors().isNotEmpty()) {
89+
// ok, get the previous version and test again
90+
val previous = s.previous
91+
if (previous == null) {
92+
throw LogicException("Should be a wrapper slideable set")
8393
} else {
84-
it.br
94+
return ensureOrbitSlideableSet(previous, side)
8595
}
96+
} else {
97+
return s
8698
}
87-
.reduce {
88-
a, b -> so.mergeSlideables(a, b)
89-
}
9099
}
100+
101+
fun mergeSlideablesInMap(l2: Map<Double, Set<RoutableSlideableSet>>) : Map<Double, C2Slideable?> {
102+
val out = l2
103+
.mapValues { (_, r) -> r.map {
104+
if (s == Side.START) {
105+
C2Slideable.getNonDoneVersion(it.bl)
106+
} else {
107+
C2Slideable.getNonDoneVersion(it.br)
108+
}
109+
}.reduceOrNull {
110+
a, b -> so.mergeSlideables(a, b)
111+
}
112+
}
113+
return out
114+
}
115+
116+
val orbitLeaves = leaves.mapValues { (_, r) -> r.map { ensureOrbitSlideableSet(it, s) }.toSet() }
117+
val rectLeaves = leaves.mapValues { (k, v) -> v.minus(orbitLeaves[k] ?: emptySet()) }
118+
119+
val mergedOrbitLeaves = mergeSlideablesInMap(orbitLeaves)
120+
var mergedRectLeaves = mergeSlideablesInMap(rectLeaves)
121+
122+
// which to return? Choose rect if it's available as it will be the grid around the orbit.
123+
val combinedLeaves = mergedOrbitLeaves.mapValues { (k,v) ->
124+
val rv = mergedRectLeaves[k]
125+
rv ?: v
126+
}
127+
128+
return combinedLeaves
91129
}
92130

93131
fun joinSides(start: Map<Double, C2Slideable?>, end: Map<Double, C2Slideable?>, so: C2SlackOptimisation, d: Dimension) {
@@ -117,13 +155,13 @@ class C2HierarchicalCompactionStep(cd: CompleteDisplayer, rr: RoutableReader, g
117155

118156
val startRects = startR.map {
119157
val rs = so.getSlideablesFor(it as Positioned)
120-
rs!!.r
121-
}
158+
rs?.r
159+
}.filterNotNull()
122160

123161
val endRects = endR.map {
124162
val ls = so.getSlideablesFor(it as Positioned)
125-
ls!!.l
126-
}
163+
ls?.l
164+
}.filterNotNull()
127165

128166
startRects.forEach { aS ->
129167
endRects.forEach { bS ->

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/labels/C2ConnectionLabelCompactionStep.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,17 @@ class C2ConnectionLabelCompactionStep(cd: CompleteDisplayer, gp: GridPositioner)
209209

210210
if (toDo.isNotEmpty()) {
211211
val labelledEdges = toDo.map { label ->
212-
val vSlideable = getNonDoneVersion(vLabelMap[label]!!)
213-
val hSlideable = getNonDoneVersion(hLabelMap[label]!!)
212+
val vSlideable = C2Slideable.getNonDoneVersion(vLabelMap[label])!!
213+
val hSlideable = C2Slideable.getNonDoneVersion(hLabelMap[label])!!
214214
handleLabel(label, C2Point(vSlideable, hSlideable, Direction.DOWN), co, d, c)
215215
}
216216

217217
val flattenedLabelEdges = labelledEdges.flatMap { p -> listOf(p.first, p.second) }
218218

219219
val unlabelledEdges = fanEdges
220-
.map { s -> getNonDoneVersion(s) }
220+
.map { s -> C2Slideable.getNonDoneVersion(s)!! }
221221
.filter { s -> !flattenedLabelEdges.contains(s) }
222-
.map { s -> Pair(getNonDoneVersion(s), getNonDoneVersion(s)) }
222+
.map { s -> Pair(C2Slideable.getNonDoneVersion(s)!!, C2Slideable.getNonDoneVersion(s)!!) }
223223

224224
val sortedEdges = getLabelOrder(c, labelledEdges + unlabelledEdges)
225225

kite9-visualization/src/commonMain/kotlin/org/kite9/diagram/visualization/compaction2/sets/RoutableSlideableSet.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ sealed interface RoutableSlideableSet : SlideableSet<RoutableSlideableSet> {
1212
val bl: C2Slideable?
1313
val br: C2Slideable?
1414

15-
override fun getAll() : Set<C2Slideable>
16-
17-
fun mergeWithOverlap(over: RoutableSlideableSet, c2: C2SlackOptimisation) : RoutableSlideableSet
15+
// the RSS before we replaced the side
16+
val previous: RoutableSlideableSet?
1817

19-
fun mergeWithGutter(after: RoutableSlideableSet, c2: C2SlackOptimisation) : RoutableSlideableSet?
18+
override fun getAll() : Set<C2Slideable>
2019

21-
fun getBufferSlideables() : Set<C2Slideable>
2220

2321
fun replaceSide(s: C2Slideable?, side: Side) : RoutableSlideableSet
2422
}

0 commit comments

Comments
 (0)