< Summary

Line coverage
100%
Covered lines: 231
Uncovered lines: 0
Coverable lines: 231
Total lines: 458
Line coverage: 100%
Branch coverage
100%
Covered branches: 86
Total branches: 86
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Write(...)100%11100%
WriteHeaderText(...)100%44100%
WriteShadowObjectFileName(...)100%22100%
WriteTraceObjectFileName(...)100%22100%
WriteMapLibraries(...)100%44100%
WriteMaterialLibraries(...)100%44100%
WriteVertices(...)100%88100%
WriteParameterSpaceVertices(...)100%66100%
WriteVertexNormals(...)100%22100%
WriteTextureVertices(...)100%22100%
WriteSurfaceConnection(...)100%22100%
WritePoints(...)100%44100%
WriteLines(...)100%44100%
WriteFaces(...)100%44100%
WriteCurves(...)100%44100%
WriteCurves2D(...)100%44100%
WriteSurfaces(...)100%44100%
WriteBodyOfFreeFormElement(...)100%2626100%

File(s)

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

#LineLine coverage
 1// <copyright file="ObjFileWriter.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 static class ObjFileWriter
 13    {
 14        public static void Write(ObjFile obj, StreamWriter stream)
 15        {
 21616            var context = new ObjFileWriterContext(obj);
 17
 21618            WriteHeaderText(obj, stream);
 19
 21620            WriteShadowObjectFileName(obj, stream);
 21621            WriteTraceObjectFileName(obj, stream);
 22
 21623            WriteMapLibraries(obj, stream);
 21624            WriteMaterialLibraries(obj, stream);
 25
 21626            WriteVertices(obj, stream);
 21627            WriteParameterSpaceVertices(obj, stream);
 21628            WriteVertexNormals(obj, stream);
 21629            WriteTextureVertices(obj, stream);
 30
 21631            WritePoints(obj, stream, context);
 21632            WriteLines(obj, stream, context);
 21633            WriteFaces(obj, stream, context);
 34
 21635            WriteCurves(obj, stream, context);
 21636            WriteCurves2D(obj, stream, context);
 21637            WriteSurfaces(obj, stream, context);
 38
 21639            WriteSurfaceConnection(obj, stream);
 21640        }
 41
 42        private static void WriteHeaderText(ObjFile obj, StreamWriter stream)
 43        {
 21644            if (string.IsNullOrEmpty(obj.HeaderText))
 45            {
 21346                return;
 47            }
 48
 349            string[] headerLines = obj.HeaderText!.Split('\n');
 50
 1851            foreach (string line in headerLines)
 52            {
 653                stream.Write('#');
 654                stream.WriteLine(line);
 55            }
 356        }
 57
 58        private static void WriteShadowObjectFileName(ObjFile obj, StreamWriter stream)
 59        {
 21660            if (!string.IsNullOrEmpty(obj.ShadowObjectFileName))
 61            {
 962                stream.Write(string.Format(CultureInfo.InvariantCulture, "shadow_obj {0}\n", obj.ShadowObjectFileName));
 63            }
 21664        }
 65
 66        private static void WriteTraceObjectFileName(ObjFile obj, StreamWriter stream)
 67        {
 21668            if (!string.IsNullOrEmpty(obj.TraceObjectFileName))
 69            {
 370                stream.Write(string.Format(CultureInfo.InvariantCulture, "trace_obj {0}\n", obj.TraceObjectFileName));
 71            }
 21672        }
 73
 74        private static void WriteMapLibraries(ObjFile obj, StreamWriter stream)
 75        {
 21676            if (obj.MapLibraries.Count == 0)
 77            {
 21378                return;
 79            }
 80
 381            stream.Write("maplib");
 82
 1883            foreach (string map in obj.MapLibraries)
 84            {
 685                stream.Write(" ");
 686                stream.Write(map);
 87            }
 88
 389            stream.WriteLine();
 390        }
 91
 92        private static void WriteMaterialLibraries(ObjFile obj, StreamWriter stream)
 93        {
 21694            if (obj.MaterialLibraries.Count == 0)
 95            {
 21396                return;
 97            }
 98
 399            stream.Write("mtllib");
 100
 18101            foreach (string map in obj.MaterialLibraries)
 102            {
 6103                stream.Write(" ");
 6104                stream.Write(map);
 105            }
 106
 3107            stream.WriteLine();
 3108        }
 109
 110        private static void WriteVertices(ObjFile obj, StreamWriter stream)
 111        {
 582112            foreach (var vertex in obj.Vertices)
 113            {
 75114                var position = vertex.Position;
 115
 75116                if (vertex.Color.HasValue)
 117                {
 6118                    var color = vertex.Color.Value;
 119
 6120                    if (color.W == 1.0f)
 121                    {
 3122                        stream.WriteLine(
 3123                            "v {0} {1} {2} {3} {4} {5}",
 3124                            position.X.ToString("F6", CultureInfo.InvariantCulture),
 3125                            position.Y.ToString("F6", CultureInfo.InvariantCulture),
 3126                            position.Z.ToString("F6", CultureInfo.InvariantCulture),
 3127                            color.X.ToString("F6", CultureInfo.InvariantCulture),
 3128                            color.Y.ToString("F6", CultureInfo.InvariantCulture),
 3129                            color.Z.ToString("F6", CultureInfo.InvariantCulture));
 130                    }
 131                    else
 132                    {
 3133                        stream.WriteLine(
 3134                            "v {0} {1} {2} {3} {4} {5} {6}",
 3135                            position.X.ToString("F6", CultureInfo.InvariantCulture),
 3136                            position.Y.ToString("F6", CultureInfo.InvariantCulture),
 3137                            position.Z.ToString("F6", CultureInfo.InvariantCulture),
 3138                            color.X.ToString("F6", CultureInfo.InvariantCulture),
 3139                            color.Y.ToString("F6", CultureInfo.InvariantCulture),
 3140                            color.Z.ToString("F6", CultureInfo.InvariantCulture),
 3141                            color.W.ToString("F6", CultureInfo.InvariantCulture));
 142                    }
 143                }
 144                else
 145                {
 69146                    if (position.W == 1.0f)
 147                    {
 66148                        stream.WriteLine(
 66149                            "v {0} {1} {2}",
 66150                            position.X.ToString("F6", CultureInfo.InvariantCulture),
 66151                            position.Y.ToString("F6", CultureInfo.InvariantCulture),
 66152                            position.Z.ToString("F6", CultureInfo.InvariantCulture));
 153                    }
 154                    else
 155                    {
 3156                        stream.WriteLine(
 3157                            "v {0} {1} {2} {3}",
 3158                            position.X.ToString("F6", CultureInfo.InvariantCulture),
 3159                            position.Y.ToString("F6", CultureInfo.InvariantCulture),
 3160                            position.Z.ToString("F6", CultureInfo.InvariantCulture),
 3161                            position.W.ToString("F6", CultureInfo.InvariantCulture));
 162                    }
 163                }
 164            }
 216165        }
 166
 167        private static void WriteParameterSpaceVertices(ObjFile obj, StreamWriter stream)
 168        {
 474169            foreach (var vertex in obj.ParameterSpaceVertices)
 170            {
 21171                if (vertex.Z == 1.0f)
 172                {
 6173                    if (vertex.Y == 0.0f)
 174                    {
 3175                        stream.WriteLine(
 3176                            "vp {0}",
 3177                            vertex.X.ToString("F6", CultureInfo.InvariantCulture));
 178                    }
 179                    else
 180                    {
 3181                        stream.WriteLine(
 3182                            "vp {0} {1}",
 3183                            vertex.X.ToString("F6", CultureInfo.InvariantCulture),
 3184                            vertex.Y.ToString("F6", CultureInfo.InvariantCulture));
 185                    }
 186                }
 187                else
 188                {
 15189                    stream.WriteLine(
 15190                        "vp {0} {1} {2}",
 15191                        vertex.X.ToString("F6", CultureInfo.InvariantCulture),
 15192                        vertex.Y.ToString("F6", CultureInfo.InvariantCulture),
 15193                        vertex.Z.ToString("F6", CultureInfo.InvariantCulture));
 194                }
 195            }
 216196        }
 197
 198        private static void WriteVertexNormals(ObjFile obj, StreamWriter stream)
 199        {
 438200            foreach (var vertex in obj.VertexNormals)
 201            {
 3202                stream.WriteLine(
 3203                    "vn {0} {1} {2}",
 3204                    vertex.X.ToString("F6", CultureInfo.InvariantCulture),
 3205                    vertex.Y.ToString("F6", CultureInfo.InvariantCulture),
 3206                    vertex.Z.ToString("F6", CultureInfo.InvariantCulture));
 207            }
 216208        }
 209
 210        private static void WriteTextureVertices(ObjFile obj, StreamWriter stream)
 211        {
 450212            foreach (var vertex in obj.TextureVertices)
 213            {
 9214                stream.WriteLine(
 9215                    "vt {0} {1} {2}",
 9216                    vertex.X.ToString("F6", CultureInfo.InvariantCulture),
 9217                    vertex.Y.ToString("F6", CultureInfo.InvariantCulture),
 9218                    vertex.Z.ToString("F6", CultureInfo.InvariantCulture));
 219            }
 216220        }
 221
 222        private static void WriteSurfaceConnection(ObjFile obj, StreamWriter stream)
 223        {
 438224            foreach (ObjSurfaceConnection con in obj.SurfaceConnections)
 225            {
 3226                stream.WriteLine("con {0} {1} {2} {3}",
 3227                    con.Surface1.ToString(CultureInfo.InvariantCulture),
 3228                    con.Curve2D1.ToString(),
 3229                    con.Surface2.ToString(CultureInfo.InvariantCulture),
 3230                    con.Curve2D2.ToString());
 231            }
 216232        }
 233
 234        private static void WritePoints(ObjFile obj, StreamWriter stream, ObjFileWriterContext context)
 235        {
 594236            foreach (ObjPoint point in obj.Points)
 237            {
 102238                context.WriteGroupNames(stream, point, g => g.Points);
 81239                context.WriteAttributesOfElement(stream, point);
 81240                context.WriteAttributesOfPolygonalElement(stream, point);
 241
 81242                stream.Write("p");
 243
 330244                foreach (ObjTriplet vertex in point.Vertices)
 245                {
 84246                    stream.Write(" {0}", vertex);
 247                }
 248
 81249                stream.WriteLine();
 250            }
 216251        }
 252
 253        private static void WriteLines(ObjFile obj, StreamWriter stream, ObjFileWriterContext context)
 254        {
 438255            foreach (ObjLine line in obj.Lines)
 256            {
 6257                context.WriteGroupNames(stream, line, g => g.Lines);
 3258                context.WriteAttributesOfElement(stream, line);
 3259                context.WriteAttributesOfPolygonalElement(stream, line);
 260
 3261                stream.Write("l");
 262
 24263                foreach (ObjTriplet vertex in line.Vertices)
 264                {
 9265                    stream.Write(" {0}", vertex);
 266                }
 267
 3268                stream.WriteLine();
 269            }
 216270        }
 271
 272        private static void WriteFaces(ObjFile obj, StreamWriter stream, ObjFileWriterContext context)
 273        {
 438274            foreach (ObjFace face in obj.Faces)
 275            {
 6276                context.WriteGroupNames(stream, face, g => g.Faces);
 3277                context.WriteAttributesOfElement(stream, face);
 3278                context.WriteAttributesOfPolygonalElement(stream, face);
 279
 3280                stream.Write("f");
 281
 36282                foreach (ObjTriplet vertex in face.Vertices)
 283                {
 15284                    stream.Write(" {0}", vertex);
 285                }
 286
 3287                stream.WriteLine();
 288            }
 216289        }
 290
 291        private static void WriteCurves(ObjFile obj, StreamWriter stream, ObjFileWriterContext context)
 292        {
 636293            foreach (ObjCurve curve in obj.Curves)
 294            {
 105295                context.WriteGroupNames(stream, curve, g => g.Curves);
 102296                context.WriteAttributesOfElement(stream, curve);
 102297                context.WriteAttributesOfFreeFormElement(stream, curve);
 298
 102299                stream.Write("curv {0} {1}",
 102300                    curve.StartParameter.ToString("F6", CultureInfo.InvariantCulture),
 102301                    curve.EndParameter.ToString("F6", CultureInfo.InvariantCulture));
 302
 816303                foreach (int vertex in curve.Vertices)
 304                {
 306305                    stream.Write(' ');
 306306                    stream.Write(vertex);
 307                }
 308
 102309                stream.WriteLine();
 310
 102311                WriteBodyOfFreeFormElement(stream, curve);
 312            }
 216313        }
 314
 315        private static void WriteCurves2D(ObjFile obj, StreamWriter stream, ObjFileWriterContext context)
 316        {
 438317            foreach (ObjCurve2D curve in obj.Curves2D)
 318            {
 6319                context.WriteGroupNames(stream, curve, g => g.Curves2D);
 3320                context.WriteAttributesOfElement(stream, curve);
 3321                context.WriteAttributesOfFreeFormElement(stream, curve);
 322
 3323                stream.Write("curv2");
 324
 24325                foreach (int vertex in curve.ParameterSpaceVertices)
 326                {
 9327                    stream.Write(' ');
 9328                    stream.Write(vertex);
 329                }
 330
 3331                stream.WriteLine();
 332
 3333                WriteBodyOfFreeFormElement(stream, curve);
 334            }
 216335        }
 336
 337        private static void WriteSurfaces(ObjFile obj, StreamWriter stream, ObjFileWriterContext context)
 338        {
 438339            foreach (ObjSurface surface in obj.Surfaces)
 340            {
 6341                context.WriteGroupNames(stream, surface, g => g.Surfaces);
 3342                context.WriteAttributesOfElement(stream, surface);
 3343                context.WriteAttributesOfFreeFormElement(stream, surface);
 344
 3345                stream.Write("surf {0} {1} {2} {3}",
 3346                    surface.StartParameterU.ToString("F6", CultureInfo.InvariantCulture),
 3347                    surface.EndParameterU.ToString("F6", CultureInfo.InvariantCulture),
 3348                    surface.StartParameterV.ToString("F6", CultureInfo.InvariantCulture),
 3349                    surface.EndParameterV.ToString("F6", CultureInfo.InvariantCulture));
 350
 24351                foreach (ObjTriplet vertex in surface.Vertices)
 352                {
 9353                    stream.Write(' ');
 9354                    stream.Write(vertex);
 355                }
 356
 3357                stream.WriteLine();
 358
 3359                WriteBodyOfFreeFormElement(stream, surface);
 360            }
 216361        }
 362
 363        private static void WriteBodyOfFreeFormElement(StreamWriter stream, ObjFreeFormElement element)
 364        {
 108365            bool writeEnd = false;
 366
 108367            if (element.ParametersU.Count != 0)
 368            {
 3369                writeEnd = true;
 3370                stream.Write("parm u");
 371
 24372                foreach (float value in element.ParametersU)
 373                {
 9374                    stream.Write(' ');
 9375                    stream.Write(value.ToString("F6", CultureInfo.InvariantCulture));
 376                }
 377
 3378                stream.WriteLine();
 379            }
 380
 108381            if (element.ParametersV.Count != 0)
 382            {
 3383                writeEnd = true;
 3384                stream.Write("parm v");
 385
 24386                foreach (float value in element.ParametersV)
 387                {
 9388                    stream.Write(' ');
 9389                    stream.Write(value.ToString("F6", CultureInfo.InvariantCulture));
 390                }
 391
 3392                stream.WriteLine();
 393            }
 394
 108395            if (element.OuterTrimmingCurves.Count != 0)
 396            {
 3397                writeEnd = true;
 3398                stream.Write("trim");
 399
 18400                foreach (ObjCurveIndex index in element.OuterTrimmingCurves)
 401                {
 6402                    stream.Write(' ');
 6403                    stream.Write(index);
 404                }
 405
 3406                stream.WriteLine();
 407            }
 408
 108409            if (element.InnerTrimmingCurves.Count != 0)
 410            {
 3411                writeEnd = true;
 3412                stream.Write("hole");
 413
 18414                foreach (ObjCurveIndex index in element.InnerTrimmingCurves)
 415                {
 6416                    stream.Write(' ');
 6417                    stream.Write(index);
 418                }
 419
 3420                stream.WriteLine();
 421            }
 422
 108423            if (element.SequenceCurves.Count != 0)
 424            {
 3425                writeEnd = true;
 3426                stream.Write("scrv");
 427
 18428                foreach (ObjCurveIndex index in element.SequenceCurves)
 429                {
 6430                    stream.Write(' ');
 6431                    stream.Write(index);
 432                }
 433
 3434                stream.WriteLine();
 435            }
 436
 108437            if (element.SpecialPoints.Count != 0)
 438            {
 3439                writeEnd = true;
 3440                stream.Write("sp");
 441
 18442                foreach (int point in element.SpecialPoints)
 443                {
 6444                    stream.Write(' ');
 6445                    stream.Write(point);
 446                }
 447
 3448                stream.WriteLine();
 449            }
 450
 108451            if (writeEnd)
 452            {
 18453                stream.WriteLine("end");
 454            }
 108455        }
 456    }
 457}
 458

Methods/Properties

Write(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteHeaderText(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteShadowObjectFileName(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteTraceObjectFileName(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteMapLibraries(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteMaterialLibraries(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteVertices(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteParameterSpaceVertices(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteVertexNormals(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteTextureVertices(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WriteSurfaceConnection(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter)
WritePoints(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter,JeremyAnsel.Media.WavefrontObj.ObjFileWriterContext)
WriteLines(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter,JeremyAnsel.Media.WavefrontObj.ObjFileWriterContext)
WriteFaces(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter,JeremyAnsel.Media.WavefrontObj.ObjFileWriterContext)
WriteCurves(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter,JeremyAnsel.Media.WavefrontObj.ObjFileWriterContext)
WriteCurves2D(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter,JeremyAnsel.Media.WavefrontObj.ObjFileWriterContext)
WriteSurfaces(JeremyAnsel.Media.WavefrontObj.ObjFile,System.IO.StreamWriter,JeremyAnsel.Media.WavefrontObj.ObjFileWriterContext)
WriteBodyOfFreeFormElement(System.IO.StreamWriter,JeremyAnsel.Media.WavefrontObj.ObjFreeFormElement)