1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 int *
 make_mesh_connectivity(int edge_ct_x,   /*number of edges in X direction*/
                        int edge_ct_y    /*number of edges in Y direction*/
                        )
 {
    SAF_ENTER(make_mesh_connectivity, NULL);

    int *node_ids;                 /* The node to element connectivity. */
    int node_ct_y = edge_ct_y + 1; /* Number of nodes in y. */
    int tri_ct = 0,                /* Number of triangles in mesh. */
        conn_idx = 0;              /* Index into connectivity array. */
    int i, j;

    /* Preconditions */

    SAF_REQUIRE(edge_ct_x > 0, SAF_LOW_CHK_COST, NULL,
                _saf_errmsg("there must be at least 1 edge in the x direction"));
    SAF_REQUIRE(edge_ct_y > 0, SAF_LOW_CHK_COST, NULL,
                _saf_errmsg("there must be at least 1 edge in the y direction"));

    /* Body */

    /* allocate node_ids as single index array,
     * but treat as node_ids[edge_ct_x][edge_ct_y][3]; */
    node_ids = (int*)malloc(edge_ct_x*edge_ct_y*2*3*sizeof(int));

    for(i = 0; i < edge_ct_x; i++)
    {
       for(j = 0; j < edge_ct_y; j++)
       {
          /* Each (i,j) pair is the lower-left-hand node in
           * a quad containing two triangles. */

          /* Connectivity for lower triangle in quad,
           * follow right-hand rule (ccw) starting
           * in llh corner of quad. */

          node_ids[conn_idx++] = NODE_ID(i,j);
          node_ids[conn_idx++] = NODE_ID(i+1, j);
          node_ids[conn_idx++] = NODE_ID(i, j+1);

          tri_ct++;


          /* Connectivity for upper triangle in quad,
           * follow right-hand rule (ccw) starting
           * in urh corner of quad. */

          node_ids[conn_idx++] = NODE_ID(i+1,j);
          node_ids[conn_idx++] = NODE_ID(i+1, j+1);
          node_ids[conn_idx++] = NODE_ID(i, j+1);

          tri_ct++;
       }
    }

    /* Postconditions */

    /* Exit */
   SAF_LEAVE(node_ids);
 }