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

MAGN-9744 Studio 1.0.0.775 partially renders graphics on first try #6356

Merged
merged 1 commit into from
Apr 7, 2016
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
124 changes: 71 additions & 53 deletions src/DynamoCoreWpf/ViewModels/Watch3D/HelixWatch3DViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public class HelixWatch3DViewModel : DefaultWatch3DViewModel
internal const double DefaultFarClipDistance = 100000;
internal static BoundingBox DefaultBounds = new BoundingBox(new Vector3(-25f, -25f, -25f), new Vector3(25f,25f,25f));

private List<Model3D> sceneItems;

#if DEBUG
private readonly Stopwatch renderTimer = new Stopwatch();
#endif
Expand All @@ -132,6 +134,9 @@ public class HelixWatch3DViewModel : DefaultWatch3DViewModel

public Object Model3DDictionaryMutex = new object();
private Dictionary<string, Model3D> model3DDictionary = new Dictionary<string, Model3D>();
// Dictionary<nodeId, List<Tuple<nodeArrayItemId, labelPosition>>>
private readonly Dictionary<string, List<Tuple<string, Vector3>>> labelPlaces
= new Dictionary<string, List<Tuple<string, Vector3>>>();

public event Action RequestViewRefresh;
protected void OnRequestViewRefresh()
Expand Down Expand Up @@ -345,18 +350,33 @@ public RoutedCommand LeftClickCommand

public bool IsResizable { get; protected set; }

private void UpdateSceneItems()
{
if (Model3DDictionary == null)
{
sceneItems = new List<Model3D>();
return;
}

var values = Model3DDictionary.Values.ToList();
if (Camera != null)
{
values.Sort(new Model3DComparer(Camera.Position));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reasons Camera is null in tests when running in self service, after adding this null check tests passed successfully:
image


sceneItems = values;
}

public IEnumerable<Model3D> SceneItems
{
get
{
if (Model3DDictionary == null)
if (sceneItems == null)
{
return new List<Model3D>();
UpdateSceneItems();
}

var values = Model3DDictionary.Values.ToList();
values.Sort(new Model3DComparer(Camera.Position));
return values;
return sceneItems;
}
}

Expand Down Expand Up @@ -524,6 +544,8 @@ protected override void OnClear()
model.Detach();
Model3DDictionary.Remove(key);
}

labelPlaces.Clear();
}

OnSceneItemsChanged();
Expand Down Expand Up @@ -690,14 +712,16 @@ private void DeleteGeometries(KeyValuePair<string, Model3D>[] geometryModels, bo
// check if the geometry is frozen. if the gemoetry is frozen
// then do not detach from UI.
var frozenModel = AttachedProperties.GetIsFrozen(model3D);
if (!frozenModel)
if (frozenModel) continue;

if (model3D != null)
{
if (model3D != null)
{
model3D.Detach();
}
Model3DDictionary.Remove(kvp.Key);
model3D.Detach();
}

Model3DDictionary.Remove(kvp.Key);
var nodePath = kvp.Key.Split(':')[0];
labelPlaces.Remove(nodePath);
}
}

Expand Down Expand Up @@ -858,6 +882,7 @@ internal void ComputeFrameUpdate()

private void OnSceneItemsChanged()
{
UpdateSceneItems();
RaisePropertyChanged("SceneItems");
OnRequestViewRefresh();
}
Expand All @@ -868,11 +893,8 @@ private KeyValuePair<string, Model3D>[] FindAllGeometryModel3DsForNode(NodeModel

lock (Model3DDictionaryMutex)
{
geometryModels =
Model3DDictionary
.Where(x => x.Key.Contains(node.AstIdentifierGuid))
.Where(x => x.Value is GeometryModel3D)
.Select(x => x).ToArray();
geometryModels = Model3DDictionary
.Where(x => x.Key.Contains(node.AstIdentifierGuid) && x.Value is GeometryModel3D).ToArray();
}

return geometryModels;
Expand All @@ -884,11 +906,8 @@ private KeyValuePair<string, Model3D>[] FindAllGeometryModel3DsForNode(string id

lock (Model3DDictionaryMutex)
{
geometryModels =
Model3DDictionary
.Where(x => x.Key.Contains(identifier))
.Where(x => x.Value is GeometryModel3D)
.Select(x => x).ToArray();
geometryModels = Model3DDictionary
.Where(x => x.Key.Contains(identifier) && x.Value is GeometryModel3D).ToArray();
}

return geometryModels;
Expand Down Expand Up @@ -1084,6 +1103,8 @@ private void InitializeHelix()
{
Model3DDictionary.Add(DefaultAxesName, axesModel3D);
}

UpdateSceneItems();
}

/// <summary>
Expand Down Expand Up @@ -1252,12 +1273,12 @@ public override void AddLabelForPath(string path)

// it may be requested an array of items to put labels
// for example, the first item in 2-dim array - path will look like var_guid:0
// and it will select var_guid:0:0, var_guid:0:1, var_guid:0:2 and so on
var nodeLabelKeys = Model3DDictionary.Keys
.Where(k => k.StartsWith(path + ':') && Model3DDictionary[k] is GeometryModel3D).ToList();

// if there is a geometry to add label
if (nodeLabelKeys.Any())
// and it will select var_guid:0:0, var_guid:0:1, var_guid:0:2 and so on.
// if there is a geometry to add label(s)
List<Tuple<string, Vector3>> requestedLabelPlaces;
if (labelPlaces.ContainsKey(nodePath) &&
(requestedLabelPlaces = labelPlaces[nodePath]
.Where(pair => pair.Item1.StartsWith(path)).ToList()).Any())
{
// second, add requested labels
var textGeometry = HelixRenderPackage.InitText3D();
Expand All @@ -1266,17 +1287,15 @@ public override void AddLabelForPath(string path)
Geometry = textGeometry
};

foreach (var key in nodeLabelKeys)
foreach (var id_position in requestedLabelPlaces)
{
var geom = (GeometryModel3D)Model3DDictionary[key];
var id = key.Remove(key.LastIndexOf(':'));
var text = HelixRenderPackage.CleanTag(id.Remove(0, nodePath.Length));
var textPosition = geom.Geometry.Positions[0] + defaultLabelOffset;
var text = HelixRenderPackage.CleanTag(id_position.Item1);
var textPosition = id_position.Item2 + defaultLabelOffset;
var textInfo = new TextInfo(text, textPosition);
textGeometry.TextInfo.Add(textInfo);
}

Model3DDictionary.Add(nodePath + TextKey, bbText);
Model3DDictionary.Add(labelName, bbText);
sceneItemsChanged = true;
AttachAllGeometryModel3DToRenderHost();
}
Expand Down Expand Up @@ -1336,7 +1355,7 @@ private void AggregateRenderPackages(IEnumerable<HelixRenderPackage> packages)
var p = rp.Points;
if (p.Positions.Any())
{
id = rp.Description + PointsKey;
id = baseId + PointsKey;

PointGeometryModel3D pointGeometry3D;

Expand Down Expand Up @@ -1366,21 +1385,16 @@ private void AggregateRenderPackages(IEnumerable<HelixRenderPackage> packages)
: Enumerable.Repeat(defaultPointColor, points.Positions.Count));
points.Indices.AddRange(p.Indices.Select(i => i + startIdx));
}


if (rp.DisplayLabels)
{
CreateOrUpdateText(baseId, p.Positions[0], rp);
}

AddLabelPlace(baseId, p.Positions[0], rp);
pointGeometry3D.Geometry = points;
pointGeometry3D.Name = baseId;
}

var l = rp.Lines;
if (l.Positions.Any())
{
id = rp.Description + LinesKey;
id = baseId + LinesKey;

LineGeometryModel3D lineGeometry3D;

Expand Down Expand Up @@ -1415,12 +1429,7 @@ private void AggregateRenderPackages(IEnumerable<HelixRenderPackage> packages)
? l.Indices.Select(i => i + startIdx)
: Enumerable.Range(startIdx, startIdx + l.Positions.Count));

if (rp.DisplayLabels)
{
var pt = lineSet.Positions[startIdx];
CreateOrUpdateText(baseId, pt, rp);
}

AddLabelPlace(baseId, lineSet.Positions[startIdx], rp);
lineGeometry3D.Geometry = lineSet;
lineGeometry3D.Name = baseId;
}
Expand Down Expand Up @@ -1471,12 +1480,7 @@ private void AggregateRenderPackages(IEnumerable<HelixRenderPackage> packages)
meshGeometry3D.SetValue(AttachedProperties.HasTransparencyProperty, true);
}

if (rp.DisplayLabels)
{
var pt = mesh.Positions[idxCount];
CreateOrUpdateText(baseId, pt, rp);
}

AddLabelPlace(baseId, mesh.Positions[idxCount], rp);
meshGeometry3D.Geometry = mesh;
meshGeometry3D.Name = baseId;
}
Expand All @@ -1485,6 +1489,20 @@ private void AggregateRenderPackages(IEnumerable<HelixRenderPackage> packages)
}
}

private void AddLabelPlace(string nodeId, Vector3 pos, IRenderPackage rp)
{
if (!labelPlaces.ContainsKey(nodeId))
{
labelPlaces[nodeId] = new List<Tuple<string, Vector3>>();
}

labelPlaces[nodeId].Add(new Tuple<string, Vector3>(rp.Description, pos));
if (rp.DisplayLabels)
{
CreateOrUpdateText(nodeId, pos, rp);
}
}

/// <summary>
/// Updates or replaces the GeometryModel3D for special IRenderPackage. Special
/// IRenderPackage has a Description field that starts with a string value defined
Expand Down
6 changes: 3 additions & 3 deletions src/VisualizationTests/HelixWatch3DViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ public void Node_RenderingUpToDate()
p1.UpdateValue(new UpdateValueParams("IsVisible", "false"));

Assert.True(BackgroundPreviewGeometry.HasNumberOfPointsCurvesAndMeshes(7, 6, 0));
Assert.AreEqual(6, BackgroundPreviewGeometry.NumberOfInvisiblePoints());
Assert.AreEqual(1, BackgroundPreviewGeometry.NumberOfInvisiblePoints());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marimano Are we just fixing the test to match the behavior here, or have the changes you made actually result in there being less objects in the scene?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ikeough these changes in tests are done to match new behavior which is presented in this PR. All points of a node are stored in a single geomentry instead of storing each array item of the node separately. So, here are 6 points which are stored in 1 PointGeometryModel3D from now


//flip off the lines node
var l1 = model.CurrentWorkspace.Nodes.First(x => x.GUID.ToString() == "7c1cecee-43ed-43b5-a4bb-5f71c50341b2");
l1.UpdateValue(new UpdateValueParams("IsVisible", "false"));

Assert.True(BackgroundPreviewGeometry.HasNumberOfPointsCurvesAndMeshes(7, 6, 0));
Assert.AreEqual(6, BackgroundPreviewGeometry.NumberOfInvisibleCurves());
Assert.AreEqual(1, BackgroundPreviewGeometry.NumberOfInvisibleCurves());

//flip those back on and ensure the visualization returns
p1.UpdateValue(new UpdateValueParams("IsVisible", "true"));
Expand Down Expand Up @@ -322,7 +322,7 @@ public void Node_VisibilityOff_CachedValueUpdated_VisibilityOn_RenderDataCorrect
p1.UpdateValue(new UpdateValueParams("IsVisible", "false"));

Assert.True(BackgroundPreviewGeometry.HasNumberOfPointsCurvesAndMeshes(7, 6, 0));
Assert.AreEqual(6, BackgroundPreviewGeometry.NumberOfInvisiblePoints());
Assert.AreEqual(1, BackgroundPreviewGeometry.NumberOfInvisiblePoints());

// Now change the number of points
var cbn =
Expand Down