Package com.sun.j3d.loaders.lw3d

Source Code of com.sun.j3d.loaders.lw3d.LwsMotion$FrameHolder

/*     */ package com.sun.j3d.loaders.lw3d;
/*     */
/*     */ import com.sun.j3d.internal.J3dUtilsI18N;
/*     */ import com.sun.j3d.loaders.IncorrectFormatException;
/*     */ import com.sun.j3d.loaders.ParsingErrorException;
/*     */ import com.sun.j3d.utils.behaviors.interpolators.KBKeyFrame;
/*     */ import com.sun.j3d.utils.behaviors.interpolators.KBRotPosScaleSplinePathInterpolator;
/*     */ import java.io.StreamTokenizer;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Vector;
/*     */ import javax.media.j3d.Alpha;
/*     */ import javax.media.j3d.Behavior;
/*     */ import javax.media.j3d.BoundingSphere;
/*     */ import javax.media.j3d.Transform3D;
/*     */ import javax.media.j3d.TransformGroup;
/*     */ import javax.vecmath.Matrix4d;
/*     */ import javax.vecmath.Point3d;
/*     */ import javax.vecmath.Point3f;
/*     */ import javax.vecmath.Quat4f;
/*     */
/*     */ class LwsMotion extends TextfileParser
/*     */ {
/*     */   String motionName;
/*     */   LwsFrame[] frames;
/*     */   int numFrames;
/*     */   int numChannels;
/*     */   boolean loop;
/*     */   float totalTime;
/*     */   int firstFrame;
/*     */   int totalFrames;
/*     */   Behavior behaviors;
/*     */
/*     */   LwsMotion(StreamTokenizer st, int frames, float time)
/*     */   {
/*  84 */     this(st, 0, frames, time, 16);
/*     */   }
/*     */
/*     */   LwsMotion(StreamTokenizer st, int firstFrame, int frames, float time, int debugVals)
/*     */     throws ParsingErrorException, IncorrectFormatException
/*     */   {
/*  96 */     this.debugPrinter.setValidOutput(debugVals);
/*  97 */     this.numFrames = 0;
/*  98 */     this.totalTime = time;
/*  99 */     this.firstFrame = firstFrame;
/* 100 */     this.totalFrames = frames;
/* 101 */     debugOutputLn(8, "about to get motion name");
/* 102 */     this.motionName = getName(st);
/* 103 */     debugOutputLn(8, "about to get motion");
/* 104 */     getMotion(st);
/*     */   }
/*     */
/*     */   void getMotion(StreamTokenizer st)
/*     */     throws ParsingErrorException, IncorrectFormatException
/*     */   {
/* 115 */     debugOutputLn(1, "getMotion()");
/* 116 */     this.numChannels = ((int)getNumber(st));
/* 117 */     if (this.numChannels != 9) {
/* 118 */       throw new IncorrectFormatException(J3dUtilsI18N.getString("LwsMotion0"));
/*     */     }
/*     */
/* 121 */     debugOutputLn(8, "got channels");
/*     */
/* 123 */     this.numFrames = ((int)getNumber(st));
/* 124 */     this.frames = new LwsFrame[this.numFrames];
/* 125 */     debugOutputLn(2, "got frames" + this.numFrames);
/*     */
/* 127 */     for (int i = 0; i < this.numFrames; i++) {
/* 128 */       this.frames[i] = new LwsFrame(st);
/*     */     }
/*     */
/* 131 */     debugOutput(8, "got all frames");
/*     */
/* 133 */     getAndCheckString(st, "EndBehavior");
/* 134 */     int repeatVal = (int)getNumber(st);
/* 135 */     if (repeatVal == 1)
/* 136 */       this.loop = false;
/*     */     else
/* 138 */       this.loop = true;
/*     */   }
/*     */
/*     */   void fixFrames()
/*     */   {
/* 165 */     boolean addedFrames = false;
/* 166 */     Vector newFramesList = new Vector();
/* 167 */     double halfPI = 1.570796370506287D;
/* 168 */     LwsFrame finalFrame = null;
/*     */
/* 170 */     for (int i = 1; i < this.numFrames; i++)
/*     */     {
/* 172 */       LwsFrame lastFrame = this.frames[(i - 1)];
/* 173 */       LwsFrame thisFrame = this.frames[i];
/*     */
/* 176 */       finalFrame = thisFrame;
/* 177 */       newFramesList.add(lastFrame);
/*     */
/* 179 */       double largestAngleDifference = 0.0D;
/* 180 */       double thisAngle = thisFrame.getHeading();
/* 181 */       double lastAngle = lastFrame.getHeading();
/* 182 */       double angleDifference = Math.abs(thisAngle - lastAngle);
/* 183 */       if (angleDifference > largestAngleDifference) {
/* 184 */         largestAngleDifference = angleDifference;
/*     */       }
/* 186 */       thisAngle = thisFrame.getPitch();
/* 187 */       lastAngle = lastFrame.getPitch();
/* 188 */       angleDifference = Math.abs(thisAngle - lastAngle);
/* 189 */       if (angleDifference > largestAngleDifference) {
/* 190 */         largestAngleDifference = angleDifference;
/*     */       }
/* 192 */       thisAngle = thisFrame.getBank();
/* 193 */       lastAngle = lastFrame.getBank();
/* 194 */       angleDifference = Math.abs(thisAngle - lastAngle);
/* 195 */       if (angleDifference > largestAngleDifference) {
/* 196 */         largestAngleDifference = angleDifference;
/*     */       }
/* 198 */       if (largestAngleDifference > halfPI)
/*     */       {
/* 200 */         addedFrames = true;
/* 201 */         int numNewFrames = (int)(largestAngleDifference / halfPI);
/* 202 */         double increment = 1.0D / (numNewFrames + 1);
/* 203 */         double currentRatio = increment;
/*     */
/* 205 */         double totalf = this.frames[(this.numFrames - 1)].getFrameNum();
/* 206 */         double tlength = (thisFrame.getFrameNum() - lastFrame.getFrameNum()) / totalf;
/*     */         double adj0;
/*     */         LwsFrame prevFrame;
/*     */         double adj0;
/* 212 */         if (i - 1 < 1) {
/* 213 */           LwsFrame prevFrame = this.frames[(i - 1)];
/* 214 */           adj0 = 0.0D;
/*     */         } else {
/* 216 */           prevFrame = this.frames[(i - 2)];
/* 217 */           adj0 = tlength / ((thisFrame.getFrameNum() - prevFrame.getFrameNum()) / totalf);
/*     */         }
/*     */         double adj1;
/*     */         LwsFrame nextFrame;
/*     */         double adj1;
/* 221 */         if (i + 1 < this.numFrames) {
/* 222 */           LwsFrame nextFrame = this.frames[(i + 1)];
/* 223 */           adj1 = tlength / ((nextFrame.getFrameNum() - lastFrame.getFrameNum()) / totalf);
/*     */         }
/*     */         else {
/* 226 */           nextFrame = this.frames[i];
/* 227 */           adj1 = 1.0D;
/*     */         }
/*     */
/* 230 */         for (int j = 0; j < numNewFrames; j++)
/*     */         {
/*     */           LwsFrame newFrame;
/*     */           LwsFrame newFrame;
/* 235 */           if (thisFrame.linearValue == 1) {
/* 236 */             newFrame = new LwsFrame(lastFrame, thisFrame, currentRatio);
/*     */           }
/*     */           else
/*     */           {
/* 241 */             newFrame = new LwsFrame(prevFrame, lastFrame, thisFrame, nextFrame, currentRatio, adj0, adj1);
/*     */           }
/*     */
/* 246 */           currentRatio += increment;
/* 247 */           newFramesList.add(newFrame);
/*     */         }
/*     */       }
/*     */
/*     */     }
/*     */
/* 253 */     if (finalFrame != null)
/* 254 */       newFramesList.add(finalFrame);
/* 255 */     if (addedFrames)
/*     */     {
/* 258 */       LwsFrame[] newFrames = new LwsFrame[newFramesList.size()];
/* 259 */       Enumeration elements = newFramesList.elements();
/* 260 */       int index = 0;
/* 261 */       while (elements.hasMoreElements()) {
/* 262 */         newFrames[(index++)] = ((LwsFrame)elements.nextElement());
/*     */       }
/* 264 */       this.frames = newFrames;
/* 265 */       this.numFrames = this.frames.length;
/* 266 */       for (int i = 0; i < this.numFrames; i++) {
/* 267 */         debugOutputLn(2, "frame " + i + " = " + this.frames[i]);
/* 268 */         this.frames[i].printVals();
/*     */       }
/*     */     }
/*     */   }
/*     */
/*     */   int intMod(int divisee, int divisor)
/*     */   {
/* 277 */     int tmpDiv = divisee;
/* 278 */     int tmpDivisor = divisor;
/* 279 */     if (tmpDiv < 0)
/* 280 */       tmpDiv = -tmpDiv;
/* 281 */     if (tmpDivisor < 0)
/* 282 */       tmpDivisor = -tmpDivisor;
/* 283 */     while (tmpDiv > tmpDivisor) {
/* 284 */       tmpDiv -= tmpDivisor;
/*     */     }
/* 286 */     return tmpDiv;
/*     */   }
/*     */
/*     */   void playWithFrameTimes(Vector framesVector)
/*     */   {
/* 310 */     debugOutputLn(1, "playWithFrameTimes: firstFrame = " + this.firstFrame);
/*     */
/* 312 */     if (this.firstFrame == 1) {
/* 313 */       return;
/*     */     }
/* 315 */     if (this.frames[(this.numFrames - 1)].getFrameNum() < this.totalFrames)
/*     */     {
/* 319 */       int motionLastFrame = (int)(this.frames[(this.numFrames - 1)].getFrameNum() + 0.4999999D);
/*     */
/* 321 */       int newFirstFrame = intMod(this.firstFrame, motionLastFrame);
/* 322 */       int newLastFrame = intMod(this.totalFrames, motionLastFrame);
/* 323 */       int index = 0;
/* 324 */       while ((index < this.numFrames) &&
/* 325 */         (this.frames[index].getFrameNum() < newFirstFrame))
/*     */       {
/* 327 */         index++;
/*     */       }
/* 329 */       int startIndex = index;
/* 330 */       if ((this.frames[startIndex].getFrameNum() > this.firstFrame) && (startIndex > 0))
/*     */       {
/* 332 */         startIndex--;
/* 333 */       }index = startIndex;
/* 334 */       if (newFirstFrame < newLastFrame)
/*     */       {
/* 336 */         while ((index < this.numFrames) && (this.frames[index].getFrameNum() <= newLastFrame)) {
/* 337 */           FrameHolder frameHolder = new FrameHolder(this.frames[index], this.frames[index].getFrameNum() - newFirstFrame);
/*     */
/* 341 */           framesVector.addElement(frameHolder);
/* 342 */           index++;
/*     */         }
/*     */       }
/*     */
/* 346 */       double currentNewFrameNumber = -1.0D;
/* 347 */       while (index < this.numFrames) {
/* 348 */         currentNewFrameNumber = this.frames[index].getFrameNum() - newFirstFrame;
/*     */
/* 350 */         FrameHolder frameHolder = new FrameHolder(this.frames[index], currentNewFrameNumber);
/*     */
/* 353 */         framesVector.addElement(frameHolder);
/* 354 */         index++;
/*     */       }
/* 356 */       index = 0;
/*     */
/* 358 */       while ((index <= startIndex) && (this.frames[index].getFrameNum() <= newLastFrame)) {
/* 359 */         if (index == 0) {
/* 360 */           LwsFrame newFrame = new LwsFrame(this.frames[index], this.frames[(index + 1)], 1.0D / (this.frames[(index + 1)].getFrameNum() - this.frames[index].getFrameNum()));
/*     */
/* 365 */           FrameHolder frameHolder = new FrameHolder(newFrame, newFrame.getFrameNum() + currentNewFrameNumber);
/*     */
/* 369 */           framesVector.addElement(frameHolder);
/*     */         }
/*     */         else {
/* 372 */           FrameHolder frameHolder = new FrameHolder(this.frames[index], this.frames[index].getFrameNum() + currentNewFrameNumber);
/*     */
/* 376 */           framesVector.addElement(frameHolder);
/*     */         }
/* 378 */         index++;
/*     */       }
/*     */     }
/*     */     else
/*     */     {
/* 383 */       int index = 0;
/* 384 */       while ((index < this.numFrames) &&
/* 385 */         (this.frames[index].getFrameNum() < this.firstFrame))
/*     */       {
/* 387 */         index++;
/*     */       }
/* 389 */       int startIndex = index;
/* 390 */       if ((this.frames[startIndex].getFrameNum() > this.firstFrame) && (startIndex > 0))
/*     */       {
/* 393 */         double ratio = this.firstFrame / (this.frames[startIndex].getFrameNum() - this.frames[(startIndex - 1)].getFrameNum());
/*     */
/* 396 */         LwsFrame newFrame = new LwsFrame(this.frames[(startIndex - 1)], this.frames[startIndex], ratio);
/*     */
/* 399 */         FrameHolder frameHolder = new FrameHolder(newFrame, newFrame.getFrameNum() - this.firstFrame);
/*     */
/* 402 */         framesVector.addElement(frameHolder);
/*     */       }
/* 404 */       index = startIndex;
/*     */
/* 406 */       while ((index < this.numFrames) && (this.frames[index].getFrameNum() <= this.totalFrames)) {
/* 407 */         FrameHolder frameHolder = new FrameHolder(this.frames[index], this.frames[index].getFrameNum() - this.firstFrame);
/*     */
/* 411 */         framesVector.addElement(frameHolder);
/* 412 */         index++;
/*     */       }
/* 414 */       if (this.frames[(index - 1)].getFrameNum() < this.totalFrames)
/*     */       {
/* 416 */         double ratio = (this.totalFrames - this.frames[(index - 1)].getFrameNum()) / (this.frames[index].getFrameNum() - this.frames[(index - 1)].getFrameNum());
/*     */
/* 420 */         LwsFrame newFrame = new LwsFrame(this.frames[(index - 1)], this.frames[index], ratio);
/*     */
/* 423 */         FrameHolder frameHolder = new FrameHolder(newFrame, this.totalFrames - this.firstFrame);
/*     */
/* 425 */         framesVector.addElement(frameHolder);
/*     */       }
/*     */     }
/*     */   }
/*     */
/*     */   void createJava3dBehaviorsForFramesSubset(TransformGroup target)
/*     */   {
/* 438 */     debugOutputLn(1, "createJava3dBehaviorsForFramesSubset");
/* 439 */     Vector frameHolders = new Vector();
/* 440 */     playWithFrameTimes(frameHolders);
/* 441 */     long alphaAtOne = 0L;
/*     */     int loopCount;
/* 445 */     if (this.loop)
/* 446 */       loopCount = -1;
/*     */     else
/* 448 */       loopCount = 1;
/* 449 */     int loopCount = -1;
/*     */
/* 451 */     int numFrames = frameHolders.size();
/*     */
/* 453 */     debugOutputLn(2, "totalTime = " + this.totalTime);
/* 454 */     debugOutputLn(2, "loopCount = " + loopCount);
/*     */
/* 456 */     FrameHolder lastFrameHolder = (FrameHolder)frameHolders.elementAt(frameHolders.size() - 1);
/*     */
/* 458 */     LwsFrame lastFrame = lastFrameHolder.frame;
/* 459 */     float animTime = 1000.0F * this.totalTime * (float)(lastFrameHolder.frameNumber / (this.totalFrames - this.firstFrame));
/*     */
/* 462 */     debugOutputLn(2, " anim time: " + animTime);
/* 463 */     debugOutputLn(2, " totalFrames = " + this.totalFrames);
/*     */
/* 465 */     if (!this.loop)
/* 466 */       alphaAtOne = ()(1000.0D * this.totalTime - animTime);
/* 467 */     Alpha theAlpha = new Alpha(loopCount, 1, 0L, 0L, ()animTime, 0L, alphaAtOne, 0L, 0L, 0L);
/*     */
/* 472 */     float[] knots = new float[numFrames];
/* 473 */     Point3f[] positions = new Point3f[numFrames];
/* 474 */     Quat4f[] quats = new Quat4f[numFrames];
/* 475 */     Point3f[] scales = new Point3f[numFrames];
/* 476 */     Transform3D yAxis = new Transform3D();
/* 477 */     Matrix4d mat = new Matrix4d();
/* 478 */     KBKeyFrame[] keyFrames = new KBKeyFrame[numFrames];
/*     */
/* 480 */     for (int i = 0; i < numFrames; i++)
/*     */     {
/* 482 */       FrameHolder frameHolder = (FrameHolder)frameHolders.elementAt(i);
/* 483 */       LwsFrame frame = frameHolder.frame;
/*     */
/* 486 */       positions[i] = frame.getPosition();
/*     */
/* 493 */       scales[i] = frame.getScale();
/*     */
/* 496 */       frame.setRotationMatrix(mat);
/* 497 */       debugOutputLn(2, "LwsMotion::createj3dbeh, mat = " + mat);
/* 498 */       quats[i] = new Quat4f();
/* 499 */       quats[i].set(mat);
/* 500 */       debugOutputLn(2, " and quat = " + quats[i]);
/*     */
/* 503 */       if (i == 0)
/* 504 */         knots[i] = 0.0F;
/*     */       else {
/* 506 */         knots[i] = ((float)frameHolder.frameNumber / (float)lastFrameHolder.frameNumber);
/*     */       }
/*     */
/* 510 */       keyFrames[i] = new KBKeyFrame(knots[i], frame.linearValue, positions[i], (float)frame.heading, (float)frame.pitch, (float)frame.bank, scales[i], (float)frame.tension, (float)frame.continuity, (float)frame.bias);
/*     */
/* 520 */       debugOutputLn(2, "pos, knots, quat = " + positions[i] + knots[i] + quats[i]);
/*     */     }
/*     */
/* 525 */     KBRotPosScaleSplinePathInterpolator b = new KBRotPosScaleSplinePathInterpolator(theAlpha, target, yAxis, keyFrames);
/*     */
/* 530 */     if (b != null) {
/* 531 */       this.behaviors = b;
/* 532 */       BoundingSphere bounds = new BoundingSphere(new Point3d(0.0D, 0.0D, 0.0D), 1000000.0D);
/*     */
/* 534 */       b.setSchedulingBounds(bounds);
/* 535 */       target.setCapability(18);
/* 536 */       target.addChild(this.behaviors);
/*     */     }
/*     */   }
/*     */
/*     */   void createJava3dBehaviors(TransformGroup target)
/*     */   {
/* 547 */     if (this.numFrames <= 1) {
/* 548 */       this.behaviors = null;
/*     */     } else {
/* 550 */       if (this.firstFrame > 1) {
/* 551 */         createJava3dBehaviorsForFramesSubset(target);
/* 552 */         return;
/*     */       }
/*     */
/* 555 */       long alphaAtOne = 0L;
/*     */       int loopCount;
/* 559 */       if (this.loop)
/* 560 */         loopCount = -1;
/*     */       else
/* 562 */         loopCount = 1;
/* 563 */       int loopCount = -1;
/*     */
/* 565 */       debugOutputLn(2, "totalTime = " + this.totalTime);
/* 566 */       debugOutputLn(2, "loopCount = " + loopCount);
/*     */
/* 568 */       float animTime = 1000.0F * this.totalTime * (float)(this.frames[(this.numFrames - 1)].getFrameNum() / this.totalFrames);
/*     */
/* 571 */       debugOutputLn(2, " anim time: " + animTime);
/* 572 */       debugOutputLn(2, " totalFrames = " + this.totalFrames);
/* 573 */       debugOutputLn(2, " lastFrame = " + this.frames[(this.numFrames - 1)].getFrameNum());
/*     */
/* 576 */       if (!this.loop)
/* 577 */         alphaAtOne = ()(1000.0D * this.totalTime - animTime);
/* 578 */       Alpha theAlpha = new Alpha(loopCount, 1, 0L, 0L, ()animTime, 0L, alphaAtOne, 0L, 0L, 0L);
/*     */
/* 583 */       float[] knots = new float[this.numFrames];
/* 584 */       Point3f[] positions = new Point3f[this.numFrames];
/* 585 */       Quat4f[] quats = new Quat4f[this.numFrames];
/* 586 */       Point3f[] scales = new Point3f[this.numFrames];
/* 587 */       Transform3D yAxis = new Transform3D();
/* 588 */       Matrix4d mat = new Matrix4d();
/* 589 */       KBKeyFrame[] keyFrames = new KBKeyFrame[this.numFrames];
/*     */
/* 591 */       for (int i = 0; i < this.numFrames; i++)
/*     */       {
/* 594 */         positions[i] = this.frames[i].getPosition();
/*     */
/* 601 */         scales[i] = this.frames[i].getScale();
/*     */
/* 604 */         this.frames[i].setRotationMatrix(mat);
/* 605 */         debugOutputLn(2, "LwsMotion::createj3dbeh, mat = " + mat);
/* 606 */         quats[i] = new Quat4f();
/* 607 */         quats[i].set(mat);
/* 608 */         debugOutputLn(2, " and quat = " + quats[i]);
/*     */
/* 611 */         if (i == 0)
/* 612 */           knots[i] = 0.0F;
/*     */         else {
/* 614 */           knots[i] = ((float)this.frames[i].getFrameNum() / (float)this.frames[(this.numFrames - 1)].getFrameNum());
/*     */         }
/*     */
/* 618 */         keyFrames[i] = new KBKeyFrame(knots[i], this.frames[i].linearValue, positions[i], (float)this.frames[i].heading, (float)this.frames[i].pitch, (float)this.frames[i].bank, scales[i], (float)this.frames[i].tension, (float)this.frames[i].continuity, (float)this.frames[i].bias);
/*     */
/* 629 */         debugOutputLn(2, "pos, knots, quat = " + positions[i] + knots[i] + quats[i]);
/*     */       }
/*     */
/* 634 */       KBRotPosScaleSplinePathInterpolator b = new KBRotPosScaleSplinePathInterpolator(theAlpha, target, yAxis, keyFrames);
/*     */
/* 639 */       if (b != null) {
/* 640 */         this.behaviors = b;
/* 641 */         BoundingSphere bounds = new BoundingSphere(new Point3d(0.0D, 0.0D, 0.0D), 1000000.0D);
/*     */
/* 643 */         b.setSchedulingBounds(bounds);
/* 644 */         target.setCapability(18);
/* 645 */         target.addChild(this.behaviors);
/*     */       }
/*     */     }
/*     */   }
/*     */
/*     */   Behavior getBehaviors()
/*     */   {
/* 655 */     return this.behaviors;
/*     */   }
/*     */
/*     */   LwsFrame getFirstFrame()
/*     */   {
/* 663 */     if (this.numFrames > 0) {
/* 664 */       return this.frames[0];
/*     */     }
/* 666 */     return null;
/*     */   }
/*     */
/*     */   void printVals()
/*     */   {
/* 673 */     debugOutputLn(2, "   motionName = " + this.motionName);
/* 674 */     debugOutputLn(2, "   numChannels = " + this.numChannels);
/* 675 */     debugOutputLn(2, "   numFrames = " + this.numFrames);
/* 676 */     debugOutputLn(2, "   loop = " + this.loop);
/* 677 */     for (int i = 0; i < this.numFrames; i++) {
/* 678 */       debugOutputLn(2, "       FRAME " + i);
/* 679 */       this.frames[i].printVals();
/*     */     }
/*     */   }
/*     */
/*     */   class FrameHolder
/*     */   {
/*     */     double frameNumber;
/*     */     LwsFrame frame;
/*     */
/*     */     FrameHolder(LwsFrame theFrame, double number)
/*     */     {
/* 298 */       this.frame = theFrame;
/* 299 */       this.frameNumber = number;
/*     */     }
/*     */   }
/*     */ }

/* Location:           Z:\System\Library\Java\Extensions\j3dutils.jar
* Qualified Name:     com.sun.j3d.loaders.lw3d.LwsMotion
* JD-Core Version:    0.6.2
*/
TOP

Related Classes of com.sun.j3d.loaders.lw3d.LwsMotion$FrameHolder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.