home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / mesh / meshpick.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-20  |  3.0 KB  |  93 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips,
  11.  * Celeste Fowler */
  12.  
  13. #include "meshP.h"
  14. #include "pickP.h"
  15. #include "appearance.h"
  16.  
  17. Mesh *
  18. MeshPick(Mesh *mesh, Pick *pick, Appearance *ap, Transform T)
  19. {
  20.   Point3 plist[4], *meshpt;
  21.   int i, nu, nv, maxnu, maxnv, maxpt;
  22.   int foundu, foundv;
  23.   unsigned int apflag;
  24.  
  25.   foundu = foundv = -1;
  26.  
  27.   maxnu = mesh->nu;
  28.   maxnv = mesh->nv;
  29.   maxpt = maxnu * maxnv;
  30.  
  31.   /* Make sure that vects do not register as visible - otherwise they
  32.    * will wreck havoc with the edge picking stuff. */
  33.   if (ap != NULL) {
  34.     apflag = ap->flag;
  35.     ap->flag ^= (ap->flag & APF_VECTDRAW) ? APF_VECTDRAW : 0;
  36.   }
  37.  
  38.   /* Make a copy of the points of the mesh so we only have to 
  39.    * transform them once. */
  40.   meshpt = OOGLNewNE(Point3, maxpt, "meshpt array");
  41.   for (i = 0; i < maxpt; i++) HPt3TransPt3(T, &mesh->p[i], &meshpt[i]);
  42.  
  43.   for (nv = 0; nv < ((mesh->flag & MESH_VWRAP) ? maxnv : maxnv - 1); nv++)
  44.     for (nu = 0; nu < ((mesh->flag & MESH_UWRAP) ? maxnu : maxnu - 1); nu++) {
  45.       plist[0] = MESHPOINT(nu, nv, mesh, meshpt);
  46.       plist[1] = MESHPOINT(nu + 1, nv, mesh, meshpt);
  47.       plist[2] = MESHPOINT(nu + 1, nv + 1, mesh, meshpt);
  48.       plist[3] = MESHPOINT(nu, nv + 1, mesh, meshpt);
  49.       if (PickFace(4, plist, pick, ap)) {
  50.     foundu = nu;
  51.     foundv = nv;
  52.       }
  53.     }
  54.  
  55.   if (meshpt != NULL) OOGLFree(meshpt);
  56.  
  57.   if (ap != NULL) ap->flag = apflag;
  58.  
  59.   if (foundu == -1) return NULL;
  60.  
  61.   if (pick->found & PW_VERT) {
  62.     pick->vi = MESHINDEX(foundu + ((pick->vi == 1 || pick->vi == 2) ? 1 : 0), 
  63.              foundv + pick->vi/2, mesh);
  64.     HPt3Transform(T, &mesh->p[pick->vi], &pick->v);
  65.   }
  66.   if (pick->found & PW_EDGE) {
  67.     pick->ei[0] = 
  68.       MESHINDEX(foundu + ((pick->ei[0] == 1 || pick->ei[0] == 2) ? 1 : 0), 
  69.         foundv + pick->ei[0]/2, mesh);
  70.     pick->ei[1] = 
  71.       MESHINDEX(foundu + ((pick->ei[1] == 1 || pick->ei[1] == 2) ? 1 : 0), 
  72.         foundv + pick->ei[1]/2, mesh);
  73.     HPt3Transform(T, &mesh->p[pick->ei[0]], &pick->e[0]);
  74.     HPt3Transform(T, &mesh->p[pick->ei[1]], &pick->e[1]);
  75.   }
  76.   if (pick->found & PW_FACE) {
  77.     pick->f = OOGLNewNE(HPoint3, 4, "Mesh pick");
  78.     pick->fi = MESHINDEX(foundu, foundv, mesh);
  79.     HPt3Transform(T, &MESHPOINT(foundu, foundv, mesh, mesh->p), 
  80.           &pick->f[0]);
  81.     HPt3Transform(T, &MESHPOINT(foundu+1, foundv, mesh, mesh->p), 
  82.           &pick->f[1]);
  83.     HPt3Transform(T, &MESHPOINT(foundu+1, foundv+1, mesh, mesh->p), 
  84.           &pick->f[2]);
  85.     HPt3Transform(T, &MESHPOINT(foundu, foundv+1, mesh, mesh->p), 
  86.           &pick->f[3]);
  87.   }
  88.  
  89.   TmCopy(T, pick->Tprim);
  90.  
  91.   return mesh;
  92. }
  93.