next up previous contents
Next: C. Uvodne datoteke modelirnika Up: B. Programska koda - NURBS-i Previous: B.1 Strukture v knji¾nici

B.2 Funkcija za izgradnjo kocke

 

Koda prikazuje potek izgradnje kocke z uporabo funkcij knji¾nice NURBS-ov. Izpis programske kode je realni primer (vzet iz programske kode modelirnika) izgradnje kocke s pomoèjo predstavitve z mejami in uporabe knji¾nice NURBS. Vse spremenljivke in komentarji so napisani v angle¹èini. Razlog za to je zelo preprost: angle¹¹èina je svetovni jezik, ki z bliskovitim ¹irjenjem Inteneta dobiva èedalje veèji pomen. Izvorna koda modelirnika bo prosto dostopna na Internetu in bi bila uporaba slovenskih spremenljivk in komentarjev nesmiselna. Razumevanje programa, katerega avtor je nekdo drug, je ¾e v angle¹èini te¾ko, kaj ¹ele, èe je napisan v nekem tujem jeziku.

int 
CubeBrep( Brep *brep, double x, double y, double z, double a )
{
  int vert_edge[12][2] = { {0,1}, {0,3}, {1,2}, {2,3}, {2,5}, {3,4},
                           {0,6}, {1,7}, {6,7}, {7,5}, {5,4}, {4,6}};
  int face_edge[6][4] = { {0,1,3,2}, {3,5,10,4}, {6,11,5,1},
                          {7,2,4,9}, {0,6,8,7}, {9,10,11,8}};
  int face_vert[6][4] = { {0,1,2,3}, {4,3,2,5}, {6,0,3,4},
                          {1,7,5,2}, {6,7,1,0}, {7,6,4,5}};
  int i, j ;
  faceBrep *face;
  edgeBrep edges[12], *edge;
  pointListElement *vertex[8];
  Ppoint3 p[8];
  

  /* ****************************/
  /* first create Brep for cube */
  /* ****************************/
  /* allocate space for 6 faces and B-rep structure */
  AllocateFaces( brep, 6 );

  for( i=0, face=brep->face ; i < 6 ; i++, face=face->nextFace ){
    /* allocate all faces; all faces have 4 edges */
    AllocateEdges( face, 4 );
  }

  for( i=0 ; i < 12 ; i++ ){
    /* assigns 12 new adges for the cube */
    edges[i].curve = NewCurveID();
  }

  for( i=0 ; i < 8 ; i++ ){
    /* assigns 8 new vertex for the cube */
    vertex[i] = NewPointID();
  }

  /* create B-rep of cube by connecting vertex, edges and faces together */
  for( i=0 ; i < 12 ; i++ ){
    /* connect edges and vertexes (see doc/cube.gif)*/
    edges[i].startPoint = vertex[ vert_edge[i][0] ];
    edges[i].endPoint = vertex[ vert_edge[i][1] ];
  }

  for( i=0, face=brep->face ; i < 6 ; i++, face=face->nextFace ){
    /* connect faces and edges (doc/cube.gif) */
    for( j=0, edge=face->edge ; j < face->noEdges ; j++, edge=edge->nextEdge ){
      /* connect edges (and corresponding vertex) to current face */
      int kje;

      kje = face_edge[i][j];
      edge->curve = edges[kje].curve;
      edge->startPoint = edges[kje].startPoint;
      edge->endPoint = edges[kje].endPoint;
    }
  }

  /*********************************************************/
  /* fill actual data for cube into Brep structure (NURBS) */
  /*********************************************************/

  /* calculate the coordinates of all 8 points and store in 3D points */
  p[0] = ptk_point3( x  , y  , z  );
  p[1] = ptk_point3( x+a, y  , z  );
  p[2] = ptk_point3( x+a, y  , z+a);
  p[3] = ptk_point3( x  , y  , z+a);
  p[4] = ptk_point3( x  , y+a, z+a);
  p[5] = ptk_point3( x+a, y+a, z+a);
  p[6] = ptk_point3( x  , y+a, z  );
  p[7] = ptk_point3( x+a, y+a, z  );

  for( i=0, face=brep->face; i < 6 ; i++, face=face->nextFace ) {
    /* create NURBS representation of all 6 faces */
    Ppoint4 point;
    /* allocate NURBS surface for current face in cube */
    nrb_allocatenurb( &(face->surface->nurbs) );
    /* allocate appropriate number of knots and points */
    face->surface->nurbs->pf_u.pf_k = 2;
    face->surface->nurbs->pf_u.pf_n = 2;
    face->surface->nurbs->pf_u.pf_nt = 4;
    face->surface->nurbs->pf_v.pf_k = 2;
    face->surface->nurbs->pf_v.pf_n = 2;
    face->surface->nurbs->pf_v.pf_nt = 4;
    /* define knots in u and v direction from 0 to 1 */
    nrb_makeknots( 0.0, 1.0, &(face->surface->nurbs->pf_u) );
    nrb_makeknots( 0.0, 1.0, &(face->surface->nurbs->pf_v) );
    /* allocate space for control points */
    nrb_allocatepts(4, &(face->surface->nurbs->pf_ppp));
    
    for( j=0 ; j < 4 ; j++ ) {
      /* fill geometric data (position in space) for control points */
      point = ptk_pt3topt4( &p[ face_vert[i][j] ] );
      face->surface->nurbs->pf_ppp->pts[j] = point;  
    }
  }

  for( i=0 ; i < 12 ; i++ ){
    /* create NURBS representation of all 12 edges */
    Ppoint3 points[2];

    /* allocate NURBS structure for every edge and create NURB curve */
    nrb_allocatenurb( &(edges[i].curve->nurbs) );
    /* allocate appropriate number of knots and points */
    nrb_init( 2, 4, 0, 1, edges[i].curve->nurbs );
    /* fill start and end point into Ppoint3 array, needed by nrb_polyline */
    points[0] = p[ vert_edge[i][0] ];
    points[1] = p[ vert_edge[i][1] ];
    /* create NURBS polyline with only 2 points (line) */
    nrb_polyline( 2, points, edges[i].curve->nurbs );
  }
  return 0;
}



Janez KREK
Fri Jul 5 12:10:59 METDST 1996