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

Linux Cross Reference
Tina4/src/covira/covira.c

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

  1 /*
  2  * covira.c
  3  *
  4  * Wrappers for Aivru code within Tosca
  5  *
  6  */
  7 
  8 #include <tina/all_tina.h>
  9 #include <tina/brain.h>
 10 #include <tina/brainfuncs.h>
 11 
 12 static int interact_mode = PUSH_CONTOUR;
 13 static int rigidity_mode = RUN_DYNAMIC;
 14 static int force_mode = SEEK_EDGE;
 15 static int steps = 50;
 16 static double smoothness = 50.0;
 17 
 18 /*
 19 Returns current user interaction mode. This takes the values
 20 PUSH_CONTOUR, allowing the user to push the snakes locally with the mouse
 21 (scale determined by distance from snake on button down),
 22 SHIFT_CONTOUR which translates the snake to follow the mouse1, iand
 23 ZOOM_CONTOUR which rotates and scales the snake to follow the mouse.
 24 */ 
 25 int aivru_snake_interact_mode_get()
 26 {
 27     return(interact_mode);
 28 }
 29 
 30 /*
 31 Sets current user interaction mode. See aivru_snake_interact_mode_get.
 32 */
 33 void aivru_snake_interact_mode_set(int mode)
 34 {
 35     interact_mode = mode;
 36 }
 37 
 38 /*
 39 Called on button down during mouse interaction.
 40 See aivru_snake_interact_mode_get.
 41 */
 42 void aivru_position_down(int x, int y)
 43 {
 44     Ipos p = ipos(x, y);
 45     switch(interact_mode)
 46     {
 47         case PUSH_CONTOUR:
 48         case SHIFT_CONTOUR:
 49         case ZOOM_CONTOUR:
 50             notv_snake_edit_down(p);
 51             break;
 52     }
 53         
 54 }
 55 
 56 /*
 57 Called on button drag during mouse interaction.
 58 See aivru_snake_interact_mode_get.
 59 */
 60 void aivru_position_drag(int x, int y)
 61 {
 62     Ipos p = ipos(x, y);
 63 
 64     switch(interact_mode)
 65     {
 66         case PUSH_CONTOUR:
 67             notv_snake_push_drag(p);
 68             break;
 69         case SHIFT_CONTOUR:
 70             notv_snake_shift_drag(p);
 71             break;
 72         case ZOOM_CONTOUR:
 73             notv_snake_zoom_drag(p);
 74             break;
 75     }
 76     ims_snake_changed();
 77 }
 78 
 79 /*
 80 Called on button up during mouse interaction.
 81 See aivru_snake_interact_mode_get.
 82 */
 83 void aivru_position_up(int x, int y)
 84 {
 85     Ipos p = ipos(x, y);
 86     switch(interact_mode)
 87     {
 88         case PUSH_CONTOUR:
 89             notv_snake_push_up(p);
 90             break;
 91         case SHIFT_CONTOUR:
 92             notv_snake_shift_up(p);
 93             break;
 94         case ZOOM_CONTOUR:
 95             notv_snake_zoom_up(p);
 96             break;
 97     }
 98 }
 99 
100 /*
101 Returns current snake rigidity mode. This takes the values
102 RUN_RIGID, where the snake retains its shape but can translate, rotate,
103 and scale, or RUN_DYNAMIC where the snake can deform arbitrarily.
104 */
105 int aivru_snake_rigidity_mode_get()
106 {
107     return(rigidity_mode);
108 }
109 
110 /*
111 Set current snake rigidity mode, see aivru_snake_rigidity_mode_get.
112 */
113 void aivru_snake_rigidity_mode_set(int mode)
114 {
115     rigidity_mode = mode;
116 }
117 
118 /*
119 Returns current snake rigidity mode. This takes the values
120 SEEK_EDGE, where the snake moves in a Canny image gradient squared
121 potential, and is draw towards edges, or GROW_REGION, where the
122 snake maximises the resemblance of its interior to a stored
123 statistical model.
124 */
125 int aivru_snake_force_mode_get()
126 {
127     return(force_mode);
128 }
129 
130 /*
131 Sets current snake rigidity mode, see aivru_snake_force_mode_get.
132 */
133 void aivru_snake_force_mode_set(int mode)
134 {
135     force_mode = mode;
136 }
137 
138 /*
139 Returns current snake smoothing scale. This determines the range over which
140 edges can attract the snake in SEEK_EDGE mode. A value of 4 pixels is
141 reasonable for 512 by 512 images.
142 */
143 double aivru_snake_scale_get()
144 {
145     return(ims_scale_get());
146 }
147 
148 /*
149 Sets current snake smoothing scale, see aivru_snake_scale_get.
150 */
151 void aivru_snake_scale_set(double newscale)
152 {
153     ims_scale_set(newscale);
154 }
155 
156 /*
157 Sets current statistical snake internal pressure. Since it is
158 the ratio tension:stiffness:pressure which matters, pressure
159 can be kept constant.
160 */
161 double aivru_snake_pressure_get()
162 {
163     return(ims_pressure_get());
164 }
165 
166 /*
167 Gets current statistical snake internal pressure, see aivru_snake_pressure_get.
168 */
169 void aivru_snake_pressure_set(double newpressure)
170 {
171     ims_pressure_set(newpressure);
172 }
173 
174 /*
175 Gets bulk snake parameter combining tension, stiffness, and pressure,
176 higher values give smoother snakes.
177 */
178 double aivru_snake_smoothness_get()
179 {
180     return(smoothness);
181 }
182 
183 /*
184 Sets bulk snake parameter combining tension, stiffness, and pressure.
185 */
186 void aivru_snake_smoothness_set(double newsmoothness)
187 {
188     smoothness = newsmoothness;
189     ims_pressure_set(1.0);
190     ims_alpha_set(smoothness/50.0);
191     ims_beta_set(smoothness/100.0);
192 }
193 
194 /*
195 static double kdev = 3.0;
196 double aivru_snake_kdev_get()
197 {
198     return(kdev);
199 }
200 void aivru_snake_kdev_set(double newkdev)
201 {
202     kdev = newkdev;
203 }
204 */
205 
206 /*
207 Get the current statistical snake region mean. In regions with
208 mean intensity close to this value value the snake will expand.
209 */
210 double aivru_snake_region_level_get()
211 {
212     return(ims_stats_mean_get());
213 }
214 
215 /*
216 Set the current statistical snake region mean, see aivru_snake_region_level_get.
217 */
218 void aivru_snake_region_level_set(double newlevel)
219 {
220     ims_stats_mean_set(newlevel);
221 }
222 
223 /*
224 Get the current statistical snake allowed deviation from the mean.
225 In regions with intensities closer than this value to the mean
226 the snake will expand. This value is set to twice the expected
227 standard deviation of intensities (95% confidence level).
228 */
229 double aivru_snake_region_window_get()
230 {
231     double kdev = 2.0;
232     return(2.0*kdev*ims_stats_sigma_get());
233 }
234 
235 /*
236 Get the current statistical snake region allowed deviation from the mean,
237 see aivru_snake_region_window_get.
238 */
239 void aivru_snake_region_window_set(double newwindow)
240 {
241     double kdev = 2.0;
242     ims_stats_sigma_set(newwindow/(2.0*kdev));
243 }
244 
245 /*
246 Get the current number of snake steps. This is (approximately) the
247 maximum distance the snake can walk in one run.
248 */
249 int aivru_snake_steps_get()
250 {
251     return(steps);
252 }
253 
254 /*
255 Set the current number of snake steps, see aivru_snake_steps_get.
256 */
257 void aivru_snake_steps_set(int newsteps)
258 {
259     steps = newsteps;
260 }
261 
262 /*
263 Set the level (mean intensity) of regions in which edges are to be detected.
264 */
265 double aivru_snake_image_level_get()
266 {
267     return(ims_level_get());
268 }
269 
270 /*
271 Get the level (mean intensity) of regions in which edges are to be detected.
272 */
273 void aivru_snake_image_level_set(double newlevel)
274 {
275     ims_level_set(newlevel);
276 }
277 
278 /*
279 Get the window (difference from mean intensity) of regions in which
280 edges are to be detected.
281 */
282 double aivru_snake_image_window_get()
283 {
284     return(ims_window_get());
285 }
286 
287 /*
288 Set the window (difference from mean intensity) of regions in which
289 edges are to be detected.
290 */
291 void aivru_snake_image_window_set(double newwindow)
292 {
293     ims_window_set(newwindow);
294 }
295 
296 /*
297 Get the 2D string from the current working slice and VOI.
298 (This may force the creation of an edge string from a snake or spline).
299 */
300 Tstring *aivru_contour_get()
301 {
302     Tstring *str = ims_string_get();
303     return(str);
304 }
305 
306 /*
307 Set the 2D string into the current working slice and VOI.
308 Strings are force to be oriented positively.
309 */
310 void aivru_contour_set(Tstring * iccontour)
311 {
312     ims_string_set(iccontour);
313     ims_string_changed();
314 }
315 
316 /*
317 Get the snake knot list from the current working slice and VOI.
318 */
319 Tstring *aivru_snake_points_get()
320 {
321     Snake *snake = ims_snake_get();
322     static Tstring *str = NULL;
323     ss_free(str);
324     str = ss_from_snake(snake);
325     return(str);
326 }
327 
328 /*
329 Set the array of images to be used as the Imstack.
330 Aivru code will not free these images.
331 */
332 int aivru_image_set(Imrect ** pImrectArray, int arraySize)
333 {
334     int i;
335     ims_dofree_set(false);
336     ims_make(short_v, 0, arraySize, 4.0);
337     for(i = 0; i < arraySize; i++)
338     {
339         ims_z_set(i);
340         ims_image_set(pImrectArray[i]);
341     }
342     return(1);
343 }
344 
345 /*
346 Set the 2D strings describing the VOI in a number of slices.
347 using strings in the sinfo array. Empty slices have NULL strings.
348 */
349 void aivru_voi_set(aivru_snake_slice_info * sinfo, int size)
350 {
351     int i;
352     for(i = 0; i < size; i++)
353     {
354         ims_z_set(sinfo[i].zCoordinate);
355         if(sinfo[i].pString != NULL)
356             ims_string_set(sinfo[i].pString);
357     }
358 }
359 
360 /*
361 Get the lowest slice number in which the current VOI has contours defined.
362 */
363 int aivru_get_voi_lbound()
364 {
365     int lz, uz;
366     ims_voi_bounds(&lz, &uz);
367     return(lz);
368 }
369 
370 /*
371 Get the highest slice number in which the current VOI has contours defined.
372 Note this value is inclusive, Aivru usually uses non-inclusive upper bounds.
373 */
374 int aivru_get_voi_ubound()
375 {
376     int lz, uz;
377     ims_voi_bounds(&lz, &uz);
378     return(uz-1);
379 }
380 
381 /*
382 Get the slice number of the current working slice.
383 */
384 int aivru_get_working_slice()
385 {
386     return(ims_z_get());
387 }
388 
389 /*
390 Set the slice number of the current working slice.
391 */
392 void aivru_set_working_slice(int slice_no)
393 {
394     ims_z_set(slice_no);
395 }
396 
397 /*
398 Copy contour data for VOI from source_slice to dest_slice.
399 */
400 int aivru_snake_transfer(int source_slice, int dest_slice)
401 {
402     ims_z_set(source_slice);
403     ims_voi_mark();
404     ims_z_set(dest_slice);
405     ims_voi_import();
406     return(1);
407 }
408 
409 /*
410 Use all available contours in current VOI to interpolate a new
411 contour in current working slice.
412 */
413 int aivru_multi_snake_interpolate_one()
414 {
415     ims_interpolate_spline();
416     return(1);
417 }
418 
419 /*
420 Use available contours in current VOI between upper and lower slices
421 numbers to interpolate a new contours into empty slices between
422 these values. lower_slice and upper_slice are inclusive
423 (Aivru upper bonds are usually exclusive).
424 */
425 int aivru_multi_snake_interpolate_between(int lower_slice, int upper_slice)
426 {
427     ims_interpolate_all_splines_between(lower_slice, upper_slice+1);
428     return(1);
429 }
430 
431 /*
432 Use 3D string to define transverse boundary of a 3D VOI.
433 String is closed loop with points in each slice.
434 If top and bottom slices contain only one point, these
435 slices will be missed (no top or bottoms allowed, VOI is
436 a tube).
437 */
438 int aivru_multi_snake_set_occluding_boundary(Tstring * pContour)
439 {
440     ims_string_tube_set(pContour);
441     return(1);
442 }
443 
444 /*
445 Use 3D string from aivru_multi_snake_set_occluding_boundary and
446 a single contour (the lowest slice number given more than one)
447 to extrapolate a tube VOI into other slices.
448 */
449 int aivru_multi_snake_interpolate_tube()
450 {
451     ims_tube_interp();
452     return(1);
453 }
454 
455 /*
456 Convert mult-slice contour representation of VOI into a surface
457 spline representation; control points are the packed into the
458 Tosca surface representation.
459     x runs over aivru u (closed spline, repeat first control at end).
460     y runs over aivru v (open spline).
461 z coordinate is unscaled (= slice number).
462 */
463 AvVoiSurface *aivru_get_voi_surface()
464 {
465     AvVoiSurface *asurf = (AvVoiSurface *) malloc(sizeof(AvVoiSurface));
466     Splinesurf3 *surf = ims_splinesurf3_make();
467     Imstack *ims = ims_imstack_get();
468     int u, v, k;
469 
470     asurf->nx = surf->nu+1;
471     asurf->ny = surf->nv;
472     asurf->cp = tvector_alloc(0, 3*asurf->nx*asurf->ny, float);
473 
474     k = 0;
475     for(v = 0; v < surf->nv; v++)
476         for(u = 0; u < surf->nu+1; u++)
477         {
478             asurf->cp[k++] = surf->x->p[u][v];
479             asurf->cp[k++] = surf->y->p[u][v];
480             asurf->cp[k++] = imstack_zunscaled(ims, surf->z->p[u][v]);
481         }
482     return(asurf);
483 }
484 
485 /*
486 Test function for Tosca surface output representation:
487 (int) nx (points on open spline = stack num)
488 (int) ny (points on closed spline)
489 (float) cx, cy, cz for control points, for each spline, for each slice.
490 */
491 void aivru_print_voi_surface(char *filename)
492 {
493     AvVoiSurface *asurf = aivru_get_voi_surface();
494     float *cp = asurf->cp;
495     int nx = asurf->nx;
496     int ny = asurf->ny;
497     int k;
498     FILE *fp = fopen(filename, "w");
499     fprintf(fp, "%d %d\n", asurf->nx, asurf->ny);
500     for(k = 0; k < nx*ny; k++)
501     {
502         fprintf(fp, "%f %f %f\n", cp[3*k], cp[3*k+1], cp[3*k+2]);
503         if(k % ny == 0)
504             fprintf(fp, "\n");
505     }
506     fclose(fp);
507 }
508 
509 /*
510 Free all memory allocated by Aivru calls 
511 */
512 void aivru_quit()
513 {
514     ims_free();
515 }
516 
517 /*
518 Compute the statistical properties of the interior of
519 the current working contour. (Called before running
520 a statistical snake to expand into the whole region).
521 */
522 void aivru_snake_stats_compute(double *mean, double *dev)
523 {
524     ims_stat_snake_init();
525     *mean = aivru_snake_region_level_get();
526     *dev = aivru_snake_region_window_get();
527 }
528 
529 /*
530 Run a snake starting with the current working contour
531 in the current force and rigidity modes.
532 */
533 void aivru_snake_run()
534 {
535     double tension = ims_alpha_get();
536     double stiffness = ims_beta_get();
537     double pressure = ims_pressure_get();
538     if(force_mode == SEEK_EDGE)
539     {
540         if(rigidity_mode == RUN_RIGID)
541             snake_run_rts(ims_snake_get(), ims_pot_get(), steps);
542         else if(rigidity_mode == RUN_DYNAMIC)
543             snake_run_all(ims_snake_get(), ims_pot_get(),
544                 stiffness, tension, steps);
545         ims_snake_changed();
546     }
547     else if (force_mode == GROW_REGION)
548     {
549         int ss_min = 4;
550         int ss_max = 7;
551         int ss_flag = 1;
552         double kdev = 2.0;
553 
554         if(rigidity_mode == RUN_DYNAMIC)
555         {
556         ims_stat_snake_run(tension, stiffness, pressure,
557                 kdev, steps, ss_flag, ss_min, ss_max, 1.0);
558         ims_stat_snake_run(tension, stiffness, pressure,
559                 kdev, 4, ss_flag, ss_min, ss_max, 0.25);
560         }
561         else if(rigidity_mode == RUN_RIGID)
562         {
563         ims_stat_snake_trans(tension, stiffness, pressure,
564                 kdev, steps, 1.0);
565         ims_stat_snake_trans(tension, stiffness, pressure,
566                 kdev, 4, 0.25);
567         }
568     }
569 }
570 
571 /*
572 Returns 1 (true) if run again is unnecessary.
573 */
574 int aivru_snake_convergence_achieved()
575 {
576     return(0);
577 }
578 

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