|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (C) 2024, Ko Sugawara |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + ******************************************************************************/ |
| 27 | +package org.elephant.mamut.plugin; |
| 28 | + |
| 29 | +import java.awt.event.ActionEvent; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | +import org.mastodon.app.ui.ViewMenuBuilder.MenuItem; |
| 37 | +import org.mastodon.mamut.KeyConfigScopes; |
| 38 | +import org.mastodon.mamut.MamutMenuBuilder; |
| 39 | +import org.mastodon.mamut.ProjectModel; |
| 40 | +import org.mastodon.mamut.model.ModelGraph; |
| 41 | +import org.mastodon.mamut.model.Spot; |
| 42 | +import org.mastodon.mamut.plugin.MamutPlugin; |
| 43 | +import org.mastodon.model.tag.ObjTagMap; |
| 44 | +import org.mastodon.model.tag.TagSetStructure.Tag; |
| 45 | +import org.mastodon.model.tag.TagSetStructure.TagSet; |
| 46 | +import org.mastodon.ui.keymap.KeyConfigContexts; |
| 47 | +import org.scijava.plugin.Plugin; |
| 48 | +import org.scijava.ui.behaviour.io.gui.CommandDescriptionProvider; |
| 49 | +import org.scijava.ui.behaviour.io.gui.CommandDescriptions; |
| 50 | +import org.scijava.ui.behaviour.util.AbstractNamedAction; |
| 51 | +import org.scijava.ui.behaviour.util.Actions; |
| 52 | + |
| 53 | +@Plugin( type = ConflictDetectorPlugin.class ) |
| 54 | +public class ConflictDetectorPlugin implements MamutPlugin |
| 55 | +{ |
| 56 | + |
| 57 | + private final static String ACTION_NAME = "conflict detector"; |
| 58 | + |
| 59 | + private ConflictDetectorAction action; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setAppPluginModel( final ProjectModel projectModel ) |
| 63 | + { |
| 64 | + this.action = new ConflictDetectorAction( projectModel ); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void installGlobalActions( final Actions actions ) |
| 69 | + { |
| 70 | + final String keyboardShortcut = "not mapped"; |
| 71 | + |
| 72 | + actions.namedAction( action, keyboardShortcut ); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + private static class ConflictDetectorAction extends AbstractNamedAction |
| 77 | + { |
| 78 | + |
| 79 | + private static final long serialVersionUID = 1L; |
| 80 | + |
| 81 | + private final ProjectModel projectModel; |
| 82 | + |
| 83 | + private final ModelGraph graph; |
| 84 | + |
| 85 | + final double[] pos = new double[ 3 ]; |
| 86 | + |
| 87 | + private ConflictDetectorAction( final ProjectModel projectModel ) |
| 88 | + { |
| 89 | + super( ACTION_NAME ); |
| 90 | + this.projectModel = projectModel; |
| 91 | + this.graph = projectModel.getModel().getGraph(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void actionPerformed( final ActionEvent e ) |
| 96 | + { |
| 97 | + TagSet tagSet = projectModel.getModel().getTagSetModel().getTagSetStructure().getTagSets().stream() |
| 98 | + .filter( ts -> ts.getName().equals( "Duplicate" ) ).findFirst().orElse( null ); |
| 99 | + Tag tag = tagSet.getTags().stream().filter( t -> t.label().equals( "duplicate" ) ).findFirst().orElse( null ); |
| 100 | + ObjTagMap< Spot, Tag > tagMap = projectModel.getModel().getTagSetModel().getVertexTags().tags( tagSet ); |
| 101 | + for ( int t = projectModel.getMinTimepoint(); t <= projectModel.getMaxTimepoint(); t++ ) |
| 102 | + { |
| 103 | + final int time = t; |
| 104 | + Set< String > hashSet = new HashSet<>(); |
| 105 | + graph.vertices().stream().filter( spot -> spot.getTimepoint() == time ).forEach( spot -> { |
| 106 | + spot.localize( pos ); |
| 107 | + String hash = GeoHash3D.geoHashStringWithCharacterPrecision( pos[ 0 ], pos[ 1 ], pos[ 2 ], 4 ); |
| 108 | + if ( hashSet.contains( hash ) ) |
| 109 | + { |
| 110 | + tagMap.set( spot, tag ); |
| 111 | + System.out.println( "Conflict: " + spot ); |
| 112 | + } |
| 113 | + hashSet.add( hash ); |
| 114 | + } ); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Plugin( type = Descriptions.class ) |
| 120 | + public static class Descriptions extends CommandDescriptionProvider |
| 121 | + { |
| 122 | + public Descriptions() |
| 123 | + { |
| 124 | + super( KeyConfigScopes.MAMUT, KeyConfigContexts.MASTODON ); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void getCommandDescriptions( final CommandDescriptions descriptions ) |
| 129 | + { |
| 130 | + final String actionName = ACTION_NAME; |
| 131 | + final String[] keyboardShortcut = new String[] { "not mapped" }; |
| 132 | + final String description = "Detect conflicts."; |
| 133 | + descriptions.add( actionName, keyboardShortcut, description ); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public List< MenuItem > getMenuItems() |
| 140 | + { |
| 141 | + final String actionName = ACTION_NAME; |
| 142 | + final MenuItem menuItem = MamutMenuBuilder.makeFullMenuItem( |
| 143 | + actionName, |
| 144 | + "Plugins", "Averof Lab" ); |
| 145 | + return Collections.singletonList( menuItem ); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Map< String, String > getMenuTexts() |
| 150 | + { |
| 151 | + final String actionName = ACTION_NAME; |
| 152 | + return Collections.singletonMap( |
| 153 | + actionName, |
| 154 | + "Detect conflicts" ); |
| 155 | + } |
| 156 | +} |
0 commit comments