<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using TinyRendererLib.GeometryDataStructures.Vectors;
using TinyRendererLib.RenderObjects;

namespace GraphicsModelImporterLib.WavefrontOBJ
{
	public class OBJmodel : IModel
    {
		private const int OBJ_VERTEX_INDEX_OFFSET = 1; // OBJ vertex indices start at 1

		/// <summary>
        /// The vertices.
        /// </summary>
        private IList<Vector4f> vertices;

        /// <summary>
        /// The vertex normals.
        /// </summary>
        private IList<Vector3f> vertexNormals;

        /// <summary>
        /// List of faces.
        /// Face is defined as 3 vectors (X vector, Y vector, Z vector)
        /// Each vector is defined as (vertex index, texture coordinate index, vertex normal index)
        /// </summary>
        private IList<Tuple<Vector3i, Vector3i, Vector3i>> faces;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:TinyRendererLib.ModelFileFormats.WavefrontOBJ.OBJmodel"/> class.
        /// </summary>
        /// <param name="filePath">File path to a Wavefront OBJ model.</param>
        public OBJmodel(string filePath)
        {
            vertices = new List<Vector4f>();
            vertexNormals = new List<Vector3f>();
            faces = new List<Tuple<Vector3i, Vector3i, Vector3i>>();

            loadModelFrom(filePath);
        }

		#region IModel
		public int Vertices()
        {
			return vertices.Count;
        }

        public Vector4f Vertex(int index)
        {
			return vertices[index];
        }

        public int Faces()
        {
			return faces.Count;
        }

        public Vector4f Vertex(int faceIndex, int nthVertex)
        {
			var face = faces[faceIndex];
            if (nthVertex == 0)
            {
                return Vertex(face.Item1.x - OBJ_VERTEX_INDEX_OFFSET);
            }
            else if (nthVertex == 1)
            {
                return Vertex(face.Item2.x - OBJ_VERTEX_INDEX_OFFSET);
            }
            else
            {
                return Vertex(face.Item3.x - OBJ_VERTEX_INDEX_OFFSET);
            }
        }
		#endregion
              
		private void loadModelFrom(string filePath)
		{
			using(StreamReader sr = new StreamReader(filePath))
			{
				string line;
				while(!sr.EndOfStream)
				{
					line = sr.ReadLine();
					string[] parts = line.Split(' ');
					if (!parts.Any())
					{
						continue;
					}

                    if (parts[0] == "v" && parts.Count() >= 4) // Read vertex
					{
						float x = float.Parse(parts[1]);
						float y = float.Parse(parts[2]);
						float z = float.Parse(parts[3]);
						float w = 1.0f;
                        if (parts.Count() == 5)
						{
							w = float.Parse(parts[4]);
						}

						vertices.Add(new Vector4f(x, y, z, w));
					}
					else if (parts[0] == "f" && parts.Count() == 4) // Read face
					{
                        // Read X Part
                        Vector3i faceX = new Vector3i();
						string[] xFaceParts = parts[1].Split('/');
                        faceX.x = int.Parse(xFaceParts[0]); // vertex index
						faceX.y = xFaceParts.Count() > 1 ? int.Parse(xFaceParts[1]) : -1; // texture coordinate index
						faceX.z = xFaceParts.Count() > 2 ? int.Parse(xFaceParts[2]) : -1; // vertex normal index

                        // Read Y Part
						Vector3i faceY = new Vector3i();
                        string[] yFaceParts = parts[2].Split('/');
						faceY.x = int.Parse(yFaceParts[0]); // vertex index
						faceY.y = yFaceParts.Count() > 1 ? int.Parse(yFaceParts[1]) : -1; // texture coordinate index
						faceY.z = yFaceParts.Count() > 2 ? int.Parse(yFaceParts[2]) : -1; // vertex normal index

                        // Read Z Part
						Vector3i faceZ = new Vector3i();
                        string[] zFaceParts = parts[3].Split('/');
						faceZ.x = int.Parse(zFaceParts[0]); // vertex index
						faceZ.y = zFaceParts.Count() > 1 ? int.Parse(zFaceParts[1]) : -1; // texture coordinate index
						faceZ.z = zFaceParts.Count() > 2 ? int.Parse(zFaceParts[2]) : -1; // vertex normal index

						faces.Add(new Tuple<Vector3i, Vector3i, Vector3i>(faceX, faceY, faceZ));
					}
				}
			}
		}      
	}
}<commit_msg>Corrected comments and variable names in OBJmodel loader<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using TinyRendererLib.GeometryDataStructures.Vectors;
using TinyRendererLib.RenderObjects;

namespace GraphicsModelImporterLib.WavefrontOBJ
{
	public class OBJmodel : IModel
    {
		private const int OBJ_VERTEX_INDEX_OFFSET = 1; // OBJ vertex indices start at 1

		/// <summary>
        /// The vertices.
        /// </summary>
        private IList<Vector4f> vertices;

        /// <summary>
        /// The vertex normals.
        /// </summary>
        private IList<Vector3f> vertexNormals;

        /// <summary>
        /// List of faces.
        /// Face is defined as 3 vectors (X vector, Y vector, Z vector)
        /// Each vector is defined as (vertex index, texture coordinate index, vertex normal index)
        /// </summary>
        private IList<Tuple<Vector3i, Vector3i, Vector3i>> faces;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:TinyRendererLib.ModelFileFormats.WavefrontOBJ.OBJmodel"/> class.
        /// </summary>
        /// <param name="filePath">File path to a Wavefront OBJ model.</param>
        public OBJmodel(string filePath)
        {
            vertices = new List<Vector4f>();
            vertexNormals = new List<Vector3f>();
            faces = new List<Tuple<Vector3i, Vector3i, Vector3i>>();

            loadModelFrom(filePath);
        }

		#region IModel
		public int Vertices()
        {
			return vertices.Count;
        }

        public Vector4f Vertex(int index)
        {
			return vertices[index];
        }

        public int Faces()
        {
			return faces.Count;
        }

        public Vector4f Vertex(int faceIndex, int nthVertex)
        {
			var face = faces[faceIndex];
            if (nthVertex == 0)
            {
                return Vertex(face.Item1.x - OBJ_VERTEX_INDEX_OFFSET);
            }
            else if (nthVertex == 1)
            {
                return Vertex(face.Item2.x - OBJ_VERTEX_INDEX_OFFSET);
            }
            else
            {
                return Vertex(face.Item3.x - OBJ_VERTEX_INDEX_OFFSET);
            }
        }
		#endregion
              
		private void loadModelFrom(string filePath)
		{
			using(StreamReader sr = new StreamReader(filePath))
			{
				string line;
				while(!sr.EndOfStream)
				{
					line = sr.ReadLine();
					string[] parts = line.Split(' ');
					if (!parts.Any())
					{
						continue;
					}

                    if (parts[0] == "v" && parts.Count() >= 4) // Read vertex
					{
						float x = float.Parse(parts[1]);
						float y = float.Parse(parts[2]);
						float z = float.Parse(parts[3]);
						float w = 1.0f;
                        if (parts.Count() == 5)
						{
							w = float.Parse(parts[4]);
						}

						vertices.Add(new Vector4f(x, y, z, w));
					}
					else if (parts[0] == "f" && parts.Count() == 4) // Read face
					{
						string[] faceElementParts; // used to deconstruct face element parts

                        // Read 1st face element
						Vector3i faceElement1 = new Vector3i();
						faceElementParts = parts[1].Split('/');
						faceElement1.x = int.Parse(faceElementParts[0]); // vertex index
						faceElement1.y = faceElementParts.Count() > 1 ? int.Parse(faceElementParts[1]) : -1; // texture coordinate index
						faceElement1.z = faceElementParts.Count() > 2 ? int.Parse(faceElementParts[2]) : -1; // vertex normal index

						// Read 2nd face element
						Vector3i faceElement2 = new Vector3i();
						faceElementParts = parts[2].Split('/');
						faceElement2.x = int.Parse(faceElementParts[0]); // vertex index
						faceElement2.y = faceElementParts.Count() > 1 ? int.Parse(faceElementParts[1]) : -1; // texture coordinate index
						faceElement2.z = faceElementParts.Count() > 2 ? int.Parse(faceElementParts[2]) : -1; // vertex normal index

						// Read 3rd face element
						Vector3i faceElement3 = new Vector3i();
						faceElementParts = parts[3].Split('/');
						faceElement3.x = int.Parse(faceElementParts[0]); // vertex index
						faceElement3.y = faceElementParts.Count() > 1 ? int.Parse(faceElementParts[1]) : -1; // texture coordinate index
						faceElement3.z = faceElementParts.Count() > 2 ? int.Parse(faceElementParts[2]) : -1; // vertex normal index

						faces.Add(new Tuple<Vector3i, Vector3i, Vector3i>(faceElement1, faceElement2, faceElement3));
					}
				}
			}
		}      
	}
}
{'fraction_non_alphanumeric': 0.126357918810749, 'fraction_numerical': 0.016923956546598055, 'mean_word_length': 1.942799461641992, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3742235', 'n_tokens_mistral': 3088, 'n_tokens_neox': 2600, 'n_words': 635}