Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting custom camera #21

Open
barrybecker4 opened this issue Feb 19, 2024 · 1 comment
Open

allow setting custom camera #21

barrybecker4 opened this issue Feb 19, 2024 · 1 comment

Comments

@barrybecker4
Copy link

I noticed that edges are only found if you click right in the middle of them.
I really would like to be able to click (or mouse over) anywhere on the edge in order to find it.
As a workaround, I would like to set my own custom camera that has my own implementation of edgeContains.
Unfortunately, this does not seem possible because there is not setCamera method on the defaultView, only getCamera.
I would like to be able to do this:

viewer.getDefaultView.setCamera(customCamera)
@barrybecker4
Copy link
Author

I was able to work around this locally by creating a custom SwingViewer.
It would be nice to update the default Camera so that it does a better calculation to determine if the mouse is over an edge.

I did work around it locally. Here is my custom camera. I would make a PR, but this project no longer seems active.

package com.barrybecker4.discreteoptimization.common.graph.visualization

import org.graphstream.ui.geom.Point3
import org.graphstream.ui.graphicGraph.stylesheet.{Style, Values}
import org.graphstream.ui.graphicGraph.{GraphicEdge, GraphicElement, GraphicGraph, GraphicNode}
import org.graphstream.ui.view.camera.DefaultCamera2D

import java.awt.geom.Point2D

class GraphCamera2D(graphicGraph: GraphicGraph) extends DefaultCamera2D(graphicGraph) {

  /**
   *   Override the edgeContains method to customize its behavior.
   *   We would like to see if the x, y passed in are on the line connecting the nodes
   */
  override def edgeContains(elt: GraphicElement, x: Double, y: Double): Boolean = {

    val edge = elt.asInstanceOf[GraphicEdge]
    val node1 = edge.from
    val node2 = edge.to
    val pt1 = bck.transform(node1.getX, node1.getY, 0) //Point2D.Double(node1.getX, node1. getY)
    val pt2 = bck.transform(node2.getX, node2.getY, 0) //Point2D.Double(node2.getX, node2. getY)

    val distThresh = Math.max(2.0, elt.getStyle.getSize.size().toDouble)

    isPointNearLineSegment(pt1, pt2, Point3(x, y, 0), distThresh)
  }

  def isPointNearLineSegment(pt1: Point3, pt2: Point3, testPoint: Point3, distance: Double): Boolean = {
    val length = pt1.distance(pt2)
    val lengthSquared = length * length

    if (lengthSquared == 0.0) {
      // Points are the same, check if the test point coincides with them
      return pt1.distance(testPoint) <= distance
    }

    // Calculate the dot product of the vectors from pt1 to testPoint and pt1 to pt2
    val deltaX = pt2.x - pt1.x
    val deltaY = pt2.y - pt1.y
    val dotProduct = ((testPoint.x - pt1.x) * deltaX + (testPoint.y - pt1.y) * deltaY) / lengthSquared

    // Check if the projection falls within the segment
    if (dotProduct < 0.0 || dotProduct > 1.0) {
      return false
    }

    // Calculate the coordinates of the projection on the line
    val projectionX = pt1.x + dotProduct * (pt2.x - pt1.x)
    val projectionY = pt1.y + dotProduct * (pt2.y - pt1.y)

    // Check if the distance from the test point to the projection is within the specified threshold
    testPoint.distance(projectionX, projectionY, 0) <= distance
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant