Text archives Help
- From: thiago@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r1318 - in trunk: Model/MiscObjects Model/Readers scenes
- Date: Tue, 27 Mar 2007 17:57:58 -0700 (MST)
Author: thiago
Date: Tue Mar 27 17:57:58 2007
New Revision: 1318
Added:
trunk/Model/MiscObjects/KeyFrameAnimation.cc
trunk/Model/MiscObjects/KeyFrameAnimation.h
Modified:
trunk/Model/MiscObjects/CMakeLists.txt
trunk/Model/Readers/PlyReader.cc
trunk/scenes/primtest.cc
Log:
Added a key frame animation class so that manta can do dynamic scenes
scenes/primtest.cc:
Added example dynamic scene.
Model/Readers/PlyReader.cc:
Fixed bug where only one ply file could be successfully loaded due
to not resetting global variables.
Model/MiscObjects/KeyFrameAnimation.cc
Model/MiscObjects/KeyFrameAnimation.h:
Currently this only does non-interpolated key framed
animations. This appears to be working correctly (ignoring
interpolated animations). Note that I'm not sure if this class
should exist in this directory, so feel free to move it somewhere
else if that makes more sense. See Model/Groups/private/CGT and the
primtest scene for an example of how to use it.
Model/MiscObjects/CMakeLists.txt:
Added KeyFrameAnimation class.
Modified: trunk/Model/MiscObjects/CMakeLists.txt
==============================================================================
--- trunk/Model/MiscObjects/CMakeLists.txt (original)
+++ trunk/Model/MiscObjects/CMakeLists.txt Tue Mar 27 17:57:58 2007
@@ -6,4 +6,6 @@
MiscObjects/Difference.cc
MiscObjects/Intersection.h
MiscObjects/Intersection.cc
+ MiscObjects/KeyFrameAnimation.h
+ MiscObjects/KeyFrameAnimation.cc
)
Added: trunk/Model/MiscObjects/KeyFrameAnimation.cc
==============================================================================
--- (empty file)
+++ trunk/Model/MiscObjects/KeyFrameAnimation.cc Tue Mar 27 17:57:58
2007
@@ -0,0 +1,124 @@
+#include "KeyFrameAnimation.h"
+#include <SCIRun/Core/Thread/Time.h>
+#include <cmath>
+
+using namespace Manta;
+using namespace std;
+
+KeyFrameAnimation::KeyFrameAnimation(InterpolationMode interpolation) :
+ interpolation(interpolation), currGroup(NULL), spareGroup(NULL),
+ duration(1), currTime(0), paused(false), barrier("keyframe animation
barrier")
+{
+}
+
+KeyFrameAnimation::~KeyFrameAnimation()
+{
+ delete spareGroup;
+}
+
+void KeyFrameAnimation::setInterpolation(InterpolationMode mode)
+{
+ interpolation = mode;
+}
+
+void KeyFrameAnimation::startAnimation()
+{
+ startTime = SCIRun::Time::currentSeconds();
+ paused = false;
+ currGroup = NULL;
+}
+
+void KeyFrameAnimation::pauseAnimation()
+{
+ if (!paused)
+ pauseTime = SCIRun::Time::currentSeconds();
+ paused = true;
+}
+
+void KeyFrameAnimation::resumeAnimation()
+{
+ if (paused)
+ startTime += SCIRun::Time::currentSeconds() - pauseTime;
+ paused = false;
+}
+
+bool KeyFrameAnimation::getLatestFrame(Group *&group, int proc, int numProcs)
+{
+ float newTime = SCIRun::Time::currentSeconds() - startTime;
+ bool differentFrame = setTime(newTime, proc, numProcs);
+ getCurrGroup(group);
+ return differentFrame;
+}
+
+bool KeyFrameAnimation::setTime(float time, int proc, int numProcs)
+{
+ if (frames.empty())
+ return true;
+
+ if (proc == 0) {
+ differentFrame = isDifferentFrame(time);
+
+ //let's assume the animation wraps at the end
+ //first we need to handle negative times
+ time += static_cast<int>(fabs(time)/duration+1) * duration;
+ //now we can safely do the wrapping
+ time = fmodf(time, duration);
+ currTime = time;
+ }
+
+ //need to make sure proc 0 has updated the currTime.
+ //a barrier is a little heavy for this, but it's cleaner code (and I'm
lazy).
+ barrier.wait(numProcs);
+
+ if (!differentFrame)
+ return false;
+
+ float frame = currTime/duration * frames.size();
+
+ if (interpolation == linear) {
+ //do interpolation in parallel
+ //TODO: Do the actual linear interpolation!
+ }
+ else if (interpolation == truncate) {
+ currGroup = frames[(int) frame];
+ }
+ return true;
+}
+
+bool KeyFrameAnimation::isDifferentFrame(float time)
+{
+ if (currGroup == NULL) return true;
+
+ //let's assume the animation wraps at the end
+ //first we need to handle negative times
+ time += static_cast<int>(fabs(time)/duration+1) * duration;
+ //now we can safely do the wrapping
+ time = fmodf(time, duration);
+
+ if (interpolation == linear)
+ return time != currTime;
+
+ if (interpolation == truncate) {
+ int i = time/duration * frames.size();
+ int j = currTime/duration * frames.size();
+ return i != j;
+ }
+
+ else //should never get here
+ return true;
+}
+
+void KeyFrameAnimation::preprocess(const PreprocessContext& context)
+{
+ for (int i=0; i < frames.size(); ++i) {
+ frames[i]->preprocess(context);
+ }
+}
+
+void KeyFrameAnimation::computeBounds(const PreprocessContext& context,
BBox& bbox) const
+{
+ if (currGroup)
+ currGroup->computeBounds(context, bbox);
+ else if (!frames.empty())
+ frames[0]->computeBounds(context, bbox);
+}
Added: trunk/Model/MiscObjects/KeyFrameAnimation.h
==============================================================================
--- (empty file)
+++ trunk/Model/MiscObjects/KeyFrameAnimation.h Tue Mar 27 17:57:58 2007
@@ -0,0 +1,74 @@
+#ifndef KeyFrameAnimation_h
+#define KeyFrameAnimation_h
+
+#include <Model/Groups/Group.h>
+#include <SCIRun/Core/Thread/Barrier.h>
+
+namespace Manta {
+ class KeyFrameAnimation{
+ public:
+ enum InterpolationMode{truncate, linear};
+
+ KeyFrameAnimation(InterpolationMode interpolation=truncate);
+ ~KeyFrameAnimation();
+
+ void push_back(Group *objects) {
+ frames.push_back(objects);
+ }
+
+ //number of seconds the animation takes from start to end
+ void setDuration(float time) { duration = time; };
+
+ void startAnimation();
+ void pauseAnimation();
+ void resumeAnimation();
+
+ //set animation to a specific time (frame)
+ //returns whether the frame is different from the previous frame.
+ //This will block waiting for all numProcs.
+ bool setTime(float time, int proc, int numProcs);
+
+ //get the time of the frame currently being worked on.
+ float getTime() { return currTime; }
+
+ //update to latest time and get the Group for that frame.
+ //returns whether the frame is different from the previous frame.
+ //This will block waiting for all numProcs.
+ bool getLatestFrame(Group *&group, int proc, int numProcs);
+
+ //get the group for the frame currently being worked on.
+ void getCurrGroup(Group *&group) {
+ group = currGroup;
+ }
+
+ bool isDifferentFrame(float time);
+
+ void setInterpolation(InterpolationMode mode);
+
+ void preprocess(const PreprocessContext& context);
+
+ void computeBounds(const PreprocessContext& context, BBox& bbox) const;
+
+ private:
+ InterpolationMode interpolation;
+
+ Group *spareGroup; //used for interpolated frame -- has already
allocated memory.
+ Group *currGroup; //The group for the frame specified by setTime()
+
+ //The time associated with the current frame being rendered.
+ //Make sure this is from [0, duration).
+ float currTime;
+
+ double startTime; //world time of animation start
+ double pauseTime;
+
+ bool paused;
+
+ SCIRun::Barrier barrier;
+ bool differentFrame; //used by setTime()
+
+ vector<Group*> frames;
+ float duration; //how many seconds long
+ };
+}
+#endif
Modified: trunk/Model/Readers/PlyReader.cc
==============================================================================
--- trunk/Model/Readers/PlyReader.cc (original)
+++ trunk/Model/Readers/PlyReader.cc Tue Mar 27 17:57:58 2007
@@ -145,6 +145,7 @@
nVertices = ply_set_read_cb(ply, "vertex", "x", vertex_cb,
NULL, 0);
+ vertices.clear();
vertices.reserve(nVertices);
ply_set_read_cb(ply, "vertex", "y", vertex_cb,
Modified: trunk/scenes/primtest.cc
==============================================================================
--- trunk/scenes/primtest.cc (original)
+++ trunk/scenes/primtest.cc Tue Mar 27 17:57:58 2007
@@ -13,6 +13,7 @@
#include <Model/MiscObjects/Difference.h>
#include <Model/MiscObjects/Intersection.h>
#include <Model/Groups/Group.h>
+#include <Model/Groups/DynBVH.h>
#include <Model/Lights/PointLight.h>
#include <Model/Materials/Checker.h>
#include <Model/Materials/Dielectric.h>
@@ -354,9 +355,14 @@
#ifdef USE_PRIVATE_CODE
bgpoly = false;
AffineTransform t;
- //t.initWithScale(Vector(100, 100, 100));
- t.initWithScale(Vector(1, 1, 1));
- delete group; group = new Grid();
+ t.initWithScale(Vector(100, 100, 100));
+ //t.initWithScale(Vector(1, 1, 1));
+
+ Grid *as= new Grid();
+ group->add(as);
+
+ //delete group; group = new DynBVH();
+
/*
ifstream in(modelName.c_str());
while (in) {
@@ -367,8 +373,33 @@
}
*/
- if (!readPlyFile(modelName, t, group, 0, matl))
+ Group *frame = new Group;
+ if (!readPlyFile(modelName, t, frame, 0, matl))
printf("error loading or reading ply file: %s\n", modelName.c_str());
//would be better to throw an error.
+
+ Group *frame2 = new Group;
+ string modelName2 =
"/usr/sci/data/Geometry/Stanford_Sculptures/bun_zipper_res2.ply";
+ if (!readPlyFile(modelName2, t, frame2, 0, matl))
+ printf("error loading or reading ply file: %s\n",
modelName2.c_str()); //would be better to throw an error.
+
+ Group *frame3 = new Group;
+ string modelName3 =
"/usr/sci/data/Geometry/Stanford_Sculptures/bun_zipper_res3.ply";
+ if (!readPlyFile(modelName3, t, frame3, 0, matl))
+ printf("error loading or reading ply file: %s\n",
modelName3.c_str()); //would be better to throw an error.
+
+ Group *frame4 = new Group;
+ string modelName4 =
"/usr/sci/data/Geometry/Stanford_Sculptures/bun_zipper_res4.ply";
+ if (!readPlyFile(modelName4, t, frame4, 0, matl))
+ printf("error loading or reading ply file: %s\n",
modelName4.c_str()); //would be better to throw an error.
+
+
+ as->animation.push_back(frame);
+ as->animation.push_back(frame2);
+ as->animation.push_back(frame3);
+ as->animation.push_back(frame4);
+
+ as->animation.setDuration(2);
+
#endif //USE_PRIVATE_CODE
} else if (primtype == "heightfield") {
- [MANTA] r1318 - in trunk: Model/MiscObjects Model/Readers scenes, thiago, 03/27/2007
Archive powered by MHonArc 2.6.16.