Skip to content
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

WIP: Add getChunk method to FrameSet and substitute its result into commands #367

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import com.imageworks.spcue.rqd.RqdClient;
import com.imageworks.spcue.service.BookingManager;
import com.imageworks.spcue.service.DependManager;
import com.imageworks.spcue.util.FrameSet;

@Transactional(propagation = Propagation.REQUIRED)
public class DispatchSupportService implements DispatchSupport {
Expand Down Expand Up @@ -346,6 +347,10 @@ public RunFrame prepareRqdRunFrame(VirtualProc proc, DispatchFrame frame) {
int frameNumber = Integer.valueOf(frame.name.substring(0,frame.name.indexOf("-")));
String zFrameNumber = String.format("%04d", frameNumber);

FrameSet fs = new FrameSet(frame.range);
int startFrameIndex = fs.index(frameNumber);
String frameSpec = fs.getChunk(startFrameIndex, frame.chunkSize);

return RunFrame.newBuilder()
.setShot(frame.shot)
.setShow(frame.show)
Expand Down Expand Up @@ -386,6 +391,7 @@ public RunFrame prepareRqdRunFrame(VirtualProc proc, DispatchFrame frame) {
.replaceAll("#IFRAME#", String.valueOf(frameNumber))
.replaceAll("#LAYER#", frame.layerName)
.replaceAll("#JOB#", frame.jobName)
.replaceAll("#FRAMESPEC#", frameSpec)
.replaceAll("#FRAME#", frame.name))
.build();
}
Expand Down
104 changes: 104 additions & 0 deletions cuebot/src/main/java/com/imageworks/spcue/util/FrameSet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.imageworks.spcue.util;

import java.lang.IllegalArgumentException;
import java.util.ArrayList;
import java.util.StringJoiner;
import com.google.common.collect.ImmutableList;
import com.google.common.base.Strings;

/**
* Represents an ordered sequence of FrameRanges.
Expand Down Expand Up @@ -59,4 +63,104 @@ private ImmutableList<Integer> parseFrameRange(String frameRange) {
}
return builder.build();
}

/**
* Return a sub-FrameSet object starting at startFrame with max chunkSize members
* @param startFrameIndex Index of frame to start at; not the frame itself
* @param chunkSize Max number of frames per chunk
* @return String representation of the chunk, e.g. 1-1001x3
*/
public String getChunk(int startFrameIndex, int chunkSize) {
if (frameList.size() <= startFrameIndex || startFrameIndex < 0) {
String sf = String.valueOf(startFrameIndex);
String sz = String.valueOf(frameList.size() - 1);
throw new IllegalArgumentException("startFrameIndex " + sf + " is not in range 0-" + sz);
}
if (chunkSize == 1) {
// Chunksize of 1 so the FrameSet is just the startFrame
return String.valueOf(frameList.get(startFrameIndex));
}
int finalFrameIndex = frameList.size() - 1;
int endFrameIndex = startFrameIndex + chunkSize;
if (endFrameIndex > finalFrameIndex) {
// We don't have enough frames, so return the remaining frames.
endFrameIndex = finalFrameIndex;
}

return framesToFrameRanges(frameList.subList(startFrameIndex, endFrameIndex));
}

/**
* Return a string representation of a subset of a frame range.
*
* This approach was adapted from https://pypi.org/project/Fileseq/
* @param startFrame Start frame
* @param endFrame End frame
* @param step The step between frames
* @return String representation of the frame range, e.g. 1-1001x3
*/
private String buildFrangePart(int startFrame, int endFrame, int step) {
if (startFrame == endFrame) {
return String.valueOf(startFrame);
} else if (step == 1) {
return String.format("%d-%d", startFrame, endFrame);
} else {
return String.format("%d-%dx%d", startFrame, endFrame, step);
}
}

/**
* Return a String representation of a frame range based on a list of literal integer frame IDs.
* @param frames List of integers representing frame IDs,
* @return String representation of a frameset, e.g. '1-10,12-100x2'
*/
private String framesToFrameRanges(ImmutableList<Integer> frames) {
int l = frames.size();
if (l == 0) {
return "";
} else if (l == 1) {
return String.valueOf(frames.get(0));
}

StringJoiner resultBuilder = new StringJoiner(",");

int curr_count = 1;
int curr_step = 0;
int new_step = 0;
int curr_start = frames.get(0);
int curr_frame = frames.get(0);
int last_frame = frames.get(0);

for (int i = 1; i < frames.size(); i++) {
curr_frame = frames.get(i);

if (curr_step == 0) {
curr_step = curr_frame - curr_start;
}
new_step = curr_frame - last_frame;
if (curr_step == new_step) {
last_frame = curr_frame;
curr_count += 1;
} else if (curr_count == 2 && curr_step != 1) {
resultBuilder.add(String.valueOf(curr_start));
curr_step = 0;
curr_start = last_frame;
last_frame = curr_frame;
} else {
resultBuilder.add(buildFrangePart(curr_start, last_frame, curr_step));
curr_step = 0;
curr_start = curr_frame;
last_frame = curr_frame;
curr_count = 1;
}
}
if (curr_count == 2 && curr_step != 1) {
resultBuilder.add(String.valueOf(curr_start));
resultBuilder.add(String.valueOf(curr_frame));
} else {
resultBuilder.add(buildFrangePart(curr_start, curr_frame, curr_step));
}

return resultBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,45 @@ public void testIndex() {
assertEquals(5, result.index(6));
assertEquals(-1, result.index(22));
}

@Test
public void testFramesToFrameRanges00() {
FrameSet result = new FrameSet("1-10x2,11-100x20,103-108");

// int[] intArray = {1, 3, 5, 7, 9, 11, 31, 51, 71, 91, 103, 104, 105, 106, 107, 108};

assertEquals("11-91x20", result.getChunk(5, 5));
}

@Test
public void testFramesToFrameRanges01() {
FrameSet result = new FrameSet("1-10x2,11-100x20,103-108");

// int[] intArray = {1, 3, 5, 7, 9, 11, 31, 51, 71, 91, 103, 104, 105, 106, 107, 108};

assertEquals("5-11x2,31", result.getChunk(2, 5));
}

@Test
public void testFramesToFrameRanges02() {
FrameSet result = new FrameSet("1-10x2,11-100x20,103-108");

// int[] intArray = {1, 3, 5, 7, 9, 11, 31, 51, 71, 91, 103, 104, 105, 106, 107, 108};

assertEquals("91,103,104", result.getChunk(9, 3));
}

@Test
public void testFramesToFrameRanges03() {
FrameSet result = new FrameSet("1-100x3");

assertEquals("28-34x3", result.getChunk(9, 3));
}

@Test
public void testFramesToFrameRanges04() {
FrameSet result = new FrameSet("1-100");

assertEquals("10-12", result.getChunk(9, 3));
}
}