<commit_before>// mesh-gen.ts - mesh generators
// Part of Stardazed TX
// (c) 2015 by <NAME> - @zenmumbler

/// <reference path="meshdata.ts" />
/// <reference path="mesh-manip.ts" />


namespace sd.mesh.gen {

	//  __  __        _    ___                       _           
	// |  \/  |___ __| |_ / __|___ _ _  ___ _ _ __ _| |_ ___ _ _ 
	// | |\/| / -_|_-< ' \ (_ / -_) ' \/ -_) '_/ _` |  _/ _ \ '_|
	// |_|  |_\___/__/_||_\___\___|_||_\___|_| \__,_|\__\___/_|  
	//                                                          

	export type Vec2AddFn = (u: number, v: number) => void;
	export type Vec3AddFn = (x: number, y: number, z: number) => void;
	export type IndexesAddFn = (a: number, b: number, c: number) => void;

	export interface MeshGenerator {
		vertexCount: number;
		faceCount: number;

		explicitNormals: boolean;

		generate(position: Vec3AddFn, face: IndexesAddFn, normal: Vec3AddFn, uv: Vec2AddFn): void;
	}

	export interface TransformedMeshGen {
		generator: MeshGenerator;
		rotation?: Float4; // quat
		translation?: Float3; // vec3
		scale?: Float3; // vec3
	}

	export type MeshGenSource = MeshGenerator | TransformedMeshGen;


	export function generate(gens: MeshGenSource | MeshGenSource[], attrList?: VertexAttribute[]): MeshData {
		if (! attrList)
			attrList = AttrList.Pos3Norm3UV2();

		var genList = <MeshGenSource[]>seq(gens);
		var totalVertexCount = 0;
		var totalFaceCount = 0;

		for (var genSource of genList) {
			var generator: MeshGenerator = ("generator" in genSource) ? (<TransformedMeshGen>genSource).generator : <MeshGenerator>genSource;
			totalVertexCount += generator.vertexCount;
			totalFaceCount += generator.faceCount;
		}

		// -- create vertex and index buffers for combined mesh
		var mesh = new MeshData(attrList);
		var vertexBuffer = mesh.primaryVertexBuffer;

		vertexBuffer.allocate(totalVertexCount);
		var indexElementType = minimumIndexElementTypeForVertexCount(totalVertexCount);
		mesh.indexBuffer.allocate(PrimitiveType.Triangle, indexElementType, totalFaceCount);

		// -- views into various attributes and the index buffer
		var normalAttr = vertexBuffer.attrByRole(VertexAttributeRole.Normal);
		var texAttr = vertexBuffer.attrByRole(VertexAttributeRole.UV);

		var posView = new VertexBufferAttributeView(vertexBuffer, vertexBuffer.attrByRole(VertexAttributeRole.Position));
		var normalView = normalAttr ? new VertexBufferAttributeView(vertexBuffer, normalAttr) : null;
		var texView = texAttr ? new VertexBufferAttributeView(vertexBuffer, texAttr) : null;

		var triView = new IndexBufferTriangleView(mesh.indexBuffer);

		// -- data add functions for the generators
		var posIx = 0, faceIx = 0, normalIx = 0, uvIx = 0, baseVertex = 0;

		var pos2: Vec3AddFn = (x: number, y: number, z: number) => {
			var v2 = posView.refItem(posIx);
			v2[0] = x; v2[1] = y;
			posIx++;
		};

		var pos3: Vec3AddFn = (x: number, y: number, z: number) => {
			var v3 = posView.refItem(posIx);
			v3[0] = x; v3[1] = y; v3[2] = z;
			posIx++;
		};

		var pos = posView.elementCount == 2 ? pos2 : pos3;

		var face: IndexesAddFn = (a: number, b: number, c: number) => {
			var i3 = triView.item(faceIx);
			i3[0] = a + baseVertex; i3[1] = b + baseVertex; i3[2] = c + baseVertex;
			faceIx++;
		};

		var normal: Vec3AddFn = normalView ?
			(x: number, y: number, z: number) => {
				var v3 = normalView.refItem(normalIx);
				v3[0] = x; v3[1] = y; v3[2] = z;
				normalIx++;
			}
			: (x: number, y: number, z: number) => { };

		var uv: Vec2AddFn = texView ?
			(u: number, v: number) => {
				var v2 = texView.refItem(uvIx);
				v2[0] = u; v2[1] = v;
				uvIx++;
			}
			: (u: number, v: number) => { };<commit_msg>mesh-gen: assert non-null-ness in callbacks<commit_after>// mesh-gen.ts - mesh generators
// Part of Stardazed TX
// (c) 2015 by <NAME> - @zenmumbler

/// <reference path="meshdata.ts" />
/// <reference path="mesh-manip.ts" />


namespace sd.mesh.gen {

	//  __  __        _    ___                       _           
	// |  \/  |___ __| |_ / __|___ _ _  ___ _ _ __ _| |_ ___ _ _ 
	// | |\/| / -_|_-< ' \ (_ / -_) ' \/ -_) '_/ _` |  _/ _ \ '_|
	// |_|  |_\___/__/_||_\___\___|_||_\___|_| \__,_|\__\___/_|  
	//                                                          

	export type Vec2AddFn = (u: number, v: number) => void;
	export type Vec3AddFn = (x: number, y: number, z: number) => void;
	export type IndexesAddFn = (a: number, b: number, c: number) => void;

	export interface MeshGenerator {
		vertexCount: number;
		faceCount: number;

		explicitNormals: boolean;

		generate(position: Vec3AddFn, face: IndexesAddFn, normal: Vec3AddFn, uv: Vec2AddFn): void;
	}

	export interface TransformedMeshGen {
		generator: MeshGenerator;
		rotation?: Float4; // quat
		translation?: Float3; // vec3
		scale?: Float3; // vec3
	}

	export type MeshGenSource = MeshGenerator | TransformedMeshGen;


	export function generate(gens: MeshGenSource | MeshGenSource[], attrList?: VertexAttribute[]): MeshData {
		if (! attrList)
			attrList = AttrList.Pos3Norm3UV2();

		var genList = <MeshGenSource[]>seq(gens);
		var totalVertexCount = 0;
		var totalFaceCount = 0;

		for (var genSource of genList) {
			var generator: MeshGenerator = ("generator" in genSource) ? (<TransformedMeshGen>genSource).generator : <MeshGenerator>genSource;
			totalVertexCount += generator.vertexCount;
			totalFaceCount += generator.faceCount;
		}

		// -- create vertex and index buffers for combined mesh
		var mesh = new MeshData(attrList);
		var vertexBuffer = mesh.primaryVertexBuffer;

		vertexBuffer.allocate(totalVertexCount);
		var indexElementType = minimumIndexElementTypeForVertexCount(totalVertexCount);
		mesh.indexBuffer.allocate(PrimitiveType.Triangle, indexElementType, totalFaceCount);

		// -- views into various attributes and the index buffer
		var normalAttr = vertexBuffer.attrByRole(VertexAttributeRole.Normal);
		var texAttr = vertexBuffer.attrByRole(VertexAttributeRole.UV);

		var posView = new VertexBufferAttributeView(vertexBuffer, vertexBuffer.attrByRole(VertexAttributeRole.Position));
		var normalView = normalAttr ? new VertexBufferAttributeView(vertexBuffer, normalAttr) : null;
		var texView = texAttr ? new VertexBufferAttributeView(vertexBuffer, texAttr) : null;

		var triView = new IndexBufferTriangleView(mesh.indexBuffer);

		// -- data add functions for the generators
		var posIx = 0, faceIx = 0, normalIx = 0, uvIx = 0, baseVertex = 0;

		var pos2: Vec3AddFn = (x: number, y: number, z: number) => {
			var v2 = posView.refItem(posIx);
			v2[0] = x; v2[1] = y;
			posIx++;
		};

		var pos3: Vec3AddFn = (x: number, y: number, z: number) => {
			var v3 = posView.refItem(posIx);
			v3[0] = x; v3[1] = y; v3[2] = z;
			posIx++;
		};

		var pos = posView.elementCount == 2 ? pos2 : pos3;

		var face: IndexesAddFn = (a: number, b: number, c: number) => {
			var i3 = triView.item(faceIx);
			i3[0] = a + baseVertex; i3[1] = b + baseVertex; i3[2] = c + baseVertex;
			faceIx++;
		};

		var normal: Vec3AddFn = normalView ?
			(x: number, y: number, z: number) => {
				var v3 = normalView!.refItem(normalIx);
				v3[0] = x; v3[1] = y; v3[2] = z;
				normalIx++;
			}
			: (x: number, y: number, z: number) => { };

		var uv: Vec2AddFn = texView ?
			(u: number, v: number) => {
				var v2 = texView!.refItem(uvIx);
				v2[0] = u; v2[1] = v;
				uvIx++;
			}
			: (u: number, v: number) => { };
{'fraction_non_alphanumeric': 0.14336043360433603, 'fraction_numerical': 0.017344173441734417, 'mean_word_length': 3.165349887133183, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2306321', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2562, 'n_words': 620}