home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / ASCENDER / ascendMar8.tar / UMass / BuildingFinder / Staging / computeIntersections.c < prev    next >
C/C++ Source or Header  |  1995-07-27  |  6KB  |  274 lines

  1. #include "../header.h"
  2. #include "../stdDefs.h"
  3.  
  4.  
  5. /*
  6.  * 
  7.  *
  8.  * Read lines from a file and compute intersections at a particular 
  9.  * angle.
  10.  *
  11.  */
  12. int
  13. compute_intersections_region(char *lines, char *corners,
  14.                  c_handle in_pane, c_handle im, int x1, 
  15.                 int y1, int x2, int y2);
  16.  
  17. void freeLineList(line  *l);
  18.  
  19. void
  20. computeIntersections() 
  21. {
  22.  
  23.  
  24.     c_handle    image;
  25.     c_handle    view;    
  26.     c_handle    view_trans;    
  27.     c_handle    projection;
  28.     double_2    *userPoint;
  29.     c_handle    im;
  30.     char        lineFile[80];
  31.     char        cornerFile[80];
  32.  
  33.     struct c_handle_1_int_3*    pick, *pick_a_pane();
  34.     c_handle    in_pane;
  35.     
  36.  
  37.  
  38.     double        x1,y1,x2,y2;
  39.  
  40.  
  41.  
  42.     /*** User selects the image and a bouding box for intersections
  43.          to be searched **/
  44.     pick = pick_a_pane(get_interactor(0),1,
  45.                                 "Pick Lower Left Corner");
  46.         in_pane = pick->h1;
  47.         im = working_image(get_2d_world(top_image(1,in_pane)));
  48.         view = top_view(1,in_pane);
  49.         view_trans = two_d_to_window_transform(view);
  50.         userPoint = (double_2 *) inverse_transform_point(view_trans,
  51.                         (double)pick->i1,(double)pick->i2,0);
  52.  
  53.         x1 = (int) floor(userPoint->d1);
  54.         y1 = (int) floor(userPoint->d2);
  55.  
  56.  
  57.         pick = pick_a_pane(get_interactor(0),1,
  58.                                 "Pick Upper Right Corner");
  59.         userPoint = (double_2 *) inverse_transform_point(view_trans,
  60.                         (double)pick->i1,(double)pick->i2,0);
  61.         x2 = (int) floor(userPoint->d1);
  62.         y2 = (int) floor(userPoint->d2);
  63.  
  64.      set_ior_alu(in_pane);
  65.  
  66.     sprintf(lineFile,"../Tokens/ldata.%s",name(get_2d_world(im)));
  67.     sprintf(cornerFile,"../Tokens/corners");
  68.  
  69.     compute_intersections_region(lineFile, cornerFile,
  70.                  in_pane,im, x1,y1,x2,y2);
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77. int
  78. compute_intersections_region(char *lineFile, char *cornerFile, c_handle in_pane,
  79.          c_handle im, int x1, int y1, int x2, int y2) 
  80. {
  81.  
  82.     line     *lines;
  83.     line     *lineptr;
  84.     line     *rest;
  85.     Intersection    *Ilist=NULL;
  86.     double_2    Ipoint;
  87.  
  88.     double         Parm[2];
  89.     double        avgHeight;
  90.     c_handle    projectionMat;
  91.     int        count=0;
  92.  
  93.  
  94.  
  95.  
  96.     projectionMat = get_projection_matrix(get_2d_world(im));
  97.  
  98.     fprintf(stderr,"Loading lines [%s]\n",lineFile);
  99.     lines =    (line *) load_line_data(x1,y1,x2,y2,lineFile,im);
  100.     if (lines == NULL) 
  101.         fprintf(stderr,"NULL!!!\n");
  102.     lineptr = lines;
  103.     while (lineptr != NULL) {
  104.        rest = lineptr->next;
  105.        while (rest != NULL) {
  106.           matchConstraints(*lineptr, *rest, projectionMat, &Ipoint);
  107.           if (Ipoint.d1 != -1.0 && Ipoint.d2 != -1.0) {
  108.                avgHeight = (lineptr->height + rest->height) / 2.0;
  109.             Ilist =
  110.                 addIntersect(Ilist, Ipoint, avgHeight, *lineptr, *rest);
  111.             count++;
  112.            } 
  113.     
  114.           rest = rest->next;
  115.         }    
  116.  
  117.         lineptr = lineptr->next;
  118.     }
  119.  
  120.     printIntersections(in_pane,Ilist, cornerFile);
  121.  
  122.     freeLineList(lines);
  123.     return(count);
  124.  
  125. }
  126.             
  127.         
  128. int closeEnough(Point p, line l1, line l2)
  129. /* 
  130.  * Check to see if point p is "closeEnough" to one of the
  131.  * lines 'l1' or 'l2'.  That is is the distance between either
  132.  * of the lines less than MIN_DIST.
  133.  *
  134.  */
  135. {
  136.  
  137.     if (distancePointLineHessian(p,l1) <= LINE_ENDPOINT_WINDOW)
  138.         return(TRUE);
  139.     if (distancePointLineHessian(p,l2) <= LINE_ENDPOINT_WINDOW)
  140.         return(TRUE);
  141.     
  142.     return(FALSE);
  143. }
  144.  
  145.  
  146. int 
  147. linesProximate(line l1, line l2, double minDist) 
  148. /*
  149.  * Are the lines close in the plane so that they can be interesected.
  150.  * Note, if THREE_D, then lines must be within HEIGHT_BUCKET, of each
  151.  * other to be proximate
  152.  *
  153.  */
  154. {
  155.     Point temp;
  156.     double distance;
  157.     double tempDist, smallDist = 9999.0;
  158.  
  159.  
  160.     /** Check that the lines are close in height **/
  161.  
  162.     if (USE_3D) 
  163.         if ( fabs(l1.height - l2.height) > HEIGHT_BUCKET) {
  164.             return(FALSE);
  165.         }
  166.             
  167.     
  168.     if (linesOverlap(l1,l2)) {
  169.        temp.x = l2.x1;
  170.        temp.y = l2.y1;
  171.        
  172.        tempDist = distancePointLineHessian(temp,l1);
  173.        if (tempDist < smallDist)
  174.         smallDist = tempDist;
  175.        temp.x = l2.x2;
  176.        temp.y = l2.y2;
  177.        tempDist = distancePointLineHessian(temp,l1);
  178.        if (tempDist < smallDist)
  179.         smallDist = tempDist;
  180.  
  181.     }
  182.      if (linesOverlap(l2,l1)) {
  183.           temp.x = l1.x1;
  184.           temp.y = l1.y1;
  185.           tempDist = distancePointLineHessian(temp,l2);
  186.           if (tempDist < smallDist)
  187.         smallDist = tempDist;
  188.           temp.x = l1.x2;
  189.           temp.y = l1.y2;
  190.           tempDist = distancePointLineHessian(temp,l2);
  191.           if (tempDist < smallDist)
  192.             smallDist = tempDist;
  193.  
  194.     }
  195.  
  196.     if (!linesOverlap(l1,l2) && !linesOverlap(l2,l1) )   {
  197.         smallDist = SMALL(SMALL(dist(l1.x1,l1.y1,l2.x1,l2.y1),
  198.                    dist(l1.x1,l1.y1,l2.x2,l2.y2)),
  199.                  SMALL(dist(l1.x2,l1.y2,l2.x1,l2.y1),
  200.                    dist(l1.x2,l1.y2,l2.x2,l2.y2)));
  201.  
  202.         if (smallDist < minDist)
  203.            return(DISTAL);
  204.     } else {
  205.         if (smallDist < minDist)
  206.             return(OVERLAP);
  207.     }
  208.  
  209.     return(FALSE);
  210.     
  211. }
  212.  
  213. void
  214. matchConstraints(line l1, line l2, c_handle proj, double_2 *p)
  215. /*
  216.  *
  217.  * Do these two lines fit the criteria necessary for an intersection
  218.  * computation to take place?
  219.  *
  220.  * 1) Are they proximate in the image?
  221.  * 2) Are the oriented at the correct angle?
  222.  *
  223.  * Returns OVERLAP : if l1 overlaps l2 in the image and they match the
  224.  *            constraints.
  225.  *        DISTAL  : if they do not overlap but match constraints.
  226.  *       FALSE   : If they do no match the constraints.
  227.  *
  228.  */
  229. {
  230.  
  231.     int val;
  232.     double_2 pval;
  233.  
  234.  
  235.     val = linesProximate(l1, l2, LINE_ENDPOINT_WINDOW);
  236.     if (val != 0) {
  237.         correctAngle(l1, l2, proj, p);
  238.     
  239.         return;
  240.     }
  241.     else {
  242.         p->d1 = -1.0;
  243.         p->d2 = -1.0;
  244.         return;
  245.     }
  246.     
  247. }
  248.  
  249.  
  250.  
  251. char printType(double s, double t)
  252. {
  253.  
  254.     if (IntersectType(s,t) == T)
  255.         return('T');
  256.     if (IntersectType(s,t) == X)
  257.         return('X');
  258.     if (IntersectType(s,t) == L)
  259.         return('L');
  260.     if (IntersectType(s,t) == N)
  261.         return('N');
  262. }
  263.  
  264. void freeLineList(line  *l)
  265. {
  266.  
  267.     line *p, *t;
  268.  
  269.     for (p = l; p != NULL; p = t) {
  270.         t = p->next;
  271.         free(p);
  272.     }
  273. }
  274.