Search in sources :

Example 1 with FlipEdgeCommand

use of facetmodeller.commands.FlipEdgeCommand in project facetmodeller by pglelievre.

the class FlipEdgeClickTask 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;
    }
    Node node2 = controller.getClosestNode();
    // Check if we need to start, continue or stop the edge flipping operation:
    Node currentNode = controller.getCurrentNode();
    if (currentNode == null) {
        // need to start fresh
        controller.setCurrentNode(node2);
    } else {
        // first node was already selected
        // Find the triangular facets containing the edge (triangular facets that contain the two selected nodes):
        // will hold the triangular facets containing the edge
        FacetVector edgeFacets = new FacetVector();
        // will hold the 4 nodes
        NodeVector nodes = new NodeVector();
        nodes.add(currentNode);
        nodes.add(node2);
        // the facets for the first node
        FacetVector facets1 = currentNode.getFacets();
        for (int i1 = 0; i1 < facets1.size(); i1++) {
            // loop over each facet for the first node
            Facet f1 = facets1.get(i1);
            // Skip non-triangular facets:
            if (f1.size() != 3) {
                continue;
            }
            // Check if the facet contains the second node:
            if (f1.containsNode(node2)) {
                // Add the facet to the list:
                edgeFacets.add(f1);
            }
        }
        // Nullify the temporary object before any dialogs can launch:
        controller.clearCurrentNode();
        // Check that the two nodes define an edge:
        if (edgeFacets.isEmpty()) {
            Dialogs.error(controller, "Those nodes don't define an edge.", title());
            return;
        }
        // Check for a boundary edge:
        if (edgeFacets.size() == 1) {
            Dialogs.error(controller, "Boundary edges can not be flipped.", title());
            return;
        }
        // Check for a triple point:
        if (edgeFacets.size() != 2) {
            Dialogs.error(controller, "Triple point edges can not be flipped.", title());
            return;
        }
        // TODO: check for a convex situation
        // Determine the two other nodes to use:
        // duplicates are not added in this call
        nodes.addAll(edgeFacets.get(0).getNodes());
        // duplicates are not added in this call
        nodes.addAll(edgeFacets.get(1).getNodes());
        // Check I did it correctly:
        if (nodes.size() != 4) {
            Dialogs.error(controller, "That edge can not be flipped.", title());
            return;
        }
        // Figure out which group to use:
        Group group = edgeFacets.get(0).getGroup();
        if (edgeFacets.get(1).getGroup() != group) {
            // the two facets belong to different groups
            group = controller.getSelectedCurrentGroup();
        }
        // Define two new facets:
        Facet newFacet1 = new Facet(group);
        newFacet1.addNode(nodes.get(0));
        newFacet1.addNode(nodes.get(2));
        newFacet1.addNode(nodes.get(3));
        Facet newFacet2 = new Facet(group);
        newFacet2.addNode(nodes.get(1));
        newFacet2.addNode(nodes.get(2));
        newFacet2.addNode(nodes.get(3));
        FacetVector newFacets = new FacetVector();
        newFacets.add(newFacet1);
        newFacets.add(newFacet2);
        // Perform the edge flip (add the new facets, delete the existing facets):
        FlipEdgeCommand com = new FlipEdgeCommand(controller.getModelManager(), edgeFacets, newFacets);
        com.execute();
        controller.undoVectorAdd(com);
    }
    // Repaint:
    controller.redraw();
}
Also used : Group(facetmodeller.groups.Group) NodeVector(facetmodeller.plc.NodeVector) FacetVector(facetmodeller.plc.FacetVector) Node(facetmodeller.plc.Node) FlipEdgeCommand(facetmodeller.commands.FlipEdgeCommand) Facet(facetmodeller.plc.Facet)

Aggregations

FlipEdgeCommand (facetmodeller.commands.FlipEdgeCommand)1 Group (facetmodeller.groups.Group)1 Facet (facetmodeller.plc.Facet)1 FacetVector (facetmodeller.plc.FacetVector)1 Node (facetmodeller.plc.Node)1 NodeVector (facetmodeller.plc.NodeVector)1