Skip to content

Commit

Permalink
Move obj reader and obj material reader commands to separate classes …
Browse files Browse the repository at this point in the history
…and functions
  • Loading branch information
B3zaleel committed Sep 15, 2024
1 parent 384c046 commit 32b8de9
Show file tree
Hide file tree
Showing 42 changed files with 1,169 additions and 661 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using ThreeDModels.Format.Obj.Display;

namespace ThreeDModels.Format.Obj.IO.Commands.DisplayAttributes;

internal class BevelCommand : ICommand
{
public static string Name => "bevel";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
context.Display ??= new Display.DisplayAttributes();
context.Display.BevelInterpolationEnabled = ObjReader.GetBool(commandLine[1]);
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ThreeDModels.Format.Obj.IO.Commands.DisplayAttributes;

internal class ColorInterpolationCommand : ICommand
{
public static string Name => "c_interp";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
context.Display ??= new Display.DisplayAttributes();
context.Display.ColorInterpolationEnabled = ObjReader.GetBool(commandLine[1]);
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ThreeDModels.Format.Obj.IO.Commands.DisplayAttributes;

internal class DissolveInterpolationCommand : ICommand
{
public static string Name => "d_interp";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
context.Display ??= new Display.DisplayAttributes();
context.Display.DissolveInterpolationEnabled = ObjReader.GetBool(commandLine[1]);
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ThreeDModels.Format.Obj.IO.Commands.DisplayAttributes;

internal class LevelOfDetailCommand : ICommand
{
public static string Name => "lod";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
context.Display ??= new Display.DisplayAttributes();
context.Display.LevelOfDetail = int.Parse(commandLine[1]);
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using ThreeDModels.Format.Obj.Display;
using ThreeDModels.Format.Obj.IO.Commands.Materials;

namespace ThreeDModels.Format.Obj.IO.Commands.DisplayAttributes;

internal class MaterialLibraryCommand : ICommand
{
private static readonly Dictionary<string, IMaterialCommandRead> _commands = new()
{
{ NewMaterialCommand.Name, NewMaterialCommand.Read },
{ AmbientColorCommand.Name, AmbientColorCommand.Read },
{ DiffuseColorCommand.Name, DiffuseColorCommand.Read },
{ SpecularColorCommand.Name, SpecularColorCommand.Read },
{ SpecularExponentCommand.Name, SpecularExponentCommand.Read },
{ DissolveCommand.Name, DissolveCommand.Read },
{ TransparencyCommand.Name, TransparencyCommand.Read },
{ IlluminationModelCommand.Name, IlluminationModelCommand.Read },
};
public static string Name => "mtllib";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
for (int i = 1; i < commandLine.Count; i++)
{
obj.MaterialLibraries.Add(GetMaterials(commandLine[i]));
}
}

public static void Write()
{
throw new NotImplementedException();
}

private static List<Material> GetMaterials(string path)
{
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
return GetMaterials(fs);
}

private static List<Material> GetMaterials(Stream stream)
{
var context = new MaterialReaderContext(material: null, materials: []);
using var textReader = new StreamReader(stream);
while (stream.Position < stream.Length)
{
var commandLine = ObjReader.GetCommandLine(textReader);
if (commandLine.Count == 0)
{
continue;
}
if (_commands.TryGetValue(commandLine[0], out var readCommand))
{
readCommand.Invoke(commandLine, context);
}
else
{
throw new InvalidDataException($"Unknown material command '{commandLine[0]}'");
}
}
if (context.Material != null)
{
context.Materials.Add(context.Material);
}
return context.Materials;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ThreeDModels.Format.Obj.IO.Commands.DisplayAttributes;

internal class UseMapCommand : ICommand
{
public static string Name => "usemap";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
context.Display ??= new Display.DisplayAttributes();
context.Display.TextureMapName = commandLine.Count > 1 && commandLine[1] != Constants.Off ? commandLine[1] : string.Empty;
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ThreeDModels.Format.Obj.IO.Commands.DisplayAttributes;

internal class UseMaterialCommand : ICommand
{
public static string Name => "usemtl";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
context.Display ??= new Display.DisplayAttributes();
context.Display.MaterialName = commandLine.Count > 1 ? commandLine[1] : string.Empty;
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace ThreeDModels.Format.Obj.IO.Commands.FreeFormCurveSurfaceAttributes;

internal class BasisMatrixCommand : ICommand
{
public static string Name => "bmat";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
if (context.FreeFormGeometry == null)
{
throw new InvalidDataException("BasisMatrix (bmat) must follow a curve or surface element");
}
var matrix = new List<float>();
for (int i = 2; i < commandLine.Count; i++)
{
matrix.Add(float.Parse(commandLine[i]));
}
if (commandLine[1] == "u")
{
context.FreeFormGeometry.BasisMatrixU = matrix;
}
else
{
context.FreeFormGeometry.BasisMatrixV = commandLine[1] == "v"
? matrix
: throw new InvalidDataException($"BasisMatrix (bmat) has an invalid direction '{commandLine[1]}'");
}
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using ThreeDModels.Format.Obj.Elements;

namespace ThreeDModels.Format.Obj.IO.Commands.FreeFormCurveSurfaceAttributes;

internal class CurveSurfaceTypeCommand : ICommand
{
public static string Name => "cstype";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
if (context.FreeFormGeometry == null)
{
throw new InvalidDataException("CurveSurfaceType (cstype) must follow a curve or surface element");
}
if (commandLine.Count > 2)
{
context.FreeFormGeometry.IsRational = commandLine[1] == "rat";
context.FreeFormGeometry.Type = Enum.Parse<CurveSurfaceType>(commandLine[2]);
}
else
{
context.FreeFormGeometry.Type = Enum.Parse<CurveSurfaceType>(commandLine[1]);
}
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace ThreeDModels.Format.Obj.IO.Commands.FreeFormCurveSurfaceAttributes;

internal class DegreeCommand : ICommand
{
public static string Name => "deg";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
if (context.FreeFormGeometry == null)
{
throw new InvalidDataException("Degree (deg) must follow a curve or surface element");
}
context.FreeFormGeometry.DegreeU = int.Parse(commandLine[1]);
context.FreeFormGeometry.DegreeV = commandLine.Count > 2 ? int.Parse(commandLine[2]) : null;
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace ThreeDModels.Format.Obj.IO.Commands.FreeFormCurveSurfaceAttributes;

internal class StepCommand : ICommand
{
public static string Name => "step";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
if (context.FreeFormGeometry == null)
{
throw new InvalidDataException("Step (step) must follow a curve or surface element");
}
context.FreeFormGeometry.StepU = int.Parse(commandLine[1]);
context.FreeFormGeometry.StepV = commandLine.Count > 2 ? int.Parse(commandLine[2]) : null;
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using ThreeDModels.Format.Obj.Elements;

namespace ThreeDModels.Format.Obj.IO.Commands.FreeFormCurveSurfaceBodyStatements;

internal class EndFreeFormGeometryCommand : ICommand
{
public static string Name => "end";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
if (context.FreeFormGeometry == null)
{
throw new InvalidDataException("The curve or surface element is not initialized");
}
else if (context.FreeFormGeometry is Curve curve)
{
context.Group.Curves.Add(curve);
}
else if (context.FreeFormGeometry is Curve2D curve2D)
{
context.Group.Curves2D.Add(curve2D);
}
else if (context.FreeFormGeometry is Surface surface)
{
context.Group.Surfaces.Add(surface);
}
else
{
throw new InvalidDataException("Unknown free-form geometry type");
}
context.FreeFormGeometry = null;
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using ThreeDModels.Format.Obj.Elements;

namespace ThreeDModels.Format.Obj.IO.Commands.FreeFormCurveSurfaceBodyStatements;

internal class HoleCommand : ICommand
{
public static string Name => "hole";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
if (context.FreeFormGeometry == null)
{
throw new InvalidDataException("Hole (hole) must follow a curve or surface element");
}
var curve2Ds = new List<Curve2DReference>();
for (int i = 1; i < commandLine.Count; i += 3)
{
curve2Ds.Add(new()
{
Start = float.Parse(commandLine[i]),
End = float.Parse(commandLine[i + 1]),
Curve2DIndex = int.Parse(commandLine[i + 2]),
});
}
context.FreeFormGeometry.Holes ??= [];
context.FreeFormGeometry.Holes.Add(curve2Ds);
}

public static void Write()
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace ThreeDModels.Format.Obj.IO.Commands.FreeFormCurveSurfaceBodyStatements;

internal class ParameterCommand : ICommand
{
public static string Name => "parm";

public static void Read(List<string> commandLine, ObjReaderContext context, ref Obj obj)
{
if (context.FreeFormGeometry == null)
{
throw new InvalidDataException("Parameter (parm) must follow a curve or surface element");
}
var matrix = new List<float>();
for (int i = 2; i < commandLine.Count; i++)
{
matrix.Add(float.Parse(commandLine[i]));
}
if (commandLine[1] == "u")
{
context.FreeFormGeometry.ParameterU = matrix;
}
else
{
context.FreeFormGeometry.ParameterV = commandLine[1] == "v"
? matrix
: throw new InvalidDataException($"BasisMatrix (bmat) has an invalid direction '{commandLine[1]}'");
}
}

public static void Write()
{
throw new NotImplementedException();
}
}
Loading

0 comments on commit 32b8de9

Please sign in to comment.