From 4f68a00391b4730a54bb820e6ead827d46b17ef1 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 11 Dec 2019 17:06:34 +0100 Subject: [PATCH] Error messages --- index.html | 5 +++++ src/Model.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/main.js | 16 +++++++++++---- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 040524a..76709a2 100644 --- a/index.html +++ b/index.html @@ -23,10 +23,15 @@ #percentage { color: white; } + + #errormessage{ + color: red; + }
+
diff --git a/src/Model.js b/src/Model.js index ee6721c..1f35e46 100644 --- a/src/Model.js +++ b/src/Model.js @@ -1,5 +1,5 @@ class Model extends THREE.Mesh { - constructor() { + constructor(path) { let geometry = new THREE.Geometry(); let materials = [ new THREE.MeshLambertMaterial( { color: 0xffffff, side: THREE.DoubleSide } ), @@ -7,12 +7,50 @@ class Model extends THREE.Mesh { ]; super(geometry, materials); this.frustumCulled = false; + this.path = path; this.vertices = []; + this.currentLine = 1; + } + + throwError(message) { + let e = new Error("In " + this.path + ":L" + this.currentLine + " " + message); + e.type = "custom"; + throw e; + } + + checkVertex(id) { + if (this.geometry.vertices[id] === undefined) { + this.throwError("EditVertex requires vertex " + (id + 1) + " but there is no such vertex"); + } + } + + checkFaceId(id) { + if (this.geometry.faces[id] === undefined) { + this.throwError("EditFace requires face " + (id + 1) + " but there is no such face"); + } + } + + checkFace(f) { + let vertices = this.geometry.vertices; + + if (vertices[f.a] === undefined) { + this.throwError("Face requires vertex " + (f.a + 1) + " but there is no such vertex"); + } + + if (vertices[f.b] === undefined) { + this.throwError("Face requires vertex " + (f.b + 1) + " but there is no such vertex"); + } + + if (vertices[f.c] === undefined) { + this.throwError("Face requires vertex " + (f.c + 1) + " but there is no such vertex"); + } + } manageElement(element) { let vertices = this.geometry.vertices; + let f, normal; switch (element.type) { case Element.AddVertex: @@ -21,14 +59,16 @@ class Model extends THREE.Mesh { break; case Element.EditVertex: + this.checkVertex(element.id); this.geometry.vertices[element.id].copy(element.value); this.geometry.verticesNeedUpdate = true; break; case Element.AddFace: - let f = element.value; - let normal = + f = element.value; + this.checkFace(f); + normal = vertices[f.b].clone().sub(vertices[f.a]) .cross(vertices[f.c].clone().sub(vertices[f.a])); normal.normalize(); @@ -43,6 +83,8 @@ class Model extends THREE.Mesh { case Element.AddTriangleFan: for (let f of element.value) { + + this.checkFace(f); let normal = vertices[f.b].clone().sub(vertices[f.a]) .cross(vertices[f.c].clone().sub(vertices[f.a])); @@ -61,12 +103,16 @@ class Model extends THREE.Mesh { case Element.EditFace: f = element.value; + this.checkFaceId(element.id); + this.checkFace(f); normal = vertices[f.b].clone().sub(vertices[f.a]) .cross(vertices[f.c].clone().sub(vertices[f.a])); normal.normalize(); f.normal = normal; + + this.geometry.faces[element.id] = f; this.geometry.elementsNeedUpdate = true; break; @@ -74,12 +120,13 @@ class Model extends THREE.Mesh { case Element.DeleteFace: this.geometry.faces[element.id].materialIndex = 1; this.geometry.elementsNeedUpdate = true; - break; default: throw new Error("unknown element type: " + element.type); } + + this.currentLine++; } } diff --git a/src/main.js b/src/main.js index 1737af7..1843bb5 100644 --- a/src/main.js +++ b/src/main.js @@ -5,13 +5,21 @@ animate(); function init() { - let url = document.URL.split('?')[1] || "bunny_remove.obj"; + let url = 'assets/' + document.URL.split('?')[1] || "bunny_remove.obj"; - loader = new Loader('assets/' + url, 1024, 20); + loader = new Loader(url, 1024, 20); loader.start(function(elements) { for (let element of elements) { if (element !== undefined) { - model.manageElement(element); + try { + model.manageElement(element); + } catch(e) { + if (e.type === "custom") { + document.getElementById('errormessage').innerHTML = e; + } else { + throw e; + } + } } } }); @@ -21,7 +29,7 @@ function init() { scene = new THREE.Scene(); - model = new Model(); + model = new Model(url); scene.add(model); light1 = new THREE.AmbientLight(0x999999);