-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move obj reader and obj material reader commands to separate classes …
…and functions
- Loading branch information
Showing
42 changed files
with
1,169 additions
and
661 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/ThreeDModels/Format/Obj/IO/Commands/DisplayAttributes/BevelCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ThreeDModels/Format/Obj/IO/Commands/DisplayAttributes/ColorInterpolationCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ThreeDModels/Format/Obj/IO/Commands/DisplayAttributes/DissolveInterpolationCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ThreeDModels/Format/Obj/IO/Commands/DisplayAttributes/LevelOfDetailCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/ThreeDModels/Format/Obj/IO/Commands/DisplayAttributes/MaterialLibraryCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ThreeDModels/Format/Obj/IO/Commands/DisplayAttributes/UseMapCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ThreeDModels/Format/Obj/IO/Commands/DisplayAttributes/UseMaterialCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/ThreeDModels/Format/Obj/IO/Commands/FreeFormCurveSurfaceAttributes/BasisMatrixCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...eDModels/Format/Obj/IO/Commands/FreeFormCurveSurfaceAttributes/CurveSurfaceTypeCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/ThreeDModels/Format/Obj/IO/Commands/FreeFormCurveSurfaceAttributes/DegreeCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/ThreeDModels/Format/Obj/IO/Commands/FreeFormCurveSurfaceAttributes/StepCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...s/Format/Obj/IO/Commands/FreeFormCurveSurfaceBodyStatements/EndFreeFormGeometryCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/ThreeDModels/Format/Obj/IO/Commands/FreeFormCurveSurfaceBodyStatements/HoleCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...hreeDModels/Format/Obj/IO/Commands/FreeFormCurveSurfaceBodyStatements/ParameterCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.