static T find( const Sphere< T > &sphere, const Ray< 3, T > &ray, T lowerBound = Numeric< T >::ZERO_TOLERANCE, T upperBound = std::numeric_limits< T >::max() ) { Vector< 3, T > centerDiff( ray.getOrigin() - sphere.getCenter() ); T a = ray.getDirection().getSquaredMagnitude(); T b = 2 * ( centerDiff * ray.getDirection() ); T c = centerDiff.getSquaredMagnitude() - ( sphere.getRadius() * sphere.getRadius() ); T t0, t1; if ( Root::compute( a, b, c, t0, t1 ) == 0 ) { return -1; } if ( t0 < upperBound && t0 > lowerBound ) { return t0; } if ( t1 < upperBound && t1 > lowerBound ) { return t1; } return -1; }
int main(int argc, char* argv[]){ FILE *inputFile; //done int breakMe = 0, lastRead = 0; char *buffer, *tmpChar; //done long i=0, j=0, n=0, l=0, colAmt=0, rowAmt=0, fileSize=0, k=atoi(argv[2]); long closePoint = 0; long iterCount = 0; double *M, *centers1, *centers2, *tmpCenters, *centerWeights; //done //Variable allocation and initialization inputFile = fopen(argv[1], "r"); buffer = malloc(memBlock); M = malloc(memBlock); srand(time(NULL)); fgets(buffer, memBlock, inputFile); tmpChar = strtok(buffer, delims); while(tmpChar != NULL){ ++colAmt; tmpChar = strtok(NULL, delims); } rowAmt = memBlock/(sizeof(double)*colAmt); fseek(inputFile, 0, SEEK_END); fileSize = ftell(inputFile); rewind(inputFile); centers1 = (double*) malloc(sizeof(double)*k*colAmt); centers2 = (double*) malloc(sizeof(double)*k*colAmt); centerWeights = (double*) malloc(sizeof(double)*k); /*******SEEDING**********************/ tmpChar = NULL; lastRead = 0; do{ fillBuffer(inputFile, fileSize, buffer); for(i=0;i<(rowAmt*colAmt); ++i){ if(tmpChar == NULL){ if(lastRead){ breakMe=1; break; } else{ lastRead = fillBuffer(inputFile, fileSize, buffer); tmpChar = strtok(buffer, delims); } } M[i] = atof(tmpChar); tmpChar = strtok(NULL, delims); } }while(i<(rowAmt*colAmt) && !breakMe); breakMe = 0; for(j=0; j<k; ++j){ centerWeights[j] = 0; memmove(centers1+(j*colAmt), M+((rand()%(i/colAmt))*colAmt), sizeof(double)*colAmt); memmove(centers2+(j*colAmt), centers1+(j*colAmt), sizeof(double)*colAmt); } /********CLUSTERING**********************/ rewind(inputFile); tmpChar = NULL; lastRead = 0; breakMe = 0; iterCount = 0; for(n=0; n<10; ++n){ do{ do{ for(i=0;i<(rowAmt*colAmt); ++i){ if(tmpChar == NULL){ if(lastRead){ breakMe=1; break; } else{ printf("%zu : %lu\r", ftell(inputFile), fileSize); lastRead = fillBuffer(inputFile, fileSize, buffer); tmpChar = strtok(buffer, delims); } } M[i] = atof(tmpChar); tmpChar = strtok(NULL, delims); } for(j=0; j<(i/colAmt); ++j){ closePoint = findClosest(M+(j*colAmt), centers1, colAmt, k); for(l=0; l<colAmt; ++l){ centers2[closePoint*colAmt+l] = ((centers2[closePoint*colAmt+l] * centerWeights[closePoint]) + M[j*colAmt + l]) / (centerWeights[closePoint] +1); } ++centerWeights[closePoint]; } }while(!breakMe); /*******PRINT CENTERS************************ printf("---CENTERS---\n"); for(j=0;j<k; ++j){ printf("%lu: ", j+1); for(l=0; l<colAmt; ++l) printf("%f ", centers2[j*colAmt + l]); printf("\n"); } ********************************************/ for(i=0;i<k;++i) centerWeights[i] = 0; tmpCenters = centers1; centers1 = centers2; centers2 = tmpCenters; breakMe = 0; lastRead = 0; rewind(inputFile); ++iterCount; if(iterCount > iterLimit){ printf("ITERLIMITBREAK\n"); break; } }while(centerDiff(centers1, centers2, .0001, k, colAmt)); } printf("---CENTERS---\n"); for(j=0;j<k; ++j){ printf("%lu: ", j+1); for(l=0; l<colAmt; ++l) printf("%f ", centers2[j*colAmt + l]); printf("\n"); } fclose(inputFile); free(buffer); free(M); free(centers1); free(centers2); free(centerWeights); return 1; }