Search in sources :

Example 1 with SnapshotSection

use of facetmodeller.sections.SnapshotSection 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 SnapshotSection

use of facetmodeller.sections.SnapshotSection in project facetmodeller by pglelievre.

the class SectionImagePanel method paintComponent.

/**
 * Paints graphics on the panel.
 * @param g Graphics context in which to draw.
 */
@Override
public void paintComponent(Graphics g) {
    // Paint background:
    super.paintComponent(g);
    // Clear the lists of painted nodes, node points, etc.:
    paintedNodes.clear();
    paintedNodePoints.clear();
    paintedFacets.clear();
    paintedFacetCentroids.clear();
    paintedRegions.clear();
    paintedRegionPoints.clear();
    // Return if no sections exist:
    if (!controller.hasSections()) {
        return;
    }
    // Return if no current section exists:
    Section currentSection = controller.getSelectedCurrentSection();
    if (currentSection == null) {
        return;
    }
    // Set some local variables:
    int shiftX = controller.getShiftingX();
    int shiftY = controller.getShiftingY();
    int mode = controller.getClickMode();
    boolean showImage = controller.getShowImage();
    boolean showImageOutline = controller.getShowImageOutline();
    // Get the other sections being displayed (if any exist):
    SectionVector otherSections;
    // if (controller.getShowOther()) {
    otherSections = controller.getSelectedOtherSections();
    // } else {
    // otherSections = null;
    // }
    // Determine the facets to paint:
    FacetVector facetsToPaint = new FacetVector();
    GroupVector groups = controller.getSelectedFacetGroups();
    if (groups != null) {
        // for (int i=0 ; i<groups.size(); i++ ) { // loop over every selected facet group
        for (int i = (groups.size() - 1); i >= 0; i--) {
            // loop over every selected facet group in reverse order
            FacetVector facets = groups.get(i).getFacets();
            for (int j = 0; j < facets.size(); j++) {
                // loop over every facet in the ith group
                Facet facet = facets.get(j);
                // Make sure there is more than one node in the facet:
                // number of nodes in the facet
                int nn = facet.size();
                if (nn <= 1) {
                    continue;
                }
                // Make sure the facet belongs to no other sections than those selected:
                boolean ok = true;
                for (int k = 0; k < nn; k++) {
                    // loop over each node in the facet
                    // Check if the kth node in the facet should be plotted: it must be either
                    // 1) in the current section, which must be calibrated if its the topo section; OR
                    // 2) in one of the other selected sections, which must be calibrated:
                    // kth node in the facet
                    Node node = facet.getNode(k);
                    // section for the kth node
                    Section s = node.getSection();
                    if (s.equals(currentSection)) {
                        // node is in current section
                        if (node.getPoint2D() == null) {
                            // node not paintable
                            ok = false;
                            break;
                        }
                    } else {
                        // node not in current section
                        if (otherSections == null || !otherSections.contains(s) || !s.isCalibrated()) {
                            // (calibration required for projection)
                            ok = false;
                            break;
                        }
                        if (node.getPoint3D() == null) {
                            // node not paintable
                            ok = false;
                            break;
                        }
                    }
                }
                if (!ok) {
                    continue;
                }
                // Add the facet to the list of facets to paint:
                facetsToPaint.add(facet);
            }
        }
    }
    /*
        // Determine the nodes to paint in the current section:
        NodeVector nodesToPaintCurrent = new NodeVector();
        NodeVector nodes = currentSection.getNodes();
        for (int i=0 ; i<nodes.size() ; i++ ) {
            Node node = nodes.get(i);
            // Make sure the node is in one of the selected groups to paint:
            if (!controller.isSelectedNodeGroup(node.getGroup())) { continue; } // cycle to next node
            // Make sure the node is paintable:
            if (node.getPoint2D()==null) { continue; } // cycle to next node
            // Add the node to the list of nodes to paint:
            nodesToPaintCurrent.add(node);
        }
        
        // Determine the nodes to paint in the other sections:
        NodeVector nodesToPaintOther = new NodeVector();
        if (otherSections!=null) {
            if (!otherSections.isEmpty()) {
                for (int j=0 ; j<otherSections.size() ; j++ ) {
                    // Skip the section if it is the current section:
                    Section s = otherSections.get(j);
                    if (s.equals(currentSection)) { continue; }
                    // Skip the section if it is not calibrated (calibration is required for projection):
                    if (!s.isCalibrated()) { continue; }
                    // Loop over the nodes:
                    nodes = s.getNodes();
                    for (int i=0 ; i<nodes.size() ; i++ ) {
                        Node node = nodes.get(i);
                        // Make sure the node is in one of the selected groups to paint:
                        if (!controller.isSelectedNodeGroup(node.getGroup())) { continue; } // cycle to next node
                        // Make sure the node is paintable:
                        if (node.getPoint3D()==null) { continue; } // cycle to next node
                        // Add the node to the list of nodes to paint:
                        nodesToPaintOther.add(node);
                    }
                }
            }
        }
        */
    // Determine the nodes to paint in the current and other sections:
    NodeVector nodesToPaint = new NodeVector();
    groups = controller.getSelectedNodeGroups();
    if (groups != null) {
        for (int i = (groups.size() - 1); i >= 0; i--) {
            // loop over every selected node group in reverse order
            NodeVector nodes = groups.get(i).getNodes();
            for (int j = 0; j < nodes.size(); j++) {
                // loop over every node in the ith group
                Node node = nodes.get(j);
                // Check if the node is in the current section:
                if (node.getSection() == currentSection) {
                    // cycle to next node
                    if (node.getPoint2D() == null) {
                        continue;
                    }
                    // Add the node to the list of nodes to paint and cycle to the next node:
                    nodesToPaint.add(node);
                    continue;
                }
                // Check if there are any other sections selected:
                if (otherSections == null) {
                    continue;
                }
                if (otherSections.isEmpty()) {
                    continue;
                }
                // Check if the node is in one of the other sections selected:
                Section s = node.getSection();
                if (!otherSections.contains(s)) {
                    continue;
                }
                // Skip the node if it's section is not calibrated (calibration is required for projection):
                if (!s.isCalibrated()) {
                    continue;
                }
                // cycle to next node
                if (node.getPoint3D() == null) {
                    continue;
                }
                // Add the node to the list of nodes to paint:
                nodesToPaint.add(node);
            }
        }
    }
    // Calculate the range, in image panel units, across which we'll need to plot:
    // will hold minimum coordinate values
    MyPoint2D p1 = MyPoint2D.zero();
    // will hold maximum coordinate values
    MyPoint2D p2 = new MyPoint2D(currentSection.getWidth(), currentSection.getHeight());
    RegionVector regions = currentSection.getRegions();
    if (!(currentSection instanceof SnapshotSection)) {
        for (int i = 0; i < facetsToPaint.size(); i++) {
            // loop over facets
            Facet facet = facetsToPaint.get(i);
            for (int j = 0; j < facet.size(); j++) {
                Node node = facet.getNode(j);
                MyPoint2D p = shiftNode(node, currentSection, shiftX, shiftY);
                if (p == null) {
                    // shouldn't happen, but I'll check for it anyway
                    facetsToPaint.remove(facet);
                    continue;
                }
                p1.min(p);
                p2.max(p);
            }
        }
        /*
            for (int i=0 ; i<nodesToPaintCurrent.size() ; i++ ) { // loop over nodes for current section
                Node node = nodesToPaintCurrent.get(i);
                MyPoint2D p = shiftNode(node,currentSection,shiftX,shiftY);
                if (p==null) { // shouldn't happen, but I'll check for it anyway
                    nodesToPaintCurrent.remove(node);
                    continue;
                }
                p1.min(p);
                p2.max(p);
            }
            for (int i=0 ; i<nodesToPaintOther.size() ; i++ ) { // loop over nodes for other section
                Node node = nodesToPaintOther.get(i);
                MyPoint2D p = shiftNode(node,currentSection,shiftX,shiftY);
                if (p==null) {  // shouldn't happen, but I'll check for it anyway
                    nodesToPaintOther.remove(node);
                    continue;
                }
                p1.min(p);
                p2.max(p);
            }
            */
        for (int i = 0; i < nodesToPaint.size(); i++) {
            // loop over nodes for other section
            Node node = nodesToPaint.get(i);
            MyPoint2D p = shiftNode(node, currentSection, shiftX, shiftY);
            if (p == null) {
                // shouldn't happen, but I'll check for it anyway
                nodesToPaint.remove(node);
                continue;
            }
            p1.min(p);
            p2.max(p);
        }
        if (controller.getShowRegions()) {
            for (int i = 0; i < regions.size(); i++) {
                // loop over regions
                Region region = regions.get(i);
                MyPoint2D p = region.getPoint2D();
                p1.min(p);
                p2.max(p);
            }
        }
    }
    // coordinate ranges
    MyPoint2D dp = MyPoint2D.minus(p2, p1);
    if (dp.min() <= 0.0) {
        return;
    }
    // Tightly fit the plot inside the panel but maintain aspect ratio:
    calculateTightFitTransform(p1, dp);
    boolean hasImage = currentSection.hasImage();
    if (hasImage && showImage) {
        // Get the current section image:
        BufferedImage image = currentSection.getImage();
        // Check the image exists:
        if (image != null) {
            // this may happen if the image file specified in a session file is not found
            // Calculate the tight fit transform:
            tightFitImage(g, image);
        }
    }
    // Get drawing styles for overlays:
    final int nodeWidth = controller.getPointWidth();
    final int edgeWidth = controller.getLineWidth();
    // centroids drawn half the node width
    final int centroidWidth = (int) Math.ceil(nodeWidth / 2.0);
    final float transparency = (float) controller.getTransparency();
    // transparency factor
    AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency);
    // final boolean showPickingRadius = controller.getShowPickingRadius();
    // final int pickingRadius = (int)( controller.getPickingRadius() * scaling ); // panel pixel width
    // The space-to-image transform may not be equidistance (may have different scalings for horizontal and vertical panel directions)
    // so, without knowing how best to show a picking radius in spatial units (it used to be in image pixel units but that caused some issues)
    // I have just gotten rid of the plotting of the picking radius.
    // Use Java2D graphics for overlays:
    Graphics2D g2 = (Graphics2D) g;
    g2.setComposite(composite);
    if (!hasImage || !showImage) {
        // section does not have an image or user does not want image shown
        double imageWidth = currentSection.getWidth();
        double imageHeight = currentSection.getHeight();
        double scaledWidth = scaling * imageWidth;
        double scaledHeight = scaling * imageHeight;
        Color col = currentSection.getColor();
        boolean ok = false;
        if (col == null) {
            col = getBackground();
        }
        // Plot a coloured rectangle:
        if (!hasImage && showImage) {
            // section does not have an image and user wants image shown
            g2.setColor(col);
            g2.fillRect((int) translation.getX(), (int) translation.getY(), (int) scaledWidth, (int) scaledHeight);
            ok = true;
        }
        // Plot outline of image:
        if (showImageOutline) {
            g2.setColor(Color.black);
            g2.drawRect((int) translation.getX(), (int) translation.getY(), (int) scaledWidth, (int) scaledHeight);
            ok = true;
        }
        // Make sure something is plotted:
        if (!ok) {
            col = getBackground();
            g2.setColor(col);
            g2.drawRect((int) translation.getX(), (int) translation.getY(), (int) scaledWidth, (int) scaledHeight);
        }
    }
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // line style
    g2.setStroke(new BasicStroke(edgeWidth));
    // Get number of dimensions:
    int ndim = controller.numberOfDimensions();
    // Get the drawing options for colouring the nodes and facets:
    int nodeColorBy = controller.getNodeColorBy();
    int facetColorBy = controller.getFacetColorBy();
    // Paint the patches, edges and centroids of the facets:
    for (int i = 0; i < facetsToPaint.size(); i++) {
        // loop over every facet that needs to be painted
        Facet facet = facetsToPaint.get(i);
        // Create a path around possibly shifted node coordinates:
        HasPathAndCentroid tmp = addFacetToPath(facet, currentSection, shiftX, shiftY);
        // shouldn't happen, but I'll check for it anyway
        if (tmp == null) {
            continue;
        }
        // Close the path:
        tmp.path.closePath();
        // Transform the path from section to panel coordinates:
        tmp.path.transform(imageToPanel);
        // Determine the painting colour for the facet:
        Color col = getFacetPaintingColor(facetColorBy, facet);
        // Greate a filled, semi-transparent polygonal patch:
        if (facet.size() > 2) {
            // (no patch exists for edge-element facets)
            g2.setPaint(col);
            g2.fill(tmp.path);
        }
        // Draw the path (i.e. the facet edges):
        if (ndim == 3) {
            col = controller.getEdgeColor();
        }
        // else {
        // color = facet.getColor();
        // }
        g2.setPaint(col);
        g2.draw(tmp.path);
        // Draw a small point at the facet centroid:
        if (ndim == 2) {
            col = controller.getEdgeColor();
        }
        g2.setPaint(col);
        // filled
        PaintingUtils.paintPoint(g2, imageToPanel, tmp.centroid, centroidWidth, true);
        // Add to the list of painted facets and centroids:
        paintedFacets.add(facet);
        paintedFacetCentroids.add(tmp.centroid);
    }
    // Paint the edges of the current facet being defined:
    BasicStroke dashedStroke = new BasicStroke(edgeWidth + 1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, (float) 10.0, new float[] { 2f, 16f }, (float) 0.0);
    Facet currentFacet = controller.getCurrentFacet();
    if (currentFacet != null) {
        // Create a path around the node coordinates:
        if (currentFacet.size() > 1) {
            HasPathAndCentroid tmp = addFacetToPath(currentFacet, currentSection, shiftX, shiftY);
            if (tmp != null) {
                // May want to close the path:
                if (mode == ClickModeManager.MODE_DEFINE_TRI_FACETS || mode == ClickModeManager.MODE_ADD_NODES_IN_FACETS) {
                    tmp.path.closePath();
                }
                // Transform the path from section to panel coordinates:
                tmp.path.transform(imageToPanel);
                // Draw the path:
                // line style changed
                g2.setStroke(dashedStroke);
                g2.setPaint(controller.getDefineFacetEdgeColor());
                g2.draw(tmp.path);
                // line style reset
                g2.setStroke(new BasicStroke(edgeWidth));
            }
        }
    }
    // Paint the edges of the facet closest to the cursor position:
    if (mouseInside && currentFacet == null) {
        Facet facet = controller.getClosestFacet();
        if (facet != null) {
            // Create a path around the node coordinates:
            if (facet.size() > 1) {
                HasPathAndCentroid tmp = addFacetToPath(facet, currentSection, shiftX, shiftY);
                if (tmp != null) {
                    // null shouldn't happen, but I'll check for it anyway
                    // Close the path:
                    tmp.path.closePath();
                    // Transform the path from section to panel coordinates:
                    tmp.path.transform(imageToPanel);
                    // Draw the path:
                    // line style changed
                    g2.setStroke(dashedStroke);
                    g2.setPaint(controller.getDefineFacetEdgeColor());
                    g2.draw(tmp.path);
                    // line style reset
                    g2.setStroke(new BasicStroke(edgeWidth));
                }
            }
        }
    }
    // Paint the nodes for the current and other sections:
    for (int i = 0; i < nodesToPaint.size(); i++) {
        Node node = nodesToPaint.get(i);
        // Check if the node is on the current section:
        MyPoint2D p;
        boolean filled;
        if (node.getSection() == currentSection) {
            // node on current section
            p = node.getPoint2D();
            // nodes on current section are painted filled
            filled = true;
        } else {
            // node is on another section so it may be shifted
            p = shiftNode(node, currentSection, shiftX, shiftY);
            // nodes on other sections are painted unfilled
            filled = false;
        }
        // shouldn't happen, but I'll check for it anyway
        if (p == null) {
            continue;
        }
        // Determine the painting colour for the node:
        Color col = getNodePaintingColor(nodeColorBy, node);
        // Paint the node:
        g2.setPaint(col);
        PaintingUtils.paintPoint(g2, imageToPanel, p, nodeWidth, filled);
        // Add to the list of painted nodes and node points:
        paintedNodes.add(node);
        paintedNodePoints.add(p);
    }
    // Paint the region points for the current section:
    if (controller.getShowRegions()) {
        for (int i = 0; i < regions.size(); i++) {
            Region region = regions.get(i);
            MyPoint2D p = region.getPoint2D();
            // shouldn't happen, but I'll check for it anyway
            if (p == null) {
                continue;
            }
            g2.setPaint(region.getColor());
            PaintingUtils.paintPoint(g2, imageToPanel, p, nodeWidth, true);
            // Add to the list of painted regions and region points:
            paintedRegions.add(region);
            paintedRegionPoints.add(p);
        }
    }
    // Paint circles around the nodes of the current facet being defined:
    if (currentFacet != null) {
        if (mode == ClickModeManager.MODE_DEFINE_TRI_FACETS) {
            g2.setPaint(Color.WHITE);
        } else {
            g2.setPaint(Color.BLACK);
        }
        for (int i = 0; i < currentFacet.size(); i++) {
            Node node = currentFacet.getNode(i);
            // Only paint the node if it is in the current or other sections:
            Section s = node.getSection();
            if (s.equals(currentSection) || (otherSections != null && otherSections.contains(s))) {
                MyPoint2D p = shiftNode(node, currentSection, shiftX, shiftY);
                if (p == null) {
                    continue;
                }
                PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
            }
        }
    }
    // Paint circles around the nodes of the facet closest to the cursor position:
    if (mouseInside && currentFacet == null) {
        Facet facet = controller.getClosestFacet();
        if (facet != null) {
            // Paint circles around the node coordinates:
            g2.setPaint(Color.WHITE);
            for (int i = 0; i < facet.size(); i++) {
                Node node = facet.getNode(i);
                MyPoint2D p = shiftNode(node, currentSection, shiftX, shiftY);
                if (p == null) {
                    continue;
                }
                PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
            }
            // If in MODE_INFO or MODE_REVERSE_FACETS then paint a thicker circle around the first and possibly second node:
            if (mode == ClickModeManager.MODE_INFO || mode == ClickModeManager.MODE_REVERSE_FACETS) {
                Node node = facet.getNode(0);
                MyPoint2D p = shiftNode(node, currentSection, shiftX, shiftY);
                if (p != null) {
                    g2.setStroke(new BasicStroke(ndim * edgeWidth));
                    PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
                    // line style reset
                    g2.setStroke(new BasicStroke(edgeWidth));
                }
                if (ndim == 3) {
                    node = facet.getNode(1);
                    p = shiftNode(node, currentSection, shiftX, shiftY);
                    if (p != null) {
                        g2.setStroke(new BasicStroke(2 * edgeWidth));
                        PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
                        // line style reset
                        g2.setStroke(new BasicStroke(edgeWidth));
                    }
                }
            }
        // // Paint a white circle around the centroid:
        // if (showPickingRadius) {
        // HasPathAndCentroid tmp = addFacetToPath(facet,currentSection,shiftX,shiftY);
        // if (tmp!=null) { // shouldn't happen, but I'll check for it anyway
        // tmp.path.closePath();
        // tmp.path.transform(imageToPanel);
        // PaintingUtils.paintPoint(g2,imageToPanel,tmp.centroid,2*centroidWidth,false); // not filled
        // g2.setPaint(Color.BLACK);
        // PaintingUtils.paintPoint(g2,imageToPanel,tmp.centroid,2*pickingRadius,false); // not filled
        // }
        // }
        }
    }
    // boolean isTopo = currentSection.isTopo();
    if (hasImage && currentSection.isCalibrated()) {
        g2.setPaint(controller.getCalibrationColor());
        PaintingUtils.paintPoint(g2, imageToPanel, currentSection.getClicked1(), nodeWidth, false);
        PaintingUtils.paintPoint(g2, imageToPanel, currentSection.getClicked2(), nodeWidth, false);
    }
    // // If calibrating then draw large cross-hairs across the image:
    // if ( controller.getMode() == FacetModeller.MODE_CALIBRATE ) {
    // p = new MyPoint2D(getMousePosition());
    // g2.setColor(Color.white);
    // g2.setStroke(new BasicStroke(1)); // line style reset
    // double x = p.getX();
    // double y = p.getY();
    // double w = getWidth()/2.0;
    // g2.drawLine((int)(x-w),(int)y,(int)(x+w),(int)y);
    // g2.drawLine((int)x,(int)(y-w),(int)x,(int)(y+w));
    // }
    // Paint a large circle around the origin node of the same colour as the node if it is present in those painted:
    Node originNode = controller.getOriginNode3D();
    if (originNode != null) {
        int i = paintedNodes.indexOf(originNode);
        if (i >= 0) {
            MyPoint2D p = shiftNode(originNode, currentSection, shiftX, shiftY);
            if (p != null) {
                // shouldn't happen, but I'll check for it anyway
                // Determine the painting colour for the origin node:
                Color col = getNodePaintingColor(nodeColorBy, originNode);
                // Paint the origin node:
                g2.setPaint(col);
                // g2.setStroke(new BasicStroke(2*edgeWidth));
                PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
            // g2.setStroke(new BasicStroke(edgeWidth)); // line style reset
            }
        }
    }
    // Paint a white circle around the current node (e.g. being moved or first in an edge being flipped):
    Node currentNode = controller.getCurrentNode();
    if (currentNode != null) {
        MyPoint2D p = shiftNode(currentNode, currentSection, shiftX, shiftY);
        if (p != null) {
            g2.setPaint(Color.WHITE);
            // not filled
            PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
        }
    }
    // Paint a white circle around the node closest to the cursor position (e.g. or that being moved):
    MyPoint2D p = controller.getClosestNodePoint();
    if (mouseInside && p != null) {
        g2.setPaint(Color.WHITE);
        // not filled
        PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
    // if (showPickingRadius) {
    // g2.setPaint(Color.BLACK);
    // PaintingUtils.paintPoint(g2,imageToPanel,p,2*pickingRadius,false); // not filled
    // }
    }
    // Paint a white circle around the region closest to the cursor position:
    p = controller.getClosestRegionPoint();
    if (mouseInside && p != null) {
        g2.setPaint(Color.WHITE);
        // not filled
        PaintingUtils.paintPoint(g2, imageToPanel, p, 2 * nodeWidth, false);
    // if (showPickingRadius) {
    // g2.setPaint(Color.BLACK);
    // PaintingUtils.paintPoint(g2,imageToPanel,p,2*pickingRadius,false); // not filled
    // }
    }
}
Also used : SectionVector(facetmodeller.sections.SectionVector) FacetVector(facetmodeller.plc.FacetVector) Node(facetmodeller.plc.Node) Section(facetmodeller.sections.Section) SnapshotSection(facetmodeller.sections.SnapshotSection) MyPoint2D(geometry.MyPoint2D) GroupVector(facetmodeller.groups.GroupVector) NodeVector(facetmodeller.plc.NodeVector) RegionVector(facetmodeller.plc.RegionVector) Region(facetmodeller.plc.Region) SnapshotSection(facetmodeller.sections.SnapshotSection) Facet(facetmodeller.plc.Facet)

Example 3 with SnapshotSection

use of facetmodeller.sections.SnapshotSection in project facetmodeller by pglelievre.

the class ResetSnapshotSectionMenuTask method execute.

@Override
public void execute() {
    // Check for the required information:
    if (!check()) {
        return;
    }
    // Get the projection information from the 3D viewer:
    Projector3D proj = controller.getProjector3D().deepCopy();
    // Reset the projector information:
    Section currentSection = controller.getSelectedCurrentSection();
    // cast
    SnapshotSection snapshotSection = (SnapshotSection) currentSection;
    snapshotSection.setProjector(proj);
    // Reset the zooming on the 2D view:
    controller.zoomReset2D();
    // Repaint:
    controller.redraw();
}
Also used : Projector3D(facetmodeller.gui.Projector3D) Section(facetmodeller.sections.Section) SnapshotSection(facetmodeller.sections.SnapshotSection) SnapshotSection(facetmodeller.sections.SnapshotSection)

Example 4 with SnapshotSection

use of facetmodeller.sections.SnapshotSection in project facetmodeller by pglelievre.

the class CopySectionMenuTask method execute.

@Override
public void execute() {
    // Check for the required information:
    if (!check()) {
        return;
    }
    // Store the selections in the group selector objects for use later:
    int ind = controller.getSelectedCurrentSectionIndex();
    SectionVector selectedOtherSections = controller.getSelectedOtherSections();
    // Deep copy the basic section information:
    Section currentSection = controller.getSelectedCurrentSection();
    Section newSection = currentSection.copySection();
    // this should only happen for a SnapshotSection (redundant check)
    if (newSection == null) {
        return;
    }
    // Deep copy the on-section nodes but don't link to any facets or sections:
    NodeVector newNodes = currentSection.getNodes().deepCopyNodesOnPointAndGroup();
    // Link the nodes to the new section:
    newNodes.setSection(newSection);
    // Add the nodes to the plc using the AddNodeCommand:
    ModelManager modelManager = controller.getModelManager();
    for (int i = 0; i < newNodes.size(); i++) {
        AddNodeCommand addNodeCommand = new AddNodeCommand(modelManager, newNodes.get(i), title());
        addNodeCommand.execute();
    }
    // Add the new section to the section vector:
    controller.addSection(newSection);
    // Update the clickable lists:
    controller.updateSectionSelectors();
    // Reset the selections:
    // positions the current section to the new section
    controller.setSelectedCurrentSectionIndex(ind + 1);
    if (selectedOtherSections != null) {
        selectedOtherSections.add(newSection);
        controller.setSelectedOtherSections(selectedOtherSections);
    }
    // Tell the controller that the section selection has changed:
    controller.sectionSelectionChanged(true);
}
Also used : SectionVector(facetmodeller.sections.SectionVector) AddNodeCommand(facetmodeller.commands.AddNodeCommand) NodeVector(facetmodeller.plc.NodeVector) ModelManager(facetmodeller.ModelManager) Section(facetmodeller.sections.Section) SnapshotSection(facetmodeller.sections.SnapshotSection)

Example 5 with SnapshotSection

use of facetmodeller.sections.SnapshotSection in project facetmodeller by pglelievre.

the class OriginNode3DClickTask method mouseClick.

@Override
public void mouseClick(MyPoint2D p) {
    // Check for the required information:
    if (!check() || p == null) {
        controller.endOriginNode3D();
        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();
    // MyPoint2D point = closestNodePoint;
    // // Convert points from image pixel coordinates to spatial coordinates:
    // MyPoint3D p3 = currentSection.imageToSpace(p);
    // MyPoint3D point3 = currentSection.imageToSpace(point);
    // // Check the distance is small enough: // already done in calculateClosestNode
    // Double d = point3.distanceToPoint(p3);
    // if (d>pickingDistance) { return; }
    // Set the origin:
    controller.setOrigin3D(node);
    // Redraw the 3D view:
    controller.redraw3D();
    if (!(controller.getSelectedCurrentSection() instanceof SnapshotSection)) {
        // Set the viewing centre:
        controller.setOrigin2D(p);
        // Redraw the 2D view:
        controller.redraw2D();
    }
    // Finish by clearing the click mode:
    controller.endOriginNode3D();
}
Also used : Node(facetmodeller.plc.Node) SnapshotSection(facetmodeller.sections.SnapshotSection)

Aggregations

SnapshotSection (facetmodeller.sections.SnapshotSection)9 Section (facetmodeller.sections.Section)8 Node (facetmodeller.plc.Node)6 Facet (facetmodeller.plc.Facet)5 Group (facetmodeller.groups.Group)4 GroupVector (facetmodeller.groups.GroupVector)3 FacetVector (facetmodeller.plc.FacetVector)3 NodeOffSection (facetmodeller.plc.NodeOffSection)3 NodeOnSection (facetmodeller.plc.NodeOnSection)3 NodeVector (facetmodeller.plc.NodeVector)3 SectionVector (facetmodeller.sections.SectionVector)3 MyPoint2D (geometry.MyPoint2D)3 ModelManager (facetmodeller.ModelManager)2 AddNodeCommand (facetmodeller.commands.AddNodeCommand)2 Projector3D (facetmodeller.gui.Projector3D)2 Region (facetmodeller.plc.Region)2 MyPoint3D (geometry.MyPoint3D)2 AddFacetCommandVector (facetmodeller.commands.AddFacetCommandVector)1 AddNodeCommandVector (facetmodeller.commands.AddNodeCommandVector)1 PLC (facetmodeller.plc.PLC)1