| | 1 | | // <copyright file="ObjFile.cs" company="Jérémy Ansel"> |
| | 2 | | // Copyright (c) 2017, 2019 Jérémy Ansel |
| | 3 | | // </copyright> |
| | 4 | | // <license> |
| | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | 6 | | // </license> |
| | 7 | |
|
| | 8 | | using System.Text; |
| | 9 | |
|
| | 10 | | namespace JeremyAnsel.Media.WavefrontObj |
| | 11 | | { |
| | 12 | | public class ObjFile |
| | 13 | | { |
| 1420 | 14 | | public ObjFile() |
| | 15 | | { |
| 1420 | 16 | | this.Vertices = new List<ObjVertex>(); |
| 1420 | 17 | | this.ParameterSpaceVertices = new List<ObjVector3>(); |
| 1420 | 18 | | this.VertexNormals = new List<ObjVector3>(); |
| 1420 | 19 | | this.TextureVertices = new List<ObjVector3>(); |
| 1420 | 20 | | this.Points = new List<ObjPoint>(); |
| 1420 | 21 | | this.Lines = new List<ObjLine>(); |
| 1420 | 22 | | this.Faces = new List<ObjFace>(); |
| 1420 | 23 | | this.Curves = new List<ObjCurve>(); |
| 1420 | 24 | | this.Curves2D = new List<ObjCurve2D>(); |
| 1420 | 25 | | this.Surfaces = new List<ObjSurface>(); |
| 1420 | 26 | | this.SurfaceConnections = new List<ObjSurfaceConnection>(); |
| 1420 | 27 | | this.DefaultGroup = new ObjGroup(); |
| 1420 | 28 | | this.Groups = new List<ObjGroup>(); |
| 1420 | 29 | | this.MergingGroupResolutions = new Dictionary<int, float>(); |
| 1420 | 30 | | this.MapLibraries = new List<string>(); |
| 1420 | 31 | | this.MaterialLibraries = new List<string>(); |
| 1420 | 32 | | } |
| | 33 | |
|
| 752 | 34 | | public string? HeaderText { get; set; } |
| | 35 | |
|
| 3140 | 36 | | public List<ObjVertex> Vertices { get; private set; } |
| | 37 | |
|
| 2472 | 38 | | public List<ObjVector3> ParameterSpaceVertices { get; private set; } |
| | 39 | |
|
| 1768 | 40 | | public List<ObjVector3> VertexNormals { get; private set; } |
| | 41 | |
|
| 1796 | 42 | | public List<ObjVector3> TextureVertices { get; private set; } |
| | 43 | |
|
| 2064 | 44 | | public List<ObjPoint> Points { get; private set; } |
| | 45 | |
|
| 1800 | 46 | | public List<ObjLine> Lines { get; private set; } |
| | 47 | |
|
| 1920 | 48 | | public List<ObjFace> Faces { get; private set; } |
| | 49 | |
|
| 2368 | 50 | | public List<ObjCurve> Curves { get; private set; } |
| | 51 | |
|
| 2116 | 52 | | public List<ObjCurve2D> Curves2D { get; private set; } |
| | 53 | |
|
| 1972 | 54 | | public List<ObjSurface> Surfaces { get; private set; } |
| | 55 | |
|
| 1752 | 56 | | public List<ObjSurfaceConnection> SurfaceConnections { get; private set; } |
| | 57 | |
|
| 2024 | 58 | | public ObjGroup DefaultGroup { get; private set; } |
| | 59 | |
|
| 2168 | 60 | | public List<ObjGroup> Groups { get; private set; } |
| | 61 | |
|
| 1476 | 62 | | public Dictionary<int, float> MergingGroupResolutions { get; private set; } |
| | 63 | |
|
| 1740 | 64 | | public List<string> MapLibraries { get; private set; } |
| | 65 | |
|
| 1744 | 66 | | public List<string> MaterialLibraries { get; private set; } |
| | 67 | |
|
| 352 | 68 | | public string? ShadowObjectFileName { get; set; } |
| | 69 | |
|
| 320 | 70 | | public string? TraceObjectFileName { get; set; } |
| | 71 | |
|
| | 72 | | public static ObjFile FromFile(string? path) |
| | 73 | | { |
| 8 | 74 | | return FromFile(path, ObjFileReaderSettings.Default); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public static ObjFile FromFile(string? path, ObjFileReaderSettings settings) |
| | 78 | | { |
| 8 | 79 | | if (path == null) |
| | 80 | | { |
| 4 | 81 | | throw new ArgumentNullException(nameof(path)); |
| | 82 | | } |
| | 83 | |
|
| 4 | 84 | | using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) |
| | 85 | | { |
| | 86 | | #if NET6_0_OR_GREATER |
| 3 | 87 | | return ObjFileReader9.FromStream(stream, settings); |
| | 88 | | #else |
| 1 | 89 | | return ObjFileReader.FromStream(stream, settings); |
| | 90 | | #endif |
| | 91 | | } |
| 4 | 92 | | } |
| | 93 | |
|
| | 94 | | public static ObjFile FromStream(Stream? stream) |
| | 95 | | { |
| 32 | 96 | | return FromStream(stream, ObjFileReaderSettings.Default); |
| | 97 | | } |
| | 98 | |
|
| | 99 | | public static ObjFile FromStream(Stream? stream, ObjFileReaderSettings settings) |
| | 100 | | { |
| | 101 | | #if NET6_0_OR_GREATER |
| 843 | 102 | | return ObjFileReader9.FromStream(stream, settings); |
| | 103 | | #else |
| 281 | 104 | | return ObjFileReader.FromStream(stream, settings); |
| | 105 | | #endif |
| | 106 | | } |
| | 107 | |
|
| | 108 | | public void WriteTo(string? path) |
| | 109 | | { |
| 8 | 110 | | if (path == null) |
| | 111 | | { |
| 4 | 112 | | throw new ArgumentNullException(nameof(path)); |
| | 113 | | } |
| | 114 | |
|
| 4 | 115 | | using (var writer = new StreamWriter(path)) |
| | 116 | | { |
| 4 | 117 | | ObjFileWriter.Write(this, writer); |
| 4 | 118 | | } |
| 4 | 119 | | } |
| | 120 | |
|
| | 121 | | public void WriteTo(Stream? stream) |
| | 122 | | { |
| 288 | 123 | | if (stream == null) |
| | 124 | | { |
| 4 | 125 | | throw new ArgumentNullException(nameof(stream)); |
| | 126 | | } |
| | 127 | |
|
| 284 | 128 | | using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true)) |
| | 129 | | { |
| 284 | 130 | | ObjFileWriter.Write(this, writer); |
| 284 | 131 | | } |
| 284 | 132 | | } |
| | 133 | | } |
| | 134 | | } |
| | 135 | |
|