Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Modules/ImageProcessor/ImageProcessorTools/BresenhamLineScan.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file Modules/ImageProcessor/ImageProcessorTools/BresenhamLineScan.cpp
    00003 *
    00004 * Utility class which performs the Bresenham algorithm for line scanning
    00005 *
    00006 * @author <a href="mailto:timlaue@tzi.de">Tim Laue</a>
    00007 * @author <a href="mailto:walter.nistico@uni-dortmund.de">Walter Nistico</a>
    00008 * @author <a href="mailto:oberlies@sim.tu-darmstadt.de">Tobias Oberlies</a> (revised constructors and commenting)
    00009 */
    00010
    00011 #include "BresenhamLineScan.h"
    00012
    00013 void BresenhamLineScan::setup(const Vector2<int>& diff)
    00014 {
    00015   int dx = diff.x;
    00016   int dy = diff.y;
    00017   int incX = ((dx>0) ? 1:-1);
    00018   int incY = ((dy>0) ? 1:-1);
    00019   int absDx(abs(dx));
    00020   int absDy(abs(dy));
    00021   alongX = (absDy < absDx);
    00022   if(alongX)
    00023   {
    00024     baseError = -absDx;
    00025     delta = 2*absDy;
    00026     standardOffset.x = incX;
    00027     standardOffset.y = 0;
    00028     correctionOffset.x = 0;
    00029     correctionOffset.y = incY;
    00030     numberOfPixels = absDx;
    00031   }
    00032   else
    00033   {
    00034     baseError = -absDy;
    00035     delta = 2*absDx;
    00036     standardOffset.x = 0;
    00037     standardOffset.y = incY;
    00038     correctionOffset.x = incX;
    00039     correctionOffset.y = 0;
    00040     numberOfPixels = absDy;
    00041   }
    00042   resetError = 2*baseError;
    00043   error = baseError;        //added, for lines != 45 degrees
    00044 }
    00045
    00046 BresenhamLineScan::BresenhamLineScan(const Vector2<int>& start, const Vector2<int>& end)
    00047 {
    00048   setup(end-start);
    00049 }
    00050
    00051 BresenhamLineScan::BresenhamLineScan(const Vector2<double>& direction)
    00052 {
    00053   setup(Vector2<int>(static_cast<int>(direction.x*1024.0),
    00054              static_cast<int>(direction.y*1024.0)));
    00055 }
    00056
    00057 BresenhamLineScan::BresenhamLineScan(const double& direction)
    00058 {
    00059   setup(Vector2<int>(static_cast<int>(cos(direction)*1024.0),
    00060              static_cast<int>(sin(direction)*1024.0)));
    00061 }
    00062
    00063 BresenhamLineScan::BresenhamLineScan(const Vector2<int>& start, const Vector2<double>& direction, const CameraInfo& cameraInfo)
    00064 {
    00065   // Create a line through the point start with the given direction
    00066   Geometry::Line line(Vector2<double>(static_cast<double>(start.x), static_cast<double>(start.y)),
    00067               direction);
    00068   // Intersect line with image border
    00069   Vector2<int> intersection1, intersection2;
    00070   Geometry::getIntersectionPointsOfLineAndRectangle(Vector2<int>(0,0), Vector2<int>(cameraInfo.resolutionWidth-1,cameraInfo.resolutionHeight-1),
    00071                             line, intersection1, intersection2);
    00072   // Select the intersection point that is on the ray defined by start and direction
    00073   Vector2<double> delta(intersection1.x - start.x, intersection1.y - start.y);
    00074   if (delta*direction > 0)
    00075     setup(intersection1-start);
    00076   else if (delta*direction < 0)
    00077         setup(intersection2-start);
    00078   else
    00079   {
    00080     // Start point is on the edge, so compute difference to other intersection point
    00081     delta.x = intersection2.x - start.x;
    00082     delta.y = intersection2.y - start.y;
    00083     if (delta*direction > 0)
    00084       setup(intersection2-start);
    00085     else
    00086       setup(intersection1-start);
    00087   }
    00088 }
    00089
    00090 BresenhamLineScan::BresenhamLineScan(const Vector2<int>& start, const double& direction, const CameraInfo& cameraInfo)
    00091 {
    00092   const Vector2<int> frameUpperLeft(0,0);
    00093   const Vector2<int> frameLowerRight(cameraInfo.resolutionWidth-1, cameraInfo.resolutionHeight-1);
    00094   Geometry::Line scanLine(start, Vector2<double>(cos(direction), sin(direction)));
    00095   Vector2<int> lineStart, lineEnd;
    00096   Geometry::getIntersectionPointsOfLineAndRectangle(
    00097                                       frameUpperLeft,
    00098                                       frameLowerRight,
    00099                                       scanLine, lineStart, lineEnd);
    00100   Vector2<double> delta((lineEnd - start).x, (lineEnd - start).y);
    00101   Vector2<double> directionVector(cos(direction), sin(direction));
    00102   if (delta*directionVector >= 0)
    00103     setup(lineEnd-start);
    00104   else
    00105     setup(lineStart-start);
    00106 }