Skip to content

Commit

Permalink
TrackEdit: add the behead() method for a MorphTrack
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 10, 2022
1 parent 613b05d commit ad649c5
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions WesLibrary/src/main/java/jme3utilities/wes/TrackEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,60 @@ private TrackEdit() {
// *************************************************************************
// new methods exposed

/**
* Copy a MorphTrack, deleting everything before the specified time and
* making that the start of the new track.
*
* @param oldTrack the input MorphTrack (not null, unaffected)
* @param neckTime the cutoff time (in seconds, >0)
* @param neckWeights the weights at the neck time (not null, unaffected)
* @return a new MorphTrack with the same target and t[0]=0
*/
public static MorphTrack behead(MorphTrack oldTrack, float neckTime,
float[] neckWeights) {
Validate.positive(neckTime, "neck time");
Validate.nonNull(neckWeights, "neck weights");

float[] oldTimes = oldTrack.getTimes();
assert neckTime >= oldTimes[0] : neckTime;
int oldCount = oldTimes.length;
float[] oldWeights = oldTrack.getWeights();

int neckIndex = MyArray.findPreviousIndex(neckTime, oldTimes);
int newCount = oldCount - neckIndex;
assert newCount > 0 : newCount;
/*
* Allocate new arrays.
*/
float[] newTimes = new float[newCount];
int numTargets = oldTrack.getNbMorphTargets();
float[] newWeights = new float[newCount * numTargets];

newTimes[0] = 0f;
for (int j = 0; j < numTargets; ++j) {
float weight = neckWeights[j];
newWeights[j] = weight;
}

for (int newIndex = 1; newIndex < newCount; ++newIndex) {
int oldIndex = newIndex + neckIndex;
newTimes[newIndex] = oldTimes[oldIndex] - neckTime;

int oldStart = oldIndex * numTargets;
int newStart = newIndex * numTargets;
for (int j = 0; j < numTargets; ++j) {
float weight = oldWeights[oldStart + j];
newWeights[newStart + j] = weight;
}
}

Geometry target = oldTrack.getTarget();
MorphTrack result
= new MorphTrack(target, newTimes, newWeights, numTargets);

return result;
}

/**
* Copy a bone/spatial track, deleting everything before the specified time
* and making that the start of the track.
Expand Down

0 comments on commit ad649c5

Please sign in to comment.