<commit_before>// asset-lwo.ts - Lightwave OBJ mesh file import
// Part of Stardazed TX
// (c) 2015-2016 by <NAME> - @zenmumbler

/// <reference path="core.ts" />
/// <reference path="meshdata.ts" />

namespace sd.asset {

	function parseLWMaterialSource(text: string): MaterialSet {
		var lines = text.split("\n");
		var materials: MaterialSet = {};
		var curMat: Material = null;

		lines.forEach(function(line) {
			var tokens = line.split(" ");
			switch (tokens[0]) {
				case "newmtl":
					curMat = materials[tokens[1]] = makeMaterial();
					break;
				case "Kd":
					curMat.diffuseColour = [parseFloat(tokens[1]), parseFloat(tokens[2]), parseFloat(tokens[3])];
					break;
				case "Ks":
					curMat.specularColour = [parseFloat(tokens[1]), parseFloat(tokens[2]), parseFloat(tokens[3])];
					break;
				default:
					break;
			}
		});

		return materials;
	}


	export interface LWDrawGroup {
		materialName: string;
		fromIndex: number;
		indexCount: number;
	}

	export interface LWMeshData {
		mtlFileName: string;
		mesh: mesh.MeshData;
		materials: MaterialSet;
		drawGroups: LWDrawGroup[];
	}


	function genColorEntriesFromDrawGroups(drawGroups: LWDrawGroup[], materials: MaterialSet, colourView: mesh.VertexBufferAttributeView) {
		drawGroups.forEach((group: LWDrawGroup) => {
			var curIndex = group.fromIndex;
			var maxIndex = group.fromIndex + group.indexCount;
			var mat = materials[group.materialName];
			assert(mat, "material " + group.materialName + " not found");

			while (curIndex < maxIndex) {
				vec3.copy(colourView.refItem(curIndex), mat.diffuseColour);
				curIndex++;
			}
		});
	}


	function parseLWObjectSource(text: string, hasColourAttr: boolean): LWMeshData {
		var lines = text.split("\n");
		var vv: number[][] = [], nn: number[][] = [], tt: number[][] = [];

		var mtlFileName = "";
		var materialGroups: LWDrawGroup[] = [];
		var curMaterialGroup: LWDrawGroup = null;

		var meshData = new mesh.MeshData(hasColourAttr ? mesh.AttrList.Pos3Norm3Colour3UV2() : mesh.AttrList.Pos3Norm3UV2());
		var vb = meshData.primaryVertexBuffer;

		var posView: mesh.VertexBufferAttributeView;
		var normView: mesh.VertexBufferAttributeView;
		var uvView: mesh.VertexBufferAttributeView;
		var vertexIx = 0;

		function vtx(vx: number, tx: number, nx: number) {
			assert(vx < vv.length, "vx out of bounds " + vx);
<commit_msg>lwo: don't assume material file is OK<commit_after>// asset-lwo.ts - Lightwave OBJ mesh file import
// Part of Stardazed TX
// (c) 2015-2016 by <NAME> - @zenmumbler

/// <reference path="core.ts" />
/// <reference path="meshdata.ts" />

namespace sd.asset {

	function parseLWMaterialSource(text: string): MaterialSet {
		var lines = text.split("\n");
		var materials: MaterialSet = {};
		var curMat: Material | null = null;

		lines.forEach(function(line) {
			var tokens = line.split(" ");
			switch (tokens[0]) {
				case "newmtl":
					curMat = materials[tokens[1]] = makeMaterial();
					break;
				case "Kd":
					if (curMat) {
						curMat.diffuseColour = [parseFloat(tokens[1]), parseFloat(tokens[2]), parseFloat(tokens[3])];
					}
					// FIXME: else unexpected()
					break;
				case "Ks":
					if (curMat) {
						curMat.specularColour = [parseFloat(tokens[1]), parseFloat(tokens[2]), parseFloat(tokens[3])];
					}
					// FIXME: else unexpected()
					break;
				default:
					break;
			}
		});

		return materials;
	}


	export interface LWDrawGroup {
		materialName: string;
		fromIndex: number;
		indexCount: number;
	}

	export interface LWMeshData {
		mtlFileName: string;
		mesh: mesh.MeshData;
		materials: MaterialSet | null;
		drawGroups: LWDrawGroup[];
	}


	function genColorEntriesFromDrawGroups(drawGroups: LWDrawGroup[], materials: MaterialSet, colourView: mesh.VertexBufferAttributeView) {
		drawGroups.forEach((group: LWDrawGroup) => {
			var curIndex = group.fromIndex;
			var maxIndex = group.fromIndex + group.indexCount;
			var mat = materials[group.materialName];
			assert(mat, "material " + group.materialName + " not found");

			while (curIndex < maxIndex) {
				vec3.copy(colourView.refItem(curIndex), mat.diffuseColour);
				curIndex++;
			}
		});
	}


	function parseLWObjectSource(text: string, hasColourAttr: boolean): LWMeshData {
		var lines = text.split("\n");
		var vv: number[][] = [], nn: number[][] = [], tt: number[][] = [];

		var mtlFileName = "";
		var materialGroups: LWDrawGroup[] = [];
		var curMaterialGroup: LWDrawGroup | null = null;

		var meshData = new mesh.MeshData(hasColourAttr ? mesh.AttrList.Pos3Norm3Colour3UV2() : mesh.AttrList.Pos3Norm3UV2());
		var vb = meshData.primaryVertexBuffer;

		var posView: mesh.VertexBufferAttributeView;
		var normView: mesh.VertexBufferAttributeView;
		var uvView: mesh.VertexBufferAttributeView;
		var vertexIx = 0;

		function vtx(vx: number, tx: number, nx: number) {
			assert(vx < vv.length, "vx out of bounds " + vx);

{'fraction_non_alphanumeric': 0.14098629015756087, 'fraction_numerical': 0.01023122570083896, 'mean_word_length': 3.9323915237134206, 'pattern_counts': {'":': 6, '<': 13, '<?xml version=': 0, '>': 11, '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': '1599719', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1587, 'n_words': 371}