-
-
Notifications
You must be signed in to change notification settings - Fork 408
DRAFT PR: adding interpolation to QuakeMDL animations with vtkInterpolateDataSe… #2775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
The-Lennzer
wants to merge
2
commits into
f3d-app:master
Choose a base branch
from
The-Lennzer:feature/quake-mdl-interpolation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,9 @@ | |
| #include <vtkRenderer.h> | ||
| #include <vtkResourceStream.h> | ||
|
|
||
| #include <vtkInterpolateDataSetAttributes.h> | ||
| #include <vtkMathUtilities.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <cstring> | ||
|
|
||
|
|
@@ -631,6 +634,8 @@ struct vtkF3DQuakeMDLImporter::vtkInternals | |
| std::vector<std::vector<double>> GroupSkinDurations; | ||
|
|
||
| vtkIdType ActiveAnimation = -1; | ||
|
|
||
| vtkSmartPointer<vtkInterpolateDataSetAttributes> Interpolator; | ||
| }; | ||
|
|
||
| //---------------------------------------------------------------------------- | ||
|
|
@@ -677,6 +682,7 @@ void vtkF3DQuakeMDLImporter::ImportActors(vtkRenderer* renderer) | |
| #endif | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------- | ||
| //---------------------------------------------------------------------------- | ||
| bool vtkF3DQuakeMDLImporter::UpdateAtTimeValue(double timeValue) | ||
| { | ||
|
|
@@ -698,23 +704,106 @@ bool vtkF3DQuakeMDLImporter::UpdateAtTimeValue(double timeValue) | |
| const std::vector<double>& times = isMeshAnimation | ||
| ? this->Internals->AnimationTimes[animIndex] | ||
| : this->Internals->GroupSkinDurations[animIndex]; | ||
| const auto found = std::lower_bound(times.begin(), times.end() - 1, timeValue); | ||
| // If found at finish time of last frame, select last frame's start time (second last value), | ||
| // else select distance | ||
| const size_t i = | ||
| (found == times.end() - 1) ? times.size() - 2 : std::distance(times.begin(), found); | ||
| // If time at index i > timeValue, then choose the previous frame | ||
| const size_t frameIndex = times[i] > timeValue && i > 0 ? i - 1 : i; | ||
|
|
||
| // Need at least 2 time values | ||
| if (times.size() < 2) | ||
| { | ||
| return true; | ||
| } | ||
| // Clamping timeValue to the valid animation range | ||
| double clampedTime = std::max(times.front(), std::min(timeValue, times.back())); | ||
| // Find the frame interval containing our time | ||
| auto upperIt = std::lower_bound(times.begin(), times.end() - 1, clampedTime); | ||
| size_t upperIndex = std::distance(times.begin(), upperIt); | ||
|
|
||
| // Get the two frame indices we're interploating between | ||
| size_t frameIndex0, frameIndex1; | ||
| double t0, t1; | ||
|
|
||
| if (upperIndex == 0) | ||
| { | ||
| // Before or at the first frame | ||
| frameIndex0 = 0; | ||
| frameIndex1 = 0; | ||
| t0 = times[0]; | ||
| t1 = times[0]; | ||
| } | ||
| else if (upperIndex >= times.size() - 1) | ||
| { | ||
| // At or past the last frame | ||
| frameIndex0 = times.size() - 2; | ||
| frameIndex1 = times.size() - 2; | ||
| t0 = times[times.size() - 2]; | ||
| t1 = times[times.size() - 2]; | ||
| } | ||
| else | ||
| { | ||
| // between two frames | ||
| if (times[upperIndex] > clampedTime) | ||
| { | ||
| // Time falls between [upperIndex-1] and [upperIndex] | ||
| frameIndex0 = upperIndex - 1; | ||
| frameIndex1 = upperIndex; | ||
| } | ||
| else | ||
| { | ||
| // Time exactly at upperIndex or between [upperIndex] and [upperIndex+1] | ||
| frameIndex0 = upperIndex; | ||
| frameIndex1 = std::min(upperIndex + 1, times.size() - 2); | ||
| } | ||
|
|
||
| t0 = times[frameIndex0]; | ||
| t1 = times[frameIndex1]; | ||
| } | ||
|
|
||
| // Calculate interpolation factor (alpha) | ||
| double alpha = 0.0; | ||
| if (frameIndex0 != frameIndex1) | ||
| { | ||
| alpha = vtkMathUtilities::SafeDivision(clampedTime - t0, t1 - t0); | ||
| alpha = std::max(0.0, std::min(1.0, alpha)); | ||
| } | ||
|
|
||
| if (isMeshAnimation) | ||
| { | ||
| this->Internals->Mapper->SetInputData( | ||
| this->Internals->AnimationFrames[animIndex][frameIndex]); | ||
| if (frameIndex0 == frameIndex1) | ||
| { | ||
| // No interpolation needed: use frame0 | ||
| this->Internals->Mapper->SetInputData( | ||
| this->Internals->AnimationFrames[animIndex][frameIndex0]); | ||
| } | ||
| else | ||
| { | ||
| // Interpolate between frame0 and frame1 | ||
| // using <vtkInterpolateDataSetAttributes> | ||
| if (!this->Internals->Interpolator) | ||
| { | ||
| this->Internals->Interpolator = vtkSmartPointer<vtkInterpolateDataSetAttributes>::New(); | ||
| } | ||
|
|
||
| // Clear previous inputs and add new ones | ||
| this->Internals->Interpolator->RemoveAllInputs(); | ||
| this->Internals->Interpolator->AddInputData( | ||
| this->Internals->AnimationFrames[animIndex][frameIndex0]); | ||
| this->Internals->Interpolator->AddInputData( | ||
| this->Internals->AnimationFrames[animIndex][frameIndex1]); | ||
| this->Internals->Interpolator->SetT(alpha); | ||
| this->Internals->Interpolator->Update(); | ||
|
|
||
| // Get the interpolated mesh and set it to the mapper | ||
| vtkPolyData* interpolatedMesh = this->Internals->Interpolator->GetPolyDataOutput(); | ||
| this->Internals->Mapper->SetInputData(interpolatedMesh); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| this->Internals->Texture->SetInputData(this->Internals->GroupSkins[animIndex][frameIndex]); | ||
| // For texture animations, use nearest neighbor approach | ||
| // Not interpolating here since texture interpolation is complex(?) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes its fine and out of scope
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK 👍🏼 |
||
| size_t textureIndex = alpha < 0.5 ? frameIndex0 : frameIndex1; | ||
| this->Internals->Texture->SetInputData(this->Internals->GroupSkins[animIndex][textureIndex]); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will apply 👍🏼