Skip to content

Commit

Permalink
Can now link shape data to shape
Browse files Browse the repository at this point in the history
  • Loading branch information
Donkie committed Dec 19, 2021
1 parent b2c06fb commit 79f4e66
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
13 changes: 13 additions & 0 deletions I3DShapesTool.Lib/Model/I3D/Camera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace I3DShapesTool.Lib.Model.I3D
{
public class Camera : TransformGroup
{
public Camera(string? name, int? id, I3DVector translation, I3DVector rotation, I3DVector scale) : base(name, id, translation, rotation, scale)
{
}
}
}
49 changes: 43 additions & 6 deletions I3DShapesTool.Lib/Model/I3D/I3D.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;

namespace I3DShapesTool.Lib.Model.I3D
{
public class I3D
{
public static ILogger? logger { get; set; }

public string? Name { get; set; }
public string? Version { get; set; }
public TransformGroup SceneRoot { get; } = new TransformGroup("root", 0, I3DVector.Zero, I3DVector.Zero, I3DVector.One);

private IDictionary<int, Shape>? _shapesMap;
private IDictionary<uint, Shape>? _shapesMap;

public void LinkShapeData(I3DShape shape)
{
if (_shapesMap == null)
throw new InvalidOperationException("Shape map not generated yet.");

if (!_shapesMap.ContainsKey(shape.Id))
throw new ArgumentException($"No shape with ID \"{shape.Id}\" in scene.");

_shapesMap[shape.Id].ShapeData = shape;
}

public void LinkShapesFile(ShapesFile shapesFile)
{
foreach (var shape in shapesFile.Shapes)
{
try
{
LinkShapeData(shape);
}
catch(ArgumentException ex)
{
logger.LogWarning(ex.Message);
}
}
}

private void MapShapesRecurse(TransformGroup parent)
{
if (_shapesMap == null)
throw new InvalidOperationException();
throw new InvalidOperationException("Shape map not generated yet.");

if (parent is Shape shape && shape.ShapeId != null)
{
_shapesMap.Add((int)shape.ShapeId, shape);
_shapesMap.Add((uint)shape.ShapeId, shape);
}

foreach (var child in parent.Children)
Expand All @@ -29,7 +58,7 @@ private void MapShapesRecurse(TransformGroup parent)

private void MapShapes()
{
_shapesMap = new Dictionary<int, Shape>();
_shapesMap = new Dictionary<uint, Shape>();
MapShapesRecurse(SceneRoot);
}

Expand All @@ -38,7 +67,15 @@ public void Setup()
MapShapes();
}

public Shape? GetShape(int id)
public IEnumerable<Shape> GetShapes()
{
if (_shapesMap == null)
throw new InvalidOperationException("Shape map not generated yet.");

return _shapesMap.Values;
}

public Shape? GetShape(uint id)
{
if (_shapesMap == null || !_shapesMap.ContainsKey(id))
return null;
Expand Down
1 change: 1 addition & 0 deletions I3DShapesTool.Lib/Model/I3D/I3DSceneType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public enum I3DSceneType
{
TransformGroup,
Camera,
Shape,
Light
}
Expand Down
1 change: 1 addition & 0 deletions I3DShapesTool.Lib/Model/I3D/I3DXMLReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private static void TraverseScene(XmlReader reader, TransformGroup parent)
{
I3DSceneType.Shape => new Shape(name, id, shapeId, pos, rot, scl),
I3DSceneType.Light => new Light(name, id, pos, rot, scl),
I3DSceneType.Camera => new Camera(name, id, pos, rot, scl),
_ => new TransformGroup(name, id, pos, rot, scl),
};
child.SetParent(parent);
Expand Down
1 change: 1 addition & 0 deletions I3DShapesTool.Lib/Model/I3D/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Shape : TransformGroup
{
public int? ShapeId { get; }
public I3DShape? ShapeData { get; set; }

public Shape(string? name, int? id, int? shapeId, I3DVector translation, I3DVector rotation, I3DVector scale) : base(name, id, translation, rotation, scale)
{
Expand Down

0 comments on commit 79f4e66

Please sign in to comment.