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

Linux Cross Reference
Tina5/tina-libs/tina/math/mathGeom_geom2.c

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

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/math/mathGeom_geom2.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathGeom_geom2.c,v 1.4 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes :@(#)2D geometry
 44  *
 45  * Notation:
 46  * q - point position
 47  * l - point on line,  v - unit direction of line
 48  * p - point on plane, n - unit normal to plane
 49  *
 50  *********
 51 */
 52 /** 
 53  *  @file
 54  *  @brief 2D Geometry functions.       
 55  *
 56  * Notation: Points and vectors are given as Vec2's where;
 57  *
 58  *  - q is point position.
 59  *  - l is point on line.
 60  *  - v is unit direction of line.
 61  *  - p is point on plane. 
 62  *  - n is unit normal to plane.
 63  * 
 64  * Standard line geometry functions such as finding line midpoints, line 
 65  * intersections etc.
 66 */
 67 
 68 #include "mathGeom_geom2.h"
 69 
 70 #if HAVE_CONFIG_H
 71 #include <config.h>
 72 #endif
 73 
 74 
 75 #include <stdio.h>
 76 #include <math.h>
 77 #include <tina/sys/sysDef.h>
 78 #include <tina/math/math_GeomDef.h>
 79 #include <tina/math/mathGeom_vec2.h>
 80 
 81 Vec2            vec2_midpoint(Vec2 q1, Vec2 q2)
 82 {
 83         Vec2            midpoint = {Vec2_id};
 84 
 85         vec2_x(midpoint) = (float) 0.5 *(vec2_x(q1) + vec2_x(q2));
 86         vec2_y(midpoint) = (float) 0.5 *(vec2_y(q1) + vec2_y(q2));
 87         return (midpoint);
 88 }
 89 
 90 Vec2            vec2_projperp(Vec2 u, Vec2 v)   /**part of  u  perpendicular to unit  v**/
 91 
 92 {
 93         return (vec2_diff(u, vec2_times(vec2_dot(u, v), v)));
 94 }
 95 
 96 Vec2            vec2_projpar(Vec2 u, Vec2 v)    /**part of  u  parallel to unit  v**/
 97 
 98 {
 99         return (vec2_times(vec2_dot(u, v), v));
100 }
101 
102 Vec2            vec2_proj_on_line(Vec2 q, Vec2 l, Vec2 v)
103 {
104         Vec2            lq = {Vec2_id};
105 
106         lq = vec2_diff(q, l);
107         return (vec2_sum(l, vec2_times(vec2_dot(lq, v), v)));
108 }
109 
110 Vec2            vec2_inter_lines(Vec2 l1, Vec2 v1, Vec2 l2, Vec2 v2)
111 {
112         double          c = vec2_cross(v1, v2), t1;
113 
114         if (c == 0.0)
115                 return (vec2_midpoint(l1, l2));
116         t1 = vec2_cross(vec2_diff(l2, l1), v2) / c;
117         return (vec2_sum(l1, vec2_times(t1, v1)));
118 }
119 
120 void            vec2_join_2_points(Vec2 q1, Vec2 q2, Vec2 * l, Vec2 * v)
121 {
122         *l = vec2_times(0.5, vec2_sum(q1, q2));
123         *v = vec2_unit(vec2_diff(q2, q1));
124 }
125 
126 double          vec2_dist_point_line(Vec2 q, Vec2 l, Vec2 v)
127 {
128         Vec2            dq = {Vec2_id};
129 
130         dq = vec2_projperp(vec2_diff(q, l), v);
131         return (vec2_mod(dq));
132 }
133 
134 void            vec2_circ_3_points(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 * centre, double *radius)
135 {
136         Vec2            a = {Vec2_id};
137         Vec2            b = {Vec2_id};
138         double          a2, b2, ab, u, v, w;
139 
140         a = vec2_diff(p2, p1);
141         b = vec2_diff(p3, p1);
142 
143         a2 = vec2_sqrmod(a);
144         b2 = vec2_sqrmod(b);
145         ab = vec2_dot(a, b);
146 
147         u = b2 * (a2 - ab);
148         v = a2 * (b2 - ab);
149         w = 2.0 * (a2 * b2 - ab * ab);
150 
151         a = vec2_times(u, a);
152         b = vec2_times(v, b);
153 
154         *centre = vec2_sum(p1, vec2_times(1 / w, vec2_sum(a, b)));
155         *radius = sqrt((a2 * b2 * (a2 + b2 - 2.0 * ab)) / (2.0 * w));
156 }
157 
158 Vec2           *vec2_inter_par_test(Vec2 p, Vec2 v1, Vec2 q, Vec2 v2, double parallel)
159 {
160         Vec2            cp = {Vec2_id};
161         Vec2            b = {Vec2_id};
162         Vec2           *isct;
163         double          a;
164 
165         cp = vec2_inter_lines(p, v1, q, v2);
166         if (!vec2_parallel(v1, v2, parallel))
167         {
168                 cp.el[0] = -v2.el[1];
169                 cp.el[1] = v2.el[0];
170 
171                 a = vec2_dot(cp, vec2_diff(p, q)) / vec2_dot(cp, v1);
172                 b = vec2_times(a, v1);
173 
174                 isct = vec2_alloc();
175                 isct->el[0] = p.el[0] - b.el[0];
176                 isct->el[1] = p.el[1] - b.el[1];
177                 return (isct);
178         } else
179                 return (NULL);
180 }
181 

~ [ 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.