1

When creating a mesh, some faces are not added and return a UniqueSharedVertex error. I don't understand why this happens. Visually, it seems perfectly valid to add a face in the given location.

For example, in the mesh below, I can't add a triangular face between vertices [25, 51, 52].

Why does this happen and what can I do to fix this?

QGIS version: 3.22.3

enter image description here

Code to reproduce issue:

from uuid import uuid4

from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsMesh, QgsMeshLayer, QgsPoint, QgsProject, QgsProviderRegistry, QgsProcessingUtils, QgsVectorLayer

ELEVATION_POINTS = [
    (91742.960662,439528.079357,0.5),
    (91733.92261,439529.209114,0.5),
    (91733.3684,439529.250096,0.5),
    (91732.812883,439529.234943,0.5),
    (91732.261731,439529.16381,0.5),
    (91731.72057,439529.037421,0.5),
    (91731.194926,439528.857069,0.5),
    (91730.690165,439528.624593,0.5),
    (91730.21144,439528.342368,0.5),
    (91729.763638,439528.013275,0.5),
    (91729.351332,439527.640673,0.5),
    (91727.851332,439526.140673,0.5),
    (91727.340419,439525.551586,0.5),
    (91725.840419,439523.551586,0.5),
    (91725.55606,439523.133735,0.5),
    (91725.311261,439522.691544,0.5),
    (91725.108087,439522.228748,0.5),
    (91724.948256,439521.749255,0.5),
    (91724.833117,439521.257114,0.5),
    (91724.763643,439520.756482,0.5),
    (91724.740419,439520.251586,0.5),
    (91724.740419,439516.218347,0.5),
    (91724.740419,439514.021549,0.5),
    (91724.740419,439511.275031,0.5),
    (91713.858254,439509.134278,0.5),
    (91742.836627,439527.08708,0),
    (91733.798575,439528.216836,0),
    (91733.345131,439528.250367,0),
    (91732.890617,439528.237969,0),
    (91732.439674,439528.179769,0),
    (91731.996906,439528.07636,0),
    (91731.566834,439527.928799,0),
    (91731.153847,439527.738592,0),
    (91730.762163,439527.507681,0),
    (91730.39578,439527.238422,0),
    (91730.058439,439526.933566,0),
    (91728.558439,439525.433566,0),
    (91728.140419,439524.951586,0),
    (91726.640419,439522.951586,0),
    (91726.407762,439522.609708,0),
    (91726.207471,439522.247915,0),
    (91726.041239,439521.869264,0),
    (91725.910468,439521.476951,0),
    (91725.816263,439521.074291,0),
    (91725.75942,439520.664683,0),
    (91725.740419,439520.251586,0),
    (91725.740419,439515.841793,0),
    (91725.740419,439514.021549,0),
    (91725.740419,439510.452587,0),
    (91725.413774,439510.388329,0),
    (91714.051276,439508.153083,0),
    (91741.720315,439518.156579,0),
    (91734.930691,439519.005282,0),
    (91734.740419,439518.751586,0),
    (91734.740419,439514.021549,0),
    (91734.740419,439513.087094,0),
    (91734.740419,439506.751586,0),
    (91734.718684,439506.309834,0),
    (91734.653688,439505.87235,0),
    (91734.54606,439505.443359,0),
    (91734.396839,439505.027006,0),
    (91734.207466,439504.627312,0),
    (91733.979772,439504.24814,0),
    (91733.715954,439503.89315,0),
    (91733.418563,439503.565774,0),
    (91733.090471,439503.269172,0),
    (91732.734847,439503.006211,0),
    (91732.355127,439502.779431,0),
    (91731.954978,439502.591022,0),
    (91731.538266,439502.442805,0),
    (91731.109017,439502.336211,0),
    (91727.150971,439501.557579,0),
    (91715.788472,439499.322333,0),
    (91741.59628,439517.164301,0.5),
    (91735.740419,439517.896283,0.5),
    (91735.740419,439514.021549,0.5),
    (91735.740419,439512.86109,0.5),
    (91735.740419,439506.751586,0.5),
    (91735.713854,439506.211667,0.5),
    (91735.634415,439505.676964,0.5),
    (91735.502869,439505.152642,0.5),
    (91735.320488,439504.643766,0.5),
    (91735.089032,439504.155252,0.5),
    (91734.810739,439503.691818,0.5),
    (91734.488296,439503.257942,0.5),
    (91734.124818,439502.857815,0.5),
    (91733.723816,439502.495303,0.5),
    (91733.289164,439502.173906,0.5),
    (91732.825062,439501.89673,0.5),
    (91732.335991,439501.666453,0.5),
    (91731.826677,439501.485298,0.5),
    (91731.302039,439501.355016,0.5),
    (91727.343993,439500.576384,0.5),
    (91715.981494,439498.341139,0.5)
]

triangles = {
    0: [0, 25, 1],
    1: [1, 25, 26],
    2: [1, 26, 27],
    3: [1, 27, 2],
    4: [2, 27, 28],
    5: [2, 28, 3],
    6: [3, 28, 29],
    7: [3, 29, 4],
    8: [4, 29, 30],
    9: [4, 30, 5],
    10: [5, 30, 31],
    11: [5, 31, 32],
    12: [5, 32, 6],
    13: [6, 32, 33],
    14: [6, 33, 7],
    15: [7, 33, 34],
    16: [7, 34, 8],
    17: [8, 34, 35],
    18: [8, 35, 9],
    19: [9, 35, 10],
    20: [10, 35, 36],
    21: [10, 36, 37],
    22: [10, 37, 11],
    23: [11, 37, 12],
    24: [12, 37, 38],
    25: [12, 38, 39],
    26: [12, 39, 40],
    27: [12, 40, 13],
    28: [13, 40, 41],
    29: [13, 41, 14],
    30: [14, 41, 42],
    31: [14, 42, 15],
    32: [15, 42, 43],
    33: [15, 43, 44],
    34: [15, 44, 16],
    35: [16, 44, 45],
    36: [16, 45, 17],
    37: [17, 45, 18],
    38: [18, 45, 19],
    39: [19, 45, 20],
    40: [20, 45, 46],
    41: [20, 46, 21],
    42: [21, 46, 47],
    43: [21, 47, 22],
    44: [22, 47, 48],
    45: [22, 48, 49],
    46: [22, 49, 23],
    47: [23, 49, 50],
    48: [23, 50, 24],
    49: [25, 51, 52],
    50: [25, 52, 26],
    51: [26, 52, 53],
    52: [26, 53, 27],
    53: [27, 53, 28],
    54: [28, 53, 29],
    55: [29, 53, 30],
    56: [30, 53, 31],
    57: [31, 53, 32],
    58: [32, 53, 33],
    59: [33, 53, 54],
    60: [33, 54, 34],
    61: [34, 54, 35],
    62: [35, 54, 55],
    63: [35, 55, 36],
    64: [36, 55, 37],
    65: [37, 55, 38],
    66: [38, 55, 39],
    67: [39, 55, 40],
    68: [40, 55, 41],
    69: [41, 55, 42],
    70: [42, 55, 56],
    71: [42, 56, 43],
    72: [43, 56, 57],
    73: [43, 57, 44],
    74: [44, 57, 58],
    75: [44, 58, 45],
    76: [45, 58, 59],
    77: [45, 59, 60],
    78: [45, 60, 61],
    79: [45, 61, 62],
    80: [45, 62, 63],
    81: [45, 63, 64],
    82: [45, 64, 65],
    83: [45, 65, 66],
    84: [45, 66, 67],
    85: [45, 67, 68],
    86: [45, 68, 46],
    87: [46, 68, 69],
    88: [46, 69, 70],
    89: [46, 70, 47],
    90: [47, 70, 71],
    91: [47, 71, 48],
    92: [48, 71, 49],
    93: [49, 71, 72],
    94: [49, 72, 50],
    95: [51, 73, 74],
    96: [51, 74, 52],
    97: [52, 74, 53],
    98: [53, 74, 75],
    99: [53, 75, 76],
    100: [53, 76, 54],
    101: [54, 76, 55],
    102: [55, 76, 77],
    103: [55, 77, 78],
    104: [55, 78, 79],
    105: [55, 79, 80],
    106: [55, 80, 56],
    107: [56, 80, 81],
    108: [56, 81, 57],
    109: [57, 81, 58],
    110: [58, 81, 82],
    111: [58, 82, 59],
    112: [59, 82, 83],
    113: [59, 83, 60],
    114: [60, 83, 84],
    115: [60, 84, 61],
    116: [61, 84, 85],
    117: [61, 85, 62],
    118: [62, 85, 63],
    119: [63, 85, 86],
    120: [63, 86, 64],
    121: [64, 86, 87],
    122: [64, 87, 65],
    123: [65, 87, 88],
    124: [65, 88, 66],
    125: [66, 88, 89],
    126: [66, 89, 67],
    127: [67, 89, 68],
    128: [68, 89, 90],
    129: [68, 90, 69],
    130: [69, 90, 91],
    131: [69, 91, 70],
    132: [70, 91, 92],
    133: [70, 92, 71],
    134: [71, 92, 93],
    135: [71, 93, 72]
}

# Create mesh
provider_meta = QgsProviderRegistry.instance().providerMetadata('mdal')
mesh = QgsMesh()
temp_mesh_filename = f"{uuid4()}.nc"
temp_mesh_fullpath = QgsProcessingUtils.generateTempFilename(temp_mesh_filename)
mesh_format = 'Ugrid'
crs = QgsCoordinateReferenceSystem()
provider_meta.createMeshData(mesh, temp_mesh_fullpath, mesh_format, crs)
mesh_layer = QgsMeshLayer(temp_mesh_fullpath, 'my_mesh', 'mdal')

# add points to mesh
transform = QgsCoordinateTransform()
mesh_layer.startFrameEditing(transform)
editor = mesh_layer.meshEditor()
points = [QgsPoint(x, y, z) for x, y, z in ELEVATION_POINTS]
points_added = editor.addPointsAsVertices(points, 0.0000001)
for vertex_indices in triangles.values():
    error = editor.addFace(vertex_indices)
    if error.errorType != 0:
        print(error.errorType)
        print(error.elementIndex)
mesh_layer.commitFrameEditing(transform, continueEditing=False)
QgsProject.instance().addMapLayer(mesh_layer)

1 Answer 1

2

For mesh editing, some topological rules have to be respected. One of them is the impossibility that two faces share only one vertices. This error uniqueSharedVertex comes from here. To avoid this error you have to take care of the order of added faces. In this case, another solution could to add a face with 4 vertices and then split it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.