| | 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 | | { |
| 1035 | 14 | | public ObjFile() |
| | 15 | | { |
| 1035 | 16 | | this.Vertices = new List<ObjVertex>(); |
| 1035 | 17 | | this.ParameterSpaceVertices = new List<ObjVector3>(); |
| 1035 | 18 | | this.VertexNormals = new List<ObjVector3>(); |
| 1035 | 19 | | this.TextureVertices = new List<ObjVector3>(); |
| 1035 | 20 | | this.Points = new List<ObjPoint>(); |
| 1035 | 21 | | this.Lines = new List<ObjLine>(); |
| 1035 | 22 | | this.Faces = new List<ObjFace>(); |
| 1035 | 23 | | this.Curves = new List<ObjCurve>(); |
| 1035 | 24 | | this.Curves2D = new List<ObjCurve2D>(); |
| 1035 | 25 | | this.Surfaces = new List<ObjSurface>(); |
| 1035 | 26 | | this.SurfaceConnections = new List<ObjSurfaceConnection>(); |
| 1035 | 27 | | this.DefaultGroup = new ObjGroup(); |
| 1035 | 28 | | this.Groups = new List<ObjGroup>(); |
| 1035 | 29 | | this.MergingGroupResolutions = new Dictionary<int, float>(); |
| 1035 | 30 | | this.MapLibraries = new List<string>(); |
| 1035 | 31 | | this.MaterialLibraries = new List<string>(); |
| 1035 | 32 | | } |
| | 33 | |
|
| 522 | 34 | | public string? HeaderText { get; set; } |
| | 35 | |
|
| 2325 | 36 | | public List<ObjVertex> Vertices { get; private set; } |
| | 37 | |
|
| 1824 | 38 | | public List<ObjVector3> ParameterSpaceVertices { get; private set; } |
| | 39 | |
|
| 1296 | 40 | | public List<ObjVector3> VertexNormals { get; private set; } |
| | 41 | |
|
| 1317 | 42 | | public List<ObjVector3> TextureVertices { get; private set; } |
| | 43 | |
|
| 1518 | 44 | | public List<ObjPoint> Points { get; private set; } |
| | 45 | |
|
| 1320 | 46 | | public List<ObjLine> Lines { get; private set; } |
| | 47 | |
|
| 1410 | 48 | | public List<ObjFace> Faces { get; private set; } |
| | 49 | |
|
| 1746 | 50 | | public List<ObjCurve> Curves { get; private set; } |
| | 51 | |
|
| 1557 | 52 | | public List<ObjCurve2D> Curves2D { get; private set; } |
| | 53 | |
|
| 1449 | 54 | | public List<ObjSurface> Surfaces { get; private set; } |
| | 55 | |
|
| 1284 | 56 | | public List<ObjSurfaceConnection> SurfaceConnections { get; private set; } |
| | 57 | |
|
| 1488 | 58 | | public ObjGroup DefaultGroup { get; private set; } |
| | 59 | |
|
| 1494 | 60 | | public List<ObjGroup> Groups { get; private set; } |
| | 61 | |
|
| 1077 | 62 | | public Dictionary<int, float> MergingGroupResolutions { get; private set; } |
| | 63 | |
|
| 1275 | 64 | | public List<string> MapLibraries { get; private set; } |
| | 65 | |
|
| 1269 | 66 | | public List<string> MaterialLibraries { get; private set; } |
| | 67 | |
|
| 252 | 68 | | public string? ShadowObjectFileName { get; set; } |
| | 69 | |
|
| 228 | 70 | | public string? TraceObjectFileName { get; set; } |
| | 71 | |
|
| | 72 | | public static ObjFile FromFile(string? path) |
| | 73 | | { |
| 6 | 74 | | if (path == null) |
| | 75 | | { |
| 3 | 76 | | throw new ArgumentNullException(nameof(path)); |
| | 77 | | } |
| | 78 | |
|
| 3 | 79 | | using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) |
| | 80 | | { |
| 3 | 81 | | return ObjFileReader.FromStream(stream); |
| | 82 | | } |
| 3 | 83 | | } |
| | 84 | |
|
| | 85 | | public static ObjFile FromStream(Stream? stream) |
| | 86 | | { |
| 816 | 87 | | return ObjFileReader.FromStream(stream); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public void WriteTo(string? path) |
| | 91 | | { |
| 3 | 92 | | if (path == null) |
| | 93 | | { |
| 0 | 94 | | throw new ArgumentNullException(nameof(path)); |
| | 95 | | } |
| | 96 | |
|
| 3 | 97 | | using (var writer = new StreamWriter(path)) |
| | 98 | | { |
| 3 | 99 | | ObjFileWriter.Write(this, writer); |
| 3 | 100 | | } |
| 3 | 101 | | } |
| | 102 | |
|
| | 103 | | public void WriteTo(Stream? stream) |
| | 104 | | { |
| 216 | 105 | | if (stream == null) |
| | 106 | | { |
| 3 | 107 | | throw new ArgumentNullException(nameof(stream)); |
| | 108 | | } |
| | 109 | |
|
| 213 | 110 | | using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true)) |
| | 111 | | { |
| 213 | 112 | | ObjFileWriter.Write(this, writer); |
| 213 | 113 | | } |
| 213 | 114 | | } |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|