1

I am working on cargo planning application and want to load the cargo in the container. I am creating the 3d model for it using threejs. I am completely new to threejs and so far have managed to create 3d objects (cargo) and palce them inside a container(another 3d object). The problem i am facing is when i add multiple 3d objects inside a container those objects overlaps with each other. I am not able to calculate the correct objects position that should be set to prevent this overlapping issue.

I have just tried using the basic math to calculate the object position and it on scene.

Here is the js fiddle for it

https://jsfiddle.net/nro48e6u/1/

The logic for calculating the objects position is written in the addCargo function which only uses basic math. Here is the code for it

function addCargo(dimension, color, isPallete) {
var dimensionExceed = false; // boolean value that checksifthe cargo dimensions exceed the containerdimension               
if (objects.length == 0) {
    var geometry = new THREE.BoxGeometry(dimension.width, dimension.height, dimension.length);
    var material = new THREE.MeshBasicMaterial({
        color: color
    });
    cargo = new THREE.Mesh(geometry, material);
    wireframe = generateWireframe(geometry);
    default_box_position = {
        x: container_dimension.width / 2 - dimension.width / 2,
        y: dimension.height / 2 - container_dimension.height / 2,
        z: container_dimension.length / 2 - dimension.length / 2
    }
    cargo.position.set(default_box_position.x, default_box_position.y, default_box_position.z);

} else {
    var geometry = new THREE.BoxGeometry(dimension.width, dimension.height, dimension.length);
    var material = new THREE.MeshBasicMaterial({
        color: color,
        wireframe: false
    });
    cargo = new THREE.Mesh(geometry, material);
    wireframe = generateWireframe(geometry);
    cargo.add(wireframe);
    var firstObject = objects[0];
    var xUpdated = false;

    position = {
        x: 0,
        y: 0,
        z: 0
    }
    var startIndex = 0;
    if (palleteDeimension.isSet) {
        position.y = palleteDeimension.height;
        startIndex = 1;
    }
    for (var i = startIndex; i < objects.length; i++) {
        if (!xUpdated || i == objects.length - 1) {
            position.z += parseFloat(objects[i].geometry.parameters.depth);
            xUpdated = false;
        } else {
            xUpdated = false;
            position.z += parseFloat(objects[i].geometry.parameters.depth);
        }
        if (position.z >= container_dimension.length) {
            if (position.z - parseFloat(objects[i].geometry.parameters.depth) + parseFloat(dimension.length) <= container_dimension.length) {
                position.z -= parseFloat(objects[i].geometry.parameters.depth);
            } else {
                position.x += parseFloat(objects[i].geometry.parameters.width);
                position.z = 0;
            }

        } else if (position.z + parseFloat(dimension.length) > container_dimension.length) {
            xUpdated = true;
            position.x += parseFloat(dimension.width);
            position.z = 0;
        }

        if (position.x >= container_dimension.width) {
            position.y += parseFloat(objects[i].geometry.parameters.height);
            position.x = 0;
            position.z = 0;
        } else if (position.x + parseFloat(dimension.width) > container_dimension.width) {
            position.y += parseFloat(dimension.height);
            position.x = 0;
            position.z = 0;
        }
    }
    var z_pos = container_dimension.length / 2 - position.z - (dimension.length / 2);
    var y_pos = position.y - container_dimension.height / 2 + (dimension.height / 2);
    var x_pos = container_dimension.width / 2 - position.x - (dimension.width / 2);
    if (Math.abs(z_pos) <= container_dimension.length / 2 && position.x == 0 && position.y == 0) {
        cargo.position.set(default_box_position.x, default_box_position.y, z_pos);

        if (firstObject.geometry.parameters.width != dimension.width)
            cargo.position.x = x_pos;
        if (firstObject.geometry.parameters.height != dimension.height)
            cargo.position.y = y_pos;
    } else if (Math.abs(y_pos) <= container_dimension.height / 2 && position.x == 0) {
        cargo.position.set(default_box_position.x, y_pos, z_pos);
        if (firstObject.geometry.parameters.width != dimension.width)
            cargo.position.x = x_pos;
    } else
        cargo.position.set(x_pos, y_pos, z_pos);
}
scene.add(cargo);
objects.push(cargo);
initDragCargo();
}

When i add multiple cargo inside the container the cargo's are overlapping with each other. I am note sure how to get rid off this overlapping issue.

1

1 Answer 1

0

You can try a relaxation method, i.e. in an infinite loop change a litte bit each box until all boxes are happy.

dx = 0.1;
while(overlap_at_all(boxes)) {
    foreach(var box in boxes) {
        var overlapping_boxes = get_overlapping_boxes(box, boxes);
        foreach(var overlapping_box : overlapping_boxes)
            push_away_boxes(box, overlapping_box, dx);
    }
}

The function "get_overlapping_boxes" does a collision detection check and return the colliding boxes with current box. This can be done efficiently using a BVH (Bounding Volume Hierarchy such as Dynamic AABB-tree) or some other datastructure. Even an OctTree could get good results. That same applies to function "overlap_at_all"

The function "push_away_boxes" just moves both boxes in opposite direction a small distance dx (lets say 0.1 or 10 or other number that can be considered "small" with respect of the sizes of the boxes). So the cumulative application of "push_away_boxes" to a given pair of overlapping boxes will eventually make them nonoverlapping. The "convergence" is reached once there are no more overlapping boxes. Convergence speed can be improved by enlarging the "dx" distance, however the precision is compromised, the larger dx the better the performance but at the expense of a worse precision.

Now, the container box is not taken into account. We can introduce it as constrain on the movement of boxes inside the "push_away_boxes" function. However the convergence of the method is not guaranteed given that constraint. So we can give a maximum number of iterations to the loop to prevent infinite cycling. This solution does not guarantee that if there is a solution it will be found.

Finding the optimal arrangement with minimal waist of space is out of scope in my answer.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.