Skip to content

Commit

Permalink
ad camera pose interface #257 metadata now being
Browse files Browse the repository at this point in the history
written with every new bookmark (not every frame)
  • Loading branch information
RebeccaNowak committed Jan 25, 2023
1 parent 4a8a0a3 commit 6d0adcb
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/PRo3D.Core/SequencedBookmarks/BookmarkUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module BookmarkUtils =
{
version = SequencedBookmarkModel.current
bookmark = bookmark
metadata = None
sceneState = Some sceneState
frustumParameters = None
poseDataPath = None
Expand Down
38 changes: 31 additions & 7 deletions src/PRo3D.Core/SequencedBookmarks/SequencedBookmarks-Model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open PRo3D.Base
open PRo3D.Core

open Chiron
open System.Text.Json
open Aether
open Aether.Operators

Expand Down Expand Up @@ -136,7 +137,13 @@ type FrustumParameters = {
fieldOfView : float
nearplane : float
farplane : float
}
} with
member this.perspective =
Aardvark.Rendering.Frustum.perspective
this.fieldOfView
this.nearplane
this.farplane
((float this.resolution.X) / (float this.resolution.Y))

module FrustumParameters =
let dummyData id =
Expand Down Expand Up @@ -179,7 +186,7 @@ type SequencedBookmarkModel = {
[<NonAdaptive>]
version : int
bookmark : Bookmark

metadata : option<string> // TODO RNO refactor
frustumParameters : option<FrustumParameters>
poseDataPath : option<string>

Expand Down Expand Up @@ -250,6 +257,7 @@ module SequencedBookmarkModel =
{
version = current
bookmark = bookmark
metadata = None
frustumParameters = None
poseDataPath = None
sceneState = None
Expand All @@ -259,10 +267,11 @@ module SequencedBookmarkModel =
}

let init' bookmark sceneState frustumParameters
poseDataPath =
poseDataPath metadata =
{
version = current
bookmark = bookmark
metadata = metadata
frustumParameters = frustumParameters
poseDataPath = poseDataPath
sceneState = sceneState
Expand All @@ -275,24 +284,26 @@ module SequencedBookmarkModel =
json {
let! bookmark = Json.read "bookmark"
let! frustumParameters = Json.tryRead "frustumParameters"
let! poseDataPath = Json.tryRead "poseDataPath"
let! sceneState = Json.read "sceneState"

let! delay = Json.read "delay"
let! duration = Json.read "duration"
let! metadata = Json.tryRead "metadata"

return {
version = 0
bookmark = bookmark
frustumParameters = frustumParameters
poseDataPath = None
metadata = metadata
poseDataPath = poseDataPath
sceneState = sceneState
delay = SequencedBookmarkDefaults.initDelay delay
duration = SequencedBookmarkDefaults.initDuration duration
basePath = None
}
}


type SequencedBookmarkModel with
static member FromJson( _ : SequencedBookmarkModel) =
json {
Expand All @@ -312,6 +323,12 @@ type SequencedBookmarkModel with
do! Json.write "sceneState" x.sceneState
do! Json.write "delay" x.delay.value
do! Json.write "duration" x.duration.value
if x.poseDataPath.IsSome then
do! Json.write "poseDataPath" x.poseDataPath
if x.frustumParameters.IsSome then
do! Json.write "frustumParameters" x.frustumParameters
if x.metadata.IsSome then
do! Json.write "metadata" x.metadata
}

[<ModelType>]
Expand Down Expand Up @@ -513,6 +530,7 @@ type AnimationTimeStep =
type SequencedBookmarks = {
version : int
bookmarks : HashMap<Guid,SequencedBookmark>
poseDataPath : option<string>
/// currently not in use, could be used to save and resotre a certain state independantly of bookmarks
savedSceneState : Option<SceneState>
orderList : List<Guid>
Expand Down Expand Up @@ -599,6 +617,7 @@ module SequencedBookmarks =
let! orderList = Json.read "orderList"
let! selected = Json.read "selectedBookmark"
let! generateOnStop = Json.tryRead "generateOnStop"
let! poseDataPath = Json.tryRead "poseDataPath"
let generateOnStop =
match generateOnStop with
| Some g -> g
Expand Down Expand Up @@ -647,6 +666,7 @@ module SequencedBookmarks =
{
version = current
bookmarks = bookmarks
poseDataPath = poseDataPath
savedSceneState = sceneState
orderList = orderList
selectedBookmark = selected
Expand Down Expand Up @@ -683,7 +703,7 @@ module SequencedBookmarks =
<| SequencedBookmarkModel.init a))
|> HashMap.ofList
| _,_ -> HashMap.empty

let! poseDataPath = Json.read "poseDataPath"
let! orderList = Json.read "orderList"
let! selected = Json.read "selectedBookmark"
let! generateOnStop = Json.tryRead "generateOnStop"
Expand Down Expand Up @@ -735,6 +755,7 @@ module SequencedBookmarks =
{
version = current
bookmarks = bookmarks
poseDataPath = poseDataPath
savedSceneState = sceneState
orderList = orderList
selectedBookmark = selected
Expand Down Expand Up @@ -763,7 +784,8 @@ module SequencedBookmarks =
{
version = current
bookmarks = HashMap.Empty
savedSceneState = None
poseDataPath = None
savedSceneState = None
orderList = List.empty
selectedBookmark = None
snapshotThreads = ThreadPool.Empty
Expand Down Expand Up @@ -805,6 +827,8 @@ type SequencedBookmarks with
|> HashMap.toList
|> List.map snd)
do! Json.write "orderList" x.orderList
if x.poseDataPath.IsSome then
do! Json.write "poseDataPath" x.poseDataPath
do! Json.write "selectedBookmark" x.selectedBookmark
if x.savedSceneState.IsSome then
do! Json.write "originalSceneState" x.savedSceneState.Value
Expand Down
121 changes: 110 additions & 11 deletions src/PRo3D.SimulatedViews/Snapshots/PoseData-Model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ open Aardvark.Rendering

open Adaptify
open Chiron
open System.Text.Json
open System.Text.Json.Serialization
open System.Text.Json.Nodes
open PRo3D.Base
open PRo3D.Core

open PRo3D.Core.SequencedBookmarks
open System.Text.Json.Nodes

type PoseId = string

Expand Down Expand Up @@ -438,11 +440,90 @@ module PoseData =
cameraDefinitions = PoseCameraDefinition.dummyData
renderingSettings = PoseRenderingSettings.dummyData
layerDefinitions = PoseLayerDefinition.dummyData // not in use yet, for future extension
layoutDefinitions = PoseLayoutDefinition.dummyData // not in use yet, for future extension
layoutDefinitions = PoseLayoutDefinition.dummyData // not in use yet, for future extension
}

let toSequencedBookmarks (m : PoseData) (sceneState : SceneState) =
seq {
type MetadataBuilder =
{
poseKey : string
fromView : SnapshotCamera -> string
}

let toMetadataWithView (view : CameraView)
(metadata : list<JsonProperty>) =
let view : SnapshotCamera =
{
location = view.Location
forward = view.Forward
up = view.Up
}
let stream = new System.IO.MemoryStream ()
let jsonWriter = new Utf8JsonWriter (stream, JsonWriterOptions ())
jsonWriter.WriteStartObject ("poseData")
for item in metadata do
let str = JsonSerializer.Serialize(item)
jsonWriter.WriteRawValue str
let viewStr =
(Json.serialize view)
|> Json.formatWith JsonFormattingOptions.SingleLine
jsonWriter.WriteRawValue viewStr
jsonWriter.WriteEndObject ()
jsonWriter.Flush ()
let reader = new System.IO.StreamReader ( stream )
let text = reader.ReadToEnd()
jsonWriter.Dispose ()
stream.Dispose ()
reader.Dispose ()
text

let toSequencedBookmarks (m : PoseData) (sceneState : SceneState) =
let text = System.IO.File.ReadAllText(m.path)
let doc = JsonDocument.Parse (text)
let ok, poses = doc.RootElement.TryGetProperty ("poses") //TODO RNO deal with fail
//let test = JsonNode.Parse text
//let poses = test["poses"]
//let jsonObj = new JsonObject ();
//for pose in poses.AsArray () do
// pose.

let jsonArray = JsonArray ()
//let allMetadata = // TODO RNO refactor
// let metadata =
// [
// for pose in poses.EnumerateArray () do
// let ok, keyProp = pose.TryGetProperty ("key")
// let key = keyProp.GetString ()
// yield key, [
// for item in pose.EnumerateObject () do
// if item.Name = "view" then
// () // let ok, view = pose.TryGetProperty ("view")
// else
// yield item
// ]
// ]

// metadata

let allMetadata =
[
for pose in poses.EnumerateArray () do
let text = pose.GetRawText()
let ok, keyProp = pose.TryGetProperty ("key")
let key = keyProp.GetString ()
//let stream = new System.IO.MemoryStream ()
//let jsonWriter = new Utf8JsonWriter (stream)

//jsonWriter.WriteStartObject();
//pose.WriteTo(jsonWriter)
//jsonWriter.WriteEndObject();
//jsonWriter.Flush ()
//let reader = new System.IO.StreamReader ( stream )
//let text = reader.ReadToEnd()
yield key, text
]


[
for pose in m.poses do
let camPose = List.tryFind
(fun (x : PoseCameraDefinition) -> x.cameraId = pose.cameraId)
Expand Down Expand Up @@ -472,14 +553,18 @@ module PoseData =

let sceneState =
Some sceneState


let metadata =
allMetadata
|> List.tryFind (fun (key, x) -> key = pose.key)
|> Option.map snd
//|> Option.map (toMetadataWithView bookmark.cameraView)

yield (SequencedBookmarkModel.init'
bookmark sceneState frustumParameters (Some m.path))
bookmark sceneState frustumParameters (Some m.path)) metadata
|> SequencedBookmark.LoadedBookmark

} |> List.ofSeq


]

type PoseData with
static member FromJson( _ : PoseData) =
Expand All @@ -500,5 +585,19 @@ type PoseData with
do! Json.write "cameraDefinitions" x.cameraDefinitions
do! Json.write "renderingSettings" x.renderingSettings
do! Json.write "layerDefinitions" x.layerDefinitions
do! Json.write "layoutDefinitions" x.layoutDefinitions
}
do! Json.write "layoutDefinitions" x.layoutDefinitions
}

static member writeDummyData path =
PoseData.dummyData
|> Json.serialize
|> Json.formatWith JsonFormattingOptions.Pretty
|> Serialization.Chiron.writeToFile path

static member read path =
let poseData : PoseData =
path
|> Serialization.readFromFile
|> Json.parse
|> Json.deserialize
{poseData with path = path}
2 changes: 1 addition & 1 deletion src/PRo3D.SimulatedViews/Snapshots/Snapshot-Model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ with
/// updates between bookmarks
type BookmarkTransformation =
| Bookmark of SequencedBookmarks.SequencedBookmarkModel
| Camera of SnapshotCamera
| Camera of SnapshotCamera
with
static member ToJson x =
match x with
Expand Down
36 changes: 28 additions & 8 deletions src/PRo3D.SimulatedViews/Snapshots/SnapshotAnimation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,27 @@ module SnapshotAnimation =
(fieldOfView : float)
(nearPlane : float)
(farPlane : float) =
let frustum =
Frustum.perspective fieldOfView nearPlane farPlane
(float(bm.resolutionX.value)/float(bm.resolutionY.value))
let defaultFrustum () =
{
resolution = V2i(bm.resolutionX.value, bm.resolutionY.value)
fieldOfView = fieldOfView
nearplane = nearPlane
farplane = farPlane
}

let frustum =
let bookmarks = BookmarkUtils.orderedLoadedBookmarks bm
// currently using frustum parameters of first bookmark
// could be extended to allow changing frustum parameters for each bookmark
match List.tryHead bookmarks with
| Some first ->
let frustumParas = first.frustumParameters
match frustumParas with
| Some frustumParas ->
frustumParas
| None -> defaultFrustum ()
| None -> defaultFrustum ()

if bm.orderList.Length > 0 then
let snapshots =
match bm.fpsSetting with
Expand All @@ -225,14 +243,16 @@ module SnapshotAnimation =
let snapshotAnimation : BookmarkSnapshotAnimation =
{
snapshots = snapshots
fieldOfView = Some (frustum |> Frustum.horizontalFieldOfViewInDegrees)
resolution = V2i (bm.resolutionX.value, bm.resolutionY.value)
nearplane = nearPlane
farplane = farPlane
fieldOfView = Some (frustum.perspective
|> Frustum.horizontalFieldOfViewInDegrees)
resolution = frustum.resolution
nearplane = frustum.nearplane
farplane = frustum.farplane
}
snapshotAnimation |> SnapshotAnimation.BookmarkAnimation
else
currentFrameToAnimation bm cameraView frustum nearPlane farPlane
currentFrameToAnimation bm cameraView frustum.perspective
frustum.nearplane frustum.farplane

let readTestAnimation () =
try
Expand Down
2 changes: 1 addition & 1 deletion src/PRo3D.SimulatedViews/Snapshots/SnapshotApp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module SnapshotApp =
let snapshot = a.snapshots.[i]
let fullPathName = Path.combine [app.outputFolder;snapshot.filename]
let actions = (app.getSnapshotActions (Snapshot.Bookmark snapshot) NearFarRecalculation.NoRecalculation fullPathName)
if app.verbose then Log.line "[Snapshots] Updating parameters for next frame."
if app.verbose then Log.line "[Snapshots] BookmarkAnimation: Updating parameters for next frame."
app.mutableApp.updateSync (Guid.NewGuid ()) actions

renderAndSave (sprintf "%s.png" fullPathName) app.verbose parameters
Expand Down
Loading

0 comments on commit 6d0adcb

Please sign in to comment.