processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.54k stars 3.3k forks source link

Implement bounding box calculation for p5.Geometry objects #6787

Closed Garima3110 closed 6 months ago

Garima3110 commented 8 months ago

Increasing access

The addition of the calculateBoundingBox() method in p5.js enhances accessibility by providing users with an intuitive and standardized method to visualize and analyze the spatial dimensions of 3D objects and will also be helpful in some physics simulations or shape packing. It will simplify complex tasks such as collision detection and camera control, making 3D programming more approachable for a diverse range of users.

Most appropriate sub-area of p5.js?

Feature request details

This feature is crucial for users working with 3D graphics, providing an easy and standardized way to determine the spatial extent of objects. Example usage of computeBoundingBox() in a p5.js sketch can be :


let myBox = box(30, 40, 50);
let boundingBox = computeBoundingBox(myBox);
console.log("Bounding Box:", boundingBox);
Garima3110 commented 8 months ago

I would like to implement this feature.

davepagurek commented 8 months ago

I think this could make sense! Some notes on the implementation:

Garima3110 commented 8 months ago

What data do you expect to be in the box?

In addition to the size and offset properties, I think we would need the max, min properties which too would be instances of the p5.js p5.Vector class.

Here's a breakdown:

Garima3110 commented 8 months ago

Considering your suggestions and that what I have understood, this is what I am upto :

p5.Geometry = class Geometry {
  constructor(detailX, detailY, callback) {
   this.vertices = [];
   this.boundingBoxCache = null;
   }

  // 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;
  }

I would love to have your suggestions on this @davepagurek . Thankyou!