Search in sources :

Example 26 with Group

use of facetmodeller.groups.Group in project facetmodeller by pglelievre.

the class ChangeGroupFacetColorMenuTask method execute.

@Override
public void execute() {
    // Check for the required information:
    if (!check()) {
        return;
    }
    // Get the selected group:
    Group group = controller.getSelectedCurrentGroup();
    // Ask for the colour:
    Color col = JColorChooser.showDialog(controller, title(), group.getFacetColor());
    // Check response:
    if (col == null) {
        return;
    }
    // Set painting colour to that specified:
    group.setFacetColor(col);
    // Repaint:
    controller.redraw();
}
Also used : Group(facetmodeller.groups.Group) Color(java.awt.Color)

Example 27 with Group

use of facetmodeller.groups.Group in project facetmodeller by pglelievre.

the class FileIOManager method exportPoly.

/**
 * Exports to a poly file.
 * @param whatToExport Specifies what to export using one of the EXPORT_* integers defined in this class.
 */
public void exportPoly(int whatToExport) {
    if (!controller.hasSections()) {
        return;
    }
    // a title for some dialogs
    String title = "Export Poly";
    // Get or make the required plc and reset the ID's:
    PLC plc;
    controller.resetIDs();
    switch(whatToExport) {
        case EXPORT_CURRENT:
            Group g = controller.getSelectedCurrentGroup();
            plc = new PLC();
            plc.addNodes(g.getNodes());
            plc.addFacets(g.getFacets());
            plc.resetIDs();
            break;
        case EXPORT_DISPLAYED:
            GroupVector gn = controller.getSelectedNodeGroups();
            GroupVector gf = controller.getSelectedFacetGroups();
            plc = new PLC();
            plc.addNodes(gn.getNodes());
            plc.addFacets(gf.getFacets());
            plc.resetIDs();
            break;
        default:
            plc = controller.getPLC();
            break;
    }
    // Check nodes and facets exist:
    if (!plc.hasNodes()) {
        Dialogs.error(controller, "There are no nodes to export.", title);
        return;
    }
    if (!plc.hasFacets()) {
        Dialogs.error(controller, "There are no facets to export.", title);
        return;
    }
    // Ask what to write for region attributes:
    boolean byIndex = true;
    if (plc.hasRegions()) {
        String message = "The region attributes can be the region indices or region group IDs. Which would you like to use?";
        int response = Dialogs.question(controller, message, title, "Indices", "Group IDs", "Cancel", "Indices");
        switch(response) {
            case Dialogs.YES_OPTION:
                byIndex = true;
                break;
            case Dialogs.NO_OPTION:
                byIndex = false;
                break;
            default:
                // user cancelled
                return;
        }
    }
    // Ask for the file name for saving:
    JFileChooser chooser = new JFileChooser();
    PolyFilter filter = new PolyFilter();
    chooser.setCurrentDirectory(getSaveDirectory());
    chooser.addChoosableFileFilter(filter);
    chooser.setFileFilter(filter);
    chooser.setDialogTitle(title);
    chooser.setMultiSelectionEnabled(false);
    File file = getSessionFile();
    if (file != null) {
        String root = FileUtils.getRoot(file);
        file = new File(root + "." + PolyFilter.POLY);
        chooser.setSelectedFile(file);
    }
    int response = chooser.showSaveDialog(controller);
    // Check response and get the selected file:
    if (response != JFileChooser.APPROVE_OPTION) {
        return;
    }
    file = chooser.getSelectedFile();
    // Check response:
    if (file == null) {
        return;
    }
    // Give the file the .poly extension:
    String root = FileUtils.getRoot(file);
    file = new File(root + "." + PolyFilter.POLY);
    // Set the save directory to the chosen directory:
    setSaveDirectory(chooser.getCurrentDirectory());
    // Check for file overwrite:
    if (file.exists()) {
        response = Dialogs.confirm(controller, "Overwrite the existing file?", title);
        if (response != Dialogs.OK_OPTION) {
            return;
        }
    }
    // Write the poly file:
    Dir3D dir = null;
    final int ndim = controller.numberOfDimensions();
    if (ndim == 2) {
        dir = controller.getSelectedCurrentSection().getDir3D();
    }
    boolean ok = plc.writePoly(file, startingIndex, precision, ndim, dir, byIndex);
    // } else {
    if (!ok) {
        Dialogs.error(controller, "Failed to save .poly file.", title);
    }
}
Also used : Group(facetmodeller.groups.Group) Dir3D(geometry.Dir3D) GroupVector(facetmodeller.groups.GroupVector) JFileChooser(javax.swing.JFileChooser) PLC(facetmodeller.plc.PLC) PolyFilter(filters.PolyFilter) File(java.io.File)

Example 28 with Group

use of facetmodeller.groups.Group in project facetmodeller by pglelievre.

the class FileIOManager method loadNodeAndEle.

public String loadNodeAndEle(NoImageDepthSection section) {
    // Get the node and ele files:
    File nodeFile = section.getNodeFile();
    File eleFile = section.getEleFile();
    // Check for a node file:
    if (nodeFile == null) {
        return null;
    }
    // Read the node file:
    NodeVector nodes = new NodeVector();
    NodeVector.ReadNodesReturnObject readNodesReturnObj = nodes.readNodes(nodeFile, -2);
    String errmsg = readNodesReturnObj.getErrmsg();
    if (errmsg != null) {
        return errmsg + System.lineSeparator() + nodeFile;
    }
    // Get number of dimensions:
    int ndim = controller.numberOfDimensions();
    // Read the ele file if present:
    FacetVector facets = new FacetVector();
    if (eleFile != null) {
        int startInd = readNodesReturnObj.getStartingIndex();
        FacetVector.ReadFacetsReturnObject readFacetsReturnObj = facets.readEle(controller, "", eleFile, startInd, nodes, ndim, false);
        errmsg = readFacetsReturnObj.getErrmsg();
        if (errmsg != null) {
            return errmsg + System.lineSeparator() + eleFile;
        }
        // Delete any unrequired nodes:
        if (readFacetsReturnObj.getDoRem()) {
            nodes.removeUnused();
        }
    }
    // Define a new group for the nodes and facets:
    Group group = new Group("TOPOGRAPHY");
    // Add section and group membership to the new nodes and optional facets:
    nodes.setSection(section);
    nodes.setGroup(group);
    facets.setGroup(group);
    // Calculate the range of the nodes:
    MyPoint3D p1 = nodes.rangeMin();
    MyPoint3D p2 = nodes.rangeMax();
    // Add a little padding:
    // HARDWIRE
    p1.times(1.1);
    // HARDWIRE
    p2.times(1.1);
    // Calibrate the section:
    double x1 = p1.getX();
    double x2 = p2.getX();
    double y1 = p1.getY();
    double y2 = p2.getY();
    double z1 = p1.getZ();
    double z2 = p2.getZ();
    // corresponds to top left pixel (0,0)
    section.setTyped1(new MyPoint3D(x1, y2, z1));
    // corresponds to bottom right pixel (height,width)
    section.setTyped2(new MyPoint3D(x2, y1, z2));
    // Add the new group:
    new AddGroupCommand(controller, group, 0).execute();
    // Add the new nodes and optional facets to the plc, section, group:
    ModelManager model = controller.getModelManager();
    new AddNodeCommandVector(model, nodes, "").execute();
    new AddFacetCommandVector(model, facets).execute();
    // Return successfully:
    return null;
}
Also used : Group(facetmodeller.groups.Group) FacetVector(facetmodeller.plc.FacetVector) AddNodeCommandVector(facetmodeller.commands.AddNodeCommandVector) AddGroupCommand(facetmodeller.commands.AddGroupCommand) MyPoint3D(geometry.MyPoint3D) AddFacetCommandVector(facetmodeller.commands.AddFacetCommandVector) NodeVector(facetmodeller.plc.NodeVector) File(java.io.File)

Example 29 with Group

use of facetmodeller.groups.Group in project facetmodeller by pglelievre.

the class SessionLoader method loadSessionAscii1.

private static boolean loadSessionAscii1(FacetModeller controller, File file, boolean merge) {
    int loadVersion = 1;
    // We will be constructing some new objects as we read the file:
    PLC plc = new PLC();
    SectionVector sections = new SectionVector();
    GroupVector groups = new GroupVector();
    // We will be saving some information to set later:
    int ndim;
    Dir3D sliceDirection = Dir3D.X;
    // these colours will be overwritten
    Color calibrationColor = Color.CYAN;
    Color edgeColor = Color.BLACK;
    Color defineFacetEdgeColor = Color.WHITE;
    int pointWidth = 5;
    int lineWidth = 1;
    // Open the file for reading:
    BufferedReader reader = FileUtils.openForReading(file);
    if (reader == null) {
        return false;
    }
    // Put everything below in an infinite loop that we can break out of when something goes wrong:
    boolean ok = true;
    while (true) {
        // Read the floored version number:
        String textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        textLine = textLine.trim();
        String[] ss = textLine.split("[ ]+", 2);
        int version;
        try {
            // converts to integer
            version = Integer.parseInt(ss[0].trim());
        } catch (NumberFormatException e) {
            ok = false;
            break;
        }
        if (!ok) {
            break;
        }
        // Check the version number:
        if (version != loadVersion) {
            // Close the file:
            FileUtils.close(reader);
            // Return unsuccessfully:
            return false;
        }
        // Read the number of dimensions, nodes, facets, regions, samples and groups:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        int nnodes, nregions, nfacets, nsections, ngroups;
        textLine = textLine.trim();
        ss = textLine.split("[ ]+", 7);
        try {
            // converts to integer
            ndim = Integer.parseInt(ss[0].trim());
            // converts to integer
            nnodes = Integer.parseInt(ss[1].trim());
            // converts to integer
            nfacets = Integer.parseInt(ss[2].trim());
            // converts to integer
            nregions = Integer.parseInt(ss[3].trim());
            // converts to integer
            nsections = Integer.parseInt(ss[4].trim());
            // converts to integer
            ngroups = Integer.parseInt(ss[5].trim());
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
            ok = false;
            break;
        }
        if (!ok) {
            break;
        }
        // Check ndim:
        if (ndim != controller.numberOfDimensions()) {
            ok = false;
            break;
        }
        // Loop over each node:
        for (int i = 0; i < nnodes; i++) {
            // Read the coordinates of the ith node:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            double x, y;
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 3);
            try {
                // converts to Double
                x = Double.parseDouble(ss[0].trim());
                // converts to Double
                y = Double.parseDouble(ss[1].trim());
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            // Add a new node to the plc:
            // section and group membership will be added later
            plc.addNode(new NodeOnSection(x, y));
        }
        if (!ok) {
            break;
        }
        // Loop over each facet:
        for (int i = 0; i < nfacets; i++) {
            // Add a new empty facet to the plc (these facets are filled later):
            plc.addFacet(new Facet());
        }
        // Loop over each region:
        for (int i = 0; i < nregions; i++) {
            // Read the coordinates of the ith region:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            double x, y;
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 3);
            try {
                // converts to Double
                x = Double.parseDouble(ss[0].trim());
                // converts to Double
                y = Double.parseDouble(ss[1].trim());
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            // Add a new region to the plc:
            // section and group membership will be added later
            plc.addRegion(new Region(false, x, y));
        }
        if (!ok) {
            break;
        }
        // Loop over each section:
        for (int i = 0; i < nsections; i++) {
            // Read the file name:
            textLine = FileUtils.readLine(reader);
            File f;
            if (textLine == null) {
                ok = false;
                break;
            }
            try {
                URI uri = new URI(textLine);
                f = new File(uri);
            } catch (URISyntaxException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            // Create a new Section object linked to that file:
            Section section = new ImageCrossSection(f);
            // Read the location coordinate:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 2);
            double loc;
            try {
                // converts to Double
                loc = Double.parseDouble(ss[0].trim());
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            // Read the typed and clicked points:
            double x, y;
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            if (!textLine.startsWith("null")) {
                ss = textLine.split("[ ]+", 3);
                try {
                    // converts to Double
                    x = Double.parseDouble(ss[0].trim());
                    // converts to Double
                    y = Double.parseDouble(ss[1].trim());
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                    ok = false;
                    break;
                }
                if (!ok) {
                    break;
                }
                section.setTyped1(new MyPoint3D(x, y, sliceDirection, loc));
            }
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            if (!textLine.startsWith("null")) {
                ss = textLine.split("[ ]+", 3);
                try {
                    // converts to Double
                    x = Double.parseDouble(ss[0].trim());
                    // converts to Double
                    y = Double.parseDouble(ss[1].trim());
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                    ok = false;
                    break;
                }
                if (!ok) {
                    break;
                }
                section.setTyped2(new MyPoint3D(x, y, sliceDirection, loc));
            }
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            if (!textLine.startsWith("null")) {
                ss = textLine.split("[ ]+", 3);
                try {
                    // converts to Double
                    x = Double.parseDouble(ss[0].trim());
                    // converts to Double
                    y = Double.parseDouble(ss[1].trim());
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                    ok = false;
                    break;
                }
                if (!ok) {
                    break;
                }
                section.setClicked1(new MyPoint2D(x, y));
            }
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            if (!textLine.startsWith("null")) {
                ss = textLine.split("[ ]+", 3);
                try {
                    // converts to Double
                    x = Double.parseDouble(ss[0].trim());
                    // converts to Double
                    y = Double.parseDouble(ss[1].trim());
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                    ok = false;
                    break;
                }
                if (!ok) {
                    break;
                }
                section.setClicked2(new MyPoint2D(x, y));
            }
            // Add the section to the list of sections:
            sections.add(section);
        }
        if (!ok) {
            break;
        }
        // Loop over each group:
        for (int i = 0; i < ngroups; i++) {
            // Create the group object:
            Group group = new Group();
            // Read the group name:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            group.setName(textLine.trim());
            // Read the group colours:
            Color col;
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            try {
                // parse from RGB string
                col = new Color(Integer.parseInt(textLine.trim()));
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            group.setNodeColor(col);
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            try {
                // parse from RGB string
                col = new Color(Integer.parseInt(textLine.trim()));
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            group.setFacetColor(col);
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            try {
                // parse from RGB string
                col = new Color(Integer.parseInt(textLine.trim()));
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            group.setRegionColor(col);
            // Add the group to the list of groups:
            groups.add(group);
        }
        if (!ok) {
            break;
        }
        // Loop over each node:
        for (int i = 0; i < nnodes; i++) {
            Node node = plc.getNode(i);
            // The node gets linked to the facets in the loop over each facet below.
            // Read the section id:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 2);
            // section id
            int id;
            try {
                // converts to integer
                id = Integer.parseInt(ss[0].trim());
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            // Cross-link the node and section:
            node.setSection(sections.get(id));
            sections.get(id).addNode(node);
            // Read the group id:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 2);
            try {
                // converts to integer
                id = Integer.parseInt(ss[0].trim());
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            // Cross-link the node and group:
            node.setGroup(groups.get(id));
            groups.get(id).addNode(node);
        }
        if (!ok) {
            break;
        }
        // Loop over each facet:
        for (int i = 0; i < nfacets; i++) {
            Facet facet = plc.getFacet(i);
            // Read the node id's and link those nodes to the facet:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+");
            // number of nodes
            int n;
            try {
                // converts to integer
                n = Integer.parseInt(ss[0].trim());
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            for (int j = 0; j < n; j++) {
                // node id
                int id;
                try {
                    // converts to integer
                    id = Integer.parseInt(ss[j + 1].trim());
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                    ok = false;
                    break;
                }
                // Cross-link the facet and node:
                facet.addNode(plc.getNode(id));
                plc.getNode(id).addFacet(facet);
            }
            // Read the section id's:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            /*
                textLine = textLine.trim();
                ss = textLine.split("[ ]+");
                try {
                    n = Integer.parseInt(ss[0].trim()); // converts to integer
                } catch (NumberFormatException e) { ok=false; break; }
                if (!ok) { break; }
                for (int j=0 ; j<n ; j++ ) {
                    int id;
                    try {
                        id = Integer.parseInt(ss[j+1].trim()); // converts to integer
                    } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { ok=false; break; }
                    // Cross-link the facet and section:
//                    facet.addSection( sections.get(id) ); // no longer necessary because facet sections defined by the facet nodes
                    sections.get(id).addFacet(facet);
                }
                */
            // Read the group id:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 2);
            int id;
            try {
                // converts to integer
                id = Integer.parseInt(ss[0].trim());
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            // Cross-link the facet and group:
            facet.setGroup(groups.get(id));
            groups.get(id).addFacet(facet);
        }
        if (!ok) {
            break;
        }
        // Loop over each region:
        for (int i = 0; i < nregions; i++) {
            Region region = plc.getRegion(i);
            // Read the section id:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 2);
            int id;
            try {
                // converts to integer
                id = Integer.parseInt(ss[0].trim());
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            // Cross-link the region and section:
            region.setSection(sections.get(id));
            sections.get(id).addRegion(region);
            // Read the group id and link that group to the node:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 2);
            try {
                // converts to integer
                id = Integer.parseInt(ss[0].trim());
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            // Cross-link the region and group:
            region.setGroup(groups.get(id));
            // groups.get(id).setRegion(region);
            groups.get(id).addRegion(region);
        }
        if (!ok) {
            break;
        }
        // ---------- Finish with the rest of the information: ----------
        // Read slice direction:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        // textLine = textLine.trim();
        // ss = textLine.split("[ ]+",2);
        // try {
        // int idir = Integer.parseInt(ss[0].trim()); // converts to integer
        // sliceDirection = Dir3D.fromInt(idir);
        // } catch (NumberFormatException e) { ok=false; break; }
        // Read painting colours, etc.:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        try {
            // parse from RGB string
            calibrationColor = new Color(Integer.parseInt(textLine.trim()));
        } catch (NumberFormatException e) {
            ok = false;
            break;
        }
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        try {
            // parse from RGB string
            edgeColor = new Color(Integer.parseInt(textLine.trim()));
        } catch (NumberFormatException e) {
            ok = false;
            break;
        }
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        try {
            // parse from RGB string
            defineFacetEdgeColor = new Color(Integer.parseInt(textLine.trim()));
        } catch (NumberFormatException e) {
            ok = false;
            break;
        }
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        try {
            pointWidth = Integer.parseInt(textLine.trim());
        } catch (NumberFormatException e) {
            ok = false;
            break;
        }
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            ok = false;
            break;
        }
        try {
            lineWidth = Integer.parseInt(textLine.trim());
        } catch (NumberFormatException e) {
            ok = false;
            break;
        }
        // Always break from while here:
        break;
    }
    // Close the file:
    FileUtils.close(reader);
    // Check for a problem:
    if (!ok) {
        return false;
    }
    // Reset the FacetModeller plc, section lists and group lists:
    controller.resetPLC(plc, merge);
    controller.resetSectionVector(sections, merge);
    controller.resetGroupVector(groups, merge);
    // If it is a large model then don't display anything:
    if (plc.numberOfNodes() >= LARGE_MODEL) {
        controller.clearGroupSelections();
    }
    // Reset some other information:
    if (!merge) {
        controller.setCalibrationColor(calibrationColor);
        controller.setEdgeColor(edgeColor);
        controller.setDefineFacetEdgeColor(defineFacetEdgeColor);
        controller.setPointWidth(pointWidth);
        controller.setLineWidth(lineWidth);
    }
    // Return successfully:
    return true;
}
Also used : SectionVector(facetmodeller.sections.SectionVector) Group(facetmodeller.groups.Group) PLC(facetmodeller.plc.PLC) Node(facetmodeller.plc.Node) URISyntaxException(java.net.URISyntaxException) NodeOnSection(facetmodeller.plc.NodeOnSection) URI(java.net.URI) MyPoint3D(geometry.MyPoint3D) Facet(facetmodeller.plc.Facet) Color(java.awt.Color) Section(facetmodeller.sections.Section) NoImageCrossSection(facetmodeller.sections.NoImageCrossSection) ImageCrossSection(facetmodeller.sections.ImageCrossSection) NodeOffSection(facetmodeller.plc.NodeOffSection) NodeOnSection(facetmodeller.plc.NodeOnSection) NoImageDepthSection(facetmodeller.sections.NoImageDepthSection) MyPoint2D(geometry.MyPoint2D) Dir3D(geometry.Dir3D) GroupVector(facetmodeller.groups.GroupVector) NoImageCrossSection(facetmodeller.sections.NoImageCrossSection) ImageCrossSection(facetmodeller.sections.ImageCrossSection) BufferedReader(java.io.BufferedReader) Region(facetmodeller.plc.Region) File(java.io.File)

Example 30 with Group

use of facetmodeller.groups.Group in project facetmodeller by pglelievre.

the class DefineNodeInFacetClickTask method calculateCandidateNode.

public Node calculateCandidateNode(MyPoint2D p, boolean verbose) {
    // Get the current section and current group:
    Section currentSection = controller.getSelectedCurrentSection();
    Group currentGroup = controller.getSelectedCurrentGroup();
    // Get the three nodes for the facet:
    Facet currentFacet = controller.getCurrentFacet();
    Node n0 = currentFacet.getNode(0);
    Node n1 = currentFacet.getNode(1);
    Node n2 = currentFacet.getNode(2);
    // Get various information about the nodes:
    Section s0 = n0.getSection();
    Section s1 = n1.getSection();
    Section s2 = n2.getSection();
    Boolean e0 = s0.equals(currentSection);
    Boolean e1 = s1.equals(currentSection);
    Boolean e2 = s2.equals(currentSection);
    // Get the 3D coordinates of the nodes:
    MyPoint3D p0 = n0.getPoint3D();
    MyPoint3D p1 = n1.getPoint3D();
    MyPoint3D p2 = n2.getPoint3D();
    // Project onto current section:
    // image pixel coordinates
    MyPoint2D q0 = currentSection.projectOnto(p0);
    // image pixel coordinates
    MyPoint2D q1 = currentSection.projectOnto(p1);
    // image pixel coordinates
    MyPoint2D q2 = currentSection.projectOnto(p2);
    // Shift projected points if required so that all points are as plotted:
    // TODO: perhaps there is an easier way to extract information from what is saved by the 2D viewing panel.
    double sx = controller.getShiftingX();
    double sy = controller.getShiftingY();
    if (!e0) {
        q0.plus(sx, sy);
    }
    if (!e1) {
        q1.plus(sx, sy);
    }
    if (!e2) {
        q2.plus(sx, sy);
    }
    // Calculate the baricentric coordinates in the 2D projection with shifted points:
    Bary2D bary = new Bary2D(q0, q1, q2);
    bary.calculate(p);
    // Check if the pixel is inside the projected triangle:
    if (!bary.inOrOn(0.1)) {
        // 10% tolerance
        if (verbose) {
            controller.clearCurrentFacet();
            Dialogs.error(controller, "The node must be added inside the facet.", title());
        }
        return null;
    }
    // Check for a SnapshotSection:
    boolean isSnapshot = (currentSection instanceof SnapshotSection);
    // if ( !isSnapshot && !section.canAddNodesOnSection() ) {
    if (!isSnapshot && !currentSection.canAddNodesOnSection()) {
        if (verbose) {
            controller.clearCurrentFacet();
            Dialogs.error(controller, "Nodes can not be added to the section.", title());
        }
        return null;
    }
    // Check if the nodes are all in the same group:
    // boolean sameGroup = true;
    // Group group = currentFacet.getNode(0).getGroup();
    // for (int i=1 ; i<3 ; i++ ) {
    // if ( currentFacet.getNode(i).getGroup() != group ) {
    // sameGroup = false;
    // break;
    // }
    // }
    // if (!sameGroup) { group = currentGroup; } // not all in the same group so add to the currentGroup
    // Create a new node at the cursor position and in the appropriate section and group:
    // Node newNode;
    // if (!isSnapshot && onSection && sameSection) { // create an on-section node on the same section
    // // Add new 2D point to the section:
    // MyPoint2D newPoint = p.deepCopy();
    // newNode = new NodeOnSection(newPoint,section,group);
    // } else { // create an off-section node
    // // Warn user:
    // if (!isSnapshot && verbose && controller.getShowConfirmationDialogs()) {
    // String message = "WARNING! That node must be added as an off-section node. Do you want to continue?";
    // int response = Dialogs.yesno(controller,message,title());
    // if ( response != Dialogs.YES_OPTION ) {
    // controller.clearCurrentFacet();
    // return null;
    // }
    // }
    // Interpolate the new 3D point using the unshifted points:
    // (the barycentric coordinates from above are used
    // but the interpolation uses unshifted points;
    // this effectively removes the shifting effect)
    double x = bary.interpolate(p0.getX(), p1.getX(), p2.getX());
    double y = bary.interpolate(p0.getY(), p1.getY(), p2.getY());
    double z = bary.interpolate(p0.getZ(), p1.getZ(), p2.getZ());
    MyPoint3D newPoint = new MyPoint3D(x, y, z);
    // newNode = new NodeOffSection(newPoint,section,group);
    return new NodeOffSection(newPoint, currentSection, currentGroup);
// }
// return newNode;
}
Also used : Group(facetmodeller.groups.Group) Bary2D(geometry.Bary2D) Node(facetmodeller.plc.Node) NodeOffSection(facetmodeller.plc.NodeOffSection) Section(facetmodeller.sections.Section) SnapshotSection(facetmodeller.sections.SnapshotSection) NodeOffSection(facetmodeller.plc.NodeOffSection) NodeOnSection(facetmodeller.plc.NodeOnSection) MyPoint2D(geometry.MyPoint2D) SnapshotSection(facetmodeller.sections.SnapshotSection) MyPoint3D(geometry.MyPoint3D) Facet(facetmodeller.plc.Facet)

Aggregations

Group (facetmodeller.groups.Group)37 Node (facetmodeller.plc.Node)16 GroupVector (facetmodeller.groups.GroupVector)14 Section (facetmodeller.sections.Section)14 Facet (facetmodeller.plc.Facet)11 NodeOnSection (facetmodeller.plc.NodeOnSection)11 NodeVector (facetmodeller.plc.NodeVector)11 FacetVector (facetmodeller.plc.FacetVector)9 NodeOffSection (facetmodeller.plc.NodeOffSection)9 MyPoint3D (geometry.MyPoint3D)9 Color (java.awt.Color)8 MyPoint2D (geometry.MyPoint2D)7 Region (facetmodeller.plc.Region)6 SnapshotSection (facetmodeller.sections.SnapshotSection)6 File (java.io.File)6 PLC (facetmodeller.plc.PLC)5 ImageCrossSection (facetmodeller.sections.ImageCrossSection)5 NoImageCrossSection (facetmodeller.sections.NoImageCrossSection)5 NoImageDepthSection (facetmodeller.sections.NoImageDepthSection)5 SectionVector (facetmodeller.sections.SectionVector)5