-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from spbu-coding-2023/elbanan-retake-work
YifanHu placement
- Loading branch information
Showing
17 changed files
with
220 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/model/algorithms/placement/YifanHuPlacement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package model.algorithms.placement | ||
|
||
import model.graphs.edges.Edge | ||
import model.graphs.graphs.AbstractGraph | ||
import org.gephi.graph.api.GraphModel | ||
import org.gephi.graph.api.Node | ||
import org.gephi.layout.plugin.force.StepDisplacement | ||
import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout | ||
import kotlin.random.Random | ||
|
||
open class YifanHuPlacement<V, E : Edge>(val graph: AbstractGraph<V, E>) { | ||
fun getPlacement(width: Int, height: Int): Map<V, Pair<Float, Float>> { | ||
val random = Random(1L) | ||
|
||
val graphModel = GraphModel.Factory.newInstance() | ||
val graphType = graphModel.undirectedGraph | ||
|
||
val nodes = mutableMapOf<V, Node>() | ||
for (vertex in graph.vertices.values) { | ||
val node: Node = graphModel.factory().newNode(vertex.value.toString()) | ||
node.setX(random.nextFloat() * width) | ||
node.setY(random.nextFloat() * height) | ||
nodes[vertex.value] = node | ||
|
||
graphType.addNode(node) | ||
} | ||
|
||
for (edge in graph.edges.values) { | ||
val firstVertex = graph.getVertexValue(edge.verticesNumbers.first) | ||
val secondVertex = graph.getVertexValue(edge.verticesNumbers.second) | ||
val e: org.gephi.graph.api.Edge = graphModel.factory().newEdge( | ||
nodes[firstVertex], | ||
nodes[secondVertex], | ||
1, | ||
false | ||
) | ||
graphType.addEdge(e) | ||
} | ||
val layout = YifanHuLayout(null, StepDisplacement(1f)) | ||
layout.setGraphModel(graphModel) | ||
layout.initAlgo() | ||
layout.resetPropertiesValues() | ||
layout.optimalDistance = 300f | ||
layout.relativeStrength = 0.2f | ||
layout.initialStep = 20.0f | ||
layout.stepRatio = 0.95f | ||
layout.isAdaptiveCooling = true | ||
|
||
var i = 0 | ||
while (i < 1000 && layout.canAlgo()) { | ||
layout.goAlgo() | ||
i++ | ||
} | ||
layout.endAlgo() | ||
val placement = mutableMapOf<V, Pair<Float, Float>>() | ||
for (vertex in graph.vertices.values) { | ||
val n = graphType.getNode(vertex.value.toString()) | ||
placement[vertex.value] = Pair(n.x(), n.y()) | ||
} | ||
|
||
return placement | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package view.screens | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.unit.dp | ||
import viewModel.screensViewModels.PlacementStrategyViewModel | ||
|
||
@Composable | ||
fun placementStrategyScreen(viewModel: PlacementStrategyViewModel, onDismiss: () -> Unit) { | ||
Row( | ||
horizontalArrangement = Arrangement.spacedBy(20.dp) | ||
) { | ||
Button( | ||
onClick = { | ||
viewModel.selectYifanHu() | ||
onDismiss() | ||
}, | ||
enabled = true | ||
) { | ||
Text( | ||
text = "YifanHu" | ||
) | ||
} | ||
|
||
Button( | ||
onClick = { | ||
viewModel.selectCircular() | ||
onDismiss() | ||
}, | ||
enabled = true | ||
) { | ||
Text( | ||
text = "Circular" | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
src/main/kotlin/viewModel/graphViewModel/RepresentationStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package viewModel.graphViewModel | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import model.graphs.edges.Edge | ||
import model.graphs.graphs.AbstractGraph | ||
import viewModel.graphViewModel.edgesViewModel.EdgeViewModel | ||
|
||
interface RepresentationStrategy { | ||
fun <V> place(width: Double, height: Double, vertices: Collection<VertexViewModel<V>>) | ||
fun <V, E : Edge> place(width: Double, height: Double, graph: AbstractGraph<V, E>, vertices: Collection<VertexViewModel<V>>) | ||
fun <V> highlightVertices(vertices: Collection<VertexViewModel<V>>, color: Color) | ||
fun <V> highlightEdges(edges: Collection<EdgeViewModel<V>>, color: Color) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/viewModel/graphViewModel/YifanHuPlacementStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package viewModel.graphViewModel | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
import model.algorithms.placement.YifanHuPlacement | ||
import model.graphs.edges.Edge | ||
import model.graphs.graphs.AbstractGraph | ||
import viewModel.graphViewModel.edgesViewModel.EdgeViewModel | ||
|
||
class YifanHuPlacementStrategy : RepresentationStrategy { | ||
override fun <V, E : Edge> place( | ||
width: Double, | ||
height: Double, | ||
graph: AbstractGraph<V, E>, | ||
vertices: Collection<VertexViewModel<V>> | ||
) { | ||
val placemnt = YifanHuPlacement(graph).getPlacement(width.toInt(), height.toInt()) | ||
|
||
for (vertex in vertices) { | ||
vertex.x = placemnt[vertex.v.value]?.first?.dp ?: 0f.dp | ||
vertex.y = placemnt[vertex.v.value]?.second?.dp ?: 0f.dp | ||
} | ||
} | ||
|
||
override fun <V> highlightVertices(vertices: Collection<VertexViewModel<V>>, color: Color) { | ||
vertices | ||
.onEach { | ||
it.color = color | ||
} | ||
} | ||
|
||
override fun <V> highlightEdges(edges: Collection<EdgeViewModel<V>>, color: Color) { | ||
edges | ||
.onEach { | ||
it.color = color | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/viewModel/screensViewModels/PlacementStrategyViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package viewModel.screensViewModels | ||
|
||
import viewModel.graphViewModel.CircularPlacementStrategy | ||
import viewModel.graphViewModel.RepresentationStrategy | ||
import viewModel.graphViewModel.YifanHuPlacementStrategy | ||
|
||
class PlacementStrategyViewModel { | ||
internal var placementStrategy: RepresentationStrategy = CircularPlacementStrategy() | ||
|
||
fun selectYifanHu() { | ||
placementStrategy = YifanHuPlacementStrategy() | ||
} | ||
fun selectCircular() { | ||
placementStrategy = CircularPlacementStrategy() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters