From d7f615c2ef6b4bcdc564d93fba6b33cfc8b792f7 Mon Sep 17 00:00:00 2001 From: Garima Date: Tue, 13 Feb 2024 18:49:09 +0530 Subject: [PATCH 1/8] Added calculateBoundingBox() --- src/webgl/p5.Geometry.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index 18731dfcb5..87861f017e 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -24,6 +24,9 @@ p5.Geometry = class Geometry { //@type [p5.Vector] this.vertices = []; + this.boundingBoxCache = null; + + //an array containing every vertex for stroke drawing this.lineVertices = new p5.DataArray(); @@ -71,6 +74,43 @@ p5.Geometry = class Geometry { } return this; // TODO: is this a constructor? } + // Custom bounding box calculation based on the object's vertices + calculateBoundingBox() { + if (this.boundingBoxCache) { + return this.boundingBoxCache; // Return cached result if available + } + + let minVertex = createVector( + Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE); + let maxVertex = createVector( + Number.MIN_VALUE, Number.MIN_VALUE, Number.MIN_VALUE); + + for (let i = 0; i < this.vertices.length; i++) { + let vertex = this.vertices[i]; + minVertex.x = min(minVertex.x, vertex.x); + minVertex.y = min(minVertex.y, vertex.y); + minVertex.z = min(minVertex.z, vertex.z); + + maxVertex.x = max(maxVertex.x, vertex.x); + maxVertex.y = max(maxVertex.y, vertex.y); + maxVertex.z = max(maxVertex.z, vertex.z); + } + // Calculate size and offset properties + let size = createVector(maxVertex.x - minVertex.x, + maxVertex.y - minVertex.y, maxVertex.z - minVertex.z); + let offset = createVector((minVertex.x + maxVertex.x) / 2, + (minVertex.y + maxVertex.y) / 2, (minVertex.z + maxVertex.z) / 2); + + // Cache the result for future access + this.boundingBoxCache = { + min: minVertex, + max: maxVertex, + size: size, + offset: offset + }; + + return this.boundingBoxCache; + } reset() { this.lineVertices.clear(); From 31fdc5f6ad67ed66d17de9cd86975958ed9c6a60 Mon Sep 17 00:00:00 2001 From: Garima Date: Sun, 18 Feb 2024 22:17:09 +0530 Subject: [PATCH 2/8] Added unit test for calculateBoundingBox() --- test/unit/webgl/p5.Geometry.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/unit/webgl/p5.Geometry.js b/test/unit/webgl/p5.Geometry.js index 2dddf8e33e..43b95e5a39 100644 --- a/test/unit/webgl/p5.Geometry.js +++ b/test/unit/webgl/p5.Geometry.js @@ -92,6 +92,20 @@ suite('p5.Geometry', function() { assert.equal(geom._addJoin.callCount, 4); }); + test('calculateBoundingBox()', function() { + geom.vertices.push( + myp5.createVector(0, 0, 0), + myp5.createVector(10, 20, 30), + myp5.createVector(-5, 15, 25) + ); + geom.calculateBoundingBox(); + assert.deepEqual(geom.boundingBox.min.array(), [-5, 0, 0]); + assert.deepEqual(geom.boundingBox.max.array(), [10, 20, 30]); + assert.deepEqual(geom.boundingBox.size.array(), [15, 20, 30]); + assert.deepEqual(geom.boundingBox.offset.array(), [2.5, 10, 15]); + }); + + test('degenerate edge in the middle', function() { geom.vertices.push( myp5.createVector(0, 0), From 399bc0c772435efdd2d753433ef0e1d815a699b7 Mon Sep 17 00:00:00 2001 From: Garima Date: Fri, 23 Feb 2024 18:59:26 +0530 Subject: [PATCH 3/8] Updated inline docs+ minor fixes --- src/webgl/p5.Geometry.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index 0a664cde37..b94b555bfa 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -77,15 +77,33 @@ p5.Geometry = class Geometry { } return this; // TODO: is this a constructor? } - // Custom bounding box calculation based on the object's vertices + + /** + * Custom bounding box calculation based on the object's vertices. + * The bounding box is a rectangular prism that encompasses the entire object. + * It is defined by the minimum and maximum coordinates along each axis, as well + * as the size and offset of the box. + * + * + * @method calculateBoundingBox + * @memberof p5.Geometry.prototype + * @return {Object} An object containing the bounding box properties: + * + * - `min`: The minimum coordinates of the bounding box as a p5.Vector. + * - `max`: The maximum coordinates of the bounding box as a p5.Vector. + * - `size`: The size of the bounding box as a p5.Vector. + * - `offset`: The offset of the bounding box as a p5.Vector. + * + */ + calculateBoundingBox() { if (this.boundingBoxCache) { return this.boundingBoxCache; // Return cached result if available } - let minVertex = createVector( + let minVertex = new p5.Vector( Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE); - let maxVertex = createVector( + let maxVertex = new p5.Vector( Number.MIN_VALUE, Number.MIN_VALUE, Number.MIN_VALUE); for (let i = 0; i < this.vertices.length; i++) { @@ -99,9 +117,9 @@ p5.Geometry = class Geometry { maxVertex.z = max(maxVertex.z, vertex.z); } // Calculate size and offset properties - let size = createVector(maxVertex.x - minVertex.x, + let size = new p5.Vector(maxVertex.x - minVertex.x, maxVertex.y - minVertex.y, maxVertex.z - minVertex.z); - let offset = createVector((minVertex.x + maxVertex.x) / 2, + let offset = new p5.Vector((minVertex.x + maxVertex.x) / 2, (minVertex.y + maxVertex.y) / 2, (minVertex.z + maxVertex.z) / 2); // Cache the result for future access From e47c1ce93753e256c98339ee85207a6afb322c1e Mon Sep 17 00:00:00 2001 From: Garima Date: Tue, 27 Feb 2024 15:46:28 +0530 Subject: [PATCH 4/8] Minor fixes --- src/webgl/p5.Geometry.js | 21 +++++++++++---------- test/unit/webgl/p5.Geometry.js | 10 +++++----- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index b94b555bfa..09892de266 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -84,16 +84,17 @@ p5.Geometry = class Geometry { * It is defined by the minimum and maximum coordinates along each axis, as well * as the size and offset of the box. * - * - * @method calculateBoundingBox - * @memberof p5.Geometry.prototype - * @return {Object} An object containing the bounding box properties: + * It returns an object containing the bounding box properties: * * - `min`: The minimum coordinates of the bounding box as a p5.Vector. * - `max`: The maximum coordinates of the bounding box as a p5.Vector. * - `size`: The size of the bounding box as a p5.Vector. * - `offset`: The offset of the bounding box as a p5.Vector. * + * @method calculateBoundingBox + * @memberof p5.Geometry.prototype + * @return {Object} + * */ calculateBoundingBox() { @@ -108,13 +109,13 @@ p5.Geometry = class Geometry { for (let i = 0; i < this.vertices.length; i++) { let vertex = this.vertices[i]; - minVertex.x = min(minVertex.x, vertex.x); - minVertex.y = min(minVertex.y, vertex.y); - minVertex.z = min(minVertex.z, vertex.z); + minVertex.x = Math.min(minVertex.x, vertex.x); + minVertex.y = Math.min(minVertex.y, vertex.y); + minVertex.z = Math.min(minVertex.z, vertex.z); - maxVertex.x = max(maxVertex.x, vertex.x); - maxVertex.y = max(maxVertex.y, vertex.y); - maxVertex.z = max(maxVertex.z, vertex.z); + maxVertex.x = Math.max(maxVertex.x, vertex.x); + maxVertex.y = Math.max(maxVertex.y, vertex.y); + maxVertex.z = Math.max(maxVertex.z, vertex.z); } // Calculate size and offset properties let size = new p5.Vector(maxVertex.x - minVertex.x, diff --git a/test/unit/webgl/p5.Geometry.js b/test/unit/webgl/p5.Geometry.js index 43b95e5a39..590b2f8696 100644 --- a/test/unit/webgl/p5.Geometry.js +++ b/test/unit/webgl/p5.Geometry.js @@ -98,11 +98,11 @@ suite('p5.Geometry', function() { myp5.createVector(10, 20, 30), myp5.createVector(-5, 15, 25) ); - geom.calculateBoundingBox(); - assert.deepEqual(geom.boundingBox.min.array(), [-5, 0, 0]); - assert.deepEqual(geom.boundingBox.max.array(), [10, 20, 30]); - assert.deepEqual(geom.boundingBox.size.array(), [15, 20, 30]); - assert.deepEqual(geom.boundingBox.offset.array(), [2.5, 10, 15]); + const boundingBox = geom.calculateBoundingBox(); + assert.deepEqual(boundingBox.min, [-5, 0, 0]); + assert.deepEqual(boundingBox.max, [10, 20, 30]); + assert.deepEqual(boundingBox.size, [15, 20, 30]); + assert.deepEqual(boundingBox.offset, [2.5, 10, 15]); }); From f99444a94717dfe66550db53cd4967cd19422dd2 Mon Sep 17 00:00:00 2001 From: Garima Date: Tue, 27 Feb 2024 16:17:28 +0530 Subject: [PATCH 5/8] Fixing the unit test --- test/unit/webgl/p5.Geometry.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/webgl/p5.Geometry.js b/test/unit/webgl/p5.Geometry.js index 590b2f8696..a7dd900f80 100644 --- a/test/unit/webgl/p5.Geometry.js +++ b/test/unit/webgl/p5.Geometry.js @@ -99,10 +99,10 @@ suite('p5.Geometry', function() { myp5.createVector(-5, 15, 25) ); const boundingBox = geom.calculateBoundingBox(); - assert.deepEqual(boundingBox.min, [-5, 0, 0]); - assert.deepEqual(boundingBox.max, [10, 20, 30]); - assert.deepEqual(boundingBox.size, [15, 20, 30]); - assert.deepEqual(boundingBox.offset, [2.5, 10, 15]); + assert.deepEqual(boundingBox.min.array(), [-5, 0, 0]); + assert.deepEqual(boundingBox.max.array(), [10, 20, 30]); + assert.deepEqual(boundingBox.size.array(), [15, 20, 30]); + assert.deepEqual(boundingBox.offset.array(), [2.5, 10, 15]); }); From 4c0734bc893605d705249da32c5174de5bc639a0 Mon Sep 17 00:00:00 2001 From: Garima Date: Wed, 28 Feb 2024 02:20:23 +0530 Subject: [PATCH 6/8] Added example --- src/webgl/p5.Geometry.js | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index 09892de266..fa0da76a31 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -95,6 +95,55 @@ p5.Geometry = class Geometry { * @memberof p5.Geometry.prototype * @return {Object} * + * @example + * + *
+ * + * let particles; + * let button; + * let resultParagraph; + * + * function setup() { + * createCanvas(100, 100, WEBGL); + * button = createButton('New'); + * button.mousePressed(makeParticles); + * + * resultParagraph = createP(''); + * resultParagraph.style('font-family', 'monospace'); + * makeParticles(); + * } + * + * function makeParticles() { + * if (particles) freeGeometry(particles); + * + * particles = buildGeometry(() => { + * for (let i = 0; i < 60; i++) { + * push(); + * translate( + * randomGaussian(0, 200), + * randomGaussian(0, 100), + * randomGaussian(0, 150) + * ); + * sphere(10); + * pop(); + * } + * }); + * + * const boundingBox = particles.calculateBoundingBox(); + * resultParagraph.html('Bounding Box: ' + JSON.stringify(boundingBox)); + * } + * + * function draw() { + * background(255); + * noStroke(); + * lights(); + * orbitControl(); + * model(particles); + * } + * + * + *
+ * */ calculateBoundingBox() { From ed5739fc715a6ed2831fc2daea22328e41b5eb7d Mon Sep 17 00:00:00 2001 From: Garima Date: Wed, 28 Feb 2024 02:36:47 +0530 Subject: [PATCH 7/8] Minor formatting --- src/webgl/p5.Geometry.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index fa0da76a31..3ccc8dfb5a 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -108,8 +108,9 @@ p5.Geometry = class Geometry { * button = createButton('New'); * button.mousePressed(makeParticles); * - * resultParagraph = createP(''); + * resultParagraph = createP('').style('width', '180px' ); * resultParagraph.style('font-family', 'monospace'); + * resultParagraph.style('font-size', '12px'); * makeParticles(); * } * @@ -130,7 +131,7 @@ p5.Geometry = class Geometry { * }); * * const boundingBox = particles.calculateBoundingBox(); - * resultParagraph.html('Bounding Box: ' + JSON.stringify(boundingBox)); + * resultParagraph.html('Bounding Box: \n' + JSON.stringify(boundingBox)); * } * * function draw() { From 6a7244b3e7e877b7c343f3df1c2c985d163745c5 Mon Sep 17 00:00:00 2001 From: Garima Date: Wed, 13 Mar 2024 02:32:51 +0530 Subject: [PATCH 8/8] Minor changes in formatting --- src/webgl/p5.Geometry.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index 3ccc8dfb5a..5035aa6028 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -93,7 +93,7 @@ p5.Geometry = class Geometry { * * @method calculateBoundingBox * @memberof p5.Geometry.prototype - * @return {Object} + * @returns {Object} * * @example * @@ -108,7 +108,7 @@ p5.Geometry = class Geometry { * button = createButton('New'); * button.mousePressed(makeParticles); * - * resultParagraph = createP('').style('width', '180px' ); + * resultParagraph = createElement('pre').style('width', '200px' ); * resultParagraph.style('font-family', 'monospace'); * resultParagraph.style('font-size', '12px'); * makeParticles(); @@ -131,7 +131,7 @@ p5.Geometry = class Geometry { * }); * * const boundingBox = particles.calculateBoundingBox(); - * resultParagraph.html('Bounding Box: \n' + JSON.stringify(boundingBox)); + * resultParagraph.html('Bounding Box: \n' + JSON.stringify(boundingBox, null, 2)); * } * * function draw() {