PTdecode/CImg-1.3.0/examples/curve_editor.cpp

changeset 5
1204ebf9340d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/PTdecode/CImg-1.3.0/examples/curve_editor.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,344 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : curve_editor.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : A simple user interface to construct 2D spline curves.
    1.10 + #                This file is a part of the CImg Library project.
    1.11 + #                ( http://cimg.sourceforge.net )
    1.12 + #
    1.13 + #  Copyright   : David Tschumperle
    1.14 + #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
    1.15 + #
    1.16 + #  License     : CeCILL v2.0
    1.17 + #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
    1.18 + #
    1.19 + #  This software is governed by the CeCILL  license under French law and
    1.20 + #  abiding by the rules of distribution of free software.  You can  use,
    1.21 + #  modify and/ or redistribute the software under the terms of the CeCILL
    1.22 + #  license as circulated by CEA, CNRS and INRIA at the following URL
    1.23 + #  "http://www.cecill.info".
    1.24 + #
    1.25 + #  As a counterpart to the access to the source code and  rights to copy,
    1.26 + #  modify and redistribute granted by the license, users are provided only
    1.27 + #  with a limited warranty  and the software's author,  the holder of the
    1.28 + #  economic rights,  and the successive licensors  have only  limited
    1.29 + #  liability.
    1.30 + #
    1.31 + #  In this respect, the user's attention is drawn to the risks associated
    1.32 + #  with loading,  using,  modifying and/or developing or reproducing the
    1.33 + #  software by the user in light of its specific status of free software,
    1.34 + #  that may mean  that it is complicated to manipulate,  and  that  also
    1.35 + #  therefore means  that it is reserved for developers  and  experienced
    1.36 + #  professionals having in-depth computer knowledge. Users are therefore
    1.37 + #  encouraged to load and test the software's suitability as regards their
    1.38 + #  requirements in conditions enabling the security of their systems and/or
    1.39 + #  data to be ensured and,  more generally, to use and operate it in the
    1.40 + #  same conditions as regards security.
    1.41 + #
    1.42 + #  The fact that you are presently reading this means that you have had
    1.43 + #  knowledge of the CeCILL license and that you accept its terms.
    1.44 + #
    1.45 +*/
    1.46 +
    1.47 +#include "CImg.h"
    1.48 +using namespace cimg_library;
    1.49 +
    1.50 +// The lines below are necessary when using a non-standard compiler as visualcpp6.
    1.51 +#ifdef cimg_use_visualcpp6
    1.52 +#define std
    1.53 +#endif
    1.54 +#ifdef min
    1.55 +#undef min
    1.56 +#undef max
    1.57 +#endif
    1.58 +
    1.59 +//---------------
    1.60 +// Main procedure
    1.61 +//---------------
    1.62 +int main(int argc, char **argv) {
    1.63 +
    1.64 +  // Read command line parameters
    1.65 +  //-----------------------------
    1.66 +  cimg_usage("2D Spline Curve Editor");
    1.67 +  const char *file_i       = cimg_option("-i",(char*)0,"Input image");
    1.68 +  const float contrast     = cimg_option("-contrast",0.6f,"Image contrast");
    1.69 +  const char *file_ip      = cimg_option("-ip",(char*)0,"Input control points");
    1.70 +  const char *file_oc      = cimg_option("-oc",(char*)0,"Output curve points");
    1.71 +  const char *file_op      = cimg_option("-op",(char*)0,"Output control points");
    1.72 +  const char *file_od      = cimg_option("-od",(char*)0,"Output distance function");
    1.73 +  bool interp              = cimg_option("-poly",true,"Use polynomial interpolation");
    1.74 +  bool closed              = cimg_option("-closed",true,"Closed curve");
    1.75 +  bool show_tangents       = cimg_option("-tangents",false,"Show tangents");
    1.76 +  bool show_points         = cimg_option("-points",true,"Show control points");
    1.77 +  bool show_outline        = cimg_option("-outline",true,"Show polygon outline");
    1.78 +  bool show_indices        = cimg_option("-indices",true,"Show points indices");
    1.79 +  bool show_coordinates    = cimg_option("-coords",false,"Show points coordinates");
    1.80 +  const float precision = cimg_option("-prec",0.05f,"Precision of curve discretization");
    1.81 +
    1.82 +  // Init image data
    1.83 +  //-----------------
    1.84 +  const unsigned char yellow[] = { 255,255,0 }, white[] = { 255,255,255 }, green[] = { 0,255,0 },
    1.85 +                      red[] = { 255,0,50 }, purple[] = { 255,100,255 }, black[] = { 0,0,0 };
    1.86 +  CImg<unsigned char> img0, img, help_img;
    1.87 +  if (file_i) {
    1.88 +    std::fprintf(stderr,"\n - Load input image '%s' : ",cimg::basename(file_i));
    1.89 +    img0 = CImg<>(file_i).normalize(0,255.0f*contrast);
    1.90 +    std::fprintf(stderr,"Size = %dx%dx%dx%d \n",img0.dimx(),img0.dimy(),img0.dimz(),img0.dimv());
    1.91 +    img0.resize(-100,-100,1,3);
    1.92 +  }
    1.93 +  else {
    1.94 +    std::fprintf(stderr,"\n - No input image specified, use default 512x512 image.\n");
    1.95 +    img0.assign(512,512,1,3,0).draw_grid(32,32,0,0,false,false,green,0.4f,0xCCCCCCCC,0xCCCCCCCC);
    1.96 +  }
    1.97 +
    1.98 +  help_img.assign(270,160,1,3,0).
    1.99 +    draw_text(5,5,
   1.100 +              "------------------------------------------\n"
   1.101 +              "2D Curve Editor\n"
   1.102 +              "------------------------------------------\n"
   1.103 +              "Left button : Create or move control point\n"
   1.104 +              "Right button : Delete control point\n"
   1.105 +              "Spacebar : Switch interpolation\n"
   1.106 +              "Key 'C' : Switch open/closed mode\n"
   1.107 +              "Key 'T' : Show/hide tangents\n"
   1.108 +              "Key 'P' : Show/hide control points\n"
   1.109 +              "Key 'O' : Show/hide polygon outline\n"
   1.110 +              "Key 'N' : Show/hide points indices\n"
   1.111 +              "Key 'X' : Show/hide points coordinates\n"
   1.112 +              "Key 'H' : Show/hide this help\n"
   1.113 +              "Key 'S' : Save control points\n"
   1.114 +              "Key 'R' : Reset curve\n",
   1.115 +              green);
   1.116 +  CImgDisplay disp(img0,"2D Curve Editor",0);
   1.117 +  CImgList<float> points, curve;
   1.118 +  bool moving = false;
   1.119 +  bool help = !file_i;
   1.120 +
   1.121 +  if (file_ip) {
   1.122 +    std::fprintf(stderr," - Load input control points '%s' : ",cimg::basename(file_ip));
   1.123 +    points = CImg<>(file_ip).transpose().get_split('x');
   1.124 +    std::fprintf(stderr," %u points\n",points.size);
   1.125 +  }
   1.126 +
   1.127 +  // Enter user loop
   1.128 +  //-----------------
   1.129 +  while (!disp.is_closed && !disp.is_keyESC && !disp.is_keyQ) {
   1.130 +
   1.131 +    // Handle mouse manipulation
   1.132 +    //---------------------------
   1.133 +    const float
   1.134 +      x = disp.mouse_x*(float)img0.dimx()/disp.dimx(),
   1.135 +      y = disp.mouse_y*(float)img0.dimy()/disp.dimy();
   1.136 +    const unsigned int
   1.137 +      button = disp.button;
   1.138 +
   1.139 +    if (points && button && x>=0 && y>=0) {
   1.140 +
   1.141 +      // Find nearest point and nearest segment
   1.142 +      float dmin_pt = 1e10f, dmin_seg = dmin_pt;
   1.143 +      unsigned int p_pt = 0, p_seg = 0;
   1.144 +      cimglist_for(points,p) {
   1.145 +        const unsigned int
   1.146 +          pnext = closed?(p+1)%points.size:(p+1<points.size?p+1:p);
   1.147 +        const float
   1.148 +          xp = points(p,0),
   1.149 +          yp = points(p,1),
   1.150 +          xm = 0.5f*(xp + points(pnext,0)),
   1.151 +          ym = 0.5f*(yp + points(pnext,1));
   1.152 +        const float
   1.153 +          d_pt  = (xp-x)*(xp-x) + (yp-y)*(yp-y),
   1.154 +          d_seg = (xm-x)*(xm-x) + (ym-y)*(ym-y);
   1.155 +        if (d_pt<dmin_pt)   { dmin_pt = d_pt; p_pt = p; }
   1.156 +        if (d_seg<dmin_seg) { dmin_seg = d_seg; p_seg = p; }
   1.157 +      }
   1.158 +
   1.159 +      // Handle button
   1.160 +      if (button&1) {
   1.161 +        if (dmin_pt<100 || moving) { points(p_pt,0) = x; points(p_pt,1) = y; }
   1.162 +        else points.insert(CImg<>::vector(x,y),p_seg+1);
   1.163 +        moving = true;
   1.164 +      }
   1.165 +      if (button&2 && dmin_pt<100) {
   1.166 +        if (points.size>3) points.remove(p_pt);
   1.167 +        else points.assign();
   1.168 +        disp.button=0;
   1.169 +      }
   1.170 +    }
   1.171 +    if (!button) moving = false;
   1.172 +
   1.173 +    if (disp.key) {
   1.174 +      switch (disp.key) {
   1.175 +      case cimg::keySPACE: interp = !interp; break;
   1.176 +      case cimg::keyC: closed = !closed; break;
   1.177 +      case cimg::keyT: show_tangents = !show_tangents; break;
   1.178 +      case cimg::keyP: show_points = !show_points; break;
   1.179 +      case cimg::keyO: show_outline = !show_outline; break;
   1.180 +      case cimg::keyN: show_indices = !show_indices; break;
   1.181 +      case cimg::keyX: show_coordinates = !show_coordinates; break;
   1.182 +      case cimg::keyR: points.assign(); break;
   1.183 +      case cimg::keyH: help = !help; break;
   1.184 +      case cimg::keyS: {
   1.185 +        const char *filename = file_op?file_op:"curve_points.dlm";
   1.186 +        std::fprintf(stderr," - Save control points in '%s'\n",filename);
   1.187 +        points.get_append('x').transpose().save(filename);
   1.188 +      } break;
   1.189 +      }
   1.190 +      disp.key = 0;
   1.191 +    }
   1.192 +
   1.193 +    // Init list of points if empty
   1.194 +    //------------------------------
   1.195 +    if (!points) {
   1.196 +      const float
   1.197 +        x0 = img0.dimx()/4.0f,
   1.198 +        y0 = img0.dimy()/4.0f,
   1.199 +        x1 = img0.dimx()-x0,
   1.200 +        y1 = img0.dimy()-y0;
   1.201 +      points.insert(CImg<>::vector(x0,y0)).
   1.202 +        insert(CImg<>::vector(x1,y0)).
   1.203 +        insert(CImg<>::vector(x1,y1)).
   1.204 +        insert(CImg<>::vector(x0,y1));
   1.205 +    }
   1.206 +
   1.207 +    // Estimate curve tangents
   1.208 +    //-------------------------
   1.209 +    CImg<> tangents(points.size,2);
   1.210 +    { cimglist_for(points,p) {
   1.211 +      const unsigned int
   1.212 +        p0 = closed?(p+points.size-1)%points.size:(p?p-1:0),
   1.213 +        p1 = closed?(p+1)%points.size:(p+1<points.size?p+1:p);
   1.214 +      const float
   1.215 +        x  = points(p,0),
   1.216 +        y  = points(p,1),
   1.217 +        x0 = points(p0,0),
   1.218 +        y0 = points(p0,1),
   1.219 +        x1 = points(p1,0),
   1.220 +        y1 = points(p1,1),
   1.221 +        u0 = x-x0,
   1.222 +        v0 = y-y0,
   1.223 +        n0 = 1e-8f + (float)std::sqrt(u0*u0+v0*v0),
   1.224 +        u1 = x1-x,
   1.225 +        v1 = y1-y,
   1.226 +        n1 = 1e-8f + (float)std::sqrt(u1*u1+v1*v1),
   1.227 +        u = u0/n0 + u1/n1,
   1.228 +        v = v0/n0 + v1/n1,
   1.229 +        n = 1e-8f + (float)std::sqrt(u*u+v*v),
   1.230 +        fact = 0.5f*(n0+n1);
   1.231 +      tangents(p,0) = fact*u/n;
   1.232 +      tangents(p,1) = fact*v/n;
   1.233 +    }}
   1.234 +
   1.235 +    // Estimate 3th-order polynomial interpolation
   1.236 +    //---------------------------------------------
   1.237 +    curve.assign();
   1.238 +    const unsigned int pmax = points.size-(closed?0:1);
   1.239 +    for (unsigned int p0=0; p0<pmax; p0++) {
   1.240 +      const unsigned int
   1.241 +        p1 = closed?(p0+1)%points.size:(p0+1<points.size?p0+1:p0);
   1.242 +      const float
   1.243 +        x0 = points(p0,0),
   1.244 +        y0 = points(p0,1),
   1.245 +        x1 = points(p1,0),
   1.246 +        y1 = points(p1,1);
   1.247 +      float ax=0, bx=0, cx=0, dx=0, ay=0, by=0, cy=0, dy=0;
   1.248 +      if (interp) {
   1.249 +        const float
   1.250 +          u0 = tangents(p0,0),
   1.251 +          v0 = tangents(p0,1),
   1.252 +          u1 = tangents(p1,0),
   1.253 +          v1 = tangents(p1,1);
   1.254 +        ax = 2*(x0-x1)+u0+u1;
   1.255 +        bx = 3*(x1-x0)-2*u0-u1;
   1.256 +        cx = u0;
   1.257 +        dx = x0;
   1.258 +        ay = 2*(y0-y1)+v0+v1;
   1.259 +        by = 3*(y1-y0)-2*v0-v1;
   1.260 +        cy = v0;
   1.261 +        dy = y0;
   1.262 +      } else {
   1.263 +        ax = ay = bx = by = 0;
   1.264 +        dx = x0;
   1.265 +        dy = y0;
   1.266 +        cx = (x1-x0);
   1.267 +        cy = (y1-y0);
   1.268 +      }
   1.269 +      const float tmax = 1+precision;
   1.270 +      for (float t=0; t<tmax; t+=precision) {
   1.271 +        const float
   1.272 +          xt = ax*t*t*t + bx*t*t + cx*t + dx,
   1.273 +          yt = ay*t*t*t + by*t*t + cy*t + dy;
   1.274 +        curve.insert(CImg<>::vector(xt,yt));
   1.275 +      }
   1.276 +    }
   1.277 +
   1.278 +    // Draw curve and display image
   1.279 +    //-------------------------------
   1.280 +    const float
   1.281 +      factx = (float)disp.dimx()/img0.dimx(),
   1.282 +      facty = (float)disp.dimy()/img0.dimy();
   1.283 +    img = img0.get_resize(disp.dimx(),disp.dimy());
   1.284 +    if (help) img.draw_image(help_img,0.6f);
   1.285 +    if (interp && show_outline) {
   1.286 +      CImg<> npoints = points.get_append('x');
   1.287 +      npoints.get_shared_line(0)*=factx;
   1.288 +      npoints.get_shared_line(1)*=facty;
   1.289 +      img.draw_polygon(npoints,red,0.4f);
   1.290 +      if (closed) img.draw_polygon(npoints,yellow,0.8f,0x11111111);
   1.291 +      else img.draw_line(npoints,yellow,0.8f,0x11111111);
   1.292 +    }
   1.293 +    CImg<> ncurve = curve.get_append('x');
   1.294 +    ncurve.get_shared_line(0)*=factx;
   1.295 +    ncurve.get_shared_line(1)*=facty;
   1.296 +    if (closed) img.draw_polygon(ncurve,white,1.0f,~0U);
   1.297 +    else img.draw_line(ncurve,white);
   1.298 +
   1.299 +    if (show_points) cimglist_for(points,p) {
   1.300 +      const float
   1.301 +        x = points(p,0)*factx,
   1.302 +        y = points(p,1)*facty;
   1.303 +      if (show_tangents) {
   1.304 +        const float
   1.305 +          u = tangents(p,0),
   1.306 +          v = tangents(p,1),
   1.307 +          n = 1e-8f + (float)std::sqrt(u*u+v*v),
   1.308 +          nu = u/n,
   1.309 +          nv = v/n;
   1.310 +        img.draw_arrow((int)(x-15*nu),(int)(y-15*nv),(int)(x+15*nu),(int)(y+15*nv),green);
   1.311 +      }
   1.312 +      if (show_indices) img.draw_text((int)x,(int)(y-16),"%d",purple,black,1,6,p);
   1.313 +      if (show_coordinates) img.draw_text((int)(x-24),(int)(y+8),"(%d,%d)",yellow,black,0.5f,6,(int)points(p,0),(int)points(p,1));
   1.314 +      img.draw_circle((int)x,(int)y,3,red,0.7f);
   1.315 +    }
   1.316 +
   1.317 +    img.display(disp);
   1.318 +    disp.wait();
   1.319 +
   1.320 +    if (disp.is_resized) disp.resize(false);
   1.321 +  }
   1.322 +
   1.323 +  // Save output result and exit
   1.324 +  //-----------------------------
   1.325 +  if (file_op) {
   1.326 +    std::fprintf(stderr," - Save control points in '%s'\n",cimg::basename(file_op));
   1.327 +    points.get_append('x').transpose().save(file_op);
   1.328 +  }
   1.329 +  if (file_oc) {
   1.330 +    std::fprintf(stderr," - Save curve points in '%s'\n",cimg::basename(file_oc));
   1.331 +    curve.get_append('x').transpose().save(file_oc);
   1.332 +  }
   1.333 +  if (file_od) {
   1.334 +    std::fprintf(stderr," - Computing distance function, please wait...."); std::fflush(stderr);
   1.335 +    CImg<> ncurve = (closed?(+curve).insert(curve[0]):curve).get_append('x');
   1.336 +    const float zero = 0.0f, one = 1.0f;
   1.337 +    CImg<> distance =
   1.338 +      CImg<>(img0.dimx(),img0.dimy(),1,1,-1.0f).draw_line(ncurve,&zero).draw_fill(0,0,&one).
   1.339 +      distance_hamilton(200);
   1.340 +    std::fprintf(stderr,"\n - Save distance function in '%s'\n",cimg::basename(file_od));
   1.341 +    distance.save(file_od);
   1.342 +  }
   1.343 +
   1.344 +  std::fprintf(stderr," - Exit.\n");
   1.345 +  std::exit(0);
   1.346 +  return 0;
   1.347 +}