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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathGeom_vec2.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_vec2.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: mathGeom_vec2.c,v 1.5 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Vector handling (allocation, additon etc)
 44  *
 45  *********
 46 */
 47 
 48 /** 
 49  *  @file
 50  *  @brief  2D Vector handling (Vec2).  
 51  *
 52  *  Two main groups of functions;
 53  *  
 54  *  - Vec2 allocation and initialisation.
 55  *  - Accessing values within a Vec2.
 56  *  - Vec2 manipulations (eg, addition, unit vector calculations etc).
 57  * 
 58 */
 59 
 60 
 61 #include "mathGeom_vec2.h"
 62 
 63 #if HAVE_CONFIG_H
 64 #include <config.h>
 65 #endif
 66 
 67 
 68 #include <stdio.h>
 69 #include <math.h>
 70 #include <tina/sys/sysDef.h>
 71 #include <tina/sys/sysPro.h>
 72 #include <tina/math/math_GeomDef.h>
 73 #include <tina/math/mathUtil_rand.h>
 74 
 75 
 76 
 77 /** for use with list utilities **/
 78 Vec2           *vec2_alloc(void)
 79 {
 80         Vec2           *v = ts_ralloc(Vec2);
 81 
 82         v->el[0] = 0.0;
 83         v->el[1] = 0.0;
 84         return (Vec2 *) ((void *) v);
 85 }
 86 
 87 /* Deep copy a vec2 (takes & returns POINTERS) */
 88 Vec2           *vec2_copy(Vec2 * vec2)
 89 {
 90         Vec2           *vec2_new = ts_ralloc(Vec2);
 91 
 92         *vec2_new = *vec2;
 93         return vec2_new;
 94 }
 95 
 96 Vec2           *vec2_make(Vec2 u)
 97 {
 98         Vec2           *v = ts_ralloc(Vec2);
 99 
100         *v = u;
101         return (Vec2 *) ((void *) v);
102 }
103 
104 void            vec2_free(void *v)
105 {
106         rfree((void *) v);
107 }
108 
109 Vec2            vec2(double x, double y)
110 {
111         Vec2            v = {Vec2_id};
112 
113         v.el[0] = x;
114         v.el[1] = y;
115         return (v);
116 }
117 
118 Vec2            vec2_zero(void)
119 {
120         static Vec2     vec2_0 = {Vec2_id};                     /* static data! */
121 
122         return (vec2_0);
123 }
124 
125 Vec2            vec2_ex(void)
126 {
127         return (vec2(1.0, 0.0));
128 }
129 
130 Vec2            vec2_ey(void)
131 {
132         return (vec2(0.0, 1.0));
133 }
134 
135 void            vec2_comps(Vec2 v, float *x, float *y)
136 {
137         *x = v.el[0];
138         *y = v.el[1];
139 }
140 
141 double          vec2_get_x(Vec2 * v)
142 {
143         return (v->el[0]);
144 }
145 
146 double          vec2_get_y(Vec2 * v)
147 {
148         return (v->el[1]);
149 }
150 
151 Vec2            vec2_sum(Vec2 v, Vec2 w)
152 {
153         Vec2            sum = {Vec2_id};
154 
155         sum.el[0] = v.el[0] + w.el[0];
156         sum.el[1] = v.el[1] + w.el[1];
157         return (sum);
158 }
159 
160 Vec2            vec2_sum3(Vec2 u, Vec2 v, Vec2 w)
161 {
162         Vec2            sum = {Vec2_id};
163 
164         sum.el[0] = u.el[0] + v.el[0] + w.el[0];
165         sum.el[1] = u.el[1] + v.el[1] + w.el[1];
166         return (sum);
167 }
168 
169 Vec2            vec2_sum4(Vec2 u, Vec2 v, Vec2 w, Vec2 x)
170 {
171         Vec2            sum = {Vec2_id};
172 
173         sum.el[0] = u.el[0] + v.el[0] + w.el[0] + x.el[0];
174         sum.el[1] = u.el[1] + v.el[1] + w.el[1] + x.el[1];
175         return (sum);
176 }
177 
178 Vec2            vec2_minus(Vec2 v)
179 {
180         Vec2            minus = {Vec2_id};
181 
182         minus.el[0] = -v.el[0];
183         minus.el[1] = -v.el[1];
184         return (minus);
185 }
186 
187 Vec2            vec2_diff(Vec2 v, Vec2 w)
188 {
189         Vec2            diff = {Vec2_id};
190 
191         diff.el[0] = v.el[0] - w.el[0];
192         diff.el[1] = v.el[1] - w.el[1];
193         return (diff);
194 }
195 
196 Vec2            vec2_times(double k, Vec2 v)
197 {
198         Vec2            prod = {Vec2_id};
199 
200         prod.el[0] = k * v.el[0];
201         prod.el[1] = k * v.el[1];
202         return (prod);
203 }
204 
205 Vec2            vec2_interp(double k, Vec2 v1, Vec2 v2)
206 {
207         double          k1 = 1.0 - k;
208         Vec2            interp = {Vec2_id};
209 
210         interp.el[0] = k1 * v1.el[0] + k * v2.el[0];
211         interp.el[1] = k1 * v1.el[1] + k * v2.el[1];
212         return (interp);
213 }
214 
215 double          vec2_dot(Vec2 v, Vec2 w)
216 {
217         return (v.el[0] * w.el[0] + v.el[1] * w.el[1]);
218 }
219 
220 double          vec2_cross(Vec2 v, Vec2 w)
221 {
222         return (v.el[0] * w.el[1] - v.el[1] * w.el[0]);
223 }
224 
225 double          vec2_mod(Vec2 v)
226 {
227         double          vx = v.el[0], vy = v.el[1];
228 
229         return (sqrt(vx * vx + vy * vy));
230 }
231 
232 double          vec2_sqrmod(Vec2 v)
233 {
234         double          vx = v.el[0], vy = v.el[1];
235 
236         return (vx * vx + vy * vy);
237 }
238 
239 double          vec2_modunit(Vec2 v, Vec2 * e)
240 {
241         double          vx = v.el[0], vy = v.el[1];
242         double          k, k1;
243 
244         k = sqrt(vx * vx + vy * vy);
245         if (k == 0.0)
246         {
247                 *e = vec2_ex();
248                 return (0.0);
249         }
250         k1 = 1.0 / k;
251         *e = vec2(vx * k1, vy * k1);
252         return (k);
253 }
254 
255 Vec2            vec2_unit(Vec2 v)
256 {
257         double          vx = v.el[0], vy = v.el[1];
258         double          k, k1;
259 
260         k = sqrt(vx * vx + vy * vy);
261         if (k == 0.0)
262                 return (vec2_ex());
263         k1 = 1.0 / k;
264         return (vec2(vx * k1, vy * k1));
265 }
266 
267 void            vec2_to_polar(Vec2 v, double *r, double *theta)
268 {
269         double          x = vec2_x(v);
270         double          y = vec2_y(v);
271 
272         *r = sqrt(x * x + y * y);
273         *theta = atan2(y, x);
274 }
275 
276 Vec2            vec2_of_polar(double r, double theta)
277 {
278         return (vec2(r * cos(theta), r * sin(theta)));
279 }
280 
281 Vec2            vec2_rand_circle(Vec2 centre, double radius)
282 {
283         double          x, y, r;
284 
285         do
286         {
287                 x = rand_unif(-1.0, 1.0);
288                 y = rand_unif(-1.0, 1.0);
289         } while ((r = x * x + y * y) > 1.0);
290         r = radius / sqrt(r);
291         return (vec2_sum(centre, vec2(x / r, y / r)));
292 }
293 
294 double          vec2_dist(Vec2 v, Vec2 w)
295 {
296         double          dx = v.el[0] - w.el[0];
297         double          dy = v.el[1] - w.el[1];
298 
299         return (sqrt(dx * dx + dy * dy));
300 }
301 
302 double          vec2_sqrdist(Vec2 v, Vec2 w)
303 {
304         double          dx = v.el[0] - w.el[0];
305         double          dy = v.el[1] - w.el[1];
306 
307         return (dx * dx + dy * dy);
308 }
309 
310 double          vec2_angle(Vec2 v, Vec2 w)
311 {
312         return (atan2(vec2_cross(w, v), vec2_dot(w, v)));
313 }
314 
315 Vec2            vec2_perp(Vec2 v)
316 {
317         float           vx, vy;
318 
319         vec2_comps(v, &vx, &vy);
320         return (vec2_unit(vec2(vy, -vx)));
321 }
322 
323 double          vec2_perp_dist(Vec2 p, Vec2 v, Vec2 d)  /* length of
324                                                          * perpendicular from d
325                                                          * to (p,v) */
326 
327 {
328         Vec2            diff = {Vec2_id};
329 
330         diff = vec2_diff(d, p);
331         return (vec2_mod(vec2_diff(diff, vec2_times(vec2_dot(diff, v), v))));
332 }
333 
334 void            vec2_basis(Vec2 up, Vec2 * ex, Vec2 * ey)
335 {
336         if (vec2_mod(up) == 0.0)
337         {
338                 *ex = vec2_ex();
339                 *ey = vec2_ey();
340                 return;
341         }
342         *ey = vec2_unit(up);
343         *ex = vec2_perp(*ey);
344 }
345 
346 int             vec2_parallel(Vec2 v1, Vec2 v2, double dotthres)
347 {
348         return (fabs(vec2_dot(v1, v2)) > dotthres);
349 }
350 
351 Vec2            vec2_read(FILE * fp)
352 {
353         Vec2            v = {Vec2_id};
354 
355         (void) fscanf(fp, "%f %f", &v.el[0], &v.el[1]);
356         return (v);
357 }
358 
359 void            vec2_print(FILE * fp, Vec2 v)
360 {
361         (void) fprintf(fp, "%f %f ", v.el[0], v.el[1]);
362 }
363 
364 void            vec2_pprint(FILE * fp, char *msg, Vec2 v)
365 {
366         (void) fprintf(fp, "%s(%15.6f%15.6f)\n", msg, v.el[0], v.el[1]);
367 }
368 
369 void            vec2_format(Vec2 v)
370 {
371         format("%f %f\n", v.el[0], v.el[1]);
372 }
373 
374 /*
375  * Functions useful for setting vec2 ranges for use with tv_camera2_rect. SMC
376  */
377 
378 /* Return (as a Vec2) the LOWER LEFT corner of a box given by 2 vec2's */
379 Vec2            vec2_less(Vec2 v1, Vec2 v2)
380 {
381         return (vec2(MIN(v1.el[0], v2.el[0]), (MIN(v1.el[1], v2.el[1]))));
382 }
383 
384 
385 /* Return (as a Vec2) the UPPER RIGHT corner of a box given by 2 vec2's */
386 Vec2            vec2_greater(Vec2 v1, Vec2 v2)
387 {
388         return (vec2(MAX(v1.el[0], v2.el[0]), (MAX(v1.el[1], v2.el[1]))));
389 }
390 
391 /*
392  * Ensure box (defined by v1 & v2)  includes vec (expanding box if required)
393  * (v1 is the UPPER LEFT) (v2 is LOWER RIGHT).
394  */
395 void            vec2_ranges(Vec2 * v1, Vec2 * v2, Vec2 vec)
396 {
397         *v1 = vec2_less(*v1, vec);
398         *v2 = vec2_greater(*v2, vec);
399 }
400 

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