Commit 6cdc16ca authored by Daniel Schramm's avatar Daniel Schramm
Browse files

Unnecessarily repeated computation on weighted-HD-ON/OFF switch eliminated

parent 260b2075
Loading
Loading
Loading
Loading
+47 −34
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ public class DistanceAction extends ControlPanelAction {
    private boolean relativeDist = false;
    private boolean weightedDist = false;
    
    private Map<MeshFacet, List<Double>> hausdorffDistance = null;
    
    private final DistancePanel controlPanel;
    
    /**
@@ -54,7 +56,7 @@ public class DistanceAction extends ControlPanelAction {

    @Override
    public void actionPerformed(ActionEvent ae) {
        String action = ae.getActionCommand();
        final String action = ae.getActionCommand();
        
        switch (action) {
            case DistancePanel.ACTION_COMMAND_SHOW_HIDE_PANEL:
@@ -62,7 +64,7 @@ public class DistanceAction extends ControlPanelAction {
                break;
            case DistancePanel.ACTION_COMMAND_SHOW_HIDE_HEATMAP:
                if (((JToggleButton) ae.getSource()).isSelected()) {
                    setHeatmap();
                    calculateHausdorffDistance();
                    getSecondaryDrawableFace().setRenderHeatmap(true);
                } else {
                    getSecondaryDrawableFace().setRenderHeatmap(false);
@@ -71,12 +73,12 @@ public class DistanceAction extends ControlPanelAction {
            case DistancePanel.ACTION_COMMAND_SET_DISTANCE_STRATEGY:
                strategy = (String) ((JComboBox) ae.getSource()).getSelectedItem();
                this.visitor = null; // recompute
                setHeatmap();
                calculateHausdorffDistance();
                break;
            case DistancePanel.ACTION_COMMAND_RELATIVE_ABSOLUTE_DIST:
                this.relativeDist = ((JToggleButton) ae.getSource()).isSelected();
                this.visitor = null; // recompute
                setHeatmap();
                calculateHausdorffDistance();
                break;
            case DistancePanel.ACTION_COMMAND_FEATURE_POINT_HIGHLIGHT:
                highlightFeaturePoint((LoadedActionEvent) ae);
@@ -86,10 +88,12 @@ public class DistanceAction extends ControlPanelAction {
                break;
            case DistancePanel.ACTION_COMMAND_WEIGHTED_DISTANCE:
                this.weightedDist = ((JToggleButton) ae.getSource()).isSelected();
                this.hausdorffDistance = null; // recompute only priorities
                calculateHausdorffDistance();
                break;
            case DistancePanel.ACTION_COMMAND_WEIGHTED_DIST_RECOMPUTE:
                this.visitor = null; // recompute
                setHeatmap();
                setFeaturePointWeigths();
                calculateHausdorffDistance();
                break;
            default:
                // to nothing
@@ -97,8 +101,8 @@ public class DistanceAction extends ControlPanelAction {
        renderScene();
    }
    
    protected void setHeatmap() {
        Strategy useStrategy;
    protected void calculateHausdorffDistance() {
        final Strategy useStrategy;
        switch (strategy) {
            case DistancePanel.STRATEGY_POINT_TO_POINT:
                useStrategy = Strategy.POINT_TO_POINT;
@@ -117,16 +121,50 @@ public class DistanceAction extends ControlPanelAction {
                    relativeDist,
                    true);
            getSecondaryDrawableFace().getHumanFace().accept(visitor);
            hausdorffDistance = null;
        }
        
        if (hausdorffDistance == null) {
            hausdorffDistance = getPrioritizedDistance();
            setHausdorffDistanceStatistics();
            setFeaturePointWeigths();
        }
        
        final Map<MeshFacet, List<Double>> hausdorffDistance = calculateHausdorffDistance();
        getSecondaryDrawableFace().setHeatMap(hausdorffDistance);
    }
    
    private Map<MeshFacet, List<Double>> getPrioritizedDistance() {
        if (!weightedDist) {
            return visitor.getDistances();
        }
        
        // Merge the map of distances with the map of priorities
        final Map<MeshFacet, List<Double>> weightedDistances = new HashMap<>(visitor.getDistances());

        final Map<MeshFacet, List<Double>> mergedPriorities = visitor.getMergedPriorities()
                .get(getSecondaryDrawableFace().getHumanFace());
        for (final Map.Entry<MeshFacet, List<Double>> facetPriorities: mergedPriorities.entrySet()) {
            weightedDistances.merge(
                    facetPriorities.getKey(),
                    facetPriorities.getValue(),
                    (distancesList, prioritiesList) ->
                            IntStream.range(0, distancesList.size())
                                    .mapToDouble(i -> distancesList.get(i) * prioritiesList.get(i))
                                    .boxed()
                                    .collect(Collectors.toList())
            );
        }

        return weightedDistances;
    }

    private void setHausdorffDistanceStatistics() {
        final DoubleSummaryStatistics distanceStats = hausdorffDistance.values()
                .stream()
                .flatMap(List::stream)
                .mapToDouble(distance -> distance)
                .summaryStatistics();
        
        controlPanel.updateHausdorffDistanceStats(
                distanceStats.getAverage(),
                distanceStats.getMax(),
@@ -156,31 +194,6 @@ public class DistanceAction extends ControlPanelAction {
                                                .orElse(Double.NaN))));
    }
    
    private Map<MeshFacet, List<Double>> calculateHausdorffDistance() {
        if (!weightedDist) {
            return visitor.getDistances();
        }
        
        // Merge the map of distances with the map of priorities
        final Map<MeshFacet, List<Double>> weightedDistances = new HashMap<>(visitor.getDistances());

        final Map<MeshFacet, List<Double>> mergedPriorities = visitor.getMergedPriorities()
                .get(getSecondaryDrawableFace().getHumanFace());
        for (final Map.Entry<MeshFacet, List<Double>> facetPriorities: mergedPriorities.entrySet()) {
            weightedDistances.merge(
                    facetPriorities.getKey(),
                    facetPriorities.getValue(),
                    (distancesList, prioritiesList) ->
                            IntStream.range(0, distancesList.size())
                                    .mapToDouble(i -> distancesList.get(i) * prioritiesList.get(i))
                                    .boxed()
                                    .collect(Collectors.toList())
            );
        }

        return weightedDistances;
    }
    
    private void highlightFeaturePoint(LoadedActionEvent actionEvent) {
        final int index = (int) actionEvent.getData();
        final FeaturePointType fpType = getTypeOfFeaturePoint(index);