1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/*
* Modification History
*
* 2004-June-12 Jason Rohrer
* Created.
*
* 2004-June-15 Jason Rohrer
* Added a copy function.
*
* 2004-June-18 Jason Rohrer
* Changed to use triangles instead of polygons.
*
* 2004-August-9 Jason Rohrer
* Made a subclass of ParameterSpaceControlPoint.
*
* 2004-August-12 Jason Rohrer
* Optimized Color constructor.
* Optimized blendColorArrays function.
*
* 2004-August-26 Jason Rohrer
* Added scaling of rotated copies to support spiral shapes.
*
* 2004-August-27 Jason Rohrer
* Added function for writing out to file.
*
* 2004-August-29 Jason Rohrer
* Added a scale factor for the angle of rotated copies.
*
* 2004-August-30 Jason Rohrer
* Optimization: avoid object blending whenever possible.
*/
#include "ObjectParameterSpaceControlPoint.h"
#include "NamedColorFactory.h"
#include <math.h>
ObjectParameterSpaceControlPoint::ObjectParameterSpaceControlPoint(
int inNumTriangleVertices, Vector3D **inTriangleVertices,
Color **inTriangleVertexFillColors,
int inNumBorderVertices, Vector3D **inBorderVertices,
Color **inBorderVertexColors,
double inBorderWidth,
double inNumRotatedCopies,
double inRotatedCopyScaleFactor,
double inRotatedCopyAngleScaleFactor,
double inRotationRate )
: mNumTriangleVertices( inNumTriangleVertices ),
mTriangleVertices( inTriangleVertices ),
mTriangleVertexFillColors( inTriangleVertexFillColors ),
mNumBorderVertices( inNumBorderVertices ),
mBorderVertices( inBorderVertices ),
mBorderVertexColors( inBorderVertexColors ),
mBorderWidth( inBorderWidth ),
mNumRotatedCopies( inNumRotatedCopies ),
mRotatedCopyScaleFactor( inRotatedCopyScaleFactor ),
mRotatedCopyAngleScaleFactor( inRotatedCopyAngleScaleFactor ),
mRotationRate( inRotationRate ) {
}
ObjectParameterSpaceControlPoint::ObjectParameterSpaceControlPoint(
FILE *inFILE, char *outError ) {
int totalNumRead = 0;
mNumTriangleVertices = 0;
totalNumRead += fscanf( inFILE, "%d", &mNumTriangleVertices );
// how many values should we successfully read (cheap error checking)
int totalToRead =
1 + // read the number of triangle vertices
mNumTriangleVertices * ( 2 + 1); // each vertex, read x, y, and 1
// color
mTriangleVertices = new Vector3D*[ mNumTriangleVertices ];
mTriangleVertexFillColors = new Color*[ mNumTriangleVertices ];
// read coordinates and colors for each vertex
for( int i=0; i<mNumTriangleVertices; i++ ) {
double x, y;
totalNumRead += fscanf( inFILE, "%lf", &x );
totalNumRead += fscanf( inFILE, "%lf", &y );
mTriangleVertices[i] = new Vector3D( x, y, 0 );
mTriangleVertexFillColors[i] = readColorFromFile( inFILE );
if( mTriangleVertexFillColors[i] != NULL ) {
totalNumRead += 1;
}
else {
// fill with dummy color
mTriangleVertexFillColors[i] = new Color( 1, 1, 1, 1, false );
}
}
mNumBorderVertices = 0;
totalNumRead += fscanf( inFILE, "%d", &mNumBorderVertices );
totalToRead +=
1 + // read the number of border vertices
mNumBorderVertices * ( 2 + 1); // each border vertex, read x, y,
// and 1 color
mBorderVertices = new Vector3D*[ mNumBorderVertices ];
mBorderVertexColors = new Color*[ mNumBorderVertices ];
// read coordinates and colors for each vertex
for( int i=0; i<mNumBorderVertices; i++ ) {
double x, y;
totalNumRead += fscanf( inFILE, "%lf", &x );
totalNumRead += fscanf( inFILE, "%lf", &y );
mBorderVertices[i] = new Vector3D( x, y, 0 );
mBorderVertexColors[i] = readColorFromFile( inFILE );
if( mBorderVertexColors[i] != NULL ) {
totalNumRead += 1;
}
else {
// fill with dummy color
mBorderVertexColors[i] = new Color( 1, 1, 1, 1, false );
}
}
totalToRead += 5; // border width, rotated copies, scale factor, angle
// scale factor, and rotation rate
totalNumRead += fscanf( inFILE, "%lf", &mBorderWidth );
totalNumRead += fscanf( inFILE, "%lf", &mNumRotatedCopies );
totalNumRead += fscanf( inFILE, "%lf", &mRotatedCopyScaleFactor );
totalNumRead += fscanf( inFILE, "%lf", &mRotatedCopyAngleScaleFactor );
totalNumRead += fscanf( inFILE, "%lf", &mRotationRate );
if( totalNumRead != totalToRead ) {
printf( "Expecting to read %d values, but read %d\n",
totalToRead, totalNumRead );
*outError = true;
}
else {
*outError = false;
}
}
ObjectParameterSpaceControlPoint::~ObjectParameterSpaceControlPoint() {
int i;
for( i=0; i<mNumTriangleVertices; i++ ) {
delete mTriangleVertices[i];
delete mTriangleVertexFillColors[i];
}
for( i=0; i<mNumBorderVertices; i++ ) {
delete mBorderVertices[i];
delete mBorderVertexColors[i];
}
delete [] mTriangleVertices;
delete [] mTriangleVertexFillColors;
delete [] mBorderVertices;
delete [] mBorderVertexColors;
}
void ObjectParameterSpaceControlPoint::writeToFile( FILE *inFILE ) {
fprintf( inFILE, "%d\n\n", mNumTriangleVertices );
// write coordinates and colors for each vertex
for( int i=0; i<mNumTriangleVertices; i++ ) {
fprintf( inFILE, "%f ", mTriangleVertices[i]->mX );
fprintf( inFILE, "%f\n", mTriangleVertices[i]->mY );
writeColorToFile( inFILE, mTriangleVertexFillColors[i] );
}
fprintf( inFILE, "\n" );
fprintf( inFILE, "%d\n\n", mNumBorderVertices );
// read coordinates and colors for each vertex
for( int i=0; i<mNumBorderVertices; i++ ) {
fprintf( inFILE, "%f ", mBorderVertices[i]->mX );
fprintf( inFILE, "%f\n", mBorderVertices[i]->mY );
writeColorToFile( inFILE, mBorderVertexColors[i] );
}
fprintf( inFILE, "\n" );
fprintf( inFILE, "%f\n", mBorderWidth );
fprintf( inFILE, "%f\n", mNumRotatedCopies );
fprintf( inFILE, "%f\n", mRotatedCopyScaleFactor );
fprintf( inFILE, "%f\n", mRotatedCopyAngleScaleFactor );
fprintf( inFILE, "%f\n", mRotationRate );
}
ParameterSpaceControlPoint *ObjectParameterSpaceControlPoint::copy() {
return new ObjectParameterSpaceControlPoint (
mNumTriangleVertices,
duplicateVertextArray( mTriangleVertices, mNumTriangleVertices ),
duplicateColorArray( mTriangleVertexFillColors, mNumTriangleVertices ),
mNumBorderVertices,
duplicateVertextArray( mBorderVertices, mNumBorderVertices ),
duplicateColorArray( mBorderVertexColors, mNumBorderVertices ),
mBorderWidth,
mNumRotatedCopies, mRotatedCopyScaleFactor,
mRotatedCopyAngleScaleFactor,
mRotationRate );
}
ParameterSpaceControlPoint *
ObjectParameterSpaceControlPoint::createLinearBlend(
ParameterSpaceControlPoint *inOtherPoint,
double inWeightOfOtherPoint ) {
// cast
ObjectParameterSpaceControlPoint *otherPoint =
(ObjectParameterSpaceControlPoint*)inOtherPoint;
// skip blending if we are at either end of our range
if( inWeightOfOtherPoint == 0 ) {
return this->copy();
}
else if( inWeightOfOtherPoint == 1 ) {
return otherPoint->copy();
}
double weightOfThisPoint = 1 - inWeightOfOtherPoint;
int numBlendTriangleVertices;
Vector3D **blendTriangleVertices =
blendVertexArrays( mTriangleVertices,
mNumTriangleVertices,
weightOfThisPoint,
otherPoint->mTriangleVertices,
otherPoint->mNumTriangleVertices,
&numBlendTriangleVertices );
int numBlendTriangleVertexFillColors;
Color **blendTriangleVertexFillColors =
blendColorArrays( mTriangleVertexFillColors,
mNumTriangleVertices,
weightOfThisPoint,
otherPoint->mTriangleVertexFillColors,
otherPoint->mNumTriangleVertices,
&numBlendTriangleVertexFillColors );
if( numBlendTriangleVertexFillColors != numBlendTriangleVertices ) {
printf( "Error in ObjectParameterSpaceControlPoint:\n"
" Number of blended triangle colors does not match number\n"
" of blended vertices.\n" );
}
int numBlendBorderVertices;
Vector3D **blendBorderVertices =
blendVertexArrays( mBorderVertices,
mNumBorderVertices,
weightOfThisPoint,
otherPoint->mBorderVertices,
otherPoint->mNumBorderVertices,
&numBlendBorderVertices );
int numBlendBorderVertexColors;
Color **blendBorderVertexColors =
blendColorArrays( mBorderVertexColors,
mNumBorderVertices,
weightOfThisPoint,
otherPoint->mBorderVertexColors,
otherPoint->mNumBorderVertices,
&numBlendBorderVertexColors );
if( numBlendBorderVertexColors != numBlendBorderVertices ) {
printf( "Error in ObjectParameterSpaceControlPoint:\n"
" Number of blended border colors does not match number of\n"
" blended vertices.\n" );
}
double blendBorderWidth =
inWeightOfOtherPoint * otherPoint->mBorderWidth +
weightOfThisPoint * mBorderWidth;
double blendNumRotatedCopies =
inWeightOfOtherPoint * otherPoint->mNumRotatedCopies +
weightOfThisPoint * mNumRotatedCopies;
double blendRotatedCopyScaleFactor =
inWeightOfOtherPoint * otherPoint->mRotatedCopyScaleFactor +
weightOfThisPoint * mRotatedCopyScaleFactor;
double blendRotatedCopyAngleScaleFactor =
inWeightOfOtherPoint * otherPoint->mRotatedCopyAngleScaleFactor +
weightOfThisPoint * mRotatedCopyAngleScaleFactor;
double blendRotationRate =
inWeightOfOtherPoint * otherPoint->mRotationRate +
weightOfThisPoint * mRotationRate;
return new ObjectParameterSpaceControlPoint(
numBlendTriangleVertices,
blendTriangleVertices,
blendTriangleVertexFillColors,
numBlendBorderVertices,
blendBorderVertices,
blendBorderVertexColors,
blendBorderWidth,
blendNumRotatedCopies,
blendRotatedCopyScaleFactor,
blendRotatedCopyAngleScaleFactor,
blendRotationRate );
}
SimpleVector<DrawableObject *> *
ObjectParameterSpaceControlPoint::getDrawableObjects() {
SimpleVector<DrawableObject *> *returnVector =
new SimpleVector<DrawableObject *>();
Angle3D *angleBetweenRotatedCopies =
new Angle3D( 0, 0,
2 * M_PI / ( mNumRotatedCopies + 1 ) );
angleBetweenRotatedCopies->scale( mRotatedCopyAngleScaleFactor );
// start out with a copy of the vertices and rotate them,
// bit by bit, to create each reflection
Vector3D **workingTriangleVertices =
duplicateVertextArray( mTriangleVertices,
mNumTriangleVertices );
Vector3D **workingBorderVertices =
duplicateVertextArray( mBorderVertices,
mNumBorderVertices );
// if we have a non-integral number of reflections, draw one
// extra reflection (it will overlap another of the reflections,
// but this eliminates reflection "pop-in" when transitioning between two
// control points that have a different number of reflections
int numRotatedCopiesToDraw = (int)ceil( mNumRotatedCopies );
char drawingExtraReflection = false;
if( numRotatedCopiesToDraw - mNumRotatedCopies > 0 ) {
drawingExtraReflection = true;
}
for( int s=0; s<=numRotatedCopiesToDraw; s++ ) {
Color **reflectedFillColors;
Color **reflectedBorderColors;
if( drawingExtraReflection && s == numRotatedCopiesToDraw ) {
// this is our extra reflection
// alpha-fade it in based on the fractional part of our number
// of reflections
double fractionalPart =
mNumRotatedCopies -
floor( mNumRotatedCopies );
reflectedFillColors =
duplicateColorArray( mTriangleVertexFillColors,
mNumTriangleVertices,
(float)fractionalPart );
reflectedBorderColors =
duplicateColorArray( mBorderVertexColors,
mNumBorderVertices,
(float)fractionalPart );
}
else {
// not an extra reflection
// do not alpha-fade it at all
reflectedFillColors = duplicateColorArray(
mTriangleVertexFillColors,
mNumTriangleVertices );
reflectedBorderColors = duplicateColorArray(
mBorderVertexColors,
mNumBorderVertices );
}
DrawableObject *reflectedObject =
new DrawableObject( mNumTriangleVertices,
duplicateVertextArray( workingTriangleVertices,
mNumTriangleVertices ),
reflectedFillColors,
mNumBorderVertices,
duplicateVertextArray( workingBorderVertices,
mNumBorderVertices ),
reflectedBorderColors,
mBorderWidth );
returnVector->push_back( reflectedObject );
// rotate the working vertices by one reflection angle
int i;
for( i=0; i<mNumTriangleVertices; i++ ) {
workingTriangleVertices[i]->rotate( angleBetweenRotatedCopies );
workingTriangleVertices[i]->scale( mRotatedCopyScaleFactor );
}
for( i=0; i<mNumBorderVertices; i++ ) {
workingBorderVertices[i]->rotate( angleBetweenRotatedCopies );
workingBorderVertices[i]->scale( mRotatedCopyScaleFactor );
}
}
// delete the working vertices
int i;
for( i=0; i<mNumTriangleVertices; i++ ) {
delete workingTriangleVertices[i];
}
delete [] workingTriangleVertices;
for( i=0; i<mNumBorderVertices; i++ ) {
delete workingBorderVertices[i];
}
delete [] workingBorderVertices;
delete angleBetweenRotatedCopies;
return returnVector;
}
double ObjectParameterSpaceControlPoint::getRotationRate() {
return mRotationRate;
}
Vector3D **ObjectParameterSpaceControlPoint::duplicateVertextArray(
Vector3D **inArray, int inNumTriangleVertices ) {
Vector3D **returnArray = new Vector3D*[ inNumTriangleVertices ];
for( int i=0; i<inNumTriangleVertices; i++ ) {
returnArray[i] = new Vector3D( inArray[i] );
}
return returnArray;
}
Color **ObjectParameterSpaceControlPoint::duplicateColorArray(
Color **inArray, int inNumColors, float inAlphaMultiplier ) {
Color **returnArray = new Color*[ inNumColors ];
for( int i=0; i<inNumColors; i++ ) {
returnArray[i] = inArray[i]->copy();
returnArray[i]->a *= inAlphaMultiplier;
}
return returnArray;
}
Color *ObjectParameterSpaceControlPoint::readColorFromFile( FILE *inFILE ) {
int numRead;
// try reading the red component to test if we have RGBA or a named color
float r, g, b, a;
Color *returnColor = NULL;
numRead = fscanf( inFILE, "%f", &r );
if( numRead == 1 ) {
// color present as RGBA components
numRead += fscanf( inFILE, "%f", &g );
numRead += fscanf( inFILE, "%f", &b );
numRead += fscanf( inFILE, "%f", &a );
if( numRead == 4 ) {
// read all 4
returnColor = new Color( r, g, b, a, false );
}
}
else {
// color might be a color name
char *colorName = new char[100];
numRead = fscanf( inFILE, "%99s", colorName );
if( numRead == 1 ) {
returnColor = NamedColorFactory::getColor( colorName );
}
delete [] colorName;
}
return returnColor;
}
void ObjectParameterSpaceControlPoint::writeColorToFile( FILE *inFILE,
Color *inColor ) {
fprintf( inFILE, "%f ", inColor->r );
fprintf( inFILE, "%f ", inColor->g );
fprintf( inFILE, "%f ", inColor->b );
fprintf( inFILE, "%f\n", inColor->a );
}
Color **ObjectParameterSpaceControlPoint::blendColorArrays(
Color **inFirstArray,
int inFirstArrayLength,
double inWeightFirstArray,
Color **inSecondArray,
int inSecondArrayLength,
int *outResultLength ) {
double weightOfSecondArray = 1 - inWeightFirstArray;
// blend has the same number of elements as the larger control array
int resultLength = inFirstArrayLength;
if( inSecondArrayLength > resultLength ) {
resultLength = inSecondArrayLength;
}
Color **blendColors = new Color*[ resultLength ];
// map the larger array nto the smaller array to
// blend
int sizeLargerArray;
int sizeSmallerArray;
Color **arrayWithMoreColors;
Color **arrayWithFewerColors;
double weightOfLargerSet;
double weightOfSmallerSet;
if( inFirstArrayLength > inSecondArrayLength ) {
sizeLargerArray = inFirstArrayLength;
sizeSmallerArray = inSecondArrayLength;
arrayWithMoreColors = inFirstArray;
arrayWithFewerColors = inSecondArray;
weightOfLargerSet = inWeightFirstArray;
weightOfSmallerSet = weightOfSecondArray;
}
else {
sizeLargerArray = inSecondArrayLength;
sizeSmallerArray = inFirstArrayLength;
arrayWithMoreColors = inSecondArray;
arrayWithFewerColors = inFirstArray;
weightOfLargerSet = weightOfSecondArray;
weightOfSmallerSet = inWeightFirstArray;
}
// size of blend array is same as size of larger set
// factor to map large array indices into the smaller array
double mapFactor =
(double)( sizeSmallerArray - 1 ) / (double)(sizeLargerArray - 1 );
for( int i=0; i<resultLength; i++ ) {
// find the index of our blend partner vertex in the smaller set
int partnerIndex =
(int)rint( i * mapFactor );
Color *largerSetColor =
arrayWithMoreColors[i];
Color *smallerSetColor = arrayWithFewerColors[ partnerIndex ];
blendColors[i] =
Color::linearSum( largerSetColor,
smallerSetColor,
weightOfLargerSet );
}
*outResultLength = resultLength;
return blendColors;
}
|