<commit_before>// Copyright © 2015 <NAME> <<EMAIL>>
// License: MIT, see the LICENSE file.

#include <fstream>
#include <map>
#include <cstdint>
#include <stdexcept>

#include "Utils/PlyReader.h"
#include "App.h"
#include "Utils/Log.h"
#include "Math/Vector2.h"
#include "Math/Vector3.h"
#include "Raytracing/Primitives/Triangle.h"

using namespace Raycer;

namespace
{
	enum class PlyType
	{
		INT8,
		UINT8,
		INT16,
		UINT16,
		INT32,
		UINT32,
		FLOAT,
		DOUBLE,
		LIST,
		NONE
	};

	struct PlyProperty
	{
		PlyType dataType = PlyType::NONE;
		std::string name;
		PlyType listLengthType = PlyType::NONE;
		PlyType listDataType = PlyType::NONE;
	};

	struct PlyElement
	{
		std::string name;
		int count = 0;
		std::vector<PlyProperty> properties;
	};

	struct PlyHeader
	{
		bool isAscii = true;
		bool hasTexcoords = false;
		bool hasNormals = false;
		std::vector<PlyElement> elements;
	};

	PlyType convertTextToType(const std::string& text)
	{
		if (text == "char" || text == "int8")
			return PlyType::INT8;
		else if (text == "uchar" || text == "uint8")
			return PlyType::UINT8;
		else if (text == "short" || text == "int16")
			return PlyType::INT16;
		else if (text == "ushort" || text == "uint16")
			return PlyType::UINT16;
		else if (text == "int" || text == "int32")
			return PlyType::INT32;
		else if (text == "uint" || text == "uint32")
			return PlyType::UINT32;
		else if (text == "float" || text == "float32")
			return PlyType::FLOAT;
		else if (text == "double" || text == "float64")
			return PlyType::DOUBLE;
		else if (text == "list")
			return PlyType::LIST;
		else
			return PlyType::NONE;
	}

	double readBinaryValueAsDouble(std::ifstream& inputStream, PlyType type)
	{
		int8_t tempInt8;
		uint8_t tempUint8;
		int16_t tempInt16;
		uint16_t tempUint16;
		int32_t tempInt32;
		uint32_t tempUint32;
		float tempFloat;
		double tempDouble;

		double returnValue;

		switch (type)
		{
			case PlyType::INT8: inputStream.read((char*)&tempInt8, sizeof(int8_t)); returnValue = (double)tempInt8; break;
			case PlyType::UINT8: inputStream.read((char*)&tempUint8, sizeof(uint8_t)); returnValue = (double)tempUint8; break;
			case PlyType::INT16: inputStream.read((char*)&tempInt16, sizeof(int16_t)); returnValue = (double)tempInt16; break;
			case PlyType::UINT16: inputStream.read((char*)&tempUint16, sizeof(uint16_t)); returnValue = (double)tempUint16; break;
			case PlyType::INT32: inputStream.read((char*)&tempInt32, sizeof(int32_t)); returnValue = (double)tempInt32; break;
			case PlyType::UINT32: inputStream.read((char*)&tempUint32, sizeof(uint32_t)); returnValue = (double)tempUint32; break;
			case PlyType::FLOAT: inputStream.read((char*)&tempFloat, sizeof(float)); returnValue = (double)tempFloat; break;
			case PlyType::DOUBLE: inputStream.read((char*)&tempDouble, sizeof(double)); returnValue = (double)tempDouble; break;
			default: returnValue = 0.0;
		}

		return returnValue;
	}

	int readBinaryValueAsInt(std::ifstream& inputStream, PlyType type)
	{
		int8_t tempInt8;
		uint8_t tempUint8;
		int16_t tempInt16;
		uint16_t tempUint16;
		int32_t tempInt32;
		uint32_t tempUint32;
		float tempFloat;
		double tempDouble;

		int returnValue;

		switch (type)
		{
			case PlyType::INT8: inputStream.read((char*)&tempInt8, sizeof(int8_t)); returnValue = (int)tempInt8; break;
			case PlyType::UINT8: inputStream.read((char*)&tempUint8, sizeof(uint8_t)); returnValue = (int)tempUint8; break;
			case PlyType::INT16: inputStream.read((char*)&tempInt16, sizeof(int16_t)); returnValue = (int)tempInt16; break;
			case PlyType::UINT16: inputStream.read((char*)&tempUint16, sizeof(uint16_t)); returnValue = (int)tempUint16; break;
			case PlyType::INT32: inputStream.read((char*)&tempInt32, sizeof(int32_t)); returnValue = (int)tempInt32; break;
			case PlyType::UINT32: inputStream.read((char*)&tempUint32, sizeof(uint32_t)); returnValue = (int)tempUint32; break;
			case PlyType::FLOAT: inputStream.read((char*)&tempFloat, sizeof(float)); returnValue = (int)tempFloat; break;
			case PlyType::DOUBLE: inputStream.read((char*)&tempDouble, sizeof(double)); returnValue = (int)tempDouble; break;
			default: returnValue = 0;
		}

		return returnValue;
	}

	void readHeader(std::ifstream& inputStream, PlyHeader& header)
	{
		std::string line;
		std::getline(inputStream, line);
		int elementIndex = -1;

		if (line != "ply")
			throw std::runtime_error("Not a PLY file");

		while (std::getline(inputStream, line))
		{
			std::string part;
			std::istringstream iss(line);
			iss >> part;

			if (part == "format")
			{
				iss >> part;

				if (part == "ascii")
					header.isAscii = true;
				else if (part == "binary_little_endian" || part == "binary_big_endian")
					header.isAscii = false;
				else
					throw std::runtime_error("Unknown PLY format");

				iss >> part;

				if (part != "1.0")<commit_msg>Fixed a readline bug in PlyReader<commit_after>// Copyright © 2015 <NAME> <<EMAIL>>
// License: MIT, see the LICENSE file.

#include <fstream>
#include <map>
#include <cstdint>
#include <stdexcept>

#include "Utils/PlyReader.h"
#include "App.h"
#include "Utils/Log.h"
#include "Math/Vector2.h"
#include "Math/Vector3.h"
#include "Raytracing/Primitives/Triangle.h"

using namespace Raycer;

namespace
{
	enum class PlyType
	{
		INT8,
		UINT8,
		INT16,
		UINT16,
		INT32,
		UINT32,
		FLOAT,
		DOUBLE,
		LIST,
		NONE
	};

	struct PlyProperty
	{
		PlyType dataType = PlyType::NONE;
		std::string name;
		PlyType listLengthType = PlyType::NONE;
		PlyType listDataType = PlyType::NONE;
	};

	struct PlyElement
	{
		std::string name;
		int count = 0;
		std::vector<PlyProperty> properties;
	};

	struct PlyHeader
	{
		bool isAscii = true;
		bool hasTexcoords = false;
		bool hasNormals = false;
		std::vector<PlyElement> elements;
	};

	PlyType convertTextToType(const std::string& text)
	{
		if (text == "char" || text == "int8")
			return PlyType::INT8;
		else if (text == "uchar" || text == "uint8")
			return PlyType::UINT8;
		else if (text == "short" || text == "int16")
			return PlyType::INT16;
		else if (text == "ushort" || text == "uint16")
			return PlyType::UINT16;
		else if (text == "int" || text == "int32")
			return PlyType::INT32;
		else if (text == "uint" || text == "uint32")
			return PlyType::UINT32;
		else if (text == "float" || text == "float32")
			return PlyType::FLOAT;
		else if (text == "double" || text == "float64")
			return PlyType::DOUBLE;
		else if (text == "list")
			return PlyType::LIST;
		else
			return PlyType::NONE;
	}

	double readBinaryValueAsDouble(std::ifstream& inputStream, PlyType type)
	{
		int8_t tempInt8;
		uint8_t tempUint8;
		int16_t tempInt16;
		uint16_t tempUint16;
		int32_t tempInt32;
		uint32_t tempUint32;
		float tempFloat;
		double tempDouble;

		double returnValue;

		switch (type)
		{
			case PlyType::INT8: inputStream.read((char*)&tempInt8, sizeof(int8_t)); returnValue = (double)tempInt8; break;
			case PlyType::UINT8: inputStream.read((char*)&tempUint8, sizeof(uint8_t)); returnValue = (double)tempUint8; break;
			case PlyType::INT16: inputStream.read((char*)&tempInt16, sizeof(int16_t)); returnValue = (double)tempInt16; break;
			case PlyType::UINT16: inputStream.read((char*)&tempUint16, sizeof(uint16_t)); returnValue = (double)tempUint16; break;
			case PlyType::INT32: inputStream.read((char*)&tempInt32, sizeof(int32_t)); returnValue = (double)tempInt32; break;
			case PlyType::UINT32: inputStream.read((char*)&tempUint32, sizeof(uint32_t)); returnValue = (double)tempUint32; break;
			case PlyType::FLOAT: inputStream.read((char*)&tempFloat, sizeof(float)); returnValue = (double)tempFloat; break;
			case PlyType::DOUBLE: inputStream.read((char*)&tempDouble, sizeof(double)); returnValue = (double)tempDouble; break;
			default: returnValue = 0.0;
		}

		return returnValue;
	}

	int readBinaryValueAsInt(std::ifstream& inputStream, PlyType type)
	{
		int8_t tempInt8;
		uint8_t tempUint8;
		int16_t tempInt16;
		uint16_t tempUint16;
		int32_t tempInt32;
		uint32_t tempUint32;
		float tempFloat;
		double tempDouble;

		int returnValue;

		switch (type)
		{
			case PlyType::INT8: inputStream.read((char*)&tempInt8, sizeof(int8_t)); returnValue = (int)tempInt8; break;
			case PlyType::UINT8: inputStream.read((char*)&tempUint8, sizeof(uint8_t)); returnValue = (int)tempUint8; break;
			case PlyType::INT16: inputStream.read((char*)&tempInt16, sizeof(int16_t)); returnValue = (int)tempInt16; break;
			case PlyType::UINT16: inputStream.read((char*)&tempUint16, sizeof(uint16_t)); returnValue = (int)tempUint16; break;
			case PlyType::INT32: inputStream.read((char*)&tempInt32, sizeof(int32_t)); returnValue = (int)tempInt32; break;
			case PlyType::UINT32: inputStream.read((char*)&tempUint32, sizeof(uint32_t)); returnValue = (int)tempUint32; break;
			case PlyType::FLOAT: inputStream.read((char*)&tempFloat, sizeof(float)); returnValue = (int)tempFloat; break;
			case PlyType::DOUBLE: inputStream.read((char*)&tempDouble, sizeof(double)); returnValue = (int)tempDouble; break;
			default: returnValue = 0;
		}

		return returnValue;
	}

	void readHeader(std::ifstream& inputStream, PlyHeader& header)
	{
		std::string line;
		std::getline(inputStream, line);
		int elementIndex = -1;

		if (line.find("ply") != 0)
			throw std::runtime_error("Not a PLY file");

		while (std::getline(inputStream, line))
		{
			std::string part;
			std::istringstream iss(line);
			iss >> part;

			if (part == "format")
			{
				iss >> part;

				if (part == "ascii")
					header.isAscii = true;
				else if (part == "binary_little_endian" || part == "binary_big_endian")
					header.isAscii = false;
				else
					throw std::runtime_error("Unknown PLY format");

				iss >> part;

				if (part != "1.0")
{'fraction_non_alphanumeric': 0.1475494233937397, 'fraction_numerical': 0.03449341021416804, 'mean_word_length': 4.861798430899215, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, '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': '3184363', 'n_tokens_mistral': 4008, 'n_tokens_neox': 3433, 'n_words': 782}