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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathGeom_ipos.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_ipos.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathGeom_ipos.c,v 1.4 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Ipos handling. Ipos is {int x; int y;}
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief Ipos handling functions.     
 50  *
 51  *  Ipos is integer position {int x; int y;} typically used as tv screen coordinates.
 52  * 
 53  *  There are three main types of function;
 54  *
 55  *  - Ipos allocation and freeing routines.
 56  *  - Ipos manipulation (eg, addition of 2 ipos').
 57  *  - Ipos printing and file functions.
 58 */
 59 
 60 #include "mathGeom_ipos.h"
 61 
 62 #if HAVE_CONFIG_H
 63 #include <config.h>
 64 #endif
 65 
 66 #include <stdio.h>
 67 #include <stdlib.h>
 68 #include <math.h>
 69 #include <tina/sys/sysDef.h>
 70 #include <tina/sys/sysPro.h>
 71 #include <tina/math/math_GeomDef.h>
 72 #include <tina/math/mathUtil_math.h>
 73 
 74 /** for use with list utilities **/
 75 
 76 Ipos           *ipos_alloc(void)
 77 {
 78         Ipos           *pos = ts_ralloc(Ipos);
 79 
 80         pos->x = 0;
 81         pos->y = 0;
 82         return (pos);
 83 }
 84 
 85 Ipos           *ipos_make(int x, int y)
 86 {
 87         Ipos           *pos = ts_ralloc(Ipos);
 88 
 89         pos->x = x;
 90         pos->y = y;
 91         return (pos);
 92 }
 93 
 94 void            ipos_free(void *pos)
 95 {
 96         rfree((void *) pos);
 97 }
 98 
 99 Bool            ipos_equal(Ipos pos1, Ipos pos2)
100 {
101         return ((Bool) ((ipos_x(pos1) == ipos_x(pos2)) &&
102                         (ipos_y(pos1) == ipos_y(pos2))));
103 }
104 
105 
106 Ipos            ipos(int x, int y)
107 {
108         Ipos            pos = {Ipos_id};
109 
110         pos.x = x;
111         pos.y = y;
112         return (pos);
113 }
114 
115 Ipos            ipos_zero(void)
116 {
117         return (ipos(0, 0));
118 }
119 
120 Ipos            ipos_ex(void)
121 {
122         return (ipos(1, 0));
123 }
124 
125 Ipos            ipos_ey(void)
126 {
127         return (ipos(0, 1));
128 }
129 
130 void            ipos_comps(Ipos pos, int *x, int *y)
131 {
132         *x = pos.x;
133         *y = pos.y;
134 }
135 
136 Ipos            ipos_sum(Ipos pos1, Ipos pos2)
137 {
138         Ipos            sum = {Ipos_id};
139 
140         sum.x = pos1.x + pos2.x;
141         sum.y = pos1.y + pos2.y;
142         return (sum);
143 }
144 
145 Ipos            ipos_sum3(Ipos pos0, Ipos pos1, Ipos pos2)
146 {
147         Ipos            sum = {Ipos_id};
148 
149         sum.x = pos0.x + pos1.x + pos2.x;
150         sum.y = pos0.y + pos1.y + pos2.y;
151         return (sum);
152 }
153 
154 Ipos            ipos_sum4(Ipos pos0, Ipos pos1, Ipos pos2, Ipos pos3)
155 {
156         Ipos            sum = {Ipos_id};
157 
158         sum.x = pos0.x + pos1.x + pos2.x + pos3.x;
159         sum.y = pos0.y + pos1.y + pos2.y + pos3.y;
160         return (sum);
161 }
162 
163 Ipos            ipos_minus(Ipos pos)
164 {
165         Ipos            minus = {Ipos_id};
166 
167         minus.x = -pos.x;
168         minus.y = -pos.y;
169         return (minus);
170 }
171 
172 Ipos            ipos_diff(Ipos pos1, Ipos pos2)
173 {
174         Ipos            diff = {Ipos_id};
175 
176         diff.x = pos1.x - pos2.x;
177         diff.y = pos1.y - pos2.y;
178         return (diff);
179 }
180 
181 Ipos            ipos_times(double k, Ipos pos)
182 {
183         Ipos            prod = {Ipos_id};
184 
185         prod.x = (int) k *pos.x;
186         prod.y = (int) k *pos.y;
187         return (prod);
188 }
189 
190 int             ipos_dot(Ipos pos1, Ipos pos2)
191 {
192         return (pos1.x * pos2.x + pos1.y * pos2.y);
193 }
194 
195 int             ipos_cross(Ipos pos1, Ipos pos2)
196 {
197         return (pos1.x * pos2.y - pos1.y * pos2.x);
198 }
199 
200 double          ipos_mod(Ipos pos)
201 {
202         double          vx = pos.x, vy = pos.y;
203 
204         return (sqrt(vx * vx + vy * vy));
205 }
206 
207 double          ipos_sqrmod(Ipos pos)
208 {
209         double          vx = pos.x, vy = pos.y;
210 
211         return (vx * vx + vy * vy);
212 }
213 
214 double          ipos_dist(Ipos pos1, Ipos pos2)
215 {
216         double          dx = pos1.x - pos2.x;
217         double          dy = pos1.y - pos2.y;
218 
219         return (sqrt(dx * dx + dy * dy));
220 }
221 
222 int             ipos_manhattan(Ipos pos1, Ipos pos2)
223 {
224         return (abs(pos1.x - pos2.x) + abs(pos1.y - pos2.y));
225 }
226 
227 int             ipos_maxabs(Ipos pos1, Ipos pos2)
228 {
229         int             dx = abs(pos1.x - pos2.x);
230         int             dy = abs(pos1.y - pos2.y);
231 
232         return (MAX(dx, dy));
233 }
234 
235 double          ipos_sqrdist(Ipos pos1, Ipos pos2)
236 {
237         double          dx = pos1.x - pos2.x;
238         double          dy = pos1.y - pos2.y;
239 
240         return (dx * dx + dy * dy);
241 }
242 
243 double          ipos_dist_seg(Ipos p, Ipos e1, Ipos e2)
244 {
245         double          x = ipos_x(p);
246         double          y = ipos_y(p);
247         double          x1 = ipos_x(e1);
248         double          y1 = ipos_y(e1);
249         double          x2 = ipos_x(e2);
250         double          y2 = ipos_y(e2);
251         double          dx, dy, dx1, dy1, l, t, d;
252 
253         dx = x2 - x1;
254         dy = y2 - y1;
255         dx1 = x - x1;
256         dy1 = y - y1;
257         l = sqrt(dx * dx + dy * dy);
258         t = (dx1 * dx + dy1 * dy) / l;
259         if (t < 0.0)
260                 d = sqrt(dx1 * dx1 + dy1 * dy1);
261         else if (t > l)
262                 d = sqrt(sqr(x - x2) + sqr(y - y2));
263         else
264                 d = fabs(dx1 * dy - dy1 * dx) / l;
265 
266         return (d);
267 }
268 
269 Ipos            ipos_midpoint(Ipos p1, Ipos p2)
270 {
271         return (ipos((p1.x + p2.x) / 2, (p1.y + p2.y) / 2));
272 }
273 
274 Ipos            ipos_read(FILE * fp)
275 {
276         Ipos            pos = {Ipos_id};
277 
278         (void) fscanf(fp, "%d %d", &pos.x, &pos.y);
279         return (pos);
280 }
281 
282 void            ipos_print(FILE * fp, Ipos pos)
283 {
284         (void) fprintf(fp, "%d %d ", pos.x, pos.y);
285 }
286 
287 void            ipos_pprint(FILE * fp, char *msg, Ipos pos)
288 {
289         (void) fprintf(fp, "%s(%15d%15d)\n", msg, pos.x, pos.y);
290 }
291 

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