-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solves issue #6787 #6809
Merged
Merged
Solves issue #6787 #6809
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d7f615c
Added calculateBoundingBox()
Garima3110 fe862c7
Merge remote-tracking branch 'upstream/main'
Garima3110 31fdc5f
Added unit test for calculateBoundingBox()
Garima3110 dc9c88b
Merge remote-tracking branch 'upstream/main' into boundingBox
Garima3110 399bc0c
Updated inline docs+ minor fixes
Garima3110 f4edd6e
Merge remote-tracking branch 'upstream/main' into boundingBox
Garima3110 e47c1ce
Minor fixes
Garima3110 f99444a
Fixing the unit test
Garima3110 4c0734b
Added example
Garima3110 ed5739f
Minor formatting
Garima3110 6a7244b
Minor changes in formatting
Garima3110 71f56e5
Merge remote-tracking branch 'upstream/main' into boundingBox
Garima3110 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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(); | ||||||
|
||||||
|
@@ -75,6 +78,112 @@ p5.Geometry = class Geometry { | |||||
return this; // TODO: is this a constructor? | ||||||
} | ||||||
|
||||||
/** | ||||||
* 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. | ||||||
* | ||||||
* 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} | ||||||
* | ||||||
* @example | ||||||
* | ||||||
* <div> | ||||||
* <code> | ||||||
* let particles; | ||||||
* let button; | ||||||
* let resultParagraph; | ||||||
* | ||||||
* function setup() { | ||||||
* createCanvas(100, 100, WEBGL); | ||||||
* button = createButton('New'); | ||||||
* button.mousePressed(makeParticles); | ||||||
* | ||||||
* resultParagraph = createP('').style('width', '180px' ); | ||||||
* resultParagraph.style('font-family', 'monospace'); | ||||||
* resultParagraph.style('font-size', '12px'); | ||||||
* 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: \n' + JSON.stringify(boundingBox)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* } | ||||||
* | ||||||
* function draw() { | ||||||
* background(255); | ||||||
* noStroke(); | ||||||
* lights(); | ||||||
* orbitControl(); | ||||||
* model(particles); | ||||||
* } | ||||||
* | ||||||
* </code> | ||||||
* </div> | ||||||
* | ||||||
*/ | ||||||
|
||||||
calculateBoundingBox() { | ||||||
if (this.boundingBoxCache) { | ||||||
return this.boundingBoxCache; // Return cached result if available | ||||||
} | ||||||
|
||||||
let minVertex = new p5.Vector( | ||||||
Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE); | ||||||
let maxVertex = new p5.Vector( | ||||||
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 = Math.min(minVertex.x, vertex.x); | ||||||
minVertex.y = Math.min(minVertex.y, vertex.y); | ||||||
minVertex.z = Math.min(minVertex.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, | ||||||
maxVertex.y - minVertex.y, maxVertex.z - minVertex.z); | ||||||
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 | ||||||
this.boundingBoxCache = { | ||||||
min: minVertex, | ||||||
max: maxVertex, | ||||||
size: size, | ||||||
offset: offset | ||||||
}; | ||||||
|
||||||
return this.boundingBoxCache; | ||||||
} | ||||||
|
||||||
reset() { | ||||||
this._hasFillTransparency = undefined; | ||||||
this._hasStrokeTransparency = undefined; | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.