void doubleReflectionAlgorithm(Snap* prev,Snap* cur) { double v1[3]; double v2[3]; double c1; double c2; double rl[3]; double tl[3]; subVectors(cur->position,prev->position,v1); c1 = dotProduct(v1,v1); ///rl multVector((-2.0/c1)*dotProduct(v1,prev->up),v1,rl); addVectors(prev->up,rl,rl); ///tl multVector((-2.0/c1)*dotProduct(v1,prev->direction),v1,tl); addVectors(prev->direction,tl,tl); ///v2 subVectors(cur->direction,tl,v2); c2 = dotProduct(v2,v2); /// nouveau up multVector((-2.0/c2)*dotProduct(v2,rl),v2,cur->up); addVectors(rl,cur->up,cur->up); crossProduct(cur->direction,cur->up,cur->cross); }
int AreaOfInterest::actionController(CvPoint mousePointer) { if (this->contains(mousePointer)) { double sqrtP = sqrt(cornerAreaPercentage_); CvPoint tl = cvPoint(rect_.x,rect_.y); CvPoint tr = cvPoint(rect_.x+rect_.width,rect_.y); CvPoint sizeVector = cvPoint(rect_.width, rect_.height); CvPoint sizeVectorInvert = cvPoint(-rect_.width, rect_.height); /*CvPoint center = divVector(addVectors(sizeVector,tl), 2);*/ CvPoint tl_inner = addVectors(tl, mulVector(sizeVector, sqrtP)); if ((mousePointer.x <= tl_inner.x) && (mousePointer.y <= tl_inner.y)) return RESIZE_TL; CvPoint tr_inner = addVectors(tr, mulVector(sizeVectorInvert, sqrtP)); if ((mousePointer.x >= tr_inner.x) && (mousePointer.y <= tr_inner.y)) return RESIZE_TR; CvPoint br_inner = addVectors(tl, mulVector(sizeVector, 1 - sqrtP)); if ((mousePointer.x >= br_inner.x) && (mousePointer.y >= br_inner.y)) return RESIZE_BR; CvPoint bl_inner = addVectors(tr, mulVector(sizeVectorInvert, 1 - sqrtP)); if ((mousePointer.x <= bl_inner.x) && (mousePointer.y >= bl_inner.y)) return RESIZE_BL; return MOVE_RECT; } else return -1; }
int Snap::work(CImg<unsigned short> *imageSource,CImg<unsigned short> *imageResult ,float voxtabsize[3],int voxelInterp) { double point[3]; double xComp[3]; double yComp[3]; normalize(direction);normalize(up);normalize(cross); double voxsize[3] = { voxtabsize[0] , voxtabsize[1] , voxtabsize[2] }; divcompVect(position,voxsize,position); for(int x = 0; x < imageResult->width();x++) { multVector((double)(-(imageResult->width()/2) + x) ,cross,xComp); for(int y = 0; y < imageResult->height();y++) { multVector((double)( (imageResult->height()/2) - y) ,up,yComp); addVectors(xComp,yComp,point); addVectors(position,point,point); if(voxelInterp == 0) (*imageResult)(x,y,n,0) = imageSource->linear_atXYZ(point[0],point[1],point[2],0,0); else if(voxelInterp == 1) (*imageResult)(x,y,n,0) = imageSource->cubic_atXYZ(point[0],point[1],point[2],0,0); } } return 0; }
int main(int argc, char **argv){ int entries = 8; float *a = new float[entries]; float *b = new float[entries]; float *ab = new float[entries]; for(int i = 0; i < entries; ++i){ a[i] = i; b[i] = 1 - i; ab[i] = 0; } occa::device device; occa::kernel addVectors; occa::memory o_a, o_b, o_ab; occa::stream streamA, streamB; // device.setup("mode = CUDA, deviceID = 0"); device.setup("mode = OpenCL, platformID = 0, deviceID = 1"); streamA = device.getStream(); streamB = device.createStream(); o_a = device.malloc(entries*sizeof(float)); o_b = device.malloc(entries*sizeof(float)); o_ab = device.malloc(entries*sizeof(float)); addVectors = device.buildKernelFromSource("addVectors.okl", "addVectors"); o_a.copyFrom(a); o_b.copyFrom(b); device.setStream(streamA); addVectors(entries, o_a, o_b, o_ab); device.setStream(streamB); addVectors(entries, o_a, o_b, o_ab); o_ab.copyTo(ab); for(int i = 0; i < entries; ++i) std::cout << i << ": " << ab[i] << '\n'; delete [] a; delete [] b; delete [] ab; addVectors.free(); o_a.free(); o_b.free(); o_ab.free(); device.free(); }
/* Shoot ninjarope at the direction pointed by v */ static void pilot_shoot_rope(struct Pilot *pilot, Vector v) { int r; v = multVector(v,24.0); pilot->rope = create_spring(&pilot->walker.physics,6.0,10); pilot->rope->tail->vel = addVectors(pilot->walker.physics.vel,v); #if 1 for(r=pilot->rope->nodecount-1;r>=0;r--) { v = multVector(v,0.1); pilot->rope->nodes[r].vel = addVectors(pilot->walker.physics.vel,v); } #endif }
void ParticleSimulation::midpoint() { particleDerivate(temp1); scaleVector(temp1, mp_constant_tstep2); particleGetState(temp2); addVectors(temp1, temp2, temp3); particleSetState(temp3); particleDerivate(temp1); scaleVector(temp1, tstep); addVectors(temp1, temp2, temp2); particleSetState(temp2); ps.t += tstep; }
void OriginalSpaceKmeans::changeAssignment(int xIndex, int closestCluster, int threadId) { unsigned short oldAssignment = assignment[xIndex]; Kmeans::changeAssignment(xIndex, closestCluster, threadId); double *xp = x->data + xIndex * d; subVectors(sumNewCenters[threadId]->data + oldAssignment * d, xp, d); addVectors(sumNewCenters[threadId]->data + closestCluster * d, xp, d); }
/* Shoot */ static int critter_shoot(struct Critter *critter, double angle) { double x = critter->physics.x + cos(angle)* critter->physics.radius; double y = critter->physics.y + sin(angle)* critter->physics.radius; if(is_solid(Round(x),Round(y))) return 0; Vector mvel = {cos(angle) * 10, sin(angle)*10}; add_projectile(make_bullet(x,y,addVectors(critter->physics.vel,mvel))); return 1; };
//------------------------------------------------------------------------------ void calc_specular(GzRender *render, GzCoord N_orig, GzColor col, bool mulByK) { // N is already sent here after transformation GzCoord N = {0.0f,0.0f,0.0f}; normalizeVector(N_orig, N); GzCoord AccumulatedSpecResult = {0.0f, 0.0f, 0.0f}; for(int i = 0; i < render->numlights; i++) { float (*ls)[3] = static_cast<float (*)[3]>(render->lights[i]); GzCoord ls_L_orig = {ls[0][0], ls[0][1], ls[0][2]}; GzCoord E = {0, 0, -1}; GzCoord ls_L = {0.0f,0.0f,0.0f}; normalizeVector(ls_L_orig, ls_L); if (!shouldCalcLight(N, ls_L, E, N)) continue; float N_dot_L = vectorDotProduct(N, ls_L); GzCoord left = {0.0f,0.0f,0.0f}; scalarMultiply(N, N_dot_L * 2.0f, left); GzCoord R_orig = {0.0f,0.0f,0.0f}; subtractVector(left, ls_L, R_orig); GzCoord R = {0.0f,0.0f,0.0f}; normalizeVector(R_orig, R); GzCoord ls_intensity = {ls[1][0], ls[1][1], ls[1][2]}; GzCoord localResult = {0.0f,0.0f,0.0f}; float RdotE = vectorDotProduct(R,E); if (RdotE < 0) RdotE = 0; if (RdotE > 1) RdotE = 1; scalarMultiply(ls_intensity, powf(RdotE, render->spec), localResult); addVectors(localResult, AccumulatedSpecResult, AccumulatedSpecResult); } if(mulByK) vectorMulElementByElement(render->Ks, AccumulatedSpecResult, col); else { col[RED] = AccumulatedSpecResult[RED]; col[GREEN] = AccumulatedSpecResult[GREEN]; col[BLUE] = AccumulatedSpecResult[BLUE]; } }
int main(int argc, char **argv){ createLibrary(); loadFromLibrary(); occa::kernelDatabase addVectors = occa::library::loadKernelDatabase("addVectors"); deviceList_t &devices = occa::getDeviceList(); deviceList_t::iterator it = devices.begin(); while(it != devices.end()){ int entries = 5; float *a = new float[entries]; float *b = new float[entries]; float *ab = new float[entries]; occa::device &device = *(it++); occa::memory o_a, o_b, o_ab; for(int i = 0; i < entries; ++i){ a[i] = i; b[i] = 1 - i; ab[i] = 0; } o_a = device.malloc(entries*sizeof(float)); o_b = device.malloc(entries*sizeof(float)); o_ab = device.malloc(entries*sizeof(float)); int dims = 1; int itemsPerGroup(2); int groups((entries + itemsPerGroup - 1)/itemsPerGroup); device[addVectors].setWorkingDims(dims, itemsPerGroup, groups); o_a.copyFrom(a); o_b.copyFrom(b); addVectors(entries, o_a, o_b, o_ab); o_ab.copyTo(ab); for(int i = 0; i < 5; ++i) std::cout << i << ": " << ab[i] << '\n'; device[addVectors].free(); o_a.free(); o_b.free(); o_ab.free(); delete [] a; delete [] b; delete [] ab; } return 0; }
void ParticleSimulation::rungekutta() { particleGetState(temp2);//p0 //-------------------------------- particleDerivate(temp1);//k1 f scaleVector(temp1, rk_constant_k1_k4);//f * h * 1/6 addVectors(temp1, temp2, temp2);// p0 + 1/6*k1 //-------------------------------- scaleVector(temp1, rk_constant_a);//set f to h*f / 2 addVectors(temp1, temp2, temp3); particleSetState(temp3); //--------------------------------k1 particleDerivate(temp1); scaleVector(temp1, rk_constant_k2_k3); addVectors(temp1, temp2, temp2); //-------------------------------- scaleVector(temp1, rk_constant_b); addVectors(temp1, temp3, temp3); particleSetState(temp3); //--------------------------------k2 particleDerivate(temp1); scaleVector(temp1, rk_constant_k2_k3); addVectors(temp1, temp2, temp2); //-------------------------------- scaleVector(temp1, 3.0); addVectors(temp1, temp3, temp3); particleSetState(temp3); //--------------------------------k3 particleDerivate(temp1); scaleVector(temp1, rk_constant_k1_k4); addVectors(temp1, temp2, temp2); //--------------------------------k4 particleSetState(temp2); ps.t += tstep; }
void ParticleSimulation::euler() { particleDerivate(temp1); scaleVector(temp1, tstep); particleGetState(temp2); addVectors(temp1, temp2, temp2); particleSetState(temp2); ps.t += tstep; }
void Ball::handleBallCollision(vector<float> targetPosition , vector<float> targetVelocity , float targetMass , float targetRadius) { //Resolves Ball Collisions //this->setVelocity() vector<float> newPos = addVectors(this->getPosition() , ScalarMult(this->getVelocity() , DELTA_T)); // Position in next iteration vector<float> deltaPos = addVectors(newPos , ScalarMult(targetPosition , -1.0)); //R1 - R2 float speedAlongNormal = dotProduct(deltaPos , addVectors(targetVelocity , ScalarMult(this->getVelocity() , -1.0))); // (V1-V2).N float distSquare = dotProduct(deltaPos , deltaPos); if( (distSquare <= pow( this->getRadius() + targetRadius , 2)) &&( speedAlongNormal >= 0.0) ) { this->setVelocity(solveBallCollision(this->getVelocity(), targetVelocity, newPos, targetPosition, this->getMass(), targetMass , coefficientRestitution).first); /// checks and updates the balls velocity if it collides with some other ball this-> setTimeSinceCollision(BLINK_TIME); //Display changes according to timeSinceCollision } #if defined(DEBUG) || defined(BALL_DEBUG) float x = this->getMass(); float y = this->getMass() * pow(this->getVelocity(),2) * 0.5; string temp_output = "Mass =" + to_string(x) + " Energy= " + to_string(y); cout<<temp_output<<"\n"; #endif }
/* Shoot in the direction of attack_vector */ static void pilot_shoot(struct Pilot *pilot) { double xoff,yoff; xoff = pilot->attack_vector.x * (pilot->walker.physics.radius+1); yoff = pilot->attack_vector.y * (pilot->walker.physics.radius+1); add_projectile(make_bullet ( pilot->walker.physics.x + xoff, pilot->walker.physics.y + yoff, addVectors(pilot->walker.physics.vel, multVector(pilot->attack_vector,10.0)))); pilot->weap_cooloff = 7; }
vector_t averageListedPoints(const points_t points, const list_t list) { index_t i; vector_t m = initVector(0.0f, 0.0f, 0.0f); for (i=0; i<getLength(list); i++) { entry_t e=getEntry(list,i); m=addVectors(getPoint(points,entry_getIndex(&e)),m); } m=scaleVector(m,1.0f/((float)getLength(list))); return m; }
static void softmaxGradientForWholeBatch( DataSet training_set, FeatureType* gradient) { memset(gradient, 0, training_set.num_features * sizeof(FeatureType) * LABEL_CLASS); // computes softmax function for each data point in the training set std::vector<float> probabilities_of_each; size_t idx = 0; for (size_t i = 0; i < training_set.num_data_points; i++) { idx = i * training_set.num_features; //http://stackoverflow.com/questions/3177241/what-is-the-best-way-to-concatenate-two-vectors // concatenate-two-vectors to make probability vector probabilities_of_each+=softmaxFunctionFloat( training_set.parameter_vector, &training_set.data_points[idx], training_set.num_features); } //change a vector to an array //http://stackoverflow.com/questions/2923272/how-to-convert-vector-to-array-c float* probabilities_array = &probabilities_of_each[0]; //initialize to 0 by c++ std::vector<float> groundTruth(LABEL_CLASS * training_set.num_data_points); //establish groundtruth function to see softmaxExercise at UFLDL Tutorial for (size_t i = 0; i < training_set.num_data_points; i++) { int idx1 = training_set.labels[i]; groundTruth[idx1 + i * LABEL_CLASS]=1.0f; } //change a vector to an array float* groundTruth_array = &groundTruth[0]; //computing 1{P(y=k|x,theta)-1{y=k}} addVectors(probabilities_array, groundTruth_array, training_set.num_data_points * LABEL_CLASS, -1); float factor = 1.0f / training_set.num_data_points; //finish computing x*sigma (i=1..k){P(y=k|x,theta)-1{y=k}} matrixMatrixMultiply(probabilities_array, training_set.data_points, factor, training_set.num_data_points, training_set.num_features, gradient); }
vector_t normalOfListedPoints(const points_t points, const list_t list) { vector_t m = averageListedPoints(points,list); vector_t n = initVector(0.0f, 0.0f, 0.0f); vector_t d1,d2,c; index_t i; for (i=1; i<=getLength(list); i++) { entry_t e=getEntry(list,i-1); d1=subtractVectors(getPoint(points,entry_getIndex(&e)),m); e=getEntry(list,i%getLength(list)); d2=subtractVectors(getPoint(points,entry_getIndex(&e)),m); c=crossProduct(d1,d2); n=addVectors(c,n); } return n; }
vector_t directionOfListedPoints(const points_t points, const list_t list) { vector_t m = averageListedPoints(points,list); vector_t dir = initVector(0.0f, 0.0f, 0.0f); vector_t d,c; index_t i; for (i=1; i<getLength(list); i++) { entry_t e1=getEntry(list,i-1); entry_t e2=getEntry(list,i ); d=subtractVectors(getPoint(points,entry_getIndex(&e1)), getPoint(points,entry_getIndex(&e2))); dir=addVectors(d,dir); } return dir; }
// computes gradient for a single datapoint static void gradientForSinglePoint( FeatureType* parameter_vector, FeatureType* data_point, LabelType label, size_t num_features, FeatureType* gradient) { //gradient for logistic function: x * (pi(theta, x) - y) double probability_of_positive = logisticFunction(parameter_vector, data_point, num_features); memset(gradient, 0, num_features * sizeof(FeatureType)); addVectors(gradient, data_point, num_features, (probability_of_positive - label)); }
void OriginalSpaceKmeans::initialize(Dataset const *aX, unsigned short aK, unsigned short *initialAssignment, int aNumThreads) { Kmeans::initialize(aX, aK, initialAssignment, aNumThreads); centers = new Dataset(k, d); sumNewCenters = new Dataset *[numThreads]; centers->fill(0.0); for (int t = 0; t < numThreads; ++t) { sumNewCenters[t] = new Dataset(k, d, false); sumNewCenters[t]->fill(0.0); for (int i = start(t); i < end(t); ++i) { addVectors(sumNewCenters[t]->data + assignment[i] * d, x->data + i * d, d); } } // put the centers at their initial locations, based on clusterSize and // sumNewCenters move_centers(); }
//------------------------------------------------------------------------------ void calc_diffuse(GzRender *render, GzCoord N_orig, GzColor col, bool mulByK) { // N is already Ncm transformed. GzCoord N = {0.0f,0.0f,0.0f}; normalizeVector(N_orig, N); GzCoord AccumulatedDiffuseResult = {0.0f, 0.0f, 0.0f}; for(int i = 0; i < render->numlights; i++) { float (*ld)[3] = static_cast<float (*)[3]>(render->lights[i]); GzCoord ld_L_orig = {ld[0][0], ld[0][1], ld[0][2]}; GzCoord ld_L = {0.0f,0.0f,0.0f}; normalizeVector(ld_L_orig, ld_L); GzCoord E = {0,0,-1}; if (!shouldCalcLight(N, ld_L, E, N)) continue; float N_dot_L = vectorDotProduct(N, ld_L); GzCoord ld_intensity = {ld[1][0], ld[1][1], ld[1][2]}; GzCoord localResult = {0.0f,0.0f,0.0f}; scalarMultiply(ld_intensity, N_dot_L, localResult); addVectors(localResult, AccumulatedDiffuseResult, AccumulatedDiffuseResult); } if(mulByK) vectorMulElementByElement(render->Kd, AccumulatedDiffuseResult, col); else { col[RED] = AccumulatedDiffuseResult[RED]; col[GREEN] = AccumulatedDiffuseResult[GREEN]; col[BLUE] = AccumulatedDiffuseResult[BLUE]; } }
// computes gradient for the whole training set static void gradientForWholeBatch( DataSet training_set, FeatureType* gradient) { memset(gradient, 0, training_set.num_features * sizeof(FeatureType)); float* probabilities_of_positive = new float[training_set.num_data_points]; // computes logistc function for each data point in the training set size_t idx = 0; for (size_t i = 0; i < training_set.num_data_points; i++) { idx = i * training_set.num_features; probabilities_of_positive[i] = logisticFunction( training_set.parameter_vector, &training_set.data_points[idx], training_set.num_features); } // computes difference between // predicted probability and actual label: (PI - Y) addVectors(probabilities_of_positive, training_set.labels, training_set.num_data_points, -1); // finishes computation of gradient: (1/n) * X^T * (PI(theta, X) - YI) float factor = 1.0f / training_set.num_data_points; matrixVectorMultiply(training_set.data_points, probabilities_of_positive, factor, training_set.num_data_points, training_set.num_features, gradient); delete[] probabilities_of_positive; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double * prior, * transmat, * obslik; int K, T, tmp; double * delta, *changes; int changesCounter; int * psi, * path; int t,k,j; double loglik; double * d; /* buffer */ double *outputToolPtr; if ( nrhs!=3 || nlhs!=3) mexErrMsgTxt("hmmViterbiC requires 3 inputs and 3 outputs"); prior=mxGetPr(prhs[0]); transmat=mxGetPr(prhs[1]); obslik=mxGetPr(prhs[2]); /* Assuming this will allow me to take row or colums as arguments. */ K = mxGetN(prhs[0]) * mxGetM(prhs[0]); /* mexPrintf("K = %d\n", K); */ tmp = mxGetN(prhs[1]); if (tmp != K) mexErrMsgTxt("The transition matrix must be of size KxK."); tmp = mxGetM(prhs[1]); if (tmp != K) mexErrMsgTxt("The transition matrix must be of size KxK."); /* meaning that obslik is K-by-T, with the chain running along with horizontal */ T = mxGetN(prhs[2]); tmp = mxGetM(prhs[1]); if (tmp != K) mexErrMsgTxt("The obslik must have K rows."); delta = mxMalloc(K*T*sizeof(double)); psi = mxMalloc(K*T*sizeof(int)); path = mxMalloc(T*sizeof(int)); t = 0; addVectors(delta + t*K, prior, obslik + t*K, K); setVectorToValue_int(psi + t*K, 0, K); d = mxMalloc(K*sizeof(double)); /* forward */ for(t=1;t<T;++t) { for(j=0;j<K;++j) { addVectors(d, delta + (t-1)*K, transmat + j*K, K); maxVectorInPlace(delta + j + t*K, psi + j + t*K, d, K); delta[j+t*K] += obslik[j+t*K]; } } /* backward */ t = T-1; maxVectorInPlace(d, path + t, delta + t*K, K); /* using the first value of d to store junk */ loglik = d[0]; for(t=T-2;t>=0;--t) { path[t] = psi[path[t+1] + (t+1)*K]; /*mexPrintf("Setting path[%d]=%d\n", t, path[t]); */ } changes = mxMalloc(4*T*sizeof(double)); changesCounter = 0; changes[changesCounter + 0*T] = 0; changes[changesCounter + 1*T] = 0; /* overwritten */ changes[changesCounter + 2*T] = path[0]; changes[changesCounter + 3*T] = 0; changesCounter = 1; for(t=1;t<T;++t) { if (path[t] != path[t-1]) { changes[changesCounter + 0*T] = t; changes[(changesCounter-1) + 1*T] = t-1; changes[changesCounter + 2*T] = path[t]; changes[changesCounter + 3*T] = 0; /* that computeSegmentBayesFactor */ changesCounter++; } } changes[(changesCounter-1) + 1*T] = T-1; plhs[0] = mxCreateDoubleMatrix(1,T,mxREAL); outputToolPtr = mxGetPr(plhs[0]); /* Be careful to add +1 to path values. This is because C starts from 0 and Matlab starts from 1. I figured it would be easier to think in C anb convert only at the end. Hence, the path[t] + 1. */ for(t=0;t<T;++t) outputToolPtr[t] = (double)(path[t]+1); /* A junk value for the loglik for backward compatibility. We're not scaling so we don't get a loglik value. */ plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); outputToolPtr = mxGetPr(plhs[1]); outputToolPtr[0] = loglik; plhs[2] = mxCreateDoubleMatrix(changesCounter,4,mxREAL); outputToolPtr = mxGetPr(plhs[2]); for(t=0;t<changesCounter; ++t) { /* +1 because of the Matlab offset from C */ outputToolPtr[t + 0*changesCounter] = changes[t + 0*T] + 1; outputToolPtr[t + 1*changesCounter] = changes[t + 1*T] + 1; outputToolPtr[t + 2*changesCounter] = changes[t + 2*T] + 1; outputToolPtr[t + 3*changesCounter] = changes[t + 3*T]; } mxFree(delta); mxFree(psi); mxFree(path); mxFree(d); mxFree(changes); return; }
void rtShade(struct object3D *obj, struct point3D *p, struct point3D *n, struct ray3D *ray, int depth, double a, double b, struct colourRGB *col) { // This function implements the shading model as described in lecture. It takes // - A pointer to the first object intersected by the ray (to get the colour properties) // - The coordinates of the intersection point (in world coordinates) // - The normal at the point // - The ray (needed to determine the reflection direction to use for the global component, as well as for // the Phong specular component) // - The current racursion depth // - The (a,b) texture coordinates (meaningless unless texture is enabled) // // Returns: // - The colour for this ray (using the col pointer) // // struct colourRGB tmp_col; // Accumulator for colour components // Apply Ambient Light. // Ambient = r_a * I_a * 255 // = Albedo_ra * objectColor double R,G,B; double refractHit = 0; double reflectHit = 0; double numLights = 0; point3D nAdj = *n; // printf("RT SHADE CALLED.\n"); if (obj->texImg==NULL) { R = obj->col.R; G = obj->col.G; B = obj->col.B; // printf("PICTURE UNDETECTED. USING R G B = [%f, %f, %f]\n", R,G,B); } else { // printf("Getting image coordinates @ [%f, %f]\n", a, b); // Get object colour from the texture given the texture coordinates (a,b), and the texturing function // for the object. Note that we will use textures also for Photon Mapping. // printf("CHECKING IMAGE COORDTINATES [%f, %f]\n", a, b); obj->textureMap(obj->texImg,a,b,&R,&G,&B); // printf("PICTURE DETECTED. USING A B = [%f, %f]\n", a, b); // printf("DONE\n"); } // double numSources = 0.0; colourRGB reflectCol, refractCol, tmp_col; tmp_col.R = 0; tmp_col.G = 0; tmp_col.B = 0; reflectCol.R = 0; reflectCol.G = 0; reflectCol.B = 0; refractCol.R = 0; refractCol.G = 0; refractCol.B = 0; // Create a ray from light source pointLS *lightSource = light_list; while (lightSource != NULL){ // numSources += 1.0; // This will hold the colour as we process all the components of // the Phong illumination model // tmp_col.R=; // tmp_col.G=obj->col.G; // tmp_col.B=obj->col.B; // Set up ray FindFirstHit. ray3D lightDirection;// = newRay(p, &lightSource->p0); lightDirection.p0 = *p; lightDirection.d = lightSource->p0; subVectors(p, &lightDirection.d); double tempLambda; object3D *tempObject; point3D tempP; point3D tempN; // Obtain intersection information. findFirstHit(&lightDirection, &tempLambda, obj, &tempObject, &tempP, &tempN, &a, &b); if (obj->frontAndBack && dot(&nAdj, &lightDirection.d) < 0){ nAdj.px = fabs(nAdj.px) * fabs(lightDirection.d.px) / lightDirection.d.px; nAdj.py = fabs(nAdj.py) * fabs(lightDirection.d.py) / lightDirection.d.py; nAdj.pz = fabs(nAdj.pz) * fabs(lightDirection.d.pz) / lightDirection.d.pz; } // if (a > 0.0001 && b > 0.0001){ // // printf("a, b returned as %f, %f\n", a, b); // } // Apply Ambient Light. // Ambient = r_a * I_a * 255 // = Albedo_ra * objectColor tmp_col.R += (R * obj->alb.ra * lightSource->col.R);// / NUM_LIGHTS;// lightSource->col.R;// / NUM_LIGHTS; tmp_col.G += (G * obj->alb.ra * lightSource->col.G);// / NUM_LIGHTS;// lightSource->col.G;// / NUM_LIGHTS; tmp_col.B += (B * obj->alb.ra * lightSource->col.B);// / NUM_LIGHTS;// lightSource->col.B;// / NUM_LIGHTS; // printf("AMBIENT LIGHT: [%f, %f, %f]\n", tmp_col.R,tmp_col.G,tmp_col.B); // Light source is shining on object if we get here. // Set up Phong components: point3D s;// = newPoint(lightSource->p0.px - p->px, lightSource->p0.py - p->py, lightSource->p0.pz - p->pz); s.px = lightSource->p0.px - p->px; s.py = lightSource->p0.py - p->py; s.pz = lightSource->p0.pz - p->pz; s.pw = 1.0; point3D c;// = newPoint(-ray->d.px, -ray->d.py, -ray->d.pz); c.px = -ray->d.px; c.py = -ray->d.py; c.pz = -ray->d.pz; c.pw = 1.0; subVectors(p, &s); // normalize(s); point3D m; //= newPoint(2 * dot(s,n) * n->px, // 2 * dot(s,n) * n->py, // 2 * dot(s,n) * n->pz); m.px = 2 * dot(&s,&nAdj) * nAdj.px; m.py = 2 * dot(&s,&nAdj) * nAdj.py; m.pz = 2 * dot(&s,&nAdj) * nAdj.pz; subVectors(&s, &m); normalize(&m); normalize(&nAdj); normalize(&s); double dotProduct = dot(&nAdj,&s); if (tempLambda < 0.000000001 || tempLambda > 1.0000000){ // Apply Diffuse // Diffuse = r_d * I_d * max(0, n dot s) // = Albedo_rd * lightSource * max(0, n dot s) tmp_col.R += (obj->alb.rd * lightSource->col.R * max(0, dotProduct)) * R;// + tmp_col.G += (obj->alb.rd * lightSource->col.G * max(0, dotProduct)) * G;// + tmp_col.B += (obj->alb.rd * lightSource->col.B * max(0, dotProduct)) * B;// + // printf("DIFFUSE LIGHT: [%f, %f, %f]\n", tmp_col.R,tmp_col.G,tmp_col.B); // Apply Specular // Specular = r_s * I_s * max(0, c dot m) ^ shinyness // = Albedo_rs * lightSource * max(0, c dot m) ^ shinyness tmp_col.R += (obj->alb.rs * lightSource->col.R * pow(max(0, dot(&c, &m)), obj->shinyness)); tmp_col.G += (obj->alb.rs * lightSource->col.G * pow(max(0, dot(&c, &m)), obj->shinyness)); tmp_col.B += (obj->alb.rs * lightSource->col.B * pow(max(0, dot(&c, &m)), obj->shinyness)); } colourRGB reflectedColor; reflectedColor.R = 0; reflectedColor.G = 0; reflectedColor.B = 0; if (depth < MAX_DEPTH){ if (obj->alb.rs > 0){ // Apply Reflections // This involves shooting a ray out from the intersection // position and obtaining a colour from that ray. // First I will obtain the mirror vector: point3D refPoint;// = newPoint(-2.0 * dot(&ray->d, n) * n->px, // -2.0 * dot(&ray->d, n) * n->py, // -2.0 * dot(&ray->d, n) * n->pz); refPoint.px = -2.0 * dot(&ray->d, &nAdj) * nAdj.px; refPoint.py = -2.0 * dot(&ray->d, &nAdj) * nAdj.py; refPoint.pz = -2.0 * dot(&ray->d, &nAdj) * nAdj.pz; refPoint.pw = 1.0; addVectors(&ray->d, &refPoint); normalize(&refPoint); ray3D refRay;// = newRay(p, refPoint); refRay.p0 = *p; refRay.d = refPoint; // Then I will obtain the colour using mirror vector. reflectHit ++; if (reflectCol.R == 0) { rayTrace(&refRay, ++depth, &reflectedColor, tempObject); reflectCol.R = obj->alb.rg * reflectedColor.R * R * NUM_LIGHTS;// * lightSource->col.R; reflectCol.G = obj->alb.rg * reflectedColor.G * G * NUM_LIGHTS;// * lightSource->col.G; reflectCol.B = obj->alb.rg * reflectedColor.B * B * NUM_LIGHTS;// * lightSource->col.B; } // Clean up created pointers. // free(refRay); // free(refPoint); // DRT Colours } if (obj->alpha < 1){ // Apply Refractions // This involves shooting a ray out from the intersection // position, finding the angle of refraction and then // shooting a ray out in that general direction. struct point3D vVec, neg_vVec, refractDir, neg_n, neg_d; vVec.px = p->px - ray->p0.px; vVec.py = p->py - ray->p0.py; vVec.pz = p->pz - ray->p0.pz; neg_vVec.px = - vVec.px; neg_vVec.py = - vVec.py; neg_vVec.pz = - vVec.pz; neg_n.px = -nAdj.px; neg_n.py = -nAdj.py; neg_n.pz = -nAdj.pz; neg_d.px = -ray->d.px; neg_d.py = -ray->d.py; neg_d.pz = -ray->d.pz; refractDir.pw = 1.0; if (dot(n, &vVec) < 0){ // Entering medium double nr = 1 / obj->r_index; double rootContent = sqrt(1 - pow(nr, 2) * (1 - pow(dot(&neg_vVec, n), 2))); if (rootContent >= 0.0) { refractDir.px = (nr * (dot(&neg_vVec, &nAdj)- rootContent)*nAdj.px - (nr * neg_vVec.px)); refractDir.py = (nr * (dot(&neg_vVec, &nAdj)- rootContent)*nAdj.py - (nr * neg_vVec.py)); refractDir.pz = (nr * (dot(&neg_vVec, &nAdj)- rootContent)*nAdj.pz - (nr * neg_vVec.pz)); } } else { // Exiting medium double nr = obj->r_index; double rootContent = sqrt(1 - pow(nr, 2) * (1-(pow(dot(&neg_n, &neg_d), 2)))); if (rootContent >= 0.0) { refractDir.px = (nr * (dot(&neg_d, &neg_n) - rootContent) * neg_n.px - (nr * neg_d.px)); refractDir.py = (nr * (dot(&neg_d, &neg_n) - rootContent) * neg_n.py - (nr * neg_d.py)); refractDir.pz = (nr * (dot(&neg_d, &neg_n) - rootContent) * neg_n.pz - (nr * neg_d.pz)); } } ray3D refRay; refRay.p0.px = p->px + 0.00009; refRay.p0.py = p->py + 0.00009; refRay.p0.pz = p->pz + 0.00009; refRay.p0.pw = 1.0; refRay.d = refractDir; colourRGB refractedColor; refractedColor.R = 0; refractedColor.G = 0; refractedColor.B = 0; // colourRGB refractedColor; rayTrace(&refRay, ++depth, &refractedColor, obj); refractHit ++; refractCol.R += (1 - obj->alpha) * refractedColor.R * R;// * lightSource->col.R;// / NUM_LIGHTS; refractCol.G += (1 - obj->alpha) * refractedColor.G * G;// * lightSource->col.G;// / NUM_LIGHTS; refractCol.B += (1 - obj->alpha) * refractedColor.B * B;// * lightSource->col.B;// / NUM_LIGHTS; } } // tmp_col.R += reflectCol.R + refractCol.R;// + reflectCol.R + refractCol.R; // tmp_col.G += reflectCol.G + refractCol.G;// + reflectCol.G + refractCol.G; // tmp_col.B += reflectCol.B + refractCol.B;// + reflectCol.B + refractCol.B; // Move pointer to next light source. numLights ++; lightSource = lightSource->next; // Clean up created pointers. // free(m); // free(s); // free(c); // free(lightDirection); } // Finally update the color with what we've calculated. // col->R = min(drtR/numSources, 1); // col->G = min(drtG/numSources, 1); // col->B = min(drtB/numSources, 1); col->R = min(tmp_col.R + reflectCol.R/fmax(NUM_LIGHTS, 1) + refractCol.R/fmax(refractHit, 1), 1); col->G = min(tmp_col.G + reflectCol.G/fmax(NUM_LIGHTS, 1) + refractCol.G/fmax(refractHit, 1), 1); col->B = min(tmp_col.B + reflectCol.B/fmax(NUM_LIGHTS, 1) + refractCol.B/fmax(refractHit, 1), 1); // col->R = (n->px + 1) / 2; // col->G = (n->py + 1) / 2; // col->B = (n->pz + 1) / 2; // printf("RTSHADE COMPLETE %f, [%f, %f, %f]\n", numSources, col->R, col->G, col->B); return; }
int main(int argc, char **argv){ int entries = 5; //---[ Init OpenCL ]------------------ cl_int error; cl_platform_id clPlatformID = occa::cl::platformID(0); cl_device_id clDeviceID = occa::cl::deviceID(0,0); cl_context clContext = clCreateContext(NULL, 1, &clDeviceID, NULL, NULL, &error); OCCA_CL_CHECK("Device: Creating Context", error); cl_command_queue clStream = clCreateCommandQueue(clContext, clDeviceID, CL_QUEUE_PROFILING_ENABLE, &error); OCCA_CL_CHECK("Device: createStream", error); cl_mem cl_a = clCreateBuffer(clContext, CL_MEM_READ_WRITE, entries*sizeof(float), NULL, &error); cl_mem cl_b = clCreateBuffer(clContext, CL_MEM_READ_WRITE, entries*sizeof(float), NULL, &error); cl_mem cl_ab = clCreateBuffer(clContext, CL_MEM_READ_WRITE, entries*sizeof(float), NULL, &error); //==================================== float *a = new float[entries]; float *b = new float[entries]; float *ab = new float[entries]; occa::device device = occa::cl::wrapDevice(clPlatformID, clDeviceID, clContext); occa::stream stream = device.wrapStream(&clStream); device.setStream(stream); occa::kernel addVectors; occa::memory o_a, o_b, o_ab; for(int i = 0; i < entries; ++i){ a[i] = i; b[i] = 1 - i; ab[i] = 0; } o_a = device.wrapMemory(&cl_a , entries*sizeof(float)); o_b = device.wrapMemory(&cl_b , entries*sizeof(float)); o_ab = device.wrapMemory(&cl_ab, entries*sizeof(float)); addVectors = device.buildKernelFromSource("addVectors.okl", "addVectors"); o_a.copyFrom(a); o_b.copyFrom(b); addVectors(entries, o_a, o_b, o_ab); o_ab.copyTo(ab); for(int i = 0; i < 5; ++i) std::cout << i << ": " << ab[i] << '\n'; addVectors.free(); o_a.free(); o_b.free(); o_ab.free(); delete [] a; delete [] b; delete [] ab; return 0; }
int main(int argc, char **argv){ int entries = 5; // [U]nified [V]irtual [A]dressing is // disabled by default // occa::enableUVAByDefault(); occa::device device; device.setup("mode = OpenCL, platformID = 0, deviceID = 0, UVA = enabled"); // Allocate [managed] arrays that will // automatically synchronize between // the process and [device] float *a = (float*) device.managedAlloc(entries * sizeof(float)); float *b = (float*) device.managedAlloc(entries * sizeof(float)); float *ab = (float*) device.managedAlloc(entries * sizeof(float)); for(int i = 0; i < entries; ++i){ a[i] = i; b[i] = 1 - i; ab[i] = 0; } occa::kernelInfo kInfo; kInfo.addDefine("P", 10); occa::kernel addVectors = device.buildKernel("addVectors.okl", "addVectors", kInfo); // Arrays a, b, and ab are now resident // on [device] addVectors(entries, a, b, ab); // b is not const in the kernel, so we can use // dontSync(b) to manually force b to not sync occa::dontSync(b); // Finish work queued up in [device], // synchronizing a, b, and ab and // making it safe to use them again device.finish(); for(int i = 0; i < 5; ++i) std::cout << i << ": " << ab[i] << '\n'; for(int i = 0; i < entries; ++i){ if(ab[i] != (a[i] + b[i])) { throw 1; } } occa::free(a); occa::free(b); occa::free(ab); addVectors.free(); device.free(); return 0; }
int main(int argc, char **argv){ int entries = 5; float *a = new float[entries]; float *b = new float[entries]; float *ab = new float[entries]; for(int i = 0; i < entries; ++i){ a[i] = i; b[i] = 1 - i; ab[i] = 0; } std::string mode = "OpenCL"; int platformID = 1; int deviceID = 0; occa::device device; occa::kernel addVectors; occa::memory o_a, o_b, o_ab; device.setup(mode, platformID, deviceID); o_a = device.malloc(entries*sizeof(float)); o_b = device.malloc(entries*sizeof(float)); o_ab = device.malloc(entries*sizeof(float)); addVectors = device.buildKernelFromLoopy("addVectors.floopy", "addVectors", occa::useFloopy); int dims = 1; int itemsPerGroup(16); int groups((entries + itemsPerGroup - 1)/itemsPerGroup); addVectors.setWorkingDims(dims, itemsPerGroup, groups); o_a.copyFrom(a); o_b.copyFrom(b); occa::initTimer(device); occa::tic("addVectors"); addVectors(entries, o_a, o_b, o_ab); double elapsedTime = occa::toc("addVectors", addVectors); o_ab.copyTo(ab); std::cout << "Elapsed time = " << elapsedTime << " s" << std::endl; occa::printTimer(); for(int i = 0; i < 5; ++i) std::cout << i << ": " << ab[i] << '\n'; for(int i = 0; i < entries; ++i){ if(ab[i] != (a[i] + b[i])) throw 1; } delete [] a; delete [] b; delete [] ab; addVectors.free(); o_a.free(); o_b.free(); o_ab.free(); device.free(); return 0; }
///The thread void* ballThread(void* args) { BallThreadParameters* arg = (BallThreadParameters*)args ; int ID = arg->ID; float myMass = ball[ID]->getMass(); float myRadius = ball[ID]->getRadius(); #if defined(DEBUG) || defined(THREAD_DEBUG) cout << ID << " created \n"; #endif while(true) { //Thread for exitting from the thread. pthread_mutex_lock(&vecMutexThreadTerminate[ID] ); if(threadTerminate[ID]) { pthread_mutex_unlock(&vecMutexThreadTerminate[ID]); break; } pthread_mutex_unlock(&vecMutexThreadTerminate[ID] ); //Functional Code. pthread_mutex_lock(&vecMutexBallPthreads[ID]); while(numBallUpdates == 0 || !vecShouldBallUpdate[ID] ) pthread_cond_wait(&vecCondBallUpdateBegin[ID] , &vecMutexBallPthreads[ID]); while( (numBallUpdates > 0) && ( vecShouldBallUpdate[ID] ) ) { pthread_mutex_lock(&mutexStateVariableUpdate); numBallUpdates--; vecShouldBallUpdate[ID] = false; pthread_mutex_unlock(&mutexStateVariableUpdate); //Timer-related things have been started. ///Generate N messages, and push them all. for(int i=0; i<NUM_BALLS; i++) { if ( i!= ID) { //Construct a message which contains present thread's data. BallDetailsMessage msg; msg.senderID = ID; msg.senderRadius = myRadius; msg.senderMass = myMass; msg.senderVelocity = ball[ID]->getVelocity(); msg.senderPosition = addVectors(ball[ID]->getPosition() , ScalarMult( ball[ID]->getVelocity(), DELTA_T)); msg.receiverID = i; sendMessage(msg); //Send message to everyone. } //Message sent. } #if defined(DEBUG) || defined(THREAD_DEBUG) cout << "thread: " << ID << " will now begin waiting \n"; #endif //waitForMessages(ID); //Deprecated ///Process messages received. for(int i = 1 ; i< NUM_BALLS; i++) { //Pop n-1 messages. pthread_mutex_lock(&vecMutexMailBox[ID]); ///Wait to receive atleast one message. waitForMessage(ID); //ASSERT : MailBox is not empty. BallDetailsMessage msg = mailBox[ID].front(); mailBox[ID].pop(); ///BallToBall Collisions ball[ID]->handleBallCollision(msg.senderPosition , msg.senderVelocity , msg.senderMass , msg.senderRadius); //Changes the velocity,not the pthread_mutex_unlock(&vecMutexMailBox[ID]); } //Ensure that ball isn't speeding float ratio = (ball[ID]->getSpeed() / MAX_VELOCITY); if ( ratio >= 1.0) { ball[ID]->slowDown(ratio); } //Self explanotry code follows ball[ID]->handleWallCollision(table); ball[ID]->displace(DELTA_T); //Updates have ended pthread_cond_signal(&condBallUpdateComplete); } pthread_mutex_unlock(&vecMutexBallPthreads[ID]); } }
int main(int argc, char *argv[]) { // rmsPropTest(); if (argc < 3) { cout << "usage rnnsynth [config_options] config_file" << endl; cout << "config_options syntax: --<variable_name>=<variable_value>" << endl; cout << "whitespace not allowed in variable names or values" << endl; cout << "all config_file variables overwritten by config_options" << endl; cout << "setting <variable_value> = \"\" removes the variable from the " "config" << endl; cout << "repeated variables overwritten by last specified" << endl; exit(0); } ConfigFile conf(argv[argc - 2]); ConfigFile conf_1(argv[argc - 1]); bool autosave = false; string configFilename; #ifdef FAST_LOGISTIC Logistic::fill_lookup(); #endif string task = conf.get<string>("task"); CHECK_STRICT(task == "prediction", "must have prediction task"); if (task == "prediction" && conf.get<int>("predictionSteps", 1) == 1) { task = conf.set_val<string>("task", "window-prediction"); } verbose = true; ostream &out = cout; string dataset = conf.get<string>("dataset", "train"); check(in(validDatasets, dataset), dataset + " given as 'dataset' parameter in config file '" + configFilename + "'\nmust be one of '" + str(validDatasets) + "'"); string dataFileString = dataset + "File"; vector<string> dataFiles = conf.get_list<string>(dataFileString); int dataFileNum = conf.get<int>("dataFileNum", 0); check(dataFiles.size() > dataFileNum, "no " + ordinal(dataFileNum) + " file in size " + str(dataFiles.size()) + " file list " + dataFileString + " in " + configFilename); string datafile = dataFiles[dataFileNum]; DataHeader header(datafile, task, 1); PRINT(task, out); std::unique_ptr<Mdrnn> net; net.reset(new VerticalNet(out, conf, header)); // build weight container after net is created WeightContainer &wc = WeightContainer::instance(); wc.build(); Rmsprop optimiser("weight_optimiser", out, wc.weights, wc.derivatives); int numWeights = WeightContainer::instance().weights.size(); out << "loading dynamic data from " << conf.filename << endl; out << "number of weights to load " << numWeights << endl; DataExportHandler::instance().load(conf, out); Vector<real_t> weights = wc.weights; Vector<real_t> derivatives = wc.derivatives; Vector<real_t> deltas = optimiser.deltas; Vector<real_t> n = optimiser.n; Vector<real_t> g = optimiser.g; out << "loading dynamic data from " << conf_1.filename << endl; DataExportHandler::instance().load(conf_1, out); addVectors(wc.weights, weights); addVectors(wc.derivatives, derivatives); addVectors(optimiser.deltas, deltas); addVectors(optimiser.n, n); addVectors(optimiser.g, g); const std::string filename("agg.save"); ofstream fout(filename.c_str()); if (fout.is_open()) { out << "saving to " << filename << endl; fout << DataExportHandler::instance(); } else { out << "WARNING unable to save to file " << filename << endl; } return 0; }
int main(int argc, char **argv){ int entries = 5; //---[ Init CUDA ]------------------ int cuDeviceID; cudaStream_t cuStream; void *cu_a, *cu_b, *cu_ab; // Default: cuStream = 0 cudaStreamCreate(&cuStream); cudaMalloc(&cu_a , entries*sizeof(float)); cudaMalloc(&cu_b , entries*sizeof(float)); cudaMalloc(&cu_ab, entries*sizeof(float)); // ---[ Get CUDA Info ]---- CUdevice cuDevice; CUcontext cuContext; cuDeviceGet(&cuDevice, cuDeviceID); cuCtxGetCurrent(&cuContext); // ======================== //==================================== float *a = new float[entries]; float *b = new float[entries]; float *ab = new float[entries]; occa::device device = occa::cuda::wrapDevice(cuDevice, cuContext); occa::stream stream = device.wrapStream(&cuStream); device.setStream(stream); occa::kernel addVectors; occa::memory o_a, o_b, o_ab; for(int i = 0; i < entries; ++i){ a[i] = i; b[i] = 1 - i; ab[i] = 0; } o_a = device.wrapMemory(cu_a , entries*sizeof(float)); o_b = device.wrapMemory(cu_b , entries*sizeof(float)); o_ab = device.wrapMemory(cu_ab, entries*sizeof(float)); addVectors = device.buildKernelFromSource("addVectors.okl", "addVectors"); o_a.copyFrom(a); o_b.copyFrom(b); addVectors(entries, o_a, o_b, o_ab); o_ab.copyTo(ab); for(int i = 0; i < 5; ++i) std::cout << i << ": " << ab[i] << '\n'; addVectors.free(); o_a.free(); o_b.free(); o_ab.free(); delete [] a; delete [] b; delete [] ab; return 0; }