From e5e15132216c239a5cc1bf96275462d7069765ae Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Mon, 29 Jun 2015 15:41:18 +0200 Subject: [PATCH] Cleaning --- geo/Geo.js | 12 ++++ geo/Makefile | 17 +++++ geo/Mesh.js | 132 ++++++++++++++++++-------------------- geo/MeshContainer.js | 46 ++++++------- geo/MeshStreamer.js | 23 +++---- js/Makefile | 2 +- socket.js | 2 +- {js => utils}/compiler.sh | 0 8 files changed, 121 insertions(+), 113 deletions(-) create mode 100644 geo/Geo.js create mode 100644 geo/Makefile rename {js => utils}/compiler.sh (100%) diff --git a/geo/Geo.js b/geo/Geo.js new file mode 100644 index 0000000..57ceded --- /dev/null +++ b/geo/Geo.js @@ -0,0 +1,12 @@ +var fs = require('fs'); + +/** + * @namespace + */ +var geo = {}; + +if (typeof module === 'object') { + + module.exports = geo; + +} diff --git a/geo/Makefile b/geo/Makefile new file mode 100644 index 0000000..40a9a2f --- /dev/null +++ b/geo/Makefile @@ -0,0 +1,17 @@ +OPT=--compilation_level SIMPLE_OPTIMIZATIONS + +ifeq ($(TYPE),RELEASE) + CLOSURE=java -jar /usr/share/java/closure-compiler/closure-compiler.jar +else + CLOSURE=../utils/compiler.sh +endif + +all: Geo + +Geo: + $(CLOSURE) $(OPT) \ + --js Geo.js \ + --js Mesh.js \ + --js MeshContainer.js \ + --js MeshStreamer.js \ + --js_output_file ../lib/geo.min.js diff --git a/geo/Mesh.js b/geo/Mesh.js index 3dbdf0b..091a339 100644 --- a/geo/Mesh.js +++ b/geo/Mesh.js @@ -1,14 +1,9 @@ -/** - * @namespace - */ -var mesh = {}; - /** * Reprensents a mesh * @constructor - * @memberOf mesh + * @memberOf geo */ -mesh.Mesh = function() { +geo.Mesh = function() { this.vertices = []; this.faces = []; this.texCoords = []; @@ -23,7 +18,7 @@ mesh.Mesh = function() { * Checks if there are normals in the mesh * @returns {Boolean} true if there are normals in the mesh, false otherwise */ -mesh.Mesh.prototype.hasNormals = function() { +geo.Mesh.prototype.hasNormals = function() { return this.normals.length > 0; } @@ -31,22 +26,22 @@ mesh.Mesh.prototype.hasNormals = function() { * Checks if there are texture coordinates in the mesh * @returns {Boolean} true if there are texture coordinates in the mesh, false otherwise */ -mesh.Mesh.prototype.hasTexCoords = function() { +geo.Mesh.prototype.hasTexCoords = function() { return this.texCoords.length > 0; } /** * Adds a vertex to a mesh - * @param {mesh.Vertex|String} A Vertex object or its string representation + * @param {geo.Vertex|String} A Vertex object or its string representation */ -mesh.Mesh.prototype.addVertex = function(vertex) { +geo.Mesh.prototype.addVertex = function(vertex) { - if (vertex instanceof mesh.Vertex) { + if (vertex instanceof geo.Vertex) { this.vertices.push(vertex); } else if (typeof vertex === 'string' || vertex instanceof String) { - this.vertices.push(new mesh.Vertex(vertex)); + this.vertices.push(new geo.Vertex(vertex)); } else { - console.error("Can only add vertex from mesh.Vertex or string"); + console.error("Can only add vertex from geo.Vertex or string"); return; } @@ -55,18 +50,18 @@ mesh.Mesh.prototype.addVertex = function(vertex) { /** * Adds a face to a mesh - * @param {mesh.Face|String} A Face object or its string representation + * @param {geo.Face|String} A Face object or its string representation */ -mesh.Mesh.prototype.addFaces = function(face) { +geo.Mesh.prototype.addFaces = function(face) { var faces; - if (face instanceof mesh.Face) { + if (face instanceof geo.Face) { this.faces.push(face); } else if (typeof face === 'string' || face instanceof String) { faces = parseFace(face); this.faces = this.faces.concat(faces); } else { - console.error("Can only add face from mesh.Face or string"); + console.error("Can only add face from geo.Face or string"); return; } @@ -79,15 +74,15 @@ mesh.Mesh.prototype.addFaces = function(face) { /** * Adds a texture coordinate to a mesh - * @param {mesh.TexCoord|String} A TexCoord object or its string representation + * @param {geo.TexCoord|String} A TexCoord object or its string representation */ -mesh.Mesh.prototype.addTexCoord = function(texCoord) { - if (texCoord instanceof mesh.TexCoord) { +geo.Mesh.prototype.addTexCoord = function(texCoord) { + if (texCoord instanceof geo.TexCoord) { this.texCoords.push(texCoord); } else if (typeof texCoord === 'string' || texCoord instanceof String) { - this.texCoords.push(new mesh.TexCoord(texCoord)); + this.texCoords.push(new geo.TexCoord(texCoord)); } else { - console.error("Can only add texCoord from mesh.TexCoord or string"); + console.error("Can only add texCoord from geo.TexCoord or string"); return; } @@ -96,31 +91,31 @@ mesh.Mesh.prototype.addTexCoord = function(texCoord) { /** * Adds a normal to a mesh - * @param {mesh.Normal|String} A Normal object or its string representation + * @param {geo.Normal|String} A Normal object or its string representation */ -mesh.Mesh.prototype.addNormal = function(normal) { - if (normal instanceof mesh.Normal) { +geo.Mesh.prototype.addNormal = function(normal) { + if (normal instanceof geo.Normal) { this.normals.push(normal); } else if (typeof normal === 'string' || normal instanceof String) { - this.normals.push(new mesh.Normal(normal)); + this.normals.push(new geo.Normal(normal)); } else { - console.error("Can only add normal from mesh.Normal of string"); + console.error("Can only add normal from geo.Normal of string"); return; } return this.normals[this.normals.length - 1]; } -mesh.Mesh.prototype.isFinished = function() { +geo.Mesh.prototype.isFinished = function() { return this.faceIndex === this.faces.length; } /** * Represent a 3D vertex * @constructor - * @memberOf mesh + * @memberOf geo */ -mesh.Vertex = function() { +geo.Vertex = function() { if (typeof arguments[0] === 'string' || arguments[0] instanceof String) { var split = arguments[0].replace(/\s+/g, " ").split(' '); @@ -155,11 +150,11 @@ mesh.Vertex = function() { * @returns {Array} An array representing the vertex * * @example - * var vertex = new mesh.Vertex('v 3.5 3.6 3.7'); + * var vertex = new geo.Vertex('v 3.5 3.6 3.7'); * vertex.index = 5; * console.log(vertex.toList()); // Prints ['v', 5, 3.5, 3.6, 3.7] */ -mesh.Vertex.prototype.toList = function() { +geo.Vertex.prototype.toList = function() { return ['v', this.index, this.x, this.y, this.z]; } @@ -168,37 +163,37 @@ mesh.Vertex.prototype.toList = function() { * @returns {string} A string representing the vertex * * @example - * var vertex = new mesh.Vertex('v 3.5 3.6 3.7'); + * var vertex = new geo.Vertex('v 3.5 3.6 3.7'); * console.log(vertex.toString()); // Prints v 3.5 3.6 3.7 */ -mesh.Vertex.prototype.toString = function() { +geo.Vertex.prototype.toString = function() { return 'v ' + this.x + ' ' + this.y + ' ' + this.z; } /** * Represent a 3D normal * @constructor - * @memberOf mesh - * @augments mesh.Vertex + * @memberOf geo + * @augments geo.Vertex */ -mesh.Normal = function() { - mesh.Vertex.apply(this, arguments); +geo.Normal = function() { + geo.Vertex.apply(this, arguments); } -mesh.Normal.prototype = Object.create(mesh.Vertex.prototype); -mesh.Normal.prototype.constructor = mesh.Normal; +geo.Normal.prototype = Object.create(geo.Vertex.prototype); +geo.Normal.prototype.constructor = geo.Normal; /** * Gives a list representation of the normal * @returns {Array} An array representing the normal * * @example - * var normal = new mesh.Normal('vn 3.5 3.6 3.7'); + * var normal = new geo.Normal('vn 3.5 3.6 3.7'); * normal.index = 5; * console.log(normal.toList()); // Prints ['vn', 5, 3.5, 3.6, 3.7] */ -mesh.Normal.prototype.toList = function() { - var superObject = mesh.Vertex.prototype.toList.call(this); +geo.Normal.prototype.toList = function() { + var superObject = geo.Vertex.prototype.toList.call(this); superObject[0] = 'vn'; return superObject; } @@ -208,11 +203,11 @@ mesh.Normal.prototype.toList = function() { * @returns {string} A string representing the normal * * @example - * var normal = new mesh.Normal('vn 3.5 3.6 3.7'); + * var normal = new geo.Normal('vn 3.5 3.6 3.7'); * console.log(normal.toString()); // Prints vn 3.5 3.6 3.7 */ -mesh.Normal.prototype.toString = function() { - var superObject = mesh.Vertex.prototype.toString.call(this); +geo.Normal.prototype.toString = function() { + var superObject = geo.Vertex.prototype.toString.call(this); superObject.replace('v', 'vn'); return superObject; } @@ -220,9 +215,9 @@ mesh.Normal.prototype.toString = function() { /** * Represent a texture coordinate element * @constructor - * @memberOf mesh + * @memberOf geo */ -mesh.TexCoord = function() { +geo.TexCoord = function() { if (typeof arguments[0] === 'string' || arguments[0] instanceof String) { var split = arguments[0].replace(/\s+/g, " ").split(' '); @@ -251,11 +246,11 @@ mesh.TexCoord = function() { * @returns {Array} An array representing the texture coordinate * * @example - * var texCoord = new mesh.TexCoord('vt 3.5 3.6'); + * var texCoord = new geo.TexCoord('vt 3.5 3.6'); * texture coordinate.index = 5; * console.log(texture coordinate.toList()); // Prints ['vt', 5, 3.5, 3.6] */ -mesh.TexCoord.prototype.toList = function() { +geo.TexCoord.prototype.toList = function() { return ['vt', this.index, this.x, this.y]; } @@ -264,10 +259,10 @@ mesh.TexCoord.prototype.toList = function() { * @returns {string} A string representing the texture coordinate * * @example - * var texCoord = new mesh.TexCoord('vt 3.5 3.6'); + * var texCoord = new geo.TexCoord('vt 3.5 3.6'); * console.log(texCoord.toString()); // Prints vt 3.5 3.6 */ -mesh.TexCoord.prototype.toString = function() { +geo.TexCoord.prototype.toString = function() { return 'vt ' + this.x + ' ' + this.y; } @@ -275,9 +270,9 @@ mesh.TexCoord.prototype.toString = function() { /** * Represents a face * @constructor - * @memberOf mesh + * @memberOf geo */ -mesh.Face = function() { +geo.Face = function() { if (typeof arguments[0] === 'string' || arguments[0] instanceof String) { if (arguments[0].indexOf('/') === -1) { // No / : easy win : "f 1 2 3" or "f 1 2 3 4" @@ -379,12 +374,12 @@ var parseFace = function(arg) { // Face3 if (split.length >= 4) { - ret.push(new mesh.Face(arg)); + ret.push(new geo.Face(arg)); } // Face3 == 2 * Face3 if (split.length >= 5) { - ret.push(new mesh.Face( + ret.push(new geo.Face( [ split[0], split[1], @@ -401,7 +396,7 @@ var parseFace = function(arg) { * Returns the max index of the vertices of the face * @returns {Number} the max index of the vertices */ -mesh.Face.prototype.max = function() { +geo.Face.prototype.max = function() { if (this.d !== undefined) { return Math.max(this.a, this.b, this.c, this.d); } else { @@ -413,7 +408,7 @@ mesh.Face.prototype.max = function() { * Returns the max index of the texture coordinates of the face * @returns {Number} the max index of the texture coordinates */ -mesh.Face.prototype.maxTexture = function() { +geo.Face.prototype.maxTexture = function() { if (this.dTexture) { return Math.max(this.aTexture, this.bTexture, this.cTexture, this.dTexture); } else { @@ -433,11 +428,11 @@ mesh.Face.prototype.maxTexture = function() { * * * @example - * var face = new mesh.Face('f 1/2/3 4/5/6 7/8/9'); + * var face = new geo.Face('f 1/2/3 4/5/6 7/8/9'); * texture coordinate.index = 5; * console.log(texture coordinate.toList()); // Prints ['f', 5, [1,4,7], [2,5,8], [3,6,9]] */ -mesh.Face.prototype.toList = function() { +geo.Face.prototype.toList = function() { var l = ['f', this.index, this.meshIndex, [this.a, this.b, this.c ], isNaN(this.aTexture) ? [] : [this.aTexture, this.bTexture, this.cTexture], @@ -464,10 +459,10 @@ mesh.Face.prototype.toList = function() { * @returns {string} A string representing the face * * @example - * var face = new mesh.Face('f 3 5 6'); + * var face = new geo.Face('f 3 5 6'); * console.log(face.toString()); // Prints f 3 5 6 */ -mesh.Face.prototype.toString = function() { +geo.Face.prototype.toString = function() { return 'f ' + this.a + ' ' + this.b + ' ' + this.c + (this.d !== undefined ? ' ' + this.d : ''); } @@ -475,9 +470,9 @@ mesh.Face.prototype.toString = function() { * Represents a material name * @constructor * @param {string} line the string representing the material - * @memberOf mesh + * @memberOf geo */ -mesh.Material = function() { +geo.Material = function() { var split = arguments[0].replace(/\s+/g, ' ').trim().split(' '); /** @@ -491,7 +486,7 @@ mesh.Material = function() { * Gives a string representation of the material * @returns {string} obj representation of usemtl */ -mesh.Material.prototype.toString = function() { +geo.Material.prototype.toString = function() { return 'usemtl ' + this.name; } @@ -499,11 +494,10 @@ mesh.Material.prototype.toString = function() { * Gives a list representation of the material * @returns {array} an array representing the material * @example - * var material = new mesh.Material('usemtl MyMaterial'); + * var material = new geo.Material('usemtl MyMaterial'); * console.log(material.toList()); // Logs ['u', 'MyMaterial'] */ -mesh.Material.prototype.toList = function() { +geo.Material.prototype.toList = function() { return ['u', this.name]; } -module.exports = mesh; diff --git a/geo/MeshContainer.js b/geo/MeshContainer.js index 255139d..85edfb0 100644 --- a/geo/MeshContainer.js +++ b/geo/MeshContainer.js @@ -1,46 +1,38 @@ -var fs = require('fs'); -var mesh = require('./Mesh.js'); - /** - * @namespace - */ -var cont = {}; - -/** - * Represents a mesh. All meshes are loaded once in cont.availableMesh to avoid + * Represents a mesh. All meshes are loaded once in geo.availableMesh to avoid * loading at each mesh request * @constructor - * @memberOf cont + * @memberOf geo */ -cont.MeshContainer = function(path) { +geo.MeshContainer = function(path) { /** * array of each part of the mesh - * @type {mesh.Mesh[]} + * @type {geo.Mesh[]} */ this.meshes = []; /** * array of the vertices of the meshes (all merged) - * @type {mesh.Vertex[]} + * @type {geo.Vertex[]} */ this.vertices = []; /** * array of the faces of the meshes (all merged) - * @type {mesh.Face[]} + * @type {geo.Face[]} */ this.faces = []; /** * array of the normals of the meshes (all merged) - * @type {mesh.Normal[]} + * @type {geo.Normal[]} */ this.normals = []; /** * array of the texture coordinates (all merged) - * @type {mesh.TexCoord[]} + * @type {geo.TexCoord[]} */ this.texCoords = []; @@ -56,7 +48,7 @@ cont.MeshContainer = function(path) { * Loads a obj file * @param {string} path the path to the file */ -cont.MeshContainer.prototype.loadFromFile = function(path) { +geo.MeshContainer.prototype.loadFromFile = function(path) { var self = this; var data = fs.readFileSync(path, {encoding: 'utf-8'}); @@ -76,13 +68,13 @@ cont.MeshContainer.prototype.loadFromFile = function(path) { if (line[1] === 't') { // Texture coord - var texCoord = new mesh.TexCoord(line); + var texCoord = new geo.TexCoord(line); texCoord.index = self.texCoords.length; self.texCoords.push(texCoord); } else if (line[1] === 'n') { - var normal = new mesh.Normal(line); + var normal = new geo.Normal(line); normal.index = self.normals.length; self.normals.push(normal); @@ -92,12 +84,12 @@ cont.MeshContainer.prototype.loadFromFile = function(path) { // if (currentMesh === undefined) { // // Chances are that we won't use any material in this case - // currentMesh = new mesh.Mesh(); + // currentMesh = new geo.Mesh(); // self.meshes.push(currentMesh); // } - var vertex = new mesh.Vertex(line); + var vertex = new geo.Vertex(line); vertex.index = self.vertices.length; self.vertices.push(vertex); @@ -107,7 +99,7 @@ cont.MeshContainer.prototype.loadFromFile = function(path) { // Create mesh if it doesn't exist if (currentMesh === undefined) { - currentMesh = new mesh.Mesh(); + currentMesh = new geo.Mesh(); self.meshes.push(currentMesh); } @@ -132,9 +124,9 @@ cont.MeshContainer.prototype.loadFromFile = function(path) { // If a current mesh exists, finish it // Create a new mesh - currentMesh = new mesh.Mesh(); + currentMesh = new geo.Mesh(); self.meshes.push(currentMesh); - currentMesh.material = (new mesh.Material(line)).name; + currentMesh.material = (new geo.Material(line)).name; // console.log(currentMesh.material); } @@ -157,13 +149,11 @@ for (var i = 1; i < 26; i++) { } -cont.availableMeshes = {}; +geo.availableMeshes = {}; for (var i = 0; i < availableMeshNames.length; i++) { var name = availableMeshNames[i]; - cont.availableMeshes[name] = new cont.MeshContainer(name.substring(1, name.length)); + geo.availableMeshes[name] = new geo.MeshContainer(name.substring(1, name.length)); } - -module.exports = cont; diff --git a/geo/MeshStreamer.js b/geo/MeshStreamer.js index 678ae4d..81c8c7c 100644 --- a/geo/MeshStreamer.js +++ b/geo/MeshStreamer.js @@ -1,12 +1,3 @@ -var fs = require('fs'); -var mesh = require('./Mesh.js'); -var cont = require('./MeshContainer.js'); - -/** - * @namespace - */ -var geo = {}; - /** * @private */ @@ -58,7 +49,13 @@ geo.MeshStreamer = function(path) { /** * array of array telling if the jth face of the ith mesh has already been sent - * @type{Boolean[][]} + * + * For each mesh, there is an object containing + * + * @type {Object[]} */ this.meshFaces = []; @@ -94,7 +91,7 @@ geo.MeshStreamer = function(path) { if (path !== undefined) { - this.mesh = cont.availableMeshes[path]; + this.mesh = geo.availableMeshes[path]; } @@ -189,7 +186,7 @@ geo.MeshStreamer.prototype.start = function(socket) { socket.on('request', function(path) { - self.mesh = cont.availableMeshes[path]; + self.mesh = geo.availableMeshes[path]; self.meshFaces = new Array(self.mesh.meshes.length); @@ -540,5 +537,3 @@ geo.MeshStreamer.prototype.isFinished = function(i) { return this.meshFaces[i].counter === this.meshFaces[i].array.length; } - -module.exports = geo; diff --git a/js/Makefile b/js/Makefile index a98455a..dcad816 100644 --- a/js/Makefile +++ b/js/Makefile @@ -3,7 +3,7 @@ OPT=--compilation_level SIMPLE_OPTIMIZATIONS ifeq ($(TYPE),RELEASE) CLOSURE=java -jar /usr/share/java/closure-compiler/closure-compiler.jar else - CLOSURE=./compiler.sh + CLOSURE=../utils/compiler.sh endif all: List ListTest Socket Three Stats ThreeTools Bouncing Multisphere StreamingSimulator PrototypeTools PrototypeReplay PrototypeInteractive Tutorial diff --git a/socket.js b/socket.js index 2fe5dfb..f33c952 100644 --- a/socket.js +++ b/socket.js @@ -1,5 +1,5 @@ var fs = require('fs'); -var geo = require('./geo/MeshStreamer.js'); +var geo = require('./lib/geo.min.js'); module.exports = function(io) { io.on('connection', function(socket) { diff --git a/js/compiler.sh b/utils/compiler.sh similarity index 100% rename from js/compiler.sh rename to utils/compiler.sh