< Summary

Information
Class: JeremyAnsel.Media.WavefrontObj.ObjFile
Assembly: JeremyAnsel.Media.WavefrontObj
File(s): C:\projects\jeremyansel-media-wavefrontobj\JeremyAnsel.Media.WavefrontObj\JeremyAnsel.Media.WavefrontObj\ObjFile.cs
Line coverage
96%
Covered lines: 75
Uncovered lines: 3
Coverable lines: 78
Total lines: 213
Line coverage: 96.1%
Branch coverage
89%
Covered branches: 41
Total branches: 46
Branch coverage: 89.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
FromFile(...)100%11100%
FromFile(...)100%22100%
FromStream(...)100%11100%
FromStream(...)100%11100%
WriteTo(...)100%22100%
WriteTo(...)100%22100%
CalculateFaceNormals()86.11%363693.55%
CalculateFaceAreaDirection(...)100%22100%

File(s)

C:\projects\jeremyansel-media-wavefrontobj\JeremyAnsel.Media.WavefrontObj\JeremyAnsel.Media.WavefrontObj\ObjFile.cs

#LineLine coverage
 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
 8using System.Numerics;
 9using System.Text;
 10
 11namespace JeremyAnsel.Media.WavefrontObj;
 12
 13public class ObjFile
 14{
 315    public ObjFile()
 16    {
 317        Vertices = new List<ObjVertex>();
 318        ParameterSpaceVertices = new List<ObjVector3>();
 319        VertexNormals = new List<ObjVector3>();
 320        TextureVertices = new List<ObjVector3>();
 321        Points = new List<ObjPoint>();
 322        Lines = new List<ObjLine>();
 323        Faces = new List<ObjFace>();
 324        Curves = new List<ObjCurve>();
 325        Curves2D = new List<ObjCurve2D>();
 326        Surfaces = new List<ObjSurface>();
 327        SurfaceConnections = new List<ObjSurfaceConnection>();
 328        DefaultGroup = new ObjGroup();
 329        Groups = new List<ObjGroup>();
 330        MergingGroupResolutions = new Dictionary<int, float>();
 331        MapLibraries = new List<string>();
 332        MaterialLibraries = new List<string>();
 333    }
 34
 35    public string? HeaderText { get; set; }
 36
 37    public List<ObjVertex> Vertices { get; private set; }
 38
 39    public List<ObjVector3> ParameterSpaceVertices { get; private set; }
 40
 41    public List<ObjVector3> VertexNormals { get; private set; }
 42
 43    public List<ObjVector3> TextureVertices { get; private set; }
 44
 45    public List<ObjPoint> Points { get; private set; }
 46
 47    public List<ObjLine> Lines { get; private set; }
 48
 49    public List<ObjFace> Faces { get; private set; }
 50
 51    public List<ObjCurve> Curves { get; private set; }
 52
 53    public List<ObjCurve2D> Curves2D { get; private set; }
 54
 55    public List<ObjSurface> Surfaces { get; private set; }
 56
 57    public List<ObjSurfaceConnection> SurfaceConnections { get; private set; }
 58
 59    public ObjGroup DefaultGroup { get; private set; }
 60
 61    public List<ObjGroup> Groups { get; private set; }
 62
 63    public Dictionary<int, float> MergingGroupResolutions { get; private set; }
 64
 65    public List<string> MapLibraries { get; private set; }
 66
 67    public List<string> MaterialLibraries { get; private set; }
 68
 69    public string? ShadowObjectFileName { get; set; }
 70
 71    public string? TraceObjectFileName { get; set; }
 72
 73    public static ObjFile FromFile(string? path)
 74    {
 375        return FromFile(path, ObjFileReaderSettings.Default);
 76    }
 77
 78    public static ObjFile FromFile(string? path, ObjFileReaderSettings settings)
 79    {
 380        if (path == null)
 81        {
 382            throw new ArgumentNullException(nameof(path));
 83        }
 84
 385        using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
 86        {
 387            return ObjFileReader.FromStream(stream, settings);
 88        }
 389    }
 90
 91    public static ObjFile FromStream(Stream? stream)
 92    {
 393        return FromStream(stream, ObjFileReaderSettings.Default);
 94    }
 95
 96    public static ObjFile FromStream(Stream? stream, ObjFileReaderSettings settings)
 97    {
 398        return ObjFileReader.FromStream(stream, settings);
 99    }
 100
 101    public void WriteTo(string? path)
 102    {
 3103        if (path == null)
 104        {
 3105            throw new ArgumentNullException(nameof(path));
 106        }
 107
 3108        using (var writer = new StreamWriter(path))
 109        {
 3110            ObjFileWriter.Write(this, writer);
 3111        }
 3112    }
 113
 114    public void WriteTo(Stream? stream)
 115    {
 3116        if (stream == null)
 117        {
 3118            throw new ArgumentNullException(nameof(stream));
 119        }
 120
 3121        using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true))
 122        {
 3123            ObjFileWriter.Write(this, writer);
 3124        }
 3125    }
 126
 127    public void CalculateFaceNormals()
 128    {
 3129        var faceAreaDirections = new Vector3[Faces.Count];
 3130        var adjacentFacesMap = Enumerable.Range(0, Vertices.Count).Select(_ => new List<int>()).ToArray();
 131
 3132        for (int faceindex = 0; faceindex < Faces.Count; faceindex++)
 133        {
 3134            var face = Faces[faceindex];
 135
 3136            faceAreaDirections[faceindex] = CalculateFaceAreaDirection(face);
 137
 3138            foreach (var triplet in face.Vertices)
 139            {
 3140                adjacentFacesMap[triplet.Vertex - 1].Add(faceindex);
 141            }
 142        }
 143
 3144        var reuseNormals = new Dictionary<Vector3, int>();
 145
 3146        for (int normalIndex = 0; normalIndex < VertexNormals.Count; ++normalIndex)
 147        {
 0148            reuseNormals[VertexNormals[normalIndex].ToVector3()] = normalIndex + 1;
 149        }
 150
 151        // 3dmax and other software interpret smooth groups as a bit mask.
 0152        Func<ObjPolygonalElement, ObjPolygonalElement, bool> smoothFunc = (l, r) => (l.SmoothingGroupNumber & r.Smoothin
 153
 154        // this should be used if no actual smooth groups are defined, but right now it's not possible to tell.
 3155        if (Faces.All(f => f.SmoothingGroupNumber == 0)) smoothFunc = (_, _) => true;
 156
 3157        for (int faceIndex = 0; faceIndex < Faces.Count; ++faceIndex)
 158        {
 3159            var face = Faces[faceIndex];
 160
 3161            for (int tripletIndex = 0; tripletIndex < face.Vertices.Count; ++tripletIndex)
 162            {
 3163                var v = face.Vertices[tripletIndex];
 3164                if (v.Normal > 0) continue;
 165
 3166                var adjacentFaces = adjacentFacesMap[v.Vertex - 1];
 167
 3168                Vector3 normal = faceAreaDirections[faceIndex];
 169
 3170                normal = adjacentFaces
 3171                        .Where(idx => idx != faceIndex && smoothFunc(face, Faces[idx]))
 3172                        .Aggregate(normal, (accum, fi) => accum + faceAreaDirections[fi]);
 173
 3174                if (normal == Vector3.Zero)
 175                {
 0176                    normal = Vertices[v.Vertex].Position.ToVector3();
 177                }
 178
 3179                normal = Vector3.Normalize(normal);
 180
 3181                if (float.IsNaN(normal.X) || float.IsNaN(normal.Y) || float.IsNaN(normal.Z)) throw new InvalidOperationE
 182
 3183                if (!reuseNormals.TryGetValue(normal, out var normalIdx))
 184                {
 3185                    VertexNormals.Add(normal);
 3186                    normalIdx = VertexNormals.Count;
 3187                    reuseNormals[normal] = normalIdx;
 188                }
 189
 3190                v.Normal = normalIdx;
 191
 3192                face.Vertices[tripletIndex] = v;
 193            }
 194        }
 3195    }
 196
 197    private Vector3 CalculateFaceAreaDirection(ObjFace face)
 198    {
 3199        var v0 = Vertices[face.Vertices[0].Vertex - 1].Position.ToVector3();
 200
 3201        Vector3 n = Vector3.Zero;
 202
 3203        for (int k = 2; k < face.Vertices.Count; k++)
 204        {
 3205            var v1 = Vertices[face.Vertices[k - 1].Vertex - 1].Position.ToVector3();
 3206            var v2 = Vertices[face.Vertices[k].Vertex - 1].Position.ToVector3();
 207
 3208            n += Vector3.Cross(v1 - v0, v2 - v0) / 2;
 209        }
 210
 3211        return n; // do NOT normalize, may be zero if triangle is degenerated.
 212    }
 213}