AFNI Message Board

Dear AFNI users-

We are very pleased to announce that the new AFNI Message Board framework is up! Please join us at:

https://discuss.afni.nimh.nih.gov

Existing user accounts have been migrated, so returning users can login by requesting a password reset. New users can create accounts, as well, through a standard account creation process. Please note that these setup emails might initially go to spam folders (esp. for NIH users!), so please check those locations in the beginning.

The current Message Board discussion threads have been migrated to the new framework. The current Message Board will remain visible, but read-only, for a little while.

Sincerely, AFNI HQ

History of AFNI updates  

|
November 26, 2022 07:14PM
Well, OK. If your points are all in a single plane, as in that image, here is a script to connect successive points (defined as a 3-column text file of XYZ locations, and the first line should be repeated as the last, so that the shape gets closed.

I used the following file as the "points.txt" below:
-6.2    6.2   43.8
    6.2    6.2   43.8
   16.2    6.2   41.2
   23.8    6.2   36.2
   28.8    6.2   26.2
   21.2    6.2   18.8
   11.2    6.2   21.2
   -8.8    6.2   23.8
   -3.8    6.2   11.2
    6.2    6.2    6.2
   -3.8    6.2   -1.2
  -16.2    6.2    3.8
  -28.8    6.2    8.8
  -31.2    6.2   21.2
  -28.8    6.2   38.8
  -13.8    6.2   41.2
   -6.2    6.2   43.8
... and the input dataset (for setting the grid), was the "mask_epi_extents+tlrc.HEAD" in the output FT.results directory of the AFNI Bootcamp s05* output script. but it should probably work in many datasets with a reaonable field of view, just as an example.

The images show the points-to-be-connected as white underlay points, and then lines that are created to connect them (just looping over all successive pairs of points in the file, walking with a reasonably small step size (based on min voxel dim) to the next point), and then the planar infill of *that*.

Hope that might be useful. This would not extend to a case of arbitrary points in 3D---it depends on them being in a single plane, so that a closed curve can be drawn.

--pt


#!/bin/tcsh

# this program tries to connect points with a line.  The file "

set dset_grid = mask_epi_extents+tlrc.HEAD      # dset with grid to use
set file_pts  = points.txt                           # input text: XYZ coord
set dset_lines = dset_connect_the_dots.nii.gz        # output dset

# ----------------------------------------------------------------------

set npts   = `cat "${file_pts}" | wc -l`       # number of points in file

# set step size from voxel dims
set ad3    = `3dinfo -ad3 "${dset_grid}"`      # input voxel dims
set minad3 = ${ad3[1]}
foreach ii ( `seq 2 1 $#ad3` )
    if ( `echo "${ad3[$ii]} < ${minad3}" | bc` ) then
        set minad3 = ${ad3[$ii]}
    endif
end
set step   = `echo "scale=5; ${minad3}/3.0" | bc`
echo "++ The step is: ${step} (from vox: ${ad3})"


# create file: temporary list of points for this line
set file_tmp = _tmp_file_line.txt
set dset_tmp = _tmp_file_line.nii.gz
printf "" > ${file_tmp}

foreach nn ( `seq 2 1 ${npts}` )
    
    @ mm = ${nn} - 1

    # XYZ coords of points
    set xyz0 = `sed -n ${mm}p "${file_pts}"`        # start point
    set xyz1 = `sed -n ${nn}p "${file_pts}"`        # end point

    # dist between start and end points
    set dist = `echo "scale=5; sqrt(($xyz1[1] - $xyz0[1])^2 + ($xyz1[2] - $xyz0[2])^2 + ($xyz1[3] - $xyz0[3])^2)" | bc`

    # how many steps to take; shouldn't need to bother about
    # remainder, bc both endpoints are already included
    set nstep = `echo "scale=0; ${dist}/${step}" | bc`
    
    echo "++ The dist and nstep are:  ${dist} and ${nstep}"

    set coord = ( $xyz0 )
    foreach ss ( `seq 1 1 ${nstep}` )
        foreach ii ( `seq 1 1 $#ad3` )
            set coord[$ii] = `echo "scale=5; ${coord[$ii]} + ${step}*($xyz1[$ii] - $xyz0[$ii])/${dist}" | bc`
            # start walkin'
            printf "%10.6f %10.6f %10.6f\n" ${coord[1]} ${coord[2]} ${coord[3]} >> ${file_tmp}
        end
    end


    echo "-----------------"
    cat  "${file_tmp}"
    echo "-----------------"

end

# remove repeated locations
cat  "${file_tmp}" | uniq > FINAL.txt

#create a starter file with the given points
3dUndump                                                                \
    -overwrite                                                          \
    -xyz                                                                \
    -orient  RAI                                                        \
    -master  "${dset_grid}"                                             \
    -prefix  "${dset_tmp}"                                              \
    -datum   short                                                      \
    FINAL.txt

# fill holes in any plane (I think)
3dmask_tool                                                                  \
    -fill_dirs   AP                                                          \
    -fill_dirs   IS                                                          \
    -fill_dirs   LR                                                          \
    -fill_holes                                                              \
    -overwrite                                                               \
    -inputs      "${dset_tmp}"                                               \
    -prefix      "${dset_lines}"

exit 0
Attachments:
open | download - pts_with_lines.jpg (3.3 KB)
open | download - pts_filled.jpg (3.1 KB)
Subject Author Posted

Filling a closed curve ROI from coordinates of the vertices Attachments

gbarisano November 21, 2022 11:15PM

Re: Filling a closed curve ROI from coordinates of the vertices

ptaylor November 22, 2022 06:21AM

Re: Filling a closed curve ROI from coordinates of the vertices

gbarisano November 23, 2022 11:13PM

Re: Filling a closed curve ROI from coordinates of the vertices Attachments

ptaylor November 26, 2022 07:14PM

Re: Filling a closed curve ROI from coordinates of the vertices

ptaylor November 26, 2022 07:16PM

Re: Filling a closed curve ROI from coordinates of the vertices Attachments

gbarisano November 28, 2022 02:42PM

Re: Filling a closed curve ROI from coordinates of the vertices

ptaylor November 28, 2022 03:08PM

Re: Filling a closed curve ROI from coordinates of the vertices

gbarisano November 29, 2022 02:44PM

Re: Filling a closed curve ROI from coordinates of the vertices

ptaylor November 29, 2022 07:29PM

Re: Filling a closed curve ROI from coordinates of the vertices

Daniel Glen December 05, 2022 04:35PM