示例#1
0
文件: ray.c 项目: damora/ivr
boolean_t getrayintersect(ray3f ray, float *tin, float* tout)
{
    int i;
    float numer, denom;
    float thit;
	vector3f * n;
    boolean_t hit = FALSE;

	n = aabb.facenormals;
     
    *tin = -MAXFLOAT;
    *tout = MAXFLOAT;
	i = 0;
    while ((i < MAXPLANE) && (*tin < *tout)) {
        // compute numer, denom for ith plane
        numer = dconst[i] - dot3f(n[i], ray.orig);
        denom = dot3f(n[i], ray.dir);
        if (denom == 0.0f) {
            if (numer < 0.0f) *tin = *tout + 1.0f;
        }
        else {
            thit = numer/denom;
            if (denom > 0.0f ) {
               *tout = fmin(*tout, thit); 
            }
            else {
               *tin = fmax(*tin, thit);
            }
        }
        i++;
    }
    if (*tin < *tout) {
        if (*tin > 0.0f) {
            hit = TRUE;
//            thit = *tin;
        }
        else if (*tout > 0.0f) {
            hit = TRUE;
//            thit = *tout;
        }
    }
    return hit;
}
示例#2
0
文件: field.c 项目: aki5/marchester
static float
halfspace(float *pos, float *pl)
{
	return dot3f(pos, pl) - pl[3];
}
示例#3
0
void RandomizeBodiesSplitData(NBodyConfig config, float* position_x, float *position_y, float *position_z, float *mass,
                         float* velocity_x, float *velocity_y, float *velocity_z, float* color, float cluster_scale, float velocity_scale, int body_count)
{
    if (color)
    {
        int v = 0;
        for (int i = 0; i < body_count; i++)
        {
            color[v++] = (float) rand() / (float) RAND_MAX;
            color[v++] = (float) rand() / (float) RAND_MAX;
            color[v++] = (float) rand() / (float) RAND_MAX;
            color[v++] = 1.0f;
        }
    }
    switch (config)
    {
    default:
    case NBODY_CONFIG_RANDOM:
    {
        float scale = cluster_scale * std::max(1.0f, body_count / (1024.f));
        float vscale = velocity_scale * scale;

        int p = 0, v = 0;
        int i = 0;
        while (i < body_count)
        {
            float3 point;
            point[0] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[1] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[2] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            float lenSqr = dot3f(point, point);
            if (lenSqr > 1)
                continue;
            float3 velocity;
            velocity[0] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            velocity[1] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            velocity[2] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            lenSqr = dot3f(velocity, velocity);
            if (lenSqr > 1)
                continue;

            position_x[p] = point[0] * scale; // pos.x
            position_y[p] = point[1] * scale; // pos.y
            position_z[p] = point[2] * scale; // pos.z
            mass[p] = 1.0f; // mass

            velocity_x[v] = velocity[0] * vscale; // pos.x
            velocity_y[v] = velocity[1] * vscale; // pos.x
            velocity_z[v] = velocity[2] * vscale; // pos.x

            p++;
            v++;
            i++;
        }
    }
    break;
    case NBODY_CONFIG_SHELL:
    {
        float scale = cluster_scale;
        float vscale = scale * velocity_scale;
        float inner = 2.5f * scale;
        float outer = 4.0f * scale;

        int p = 0, v = 0;
        int i = 0;
        while (i < body_count)
        {
            float x, y, z;
            x = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            y = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            z = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;

            float3 point = { {x, y, z} };
            float len = normalize3f(point);
            if (len > 1)
                continue;

            position_x[p] =  point[0] * (inner + (outer - inner) * rand() / (float) RAND_MAX);
            position_y[p] =  point[1] * (inner + (outer - inner) * rand() / (float) RAND_MAX);
            position_z[p] =  point[2] * (inner + (outer - inner) * rand() / (float) RAND_MAX);
            mass[p] = 1.0f;

            x = 0.0f;
            y = 0.0f;
            z = 1.0f;
            float3 axis = { {x, y, z} };
            normalize3f(axis);

            if (1 - dot3f(point, axis) < 1e-6)
            {
                axis[0] = point[1];
                axis[1] = point[0];
                normalize3f(axis);
            }

            float3 vv = { {position_x[i], position_y[i], position_z[i]} };
            vv = cross3f(vv, axis);
            velocity_x[v] = vv[0] * vscale;
            velocity_y[v] = vv[1] * vscale;
            velocity_z[v] = vv[2] * vscale;

            p++;
            v++;
            i++;
        }
    }
    break;
    case NBODY_CONFIG_MWM31:       //////////////     Galaxy collision ////////////////////////////
    {
        float scale = cluster_scale;
        float vscale = scale * velocity_scale;
        float mscale = scale * scale * scale;

        std::ifstream *infile;

        switch (body_count)
        {
        case 16384:
            infile = new std::ifstream(GalaxyDataFiles[0]);
            break;
        case 24576:
            infile = new std::ifstream(GalaxyDataFiles[1]);
            break;
        case 32768:
            infile = new std::ifstream(GalaxyDataFiles[2]);
            break;
        case 65536:
            infile = new std::ifstream(GalaxyDataFiles[3]);
            break;
        case 81920:
            infile = new std::ifstream(GalaxyDataFiles[4]);
            break;
        default:
            printf("Numbodies must be one of 16384, 24576, 32768, 65536 or 81920.\n");
            exit(1);
            break;
        }

        int numPoints = 0;

        int p = 0;

        float pX, pY, pZ, vX, vY, vZ, bMass, bIDf;
        int bID;
        if (!infile->fail())
        {

            while (!(infile->eof()) && numPoints < body_count)
            {

                numPoints++;

                *infile >> bMass >> pX >> pY >> pZ >> vX >> vY >> vZ >> bIDf;

                bID = (int)bIDf;

                bMass *= mscale;

                position_x[p] = scale * pX;
                position_y[p] = scale * pY;
                position_z[p] = scale * pZ;
                mass[p] = bMass;
                velocity_x[p] = vscale * vX;
                velocity_y[p] = vscale * vY;
                velocity_z[p] = vscale * vZ;
                SetupGalaxyColor(p, color, bID);
                p++;
            }
        }
        delete infile;
    }
    break;
    case NBODY_CONFIG_EXPAND:
    {
        float scale = cluster_scale * std::max(1.0f, body_count / (1024.f));
        float vscale = scale * velocity_scale;

        int p = 0, v = 0;
        for (int i = 0; i < body_count;)
        {
            float3 point;

            point[0] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[1] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[2] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;

            float lenSqr = dot3f(point, point);
            if (lenSqr > 1)
                continue;

            position_x[p] = point[0] * scale; // pos.x
            position_y[p] = point[1] * scale; // pos.y
            position_z[p] = point[2] * scale; // pos.z
            mass[p] = 1.0f; // mass
            
            velocity_x[v] = point[0] * vscale; // pos.x
            velocity_y[v] = point[1] * vscale; // pos.x
            velocity_z[v] = point[2] * vscale; // pos.x

            p++;
            v++;
            i++;
        }
    }
    break;
    }
}