2019-11-29 23:13:57 -05:00
|
|
|
package com.chameleonvision.vision.pipeline;
|
|
|
|
|
|
|
|
|
|
import com.chameleonvision.config.CameraConfig;
|
|
|
|
|
import com.chameleonvision.config.ConfigManager;
|
|
|
|
|
import com.chameleonvision.vision.VisionManager;
|
|
|
|
|
import com.chameleonvision.vision.VisionProcess;
|
|
|
|
|
import com.chameleonvision.web.SocketHandler;
|
2019-11-29 21:41:38 -08:00
|
|
|
import edu.wpi.first.networktables.NetworkTableEntry;
|
2019-11-29 23:13:57 -05:00
|
|
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("WeakerAccess")
|
|
|
|
|
public class PipelineManager {
|
|
|
|
|
|
2019-11-30 11:52:35 -05:00
|
|
|
private static final int DRIVERMODE_INDEX = -1;
|
|
|
|
|
|
2019-11-29 23:13:57 -05:00
|
|
|
public final LinkedList<CVPipeline> pipelines = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
public final CVPipeline driverModePipeline = new DriverVisionPipeline(new CVPipelineSettings());
|
|
|
|
|
|
|
|
|
|
private final VisionProcess parentProcess;
|
2019-11-30 11:52:35 -05:00
|
|
|
private int lastPipelineIndex;
|
2019-11-29 23:13:57 -05:00
|
|
|
private int currentPipelineIndex;
|
2019-11-29 21:41:38 -08:00
|
|
|
public NetworkTableEntry ntIndexEntry;
|
2019-11-29 23:13:57 -05:00
|
|
|
|
|
|
|
|
public PipelineManager(VisionProcess visionProcess, List<CVPipelineSettings> loadedPipelineSettings) {
|
|
|
|
|
parentProcess = visionProcess;
|
|
|
|
|
if (loadedPipelineSettings == null || loadedPipelineSettings.size() == 0) {
|
|
|
|
|
pipelines.add(new CVPipeline2d("New Pipeline"));
|
|
|
|
|
} else {
|
|
|
|
|
for (CVPipelineSettings setting : loadedPipelineSettings) {
|
|
|
|
|
addInternalPipeline(setting);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-29 21:18:07 -08:00
|
|
|
driverModePipeline.initPipeline(visionProcess.getCamera());
|
2019-11-29 23:13:57 -05:00
|
|
|
setCurrentPipeline(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void reassignIndexes() {
|
|
|
|
|
pipelines.sort(IndexComparator);
|
|
|
|
|
for (int i = 0; i < pipelines.size(); i++) {
|
|
|
|
|
pipelines.get(i).settings.index = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CameraConfig getConfig(VisionProcess process) {
|
|
|
|
|
return VisionManager.getCameraConfig(process);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CameraConfig getConfig() {
|
|
|
|
|
return getConfig(parentProcess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void savePipelineConfig(CVPipelineSettings setting) {
|
|
|
|
|
getConfig().pipelineConfig.save(setting);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void deletePipelineConfig(CVPipelineSettings setting) {
|
|
|
|
|
getConfig().pipelineConfig.delete(setting);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void renamePipelineConfig(CVPipelineSettings setting, String newName) {
|
|
|
|
|
getConfig().pipelineConfig.rename(setting, newName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void saveAllPipelines() {
|
|
|
|
|
pipelines.parallelStream().map(pipeline -> pipeline.settings).forEach(this::savePipelineConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addInternalPipeline(CVPipelineSettings setting) {
|
|
|
|
|
if (setting instanceof CVPipeline3dSettings) {
|
|
|
|
|
pipelines.add(new CVPipeline3d((CVPipeline3dSettings) setting));
|
|
|
|
|
} else if (setting instanceof CVPipeline2dSettings) {
|
|
|
|
|
pipelines.add(new CVPipeline2d((CVPipeline2dSettings) setting));
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("Non 2D/3D pipelines not supported!");
|
|
|
|
|
}
|
|
|
|
|
reassignIndexes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setDriverMode(boolean driverMode) {
|
2019-11-30 11:52:35 -05:00
|
|
|
if (driverMode) {
|
|
|
|
|
setCurrentPipeline(DRIVERMODE_INDEX);
|
|
|
|
|
} else {
|
|
|
|
|
setCurrentPipeline(lastPipelineIndex);
|
|
|
|
|
}
|
2019-11-29 23:13:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getDriverMode() {
|
2019-11-30 11:52:35 -05:00
|
|
|
return currentPipelineIndex == DRIVERMODE_INDEX;
|
2019-11-29 23:13:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getCurrentPipelineIndex() {
|
|
|
|
|
return currentPipelineIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CVPipeline getCurrentPipeline() {
|
2019-11-30 20:31:55 -05:00
|
|
|
if (currentPipelineIndex <= DRIVERMODE_INDEX) {
|
|
|
|
|
return driverModePipeline;
|
|
|
|
|
} else {
|
|
|
|
|
return pipelines.get(currentPipelineIndex);
|
|
|
|
|
}
|
2019-11-29 23:13:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCurrentPipeline(int index) {
|
2019-11-30 11:52:35 -05:00
|
|
|
CVPipeline newPipeline;
|
|
|
|
|
if (index == DRIVERMODE_INDEX) {
|
|
|
|
|
newPipeline = driverModePipeline;
|
|
|
|
|
} else {
|
|
|
|
|
newPipeline = pipelines.get(index);
|
|
|
|
|
}
|
2019-11-29 23:13:57 -05:00
|
|
|
if (newPipeline != null) {
|
2019-11-30 11:52:35 -05:00
|
|
|
lastPipelineIndex = currentPipelineIndex;
|
2019-11-29 23:13:57 -05:00
|
|
|
currentPipelineIndex = index;
|
|
|
|
|
getCurrentPipeline().initPipeline(parentProcess.getCamera());
|
|
|
|
|
|
|
|
|
|
if(ConfigManager.settings.currentCamera.equals(parentProcess.getCamera().getProperties().name)) {
|
|
|
|
|
ConfigManager.settings.currentPipeline = currentPipelineIndex;
|
|
|
|
|
|
|
|
|
|
HashMap<String, Object> pipeChange = new HashMap<>();
|
|
|
|
|
pipeChange.put("currentPipeline", currentPipelineIndex);
|
|
|
|
|
SocketHandler.broadcastMessage(pipeChange);
|
|
|
|
|
try {
|
|
|
|
|
SocketHandler.sendFullSettings();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// avoid NullPointerException when run before threads start
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
newPipeline.initPipeline(parentProcess.getCamera());
|
2019-11-29 21:41:38 -08:00
|
|
|
if(ntIndexEntry != null) {
|
|
|
|
|
ntIndexEntry.setDouble(index);
|
|
|
|
|
}
|
2019-11-29 23:13:57 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addPipeline(CVPipelineSettings setting) {
|
|
|
|
|
addInternalPipeline(setting);
|
|
|
|
|
savePipelineConfig(setting);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addPipeline(CVPipeline pipeline) {
|
|
|
|
|
pipelines.add(pipeline);
|
|
|
|
|
reassignIndexes();
|
|
|
|
|
savePipelineConfig(pipeline.settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addNewPipeline(boolean is3D) {
|
|
|
|
|
CVPipeline newPipeline;
|
|
|
|
|
if (!is3D) {
|
|
|
|
|
newPipeline = new CVPipeline2d();
|
|
|
|
|
} else {
|
|
|
|
|
newPipeline = new CVPipeline3d();
|
|
|
|
|
}
|
|
|
|
|
newPipeline.settings.index = pipelines.size();
|
|
|
|
|
addPipeline(newPipeline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CVPipeline getPipeline(int index) {
|
|
|
|
|
return pipelines.get(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void duplicatePipeline(CVPipelineSettings pipeline) {
|
|
|
|
|
duplicatePipeline(pipeline, parentProcess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void duplicatePipeline(CVPipelineSettings pipeline, VisionProcess destinationProcess) {
|
|
|
|
|
pipeline.index = destinationProcess.pipelineManager.pipelines.size();
|
|
|
|
|
pipeline.nickname += "(Copy)";
|
|
|
|
|
destinationProcess.pipelineManager.addPipeline(pipeline);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-30 11:52:35 -05:00
|
|
|
public void renameCurrentPipeline(String newName) {
|
|
|
|
|
CVPipelineSettings settings = getCurrentPipeline().settings;
|
|
|
|
|
settings.nickname = newName;
|
|
|
|
|
renamePipelineConfig(settings, newName);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 23:13:57 -05:00
|
|
|
public void deleteCurrentPipeline() {
|
|
|
|
|
deletePipeline(currentPipelineIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void deletePipeline(int index) {
|
|
|
|
|
if (index == currentPipelineIndex) {
|
|
|
|
|
currentPipelineIndex -= 1;
|
|
|
|
|
}
|
|
|
|
|
deletePipelineConfig(getPipeline(index).settings);
|
|
|
|
|
pipelines.remove(index);
|
|
|
|
|
reassignIndexes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void saveDriverModeConfig() {
|
|
|
|
|
getConfig().saveDriverMode(driverModePipeline.settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Comparator<CVPipeline> IndexComparator = (o1, o2) -> {
|
|
|
|
|
int o1Index = o1.settings.index;
|
|
|
|
|
int o2Index = o2.settings.index;
|
|
|
|
|
|
|
|
|
|
if (o1Index == o2Index) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else if (o1Index < o2Index) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
};
|
|
|
|
|
}
|