< Summary

Line coverage
100%
Covered lines: 155
Uncovered lines: 0
Coverable lines: 155
Total lines: 374
Line coverage: 100%
Branch coverage
100%
Covered branches: 88
Total branches: 88
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.Media.WavefrontObj/dbbfcd213b7c54f49194f82fcd56e790cca193c3/JeremyAnsel.Media.WavefrontObj/JeremyAnsel.Media.WavefrontObj/ObjFileWriterContext.cs

#LineLine coverage
 1// <copyright file="ObjFileWriterContext.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.Globalization;
 9
 10namespace JeremyAnsel.Media.WavefrontObj
 11{
 12    internal class ObjFileWriterContext
 13    {
 14        private ObjFile obj;
 15
 21616        public ObjFileWriterContext(ObjFile obj)
 17        {
 21618            this.obj = obj;
 21619            this.GroupNames = string.Empty;
 21620        }
 21
 44122        public string GroupNames { get; private set; }
 23
 26424        public string? ObjectName { get; private set; }
 25
 23726        public int LevelOfDetail { get; private set; }
 27
 28228        public string? MapName { get; private set; }
 29
 28230        public string? MaterialName { get; private set; }
 31
 14732        public long SmoothingGroupNumber { get; private set; }
 33
 11734        public bool IsBevelInterpolationEnabled { get; private set; }
 35
 11736        public bool IsColorInterpolationEnabled { get; private set; }
 37
 11738        public bool IsDissolveInterpolationEnabled { get; private set; }
 39
 16240        public int MergingGroupNumber { get; private set; }
 41
 42        public void WriteGroupNames<T>(StreamWriter stream, T element, Func<ObjGroup, List<T>> func)
 43            where T : ObjElement
 44        {
 19545            string groupNames = GetGroupNames(element, func);
 46
 19547            if (groupNames != this.GroupNames)
 48            {
 3049                this.GroupNames = groupNames;
 50
 3051                if (string.IsNullOrEmpty(groupNames))
 52                {
 353                    stream.WriteLine("g default");
 54                }
 55                else
 56                {
 2757                    stream.WriteLine("g {0}", groupNames);
 58                }
 59            }
 19260        }
 61
 62        public void WriteAttributesOfElement(StreamWriter stream, ObjElement element)
 63        {
 19564            if (element.ObjectName != this.ObjectName)
 65            {
 2466                this.ObjectName = element.ObjectName;
 67
 2468                if (string.IsNullOrEmpty(this.ObjectName))
 69                {
 370                    stream.WriteLine("o");
 71                }
 72                else
 73                {
 2174                    stream.WriteLine("o {0}", this.ObjectName);
 75                }
 76            }
 77
 19578            if (element.LevelOfDetail != this.LevelOfDetail)
 79            {
 2180                this.LevelOfDetail = element.LevelOfDetail;
 81
 2182                stream.WriteLine("lod {0}", this.LevelOfDetail);
 83            }
 84
 19585            if (element.MapName != this.MapName)
 86            {
 3087                this.MapName = element.MapName;
 88
 3089                if (string.IsNullOrEmpty(this.MapName))
 90                {
 391                    stream.WriteLine("usemap off");
 92                }
 93                else
 94                {
 2795                    stream.WriteLine("usemap {0}", this.MapName);
 96                }
 97            }
 98
 19599            if (element.MaterialName != this.MaterialName)
 100            {
 30101                this.MaterialName = element.MaterialName;
 102
 30103                if (string.IsNullOrEmpty(this.MaterialName))
 104                {
 3105                    stream.WriteLine("usemtl off");
 106                }
 107                else
 108                {
 27109                    stream.WriteLine("usemtl {0}", this.MaterialName);
 110                }
 111            }
 192112        }
 113
 114        public void WriteAttributesOfPolygonalElement(StreamWriter stream, ObjPolygonalElement element)
 115        {
 87116            if (element.SmoothingGroupNumber != this.SmoothingGroupNumber)
 117            {
 21118                this.SmoothingGroupNumber = element.SmoothingGroupNumber;
 119
 21120                if (this.SmoothingGroupNumber == 0)
 121                {
 3122                    stream.WriteLine("s off");
 123                }
 124                else
 125                {
 18126                    stream.WriteLine("s {0}", this.SmoothingGroupNumber);
 127                }
 128            }
 129
 87130            if (element.IsBevelInterpolationEnabled != this.IsBevelInterpolationEnabled)
 131            {
 15132                this.IsBevelInterpolationEnabled = element.IsBevelInterpolationEnabled;
 133
 15134                if (this.IsBevelInterpolationEnabled)
 135                {
 12136                    stream.WriteLine("bevel on");
 137                }
 138                else
 139                {
 3140                    stream.WriteLine("bevel off");
 141                }
 142            }
 143
 87144            if (element.IsColorInterpolationEnabled != this.IsColorInterpolationEnabled)
 145            {
 15146                this.IsColorInterpolationEnabled = element.IsColorInterpolationEnabled;
 147
 15148                if (this.IsColorInterpolationEnabled)
 149                {
 12150                    stream.WriteLine("c_interp on");
 151                }
 152                else
 153                {
 3154                    stream.WriteLine("c_interp off");
 155                }
 156            }
 157
 87158            if (element.IsDissolveInterpolationEnabled != this.IsDissolveInterpolationEnabled)
 159            {
 15160                this.IsDissolveInterpolationEnabled = element.IsDissolveInterpolationEnabled;
 161
 15162                if (this.IsDissolveInterpolationEnabled)
 163                {
 12164                    stream.WriteLine("d_interp on");
 165                }
 166                else
 167                {
 3168                    stream.WriteLine("d_interp off");
 169                }
 170            }
 75171        }
 172
 173        public void WriteAttributesOfFreeFormElement(StreamWriter stream, ObjFreeFormElement element)
 174        {
 108175            if (element.MergingGroupNumber != this.MergingGroupNumber)
 176            {
 15177                this.MergingGroupNumber = element.MergingGroupNumber;
 178
 15179                if (this.MergingGroupNumber == 0)
 180                {
 3181                    stream.WriteLine("mg off");
 182                }
 183                else
 184                {
 12185                    float res = obj.MergingGroupResolutions[this.MergingGroupNumber];
 12186                    stream.WriteLine("mg {0} {1}", this.MergingGroupNumber, res.ToString("F6", CultureInfo.InvariantCult
 187                }
 188            }
 189
 108190            switch (element.FreeFormType)
 191            {
 192                case ObjFreeFormType.BasisMatrix:
 81193                    stream.Write("cstype");
 194
 81195                    if (element.IsRationalForm)
 196                    {
 78197                        stream.Write(" rat");
 198                    }
 199
 81200                    stream.WriteLine(" bmatrix");
 81201                    break;
 202
 203                case ObjFreeFormType.Bezier:
 6204                    stream.Write("cstype");
 205
 6206                    if (element.IsRationalForm)
 207                    {
 3208                        stream.Write(" rat");
 209                    }
 210
 6211                    stream.WriteLine(" bezier");
 6212                    break;
 213
 214                case ObjFreeFormType.BSpline:
 6215                    stream.Write("cstype");
 216
 6217                    if (element.IsRationalForm)
 218                    {
 3219                        stream.Write(" rat");
 220                    }
 221
 6222                    stream.WriteLine(" bspline");
 6223                    break;
 224
 225                case ObjFreeFormType.Cardinal:
 6226                    stream.Write("cstype");
 227
 6228                    if (element.IsRationalForm)
 229                    {
 3230                        stream.Write(" rat");
 231                    }
 232
 6233                    stream.WriteLine(" cardinal");
 6234                    break;
 235
 236                case ObjFreeFormType.Taylor:
 6237                    stream.Write("cstype");
 238
 6239                    if (element.IsRationalForm)
 240                    {
 3241                        stream.Write(" rat");
 242                    }
 243
 6244                    stream.WriteLine(" taylor");
 245                    break;
 246            }
 247
 108248            if (element.DegreeV == 0)
 249            {
 3250                stream.WriteLine("deg {0}", element.DegreeU);
 251            }
 252            else
 253            {
 105254                stream.WriteLine("deg {0} {1}", element.DegreeU, element.DegreeV);
 255            }
 256
 108257            if (element.BasicMatrixU != null)
 258            {
 105259                stream.Write("bmat u");
 260
 1050261                foreach (float value in element.BasicMatrixU)
 262                {
 420263                    stream.Write(' ');
 420264                    stream.Write(value.ToString("F6", CultureInfo.InvariantCulture));
 265                }
 266
 105267                stream.WriteLine();
 268            }
 269
 108270            if (element.BasicMatrixV != null)
 271            {
 105272                stream.Write("bmat v");
 273
 1050274                foreach (float value in element.BasicMatrixV)
 275                {
 420276                    stream.Write(' ');
 420277                    stream.Write(value.ToString("F6", CultureInfo.InvariantCulture));
 278                }
 279
 105280                stream.WriteLine();
 281            }
 282
 108283            if (element.StepV == 1.0f)
 284            {
 3285                stream.WriteLine(
 3286                    "step {0}",
 3287                    element.StepU.ToString("F6", CultureInfo.InvariantCulture));
 288            }
 289            else
 290            {
 105291                stream.WriteLine(
 105292                    "step {0} {1}",
 105293                    element.StepU.ToString("F6", CultureInfo.InvariantCulture),
 105294                    element.StepV.ToString("F6", CultureInfo.InvariantCulture));
 295            }
 296
 108297            if (element.CurveApproximationTechnique != null)
 298            {
 105299                if (element.CurveApproximationTechnique is ObjConstantParametricSubdivisionTechnique)
 300                {
 99301                    var technique = (ObjConstantParametricSubdivisionTechnique)element.CurveApproximationTechnique;
 99302                    stream.WriteLine("ctech cparm {0}", technique.ResolutionU.ToString("F6", CultureInfo.InvariantCultur
 303                }
 6304                else if (element.CurveApproximationTechnique is ObjConstantSpatialSubdivisionTechnique)
 305                {
 3306                    var technique = (ObjConstantSpatialSubdivisionTechnique)element.CurveApproximationTechnique;
 3307                    stream.WriteLine("ctech cspace {0}", technique.MaximumLength.ToString("F6", CultureInfo.InvariantCul
 308                }
 3309                else if (element.CurveApproximationTechnique is ObjCurvatureDependentSubdivisionTechnique)
 310                {
 3311                    var technique = (ObjCurvatureDependentSubdivisionTechnique)element.CurveApproximationTechnique;
 3312                    stream.WriteLine(
 3313                        "ctech curv {0} {1}",
 3314                        technique.MaximumDistance.ToString("F6", CultureInfo.InvariantCulture),
 3315                        technique.MaximumAngle.ToString("F6", CultureInfo.InvariantCulture));
 316                }
 317            }
 318
 108319            if (element.SurfaceApproximationTechnique != null)
 320            {
 105321                if (element.SurfaceApproximationTechnique is ObjConstantParametricSubdivisionTechnique)
 322                {
 99323                    var technique = (ObjConstantParametricSubdivisionTechnique)element.SurfaceApproximationTechnique;
 324
 99325                    if (technique.ResolutionU == technique.ResolutionV)
 326                    {
 3327                        stream.WriteLine(
 3328                            "stech cparmb {0}",
 3329                            technique.ResolutionU.ToString("F6", CultureInfo.InvariantCulture));
 330                    }
 331                    else
 332                    {
 96333                        stream.WriteLine(
 96334                            "stech cparma {0} {1}",
 96335                            technique.ResolutionU.ToString("F6", CultureInfo.InvariantCulture),
 96336                            technique.ResolutionV.ToString("F6", CultureInfo.InvariantCulture));
 337                    }
 338                }
 6339                else if (element.SurfaceApproximationTechnique is ObjConstantSpatialSubdivisionTechnique)
 340                {
 3341                    var technique = (ObjConstantSpatialSubdivisionTechnique)element.SurfaceApproximationTechnique;
 3342                    stream.WriteLine("stech cspace {0}", technique.MaximumLength.ToString("F6", CultureInfo.InvariantCul
 343                }
 3344                else if (element.SurfaceApproximationTechnique is ObjCurvatureDependentSubdivisionTechnique)
 345                {
 3346                    var technique = (ObjCurvatureDependentSubdivisionTechnique)element.SurfaceApproximationTechnique;
 3347                    stream.WriteLine(
 3348                        "stech curv {0} {1}",
 3349                        technique.MaximumDistance.ToString("F6", CultureInfo.InvariantCulture),
 3350                        technique.MaximumAngle.ToString("F6", CultureInfo.InvariantCulture));
 351                }
 352            }
 6353        }
 354
 355        private string GetGroupNames<T>(T element, Func<ObjGroup, List<T>> func)
 356            where T : ObjElement
 357        {
 195358            var groups = new List<string>();
 359
 462360            foreach (ObjGroup group in obj.Groups)
 361            {
 36362                List<T> elements = func(group);
 363
 36364                if (elements.Contains(element))
 365                {
 33366                    groups.Add(group.Name);
 367                }
 368            }
 369
 195370            return string.Join(" ", groups);
 371        }
 372    }
 373}
 374