int inner_solve_dprimme(double *x, double *r, double *rnorm, double *evecs, double *evecsHat, double *UDU, int *ipivot, double *xKinvx, double *Lprojector, double *RprojectorQ, double *RprojectorX, int sizeLprojector, int sizeRprojectorQ, int sizeRprojectorX, double *sol, double eval, double shift, double eresTol, double aNormEstimate, double machEps, double *rwork, int rworkSize, primme_params *primme) { int i; /* loop variable */ int workSpaceSize; /* Size of local work array. */ int numIts; /* Number of inner iterations */ int ret; /* Return value used for error checking. */ int maxIterations; /* The maximum # iterations allowed. Depends on primme */ double *workSpace; /* Workspace needed by UDU routine */ /* QMR parameters */ double *g, *d, *delta, *w; double alpha_prev, beta, rho_prev, rho; // double ztmp; // [RMS: unused] double Theta_prev, Theta, c, sigma_prev, tau_init, tau_prev, tau; /* Parameters used to dynamically update eigenpair */ double Beta, Delta, Psi, Beta_prev, Delta_prev, Psi_prev; double eta; double dot_sol, eval_updated, eval_prev, eres2_updated, eres_updated, R; double Gamma_prev, Phi_prev; double Gamma, Phi; double gamma; /* The convergence criteria of the inner linear system must satisfy: */ /* || current residual || <= relativeTolerance * || initial residual || */ /* + absoluteTol */ double relativeTolerance; double absoluteTolerance; double LTolerance, ETolerance; /* Some constants */ double tpone = +1.0e+00, tzero = +0.0e+00; /* -------------------------------------------*/ /* Subdivide the workspace into needed arrays */ /* -------------------------------------------*/ g = rwork; d = g + primme->nLocal; delta = d + primme->nLocal; w = delta + primme->nLocal; workSpace = w + primme->nLocal; // This needs at least 2*numOrth+NumEvals) workSpaceSize = rworkSize - (int)(workSpace - rwork); /* -----------------------------------------*/ /* Set up convergence criteria by Tolerance */ /* -----------------------------------------*/ if (primme->aNorm <= 0.0L) { absoluteTolerance = aNormEstimate*machEps; eresTol = eresTol*aNormEstimate; } else { absoluteTolerance = primme->aNorm*machEps; } tau_prev = tau_init = *rnorm; /* Assumes zero initial guess */ LTolerance = eresTol; // Andreas: note that eigenresidual tol may not be achievable, because we // iterate on P(A-s)P not (A-s). But tau reflects linSys on P(A-s)P. if (primme->correctionParams.convTest == primme_adaptive) { ETolerance = max(eresTol/1.8L, absoluteTolerance); LTolerance = ETolerance; } else if (primme->correctionParams.convTest == primme_adaptive_ETolerance) { LTolerance = max(eresTol/1.8L, absoluteTolerance); ETolerance = max(tau_init*0.1L, LTolerance); } else if (primme->correctionParams.convTest == primme_decreasing_LTolerance) { relativeTolerance = pow(primme->correctionParams.relTolBase, (double)-primme->stats.numOuterIterations); LTolerance = relativeTolerance * tau_init + absoluteTolerance + eresTol; //printf(" RL %e INI %e abso %e LToler %e aNormEstimate %e \n", //relativeTolerance, tau_init, absoluteTolerance,LTolerance,aNormEstimate); } /* --------------------------------------------------------*/ /* Set up convergence criteria by max number of iterations */ /* --------------------------------------------------------*/ /* compute first total number of remaining matvecs */ maxIterations = primme->maxMatvecs - primme->stats.numMatvecs; /* Perform primme.maxInnerIterations, but do not exceed total remaining */ if (primme->correctionParams.maxInnerIterations > 0) { maxIterations = min(primme->correctionParams.maxInnerIterations, maxIterations); } /* --------------------------------------------------------*/ /* Rest of initializations */ /* --------------------------------------------------------*/ /* Assume zero initial guess */ Num_dcopy_dprimme(primme->nLocal, r, 1, g, 1); ret = apply_projected_preconditioner(g, evecs, RprojectorQ, x, RprojectorX, sizeRprojectorQ, sizeRprojectorX, xKinvx, UDU, ipivot, d, workSpace, primme); if (ret != 0) { primme_PushErrorMessage(Primme_inner_solve, Primme_apply_projected_preconditioner, ret, __FILE__, __LINE__, primme); return APPLYPROJECTEDPRECONDITIONER_FAILURE; } Theta_prev = 0.0L; eval_prev = eval; rho_prev = dist_dot(g, 1, d, 1, primme); /* Initialize recurrences used to dynamically update the eigenpair */ Beta_prev = Delta_prev = Psi_prev = tzero; Gamma_prev = Phi_prev = 0.0L; /* other initializations */ for (i = 0; i < primme->nLocal; i++) { delta[i] = tzero; sol[i] = tzero; } numIts = 0; /*----------------------------------------------------------------------*/ /*------------------------ Begin Inner Loop ----------------------------*/ /*----------------------------------------------------------------------*/ while (numIts < maxIterations) { apply_projected_matrix(d, shift, Lprojector, sizeLprojector, w, workSpace, primme); sigma_prev = dist_dot(d, 1, w, 1, primme); if (sigma_prev == 0.0L) { if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile,"Exiting because SIGMA %e\n",sigma_prev); } break; } alpha_prev = rho_prev/sigma_prev; if (fabs(alpha_prev) < machEps || fabs(alpha_prev) > 1.0L/machEps){ if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile,"Exiting because ALPHA %e\n",alpha_prev); } break; } Num_axpy_dprimme(primme->nLocal, -alpha_prev, w, 1, g, 1); Theta = dist_dot(g, 1, g, 1, primme); Theta = sqrt(Theta); Theta = Theta/tau_prev; c = 1.0L/sqrt(1+Theta*Theta); tau = tau_prev*Theta*c; gamma = c*c*Theta_prev*Theta_prev; eta = alpha_prev*c*c; Num_scal_dprimme(primme->nLocal, gamma, delta, 1); Num_axpy_dprimme(primme->nLocal, eta, d, 1, delta, 1); Num_axpy_dprimme(primme->nLocal, tpone, delta, 1, sol, 1); numIts++; if (fabs(rho_prev) == 0.0L ) { if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile,"Exiting because abs(rho) %e\n", fabs(rho_prev)); } break; } if (tau < LTolerance) { if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile, " tau < LTol %e %e\n",tau, LTolerance); } break; } else if (primme->correctionParams.convTest == primme_adaptive_ETolerance || primme->correctionParams.convTest == primme_adaptive) { /* --------------------------------------------------------*/ /* Adaptive stopping based on dynamic monitoring of eResid */ /* --------------------------------------------------------*/ /* Update the Ritz value and eigenresidual using the */ /* following recurrences. */ Delta = gamma*Delta_prev + eta*rho_prev; Beta = Beta_prev - Delta; Phi = gamma*gamma*Phi_prev + eta*eta*sigma_prev; Psi = gamma*Psi_prev + gamma*Phi_prev; Gamma = Gamma_prev + 2.0L*Psi + Phi; /* Perform the update: update the eigenvalue and the square of the */ /* residual norm. */ dot_sol = dist_dot(sol, 1, sol, 1, primme); eval_updated = shift + (eval - shift + 2*Beta + Gamma)/(1 + dot_sol); eres2_updated = (tau*tau)/(1 + dot_sol) + ((eval - shift + Beta)*(eval - shift + Beta))/(1 + dot_sol) - (eval_updated - shift)*(eval_updated - shift); /* If numerical problems, let eres about the same as tau */ if (eres2_updated < 0){ eres_updated = sqrt( (tau*tau)/(1 + dot_sol) ); } else eres_updated = sqrt(eres2_updated); /* --------------------------------------------------------*/ /* Stopping criteria */ /* --------------------------------------------------------*/ R = max(0.9878, sqrt(tau/tau_prev))*sqrt(1+dot_sol); if ( tau <= R*eres_updated || eres_updated <= tau*R ) { if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile, " tau < R eres \n"); } break; } if (primme->target == primme_smallest && eval_updated > eval_prev) { if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile, "eval_updated > eval_prev\n"); } break; } else if (primme->target == primme_largest && eval_updated < eval_prev){ if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile, "eval_updated < eval_prev\n"); } break; } if (eres_updated < ETolerance) { // tau < LTol has been checked if (primme->printLevel >= 5 && primme->procID == 0) { fprintf(primme->outputFile, "eres < eresTol %e \n",eres_updated); } break; } eval_prev = eval_updated; if (primme->printLevel >= 4 && primme->procID == 0) { fprintf(primme->outputFile, "INN MV %d Sec %e Eval %e Lin|r| %.3e EV|r| %.3e\n", primme->stats. numMatvecs, primme_wTimer(0), eval_updated, tau, eres_updated); fflush(primme->outputFile); } /* --------------------------------------------------------*/ } /* End of if adaptive JDQMR section */ /* --------------------------------------------------------*/ else if (primme->printLevel >= 4 && primme->procID == 0) { // Report for non adaptive inner iterations fprintf(primme->outputFile, "INN MV %d Sec %e Lin|r| %e\n", primme->stats.numMatvecs, primme_wTimer(0),tau); fflush(primme->outputFile); } if (numIts < maxIterations) { ret = apply_projected_preconditioner(g, evecs, RprojectorQ, x, RprojectorX, sizeRprojectorQ, sizeRprojectorX, xKinvx, UDU, ipivot, w, workSpace, primme); if (ret != 0) { primme_PushErrorMessage(Primme_inner_solve, Primme_apply_projected_preconditioner, ret, __FILE__, __LINE__, primme); ret = APPLYPROJECTEDPRECONDITIONER_FAILURE; break; } rho = dist_dot(g, 1, w, 1, primme); beta = rho/rho_prev; Num_scal_dprimme(primme->nLocal, beta, d, 1); Num_axpy_dprimme(primme->nLocal, tpone, w, 1, d, 1); rho_prev = rho; tau_prev = tau; Theta_prev = Theta; Delta_prev = Delta; Beta_prev = Beta; Phi_prev = Phi; Psi_prev = Psi; Gamma_prev = Gamma; } /* --------------------------------------------------------*/ } /* End of QMR main while loop */ /* --------------------------------------------------------*/ *rnorm = eres_updated; return 0; }

void PithExtractor::process( Billon &billon ) const { billon._pith.clear(); const int width = billon.n_cols; const int height = billon.n_rows; const int depth = billon.n_slices; uiCoord2D coordPrec, coordCurrent; float maxStandList[depth]; float *maxStandList2 = 0; float shift,houghStandThreshold; int max,x,y, nbContourPoints; max = x = y = 0; //extraction de la moelle de la premiere coupe sur la totalité de la coupe //nous permet d'avoir un coordonnée a laquelle appliqué la fenetre transHough( billon.slice(0), width, height, &x, &y, &max, &nbContourPoints ); //calcul la coordonnée d'origine de la fenetre x = x - (_windowWidth/2); x = x < 0 ? 0 : (x > (width - _windowWidth) ? width - _windowWidth : x); y = y - (_windowHeight/2); y = y < 0 ? 0 : (y > (height - _windowHeight) ? height - _windowHeight : y); //extraction de la moelle de la premiere coupe sur la coupe redimensionnée coordCurrent = transHough( billon.slice(0), _windowWidth, _windowHeight, &x, &y, &max, &nbContourPoints ); billon._pith.append(coordCurrent); maxStandList[0] = ((float)max)/nbContourPoints; //extraction des coupes suivantes std::cerr << "extractMoelle : "; for(int i=1; i<depth; i++) { //~ if(!listeCoupe->contains(i)){ std::cerr << " " << i; coordPrec = billon._pith.last(); x = x - (_windowWidth/2); x = x < 0 ? 0 : (x > (width - _windowWidth) ? width - _windowWidth : x); y = y - (_windowHeight/2); y = y < 0 ? 0 : (y > (height - _windowHeight) ? height - _windowHeight : y); //extraction de la moelle de la coupe i coordCurrent = transHough( billon.slice(i), _windowWidth, _windowHeight, &x, &y, &max, &nbContourPoints ); billon._pith.append(coordCurrent); shift = sqrt(pow((double) ((int)(billon._pith.last().x) - (int)(coordPrec.x)),(double) 2.0) + pow( (double)((int)(billon._pith.last().y) - (int)(coordPrec.y)), (double)2.0) ); //si le resultat obtenu a un decalage trop important avec la coupe precedente alors on recommence l'extraction sur l'ensemble de la coupe if(shift > _pithLag && width > _windowWidth && height > _windowHeight){ std::cerr << "*"; // std::cerr << " :> decalage=" << decalage << ">" << _pithLag << "\n"; billon._pith.pop_back(); x = y = 0; transHough(billon.slice(i), width, height, &x, &y, &max, &nbContourPoints ); x = x - (_windowWidth/2); x = x < 0 ? 0 : (x > (width - _windowWidth) ? width - _windowWidth : x); y = y - (_windowHeight/2); y = y < 0 ? 0 : (y > (height - _windowHeight) ? height - _windowHeight : y); coordCurrent = transHough( billon.slice(i), _windowWidth, _windowHeight, &x, &y, &max, &nbContourPoints ); billon._pith.append(coordCurrent); } maxStandList[i] = ((float)max)/nbContourPoints; if (!(i % 20)) { std::cerr << std::endl; } //~ }else{ //~ listMoelle->append(listMoelle->last()); //~ listMaxStand[i] = 0; //~ } } std::cerr << "\nnbCoupes=" << depth << " listMoelle.size()=" << billon._pith.size() << "\n"; //calcul du seuil a partir du quel les coupes sont considerées comme erronées maxStandList2 = new float[depth]; memcpy(maxStandList2, maxStandList, depth*sizeof(float)); qsort(maxStandList2, depth, sizeof(float), &floatCompare); int nfc = (_falseCutPercent*depth)/100; houghStandThreshold = nfc > 0 ? (maxStandList2[nfc]+maxStandList2[nfc-1])/2 : maxStandList2[nfc]; delete [] maxStandList2; // applique la coorection a la moelle correctPith(billon._pith, maxStandList, houghStandThreshold); }

void Foam::uniformTotalPressureFvPatchScalarField::updateCoeffs ( const vectorField& Up ) { if (updated()) { return; } scalar p0 = pressure_->value(this->db().time().timeOutputValue()); const fvsPatchField<scalar>& phip = patch().lookupPatchField<surfaceScalarField, scalar>(phiName_); if (psiName_ == "none" && rhoName_ == "none") { operator==(p0 - 0.5*(1.0 - pos(phip))*magSqr(Up)); } else if (rhoName_ == "none") { const fvPatchField<scalar>& psip = patch().lookupPatchField<volScalarField, scalar>(psiName_); if (gamma_ > 1.0) { scalar gM1ByG = (gamma_ - 1.0)/gamma_; operator== ( p0 /pow ( (1.0 + 0.5*psip*gM1ByG*(1.0 - pos(phip))*magSqr(Up)), 1.0/gM1ByG ) ); } else { operator==(p0/(1.0 + 0.5*psip*(1.0 - pos(phip))*magSqr(Up))); } } else if (psiName_ == "none") { const fvPatchField<scalar>& rho = patch().lookupPatchField<volScalarField, scalar>(rhoName_); operator==(p0 - 0.5*rho*(1.0 - pos(phip))*magSqr(Up)); } else { FatalErrorInFunction << " rho or psi set inconsitently, rho = " << rhoName_ << ", psi = " << psiName_ << ".\n" << " Set either rho or psi or neither depending on the " "definition of total pressure.\n" << " Set the unused variables to 'none'.\n" << " on patch " << this->patch().name() << " of field " << this->dimensionedInternalField().name() << " in file " << this->dimensionedInternalField().objectPath() << exit(FatalError); } fixedValueFvPatchScalarField::updateCoeffs(); }

int main (int argc, char ** argv) { if (argc != 2) { std::cout << "Usage: " << argv[0] << " [depthMap.yml]" << std::endl; return 1; } IplImage * tmp = (IplImage*)cvLoad(argv[1]); cv::Mat depth (tmp); cv::Mat boundary (depth.rows, depth.cols, CV_8UC3); enum RegionType {Shadow, Veil, Object}; for (size_t j = 0; j < boundary.rows; ++j) { for (size_t i = 0; i < boundary.cols; ++i) { boundary.at<cv::Vec3b>(j, i)[0] = boundary.at<cv::Vec3b>(j, i)[1] = boundary.at<cv::Vec3b>(j, i)[2] = 0; if (depth.at<float>(j, i) <= 0) { boundary.at<cv::Vec3b>(j, i)[Shadow] = 255; } else { boundary.at<cv::Vec3b>(j, i)[Object] = 255; } } } cv::Mat mean (boundary.rows, boundary.cols, CV_32FC1); cv::Mat var (boundary.rows, boundary.cols, CV_32FC1); int window = 2; float sumVar = 0; int nVar = 0; for (size_t j = 0; j < boundary.rows; ++j) { for (size_t i = 0; i < boundary.cols; ++i) { if (boundary.at<cv::Vec3b>(j, i)[Shadow] == 255) { continue; } float sum = 0; float sum2 = 0; int n = 0; int left = MAX(0, (int)i - window); int right = MIN (boundary.cols - 1, (int)i + window); int top = MAX (0, (int)j - window); int bottom = MIN (boundary.rows - 1, (int)j + window); for (int x = left; x < right; ++x) { for (int y = top; y < bottom; ++y) { float horz = fabs(depth.at<float>(y, x) - depth.at<float>(y, x + 1)); float vert = fabs(depth.at<float>(y, x) - depth.at<float>(y + 1, x)); if (boundary.at<cv::Vec3b>(y, x + 1)[Shadow] == 0) { sum += horz; sum2 += horz*horz; ++n; } if (boundary.at<cv::Vec3b>(y + 1, x)[Shadow] == 0) { sum += vert; sum2 += vert*vert; ++n; } } } if (n) { mean.at<float>(j, i) = sum/n; var.at<float>(j, i) = sum2/n - pow (mean.at<float>(j, i), 2.0); sumVar += var.at<float>(j, i); ++nVar; } else { mean.at<float>(j, i) = 0; var.at<float>(j, i) = 0; } } } float meanVar = sumVar/nVar; for (size_t j = 0; j < boundary.rows; ++j) { for (size_t i = 0; i < boundary.cols; ++i) { if (var.at<float>(j, i) > meanVar) { boundary.at<cv::Vec3b>(j, i)[Veil] = 255; } } } cv::imwrite("foo.jpg", boundary); std::ofstream output ("foo.csv"); for (size_t j = 0; j < var.rows; ++j) { for (size_t i = 0; i < var.cols; ++i) { output << depth.at<float>(j,i) << " "; } output << std::endl; } output.close(); cvReleaseImage (&tmp); return 0; }

void sdvd(int g[2][K], float es_ovr_n0, long channel_length, float *channel_output_vector, int *decoder_output_matrix) { int i, j, l, ll; /* loop variables */ long t; /* time */ int memory_contents[K]; /* input + conv. encoder sr */ int input[TWOTOTHEM][TWOTOTHEM]; /* maps current/nxt sts to input */ int output[TWOTOTHEM][2]; /* gives conv. encoder output */ int nextstate[TWOTOTHEM][2]; /* for current st, gives nxt given input */ int accum_err_metric[TWOTOTHEM][2]; /* accumulated error metrics */ int state_history[TWOTOTHEM][K * 5 + 1]; /* state history table */ int state_sequence[K * 5 + 1]; /* state sequence list */ int *channel_output_matrix; /* ptr to input matrix */ int binary_output[2]; /* vector to store binary enc output */ int branch_output[2]; /* vector to store trial enc output */ int m, n, number_of_states, depth_of_trellis, step, branch_metric, sh_ptr, sh_col, x, xx, h, hh, next_state, last_stop; /* misc variables */ /* ************************************************************************** */ /* n is 2^1 = 2 for rate 1/2 */ n = 2; /* m (memory length) = K - 1 */ m = K - 1; /* number of states = 2^(K - 1) = 2^m for k = 1 */ number_of_states = (int) pow(2, m); /* little degradation in performance achieved by limiting trellis depth to K * 5--interesting to experiment with smaller values and measure the resulting degradation. */ depth_of_trellis = K * 5; /* initialize data structures */ for (i = 0; i < number_of_states; i++) { for (j = 0; j < number_of_states; j++) input[i][j] = 0; for (j = 0; j < n; j++) { nextstate[i][j] = 0; output[i][j] = 0; } for (j = 0; j <= depth_of_trellis; j++) { state_history[i][j] = 0; } /* initial accum_error_metric[x][0] = zero */ accum_err_metric[i][0] = 0; /* by setting accum_error_metric[x][1] to INT_MAX, we don't need a flag */ /* so I don't get any more questions about this: */ /* INT_MAX is simply the largest possible integer, defined in limits.h */ accum_err_metric[i][1] = INT_MAX; } /* generate the state transition matrix, output matrix, and input matrix - input matrix shows how FEC encoder bits lead to next state - next_state matrix shows next state given current state and input bit - output matrix shows FEC encoder output bits given current presumed encoder state and encoder input bit--this will be compared to actual received symbols to determine metric for corresponding branch of trellis */ for (j = 0; j < number_of_states; j++) { for (l = 0; l < n; l++) { next_state = nxt_stat(j, l, memory_contents); input[j][next_state] = l; /* now compute the convolutional encoder output given the current state number and the input value */ branch_output[0] = 0; branch_output[1] = 0; for (i = 0; i < K; i++) { branch_output[0] ^= memory_contents[i] & g[0][i]; branch_output[1] ^= memory_contents[i] & g[1][i]; } /* next state, given current state and input */ nextstate[j][l] = next_state; /* output in decimal, given current state and input */ output[j][l] = bin2deci(branch_output, 2); } /* end of l for loop */ } /* end of j for loop */ #ifdef DEBUG printf("\nInput:"); for (j = 0; j < number_of_states; j++) { printf("\n"); for (l = 0; l < number_of_states; l++) printf("%2d ", input[j][l]); } /* end j for-loop */ printf("\nOutput:"); for (j = 0; j < number_of_states; j++) { printf("\n"); for (l = 0; l < n; l++) printf("%2d ", output[j][l]); } /* end j for-loop */ printf("\nNext State:"); for (j = 0; j < number_of_states; j++) { printf("\n"); for (l = 0; l < n; l++) printf("%2d ", nextstate[j][l]); } /* end j for-loop */ #endif channel_output_matrix = malloc( channel_length * sizeof(int) ); if (channel_output_matrix == NULL) { printf( "\nsdvd.c: Can't allocate memory for channel_output_matrix! Aborting..."); exit(1); } /* now we're going to rearrange the channel output so it has n rows, and n/2 columns where each row corresponds to a channel symbol for a given bit and each column corresponds to an encoded bit */ channel_length = channel_length / n; /* interesting to compare performance of fixed vs adaptive quantizer */ /* init_quantizer(); */ init_adaptive_quant(es_ovr_n0); /* quantize the channel output--convert float to short integer */ /* channel_output_matrix = reshape(channel_output, n, channel_length) */ for (t = 0; t < (channel_length * n); t += n) { for (i = 0; i < n; i++) *(channel_output_matrix + (t / n) + (i * channel_length) ) = soft_quant( *(channel_output_vector + (t + i) ) ); } /* end t for-loop */ /* ************************************************************************** */ /* End of setup. Start decoding of channel outputs with forward traversal of trellis! Stop just before encoder-flushing bits. */ for (t = 0; t < channel_length - m; t++) { if (t <= m) /* assume starting with zeroes, so just compute paths from all-zeroes state */ step = pow(2, m - t * 1); else step = 1; /* we're going to use the state history array as a circular buffer so we don't have to shift the whole thing left after each bit is processed so that means we need an appropriate pointer */ /* set up the state history array pointer for this time t */ sh_ptr = (int) ( ( t + 1 ) % (depth_of_trellis + 1) ); /* repeat for each possible state */ for (j = 0; j < number_of_states; j+= step) { /* repeat for each possible convolutional encoder output n-tuple */ for (l = 0; l < n; l++) { branch_metric = 0; /* compute branch metric per channel symbol, and sum for all channel symbols in the convolutional encoder output n-tuple */ #ifdef SLOWACS /* convert the decimal representation of the encoder output to binary */ deci2bin(output[j][l], n, binary_output); /* compute branch metric per channel symbol, and sum for all channel symbols in the convolutional encoder output n-tuple */ for (ll = 0; ll < n; ll++) { branch_metric = branch_metric + soft_metric( *(channel_output_matrix + ( ll * channel_length + t )), binary_output[ll] ); } /* end of 'll' for loop */ #endif #ifdef FASTACS /* this only works for n = 2, but it's fast! */ /* convert the decimal representation of the encoder output to binary */ binary_output[0] = ( output[j][l] & 0x00000002 ) >> 1; binary_output[1] = output[j][l] & 0x00000001; /* compute branch metric per channel symbol, and sum for all channel symbols in the convolutional encoder output n-tuple */ branch_metric = branch_metric + abs( *( channel_output_matrix + ( 0 * channel_length + t ) ) - 7 * binary_output[0] ) + abs( *( channel_output_matrix + ( 1 * channel_length + t ) ) - 7 * binary_output[1] ); #endif /* now choose the surviving path--the one with the smaller accumlated error metric... */ if ( accum_err_metric[ nextstate[j][l] ] [1] > accum_err_metric[j][0] + branch_metric ) { /* save an accumulated metric value for the survivor state */ accum_err_metric[ nextstate[j][l] ] [1] = accum_err_metric[j][0] + branch_metric; /* update the state_history array with the state number of the survivor */ state_history[ nextstate[j][l] ] [sh_ptr] = j; } /* end of if-statement */ } /* end of 'l' for-loop */ } /* end of 'j' for-loop -- we have now updated the trellis */ /* for all rows of accum_err_metric, move col 2 to col 1 and flag col 2 */ for (j = 0; j < number_of_states; j++) { accum_err_metric[j][0] = accum_err_metric[j][1]; accum_err_metric[j][1] = INT_MAX; } /* end of 'j' for-loop */ /* now start the traceback, if we've filled the trellis */ if (t >= depth_of_trellis - 1) { /* initialize the state_sequence vector--probably unnecessary */ for (j = 0; j <= depth_of_trellis; j++) state_sequence[j] = 0; /* find the element of state_history with the min. accum. error metric */ /* since the outer states are reached by relatively-improbable runs of zeroes or ones, search from the top and bottom of the trellis in */ x = INT_MAX; for (j = 0; j < ( number_of_states / 2 ); j++) { if ( accum_err_metric[j][0] < accum_err_metric[number_of_states - 1 - j][0] ) { xx = accum_err_metric[j][0]; hh = j; } else { xx = accum_err_metric[number_of_states - 1 - j][0]; hh = number_of_states - 1 - j; } if ( xx < x) { x = xx; h = hh; } } /* end 'j' for-loop */ #ifdef NORM /* interesting to experiment with different numbers of bits in the accumulated error metric--does performance decrease with fewer bits? */ /* if the smallest accum. error metric value is > MAXMETRIC, normalize the accum. errror metrics by subtracting the value of the smallest one from all of them (making the smallest = 0) and saturate all other metrics at MAXMETRIC */ if (x > MAXMETRIC) { for (j = 0; j < number_of_states; j++) { accum_err_metric[j][0] = accum_err_metric[j][0] - x; if (accum_err_metric[j][0] > MAXMETRIC) accum_err_metric[j][0] = MAXMETRIC; } /* end 'j' for-loop */ } #endif /* now pick the starting point for traceback */ state_sequence[depth_of_trellis] = h; /* now work backwards from the end of the trellis to the oldest state in the trellis to determine the optimal path. The purpose of this is to determine the most likely state sequence at the encoder based on what channel symbols we received. */ for (j = depth_of_trellis; j > 0; j--) { sh_col = j + ( sh_ptr - depth_of_trellis ); if (sh_col < 0) sh_col = sh_col + depth_of_trellis + 1; state_sequence[j - 1] = state_history[ state_sequence[j] ] [sh_col]; } /* end of j for-loop */ /* now figure out what input sequence corresponds to the state sequence in the optimal path */ *(decoder_output_matrix + t - depth_of_trellis + 1) = input[ state_sequence[0] ] [ state_sequence[1] ]; } /* end of if-statement */ } /* end of 't' for-loop */ /* ************************************************************************** */ /* now decode the encoder flushing channel-output bits */ for (t = channel_length - m; t < channel_length; t++) { /* set up the state history array pointer for this time t */ sh_ptr = (int) ( ( t + 1 ) % (depth_of_trellis + 1) ); /* don't need to consider states where input was a 1, so determine what is the highest possible state number where input was 0 */ last_stop = number_of_states / pow(2, t - channel_length + m); /* repeat for each possible state */ for (j = 0; j < last_stop; j++) { branch_metric = 0; deci2bin(output[j][0], n, binary_output); /* compute metric per channel bit, and sum for all channel bits in the convolutional encoder output n-tuple */ for (ll = 0; ll < n; ll++) { branch_metric = branch_metric + soft_metric( *(channel_output_matrix + (ll * channel_length + t)), binary_output[ll] ); } /* end of 'll' for loop */ /* now choose the surviving path--the one with the smaller total metric... */ if ( (accum_err_metric[ nextstate[j][0] ][1] > accum_err_metric[j][0] + branch_metric) /*|| flag[ nextstate[j][0] ] == 0*/) { /* save a state metric value for the survivor state */ accum_err_metric[ nextstate[j][0] ][1] = accum_err_metric[j][0] + branch_metric; /* update the state_history array with the state number of the survivor */ state_history[ nextstate[j][0] ][sh_ptr] = j; } /* end of if-statement */ } /* end of 'j' for-loop */ /* for all rows of accum_err_metric, swap columns 1 and 2 */ for (j = 0; j < number_of_states; j++) { accum_err_metric[j][0] = accum_err_metric[j][1]; accum_err_metric[j][1] = INT_MAX; } /* end of 'j' for-loop */ /* now start the traceback, if i >= depth_of_trellis - 1*/ if (t >= depth_of_trellis - 1) { /* initialize the state_sequence vector */ for (j = 0; j <= depth_of_trellis; j++) state_sequence[j] = 0; /* find the state_history element with the minimum accum. error metric */ x = accum_err_metric[0][0]; h = 0; for (j = 1; j < last_stop; j++) { if (accum_err_metric[j][0] < x) { x = accum_err_metric[j][0]; h = j; } /* end if */ } /* end 'j' for-loop */ #ifdef NORM /* if the smallest accum. error metric value is > MAXMETRIC, normalize the accum. errror metrics by subtracting the value of the smallest one from all of them (making the smallest = 0) and saturate all other metrics at MAXMETRIC */ if (x > MAXMETRIC) { for (j = 0; j < number_of_states; j++) { accum_err_metric[j][0] = accum_err_metric[j][0] - x; if (accum_err_metric[j][0] > MAXMETRIC) { accum_err_metric[j][0] = MAXMETRIC; } /* end if */ } /* end 'j' for-loop */ } #endif state_sequence[depth_of_trellis] = h; /* now work backwards from the end of the trellis to the oldest state in the trellis to determine the optimal path. The purpose of this is to determine the most likely state sequence at the encoder based on what channel symbols we received. */ for (j = depth_of_trellis; j > 0; j--) { sh_col = j + ( sh_ptr - depth_of_trellis ); if (sh_col < 0) sh_col = sh_col + depth_of_trellis + 1; state_sequence[j - 1] = state_history[ state_sequence[j] ][sh_col]; } /* end of j for-loop */ /* now figure out what input sequence corresponds to the optimal path */ *(decoder_output_matrix + t - depth_of_trellis + 1) = input[ state_sequence[0] ][ state_sequence[1] ]; } /* end of if-statement */ } /* end of 't' for-loop */ for (i = 1; i < depth_of_trellis - m; i++) *(decoder_output_matrix + channel_length - depth_of_trellis + i) = input[ state_sequence[i] ] [ state_sequence[i + 1] ]; /* free the dynamically allocated array storage area */ free(channel_output_matrix); return; } /* end of function sdvd */

static void LIQSS2_recomputeNextTimes(uinteger STATES, LIQSS2_quantizerState p, modelica_real t, modelica_real ft, int dt_index){ modelica_real diffQ = 0, timeaux = 0; uinteger i =0, j =0; for(j=0;j<p->nSD[dt_index];j++){ i= p->SD[dt_index][j]; // for(j=0;j<STATES;j++){ // i=j; // if(t>0.00001 && t<0.00001001){ // printf("i: %d\n", i); // printf("t: %.20f\n", t); // printf("p->nTime[i]: %.20f \n", p->nTime[i]); // } if(1){ // printf("LIQSS2_recomputeNextTimes\n"); // printf("i: %d\n", i); // printf("t: %.20f\n", t); // printf("p->nTime[i]: %.20f\n", p->nTime[i]); // printf("p->q[i][1]: %.20f \n", p->q[i][1]); // printf("p->x[i][1]: %.20f \n", p->x[i][1]); } if(p->ltq[i] == t){ diffQ = p->q[i][0] - p->qAux[i]; if(fabs(diffQ) > p->lqu[i] * 1e-6){ p->a[i] = (p->x[i][1] - p->oldDx[i]) / diffQ; if(p->a[i] > 0) p->a[i] = 0; // printf("! a[var]: %.20f\n", p->a[i]); // printf("! x[cf1]: %.20f\n", p->x[i][1]); // printf("! oldDx[var]: %.20f\n", p->oldDx[i]); // printf("! diffQ: %.20f\n", diffQ); } } else p->flag3[i] = false; p->u0[i] = p->x[i][1] - p->q[i][0] * p->a[i]; p->u1[i] = 2 * p->x[i][2] - p->q[i][1] * p->a[i]; p->lt[i] = t; if(p->flag4[i]) p->nTime[i] = t; else{ p->diffxq[i][1] = p->q[i][1] - p->x[i][1]; // if(1){ // printf("p->q[i][1] %.20f\n", p->q[i][1]); // printf("p->x[i][1] %.20f\n", p->x[i][1]); // printf("p->diffxq[i][1] %.20f\n", p->diffxq[i][1]); // } p->diffxq[i][2] = -p->x[i][2]; p->diffxq[i][0] = p->q[i][0] - p->dq[i] + p->lqu[i] - p->x[i][0]; p->nTime[i] = t + minRootPos(p->diffxq,i,2); // printf("nTime[var]: %.20f\n", p->nTime[i]); p->diffxq[i][0] = p->q[i][0] - p->dq[i] - p->lqu[i] - p->x[i][0]; timeaux = t + minRootPos(p->diffxq,i,2); // printf("timeaux: %.20f\n", timeaux); if(timeaux < p->nTime[i]) p->nTime[i] = timeaux; if(p->a[i] != 0 && fabs(p->x[i][2]) > 1e-10 && !p->flag3[i] && !p->flag2[i]){ modelica_real coeff[2]; coeff[0] = p->a[i] * p->a[i] * p->q[i][0] + p->a[i] * p->u0[i] + p->u1[i]; coeff[1] = p->a[i] * p->a[i] * p->q[i][1] + p->a[i] * p->u1[i]; // printf("coeff[0] %.20f\n", coeff[0]); // printf("coeff[1] %.20f\n", coeff[1]); timeaux = t + minRootPosCoeff(&coeff, i, 1); // printf("??timeaux: %.20f\n", timeaux); if(timeaux < p->nTime[i]){ p->flag2[i] = true; p->nTime[i] = timeaux; p->lquOld[i] = p->lqu[i]; } } else p->flag2[i] = false; if(p->nTime[i] > ft) p->nTime[i] = ft; modelica_real err1 = p->q[i][0] - p->x[i][0] + (p->diffxq[i][1] * (p->nTime[i] - t) / 2) + p->diffxq[i][2] * pow ((p->nTime[i] - t) / 2, 2); if (fabs (err1) > 3 * fabs (p->lqu[i])){ // printf("var: %d\n", i); // printf("err1 %.20f\n", err1); // printf("t %.20f\n\n", t); // printf("lqu[var] %.20f\n", p->lqu[i]); p->nTime[i] = t + ft * 1.0e-14; } } // if(t>0.00001 && t<0.00001001){ // //printf("Now we are in LIQSS2_recomputeNextTimes\n"); // //printf("We are considering the case where i=6\n"); // printf("p->nTime[i]: %.20f \n", p->nTime[i]); // printf("p->diffxq[i][1]: %.20f \n", p->diffxq[i][1]); // printf("p->diffxq[i][2]: %.20f \n", p->diffxq[i][2]); // printf("p->diffxq[i][0]: %.20f \n", p->q[i][0] - p->dq[i] + p->lqu[i] - p->x[i][0]); // printf("p->diffxq[i][0]: %.20f \n", p->q[i][0] - p->dq[i] - p->lqu[i] - p->x[i][0]); // } if(1){ //printf("Now we are in LIQSS2_recomputeNextTimes\n"); //printf("We are considering the case where i=6\n"); // printf("p->q[i][0]: %.20f \n", p->q[i][0]); // printf("p->dq[i]: %.20f \n", p->dq[i]); // printf("p->lqu[i]: %.20f \n", p->lqu[i]); // printf("p->x[i][0]: %.20f \n", p->x[i][0]); // // // printf("p->nTime[i]: %.20f \n", p->nTime[i]); // printf("p->diffxq[i][1]: %.20f \n", p->diffxq[i][1]); // printf("p->diffxq[i][2]: %.20f \n", p->diffxq[i][2]); // printf("p->diffxq[i][0]: %.20f \n", p->q[i][0] - p->dq[i] + p->lqu[i] - p->x[i][0]); // printf("p->diffxq[i][0]: %.20f \n\n", p->q[i][0] - p->dq[i] - p->lqu[i] - p->x[i][0]); } } }

static double UnquantizeSendRate(guint16 send_rate) { return (send_rate >> 4) * 10.0 / 4096.0 * pow(10.0, (send_rate & 0x000f)); }

double Point::Distance() const //calculating distance from origin as sqrt(x^2+y^2) { return(sqrt(pow(m_x,2)+pow(m_y,2))); }

double Point::Distance(const Point& Pt) const //calculating distance between two points { return (sqrt(pow(Pt.m_x-m_x,2)+pow(Pt.m_y-m_y,2))); }

double p_f(double n) { return pow(3.0 * pi * pi * D * n, 1.0 / 3.0); }

double t_E(std::vector<double> n, double f, set_const * C) { double res = 0.5 * pow(m_n * m_n * f / C->C_s, 2.0)*C->eta_s(f); res += C->U(f); int num = n.size(); /*std::vector<double> nvec; for (int i = 0; i < num; i++){ nvec.push_back(boost::python::extract<double>(n[i])); }*/ double o_sum = 0; double r_sum = 0; for (int i = 0; i < num; i++){ // cout << i << ", n[i] = " << n[i] << ", pf = " << p_f(n[i]) << endl; //res += KineticIntegral(p_f(n[i]), C->M[i] * C->phi_n(C->X_s[i] * f), C); res += KineticIntegral(p_f(n[i]), C->M[i] * C->phi_n(C->X_s[i] * (C->M[0]/C->M[i]) * f), C); o_sum += C->X_o[i] * n[i]; r_sum += C->X_r[i] * C->T[i] * n[i]; } res += 0.5 * pow(C->C_o * D * (o_sum) / m_n, 2.0) / (C->eta_o(f)); res += 0.5 * pow(C->C_r * D * (r_sum) / m_n, 2.0) / (C->eta_r(f)); //double me = m_e; //double pf_e = 0; //if (pow(mu_e(n[0] + n[1], n[1], f, C), 2.0) - me*me >= 0){ // pf_e = sqrt(pow(mu_e(n[0] + n[1], n[1], f, C), 2.0) - me*me); //} //if (n[1] != 0.0){ // res += KineticIntegral(pf_e, m_e, C); //} //double mmu = m_mu; //double pf_mu = 0; //if (pow(mu_e(n[0] + n[1], n[1], f, C), 2.0) - mmu*mmu >= 0){ // pf_mu = sqrt(pow(mu_e(n[0] + n[1], n[1], f, C), 2.0) - mmu*mmu); //} //if (n[1] != 0.0){ // res += KineticIntegral(pf_mu, m_mu, C); //} //gsl_function F; //F.function = &func_e; //double result = 0.0; //double error; //double me = m_e; //F.params = &me; //double pf_e = 0; //if (pow(mu_e_2(n[0] + n[1], n[1], f, C), 2.0) - me*me >= 0){ // pf_e = sqrt(pow(mu_e_2(n[0] + n[1], n[1], f, C), 2.0) - me*me); //} //// cout << "MU_E = " << mu_e(nn+np, np, f, C) << endl; //gsl_integration_qags(&F, 0.0, pf_e, 0, 1e-10, 1000, w_t_E, &result, &error); //if (n[1] != 0.0){ // //cout << "hey! " << np << endl; // res += result / (pi*pi); //} ////cout << "RESULT " << result << endl; //double mmu = m_mu; //F.params = &mmu; //double pf_mu = 0; //if (pow(mu_e_2(n[0] + n[1], n[1], f, C), 2.0) - mmu*mmu >= 0){ // pf_mu = sqrt(pow(mu_e_2(n[0] + n[1], n[1], f, C), 2.0) - mmu*mmu); //} //gsl_integration_qags(&F, 0.0, pf_mu, 0, 1e-10, 1000, w_t_E, &result, &error); //if (n[1] != 0.0){ // //cout << "hey! " << np << endl; // res += result / (pi*pi); //} return res; }

ImageObject * CreateDropShadowMask(const ViewportInfo *vpInfo, int offx, int offy, int r) { int rx2 = r * 2; int img_w = vpInfo->file->Width(); int img_h = vpInfo->file->Height(); ImageObject *mask = new ImageObject( offx + r + img_w, offy + r + img_h, 1); // Create the blur image float *blur = new float[rx2 * rx2]; //float root2 = sqrt(2.); float blurSum = 0.; for (int i=0; i<rx2; i++) { for (int j=0; j<rx2; j++) { float u = float(i)/float(rx2-1); float v = float(j)/float(rx2-1); float gu = exp(-pow((4*(u - .5)), 2)); float gv = exp(-pow((4*(v - .5)), 2)); float a = gu * gv; blur[i*rx2+j] = a; blurSum += a; } } for(int j = 0; j < rx2; ++j) for(int i = 0; i < rx2; ++i) blur[j*rx2 + i] /= blurSum; if(vpInfo->opaqueMode == M_OPAQUE) { int white = 255; mask->SetBlockToColor(offx, offy, offx + img_w, offy + img_h, &white); } else if(vpInfo->opaqueMode == M_TRANSPARENT) { int gray = int(vpInfo->opacity * 255.); mask->SetBlockToColor(offx, offy, offx + img_w, offy + img_h, &gray); } else { unsigned char rr, rg, rb; rr = vpInfo->transparentColor[0]; rg = vpInfo->transparentColor[1]; rb = vpInfo->transparentColor[2]; // Everywhere that does not match the bg color, color white. for(int y = 0; y < img_h; ++y) { for(int x = 0; x < img_w; ++x) { unsigned char *src = vpInfo->file->Pixel(x, y); unsigned char *dest = mask->Pixel(x+offx, y+offy); if(src[0] != rr || src[1] != rg || src[2] != rb) *dest = 255; } } } // Blur the mask by convolving it with the blur kernel. for(int y = offy; y < offy + img_h; ++y) { for(int x = offx; x < offx + img_w; ++x) { int ksrcx = x - r; int ksrcy = y - r; float channelSum = 0.; for(int ky = 0; ky < rx2; ++ky) { for(int kx = 0; kx < rx2; ++kx) { unsigned char *mask_ptr = mask->Pixel(ksrcx + kx, ksrcy + ky); channelSum += float(*mask_ptr) * blur[ky*rx2+kx]; } } unsigned char *dest = mask->Pixel(x, y); *dest = (unsigned char)(int)(channelSum); } } if(vpInfo->opaqueMode == M_TRANSPARENT) { int black = 0; mask->SetBlockToColor(0, 0, img_w, img_h, &black); } delete [] blur; return mask; }

//calculation function once FFT data ready void KeyTrack_calculatekey(KeyTrack *unit, uint32 ibufnum) { World *world = unit->mWorld; SndBuf *buf; if (ibufnum >= world->mNumSndBufs) { int localBufNum = ibufnum - world->mNumSndBufs; Graph *parent = unit->mParent; if(localBufNum <= parent->localBufNum) { buf = parent->mLocalSndBufs + localBufNum; } else { buf = world->mSndBufs; if(unit->mWorld->mVerbosity > -1){ Print("KeyTrack error: Buffer number overrun: %i\n", ibufnum); } } } else { buf = world->mSndBufs + ibufnum; } LOCK_SNDBUF(buf); int numbins = buf->samples - 2 >> 1; //assumed in this representation SCComplexBuf *p = ToComplexApx(buf); const float * data= buf->data; //memcpy(unit->m_FFTBuf, data, NOVER2); //to hold powers float * fftbuf= unit->m_FFTBuf; //get powers for bins //don't need to calculate past half Nyquist, because no indices involved of harmonics above 10000 Hz or so (see index data at top of file) for (int i=0; i<NOVER2; i+=2) { //i>>1 is i/2 fftbuf[i>>1] = ((data[i] * data[i]) + (data[i+1] * data[i+1])); } float * chroma= unit->m_chroma; float sum; int indexbase, index; //experimental; added leaky integration on each note; also, only add to sum if harmonic, ie not a transient float * weights = unit->m_weights; int * bins = unit->m_bins; float chromaleak= ZIN0(2); //zero for new round (should add leaky integrator here! for (int i=0;i<12;++i) chroma[i] *= chromaleak; for (int i=0;i<60;++i) { int chromaindex = (i+9)%12; //starts at A1 up to G#6 sum=0.0; indexbase= 12*i; //6 partials, 2 of each //transient sum, setting up last values too float phasesum=0.0; for(int j=0;j<12;++j) { //12 if 144 data points index=indexbase+j; //experimental transient detection code, not reliable //int binindex= unit->m_bins[index]-1; //SCPolar binnow= p->bin[binindex].ToPolarApx(); //float phaseadvance= (binindex+1)*(TWOPI*0.5); //k * (512/44100) * (44100/1024) //convert bin number to frequency //float power= binnow.mag * binnow.mag; //(p->bin[binindex].real)*(p->bin[binindex].real) + (p->bin[binindex].imag)*(p->bin[binindex].imag); //(p->bin[binindex].mag); //power *= power; //int phaseindex= indexbase+j; //float phasenow= binnow.phase; //0.0; //(p->bin[binindex].phase); //float prevphase = fmod(unit->m_prevphase[index]+phaseadvance,TWOPI); //float a,b,tmp; //a=phasenow; b=prevphase; //b=phasenow; a=prevphase; //if(b<a) {b= b+TWOPI;} //float phasechange = sc_min(b-a,a+TWOPI-b); //more complicated, need mod 2pi and to know lower and upper //phasesum+= phasechange; //unit->m_prevphase[index]= phasenow; //((p->bin[index-1].mag) * (p->bin[index-1].mag)) //printf("comparison %f %f \n",fftbuf[g_bins2[index]], power); //sum+= (unit->m_weights[index])* power; sum+= (weights[index])* (fftbuf[bins[index]]); } //transient test here too? //if(phasesum>(5*PI)){sum=0.0;} //if((i>5) && (i<15)) //printf("test phasesum %f \n", phasesum); //unit->m_leaknote[i] = (0.8*unit->m_leaknote[i]) + sum; chroma[chromaindex]+= sum; //unit->m_leaknote[i]; //sum; } float* key = unit->m_key; //major for (int i=0;i<12;++i) { sum=0.0; for (int j=0;j<7;++j) { indexbase=g_major[j]; index=(i+indexbase)%12; //sum+=(chroma[index]*g_kkmajor[indexbase]); sum+=(chroma[index]*g_diatonicmajor[indexbase]); } key[i]=sum; //10*log10(sum+1); } //minor for (int i=0;i<12;++i) { sum=0.0; for (int j=0;j<7;++j) { indexbase=g_minor[j]; index=(i+indexbase)%12; //sum+=(chroma[index]*g_kkminor[indexbase]); sum+=(chroma[index]*g_diatonicminor[indexbase]); } key[12+i]=sum; } float keyleak= ZIN0(1); //fade parameter to 0.01 for histogram in seconds, convert to FFT frames //keyleak in seconds, convert to drop time in FFT hop frames (FRAMEPERIOD) keyleak= sc_max(0.001f,keyleak/unit->m_frameperiod); //FRAMEPERIOD; //now number of frames, actual leak param is decay exponent to reach 0.01 in x seconds, ie 0.01 = leakparam ** (x/ffthopsize) //0.01 is -40dB keyleak= pow(0.01f,(1.f/keyleak)); float * histogram= unit->m_histogram; int bestkey=0; float bestscore=0.0; for (int i=0;i<24;++i) { histogram[i]= (keyleak*histogram[i])+key[i]; if(histogram[i]>bestscore) { bestscore=histogram[i]; bestkey=i; } //printf("%f ",histogram[i]); } //should find secondbest and only swap if win by a margin //printf(" best %d \n\n",bestkey); //what is winning currently? find max in histogram unit->m_currentKey=bestkey; //about 5 times per second //if((unit->m_triggerid) && ((unit->m_frame%2==0))) SendTrigger(&unit->mParent->mNode, unit->m_triggerid, bestkey); }

////////////////////////////////////////////////////////////////////////////// // Function for stepping advection equation forward in time. // // The advection equation: q_t + u q_x = 0. // // It is assumed that aux(1,1,1) = u is constant. // ////////////////////////////////////////////////////////////////////////////// void StepAdvecFluxForm(const double& dt, const dTensor2& node, dTensorBC3& auxvals, dTensorBC1& smax, const dTensorBC3& qold, dTensorBC3& qnew) { void SetBndValues(const dTensor2& node, dTensorBC3& aux, dTensorBC3& q); SetBndValues(node, auxvals, qnew); //-local parameters -----------------------------------------------------// const int melems = qnew.getsize(1); //number of elements in grid const int meqn = qnew.getsize(2); //number of equations const int kmax = qnew.getsize(3); //number of polynomials const int mbc = qnew.getmbc(); //number of ghost cells //-----------------------------------------------------------------------// //save the center of each grid cell const double dx = node.get(2,1)-node.get(1,1); const double speed = auxvals.get(1,1,1); for(int j=1-mbc; j<=melems+mbc; j++) { smax.set(j, Max(smax.get(j), fabs(speed) ) ); } // compute quadrature points for where Q needs to be sampled const double s_area = 2.0; const double large_eta = speed*dt/dx; // integration points and weights (for interior) const int mpts = 5; dTensor1 wgt( mpts ), spts( mpts ); dTensor2 phi( mpts, 5), phi_x(mpts, 5); if( kmax > 1 ) { void setGaussPoints1d(dTensor1& x1d, dTensor1& w1d); void evaluateLegendrePolys( const double dx, const dTensor1& spts, dTensor2& phi, dTensor2& phi_x); setGaussPoints1d( spts, wgt ); evaluateLegendrePolys(dx, spts, phi, phi_x); } #pragma omp parallel for for(int i=1; i<=melems; i++) { //loop over each equation for(int me=1; me<= meqn; me++) { dg_cell* dgc = new dg_cell(); for( int k=1; k <= kmax; k++) { double qnp1 = qold.get(i,me,k); if( k > 1 ) { // interior integral for( int m=1; m <= mpts; m++ ) { double a = spts.get(m) - 2.0 * large_eta; double b = spts.get(m); int ishift = get_shift_index( a ); double integral = 0.0; if( ishift == 0 ) { // no shift takes place, can simply integrate from a to b for( int kq = 1; kq <= kmax; kq++ ) { integral += qold.get(i, me, kq) * dgc->integratePhi( a, b, kq ); } } else if( ishift < 0 ) { // integral at the shifted value for( int kq = 1; kq <= kmax; kq++ ) { integral += qold.get(i+ishift, me, kq) * dgc->integratePhi( shift_xi(a), 1.0, kq ); } // integrals between i and ishift for( int n=-1; n > ishift; n-- ) { integral += qold.get(i + n, meqn, 1) * dgc->integratePhi(-1.0, 1.0, 1); } // integral inside current cell for( int kq = 1; kq <= kmax; kq++ ) { integral += qold.get(i, me, kq) * dgc->integratePhi( -1.0, spts.get(m), kq ); } } else { printf(" StepAdvecFluxForm.cpp problem can't handle negative velocities yet\n"); exit(1); } qnp1 += 0.25 * dx * wgt.get(m) * phi_x.get(m,k) * integral; } } // left flux double a = -1.0 - 2.0 * large_eta; int ishift = get_shift_index( a ); double Fm = 0.0; if( ishift < 0 ) { for( int kq = 1; kq <= kmax; kq++ ) { Fm += qold.get(i+ishift, me, kq) * dgc->integratePhi( shift_xi(a), 1.0, kq ) / 2.0; } for( int n=-1; n > ishift; n-- ) { Fm += qold.get(i+n, me, 1); } } Fm *= sqrt(2*k-1) * pow(-1, k+1 ); // right flux double Fp = 0.0; a += 2.0; ishift = get_shift_index( a ); if( ishift < 1 ) { for( int kq = 1; kq <= kmax; kq++ ) { Fp += qold.get(i+ishift, me, kq) * dgc->integratePhi( shift_xi(a), 1.0, kq ) / 2.0; } } for( int n=0; n > ishift; n-- ) { Fp += qold.get(i+n, me, 1); } Fp *= sqrt(2*k-1); qnew.set(i, me, k, qnp1 - ( Fp - Fm ) ); } delete dgc; }//end of loop over each cell }//end of loop over each eqn void SetBndValues(const dTensor2& node, dTensorBC3& aux, dTensorBC3& q); SetBndValues(node, auxvals, qnew); }

body_part Creature::select_body_part(Creature *source, int hit_roll) { // Get size difference (-1,0,1); int szdif = source->get_size() - get_size(); if(szdif < -1) { szdif = -1; } else if (szdif > 1) { szdif = 1; } if(g->debugmon) { g->add_msg("source size = %d", source->get_size()); g->add_msg("target size = %d", get_size()); g->add_msg("difference = %d", szdif); } std::map<body_part, double> hit_weights = default_hit_weights[szdif]; std::map<body_part, double>::iterator iter; // If the target is on the ground, even small/tiny creatures may target eyes/head. Also increases chances of larger creatures. // Any hit modifiers to locations should go here. (Tags, attack style, etc) if(is_on_ground()) { hit_weights[bp_eyes] += 10; hit_weights[bp_head] += 20; } //Adjust based on hit roll: Eyes, Head & Torso get higher, while Arms and Legs get lower. //This should eventually be replaced with targeted attacks and this being miss chances. hit_weights[bp_eyes] = floor(hit_weights[bp_eyes] * pow(hit_roll, 1.15) * 10); hit_weights[bp_head] = floor(hit_weights[bp_head] * pow(hit_roll, 1.15) * 10); hit_weights[bp_torso] = floor(hit_weights[bp_torso] * pow(hit_roll, 1) * 10); hit_weights[bp_arms] = floor(hit_weights[bp_arms] * pow(hit_roll, 0.95) * 10); hit_weights[bp_legs] = floor(hit_weights[bp_legs] * pow(hit_roll, 0.975) * 10); // Debug for seeing weights. if(g->debugmon) { g->add_msg("eyes = %f", hit_weights.at(bp_eyes)); g->add_msg("head = %f", hit_weights.at(bp_head)); g->add_msg("torso = %f", hit_weights.at(bp_torso)); g->add_msg("arms = %f", hit_weights.at(bp_arms)); g->add_msg("legs = %f", hit_weights.at(bp_legs)); } double totalWeight = 0; std::set<weight_pair, weight_compare> adjusted_weights; for(iter = hit_weights.begin(); iter != hit_weights.end(); ++iter) { totalWeight += iter->second; adjusted_weights.insert(*iter); } double roll = rng_float(1, totalWeight); body_part selected_part = bp_torso; std::set<weight_pair, weight_compare>::iterator adj_iter; for(adj_iter = adjusted_weights.begin(); adj_iter != adjusted_weights.end(); ++adj_iter) { roll -= adj_iter->second; if(roll <= 0) { selected_part = adj_iter->first; break; } } return selected_part; }

//--------------------------------------------------------- bool CWatersheds_ext::Get_Basin(CSG_Grid *pBasins, CSG_Shapes *pPolygons, int xMouth, int yMouth, int Main_ID) { int x, y, Basin_ID = 1 + pPolygons->Get_Count(); CSG_Shape *pPolygon; CSG_Grid_Stack Stack; CSG_Simple_Statistics s_Height, s_Distance; //----------------------------------------------------- Stack.Push(x = xMouth, y = yMouth); pBasins ->Set_Value(x, y, Basin_ID); m_Distance .Set_Value(x, y, 0.0); s_Height += m_pDEM->asDouble(x, y); s_Distance += 0.0; //----------------------------------------------------- while( Stack.Get_Size() > 0 && Process_Get_Okay() ) { Stack.Pop(x, y); double d = m_Distance.asDouble(x, y); //------------------------------------------------- for(int i=0; i<8; i++) { int ix = Get_xFrom(i, x); int iy = Get_yFrom(i, y); if( is_InGrid(ix, iy) && pBasins->is_NoData(ix, iy) && i == m_Direction.asInt(ix, iy) ) { Stack.Push(ix, iy); pBasins ->Set_Value(ix, iy, Basin_ID); m_Distance .Set_Value(ix, iy, d + Get_Length(i)); s_Height += m_pDEM->asDouble(ix, iy); s_Distance += d + Get_Length(i); } } } //----------------------------------------------------- if( s_Height.Get_Count() > 1 && (pPolygon = Get_Basin(pBasins, pPolygons)) != NULL ) { double d, Area, Perimeter, Side_A, Side_B; CSG_String Gravelius; // Area = s_Height.Get_Count() * Get_System()->Get_Cellarea(); Area = ((CSG_Shape_Polygon*)pPolygon)->Get_Area(); Perimeter = ((CSG_Shape_Polygon*)pPolygon)->Get_Perimeter(); d = 0.28 * Perimeter / sqrt(Area); Gravelius = d > 1.75 ? _TL("rectangular") : d > 1.5 ? _TL("ovalooblonga-rectangularoblonga") : d > 1.25 ? _TL("ovaloredonda-ovalooblonga") : _TL("redonda-ovaloredonda"); d = pow(Perimeter, 2.0) - 8.0 * Area; Side_A = d > 0.0 ? (Perimeter + sqrt(d)) / 4.0 : -1.0; Side_B = d > 0.0 ? (Perimeter - 2.0 * Side_A) / 2.0 : -1.0; pPolygon->Set_Value(FIELD_ID , Basin_ID); pPolygon->Set_Value(FIELD_ID_MAIN , Main_ID); pPolygon->Set_Value(FIELD_MOUTH_X , Get_System()->Get_xGrid_to_World(xMouth)); pPolygon->Set_Value(FIELD_MOUTH_Y , Get_System()->Get_yGrid_to_World(yMouth)); pPolygon->Set_Value(FIELD_PERIMETER , Perimeter); pPolygon->Set_Value(FIELD_AREA , Area); pPolygon->Set_Value(FIELD_CENTROID_X , ((CSG_Shape_Polygon*)pPolygon)->Get_Centroid().x); pPolygon->Set_Value(FIELD_CENTROID_Y , ((CSG_Shape_Polygon*)pPolygon)->Get_Centroid().y); pPolygon->Set_Value(FIELD_Z_MEAN , s_Height .Get_Mean()); pPolygon->Set_Value(FIELD_Z_RANGE , s_Height .Get_Range()); pPolygon->Set_Value(FIELD_DIST_MEAN , s_Distance.Get_Mean()); pPolygon->Set_Value(FIELD_DIST_MAX , s_Distance.Get_Maximum()); pPolygon->Set_Value(FIELD_CONCTIME , s_Height.Get_Range() <= 0.0 ? -1.0 : pow(0.87 * pow(s_Distance.Get_Maximum() / 1000.0, 3.0) / s_Height.Get_Range(), 0.385) ); pPolygon->Set_Value(FIELD_BASIN_TYPE , Gravelius); pPolygon->Set_Value(FIELD_EQVRECT_A , Side_A); pPolygon->Set_Value(FIELD_EQVRECT_B , Side_B); pPolygon->Set_Value(FIELD_OROG_IDX , SG_Get_Square(s_Height.Get_Mean()) / (0.0001 * Area)); // Orographic index, defined as the mean catchment altitude times the ratio of the mean catchment altitude to the orthogonal projection of drainage area (Alcázar, Palau (2010): Establishing environmental flow regimes in a Mediterranean watershed based on a regional classification. Journal of Hydrology, V. 388 pPolygon->Set_Value(FIELD_MASS_IDX , Perimeter / (0.0001 * Area)); // Perimeter / (0.0001 * Area) ??!! pPolygon->Set_Value(FIELD_BASINS_UP , 0.0); // Upslope Basins pPolygon->Set_Value(FIELD_BASINS_DOWN , 0.0); // Downslope Basins return( true ); } return( false ); }

// On copy apply for type channel! bool huMoments::apply(const channel& src,dvector& dest, dvector& more) const { channel::value_type val; dest.resize(NUM_FEAT,0,false,true); more.resize(MORE_FEAT,0,false,true); double m00=0.0; double cm11,cm20,cm02,cm30,cm03,cm12,cm21; cm11=cm20=cm02=cm30=cm03=cm12=cm21=0.0; double xcog, ycog; xcog=ycog=0.0; int r, rows=src.rows(); int c, cols=src.columns(); // compute simple moments and cog for (r=0; r<rows; r++) { for (c=0; c<cols; c++) { val = src.at(r,c); m00+=val; xcog+=c*val; ycog+=r*val; } } // end here, if no content if (m00==0) { return false; } // compute cog's more[huMoments::xcog]=xcog=xcog/m00; //xcog more[huMoments::ycog]=ycog=ycog/m00; //ycog // compute central moments for (r=0; r<rows; r++) { for (c=0; c<cols; c++) { val = src.at(r,c); double x_xcog = c-xcog; double y_ycog = r-ycog; cm11+=(x_xcog)*(y_ycog)*val; cm20+=(x_xcog)*(x_xcog)*val; cm02+=(y_ycog)*(y_ycog)*val; cm30+=(x_xcog)*(x_xcog)*(x_xcog)*val; cm03+=(y_ycog)*(y_ycog)*(y_ycog)*val; cm12+=(x_xcog)*(y_ycog)*(y_ycog)*val; cm21+=(x_xcog)*(x_xcog)*(y_ycog)*val; } } double m00pow2,m00pow2_5; m00pow2 = m00*m00; m00pow2_5 = pow(m00,2.5); // normalized central moments cm02 = cm02/m00pow2; cm03 = cm03/m00pow2_5; cm11 = cm11/m00pow2; cm12 = cm12/m00pow2_5; cm20 = cm20/m00pow2; cm21 = cm21/m00pow2_5; cm30 = cm30/m00pow2_5; // calculate moment invariants dest[huMoments::hu1] = cm20 + cm02; dest[huMoments::hu2] = pow((cm20 - cm02),2) + 4*pow(cm11,2); dest[huMoments::hu3] = pow((cm30 - 3*cm12),2) + pow((3*cm21 - cm03),2); dest[huMoments::hu4] = pow((cm30 + cm12),2) + pow((cm21 + cm03),2); dest[huMoments::hu5] = (cm30-3*cm12)*(cm30+cm12)*( pow((cm30+cm12),2) - 3*pow((cm21+cm03),2) ) + (3*cm21-cm03)*(cm21+cm03)*( 3*pow((cm30+cm12),2) - pow((cm21+cm03),2) ); dest[huMoments::hu6] = (cm20-cm02)*( pow((cm30+cm12),2) - pow((cm21+cm03),2) ) + 4*cm11*(cm30+cm12)*(cm21+cm03); dest[huMoments::hu7] = (3*cm21-cm03)*(cm30+cm12)*( pow((cm30+cm12),2) - 3*pow((cm21+cm03),2) ) - (cm30-3*cm12)*(cm21+cm03)*( 3*pow((cm30+cm12),2) - pow((cm21+cm03),2) ); double temp = sqrt( (cm20 - cm02)*(cm20 - cm02) + 4*cm11*cm11 ); more[huMoments::eigen1]=m00*0.5*((cm20+cm02) + temp); //eigen 1 more[huMoments::eigen2]=m00*0.5*((cm20+cm02) - temp); //eigen 2 more[huMoments::orientation]=0.5*atan2(2*cm11, cm20 - cm02); //orientation more[huMoments::m00]=m00; //m00 const parameters& param = getParameters(); if (param.scaling) { int i; for (i=0; i<dest.size();i++){ dest[i]=-logn(dest[i]); } } return true; }

inline void stm_tune_scheduler(){ TX_GET; int m=max_allowed_running_transactions; stm_time_t now=STM_TIMER_READ(); stm_time_t total_tx_wasted_time=0; stm_time_t total_tx_time=0; stm_time_t total_no_tx_time=0; stm_time_t total_tx_spin_time=0; memset(conflicts_per_active_transactions, 0, (max_concurrent_threads+1) * sizeof(long)); memset(committed_per_active_transactions, 0, (max_concurrent_threads+1) * sizeof(long)); memset(wasted_time_k, 0, (max_concurrent_threads+1) * sizeof(stm_time_t)); memset(useful_time_k, 0, (max_concurrent_threads+1) * sizeof(stm_time_t)); long total_committed_transactions_by_collector_threads=0; long total_committed_transactions=0; long total_aborted_transactions=0; float average_running_transactions=0; tx->total_no_tx_time+=now - tx->start_no_tx_time ; stm_tx_t *thread=_tinystm.threads; int i=0; while(thread!=NULL){ total_tx_time+=thread->total_useful_time; total_no_tx_time+=thread->total_no_tx_time; total_tx_wasted_time+=thread->total_wasted_time; total_tx_spin_time+=thread->total_spin_time; total_committed_transactions_by_collector_threads+=thread->committed_transactions_as_a_collector_thread; total_committed_transactions+=thread->committed_transactions; total_aborted_transactions+=thread->aborted_transactions; for(i=0;i<max_concurrent_threads+1;i++){ wasted_time_k[i]+=thread->total_tx_wasted_per_active_transactions[i]; //printf("\nwasted_time_k[%i] %llu", i, thread->total_tx_wasted_per_active_transactions[i]); useful_time_k[i]+=thread->total_tx_useful_per_active_transactions[i]; committed_per_active_transactions[i]+=thread->total_tx_committed_per_active_transactions[i]; average_running_transactions+=(float)i * (float) thread->total_tx_committed_per_active_transactions[i]; conflicts_per_active_transactions[i]+=thread->total_conflict_per_active_transactions[i]; } reset_local_stats(thread); thread=thread->next; } //for(i=0;i<max_concurrent_threads+1;i++) printf("\nwasted_time_k[%i] %llu", i, wasted_time_k[i]); //printf("\ntotal_tx_time %llu, total_tx_wasted_time %llu, total_no_tx_time %llu, total_committed_transactions_by_collector_threads %i", total_tx_time, total_tx_wasted_time, total_no_tx_time, total_committed_transactions_by_collector_threads); average_running_transactions=average_running_transactions/(float)total_committed_transactions_by_collector_threads; float *mu_k=(float*)malloc((max_concurrent_threads+1) * sizeof(float)); float lambda = 1.0 / (((float) total_no_tx_time/(float)1000000000)/(float) total_committed_transactions_by_collector_threads); for (i=0;i<max_concurrent_threads+1;i++){ if((wasted_time_k[i]>0 || useful_time_k[i]>0) && committed_per_active_transactions[i] > 0){ mu_k[i]= 1.0 / ((((float) wasted_time_k[i] / (float)1000000000) / (float)committed_per_active_transactions[i]) + (((float) useful_time_k[i]/(float)1000000000) / (float) committed_per_active_transactions[i])); //printf("\nk:%i\tmu_k: %f, %llu, %llu, %llu", i, mu_k[i], wasted_time_k[i], useful_time_k[i], committed_per_active_transactions[i]); }else{ mu_k[i]= 1.0 / ((((float)total_tx_wasted_time/(float)1000000000)/(float)total_committed_transactions_by_collector_threads)+(((float)total_tx_time/(float)1000000000) / (float) total_committed_transactions_by_collector_threads)); //printf("\nk:%i\tmu_k: %f - average", i, mu_k[i]); } } float th = get_throughput(lambda,mu_k,m); float th_minus_1=0.0,th_plus_1=0.0,th_minus_2=0.0; if(m>3){ th_minus_1=get_throughput(lambda,mu_k,m-1); th_minus_2=get_throughput(lambda,mu_k,m-2); }else if(m>2)th_minus_1=get_throughput(lambda,mu_k,m-1); if(th_minus_2 >= th && th_minus_2 >= th_minus_1 && m>3) { max_allowed_running_transactions-=2; //printf("\nSelected th_minus_2"); }else if(th_minus_1>=th){ max_allowed_running_transactions--; //printf("\nSelected th_minus_1"); }else if(m<max_concurrent_threads){ float average_restarted_transactions= (float)conflicts_per_active_transactions[m]/(float)committed_per_active_transactions[m]; float p_a_k = average_restarted_transactions /(1.0 + average_restarted_transactions); float p_a_1 = 1- pow(1-p_a_k,1.0/(double)(m-1)); float average_restarted_transactions_plus_1 = ((1.0 - pow((1.0 - p_a_1),m))/ pow((1-p_a_1),m)); float w_m=0.0,u_m=0.0; if(conflicts_per_active_transactions[m]>0) w_m=((float)wasted_time_k[m]/(float)1000000000)/(float)conflicts_per_active_transactions[m]; else if(total_aborted_transactions>0)w_m=((float)total_tx_wasted_time/(float)1000000000)/(float)total_aborted_transactions; if(committed_per_active_transactions[m]>0) u_m = ((float)useful_time_k[m]/(float)1000000000)/(float)committed_per_active_transactions[m]; else u_m = ((float)total_tx_time/(float)1000000000)/(float)total_committed_transactions_by_collector_threads; mu_k[m + 1]= 1.0/((w_m * average_restarted_transactions_plus_1) + u_m ); th_plus_1 = get_throughput(lambda,mu_k,m + 1); if(th_plus_1 > 1.1* th) { max_allowed_running_transactions++; //printf("\nSelected th_plus_1"); } else { //printf("\nSelected th"); } }// tx->start_no_tx_time=STM_TIMER_READ(); //printf("\nPredicted: %f|%f|%f|%f, measured: %f, max txs: %i", th_minus_2, th_minus_1, th, th_plus_1, (float)total_committed_transactions/((float)(now-last_tuning_time)/(float)1000000000), max_allowed_running_transactions); //printf("\tTotal commits: %i (as a collector: %i)",total_committed_transactions, total_committed_transactions_by_collector_threads); //printf("\nlambda: %f mu: %f", lambda, 1.0 / ((((float)total_tx_wasted_time/(float)1000000000)/(float)total_committed_transactions_by_collector_threads)+(((float)total_tx_time/(float)1000000000) / (float) total_committed_transactions_by_collector_threads))); //printf("\naverage_running_transactions: %f", average_running_transactions, 1.0); //fflush(stdout); last_tuning_time=STM_TIMER_READ(); }

int main(){ int a,b; char c; int i; int j; double dif; int col = 0; double sum_sq_error = 0 ; int epoch=0; double pre_avg_sq_error = 0; double accuracy = 0; double max = 0; int num_correct= 0, tmp, k; //�ʱ�ȭ for(i = 0; i<NLayer; i++){ for(j = 0; j<MLayerSize; j++){ s[i][j] = 0; f[i][j] = 0; delta[i][j] = 0; } } initialization(); // ����ġ ������ �ʱ�ȭ FILE *fp, *fp1; fp = fopen("traindata.txt", "r");fp1 = fopen("testdata.txt", "r");//�Է½�ȣ �������� while(!feof(fp)){ fscanf(fp, "%d", &a); for(i = 0; i<m2; i++){ if(a == i) d[col][i] = 1; else d[col][i] = 0; } fscanf(fp, " %c ", &c); c = '0'; // $������ for(i = 0; i<N; i++){ fscanf(fp, "%d", &b); TrainData[col][i] = b; } col ++; } col = 0;a = 0;b = 0; while(!feof(fp1)) { fscanf(fp1, "%d", &a); for(i=0; i<m2; i++) { if(a==i) Td[col][i]=1; else Td[col][i]=0; } fscanf(fp1, " %c ", &c); c = '0'; // $������ for(i=0; i<N; i++) { fscanf(fp1, " %d", &b); TestData[col][i]=b; } col++; } //file input do{ pre_avg_sq_error = avg_sq_error; // printf("pre = %lf\n", pre_avg_sq_error); avg_sq_error=0, sum_sq_error=0; for(int t=0; t<=N_tr_examples-1; t++){ // �� ������ �Ʒð��� forward_compute(t); backward_compute(t); weight_update(t); } epoch++; for(int t = 0; t<=N_tr_examples-1; t++){ // �� ���� �� ��� ���� ��� // t���� �Ʒÿ����� �̿��Ͽ� �ý����� ����� ���� error�� ���� forward_compute(t); // t ��° �Ʒÿ��� �����Ͽ� ����� ���� for(i = 0; i<=M[NLayer-1]-1 ; i++){ // �������� ����� �̿��Ͽ� ������ ����. sum_sq_error+=pow((d[t][i]-f[NLayer-1][i]),2); } } avg_sq_error = sum_sq_error/(N_tr_examples*M[NLayer-1]); //��� �Ʒ� ���� ���Ͽ� ��� square ������ ����� printf("%d epoch error rate = %lf\n",epoch,avg_sq_error); /* Fail �κ�, �Ӱ�ġ ���Ϸ� �������� �ʹ� ���� ����� ������ ������ �Ͼ���ϴ�. dif = pre_avg_sq_error - avg_sq_error; if(dif < 0) dif = -dif; printf("dif = %lf\n", dif); */ }while(threshold < avg_sq_error) ; //&& (threshold < dif)); //avg_sq_error �� �Ӱ�ġ �����̸� ����, //�Ǵ� ���� epoch ������ avg_sq_error �� �̹� epoch �� avg_sq_error �� ���̰� �Ӱ�ġ �����̸� ���� for(int t = 0; t<=N_te_examples-1; t++){ // TestData[t], d_te[t] �� input,d�� �ִ´�. t_forward_compute(t); max=0.; for(j=0; j<NLayer; j++) { if(max < f[NLayer-1][j]) { max = f[NLayer-1][j]; tmp = j; } } for(k=0; k<NLayer; k++) { if(Td[t][k] == 1 && tmp == k) num_correct++; } } accuracy = (double)num_correct/(double)N_te_examples; printf("Accuracy = %lf", accuracy); fclose(fp); fclose(fp1); }

RGB Scene::trace(Ray ray, double ray_intensity, int trace_depth) { RGB result(0.0, 0.0, 0.0); if (trace_depth > MAX_DEPTH) return (result); if (ray_intensity < MIN_INTENSITY) return (result); Intersection min_hit; min_hit.dist = MAX_DIST; bool intersected = false; for(int i = 0; i < active_prims; i++) { Intersection current_hit; if(prim_refs[i]->get_intersect(ray, current_hit) && current_hit.dist < min_hit.dist) { min_hit = current_hit; intersected = true; } } //std::cout << "intersection pos:" << intersect.pos.x[0] << " " << intersect.pos.x[1] << " " << intersect.pos.x[2] << "\n"; //if(!intersected) std::cout << "wtf negz no hit\n"; // returns pure black if (!intersected) return (result); // slight adjustment to prevent floating-point inaccuracy from // causing unwanted collisions min_hit.pos += min_hit.normal * 0.0001; //if (intersected) std::cout << "adjusted min_hition pos:" << min_hit.pos.x[0] << " " << min_hit.pos.x[1] << " " << min_hit.pos.x[2] << "\n"; for (int i = 0; i < active_lights; i++) { Vec3 lightray; // deposits lightray in lightray variable bool shadowed = check_shadowing(light_refs[i], min_hit.pos, lightray); if (!shadowed) { // currently ignores color of light result += min_hit.shape_ref->get_diff_mat() * std::max( dot(min_hit.normal, lightray), 0.0 ); // reflection Vec3 R = lightray - min_hit.normal * 2 * dot( lightray, min_hit.normal ); double specdot = dot( R, min_hit.dir ); if (specdot > 0) result += min_hit.shape_ref->get_spec_mat() * pow( specdot, 20 ); } //if(shadowed) std::cout <<" wtf negz shadowed at:" << min_hit.pos.x[0] << " " << min_hit.pos.x[1] << " " << min_hit.pos.x[2] << "\n"; //else std::cout << "plane normal: " << min_hit.dir.x[0] << " " << min_hit.dir.x[1] << " " << min_hit.dir.x[2] << "\n"; //else std::cout << "dot result: " << dot(min_hit.dir, lposdir) << "\n"; } // reflection double obj_refl = min_hit.shape_ref->get_reflection(); if (obj_refl > 0.0) { Vec3 refl_dir = ray.dir - min_hit.normal * 2 * dot( ray.dir, min_hit.normal ); Ray refl_ray ( min_hit.pos, refl_dir ); result += trace( refl_ray, obj_refl * ray_intensity, trace_depth + 1 ); } // refraction double obj_transp = min_hit.shape_ref->get_transparency(); if (obj_transp > 0.0) { // check that when transp not 0, refr not 0. double obj_refr = min_hit.shape_ref->get_refraction(); // check htis double cos_term = dot( ray.dir, min_hit.normal ) ; double nr = (cos_term < 0.0) ? (1 / obj_refr) : (obj_refr); double sin0t2 = nr*nr * (1 - pow(cos_term, 2)); if (sin0t2 < 1.0) { Vec3 refracted_dir = ray.dir * nr - min_hit.normal * (nr * abs(cos_term) - sqrt(1 - sin0t2)); assert(refracted_dir.mag() > 0.0); Vec3 irpos = (cos_term < 0.0) ? (min_hit.pos - min_hit.normal * 0.0002) : (min_hit.pos); Ray refr_ray ( irpos, refracted_dir); result += trace( refr_ray, obj_transp * ray_intensity, trace_depth + 1 ); } } for (int i = 0; i < 3; i++) result.x[i] = std::min(result.x[i], 1.0); assert(ray_intensity <= 1.0); return (result * ray_intensity); }

int populate_lattice(particle_data* atom_data) { /* * This routine will populate the lattice using the * values read from the pdb and itp files. * WARNING: It contains much logic and interpolation stuff! */ #ifdef DEBUG printf("pdb_n_particles=%u, itp_n_particles=%u, itp_n_parameters=%u\n",atom_data->pdb_n_particles,atom_data->itp_n_particles,atom_data->itp_n_parameters); #endif // TODO: Check if bounding box fits into simbox bounding_box bbox; calculate_bounding_box(&bbox, atom_data); // calculate the shift of the bounding box float shift[3]; shift[0] = ek_parameters.agrid / 2.0 * ek_parameters.dim_x - bbox.center[0]; shift[1] = ek_parameters.agrid / 2.0 * ek_parameters.dim_y - bbox.center[1]; shift[2] = ek_parameters.agrid / 2.0 * ek_parameters.dim_z - bbox.center[2]; #ifdef DEBUG printf("bbox.max_x=%f, bbox.max_y=%f, bbox.max_z=%f, bbox.min_x=%f, bbox.min_y=%f, bbox.min_z=%f, bbox->center=[%f; %f; %f]\n", bbox.max_x, bbox.max_y, bbox.max_z, bbox.min_x, bbox.min_y, bbox.min_z, bbox.center[0], bbox.center[1], bbox.center[2]); printf("agrid=%f, dim_x=%d, dim_y=%d, dim_z=%d\n",ek_parameters.agrid, ek_parameters.dim_x, ek_parameters.dim_y, ek_parameters.dim_z); printf("shift=[%f; %f; %f]\n",shift[0], shift[1], shift[2]); #endif // joining the array int lowernode[3]; float cellpos[3]; float gridpos; float a_x_shifted, a_y_shifted, a_z_shifted; for (unsigned int i = 0; i <= atom_data->pdb_n_particles-1; i++) { pdb_ATOM* a = &atom_data->pdb_array_ATOM[i]; itp_atoms* b; itp_atomtypes* c; for (unsigned int j = 0; j <= atom_data->itp_n_particles-1; j++) { b = &atom_data->itp_array_atoms[j]; if (a->i == b->i) { for (unsigned int k = 0; k <= atom_data->itp_n_parameters-1; k++) { c = &atom_data->itp_array_atomtypes[k]; if (strcmp(b->type,c->type) == 0) { #ifdef DEBUG printf("i=%d x=%f y=%f z=%f type=%s charge=%f sigma=%f epsilon=%f\n",a->i,a->x,a->y,a->z,b->type,b->charge,c->sigma,c->epsilon); #endif // Interpolate the charge to the lattice gridpos = (a->x + shift[0]) / ek_parameters.agrid - 0.5f; lowernode[0] = (int) floorf( gridpos ); cellpos[0] = gridpos - lowernode[0]; gridpos = (a->y + shift[1]) / ek_parameters.agrid - 0.5f; lowernode[1] = (int) floorf( gridpos ); cellpos[1] = gridpos - lowernode[1]; gridpos = (a->z + shift[2]) / ek_parameters.agrid - 0.5f; lowernode[2] = (int) floorf( gridpos ); cellpos[2] = gridpos - lowernode[2]; lowernode[0] = (lowernode[0] + ek_parameters.dim_x) % ek_parameters.dim_x; lowernode[1] = (lowernode[1] + ek_parameters.dim_y) % ek_parameters.dim_y; lowernode[2] = (lowernode[2] + ek_parameters.dim_z) % ek_parameters.dim_z; pdb_charge_lattice[pdb_rhoindex_cartesian2linear( lowernode[0],lowernode[1],lowernode[2] )] += b->charge * ( 1 - cellpos[0] ) * ( 1 - cellpos[1] ) * ( 1 - cellpos[2] ); pdb_charge_lattice[pdb_rhoindex_cartesian2linear( ( lowernode[0] + 1 ) % ek_parameters.dim_x,lowernode[1],lowernode[2] )] += b->charge * cellpos[0] * ( 1 - cellpos[1] ) * ( 1 - cellpos[2] ); pdb_charge_lattice[pdb_rhoindex_cartesian2linear( lowernode[0],( lowernode[1] + 1 ) % ek_parameters.dim_y,lowernode[2] )] += b->charge * ( 1 - cellpos[0] ) * cellpos[1] * ( 1 - cellpos[2] ); pdb_charge_lattice[pdb_rhoindex_cartesian2linear( lowernode[0],lowernode[1],( lowernode[2] + 1 ) % ek_parameters.dim_z )] += b->charge * ( 1 - cellpos[0] ) * ( 1 - cellpos[1] ) * cellpos[2]; pdb_charge_lattice[pdb_rhoindex_cartesian2linear( ( lowernode[0] + 1 ) % ek_parameters.dim_x,( lowernode[1] + 1 ) % ek_parameters.dim_y,lowernode[2] )] += b->charge * cellpos[0] * cellpos[1] * ( 1 - cellpos[2] ); pdb_charge_lattice[pdb_rhoindex_cartesian2linear( ( lowernode[0] + 1 ) % ek_parameters.dim_x,lowernode[1],( lowernode[2] + 1 ) % ek_parameters.dim_z )] += b->charge * cellpos[0] * ( 1 - cellpos[1] ) * cellpos[2]; pdb_charge_lattice[pdb_rhoindex_cartesian2linear( lowernode[0],( lowernode[1] + 1 ) % ek_parameters.dim_y,( lowernode[2] + 1 ) % ek_parameters.dim_z )] += b->charge * ( 1 - cellpos[0] ) * cellpos[1] * cellpos[2]; pdb_charge_lattice[pdb_rhoindex_cartesian2linear( ( lowernode[0] + 1 ) % ek_parameters.dim_x,( lowernode[1] + 1 ) % ek_parameters.dim_y,( lowernode[2] + 1 ) % ek_parameters.dim_z )] += b->charge * cellpos[0] * cellpos[1] * cellpos[2]; // Interpolate lennard-jones parameters to boundary float r = pow(2,1./6.)*c->sigma; a_x_shifted = (a->x + shift[0]) / ek_parameters.agrid - 0.5f; a_y_shifted = (a->y + shift[1]) / ek_parameters.agrid - 0.5f; a_z_shifted = (a->z + shift[2]) / ek_parameters.agrid - 0.5f; for (float z = a->z - r; z <= a->z + r + ek_parameters.agrid; z += ek_parameters.agrid) { for (float y = a->y - r; y <= a->y + r + ek_parameters.agrid; y += ek_parameters.agrid) { for (float x = a->x - r; x <= a->x + r + ek_parameters.agrid; x += ek_parameters.agrid) { gridpos = (x + shift[0]) / ek_parameters.agrid - 0.5f; lowernode[0] = (int) floorf( gridpos ); gridpos = (y + shift[1]) / ek_parameters.agrid - 0.5f; lowernode[1] = (int) floorf( gridpos ); gridpos = (z + shift[2]) / ek_parameters.agrid - 0.5f; lowernode[2] = (int) floorf( gridpos ); lowernode[0] = (lowernode[0] + ek_parameters.dim_x) % ek_parameters.dim_x; lowernode[1] = (lowernode[1] + ek_parameters.dim_y) % ek_parameters.dim_y; lowernode[2] = (lowernode[2] + ek_parameters.dim_z) % ek_parameters.dim_z; #ifdef DEBUG printf("shifted: %f %f %f\n", a_x_shifted, a_y_shifted, a_z_shifted); printf("lowernode: %d %d %d\n", lowernode[0], lowernode[1], lowernode[2]); printf("distance: %f %f %f\n", lowernode[0] - a_x_shifted, lowernode[1] - a_y_shifted, lowernode[2] - a_z_shifted); printf("distance: %f <= %f\n\n", pow(lowernode[0] - a_x_shifted,2) + pow(lowernode[1] - a_y_shifted,2) + pow(lowernode[2] - a_z_shifted,2), pow(r/ek_parameters.agrid,2)); #endif if ( pow(lowernode[0] - a_x_shifted,2) + pow(lowernode[1] - a_y_shifted,2) + pow(lowernode[2] - a_z_shifted,2) <= pow(r/ek_parameters.agrid,2) ) { pdb_boundary_lattice[ek_parameters.dim_y*ek_parameters.dim_x*lowernode[2] + ek_parameters.dim_x*lowernode[1] + lowernode[0]] = 1; } } } } break; } } } } } return pdb_SUCCESS; }

float XboxController::GetLeftX() { return pow(xbox->GetRawAxis(1), joystickCurve); }

double cbrt(double n) { return n < 0 ? -pow(-n, 1.0 / 3.0) : pow(n, 1.0 / 3.0); }

float XboxController::GetRightY() { return pow(xbox->GetRawAxis(5), joystickCurve); }

double dist(int i, int j){ return sqrt( (double)pow((double)coord[i][0]-coord[j][0], 2) + (double)pow((double)coord[i][1]-coord[j][1], 2) ); }

int gauss_seidel_solve_pivoting(int nnodes, int nnz, int *ia, int *ja, int *iau, double *A, double *rhs, int neqs, double *x_star, double *xn1, double *xn, short init) { //locals int i,j; int jstart, jend; double GS_res = 0.; //int counter = 0; // allocating permutation matrix int *P = (int *)malloc( nnodes * neqs * neqs * sizeof(int) ); //replace the diagonal matrices with their LU and save in-place //also save permutation matrix band per main diagonal in [P] for ( i = 0; i < nnodes; i++) lu_serial((A + iau[i]*neqs*neqs), (P + i*neqs*neqs), neqs); //initializing solution [xn] with [rhs]/A(diag) if (init) for ( i = 0; i < nnodes; i++) for( j = 0; j < neqs; j++) { jstart = iau[i]; xn[i*neqs + j] = rhs[i*neqs + j] / A[jstart*neqs*neqs + j*neqs + j]; } do { //resetting [x_star] for ( i = 0; i < nnodes; i++) for( j = 0; j < neqs; j++) x_star[i*neqs + j] = 0.; //calculating [x_star] = -[O] [xn]. for( i = 0; i < nnodes; i++) { jstart = ia[i]; jend = ia[i+1]-1; for ( j = jstart; j <= jend; j++) if( j == iau[i] ) // this is diagonal block, skip it! continue; else //contribute the off-diagonal elements neg_matrix_vec__mult( (A + j* neqs *neqs) , (xn + ja[j]*neqs) , (x_star + i *neqs) , neqs); for ( j = 0; j < neqs; j++) //[x_star] = [rhs] + [x_star] x_star[i *neqs + j] += rhs[i * neqs + j]; } //matrix multiplication and addition will be finished after this! //solving diagonal, i.e. [xn+1] = [D]^-1 [x_star] for( i = 0; i < nnodes; i++) { j = iau[i]; solve_lu_serial((P + i*neqs*neqs), (A + j*neqs*neqs), (xn1 + i*neqs), (x_star + i*neqs), neqs); } //xn+1 will be obtained after this loop //calculating residuals GS_res = 0.; //initial for ( i = 0; i < nnodes; i++) for( j = 0; j < neqs; j++) GS_res += pow((xn1[i*neqs + j] - xn[i*neqs + j]) , 2.); GS_res /= (neqs * nnodes); GS_res = sqrt(GS_res); //updating [xn] for ( i = 0; i < nnodes; i++) for( j = 0; j < neqs; j++) xn[i*neqs + j] = xn1[i*neqs + j]; //printf("\n%e\n", GS_res); } while( (GS_res >= GS_RES_EPS) /* && (counter++ < 40) */ ); //clean - up free(P); //completed successfully! return 0; }

int * PithExtractor::drawLine(int xOrigine, int yOrigine, float orientation_ORI, int width, int height, int *length) const { float orientation = -orientation_ORI; // orientation au sens de Fleur = - orientation_ORI int x, y, k1=0, k2=0; int dim = floor(sqrt(pow(width,2.0)+pow(height,2.0))); int *droite1, *droite2, *droite; float temp; droite1 = new int[dim]; droite2 = new int[dim]; droite = new int[dim]; // 1er et 5e octant if ((orientation >= 0) && (orientation < 1)) { //entre 0 et 45 [0;45[ // 1er octant temp = yOrigine; for(x=xOrigine; x<width; x++){ y= roundf(temp); if(y>=height) break; droite1[k1] = y*width+x; k1++; temp += orientation; } //5e octant temp = yOrigine - orientation; for(x=xOrigine-1; x>=0; x--){ y = roundf(temp); if(y<0) break; droite2[k2] = y*width+x; k2++; temp -= orientation; } } // 4e et 8e octant if ((orientation < 0) && (orientation > -1)) { //entre 0 et -45 ]0;-45[ //8e octant temp = yOrigine; for(x=xOrigine; x<width; x++){ y=roundf(temp); if(y<0) break; droite1[k1] = y*width+x; k1++; temp += orientation; } //4e octant temp = yOrigine-orientation; for(x=xOrigine-1; x>=0; x--){ y = roundf(temp); if(y>=height) break; droite2[k2] = y*width+x; k2++; temp -= orientation; } } // 2e et 6e octant corrigé if (orientation >= 1) { //entre 45 et 90 [45;90] //2e octant temp = xOrigine; for(y=yOrigine; y<height; y++){ x = roundf(temp); if(x>=width) break; droite1[k1] = y*width+x; k1++; temp += (1/orientation); } //6e octant temp=xOrigine-(1/orientation); for(y=yOrigine-1; y>=0; y--){ x = roundf(temp); if(x<0) break; droite2[k2] = y*width+x; k2++; temp -= (1/orientation); } } // 3e et 7e octant corrigé if (orientation <= -1) { //entre -45 et -90 [-45;-90] //7e octant temp = xOrigine-(1/orientation); for (y=yOrigine-1; y>=0; y--) { x = roundf(temp); if(x>=width) break; droite1[k1] = y*width+x; k1++; temp -= 1/orientation; } //3e octant temp = xOrigine; for (y=yOrigine; y<height; y++) { x = roundf(temp); if(x<0) break; droite2[k2] = y*width+x; k2++; temp += 1/orientation; } } *length = k1 + k2; for (int i=0, j=k2-1; i<k2; i++, j--) { droite[i]=droite2[j]; } for(int i=k2, j=0; i<k2+k1; i++, j++){ droite[i]=droite1[j]; } delete [] droite1; delete [] droite2; return droite; }

void workshopScene::update(sceneInfo &info) { gWorld->btWorld->stepSimulation(1.f/60.f); mousevelx = mousevelx * 0.95f + info.dmousex * 0.2f; mousevely = mousevely * 0.95f + info.dmousey * 0.2f; if (info.keys.held.MouseM) { camera.pitch -= info.dmousey * 0.02; camera.yaw -= info.dmousex * 0.02; camera.orientationFromAngles(); } sceneInfo::keyState &keys = info.keys; if (!glfwGetKey('E')) { if (keys.held.W) { camera.position += camera.forward * 0.1; } else if (keys.held.S) { camera.position -= camera.forward * 0.1; } if (keys.held.D) { camera.position += camera.right * 0.1; } else if (keys.held.A) { camera.position -= camera.right * 0.1; } } if (info.captureMouse) { if (mouseWasCaptured) { cursorx += info.dmousex; cursory += info.dmousey; if (cursorx < 0) cursorx = 0; if (cursorx > info.width) cursorx = info.width; if (cursory < 0) cursory = 0; if (cursory > info.height) cursory = info.height; } else { cursorx = info.lastmousex; cursory = info.lastmousey; } } else if (mouseWasCaptured) { glfwSetMousePos(cursorx, cursory); } mouseWasCaptured = info.captureMouse; if (info.keys.held.space && selectedItem < partnames.size()) { int initialcount = gWorld->objects.size(); loadAssembly(partnames[selectedItem], btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 10, 0)), path, gWorld); for (unsigned int i = initialcount; i < gWorld->objects.size(); i++) gWorld->objects[i]->body->setDamping(0.95f, 0.96f); } if (cursorx < UI_SIZE) { if (keys.newPress.MouseL) { selectedItem = (cursory - 4) / (UI_SIZE + 8); selectedTool = -1; } } else if (cursory > info.height - UI_SMALL - 16) { if (keys.newPress.MouseL) { selectedTool = (cursorx - UI_SIZE - 16 - 8) / (UI_SMALL + 8); selectedItem = -1; } } else { mouseRayDir = camera.forward + camera.right * (cursorx - info.width / 2) / (float)info.height * 2 - camera.up * (cursory - info.height / 2) / (float)info.height * 2; btVector3 raystart = camera.position; btVector3 rayend = raystart + mouseRayDir * 100; mouseRayCallback = btCollisionWorld::ClosestRayResultCallback(raystart, rayend); gWorld->btWorld->rayTest(raystart, rayend, mouseRayCallback); if (keys.newPress.MouseL && mouseRayCallback.hasHit()) { mouseHeldBody = (btRigidBody*)mouseRayCallback.m_collisionObject; if (selectedItem >= 0) { if (selectedItem < partnames.size()) { int initialcount = gWorld->objects.size(); btTransform trans(btQuaternion(0, 0, 0, 1), mouseRayCallback.m_hitPointWorld + mouseRayCallback.m_hitNormalWorld * 1.f); loadAssembly(partnames[selectedItem], trans, path, gWorld); for (unsigned int i = initialcount; i < gWorld->objects.size(); i++) gWorld->objects[i]->body->setDamping(0.95f, 0.96f); } } else if (selectedTool == 0) { mousePerpDist = camera.forward.dot(mouseRayCallback.m_hitPointWorld - raystart); mouseHeldBody->setDamping(0.995, 0.98); mouseHeldBody->activate(); btVector3 localPivot = mouseHeldBody->getCenterOfMassTransform().inverse() * mouseRayCallback.m_hitPointWorld; mouseConstraint = new btGeneric6DofConstraint(*mouseHeldBody, btTransform(btQuaternion(0, 0, 0, 1), localPivot), false); if (glfwGetKey('E')) { mouseConstraint->setAngularLowerLimit(btVector3(0, 0, 0)); mouseConstraint->setAngularUpperLimit(btVector3(0, 0, 0)); } gWorld->btWorld->addConstraint(mouseConstraint); } else if (selectedTool == 1) { if (!mouseHeldBody->isStaticObject()) { if (!axisHasFirst) { axisHasFirst = true; axisResult = mouseRayCallback; axisFirstPivot = mouseHeldBody->getCenterOfMassTransform().inverse() * axisResult.m_hitPointWorld; axisFirstNormal = btTransform(mouseHeldBody->getCenterOfMassTransform().inverse().getRotation(), btVector3(0, 0, 0)) * axisResult.m_hitNormalWorld; std::cout << "First point for axis.\n"; } else { btVector3 axisSecondNormal = btTransform(mouseHeldBody->getCenterOfMassTransform().inverse().getRotation(), btVector3(0, 0, 0)) * mouseRayCallback.m_hitNormalWorld; btVector3 axisSecondPivot = mouseHeldBody->getCenterOfMassTransform().inverse() * mouseRayCallback.m_hitPointWorld + axisSecondNormal * 0.05; gWorld->addConstraint(new btHingeConstraint(*(btRigidBody*)axisResult.m_collisionObject, *mouseHeldBody, axisFirstPivot, axisSecondPivot, -axisFirstNormal, axisSecondNormal)); mouseHeldBody->activate(); axisHasFirst = false; } } } else if (selectedTool == 2) { btRigidBody *body = (btRigidBody*)mouseRayCallback.m_collisionObject; if (!body->isStaticObject()) gWorld->removeBody(body); } } } if (mouseConstraint) { if (keys.held.MouseL) { mousePerpDist *= pow(1.1, keys.dmouseWheel); mouseConstraint->getFrameOffsetA().setOrigin(camera.position + mouseRayDir * mousePerpDist); // note that raydir is not unit length: it stretches from the camera to the near plane. } else { mouseHeldBody->setDamping(0.95, 0.96); gWorld->btWorld->removeConstraint(mouseConstraint); delete mouseConstraint; mouseConstraint = 0; } if (glfwGetKey('E')) { btTransform invrotate; mouseHeldBody->getMotionState()->getWorldTransform(invrotate); invrotate = invrotate.inverse(); if (keys.held.W) mouseHeldBody->applyImpulse(invrotate * camera.forward, invrotate * camera.up); if (keys.held.S) mouseHeldBody->applyImpulse(-camera.forward, camera.up); if (keys.held.A) mouseHeldBody->applyImpulse(-camera.right, camera.forward); if (keys.held.D) mouseHeldBody->applyImpulse(camera.right, camera.forward); } } if (axisHasFirst && selectedTool != 1) axisHasFirst = false; }

bool Sphere::intersect(const Ray &r, Hit &h) const { // std::cout << " current t value " << h.getT() << std::endl;; // ========================================== // IMPLEMENT SPHERE INTERSECTION // ========================================== // plug the explicit ray equation into the implict sphere equation and solve // return true if the sphere was intersected, and update // the hit data structure to contain the value of t for the ray at // the intersection point, the material, and the normal Vec3f ray_origin = r.getOrigin(); Vec3f ray_direction = r.getDirection(); double b = 2.0 * (ray_direction.Dot3(ray_origin - center)); double c = (ray_origin - center).Dot3(ray_origin - center) - pow(radius, 2.0); double d = (pow(b,2.0) - 4.0 * c); //case where roots are imaginary if(d < 0){ return false; } // std::cout << "b: " << b << " c: " << c << std::endl; double root1 = ((-1)* b + sqrt(d))/2.0; double root2 = ((-1)* b - sqrt(d))/2.0; // std::cout << "root1: " << root1 << " root2: " << root2 << std::endl; Vec3f root1_normal = ray_origin + root1*ray_direction; root1_normal -=center; root1_normal.Normalize(); Vec3f root2_normal = ray_origin + root2*ray_direction; root2_normal -=center; root2_normal.Normalize(); //sphere is behind us if(root1 < 0 && root2 < 0){ // std::cout << "both are negative" << std::endl; return false; } //only root1 is in front if(root1 >= eps && root2 < 0 && (h.getT() < eps || root1 < h.getT())) { // if(root1 < h.get_t()){ h.set(root1, material, root1_normal,NULL); // } return true; } //only root2 is in front if(root2 >= eps && root1 < 0 && (h.getT() < eps || root1 < h.getT())) { // if(root2 < h.get_t()){ h.set(root2, material, root2_normal,NULL); // } return true; } //both are in front if(root1 >= eps && root2 >= eps && (root1 < h.getT() || root2 < h.getT() || h.getT() < eps) ) { if(root1 < root2 ){ h.set(root1, material, root1_normal,NULL); } else { h.set(root2, material, root2_normal,NULL); } return true; } //If it gets to this case it means that we did hit the sphere, but there was already a closer hit. //std::cout << "All cases exhausted. This shouldn't happen" << std::endl; return false; }

int main (void) { float zerof = 0.0f, minus_onef = -1.0f ; double zero = 0.0, minus_one = -1.0 ; /* Check for +ZERO (float) */ { float f = 0.0f; const char mantissa[] = "00000000000000000000000"; gsl_ieee_float_rep r; gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 0, "float x = 0, sign is +"); gsl_test_int (r.exponent, -127, "float x = 0, exponent is -127"); gsl_test_str (r.mantissa, mantissa, "float x = 0, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = 0, type is ZERO"); } /* Check for -ZERO (float) */ { float f = minus_onef; const char mantissa[] = "00000000000000000000000"; gsl_ieee_float_rep r; while (f < 0) { f *= 0.1f; } gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 1, "float x = -1*0, sign is -"); gsl_test_int (r.exponent, -127, "float x = -1*0, exponent is -127"); gsl_test_str (r.mantissa, mantissa, "float x = -1*0, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = -1*0, type is ZERO"); } /* Check for a positive NORMAL number (e.g. 2.1) (float) */ { float f = 2.1f; const char mantissa[] = "00001100110011001100110"; gsl_ieee_float_rep r; gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 0, "float x = 2.1, sign is +"); gsl_test_int (r.exponent, 1, "float x = 2.1, exponent is 1"); gsl_test_str (r.mantissa, mantissa, "float x = 2.1, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 2.1, type is NORMAL"); } /* Check for a negative NORMAL number (e.g. -1.3304...) (float) */ { float f = -1.3303577090924210f ; const char mantissa[] = "01010100100100100101001"; gsl_ieee_float_rep r; gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 1, "float x = -1.3304..., sign is -"); gsl_test_int (r.exponent, 0, "float x = -1.3304..., exponent is 0"); gsl_test_str (r.mantissa, mantissa, "float x = -1.3304..., mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = -1.3304..., type is NORMAL"); } /* Check for a large positive NORMAL number (e.g. 3.37e31) (float) */ { float f = 3.37e31f; const char mantissa[] = "10101001010110101001001"; gsl_ieee_float_rep r; gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 0, "float x = 3.37e31, sign is +"); gsl_test_int (r.exponent, 104, "float x = 3.37e31, exponent is 104"); gsl_test_str (r.mantissa, mantissa, "float x = 3.37e31, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 3.37e31, type is NORMAL"); } /* Check for a small positive NORMAL number (e.g. 3.37e-31) (float) */ { float f = 3.37e-31f; const char mantissa[] = "10110101011100110111011"; gsl_ieee_float_rep r; gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 0, "float x = 3.37e-31, sign is +"); gsl_test_int (r.exponent, -102, "float x = 3.37e-31, exponent is -102"); gsl_test_str (r.mantissa, mantissa, "float x = 3.37e-31, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 3.37e-31, type is NORMAL"); } /* Check for FLT_MIN (smallest possible number that is not denormal) */ { float f = FLT_MIN; const char mantissa[] = "00000000000000000000000"; gsl_ieee_float_rep r; gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 0, "float x = FLT_MIN, sign is +"); gsl_test_int (r.exponent, -126, "float x = FLT_MIN, exponent is -126"); gsl_test_str (r.mantissa, mantissa, "float x = FLT_MIN, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MIN, type is NORMAL"); } /* Check for FLT_MAX (largest possible number that is not Inf) */ { float f = FLT_MAX; const char mantissa[] = "11111111111111111111111"; gsl_ieee_float_rep r; gsl_ieee_float_to_rep (&f, &r); gsl_test_int (r.sign, 0, "float x = FLT_MAX, sign is +"); gsl_test_int (r.exponent, 127, "float x = FLT_MAX, exponent is 127"); gsl_test_str (r.mantissa, mantissa, "float x = FLT_MAX, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MAX, type is NORMAL"); } /* Check for DENORMAL numbers (e.g. FLT_MIN/2^n) */ #ifdef TEST_DENORMAL { float f = FLT_MIN; char mantissa[] = "10000000000000000000000"; int i; gsl_ieee_float_rep r; for (i = 0; i < 23; i++) { float x = f / (float)pow (2.0, 1 + (float) i); mantissa[i] = '1'; gsl_ieee_float_to_rep (&x, &r); gsl_test_int (r.sign, 0, "float x = FLT_MIN/2^%d, sign is +", i + 1); gsl_test_int (r.exponent, -127, "float x = FLT_MIN/2^%d, exponent is -127", i + 1); gsl_test_str (r.mantissa, mantissa, "float x = FLT_MIN/2^%d, mantissa", i + 1); gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL, "float x = FLT_MIN/2^%d, type is DENORMAL", i + 1); mantissa[i] = '0'; } } #endif /* Check for positive INFINITY (e.g. 2*FLT_MAX) */ { float f = FLT_MAX; const char mantissa[] = "00000000000000000000000"; gsl_ieee_float_rep r; float x; x = 2 * f; gsl_ieee_float_to_rep (&x, &r); gsl_test_int (r.sign, 0, "float x = 2*FLT_MAX, sign is +"); gsl_test_int (r.exponent, 128, "float x = 2*FLT_MAX, exponent is 128"); gsl_test_str (r.mantissa, mantissa, "float x = 2*FLT_MAX, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF"); } /* Check for negative INFINITY (e.g. -2*FLT_MAX) */ { float f = FLT_MAX; const char mantissa[] = "00000000000000000000000"; gsl_ieee_float_rep r; float x; x = -2 * f; gsl_ieee_float_to_rep (&x, &r); gsl_test_int (r.sign, 1, "float x = -2*FLT_MAX, sign is -"); gsl_test_int (r.exponent, 128, "float x = -2*FLT_MAX, exponent is 128"); gsl_test_str (r.mantissa, mantissa, "float x = -2*FLT_MAX, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF"); } /* Check for NAN (e.g. Inf - Inf) (float) */ { gsl_ieee_float_rep r; float x = 1.0f, y = 2.0f, z = zerof; x = x / z; y = y / z; z = y - x; gsl_ieee_float_to_rep (&z, &r); /* We don't check the sign and we don't check the mantissa because they could be anything for a NaN */ gsl_test_int (r.exponent, 128, "float x = NaN, exponent is 128"); gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "float x = NaN, type is NAN"); } /* Check for +ZERO */ { double d = 0.0; const char mantissa[] = "0000000000000000000000000000000000000000000000000000"; gsl_ieee_double_rep r; gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 0, "double x = 0, sign is +"); gsl_test_int (r.exponent, -1023, "double x = 0, exponent is -1023"); gsl_test_str (r.mantissa, mantissa, "double x = 0, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = 0, type is ZERO"); } /* Check for -ZERO */ { double d = minus_one; const char mantissa[] = "0000000000000000000000000000000000000000000000000000"; gsl_ieee_double_rep r; while (d < 0) { d *= 0.1; } gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 1, "double x = -1*0, sign is -"); gsl_test_int (r.exponent, -1023, "double x = -1*0, exponent is -1023"); gsl_test_str (r.mantissa, mantissa, "double x = -1*0, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = -1*0, type is ZERO"); } /* Check for a positive NORMAL number (e.g. 2.1) */ { double d = 2.1; const char mantissa[] = "0000110011001100110011001100110011001100110011001101"; gsl_ieee_double_rep r; gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 0, "double x = 2.1, sign is +"); gsl_test_int (r.exponent, 1, "double x = 2.1, exponent is 1"); gsl_test_str (r.mantissa, mantissa, "double x = 2.1, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = 2.1, type is NORMAL"); } /* Check for a negative NORMAL number (e.g. -1.3304...) */ { double d = -1.3303577090924210146738460025517269968986511230468750; const char mantissa[] = "0101010010010010010100101010010010001000100011101110"; gsl_ieee_double_rep r; gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 1, "double x = -1.3304..., sign is -"); gsl_test_int (r.exponent, 0, "double x = -1.3304..., exponent is 0"); gsl_test_str (r.mantissa, mantissa, "double x = -1.3304..., mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = -1.3304..., type is NORMAL"); } /* Check for a large positive NORMAL number (e.g. 3.37e297) */ { double d = 3.37e297; const char mantissa[] = "0100100111001001100101111001100000100110011101000100"; gsl_ieee_double_rep r; gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 0, "double x = 3.37e297, sign is +"); gsl_test_int (r.exponent, 988, "double x = 3.37e297, exponent is 998"); gsl_test_str (r.mantissa, mantissa, "double x = 3.37e297, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = 3.37e297, type is NORMAL"); } /* Check for a small positive NORMAL number (e.g. 3.37e-297) */ { double d = 3.37e-297; const char mantissa[] = "0001101000011011101011100001110010100001001100110111"; gsl_ieee_double_rep r; gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 0, "double x = 3.37e-297, sign is +"); gsl_test_int (r.exponent, -985, "double x = 3.37e-297, exponent is -985"); gsl_test_str (r.mantissa, mantissa, "double x = 3.37e-297, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = 3.37e-297, type is NORMAL"); } /* Check for DBL_MIN (smallest possible number that is not denormal) */ { double d = DBL_MIN; const char mantissa[] = "0000000000000000000000000000000000000000000000000000"; gsl_ieee_double_rep r; gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 0, "double x = DBL_MIN, sign is +"); gsl_test_int (r.exponent, -1022, "double x = DBL_MIN, exponent is -1022"); gsl_test_str (r.mantissa, mantissa, "double x = DBL_MIN, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = DBL_MIN, type is NORMAL"); } /* Check for DBL_MAX (largest possible number that is not Inf) */ { double d = DBL_MAX; const char mantissa[] = "1111111111111111111111111111111111111111111111111111"; gsl_ieee_double_rep r; gsl_ieee_double_to_rep (&d, &r); gsl_test_int (r.sign, 0, "double x = DBL_MAX, sign is +"); gsl_test_int (r.exponent, 1023, "double x = DBL_MAX, exponent is 1023"); gsl_test_str (r.mantissa, mantissa, "double x = DBL_MAX, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = DBL_MAX, type is NORMAL"); } /* Check for DENORMAL numbers (e.g. DBL_MIN/2^n) */ #ifdef TEST_DENORMAL { double d = DBL_MIN; char mantissa[] = "1000000000000000000000000000000000000000000000000000"; int i; gsl_ieee_double_rep r; for (i = 0; i < 52; i++) { double x = d / pow (2.0, 1 + (double) i); mantissa[i] = '1'; gsl_ieee_double_to_rep (&x, &r); gsl_test_int (r.sign, 0, "double x = DBL_MIN/2^%d, sign is +", i + 1); gsl_test_int (r.exponent, -1023, "double x = DBL_MIN/2^%d, exponent", i + 1); gsl_test_str (r.mantissa, mantissa, "double x = DBL_MIN/2^%d, mantissa", i + 1); gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL, "double x = DBL_MIN/2^%d, type is DENORMAL", i + 1); mantissa[i] = '0'; } } #endif /* Check for positive INFINITY (e.g. 2*DBL_MAX) */ { double d = DBL_MAX; const char mantissa[] = "0000000000000000000000000000000000000000000000000000"; gsl_ieee_double_rep r; double x; x = 2.0 * d; gsl_ieee_double_to_rep (&x, &r); gsl_test_int (r.sign, 0, "double x = 2*DBL_MAX, sign is +"); gsl_test_int (r.exponent, 1024, "double x = 2*DBL_MAX, exponent is 1024"); gsl_test_str (r.mantissa, mantissa, "double x = 2*DBL_MAX, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "double x = 2*DBL_MAX, type is INF"); } /* Check for negative INFINITY (e.g. -2*DBL_MAX) */ { double d = DBL_MAX; const char mantissa[] = "0000000000000000000000000000000000000000000000000000"; gsl_ieee_double_rep r; double x; x = -2.0 * d; gsl_ieee_double_to_rep (&x, &r); gsl_test_int (r.sign, 1, "double x = -2*DBL_MAX, sign is -"); gsl_test_int (r.exponent, 1024, "double x = -2*DBL_MAX, exponent is 1024"); gsl_test_str (r.mantissa, mantissa, "double x = -2*DBL_MAX, mantissa"); gsl_test_int (r.type, GSL_IEEE_TYPE_INF,"double x = -2*DBL_MAX, type is INF"); } /* Check for NAN (e.g. Inf - Inf) */ { gsl_ieee_double_rep r; double x = 1.0, y = 2.0, z = zero; x = x / z; y = y / z; z = y - x; gsl_ieee_double_to_rep (&z, &r); /* We don't check the sign and we don't check the mantissa because they could be anything for a NaN */ gsl_test_int (r.exponent, 1024, "double x = NaN, exponent is 1024"); gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "double x = NaN, type is NAN"); } exit (gsl_test_summary ()); }