Object.raycast: Test the hit on the BoundBox first
authorGermano Cavalcante <germano.costa@ig.com.br>
Sat, 15 Apr 2017 03:44:05 +0000 (00:44 -0300)
committerGermano Cavalcante <germano.costa@ig.com.br>
Sat, 15 Apr 2017 03:44:05 +0000 (00:44 -0300)
This avoids the unnecessary creation of bvhtree, which can be highly inefficient in some cases
(for example: in the `operator_modal_view3d_raycast.py` template)

source/blender/makesrna/intern/rna_object_api.c

index c680abe71a4940c121c05a04692a84d9d1f18862..3b2e57a06e58aa9b01b8f6b57e0360514176514b 100644 (file)
@@ -321,13 +321,19 @@ static void rna_Object_ray_cast(
         float origin[3], float direction[3], float distance,
         int *r_success, float r_location[3], float r_normal[3], int *r_index)
 {
-       BVHTreeFromMesh treeData = {NULL};
-       
        if (ob->derivedFinal == NULL) {
                BKE_reportf(reports, RPT_ERROR, "Object '%s' has no mesh data to be used for ray casting", ob->id.name + 2);
                return;
        }
 
+       /* Test BoundBox */
+       BoundBox *bb = BKE_object_boundbox_get(ob);
+       if (bb && !isect_ray_aabb_v3_simple(origin, direction, bb->vec[0], bb->vec[6], NULL, NULL)) {
+               goto finally;
+       }
+
+       BVHTreeFromMesh treeData = {NULL};
+
        /* no need to managing allocation or freeing of the BVH data. this is generated and freed as needed */
        bvhtree_from_mesh_looptri(&treeData, ob->derivedFinal, 0.0f, 4, 6);