Search in sources :

Example 1 with NodeOffSection

use of facetmodeller.plc.NodeOffSection in project facetmodeller by pglelievre.

the class ModelManager method readSessionInformation.

@Override
public String readSessionInformation(BufferedReader reader, boolean merge) {
    // I need to construct new objects as I read the file:
    PLC plc2 = new PLC();
    SectionVector sections2 = new SectionVector();
    GroupVector groups2 = new GroupVector();
    // Read the number of dimensions, nodes, facets, regions, samples and groups:
    String textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Reading number of dimensions, etc.";
    }
    int ndim, nnodes, nregions, nfacets, nsections, ngroups;
    textLine = textLine.trim();
    String[] ss = textLine.split("[ ]+");
    if (ss.length < 6) {
        return "Not enough values on number of dimensions etc. line.";
    }
    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) {
        return "Parsing number of dimensions, etc.";
    }
    // Check ndim:
    if (ndim != numberOfDimensions()) {
        return "Incorrect number of dimensions.";
    }
    // ---------- IN THE FIRST PASS I READ ALL INFORMATION OTHER THAN ID'S AND CREATE NEW OBJECTS ----------
    // Skip the commented start of node definitions:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of node definitions.";
    }
    // Loop over each node:
    for (int i = 0; i < nnodes; i++) {
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading node ID etc. line.";
        }
        textLine = textLine.trim();
        ss = textLine.split("[ ]+");
        if (ss.length < 2) {
            return "Not enough values on node ID etc. line.";
        }
        int nodeType;
        boolean bmarker = false;
        try {
            int nodeID = Integer.parseInt(ss[0].trim());
            if (nodeID != i) {
                return "Unmatched node ID";
            }
            nodeType = Integer.parseInt(ss[1].trim());
            if (ss.length > 2) {
                bmarker = Boolean.parseBoolean(ss[2].trim());
            }
        } catch (NumberFormatException e) {
            return "Parsing node ID etc.";
        }
        Node node;
        switch(nodeType) {
            case Node.NODE_ON_SECTION:
                node = new NodeOnSection();
                break;
            case Node.NODE_OFF_SECTION:
                node = new NodeOffSection();
                break;
            default:
                return "Unmatched node type.";
        }
        if (node == null) {
            return "Unexpected empty new Node created.";
        }
        // Set the node boundary marker:
        node.setBoundaryMarker(bmarker);
        // Read the additional information for the node (depends on the node type):
        String msg = node.readSessionInformation(reader, merge);
        if (msg != null) {
            return "Reading information for node " + i + "." + System.lineSeparator() + msg.trim();
        }
        plc2.addNode(node);
    }
    // Loop over each facet:
    for (int i = 0; i < nfacets; i++) {
        // Add a new empty facet to the plc (these facets are filled later):
        plc2.addFacet(new Facet());
    }
    // Skip the commented start of region definitions:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of region definitions.";
    }
    // Loop over each region:
    for (int i = 0; i < nregions; i++) {
        // Make a new region object:
        Region region = new Region();
        // Read the region information:
        region.readSessionInformation(reader, merge);
        // Add the region to the plc:
        // section and group membership will be added later
        plc2.addRegion(region);
    }
    // Skip the commented start of section definitions:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of section definitions.";
    }
    for (int i = 0; i < nsections; i++) {
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Skipping start of ith section definition.";
        }
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading section type.";
        }
        textLine = textLine.trim();
        int sectionType;
        try {
            sectionType = Integer.parseInt(textLine);
        } catch (NumberFormatException e) {
            return "Parsing section type.";
        }
        Section section;
        switch(sectionType) {
            case Section.SECTION_IMAGE_CROSS:
                section = new ImageCrossSection();
                break;
            case Section.SECTION_IMAGE_DEPTH:
                section = new ImageDepthSection();
                break;
            case Section.SECTION_NOIMAGE_CROSS:
                section = new NoImageCrossSection();
                break;
            case Section.SECTION_NOIMAGE_DEPTH:
                section = new NoImageDepthSection();
                break;
            case Section.SECTION_SNAPSHOT:
                section = new SnapshotSection();
                break;
            case Section.SECTION_TOPO:
                // replacement for obsolete TopoSection (.node and .ele file will be read later)
                section = new NoImageDepthSection(true);
                break;
            default:
                return "Unmatched section type.";
        }
        if (section == null) {
            return "Unexpected empty new Section created.";
        }
        String msg = section.readSessionInformation(reader, merge);
        if (msg != null) {
            return "Reading information for section " + i + "." + System.lineSeparator() + msg.trim();
        }
        sections2.add(section);
    }
    // Skip the commented start of group definitions:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of group definitions.";
    }
    // Loop over each group:
    for (int i = 0; i < ngroups; i++) {
        // Create a new group object:
        Group group = new Group();
        // Read the group information:
        String msg = group.readSessionInformation(reader, merge);
        if (msg != null) {
            return "Reading information for group" + i + "." + System.lineSeparator() + msg.trim();
        }
        // Add the group to the list of groups:
        groups2.add(group);
    }
    // ---------- IN THE SECOND PASS I READ THE ID'S AND SET THE CROSS-LINKAGES ----------
    // Skip the commented start of node linkages:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of node linkages.";
    }
    // Loop over each node:
    for (int i = 0; i < nnodes; i++) {
        Node node = plc2.getNode(i);
        // The node gets linked to the facets in the loop over each facet below.
        // Read the section id and group id:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading node section and group IDs line.";
        }
        textLine = textLine.trim();
        ss = textLine.split("[ ]+");
        if (ss.length < 2) {
            return "Not enough values on node section and group IDs line.";
        }
        // section and group id
        int sid, gid;
        try {
            // converts to integer
            sid = Integer.parseInt(ss[0].trim());
            // converts to integer
            gid = Integer.parseInt(ss[1].trim());
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
            return "Parsing node section and group IDs.";
        }
        // Cross-link the node and section:
        node.setSection(sections2.get(sid));
        sections2.get(sid).addNode(node);
        // Cross-link the node and group:
        node.setGroup(groups2.get(gid));
        groups2.get(gid).addNode(node);
    }
    // Skip the commented start of facet linkages:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of facet linkages.";
    }
    // Loop over each facet:
    for (int i = 0; i < nfacets; i++) {
        Facet facet = plc2.getFacet(i);
        // Read the node id's and link those nodes to the facet:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading facet node IDs line.";
        }
        textLine = textLine.trim();
        ss = textLine.split("[ ]+");
        if (ss.length < 1) {
            return "No values on facet node IDs line.";
        }
        // number of nodes
        int n;
        try {
            // converts to integer
            n = Integer.parseInt(ss[0].trim());
        } catch (NumberFormatException e) {
            return "Parsing facet node length.";
        }
        if (ss.length < n + 1) {
            return "Not enough values on facet node IDs line.";
        }
        for (int j = 0; j < n; j++) {
            // node id
            int id;
            try {
                // converts to integer
                id = Integer.parseInt(ss[j + 1].trim());
            } catch (NumberFormatException e) {
                return "Parsing facet node ID.";
            }
            // Cross-link the facet and node:
            facet.addNode(plc2.getNode(id));
            plc2.getNode(id).addFacet(facet);
        }
        // Read the section id's:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading facet section ID line.";
        }
        /*
            textLine = textLine.trim();
            ss = textLine.split("[ ]+");
            if (ss.length<1) { return "No values on facet section ID line."; }
            try {
                n = Integer.parseInt(ss[0].trim()); // converts to integer
            } catch (NumberFormatException e) { return "Parsing facet section length."; }
            if ( ss.length < n+1 ) { return "Not enough values on facet section ID line."; }
            for (int j=0 ; j<n ; j++ ) {
                int id;
                try {
                    id = Integer.parseInt(ss[j+1].trim()); // converts to integer
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { return "Parsing facet section ID."; }
                // Cross-link the facet and section:
//                    facet.addSection( sections.get(id) ); // no longer necessary because facet sections defined by the facet nodes
                sections2.get(id).addFacet(facet);
            }
            */
        // Read the group id and boundary marker:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading facet group ID and boundary marker line.";
        }
        textLine = textLine.trim();
        ss = textLine.split("[ ]+");
        if (ss.length < 1) {
            return "No values on facet group ID and boundary marker line.";
        }
        int id;
        boolean bmarker = false;
        try {
            // converts to integer
            id = Integer.parseInt(ss[0].trim());
            if (ss.length > 1) {
                bmarker = Boolean.parseBoolean(ss[1].trim());
            }
        } catch (NumberFormatException e) {
            return "Parsing facet group ID and boundary marker line.";
        }
        // Set the facet boundary marker:
        facet.setBoundaryMarker(bmarker);
        // Cross-link the facet and group:
        facet.setGroup(groups2.get(id));
        groups2.get(id).addFacet(facet);
    }
    // Skip the commented start of region linkages:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of region linkages.";
    }
    // Loop over each region:
    for (int i = 0; i < nregions; i++) {
        Region region = plc2.getRegion(i);
        // Read the section id:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading region section ID line.";
        }
        textLine = textLine.trim();
        ss = textLine.split("[ ]+");
        if (ss.length < 1) {
            return "No values on region section ID line.";
        }
        int id;
        try {
            // converts to integer
            id = Integer.parseInt(ss[0].trim());
        } catch (NumberFormatException e) {
            return "Parsing region section ID.";
        }
        // Cross-link the region and section:
        region.setSection(sections2.get(id));
        sections2.get(id).addRegion(region);
        // Read the group id and link that group to the node:
        textLine = FileUtils.readLine(reader);
        if (textLine == null) {
            return "Reading region group ID line.";
        }
        textLine = textLine.trim();
        ss = textLine.split("[ ]+");
        if (ss.length < 1) {
            return "No values on region group ID line.";
        }
        try {
            // converts to integer
            id = Integer.parseInt(ss[0].trim());
        } catch (NumberFormatException e) {
            return "Parsing region group ID.";
        }
        // Cross-link the region and group:
        region.setGroup(groups2.get(id));
        // groups.get(id).setRegion(region);
        groups2.get(id).addRegion(region);
    }
    // ---------- Read the VOI information: ----------
    // Skip the commented start of the VOI information:
    textLine = FileUtils.readLine(reader);
    if (textLine == null) {
        return "Skipping start of VOI information.";
    }
    // Read the VOI:
    VOI voi2 = new VOI();
    String msg = voi2.readSessionInformation(reader, merge);
    if (msg != null) {
        if (msg.startsWith("Null")) {
            // Assume encountered line with "null" in it:
            voi2 = null;
        } else {
            return msg;
        }
    }
    // If overwriting then set existing information to new information, otherwise combine the information:
    if (merge) {
        // Don't change the VOI!
        plc.addAll(plc2);
        groups.addAll(groups2);
    } else {
        // these aren't necessary because the garbage collection should deal with them
        plc.clear();
        groups.clear();
        voi = voi2;
        plc = plc2;
        groups = groups2;
    }
    if (merge && ndim == 3) {
        sections.addAll(sections2);
    } else {
        sections.clear();
        sections = sections2;
    }
    // Return successfully:
    return null;
}
Also used : SectionVector(facetmodeller.sections.SectionVector) Group(facetmodeller.groups.Group) NoImageDepthSection(facetmodeller.sections.NoImageDepthSection) PLC(facetmodeller.plc.PLC) Node(facetmodeller.plc.Node) NodeOffSection(facetmodeller.plc.NodeOffSection) NodeOnSection(facetmodeller.plc.NodeOnSection) NoImageCrossSection(facetmodeller.sections.NoImageCrossSection) NodeOffSection(facetmodeller.plc.NodeOffSection) NoImageDepthSection(facetmodeller.sections.NoImageDepthSection) Section(facetmodeller.sections.Section) ImageCrossSection(facetmodeller.sections.ImageCrossSection) SnapshotSection(facetmodeller.sections.SnapshotSection) NodeOnSection(facetmodeller.plc.NodeOnSection) ImageDepthSection(facetmodeller.sections.ImageDepthSection) NoImageDepthSection(facetmodeller.sections.NoImageDepthSection) ImageDepthSection(facetmodeller.sections.ImageDepthSection) GroupVector(facetmodeller.groups.GroupVector) NoImageCrossSection(facetmodeller.sections.NoImageCrossSection) ImageCrossSection(facetmodeller.sections.ImageCrossSection) Region(facetmodeller.plc.Region) NoImageCrossSection(facetmodeller.sections.NoImageCrossSection) SnapshotSection(facetmodeller.sections.SnapshotSection) Facet(facetmodeller.plc.Facet)

Example 2 with NodeOffSection

use of facetmodeller.plc.NodeOffSection in project facetmodeller by pglelievre.

the class SessionLoader method loadSessionAscii2.

private static boolean loadSessionAscii2(FacetModeller controller, File file, boolean merge) {
    int loadVersion = 2;
    // 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;
    // 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;
            }
            boolean isTopo;
            // initialization of z is needed to avoid compiler warning
            double x, y, z = 0.0;
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 4);
            try {
                isTopo = Boolean.parseBoolean(ss[0].trim());
                // converts to Double
                x = Double.parseDouble(ss[1].trim());
                // converts to Double
                y = Double.parseDouble(ss[2].trim());
                if (isTopo) {
                    z = Double.parseDouble(ss[3].trim());
                }
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            // Add a new node to the plc:
            if (isTopo) {
                plc.addNode(new NodeOffSection(x, y, z));
            } else {
                plc.addNode(new NodeOnSection(x, y));
            }
        // section and group membership will be added later
        }
        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 and the isControl information:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            double x, y;
            boolean isCon;
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 4);
            // Try parsing coordinates (must be able to do this):
            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;
            }
            // Check for iscontrol information:
            if (ss.length < 3) {
                isCon = false;
            } else {
                if (ss[2].trim().isEmpty()) {
                    // missing from file (old version of session saver was used)
                    isCon = false;
                } else {
                    // converts to Boolean
                    isCon = Boolean.parseBoolean(ss[2].trim());
                }
            }
            // Add a new region to the plc:
            // section and group membership will be added later
            plc.addRegion(new Region(isCon, x, y));
        }
        if (!ok) {
            break;
        }
        for (int i = 0; i < nsections; i++) {
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            int sectionType;
            try {
                sectionType = Integer.parseInt(textLine);
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            Section section = null;
            switch(sectionType) {
                case 1:
                    // Read the file name:
                    textLine = FileUtils.readLine(reader);
                    if (textLine == null) {
                        ok = false;
                        break;
                    }
                    textLine = textLine.trim();
                    File imageFile;
                    if (textLine.startsWith("null")) {
                        imageFile = null;
                    } else {
                        try {
                            URI uri = new URI(textLine);
                            // image file or .node file
                            imageFile = new File(uri);
                        } catch (URISyntaxException e) {
                            ok = false;
                            break;
                        }
                    }
                    // Make a new HasImage object associated with the file:
                    section = new ImageCrossSection(imageFile);
                    break;
                case 3:
                    // Read the section name:
                    textLine = FileUtils.readLine(reader);
                    if (textLine == null) {
                        ok = false;
                        break;
                    }
                    String name = textLine.trim();
                    // Read the image height:
                    textLine = FileUtils.readLine(reader);
                    if (textLine == null) {
                        ok = false;
                        break;
                    }
                    textLine = textLine.trim();
                    int height;
                    try {
                        height = Integer.parseInt(textLine);
                    } catch (NumberFormatException e) {
                        ok = false;
                        break;
                    }
                    // Read the image color:
                    Color color;
                    try {
                        // parse from RGB string
                        color = new Color(Integer.parseInt(textLine.trim()));
                    } catch (NumberFormatException e) {
                        ok = false;
                        break;
                    }
                    section = new NoImageCrossSection(name, color);
                    break;
                case 2:
                    File nodeFile = null;
                    File eleFile = null;
                    // Read the node file name:
                    textLine = FileUtils.readLine(reader);
                    if (textLine == null) {
                        ok = false;
                        break;
                    }
                    textLine = textLine.trim();
                    if (textLine.startsWith("null")) {
                        nodeFile = null;
                    } else {
                        URI uri = null;
                        try {
                            uri = new URI(textLine);
                        } catch (URISyntaxException e) {
                            ok = false;
                        }
                        if (!ok) {
                            break;
                        }
                        try {
                            // image file or .node file
                            nodeFile = new File(uri);
                        } catch (IllegalArgumentException e) {
                            ok = false;
                        }
                    }
                    if (!ok) {
                        break;
                    }
                    // Read the ele file name:
                    textLine = FileUtils.readLine(reader);
                    if (textLine == null) {
                        ok = false;
                        break;
                    }
                    textLine = textLine.trim();
                    if (textLine.startsWith("null")) {
                        eleFile = null;
                    } else {
                        try {
                            URI uri = new URI(textLine);
                            // image file or .node file
                            eleFile = new File(uri);
                        } catch (URISyntaxException e) {
                            ok = false;
                        }
                    }
                    if (!ok) {
                        break;
                    }
                    // formerly a TopoSection
                    section = new NoImageDepthSection(nodeFile, eleFile);
                    break;
                default:
                    ok = false;
                    break;
            }
            if (!ok) {
                break;
            }
            if (section == null) {
                ok = false;
                break;
            }
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 2);
            Dir3D sliceDirection;
            try {
                // converts to integer
                int idir = Integer.parseInt(ss[0].trim());
                sliceDirection = Dir3D.fromInt(idir);
            } catch (NumberFormatException e) {
                ok = false;
                break;
            }
            if (!ok) {
                break;
            }
            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;
            }
            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));
            }
            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 and group id:
            textLine = FileUtils.readLine(reader);
            if (textLine == null) {
                ok = false;
                break;
            }
            textLine = textLine.trim();
            ss = textLine.split("[ ]+", 3);
            // section and group id
            int sid, gid;
            try {
                // converts to integer
                sid = Integer.parseInt(ss[0].trim());
                // converts to integer
                gid = Integer.parseInt(ss[1].trim());
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                ok = false;
                break;
            }
            // Cross-link the node and section:
            node.setSection(sections.get(sid));
            sections.get(sid).addNode(node);
            // Cross-link the node and group:
            node.setGroup(groups.get(gid));
            groups.get(gid).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 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) NoImageDepthSection(facetmodeller.sections.NoImageDepthSection) PLC(facetmodeller.plc.PLC) Node(facetmodeller.plc.Node) URISyntaxException(java.net.URISyntaxException) NodeOnSection(facetmodeller.plc.NodeOnSection) URI(java.net.URI) MyPoint3D(geometry.MyPoint3D) NoImageCrossSection(facetmodeller.sections.NoImageCrossSection) Facet(facetmodeller.plc.Facet) Color(java.awt.Color) NodeOffSection(facetmodeller.plc.NodeOffSection) 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 3 with NodeOffSection

use of facetmodeller.plc.NodeOffSection in project facetmodeller by pglelievre.

the class ChangeNodeSectionClickTask method mouseClick.

@Override
public void mouseClick(MyPoint2D p) {
    // Check for the required information:
    if (!check()) {
        return;
    }
    if (p == null) {
        return;
    }
    // Get the current section:
    Section currentSection = controller.getSelectedCurrentSection();
    // Calculate the closest node to the clicked point:
    if (!controller.calculateClosestNode(p)) {
        return;
    }
    // just in case the closestNode object gets nullified by a mouse move (not sure if that is possible but better safe than sorry)
    Node node = controller.getClosestNode();
    if (node == null) {
        return;
    }
    // Check if the node is already a 3D node in the current section:
    if (node.getSection() == currentSection && node.isOff()) {
        return;
    }
    // Nullify temporary objects:
    // (or else the old closest node point will be painted)
    controller.clearClosestNode();
    // Check if node is on or off section (it will be changed to off section regardless):
    if (!node.isOff()) {
        // Ask user for confirmation and calculate 3D point:
        int response = Dialogs.confirm(controller, "The node will be changed to a 3D off-section node.", title());
        if (response != Dialogs.OK_OPTION) {
            return;
        }
    }
    MyPoint3D p3 = node.getPoint3D();
    if (p3 == null) {
        return;
    }
    p3 = p3.deepCopy();
    // Create a new off-section node object:
    Node newNode = new NodeOffSection(p3, currentSection, node.getGroup());
    // Replace the old node with the new node:
    MergeNodesCommand com = new MergeNodesCommand(controller.getModelManager(), node, newNode, title());
    com.execute();
    controller.undoVectorAdd(com);
    // Enable or disable menu items:
    controller.checkItemsEnabled();
    // Repaint:
    controller.redraw();
}
Also used : MergeNodesCommand(facetmodeller.commands.MergeNodesCommand) Node(facetmodeller.plc.Node) NodeOffSection(facetmodeller.plc.NodeOffSection) Section(facetmodeller.sections.Section) NodeOffSection(facetmodeller.plc.NodeOffSection) MyPoint3D(geometry.MyPoint3D)

Example 4 with NodeOffSection

use of facetmodeller.plc.NodeOffSection in project facetmodeller by pglelievre.

the class ChangeNodeCoordsClickTask method mouseClick.

@Override
public void mouseClick(MyPoint2D p) {
    // Check for the required information:
    if (!check()) {
        return;
    }
    if (p == null) {
        return;
    }
    // Calculate the closest node to the clicked point:
    if (!controller.calculateClosestNode(p)) {
        return;
    }
    // just in case the closestNode object gets nullified by a mouse move (not sure if that is possible but better safe than sorry)
    Node node = controller.getClosestNode();
    if (node == null) {
        return;
    }
    // Nullify temporary objects:
    // (or else the old closest node point will be painted)
    controller.clearClosestNode();
    // Check for on-section node:
    boolean do3D = true;
    if (!node.isOff()) {
        // Ask user how to continue:
        int response = Dialogs.question(controller, "What coordinates do you want to specify for this on-section node?", title(), "3D spatial", "2D pixel", "Cancel");
        if (response == Dialogs.CANCEL_OPTION) {
            return;
        }
        do3D = (response == Dialogs.YES_OPTION);
        if (do3D) {
            response = Dialogs.confirm(controller, "The node will be changed to a 3D off-section node.", title());
            if (response != Dialogs.OK_OPTION) {
                return;
            }
        }
    }
    // Do whatever is required:
    Command com;
    if (do3D) {
        // it's an off-section node, or we're converting an no-section node to an off-section node, and we're changing the 3D spatial coordinates
        // Get the node's existing 3D coordinates:
        MyPoint3D p3 = node.getPoint3D();
        // Ask the user for the new node coordinates:
        String message = "You must enter three numeric values separated by spaces. Please try again.";
        String prompt = "Enter the new 3D spatial coordinates (x y z) for the node, separated by spaces:";
        String input = Dialogs.input(controller, prompt, title(), p3.toString());
        // user cancelled
        if (input == null) {
            return;
        }
        input = input.trim();
        String[] s;
        s = input.split("[ ]+");
        if (s.length != 3) {
            Dialogs.error(controller, message, title());
            return;
        }
        double x, y, z;
        try {
            x = Double.parseDouble(s[0].trim());
            y = Double.parseDouble(s[1].trim());
            z = Double.parseDouble(s[2].trim());
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
            Dialogs.error(controller, message, title());
            return;
        }
        // Create a new 3D point object:
        p3 = new MyPoint3D(x, y, z);
        if (node.isOff()) {
            // Change the 3D spatial coordinates of the off-section node:
            com = new ChangeNodeCoordinateCommand((NodeOffSection) node, p3);
            com.execute();
        } else {
            // Create a new off-section node object at the specified 3D spatial coordinates, attached to the same section and group as the old node:
            Node newNode = new NodeOffSection(p3, node.getSection(), node.getGroup());
            // Replace the old node with the new node:
            com = new MergeNodesCommand(controller.getModelManager(), node, newNode, title());
            com.execute();
        }
    } else {
        // it's an on-section node and we're changing the 2D pixel coordinates
        // Get the node's existing 2D coordinates:
        MyPoint2D p2 = node.getPoint2D();
        // Ask the user for the new node coordinates:
        String message = "You must enter two numeric values separated by spaces. Please try again.";
        String prompt = "Enter the new 2D pixel coordinates (x y) for the node, separated by spaces:";
        String input = Dialogs.input(controller, prompt, title(), p2.toString());
        // user cancelled
        if (input == null) {
            return;
        }
        input = input.trim();
        String[] s;
        s = input.split("[ ]+");
        if (s.length != 2) {
            Dialogs.error(controller, message, title());
            return;
        }
        double x, y;
        try {
            x = Double.parseDouble(s[0].trim());
            y = Double.parseDouble(s[1].trim());
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
            Dialogs.error(controller, message, title());
            return;
        }
        // Create a new 2D point object:
        p2 = new MyPoint2D(x, y);
        // Change the 2D pixel coordinates of the on-section node:
        com = new ChangeNodeCoordinateCommand((NodeOnSection) node, p2);
        com.execute();
    }
    // Add the command to the undo information:
    controller.undoVectorAdd(com);
    // Enable or disable menu items:
    controller.checkItemsEnabled();
    // Repaint:
    controller.redraw();
}
Also used : Node(facetmodeller.plc.Node) NodeOffSection(facetmodeller.plc.NodeOffSection) ChangeNodeCoordinateCommand(facetmodeller.commands.ChangeNodeCoordinateCommand) MyPoint2D(geometry.MyPoint2D) NodeOnSection(facetmodeller.plc.NodeOnSection) MyPoint3D(geometry.MyPoint3D) MergeNodesCommand(facetmodeller.commands.MergeNodesCommand) Command(facetmodeller.commands.Command) MergeNodesCommand(facetmodeller.commands.MergeNodesCommand) ChangeNodeCoordinateCommand(facetmodeller.commands.ChangeNodeCoordinateCommand)

Example 5 with NodeOffSection

use of facetmodeller.plc.NodeOffSection 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

NodeOffSection (facetmodeller.plc.NodeOffSection)9 Node (facetmodeller.plc.Node)8 MyPoint3D (geometry.MyPoint3D)8 Section (facetmodeller.sections.Section)7 Group (facetmodeller.groups.Group)6 NodeOnSection (facetmodeller.plc.NodeOnSection)6 MyPoint2D (geometry.MyPoint2D)5 Facet (facetmodeller.plc.Facet)4 AddNodeCommand (facetmodeller.commands.AddNodeCommand)3 SnapshotSection (facetmodeller.sections.SnapshotSection)3 MergeNodesCommand (facetmodeller.commands.MergeNodesCommand)2 GroupVector (facetmodeller.groups.GroupVector)2 PLC (facetmodeller.plc.PLC)2 Region (facetmodeller.plc.Region)2 ImageCrossSection (facetmodeller.sections.ImageCrossSection)2 NoImageCrossSection (facetmodeller.sections.NoImageCrossSection)2 NoImageDepthSection (facetmodeller.sections.NoImageDepthSection)2 SectionVector (facetmodeller.sections.SectionVector)2 AddNodeCommandVector (facetmodeller.commands.AddNodeCommandVector)1 ChangeNodeCoordinateCommand (facetmodeller.commands.ChangeNodeCoordinateCommand)1