How to resolve the algorithm Faces from a mesh step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Faces from a mesh step by step in the Wren programming language

Table of Contents

Problem Statement

A mesh defining a surface has uniquely numbered vertices, and named, simple-polygonal faces described usually by an ordered list of edge numbers going around the face,

For example: External image of two faces Rough textual version without edges: any of all the rotations of those ordered vertices. of their rotations. Let's call the above the perimeter format as it traces around the perimeter. A separate algorithm returns polygonal faces consisting of a face name and an unordered set of edge definitions for each face. ascending order. For example face A could be described by the edges (1, 11), (7, 11), and (1, 7) (The order of each vertex number in an edge is ascending, but the order in which the edges are stated is arbitrary). Similarly face B could be described by the edges (11, 23), (1, 17), (17, 23), and (1, 11) in arbitrary order of the edges. Let's call this second format the edge format.

  1. Write a routine to check if two perimeter formatted faces have the same perimeter. Use it to check if the following pairs of perimeters are the same:
  2. Write a routine and use it to transform the following faces from edge to perimeter format. Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Faces from a mesh step by step in the Wren programming language

Source code in the wren programming language

import "./sort" for Sort
import "./seq" for Lst
import "./fmt" for Fmt
 
// Check two perimeters are equal.
var perimEqual = Fn.new { |p1, p2|
    var le = p1.count
    if (le != p2.count) return false
    for (p in p1) {
        if (!p2.contains(p)) return false
    }
    // use copy to avoid mutating 'p1'
    var c = p1.toList
    for (r in 0..1) {
        for (i in 0...le) {
            if (Lst.areEqual(c, p2)) return true
            // do circular shift to right
            Lst.rshift(c)
        }
        // now process in opposite direction
        Lst.reverse(c) // reverses 'c' in place
    }
    return false
}
 
var faceToPerim = Fn.new { |face|
    // use copy to avoid mutating 'face'
    var le = face.count
    if (le == 0) return []
    var edges = List.filled(le, null)
    for (i in 0...le) {
        // check edge pairs are in correct order
        if (face[i][1] <= face[i][0]) return []
        edges[i] = [face[i][0], face[i][1]]
    }
    // sort edges in ascending order
    var cmp = Fn.new { |e1, e2|
        if (e1[0] != e2[0]) {
            return (e1[0] - e2[0]).sign
        }
        return (e1[1] - e2[1]).sign
    }
    Sort.insertion(edges, cmp)
    var first = edges[0][0]
    var last  = edges[0][1]
    var perim = [first, last]
    // remove first edge
    edges.removeAt(0)
    le = le - 1
    while (le > 0) {
        var i = 0
        var outer = false
        var cont = false
        for (e in edges) {
            var found = false
            if (e[0] == last) {
                perim.add(e[1])
                last = e[1]
                found = true
            } else if (e[1] == last) {
                perim.add(e[0])
                last = e[0]  
                found = true
            }
            if (found) {
                // remove i'th edge
                edges.removeAt(i)
                le = le - 1
                if (last == first) {
                    if (le == 0) {
                        outer = true
                        break
                    } else {
                        return []
                    }
                }
                cont = true
                break
            }
            i = i + 1
        }
        if (outer && !cont) break
    }
    return perim[0..-2]
}
 
System.print("Perimeter format equality checks:")
var areEqual = perimEqual.call([8, 1, 3], [1, 3, 8])
System.print("  Q == R is %(areEqual)")
areEqual = perimEqual.call([18, 8, 14, 10, 12, 17, 19], [8, 14, 10, 12, 17, 19, 18])
System.print("  U == V is %(areEqual)")
var e = [[7, 11], [1, 11], [1, 7]]
var f = [[11, 23], [1, 17], [17, 23], [1, 11]]
var g = [[8, 14], [17, 19], [10, 12], [10, 14], [12, 17], [8, 18], [18, 19]]
var h = [[1, 3], [9, 11], [3, 11], [1, 11]]
System.print("\nEdge to perimeter format translations:")
var i = 0
for (face in [e, f, g, h]) {
    var perim = faceToPerim.call(face)
    if (perim.isEmpty) {
        Fmt.print("  $c => Invalid edge format", i + 69) // 'E' is ASCII 69
    } else {
        Fmt.print("  $c => $n", i + 69, perim)
    }
}

  

You may also check:How to resolve the algorithm Repeat a string step by step in the Lang programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Draco programming language
You may also check:How to resolve the algorithm Date format step by step in the 8th programming language
You may also check:How to resolve the algorithm Minimum positive multiple in base 10 using only 0 and 1 step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Logical operations step by step in the PHP programming language