static int global_physics_test (lua_State *L)
{
TRY_START
        LuaTestCallback lcb;
        push_cfunction(L, my_lua_error_handler);
        int error_handler = lua_gettop(L);
        if (lua_gettop(L)==5) {
                float radius = lua_tonumber(L, 1);
                Vector3 pos = check_v3(L, 2);
                bool only_dyn = check_bool(L, 3);
                if (lua_type(L, 4) != LUA_TFUNCTION)
                        my_lua_error(L, "Parameter 4 should be a function.");

                physics_test_sphere(radius, pos, only_dyn, lcb);
                lcb.pushResults(L, 4, error_handler);
        } else {
                check_args(L, 6);
                std::string col_mesh_name = check_path(L, 1);
                DiskResource *col_mesh_ = disk_resource_get_or_make(col_mesh_name);
                CollisionMesh *col_mesh = dynamic_cast<CollisionMesh*>(col_mesh_);
                if (col_mesh==NULL) my_lua_error(L, "Not a collision mesh: \""+col_mesh_name+"\"");
                if (!col_mesh->isLoaded()) my_lua_error(L, "Not loaded: \""+col_mesh_name+"\"");
                Vector3 pos = check_v3(L, 2);
                Quaternion quat = check_quat(L, 3);
                bool only_dyn = check_bool(L, 4);
                if (lua_type(L, 5) != LUA_TFUNCTION)
                        my_lua_error(L, "Parameter 5 should be a function.");

                physics_test(col_mesh, pos, quat, only_dyn, lcb);
                lcb.pushResults(L, 5, error_handler);
        }
        lua_pop(L, 1); // error handler
        return 0;
TRY_END
}
static int global_physics_sweep_col_mesh (lua_State *L)
{
TRY_START
        int base_line = 7;
        check_args_min(L, base_line);

        std::string col_mesh_name = check_path(L, 1);
        DiskResource *col_mesh_ = disk_resource_get_or_make(col_mesh_name);
        CollisionMesh *col_mesh = dynamic_cast<CollisionMesh*>(col_mesh_);
        Quaternion q = check_quat(L, 2);
        Vector3 start = check_v3(L, 3);
        Vector3 ray = check_v3(L, 4);
        bool nearest_only = check_bool(L, 5);
        unsigned long flags = check_t<unsigned long>(L, 6);
        if (lua_type(L, 7) != LUA_TFUNCTION)
                my_lua_error(L, "Parameter 5 should be a function.");

        LuaSweepCallback lcb(nearest_only, flags);
        init_cast_blacklist(L, base_line, lcb);

        physics_sweep_col_mesh(start, q, start + ray, q, lcb, col_mesh);

        push_cfunction(L, my_lua_error_handler);
        int error_handler = lua_gettop(L);

        lcb.pushResults(L, 7, error_handler);
        return 0;
TRY_END
}
static int rbody_impulse (lua_State *L)
{
TRY_START
        check_args(L, 3);
        GET_UD_MACRO(RigidBody, self, 1, RBODY_TAG);
        Vector3 impulse = check_v3(L, 2);
        Vector3 wpos = check_v3(L, 3);
        const Vector3 &pos = self.getPosition();
        self.impulse(impulse, wpos-pos);
        return 0;
TRY_END
}
static int global_streamer_centre_full (lua_State *L)
{
TRY_START
        check_args(L,1);
        Vector3 pos = check_v3(L,1);
        streamer_centre(L, pos, true);
        return 0;
TRY_END
}
static int rbody_torque (lua_State *L)
{
TRY_START
        check_args(L, 2);
        GET_UD_MACRO(RigidBody, self, 1, RBODY_TAG);
        Vector3 torque = check_v3(L, 2);
        self.torque(torque);
        return 0;
TRY_END
}
static int global_physics_body_make (lua_State *L)
{
TRY_START
        check_args(L, 3);
        std::string n = check_path(L, 1);
        Vector3 pos = check_v3(L, 2);
        Quaternion quat = check_quat(L, 3);
        push_rbody(L, new RigidBody(n, pos, quat));
        return 1;
TRY_END
}
static int rbody_world_to_local (lua_State *L)
{
TRY_START
        check_args(L, 2);
        GET_UD_MACRO(RigidBody, self, 1, RBODY_TAG);
        Vector3 a = check_v3(L, 2);
        Vector3 result = self.getOrientation().inverse() * (a - self.getPosition());
        push_v3(L, result);
        return 1;
TRY_END
}
static int gritobj_update_sphere (lua_State *L)
{
TRY_START
        check_args(L,3);
        GET_UD_MACRO(GritObjectPtr,self,1,GRITOBJ_TAG);
        Vector3 pos = check_v3(L,2);
        float r = check_float(L,3);
        self->updateSphere(pos, r);
        return 0;
TRY_END
}
static int global_physics_cast_ray (lua_State *L)
{
TRY_START
        int base_line = 4;
        check_args_min(L, base_line);

        Vector3 start = check_v3(L, 1);
        Vector3 ray = check_v3(L, 2);
        bool nearest_only = check_bool(L, 3);
        unsigned long flags = check_t<unsigned long>(L, 4);

        LuaSweepCallback lcb(nearest_only, flags);
        init_cast_blacklist(L, base_line, lcb);

        physics_ray(start, start+ray, lcb);

        return lcb.pushResults(L);

TRY_END
}
static int rbody_set_part_position_offset (lua_State *L)
{
TRY_START
        check_args(L, 3);
        GET_UD_MACRO(RigidBody, self, 1, RBODY_TAG);
        int i = (int)check_int(L, 2, 0, self.getNumElements()-1);
        Vector3 v = check_v3(L, 3);
        self.setElementPositionOffset(i, v);
        return 0;
TRY_END
}
static int global_physics_sweep_cylinder (lua_State *L)
{
TRY_START
        int base_line = 7;
        check_args_min(L, base_line);

        float radius = check_float(L, 1);
        float height = check_float(L, 2);
        Quaternion q = check_quat(L, 3);
        Vector3 start = check_v3(L, 4);
        Vector3 ray = check_v3(L, 5);
        bool nearest_only = check_bool(L, 6);
        unsigned long flags = check_t<unsigned long>(L, 7);

        LuaSweepCallback lcb(nearest_only, flags);
        init_cast_blacklist(L, base_line, lcb);

        physics_sweep_cylinder(start, q, start+ray, lcb, radius, height);

        return lcb.pushResults(L);

TRY_END
}
Esempio n. 12
0
int main(int argc, char *argv[])
{
    perf_parse_args(argc, argv);

    std::cout << "size: " << PERF_N << std::endl;

    boost::compute::device device = boost::compute::system::default_device();
    boost::compute::context context(device);
    boost::compute::command_queue queue(context, device);

    std::vector<int> v1 = generate_random_vector<int>(PERF_N / 2);
    std::vector<int> v2 = generate_random_vector<int>(PERF_N / 2);
    std::vector<int> v3(PERF_N);

    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());

    boost::compute::vector<int> gpu_v1(PERF_N / 2, context);
    boost::compute::vector<int> gpu_v2(PERF_N / 2, context);
    boost::compute::vector<int> gpu_v3(PERF_N, context);

    boost::compute::copy(v1.begin(), v1.end(), gpu_v1.begin(), queue);
    boost::compute::copy(v2.begin(), v2.end(), gpu_v2.begin(), queue);

    perf_timer t;
    for(size_t trial = 0; trial < PERF_TRIALS; trial++){
        t.start();
        boost::compute::merge(gpu_v1.begin(), gpu_v1.end(),
                              gpu_v2.begin(), gpu_v2.end(),
                              gpu_v3.begin(),
                              queue
        );
        t.stop();
    }
    std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;

    std::vector<int> check_v3(PERF_N);
    boost::compute::copy(gpu_v3.begin(), gpu_v3.end(), check_v3.begin(), queue);
    queue.finish();

    std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
    bool ok = std::equal(check_v3.begin(), check_v3.end(), v3.begin());
    if(!ok){
        std::cerr << "ERROR: merged ranges different" << std::endl;
        return -1;
    }

    return 0;
}
static int rbody_local_vel (lua_State *L)
{
TRY_START
        check_args(L, 3);
        GET_UD_MACRO(RigidBody, self, 1, RBODY_TAG);
        Vector3 pos = check_v3(L, 2);
        bool world_space = check_bool(L, 3);
        Vector3 local_pos = pos;
        if (world_space) {
                local_pos -= self.getPosition();
        }
        Vector3 local_vel = self.getLocalVelocity(local_pos);
        push_v3(L, local_vel);
        return 1;
TRY_END
}
int plot_v3_make (lua_State *L)
{
TRY_START
        PlotV3 *self = new PlotV3();
        check_args(L,1);
        int table = lua_gettop(L);
        if (!lua_istable(L,table))
                my_lua_error(L,"Parameter should be a table");
        for (lua_pushnil(L) ; lua_next(L,table)!=0 ; lua_pop(L,1)) {
                // the name is held in the object anyway
                float k = check_float(L,-2);
                Vector3 v = check_v3(L,-1);
                self->addPoint(k,v);
        }
        self->commit();
        push(L,self,PLOT_V3_TAG);
        return 1;
TRY_END
}
static int gritobj_newindex (lua_State *L)
{
TRY_START
        check_args(L,3);
        GET_UD_MACRO(GritObjectPtr,self,1,GRITOBJ_TAG);
        std::string key = check_string(L,2);

        if (key=="destroy") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="near") {
                if (lua_isnil(L,3)) {
                        self->setNearObj(self,GritObjectPtr());
                } else {
                        GET_UD_MACRO(GritObjectPtr,v,3,GRITOBJ_TAG);
                        self->setNearObj(self,v);
                }
        } else if (key=="far") {
                if (lua_isnil(L,3)) {
                        self->setNearObj(self,GritObjectPtr());
                } else {
                        GET_UD_MACRO(GritObjectPtr,v,3,GRITOBJ_TAG);
                        self->setFarObj(self,v);
                }
        } else if (key=="fade") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="updateSphere") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="pos") {
                self->updateSphere(check_v3(L,3));
        } else if (key=="renderingDistance") {
                self->updateSphere(check_float(L,3));
        } else if (key=="getSphere") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="activated") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="deactivate") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="activate") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="instance") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="hintAdvancePrepare") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="getAdvancePrepareHints") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="destroyed") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="class") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="className") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="name") {
                my_lua_error(L,"Not a writeable GritObject member: "+key);
        } else if (key=="needsFrameCallbacks") {
                self->setNeedsFrameCallbacks(self, check_bool(L,3));
        } else if (key=="needsStepCallbacks") {
                self->setNeedsStepCallbacks(self, check_bool(L,3));
        } else {
                GritClass *c = self->getClass();
                if (c==NULL) my_lua_error(L,"GritObject destroyed");
                const char *err = self->userValues.luaSet(L);
                if (err) my_lua_error(L, err);
        }

        return 0;
TRY_END
}
                push_gritcls(L,i->second);
                lua_rawseti(L,-2,c+1);
                c++;                 
        }       
        return 1;
TRY_END
}


static int global_object_add (lua_State *L)
{
TRY_START
        if (lua_gettop(L)==2) lua_newtable(L);
        check_args(L,3);
        std::string className = check_path(L,1);
        Vector3 spawnPos = check_v3(L, 2);
        int table_index = lua_gettop(L);
        if (!lua_istable(L,table_index)) my_lua_error(L,"Last parameter should be a table");
        lua_getfield(L,table_index,"name");
        std::string name;
        if (lua_isnil(L,-1)) {
                name = "";
        } else {
                if (lua_type(L,-1)!=LUA_TSTRING) my_lua_error(L,"Name wasn't a string!");
                name = lua_tostring(L,-1);
        }
        lua_pop(L,1);

        GritObjectPtr o = object_add(L,name,class_get(className));
        o->userValues.set("spawnPos", spawnPos);
        o->getClass()->get(L,"renderingDistance");
static int rbody_newindex (lua_State *L)
{
TRY_START
        check_args(L, 3);
        GET_UD_MACRO(RigidBody, self, 1, RBODY_TAG);
        const char *key = luaL_checkstring(L, 2);
        if (!::strcmp(key, "linearVelocity")) {
                Vector3 v = check_v3(L, 3);
                self.setLinearVelocity(v);
        } else if (!::strcmp(key, "angularVelocity")) {
                Vector3 v = check_v3(L, 3);
                self.setAngularVelocity(v);
        } else if (!::strcmp(key, "worldPosition")) {
                Vector3 v = check_v3(L, 3);
                self.setPosition(v);
        } else if (!::strcmp(key, "worldOrientation")) {
                Quaternion v = check_quat(L, 3);
                self.setOrientation(v);
        } else if (!::strcmp(key, "contactProcessingThreshold")) {
                float v = check_float(L, 3);
                self.setContactProcessingThreshold(v);
        } else if (!::strcmp(key, "linearDamping")) {
                float v = check_float(L, 3);
                self.setLinearDamping(v);
        } else if (!::strcmp(key, "angularDamping")) {
                float v = check_float(L, 3);
                self.setAngularDamping(v);
        } else if (!::strcmp(key, "linearSleepThreshold")) {
                float v = check_float(L, 3);
                self.setLinearSleepThreshold(v);
        } else if (!::strcmp(key, "angularSleepThreshold")) {
                float v = check_float(L, 3);
                self.setAngularSleepThreshold(v);
        } else if (!::strcmp(key, "mass")) {
                float v = check_float(L, 3);
                self.setMass(v);
        } else if (!::strcmp(key, "ghost")) {
                bool v = check_bool(L, 3);
                self.setGhost(v);
        } else if (!::strcmp(key, "updateCallback")) {
                self.updateCallbackPtr.set(L);
        } else if (!::strcmp(key, "stepCallback")) {
                self.stepCallbackPtr.set(L);
        } else if (!::strcmp(key, "collisionCallback")) {
                self.collisionCallbackPtr.set(L);
        } else if (!::strcmp(key, "stabiliseCallback")) {
                self.stabiliseCallbackPtr.set(L);
        } else if (!::strcmp(key, "inertia")) {
                Vector3 v = check_v3(L, 3);
                self.setInertia(v);
        } else if (!::strcmp(key, "owner")) {
                if (lua_isnil(L, 3)) {
                        self.owner.setNull();
                } else {
                        GET_UD_MACRO(GritObjectPtr, v, 3, GRITOBJ_TAG);
                        self.owner = v;
                }

        } else {
               my_lua_error(L, "Not a writeable RigidBody member: "+std::string(key));
        }
        return 0;
TRY_END
}