16 long int random(
void) {
return (
long int) rand(); }
17 void srandom(
unsigned int seed) { srand(seed); }
85 void get_angles(
tfloat *a1,
tfloat *a2,
tfloat *a3,
tfloat xr,
tfloat yr,
tfloat or,
tfloat x1,
tfloat y1,
tfloat x2,
tfloat y2,
tfloat x3,
tfloat y3)
87 *a1 = atan2( (y1 - yr) , (x1 - xr) ) - or ;
88 *a2 = atan2( (y2 - yr) , (x2 - xr) ) - or ;
89 *a3 = atan2( (y3 - yr) , (x3 - xr) ) - or ;
118 case 1:
return triangulationPierlot(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
119 case 2:
return triangulationEsteves(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
120 case 3:
return triangulationFontLlagunes(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
121 case 4:
return triangulationZalama(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
122 case 5:
return triangulationMcGillemTrigo(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
123 case 6:
return triangulationMcGillemGeometric(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
124 case 7:
return triangulationEaston(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
125 case 8:
return triangulationHmam(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
126 case 9:
return triangulationCohenTrigo(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
127 case 10:
return triangulationCohenGeometric(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
128 case 11:
return triangulationCohenGeometricOriginal(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
129 case 12:
return triangulationTsukiyama(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
130 case 13:
return triangulationTsukiyamaOriginal(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
131 case 14:
return triangulationMadsen(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
132 case 15:
return triangulationLukic(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
133 case 16:
return triangulationLukicOriginal(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
134 case 17:
return triangulationTienstra(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
135 case 18:
return triangulationLigas(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
136 case 19:
return triangulationFontLlagunes2(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
137 case 20:
return triangulationKaestner(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
138 case 21:
return triangulationCollins(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
139 case 22:
return triangulationCassini(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
140 case 42:
return triangulationPierlot2(x, y, alpha1, alpha2, alpha3, x1, y1, x2, y2, x3, y3) ;
141 default: fprintf(stderr,
"triangulationMethod(): Bad method number!\n") ;
return -1 ;
158 for( i = 0 ; i < N ; ++i )
178 m =
mean( data , N ) ;
180 for( i = 0 ; i < N ; ++i )
181 variance += (data[i]-m) * (data[i]-m) ;
192 struct timeval startTime ;
193 gettimeofday( &startTime , NULL ) ;
194 srandom( startTime.tv_usec ) ;
208 while( s == 0.0 || s >= 1.0 )
210 u = ( ( (
tfloat) random() / (
tfloat) RAND_MAX ) * 2.0 ) - 1.0 ;
211 v = ( ( (
tfloat) random() / (
tfloat) RAND_MAX ) * 2.0 ) - 1.0 ;
215 return u * sqrt( -2 * log(s) / s ) ;
243 int method,
tfloat sigmaAngle,
tfloat sigmaBeacon,
unsigned int n,
int outputType,
double thresh)
254 tfloat ea1=0.0, ea2=0.0, ea3=0.0, ex1=0.0, ey1=0.0, ex2=0.0, ey2=0.0, ex3=0.0, ey3=0.0, s ;
255 tfloat *errP = (
tfloat * ) malloc( n *
sizeof(
tfloat) ) ;
if( errP == NULL )
return -1 ;
256 tfloat *errO = (
tfloat * ) malloc( n *
sizeof(
tfloat) ) ;
if( errO == NULL )
return -1 ;
257 tfloat *Qual = (
tfloat * ) malloc( n *
sizeof(
tfloat) ) ;
if( Qual == NULL )
return -1 ;
263 for ( i = 0 ; i < n ; ++i )
265 if ( sigmaAngle != 0.0 )
271 if ( sigmaBeacon != 0.0 )
281 get_angles(&alpha1, &alpha2, &alpha3, xTrue, yTrue, 0.0, x1+ex1, y1+ey1, x2+ex2, y2+ey2, x3+ex3, y3+ey3) ;
282 qual =
triangulationMethod(&xNoise, &yNoise, alpha1+ea1, alpha2+ea2, alpha3+ea3, x1, y1, x2, y2, x3, y3, method) ;
284 oNoise = atan2( (y1-yNoise) , (x1-xNoise) ) - alpha1 ;
292 errP[ i ] = sqrt( (xTrue-xNoise)*(xTrue-xNoise) + (yTrue-yNoise)*(yTrue-yNoise) ) ;
293 errO[ i ] = oTrue - oNoise ;
while( errO[ i ] >
PI ) errO[ i ] -= TWOPI ;
while( errO[ i ] <= -
PI ) errO[ i ] += TWOPI ;
299 case 1: s = sqrt (
variance ( errP , n ) ) ; break ;
300 case 2: s = sqrt (
variance ( errO , n ) ) * RAD2DEG ; break ;
301 case 3: s =
mean ( Qual , n ) ; break ;
302 default: fprintf( stderr ,
"triangulationSensitivity(): Bad output type!\n" ) ;
return -1 ;
311 if( s <= thresh )
return 1.0 ;
336 tfloat jacobian[3][3] , det , d1 , d2 , d3 ;
338 d1 = ( x1 - xr ) * ( x1 - xr ) + ( y1 - yr ) * ( y1 - yr ) ;
339 d2 = ( x2 - xr ) * ( x2 - xr ) + ( y2 - yr ) * ( y2 - yr ) ;
340 d3 = ( x3 - xr ) * ( x3 - xr ) + ( y3 - yr ) * ( y3 - yr ) ;
342 jacobian[0][0] = ( y1 - yr ) / d1 ; jacobian[0][1] = ( x1 - xr ) / d1 ; jacobian[0][2] = 1.0 ;
343 jacobian[1][0] = ( y2 - yr ) / d2 ; jacobian[1][1] = ( x2 - xr ) / d2 ; jacobian[1][2] = 1.0 ;
344 jacobian[2][0] = ( y3 - yr ) / d3 ; jacobian[2][1] = ( x3 - xr ) / d3 ; jacobian[2][2] = 1.0 ;
348 return 1.0 / fabs( det ) ;
360 return M[0][0]*M[1][1]*M[2][2] + M[0][1]*M[1][2]*M[2][0] + M[1][0]*M[2][1]*M[0][2] - M[0][2]*M[1][1]*M[2][0] - M[0][1]*M[1][0]*M[2][2] - M[1][2]*M[2][1]*M[0][0] ;
380 tfloat *map = (
tfloat * ) malloc( width * height *
sizeof(
tfloat) ) ;
if( map == NULL )
return -1 ;
381 tfloat *scale = (
tfloat * ) malloc( (width/SWP) * height *
sizeof(
tfloat) ) ;
if( scale == NULL )
return -1 ;
382 tfloat *map_color = (
tfloat * ) malloc( width * height * 3 *
sizeof(
tfloat) ) ;
if( map_color == NULL )
return -1 ;
383 tfloat *scale_color = (
tfloat * ) malloc( (width/SWP) * height * 3 *
sizeof(
tfloat) ) ;
if( scale_color == NULL )
return -1 ;
405 fprintf(stderr,
"saveMapAndScale(): Bad map scaling mode!\n") ;
409 free( scale_color ) ;
412 writePGM( map , width , height , Max ,
"map.pgm" ) ;
413 writePGM( scale , width/SWP , height , Max ,
"scale.pgm" ) ;
416 grayscale2RGB( scale , scale_color , width/SWP , height , Max ) ;
418 writePPM( map_color , width , height , Max ,
"map.ppm" ) ;
419 writePPM( scale_color , width/SWP , height , Max ,
"scale.ppm" ) ;
425 free( scale_color ) ;
445 for( j = 0 ; j < height ; ++j )
447 for( i = 0 ; i < width ; ++i )
449 v = imageG [ (height-j-1)*width + i ] ;
450 c =
mapRGB( v , 0.0 , Max ) ;
451 imageRGB [ (height-j-1)*width*3 + i*3 + 0 ] = c.r * Max ;
452 imageRGB [ (height-j-1)*width*3 + i*3 + 1 ] = c.g * Max ;
453 imageRGB [ (height-j-1)*width*3 + i*3 + 2 ] = c.b * Max ;
474 color c = { 1.0 , 1.0 , 1.0 } ;
477 if (v < vmin) v = vmin ;
478 if (v > vmax) v = vmax ;
481 if ( v < ( vmin + 0.25 * dv ) ) {
483 c.g = 4 * ( v - vmin ) / dv ;
484 }
else if ( v < ( vmin + 0.5 * dv ) ) {
486 c.b = 1 + 4 * ( vmin + 0.25 * dv - v ) / dv ;
487 }
else if ( v < ( vmin + 0.75 * dv ) ) {
488 c.r = 4 * ( v - vmin - 0.5 * dv ) / dv ;
491 c.g = 1 + 4 * ( vmin + 0.75 * dv - v ) / dv ;
513 int writePGM(
tfloat image[] ,
unsigned int width ,
unsigned int height ,
unsigned int Max ,
char* fileName )
516 FILE *file = fopen( fileName ,
"w" ) ;
if( file == NULL )
return -1 ;
519 fprintf( file ,
"P2\n" ) ;
520 fprintf( file ,
"%d\n", width ) ;
521 fprintf( file ,
"%d\n", height ) ;
522 fprintf( file ,
"%d\n", Max ) ;
523 for( j = 0 ; j < height ; ++j )
525 for( i = 0 ; i < width ; ++i )
527 fprintf( file ,
"%u ", (
unsigned int ) image [ (height-j-1)*width + i ] ) ;
529 fprintf( file ,
"\n" ) ;
551 int writePPM(
tfloat image[] ,
unsigned int width ,
unsigned int height ,
unsigned int Max ,
char* fileName )
554 FILE *file = fopen( fileName ,
"w" ) ;
if( file == NULL )
return -1 ;
557 fprintf( file ,
"P3\n" ) ;
558 fprintf( file ,
"%d\n", width ) ;
559 fprintf( file ,
"%d\n", height ) ;
560 fprintf( file ,
"%d\n", Max ) ;
561 for( j = 0 ; j < height ; ++j )
563 for( i = 0 ; i < width ; ++i )
565 fprintf( file ,
"%u ", (
unsigned int ) image [ (height-j-1)*width*3 + i*3 ] ) ;
566 fprintf( file ,
"%u ", (
unsigned int ) image [ (height-j-1)*width*3 + i*3 + 1 ] ) ;
567 fprintf( file ,
"%u ", (
unsigned int ) image [ (height-j-1)*width*3 + i*3 + 2 ] ) ;
569 fprintf( file ,
"\n" ) ;
598 if ( a_->value < b_->value )
return -1 ;
599 if ( a_->value > b_->value )
return 1 ;
616 unsigned int N = (width * height) ;
618 if ( tmp == NULL ) return ;
619 for ( pos = 0 ; pos < N ; ++ pos ) {
620 tmp [ pos ] .pos = pos ;
621 tmp [ pos ] .value = in [ pos ] ;
625 printf(
"min = %f\n" , tmp[ 0 ] .value ) ;
626 printf(
"max = %f\n" , tmp[ N-1 ] .value ) ;
628 for ( pos = 0 ; pos < N ; ++ pos ) {
629 out [ tmp [ pos ] .pos ] = pos * numgraylevels / N ;
648 unsigned int N = (width * height) ;
649 unsigned int Nmax = (width * height) - ( (width * height) / p ) ;
652 if ( tmp == NULL ) return ;
653 for ( pos = 0 ; pos < N ; ++ pos ) {
654 tmp [ pos ] .pos = pos ;
655 tmp [ pos ] .value = in [ pos ] ;
659 max = tmp [ Nmax ] .value ;
661 for ( pos = 0 ; pos < N ; ++ pos ) {
662 if( in [ pos ] > max ) out [ pos ] = max ;
663 else out [ pos ] = in [ pos ] ;
681 unsigned int index , indexMin = 0 , indexMax = 0 , j ;
682 unsigned int N = (width * height) ;
683 tfloat min = in[0] , max = in[0] ;
686 for( index = 0 ; index < N ; ++index )
688 if( in[index] < min ) min = in[index] , indexMin = index ;
689 if( in[index] > max ) max = in[index] , indexMax = index ;
691 printf(
"min = %f @ (%d,%d)\n" , min , indexMin/width , indexMin%width ) ;
692 printf(
"max = %f @ (%d,%d)\n" , max , indexMax/width , indexMax%width ) ;
695 for( index = 0 ; index < N ; ++index )
697 out[index] = ( ( in[index] - min ) / ( max - min ) ) * numgraylevels ;
701 for( index = 0 ; index < height ; ++index )
703 for( j = 0 ; j < (width/SWP) ; ++j )
704 scale[ index*(width/SWP) + j ] = index * (
tfloat)numgraylevels / (
tfloat)height ;
721 unsigned int index , indexMin = 0 , indexMax = 0 , j ;
722 unsigned int N = (width * height) ;
723 tfloat min = in[0] , max = in[0] ;
726 for( index = 0 ; index < N ; ++index )
728 if( in[index] < min ) min = in[index] , indexMin = index ;
729 if( in[index] > max ) max = in[index] , indexMax = index ;
731 printf(
"min = %f @ (%d,%d)\n" , min , indexMin/width , indexMin%width ) ;
732 printf(
"max = %f @ (%d,%d)\n" , max , indexMax/width , indexMax%width ) ;
735 for( index = 0 ; index < N ; ++index )
737 out[index] = log( 1 + P1 * (( in[index] - min ) / ( max - min )) ) * (
tfloat)numgraylevels / log( 1 + P1 ) ;
741 for( index = 0 ; index < height ; ++index )
743 for( j = 0 ; j < (width/SWP) ; ++j )
744 scale[ index*(width/SWP) + j ] = log( 1 + P1 * ( (
tfloat)index / (
tfloat)height ) ) * (
tfloat)numgraylevels / log( 1 + P1 ) ;
#define PI
The value of PI.
double tfloat
Defines the type for float/double.
tfloat triangulationPierlot(tfloat *x, tfloat *y, tfloat alpha1, tfloat alpha2, tfloat alpha3, tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)