static int vec2_normalize(lua_State *L) { t_jit_vec2 v1; double s = DBL_EPSILON; double m; /* test argument type */ if(! lua_istable(L, 1)) luaL_error(L, "first argument must be a table"); table_to_vec2(L, 1, &v1); m = v1.x*v1.x + v1.y*v1.y; if (m > DBL_EPSILON) { s = jit_math_sqrt(m); v1.x /= s; v1.y /= s; } else { v1.x = s; v1.x = s; } vec2_to_table(L, &v1); return 1; }
static int vec3_intersect_line_sphere(lua_State *L) { //input arguments t_jit_vec3 line_a; //start point of line t_jit_vec3 line_b; //end point of line t_jit_vec3 center; //center of sphere float r; //sphere radius //output value t_jit_vec3 p1; //point of intersection if it exists //temp vars t_jit_vec3 EO; //vector pointing from start point of line to center of sphere t_jit_vec3 V; //vector pointing from the start point of the line to the end of the line float v, disc, d; /* test argument type */ if(! lua_istable(L, 1)) luaL_error(L, "first argument must be a table"); if(! lua_istable(L, 2)) luaL_error(L, "second argument must be a table"); if(! lua_istable(L, 3)) luaL_error(L, "third argument must be a table"); if(! lua_isnumber(L, 4)) luaL_error(L, "fourth argument must be a number"); table_to_vec3(L, 1, &line_a); table_to_vec3(L, 2, &line_b); table_to_vec3(L, 3, ¢er); r = lua_tonumber(L, 4); jit_vec3_sub(&EO, ¢er, &line_a); jit_vec3_sub(&V, &line_b, &line_a); jit_vec3_normalize(&V); v = jit_vec3_dot(&EO, &V); disc = r*r - (jit_vec3_dot(&EO, &EO) - v*v); if (disc < 0) { lua_pushboolean(L, 0); lua_pushnil(L); } else { d = jit_math_sqrt(disc); jit_vec3_scale(&V, &V, v-d); jit_vec3_add(&p1, &V, &line_a); lua_pushboolean(L, 1); vec3_to_table(L, &p1); } return 2; }
void NormalizeVelocity(double *direction) { float hypot; hypot = jit_math_sqrt(direction[x] * direction[x] + direction[y] * direction[y]);// + direction[z] * direction[z] ); if (hypot != 0.0) { direction[x] = direction[x] / hypot; direction[y] = direction[y] / hypot; //direction[z] = direction[z] / hypot; } }
t_xray_jit_levelsetseg *xray_jit_levelsetseg_new(void) { t_xray_jit_levelsetseg *x; void *m; t_jit_matrix_info info; t_atom a; if (x=(t_xray_jit_levelsetseg *)jit_object_alloc(_xray_jit_levelsetseg_class)) { x->evolve = 0; x->smooth = 0; x->point[0] = 160; x->point[1] = 120; x->pointcount = 2; x->radius = 4; x->cycles = 7; x->Na = 7; x->Ng = 3; x->Ns = 5; x->gaussKernel = NULL; jit_atom_setlong(&a, 5); xray_jit_levelsetseg_Ng(x, 0L, 1, &a); //allocate and initialize linked-list DLLcreateList( &(x->L_in) ); DLLcreateList( &(x->L_out) ); x->inside = (t_RegionStats *)jit_getbytes(sizeof(t_RegionStats)); x->outside = (t_RegionStats *)jit_getbytes(sizeof(t_RegionStats)); sqrtTwoPi = jit_math_sqrt(2*jit_math_acos(-1)); //setup backgFloat matrix jit_matrix_info_default(&info); info.type = _jit_sym_char; info.planecount = 1; x->phiName = jit_symbol_unique(); m = jit_object_new(_jit_sym_jit_matrix, &info); m = jit_object_method(m, _jit_sym_register, x->phiName); //Register matrix name if(!m) error("could not allocate internal matrix!"); jit_object_attach(x->phiName, x); x->phi = m; } else { x = NULL; } return x; }
float calcProb(t_RegionStats *inside, t_RegionStats *outside, uchar pixelValue) { float insideProb, outsideProb; if(inside->varianceSq > 0) { //calculate probability in gaussian distribution insideProb = (1/(jit_math_sqrt(inside->varianceSq)*sqrtTwoPi))* jit_math_exp( -((float)pixelValue - (inside->mean))*((float)pixelValue - (inside->mean))/(2*(inside->varianceSq))); } else { if( (float)pixelValue == inside->mean) insideProb = 1.0f; else insideProb = 0.0f; } if(outside->varianceSq > 0) { //calculate probability in gaussian distribution outsideProb = (1/(jit_math_sqrt(outside->varianceSq)*sqrtTwoPi))* jit_math_exp( -((float)pixelValue - (outside->mean))*((float)pixelValue - (outside->mean))/(2*(outside->varianceSq))); } else { if( (float)pixelValue == outside->mean) outsideProb = 1.0f; else outsideProb = 0.0f; } //post("mean: %f %f", inside->mean, outside->mean); //post("varSQ: %f %f", inside->varianceSq, outside->varianceSq); //not returning the real probability but just what's needed //to make a decision to switch_in or switch_out return (insideProb > outsideProb) ? 1.1f : 0.9f; }
float MatchAndAvoidNeighbors(t_jit_boids2d *flockPtr, long theBoid, double *matchNeighborVel, double *avoidNeighborVel) { long i, j, neighbor; double distSqr; double dist, distH, distV,distD; double tempSpeed; short numClose = 0; double totalVel[2] = {0.0,0.0}; /**********************/ /* Find the neighbors */ /**********************/ /* special case of one neighbor */ if (flockPtr->neighbors == 1) { flockPtr->boid[theBoid].neighborDistSqr[0] = kMaxLong; for (i = 0; i < flockPtr->number; i++) { if (i != theBoid) { distSqr = DistSqrToPt(flockPtr->boid[theBoid].oldPos, flockPtr->boid[i].oldPos); /* if this one is closer than the closest so far, then remember it */ if (flockPtr->boid[theBoid].neighborDistSqr[0] > distSqr) { flockPtr->boid[theBoid].neighborDistSqr[0] = distSqr; flockPtr->boid[theBoid].neighbor[0] = i; } } } } /* more than one neighbor */ else { for (j = 0; j < flockPtr->neighbors; j++) flockPtr->boid[theBoid].neighborDistSqr[j] = kMaxLong; for (i = 0 ; i < flockPtr->number; i++) { /* if this one is not me... */ if (i != theBoid) { distSqr = DistSqrToPt(flockPtr->boid[theBoid].oldPos, flockPtr->boid[i].oldPos); /* if distSqr is less than the distance at the bottom of the array, sort into array */ if (distSqr < flockPtr->boid[theBoid].neighborDistSqr[flockPtr->neighbors-1]) { j = flockPtr->neighbors - 1; /* sort distSqr in to keep array in size order, smallest first */ while ((distSqr < flockPtr->boid[theBoid].neighborDistSqr[j-1]) && (j > 0)) { flockPtr->boid[theBoid].neighborDistSqr[j] = flockPtr->boid[theBoid].neighborDistSqr[j - 1]; flockPtr->boid[theBoid].neighbor[j] = flockPtr->boid[theBoid].neighbor[j - 1]; j--; } flockPtr->boid[theBoid].neighborDistSqr[j] = distSqr; flockPtr->boid[theBoid].neighbor[j] = i; } } } } /*********************************/ /* Match and avoid the neighbors */ /*********************************/ matchNeighborVel[x] = 0; matchNeighborVel[y] = 0; //matchNeighborVel[z] = 0; // set tempSpeed to old speed tempSpeed = flockPtr->boid[theBoid].speed; for (i = 0; i < flockPtr->neighbors; i++) { neighbor = flockPtr->boid[theBoid].neighbor[i]; // calculate matchNeighborVel by averaging the neighbor velocities matchNeighborVel[x] += flockPtr->boid[neighbor].oldDir[x]; matchNeighborVel[y] += flockPtr->boid[neighbor].oldDir[y]; //matchNeighborVel[z] += flockPtr->boid[neighbor].oldDir[z]; // if distance is less than preferred distance, then neighbor influences boid distSqr = flockPtr->boid[theBoid].neighborDistSqr[i]; if (distSqr < flockPtr->prefDistSqr) { dist = jit_math_sqrt(distSqr); distH = flockPtr->boid[neighbor].oldPos[x] - flockPtr->boid[theBoid].oldPos[x]; distV = flockPtr->boid[neighbor].oldPos[y] - flockPtr->boid[theBoid].oldPos[y]; //distD = flockPtr->boid[neighbor].oldPos[z] - flockPtr->boid[theBoid].oldPos[z]; if(dist == 0.0) dist = 0.0000001; totalVel[x] = totalVel[x] - distH - (distH * ((float) flockPtr->prefdist / (dist))); totalVel[y] = totalVel[y] - distV - (distV * ((float) flockPtr->prefdist / (dist))); //totalVel[z] = totalVel[z] - distD - (distV * ((float) flockPtr->prefdist / (dist))); numClose++; } if (InFront(&(flockPtr->boid[theBoid]), &(flockPtr->boid[neighbor]))) { // adjust speed if (distSqr < flockPtr->prefDistSqr) tempSpeed /= (flockPtr->accel / 100.0); else tempSpeed *= (flockPtr->accel / 100.0); } else { if (distSqr < flockPtr->prefDistSqr) tempSpeed *= (flockPtr->accel / 100.0); else tempSpeed /= (flockPtr->accel / 100.0); } } if (numClose) { avoidNeighborVel[x] = totalVel[x] / numClose; avoidNeighborVel[y] = totalVel[y] / numClose; //avoidNeighborVel[z] = totalVel[z] / numClose; NormalizeVelocity(matchNeighborVel); } else { avoidNeighborVel[x] = 0; avoidNeighborVel[y] = 0; //avoidNeighborVel[z] = 0; } return(tempSpeed); }
void jit_boids2d_calculate_ndim(t_jit_boids2d *flockPtr, long dimcount, long *dim, long planecount, t_jit_matrix_info *out_minfo, char *bop) { long i, k; float *fop; BoidPtr boid; double tempNew_x, tempNew_y;//, tempNew_z; double tempOld_x, tempOld_y;//, tempOld_z; double delta_x, delta_y,/* delta_z,*/ azi,/* ele,*/ speed; FlightStep(flockPtr); //copy pointer so we don't have to dereference in the for loop boid = flockPtr->boid; fop = (float *)bop; switch(flockPtr->mode) { // newpos case 0: for(i=0; i < dim[0]; i++) { fop[0] = boid[i].newPos[x]; fop[1] = boid[i].newPos[y]; fop += planecount; } break; case 1: //newpos + oldpos for(i=0; i < dim[0]; i++) { fop[0] = boid[i].newPos[x]; fop[1] = boid[i].newPos[y]; fop[2] = boid[i].oldPos[x]; fop[3] = boid[i].oldPos[y]; fop += planecount; } break; case 2: //newpos + oldpos + speed-azimuth-elevation for(i=0; i < dim[0]; i++) { tempNew_x = boid[i].newPos[x]; tempNew_y = boid[i].newPos[y]; tempOld_x = boid[i].oldPos[x]; tempOld_y = boid[i].oldPos[y]; delta_x = tempNew_x - tempOld_x; delta_y = tempNew_y - tempOld_y; azi = jit_math_atan2(delta_y, delta_x) * flockPtr->r2d; speed = jit_math_sqrt(delta_x * delta_x + delta_y * delta_y ); fop[0] = tempNew_x; fop[1] = tempNew_y; fop[2] = tempOld_x; fop[3] = tempOld_y; fop[4] = speed; fop[5] = azi; fop += planecount; } break; } }
void xray_jit_crossproduct_calculate_ndim(t_xray_jit_crossproduct *obj, long dimcount, long planecount, long *dim, t_jit_matrix_info *in1_minfo, char *bip1, t_jit_matrix_info *in2_minfo, char *bip2, t_jit_matrix_info *out1_minfo, char *bop1) { long i, j; long width, height; float *fop, *fip1, *fip2; double *dop, *dip1, *dip2; long in1colspan, in1rowspan; long in2colspan, in2rowspan; long outcolspan, outrowspan; float norm1, norm2; double dnorm1, dnorm2; if (dimcount<1) return; //safety switch(dimcount) { case 1: dim[1] = 1; case 2: width = dim[0]; height = dim[1]; if (in1_minfo->type==_jit_sym_float32) { in1colspan = in1_minfo->dimstride[0]; in1rowspan = in1_minfo->dimstride[1]; in2colspan = in2_minfo->dimstride[0]; in2rowspan = in2_minfo->dimstride[1]; outcolspan = out1_minfo->dimstride[0]; outrowspan = out1_minfo->dimstride[1]; if(obj->normalize[0] == 0 && obj->normalize[1] == 0) { for(i=0; i < height; i++) { fip1 = (float *)(bip1 + i*in1rowspan); fip2 = (float *)(bip2 + i*in2rowspan); fop = (float *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { fop[3*j] = fip1[3*j+1]*fip2[3*j+2] - fip1[3*j+2]*fip2[3*j+1]; fop[3*j+1] = fip1[3*j+2]*fip2[3*j] - fip1[3*j]*fip2[3*j+2]; fop[3*j+2] = fip1[3*j]*fip2[3*j+1] - fip1[3*j+1]*fip2[3*j]; } } } else if(obj->normalize[0] == 1 && obj->normalize[1] == 0) { for(i=0; i < height; i++) { fip1 = (float *)(bip1 + i*in1rowspan); fip2 = (float *)(bip2 + i*in2rowspan); fop = (float *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { norm1 = jit_math_sqrt(fip1[3*j]*fip1[3*j] + fip1[3*j+1]*fip1[3*j+1] + fip1[3*j+2]*fip1[3*j+2]); if(norm1 != 0.0) { fop[3*j] = (fip1[3*j+1]*fip2[3*j+2] - fip1[3*j+2]*fip2[3*j+1])/norm1; fop[3*j+1] = (fip1[3*j+2]*fip2[3*j] - fip1[3*j]*fip2[3*j+2])/norm1; fop[3*j+2] = (fip1[3*j]*fip2[3*j+1] - fip1[3*j+1]*fip2[3*j])/norm1; } else { fop[3*j] = 0.0; fop[3*j+1] = 0.0; fop[3*j+2] = 0.0; } } } } else if(obj->normalize[0] == 0 && obj->normalize[1] == 1) { for(i=0; i < height; i++) { fip1 = (float *)(bip1 + i*in1rowspan); fip2 = (float *)(bip2 + i*in2rowspan); fop = (float *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { norm2 = jit_math_sqrt(fip2[3*j]*fip2[3*j] + fip2[3*j+1]*fip2[3*j+1] + fip2[3*j+2]*fip2[3*j+2]); if(norm2 != 0.0) { fop[3*j] = (fip1[3*j+1]*fip2[3*j+2] - fip1[3*j+2]*fip2[3*j+1])/norm2; fop[3*j+1] = (fip1[3*j+2]*fip2[3*j] - fip1[3*j]*fip2[3*j+2])/norm2; fop[3*j+2] = (fip1[3*j]*fip2[3*j+1] - fip1[3*j+1]*fip2[3*j])/norm2; } else { fop[3*j] = 0.0; fop[3*j+1] = 0.0; fop[3*j+2] = 0.0; } } } } else if(obj->normalize[0] == 1 && obj->normalize[1] == 1) { for(i=0; i < height; i++) { fip1 = (float *)(bip1 + i*in1rowspan); fip2 = (float *)(bip2 + i*in2rowspan); fop = (float *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { norm1 = jit_math_sqrt(fip1[3*j]*fip1[3*j] + fip1[3*j+1]*fip1[3*j+1] + fip1[3*j+2]*fip1[3*j+2]); norm2 = jit_math_sqrt(fip2[3*j]*fip2[3*j] + fip2[3*j+1]*fip2[3*j+1] + fip2[3*j+2]*fip2[3*j+2]); if(norm1*norm2 != 0.0) { fop[3*j] = (fip1[3*j+1]*fip2[3*j+2] - fip1[3*j+2]*fip2[3*j+1])/(norm1*norm2); fop[3*j+1] = (fip1[3*j+2]*fip2[3*j] - fip1[3*j]*fip2[3*j+2])/(norm1*norm2); fop[3*j+2] = (fip1[3*j]*fip2[3*j+1] - fip1[3*j+1]*fip2[3*j])/(norm1*norm2); } else { fop[3*j] = 0.0; fop[3*j+1] = 0.0; fop[3*j+2] = 0.0; } } } } } else if(in1_minfo->type==_jit_sym_float64) { in1colspan = in1_minfo->dimstride[0]; in1rowspan = in1_minfo->dimstride[1]; in2colspan = in2_minfo->dimstride[0]; in2rowspan = in2_minfo->dimstride[1]; outcolspan = out1_minfo->dimstride[0]; outrowspan = out1_minfo->dimstride[1]; if(obj->normalize[0] == 0 && obj->normalize[1] == 0) { for(i=0; i < height; i++) { dip1 = (double *)(bip1 + i*in1rowspan); dip2 = (double *)(bip2 + i*in2rowspan); dop = (double *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { dop[3*j] = dip1[3*j+1]*dip2[3*j+2] - dip1[3*j+2]*dip2[3*j+1]; dop[3*j+1] = dip1[3*j+2]*dip2[3*j] - dip1[3*j]*dip2[3*j+2]; dop[3*j+2] = dip1[3*j]*dip2[3*j+1] - dip1[3*j+1]*dip2[3*j]; } } } else if(obj->normalize[0] == 1 && obj->normalize[1] == 0) { for(i=0; i < height; i++) { dip1 = (double *)(bip1 + i*in1rowspan); dip2 = (double *)(bip2 + i*in2rowspan); dop = (double *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { dnorm1 = jit_math_sqrt(dip1[3*j]*dip1[3*j] + dip1[3*j+1]*dip1[3*j+1] + dip1[3*j+2]*dip1[3*j+2]); if(dnorm1 != 0.0) { dop[3*j] = (dip1[3*j+1]*dip2[3*j+2] - dip1[3*j+2]*dip2[3*j+1])/dnorm1; dop[3*j+1] = (dip1[3*j+2]*dip2[3*j] - dip1[3*j]*dip2[3*j+2])/dnorm1; dop[3*j+2] = (dip1[3*j]*dip2[3*j+1] - dip1[3*j+1]*dip2[3*j])/dnorm1; } else { dop[3*j] = 0.0; dop[3*j+1] = 0.0; dop[3*j+2] = 0.0; } } } } else if(obj->normalize[0] == 0 && obj->normalize[1] == 1) { for(i=0; i < height; i++) { dip1 = (double *)(bip1 + i*in1rowspan); dip2 = (double *)(bip2 + i*in2rowspan); dop = (double *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { dnorm2 = jit_math_sqrt(dip2[3*j]*dip2[3*j] + dip2[3*j+1]*dip2[3*j+1] + dip2[3*j+2]*dip2[3*j+2]); if(dnorm2 != 0.0) { dop[3*j] = (dip1[3*j+1]*dip2[3*j+2] - dip1[3*j+2]*dip2[3*j+1])/dnorm2; dop[3*j+1] = (dip1[3*j+2]*dip2[3*j] - dip1[3*j]*dip2[3*j+2])/dnorm2; dop[3*j+2] = (dip1[3*j]*dip2[3*j+1] - dip1[3*j+1]*dip2[3*j])/dnorm2; } else { dop[3*j] = 0.0; dop[3*j+1] = 0.0; dop[3*j+2] = 0.0; } } } } else if(obj->normalize[0] == 1 && obj->normalize[1] == 1) { for(i=0; i < height; i++) { dip1 = (double *)(bip1 + i*in1rowspan); dip2 = (double *)(bip2 + i*in2rowspan); dop = (double *)(bop1 + i*outrowspan); //calculate crossproduct for(j=0; j < width; j++) { dnorm1 = jit_math_sqrt(dip1[3*j]*dip1[3*j] + dip1[3*j+1]*dip1[3*j+1] + dip1[3*j+2]*dip1[3*j+2]); dnorm2 = jit_math_sqrt(dip2[3*j]*dip2[3*j] + dip2[3*j+1]*dip2[3*j+1] + dip2[3*j+2]*dip2[3*j+2]); if(dnorm1*dnorm2 != 0.0) { dop[3*j] = (dip1[3*j+1]*dip2[3*j+2] - dip1[3*j+2]*dip2[3*j+1])/(dnorm1*dnorm2); dop[3*j+1] = (dip1[3*j+2]*dip2[3*j] - dip1[3*j]*dip2[3*j+2])/(dnorm1*dnorm2); dop[3*j+2] = (dip1[3*j]*dip2[3*j+1] - dip1[3*j+1]*dip2[3*j])/(dnorm1*dnorm2); } else { dop[3*j] = 0.0; dop[3*j+1] = 0.0; dop[3*j+2] = 0.0; } } } } } break; default: break; } }