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

If Layer BoundingBoxes intersect, Use Intersection Center for all Layer Thumbnails #6411

Merged
merged 3 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.released
- The default name for volume annotation layers with fallback segmentations is now set to the name of their fallback segmentation layer, no longer “Volume”. [#6373](https://github.com/scalableminds/webknossos/pull/6373)
- The “reload data” button for dataset and annotation layers is now only shown to users who have administrative permissions for the dataset (because the button clears server caches and file handles). [#6380](https://github.com/scalableminds/webknossos/pull/6380)
- Requests for missing chunks in zarr datasets now return status code 404 instead of 200 with an empty response. [#6381](https://github.com/scalableminds/webknossos/pull/6381)
- Dataset layer thumbnails now by default show the center of the intersection of all layer bounding boxes, so all layer thumbnails show the same region. [6411](https://github.com/scalableminds/webknossos/pull/6411)

### Fixed
- Fixed a regression where the mapping activation confirmation dialog was never shown. [#6346](https://github.com/scalableminds/webknossos/pull/6346)
Expand Down
22 changes: 14 additions & 8 deletions app/controllers/DataSetController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package controllers

import com.mohiva.play.silhouette.api.Silhouette
import com.scalableminds.util.accesscontext.{DBAccessContext, GlobalAccessContext}
import com.scalableminds.util.geometry.Vec3Int
import com.scalableminds.util.geometry.{BoundingBox, Vec3Int}
import com.scalableminds.util.mvc.Filter
import com.scalableminds.util.tools.{Fox, JsonHelper, Math}
import com.scalableminds.webknossos.datastore.models.datasource.{DataLayerLike, GenericDataSource}
import io.swagger.annotations._

import javax.inject.Inject
import models.analytics.{AnalyticsService, ChangeDatasetSettingsEvent, OpenDatasetEvent}
import models.binary._
Expand Down Expand Up @@ -74,17 +76,19 @@ class DataSetController @Inject()(userService: UserService,
w: Option[Int],
h: Option[Int]): Action[AnyContent] =
sil.UserAwareAction.async { implicit request =>
def imageFromCacheIfPossible(dataSet: DataSet): Fox[Array[Byte]] = {
def imageFromCacheIfPossible(dataSet: DataSet, dataSource: GenericDataSource[DataLayerLike]): Fox[Array[Byte]] = {
val width = Math.clamp(w.getOrElse(DefaultThumbnailWidth), 1, MaxThumbnailWidth)
val height = Math.clamp(h.getOrElse(DefaultThumbnailHeight), 1, MaxThumbnailHeight)
dataSetService.thumbnailCache.find(
thumbnailCacheKey(organizationName, dataSetName, dataLayerName, width, height)) match {
case Some(a) =>
Fox.successful(a)
case _ =>
val defaultCenterOpt = dataSet.adminViewConfiguration.flatMap(c =>
val configuredCenterOpt = dataSet.adminViewConfiguration.flatMap(c =>
c.get("position").flatMap(jsValue => JsonHelper.jsResultToOpt(jsValue.validate[Vec3Int])))
val defaultZoomOpt = dataSet.adminViewConfiguration.flatMap(c =>
val centerOpt = configuredCenterOpt.orElse(
BoundingBox.intersection(dataSource.dataLayers.map(_.boundingBox)).map(_.center))
val configuredZoomOpt = dataSet.adminViewConfiguration.flatMap(c =>
c.get("zoom").flatMap(jsValue => JsonHelper.jsResultToOpt(jsValue.validate[Double])))
dataSetService
.clientFor(dataSet)(GlobalAccessContext)
Expand All @@ -93,8 +97,8 @@ class DataSetController @Inject()(userService: UserService,
dataLayerName,
width,
height,
defaultZoomOpt,
defaultCenterOpt))
configuredZoomOpt,
centerOpt))
.map { result =>
// We don't want all images to expire at the same time. Therefore, we add some random variation
dataSetService.thumbnailCache.insert(
Expand All @@ -110,10 +114,12 @@ class DataSetController @Inject()(userService: UserService,
for {
dataSet <- dataSetDAO.findOneByNameAndOrganizationName(dataSetName, organizationName) ?~> notFoundMessage(
dataSetName) ~> NOT_FOUND
_ <- dataSetDataLayerDAO.findOneByNameForDataSet(dataLayerName, dataSet._id) ?~> Messages(
dataSource <- dataSetService.dataSourceFor(dataSet) ?~> "dataSource.notFound" ~> NOT_FOUND
usableDataSource <- dataSource.toUsable.toFox ?~> "dataSet.notImported"
_ <- bool2Fox(usableDataSource.dataLayers.exists(_.name == dataLayerName)) ?~> Messages(
"dataLayer.notFound",
dataLayerName) ~> NOT_FOUND
image <- imageFromCacheIfPossible(dataSet)
image <- imageFromCacheIfPossible(dataSet, usableDataSource)
} yield {
addRemoteOriginHeaders(Ok(image)).as(jpegMimeType).withHeaders(CACHE_CONTROL -> "public, max-age=86400")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,27 @@ case class BoundingBox(topLeft: Vec3Int, width: Int, height: Int, depth: Int) {
math.max(topLeft.y, other.topLeft.y) < math.min(bottomRight.y, other.bottomRight.y) &&
math.max(topLeft.z, other.topLeft.z) < math.min(bottomRight.z, other.bottomRight.z)

def combineWith(other: BoundingBox): BoundingBox = {
def intersection(other: BoundingBox): Option[BoundingBox] = {
val newTopLeft = Vec3Int(
math.max(topLeft.x, other.topLeft.x),
math.max(topLeft.y, other.topLeft.y),
math.max(topLeft.z, other.topLeft.z)
)
val newBottomRight = Vec3Int(
math.min(bottomRight.x, other.bottomRight.x),
math.min(bottomRight.y, other.bottomRight.y),
math.min(bottomRight.z, other.bottomRight.z)
)
if (newTopLeft.x < newBottomRight.x && newTopLeft.y < newBottomRight.y && newTopLeft.z < newBottomRight.z) {
Some(
BoundingBox(newTopLeft,
newBottomRight.x - newTopLeft.x,
newBottomRight.y - newTopLeft.y,
newBottomRight.z - newTopLeft.z))
} else None
jstriebel marked this conversation as resolved.
Show resolved Hide resolved
}

def union(other: BoundingBox): BoundingBox = {
val x = math.min(other.topLeft.x, topLeft.x)
val y = math.min(other.topLeft.y, topLeft.y)
val z = math.min(other.topLeft.z, topLeft.z)
Expand All @@ -32,7 +52,7 @@ case class BoundingBox(topLeft: Vec3Int, width: Int, height: Int, depth: Int) {
def scale(s: Float): BoundingBox =
BoundingBox(topLeft.scale(s), (width * s).toInt, (height * s).toInt, (depth * s).toInt)

def toSql =
def toSql: List[Int] =
List(topLeft.x, topLeft.y, topLeft.z, width, height, depth)

def volume: Long =
Expand Down Expand Up @@ -77,13 +97,23 @@ object BoundingBox {
else
None

def combine(bbs: List[BoundingBox]): BoundingBox =
def union(bbs: List[BoundingBox]): BoundingBox =
bbs match {
case head :: tail =>
tail.foldLeft(head)(_ combineWith _)
tail.foldLeft(head)(_ union _)
case _ =>
BoundingBox(Vec3Int(0, 0, 0), 0, 0, 0)
jstriebel marked this conversation as resolved.
Show resolved Hide resolved
}

def intersection(bbs: List[BoundingBox]): Option[BoundingBox] =
bbs match {
case head :: tail =>
tail.foldLeft[Option[BoundingBox]](Some(head)) { (aOpt, b) =>
aOpt.flatMap(_ intersection b)
}
case _ =>
None
jstriebel marked this conversation as resolved.
Show resolved Hide resolved
}

implicit val jsonFormat: OFormat[BoundingBox] = Json.format[BoundingBox]
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ package object datasource {
val center: Vec3Int = boundingBox.center

lazy val boundingBox: BoundingBox =
BoundingBox.combine(dataLayers.map(_.boundingBox))
BoundingBox.union(dataLayers.map(_.boundingBox))
}

object GenericDataSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait BoundingBoxMerger extends ProtoGeometryImplicits {
boundinBoxB <- boundingBoxBOpt
} yield {
com.scalableminds.util.geometry.BoundingBox
.combine(List[com.scalableminds.util.geometry.BoundingBox](boundinBoxA, boundinBoxB))
.union(List[com.scalableminds.util.geometry.BoundingBox](boundinBoxA, boundinBoxB))
}

protected def combineUserBoundingBoxes(singleBoundingBoxAOpt: Option[ProtoBoundingBox],
Expand Down