1 /*********
2 * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
3 * University of Manchester, UK. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * . Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * . Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * . Neither the name of the University of Manchester nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 **********
33 *
34 * Program : TINA
35 * File : $Source: /home/tina/cvs/tina-tools/tinatool/draw/drawTv_proj2.c,v $
36 * Date : $Date: 2003/10/01 16:02:47 $
37 * Version : $Revision: 1.2 $
38 * CVS Id : $Id: drawTv_proj2.c,v 1.2 2003/10/01 16:02:47 tony Exp $
39 *
40 * Author : Legacy TINA
41 *
42 * Notes :
43 *
44 *********
45 */
46
47 #include "drawTv_proj2.h"
48
49 #if HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53 #include <stdio.h>
54 #include <tina/sys/sysDef.h>
55 #include <tina/math/mathDef.h>
56 #include <tina/math/mathPro.h>
57 #include <tinatool/draw/draw_TvDef.h>
58
59 /**2D general projection**/
60
61 static Ipos proj2(Tv * tv, Vec2 v)
62 {
63 Ipos pos = {Ipos_id};
64 double x, y;
65
66 v = vec2_diff(v, tv->centre2);
67 x = tv->cx + tv->scalex * vec2_dot(v, tv->ex2);
68 y = tv->cy + tv->scaley * vec2_dot(v, tv->ey2);
69 pos.x = ROUND(x);
70 pos.y = ROUND(y);
71 return (pos);
72 }
73
74 static Vec2 backproj2(Tv * tv, Ipos pos)
75 {
76 Vec2 p = {Vec2_id};
77 double x = (pos.x - tv->cx) / tv->scalex;
78 double y = (pos.y - tv->cy) / tv->scaley;
79
80 p = vec2_sum3(tv->centre2, vec2_times(x, tv->ex2), vec2_times(y, tv->ey2));
81 return (p);
82 }
83
84 void tv_set_proj2(Tv * tv)
85 {
86 tv->proj2 = proj2;
87 tv->backproj2 = backproj2;
88 }
89
90 /**generic 2D projection**/
91
92 Ipos tv_proj2(Tv * tv, Vec2 p)
93 {
94 return (tv->proj2(tv, p));
95 }
96
97 Vec2 tv_backproj2(Tv * tv, Ipos pos)
98 {
99 return (tv->backproj2(tv, pos));
100 }
101
102 Bool tv_ipos_within(Tv * tv, Ipos pos)
103 {
104 int r, c;
105
106 if (tv == NULL)
107 return (false);
108
109 r = ipos_y(pos);
110 c = ipos_x(pos);
111
112 return (Bool) (r >= 0 && r < tv->height && c >= 0 && c < tv->width);
113 }
114
115 /**setting and changing 2D cameras**/
116
117 void tv_camera2_rect(Tv * tv, Vec2 v1, Vec2 v2)
118 {
119 double x1 = vec2_x(v1);
120 double y1 = vec2_y(v1);
121 double x2 = vec2_x(v2);
122 double y2 = vec2_y(v2);
123 double width, height;
124
125 if (tv == NULL || x1 >= x2 || y1 >= y2)
126 return;
127 width = x2 - x1;
128 height = y2 - y1;
129 tv->scalex = (float)(tv->width / width);
130 tv->scaley = (float)(tv->height / height);
131 tv->cx = (float)(tv->width / 2);
132 tv->cy = (float)(tv->height / 2);
133 tv->centre2 = vec2((x1 + x2) / 2.0, (y1 + y2) / 2.0);
134 tv->radius2 = (float)(MAX(width, height));
135 tv->ex2 = vec2_ex();
136 tv->ey2 = vec2_minus(vec2_ey());
137 }
138
139 void tv_camera2_roi(Tv * tv, Imregion * roi)
140 {
141 double x1 = roi->lx;
142 double y1 = roi->ly;
143 double x2 = roi->ux;
144 double y2 = roi->uy;
145 double width, height;
146
147 if(tv == NULL)
148 return;
149 if (x1 >= x2 || y1 >= y2)
150 return;
151 width = x2 - x1;
152 height = y2 - y1;
153 tv->scalex = tv->scaley = (float)(MIN(tv->width / width, tv->height / height));
154 tv->cx = (float)(tv->width / 2);
155 tv->cy = (float)(tv->height / 2);
156 tv->centre2 = vec2((x1 + x2) / 2.0, (y1 + y2) / 2.0);
157 tv->radius2 = (float)(MAX(width, height));
158 tv->ex2 = vec2_ex();
159 tv->ey2 = vec2_ey();
160 }
161
162 void tv_camera2_image(Tv * tv, int width, int height)
163 {
164 if(tv == NULL)
165 return;
166 tv->scalex = tv->scaley =
167 (float)(MIN(tv->width / (double) width, tv->height / (double) height));
168 tv->cx = (float)(tv->width / 2);
169 tv->cy = (float)(tv->height / 2);
170 tv->centre2 = vec2(width / 2.0, height / 2.0);
171 tv->radius2 = (float)(MAX(width, height));
172 tv->ex2 = vec2_ex();
173 tv->ey2 = vec2_ey();
174 }
175
176 /* Stretch the camera of a tv to make region of interest of an image
177 * (with given aspect ratio) fill the screen. */
178 void tv_camera2_roi_stretch(Tv * tv, Imregion * roi, double aspect_ratio)
179 {
180 double width = (double) (roi->ux - roi->lx);
181 double cx = (double) (roi->ux + roi->lx) / 2.0;
182 double cy = (double) (roi->uy + roi->ly) / 2.0;
183
184 if(tv == NULL)
185 return;
186 tv->scalex = (float)(tv->width / width);
187 tv->scaley = (float)(tv->scalex * aspect_ratio);
188 tv->cx = (float)(tv->width / 2);
189 tv->cy = (float)(tv->height / 2);
190 tv->centre2 = vec2(cx, cy);
191 tv->ex2 = vec2_ex();
192 tv->ey2 = vec2_ey();
193 }
194
195 void tv_camera2(Tv * tv, Vec2 centre, double radius, Vec2 down)
196 {
197 if(tv == NULL)
198 return;
199 tv->scalex = tv->scaley = (float)(0.5 * MIN(tv->width, tv->height) / radius);
200 tv->cx = (float)(tv->width / 2);
201 tv->cy = (float)(tv->height / 2);
202 tv->centre2 = centre;
203 tv->radius2 = (float)radius;
204 vec2_basis(down, &tv->ex2, &tv->ey2);
205 }
206
207 void tv_orient2(Tv * tv, Vec2 down)
208 {
209 if(tv == NULL)
210 return;
211 vec2_basis(down, &tv->ex2, &tv->ey2);
212 }
213
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.