~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/geometry/geomLine_inter.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * This file is part of the TINA Open Source Image Analysis Environment
  4  * henceforth known as TINA
  5  *
  6  * TINA is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License as 
  8  * published by the Free Software Foundation.
  9  *
 10  * TINA is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU General Public License
 16  * along with TINA; if not, write to the Free Software Foundation, Inc., 
 17  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  *
 19  * ANY users of TINA who require exemption from the existing licence must
 20  * negotiate a new licence with Dr. Neil.A.Thacker, the sole agent for
 21  * the University of Manchester.
 22  *
 23  **********
 24  * 
 25  * Program :    TINA
 26  * File    :  $Source: /home/tina/cvs/tina-libs/tina/geometry/geomLine_inter.c,v $
 27  * Date    :  $Date: 2005/01/09 17:49:25 $
 28  * Version :  $Revision: 1.2 $
 29  * CVS Id  :  $Id: geomLine_inter.c,v 1.2 2005/01/09 17:49:25 paul Exp $
 30  *
 31  * Notes :
 32  *
 33  *********
 34 */
 35 
 36 
 37 
 38 #if HAVE_CONFIG_H
 39   #include <config.h>
 40 #endif
 41 
 42 #include <stdio.h>
 43 #include <math.h>
 44 #include <stdlib.h>
 45 #include <tina/sys/sysDef.h>
 46 #include <tina/sys/sysPro.h>
 47 #include <tina/math/mathDef.h>
 48 #include <tina/math/mathPro.h>
 49 #include <tina/geometry/geomDef.h>
 50 #include <tina/geometry/geom_LineDef.h>
 51 #include <tina/geometry/geom_LinePro.h>
 52 
 53 #include "geomLine_inter.h"
 54 
 55 void test_line(Line2 *tline)
 56 {
 57       if ((tline->length < 0.0) || (tline->length >= 0.0))
 58       {
 59           return;
 60       }
 61       else
 62       {
 63          printf("NaN found \n");
 64       }
 65 }
 66 
 67 Line2 *line2_circ_inter(Line2 *l, Vec2 p, double r)
 68 {
 69         double A, B, C, D;
 70         double m, c;
 71         double dx, dy;
 72         double t0, t1, t;       /* Parametric line coefficients */
 73         Vec2 s0, s1, s;
 74         Line2 *newline;
 75 
 76         double start[2], end[2], circ[2], i0[2], i1[2];
 77         int x, y;
 78 
 79         x=0;
 80         y=1;
 81 
 82         start[x] = vec2_x(l->p1);
 83         start[y] = vec2_y(l->p1);
 84         end[x] = vec2_x(l->p2);
 85         end[y] = vec2_y(l->p2);
 86 
 87         circ[x] = vec2_x(p);
 88         circ[y] = vec2_y(p);
 89 
 90         dx = end[x]-start[x];
 91         dy = end[y]-start[y];
 92 
 93         if (fabs(dy)>fabs(dx))         /* All x's -> y's and vice versa */
 94         {
 95                 x=1;
 96                 y=0;    
 97                 dx = end[x]-start[x]; /* Recalc. */
 98                 dy = end[y]-start[y];
 99         }
100 
101         m = dy/dx;
102         c = start[y]-m*start[x];
103 
104         A = 1.0 + m*m;
105         B = -2.0*circ[x] + 2.0*m*c - 2.0*m*circ[y];
106         C = circ[x]*circ[x] + c*c - 2.0*c*circ[y] + circ[y]*circ[y] - r*r;
107 
108         D = B*B - 4.0*A*C;
109 
110         if (D<=0.0) /* No intersection */
111         {
112                 return NULL;
113         }
114 
115         i0[x] = (-B+sqrt(D))/(2.0*A);
116         i0[y] = m*i0[x]+c;
117         i1[x] = (-B-sqrt(D))/(2.0*A);
118         i1[y] = m*i1[x]+c;
119 
120         t0 = (i0[x]-start[x])/(end[x]-start[x]);
121         t1 = (i1[x]-start[x])/(end[x]-start[x]);
122 
123         if (((t0>=1.0) && (t1>=1.0)) || 
124                 ((t0<=0.0) && (t1<=0.0))    )   /* Line extent does not intersect */
125         {
126                 return NULL;
127         }
128 
129         if (((t0>=1.0) && (t1<=0.0)) || 
130                 ((t0<=0.0) && (t1>=1.0))    )   /* Whole line in circle */
131         {
132                 newline = line2_copy(l);
133                 test_line(newline);
134                 return (newline);
135         }
136 
137         /* Need to split the line */
138 
139         x = 0;
140         y = 1;
141 
142         if (((t0>=0.0) && (t0<=1.0)) && 
143                 ((t1>=0.0) && (t1<=1.0))    )   /* The line is split twice */
144         {
145                 vec2_x(s0) = start[x]+t0*(end[x]-start[x]);
146                 vec2_y(s0) = start[y]+t0*(end[y]-start[y]);
147                 vec2_x(s1) = start[x]+t1*(end[x]-start[x]);
148                 vec2_y(s1) = start[y]+t1*(end[y]-start[y]);
149                 newline = line2_make(s0, s1, LINE2);
150 
151                 test_line(newline);
152                 return (newline);
153         }
154 
155         if ((t0>=0.0) && (t0<=1.0))
156                 t = t0;
157         else
158                 t = t1;
159 
160         vec2_x(s) = start[x]+t*(end[x]-start[x]);
161         vec2_y(s) = start[y]+t*(end[y]-start[y]);
162 
163         if (vec2_dist(p, l->p1)<r) /* Use p1 */
164         {
165                 newline = line2_make(l->p1, s, LINE2);
166                 test_line(newline);
167                 return (newline);
168         }
169         else    /* Use p2 */
170         {
171                 newline = line2_make(l->p2, s, LINE2);
172                 test_line(newline);
173                 return (newline);
174         }       
175 }
176 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.