<commit_before>'use strict';
window.ThreeBSP = (function() {
	
	var ThreeBSP,
		EPSILON = 1e-5,
		COPLANAR = 0,
		FRONT = 1,
		BACK = 2,
		SPANNING = 3;
	
	ThreeBSP = function( geometry ) {
		// Convert THREE.Geometry to ThreeBSP
		var i, _length_i,
			face, vertex,
			polygon,
			polygons = [],
			tree;
		
		if ( geometry instanceof THREE.Geometry ) {
			this.matrix = new THREE.Matrix4;
		} else if ( geometry instanceof THREE.Mesh ) {
			// #todo: add hierarchy support
			geometry.updateMatrix();
			this.matrix = geometry.matrix.clone();
			geometry = geometry.geometry;
		} else if ( geometry instanceof ThreeBSP.Node ) {
			this.tree = geometry;
			this.matrix = new THREE.Matrix4;
			return this;
		} else {
			throw 'ThreeBSP: Given geometry is unsupported';
		}
		
		for ( i = 0, _length_i = geometry.faces.length; i < _length_i; i++ ) {
			face = geometry.faces[i];
			polygon = new ThreeBSP.Polygon;
			
			if ( face instanceof THREE.Face3 ) {
				vertex = geometry.vertices[ face.a ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.b ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.c ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
			} else if ( typeof THREE.Face4 ) {
				vertex = geometry.vertices[ face.a ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.b ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.c ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.d ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
			} else {
				throw 'Invalid face type at index ' + i;
			}
			
			polygon.calculateProperties();
			polygons.push( polygon );
		};
		
		this.tree = new ThreeBSP.Node( polygons );
	};
	ThreeBSP.prototype.subtract = function( other_tree ) {
		var a = this.tree.clone(),
			b = other_tree.tree.clone();
		
		a.invert();
		a.clipTo( b );
		b.clipTo( a );
		b.invert();
		b.clipTo( a );
		b.invert();
		a.build( b.allPolygons() );
		a.invert();
		a = new ThreeBSP( a );
		a.matrix = this.matrix;
		return a;
	};
	ThreeBSP.prototype.union = function( other_tree ) {
		var a = this.tree.clone(),
			b = other_tree.tree.clone();
		
		a.clipTo( b );
		b.clipTo( a );
		b.invert();
		b.clipTo( a );
		b.invert();
		a.build( b.allPolygons() );
		a = new ThreeBSP( a );
		a.matrix = this.matrix;
		return a;
	};
	ThreeBSP.prototype.intersect = function( other_tree ) {
		var a = this.tree.clone(),
			b = other_tree.tree.clone();
		
		a.invert();
		b.clipTo( a );
		b.invert();
		a.clipTo( b );
		b.clipTo( a );
		a.build( b.allPolygons() );
		a.invert();
		a = new ThreeBSP( a );
		a.matrix = this.matrix;
		return a;
	};
	ThreeBSP.prototype.toGeometry = function() {
		var i, j,
			matrix = new THREE.Matrix4().getInverse( this.matrix ),
			geometry = new THREE.Geometry(),
			polygons = this.tree.allPolygons(),
			polygon_count = polygons.length,
			polygon, polygon_vertice_count,
			vertex, face;
		
		for ( i = 0; i < polygon_count; i++ ) {
			polygon = polygons[i];
			polygon_vertice_count = polygon.vertices.length;
			
			for ( j = 2; j < polygon_vertice_count; j++ ) {
				vertex = polygon.vertices[0];
				vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
				matrix.multiplyVector3( vertex );
				geometry.vertices.push( vertex );
				
				vertex = polygon.vertices[j-1];
				vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
				matrix.multiplyVector3( vertex );
				geometry.vertices.push( vertex );
				
				vertex = polygon.vertices[j];
				vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
				matrix.multiplyVector3( vertex );
				geometry.vertices.push( vertex );
				
				face = new THREE.Face3(
					geometry.vertices.length - 3,
					geometry.vertices.length - 2,
					geometry.vertices.length - 1,
					new THREE.Vector3( polygon.normal.x, polygon.normal.y, polygon.normal.z )
				);
				
				geometry.faces.push( face );
			}
			
		}
		
		return geometry;
	};
	ThreeBSP.prototype.toMesh = function( material ) {
		var geometry = this.toGeometry(),
			mesh = new THREE.Mesh( geometry, material );
		<commit_msg>Share vertices between faces on geometry creation<commit_after>'use strict';
window.ThreeBSP = (function() {
	
	var ThreeBSP,
		EPSILON = 1e-5,
		COPLANAR = 0,
		FRONT = 1,
		BACK = 2,
		SPANNING = 3;
	
	ThreeBSP = function( geometry ) {
		// Convert THREE.Geometry to ThreeBSP
		var i, _length_i,
			face, vertex,
			polygon,
			polygons = [],
			tree;
		
		if ( geometry instanceof THREE.Geometry ) {
			this.matrix = new THREE.Matrix4;
		} else if ( geometry instanceof THREE.Mesh ) {
			// #todo: add hierarchy support
			geometry.updateMatrix();
			this.matrix = geometry.matrix.clone();
			geometry = geometry.geometry;
		} else if ( geometry instanceof ThreeBSP.Node ) {
			this.tree = geometry;
			this.matrix = new THREE.Matrix4;
			return this;
		} else {
			throw 'ThreeBSP: Given geometry is unsupported';
		}
		
		for ( i = 0, _length_i = geometry.faces.length; i < _length_i; i++ ) {
			face = geometry.faces[i];
			polygon = new ThreeBSP.Polygon;
			
			if ( face instanceof THREE.Face3 ) {
				vertex = geometry.vertices[ face.a ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.b ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.c ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
			} else if ( typeof THREE.Face4 ) {
				vertex = geometry.vertices[ face.a ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.b ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.c ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
				
				vertex = geometry.vertices[ face.d ];
				vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z );
				this.matrix.multiplyVector3( vertex );
				polygon.vertices.push( vertex );
			} else {
				throw 'Invalid face type at index ' + i;
			}
			
			polygon.calculateProperties();
			polygons.push( polygon );
		};
		
		this.tree = new ThreeBSP.Node( polygons );
	};
	ThreeBSP.prototype.subtract = function( other_tree ) {
		var a = this.tree.clone(),
			b = other_tree.tree.clone();
		
		a.invert();
		a.clipTo( b );
		b.clipTo( a );
		b.invert();
		b.clipTo( a );
		b.invert();
		a.build( b.allPolygons() );
		a.invert();
		a = new ThreeBSP( a );
		a.matrix = this.matrix;
		return a;
	};
	ThreeBSP.prototype.union = function( other_tree ) {
		var a = this.tree.clone(),
			b = other_tree.tree.clone();
		
		a.clipTo( b );
		b.clipTo( a );
		b.invert();
		b.clipTo( a );
		b.invert();
		a.build( b.allPolygons() );
		a = new ThreeBSP( a );
		a.matrix = this.matrix;
		return a;
	};
	ThreeBSP.prototype.intersect = function( other_tree ) {
		var a = this.tree.clone(),
			b = other_tree.tree.clone();
		
		a.invert();
		b.clipTo( a );
		b.invert();
		a.clipTo( b );
		b.clipTo( a );
		a.build( b.allPolygons() );
		a.invert();
		a = new ThreeBSP( a );
		a.matrix = this.matrix;
		return a;
	};
	ThreeBSP.prototype.toGeometry = function() {
		var i, j,
			matrix = new THREE.Matrix4().getInverse( this.matrix ),
			geometry = new THREE.Geometry(),
			polygons = this.tree.allPolygons(),
			polygon_count = polygons.length,
			polygon, polygon_vertice_count,
			vertice_dict = {},
			vertex_idx_a, vertex_idx_b, vertex_idx_c,
			vertex, face;
		
		for ( i = 0; i < polygon_count; i++ ) {
			polygon = polygons[i];
			polygon_vertice_count = polygon.vertices.length;
			
			for ( j = 2; j < polygon_vertice_count; j++ ) {
				vertex = polygon.vertices[0];
				vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
				matrix.multiplyVector3( vertex );
				
				if ( typeof vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] !== 'undefined' ) {
					vertex_idx_a = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ];
				} else {
					geometry.vertices.push( vertex );
					vertex_idx_a = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] = geometry.vertices.length - 1;
				}
				
				vertex = polygon.vertices[j-1];
				vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
				matrix.multiplyVector3( vertex );
				if ( typeof vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] !== 'undefined' ) {
					vertex_idx_b = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ];
				} else {
					geometry.vertices.push( vertex );
					vertex_idx_b = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] = geometry.vertices.length - 1;
				}
				
				vertex = polygon.vertices[j];
				vertex = new THREE.Vector3( vertex.x, vertex.y, vertex.z );
				matrix.multiplyVector3( vertex );
				if ( typeof vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] !== 'undefined' ) {
					vertex_idx_c = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ];
				} else {
					geometry.vertices.push( vertex );
					vertex_idx_c = vertice_dict[ vertex.x + ',' + vertex.y + ',' + vertex.z ] = geometry.vertices.length - 1;
				}
				
				face = new THREE.Face3(
					vertex_idx_a,
					vertex_idx_b,
					vertex_idx_c,
					new THREE.Vector3( polygon.normal.x, polygon.normal.y, polygon.normal.z )
				);
				
				geometry.faces.push( face );
			}
			
		}
		return geometry;
	};
	ThreeBSP.prototype.toMesh = function( material ) {
		var geometry = this.toGeometry(),
			mesh = new THREE.Mesh( geometry, material );
		
{'fraction_non_alphanumeric': 0.14735668493914336, 'fraction_numerical': 0.006317941094490384, 'mean_word_length': 2.79147587178584, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '2424096', 'n_tokens_mistral': 4600, 'n_tokens_neox': 3761, 'n_words': 827}