bresenham.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Maxim Likhachev
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the University of Pennsylvania nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00031 #include <sbpl_interface/bresenham.h>
00032 
00033 void get_bresenham3d_parameters(int p1x, int p1y, int p1z, int p2x, int p2y, int p2z, bresenham3d_param_t *params)
00034 {
00035   params->X1=p1x;
00036   params->Y1=p1y;
00037   params->Z1=p1z;
00038   params->X2=p2x;
00039   params->Y2=p2y;
00040   params->Z2=p2z;
00041 
00042   params->XIndex = params->X1;
00043   params->YIndex = params->Y1;
00044   params->ZIndex = params->Z1;
00045 
00046   params->dx = fabs((double)(p2x-p1x));
00047   params->dy = fabs((double)(p2y-p1y));
00048   params->dz = fabs((double)(p2z-p1z));
00049   params->dx2 = params->dx << 1;
00050   params->dy2 = params->dy << 1;
00051   params->dz2 = params->dz << 1;
00052 
00053   //get direction of slope
00054   if((double)(p2x-p1x) < 0)
00055     params->IncX = -1;
00056   else
00057     params->IncX = 1;
00058 
00059   if((double)(p2y-p1y) < 0)
00060     params->IncY = -1;
00061   else
00062     params->IncY = 1;
00063 
00064   if((double)(p2z-p1z) < 0)
00065     params->IncZ = -1;
00066   else
00067     params->IncZ = 1;
00068 
00069   //choose which axis to use as the index
00070   if(params->dx >= params->dy && params->dx >= params->dz)
00071   {
00072     params->UsingXYZIndex = 0;
00073     params->err1 = params->dy2 - params->dx;
00074     params->err2 = params->dz2 - params->dx;
00075   }
00076   else if(params->dy >= params->dx && params->dy >= params->dz)
00077   {
00078     params->UsingXYZIndex = 1;
00079     params->err1 = params->dx2 - params->dy;
00080     params->err2 = params->dz2 - params->dy;
00081   }
00082   else
00083   {
00084     params->UsingXYZIndex = 2;
00085     params->err1 = params->dy2 - params->dz;
00086     params->err2 = params->dx2 - params->dz;
00087   }
00088 }
00089 
00090 void get_current_point3d(bresenham3d_param_t *params, int *x, int *y, int *z)
00091 {
00092   *x = params->XIndex;
00093   *y = params->YIndex;
00094   *z = params->ZIndex;
00095 }
00096 
00097 int get_next_point3d(bresenham3d_param_t *params)
00098 {
00099   //check to see if at end of line
00100   if (params->XIndex == params->X2 && params->YIndex == params->Y2 && params->ZIndex == params->Z2)
00101     return 0;
00102 
00103   if (params->UsingXYZIndex == 0)
00104   {
00105     if (params->err1 > 0) {
00106       params->YIndex += params->IncY;
00107       params->err1 -= params->dx2;
00108     }
00109     if (params->err2 > 0) {
00110       params->ZIndex += params->IncZ;
00111       params->err2 -= params->dx2;
00112     }
00113     params->err1 += params->dy2;
00114     params->err2 += params->dz2;
00115     params->XIndex += params->IncX;
00116   }
00117   else if(params->UsingXYZIndex == 1)
00118   {
00119     if (params->err1 > 0) {
00120       params->XIndex += params->IncX;
00121       params->err1 -= params->dy2;
00122     }
00123     if (params->err2 > 0) {
00124       params->ZIndex += params->IncZ;
00125       params->err2 -= params->dy2;
00126     }
00127     params->err1 += params->dx2;
00128     params->err2 += params->dz2;
00129     params->YIndex += params->IncY;
00130   }
00131   else
00132   {
00133     if (params->err1 > 0) {
00134       params->YIndex += params->IncY;
00135       params->err1 -= params->dz2;
00136     }
00137     if (params->err2 > 0) {
00138       params->XIndex += params->IncX;
00139       params->err2 -= params->dz2;
00140     }
00141     params->err1 += params->dy2;
00142     params->err2 += params->dx2;
00143     params->ZIndex += params->IncZ;
00144   }
00145   return 1;
00146 }


sbpl_interface
Author(s): Gil Jones
autogenerated on Mon Oct 6 2014 11:11:34