55 delete upperPoints; upperPoints =
new PointList (true);
56 delete lowerPoints; lowerPoints =
new PointList (true);
65 delete upperPoints; upperPoints = NULL;
66 delete lowerPoints; lowerPoints = NULL;
67 delete upper; upper = NULL;
68 delete lower; lower = NULL;
99 if ((upperPoints->QueueSize () == 1) &&
100 (lowerPoints->QueueSize () == 1))
103 DrawLine (*dest, (*upperPoints)[0], (*lowerPoints)[0], 255);
104 CalcConvexArea (dest);
108 else if ((upperPoints->QueueSize () < 1) || (lowerPoints->QueueSize () < 1))
121 CalcConvexArea (dest);
144 if ((upperPoints->QueueSize () == 1) &&
145 (lowerPoints->QueueSize () == 1))
148 DrawLine (*dest, (*upperPoints)[0], (*lowerPoints)[0], 255);
149 CalcConvexArea (dest);
153 else if ((upperPoints->QueueSize () < 1) || (lowerPoints->QueueSize () < 1))
166 CalcConvexArea (dest);
183 return sqrt (deltaX * deltaX + deltaY * deltaY);
202 if (upper->QueueSize () == 2)
205 return Distance ((*upper)[0], (*upper)[1]);
209 if (upper->QueueSize () == 1)
216 if (upper->QueueSize () == 0)
219 PointList::iterator it;
220 it = upper->begin ();
224 PointPtr p = *it; ++it;
225 PointPtr m = *it; ++it;
227 while (it != upper->end ())
229 PointPtr current = *it;
230 area = area + TriangleArea (*p, *m, *current);
241 void ConvexHull::CalcConvexArea (RasterPtr raster)
250 for (
kkint32 col = 0; col < w; col++)
255 for (topRow = h - 1; topRow >= 0; topRow--)
264 for (botRow = 0; botRow < h; botRow++)
270 convexArea += 1 + topRow - botRow;
284 double deltaY = 1.0 + fabs ((
float)(p1
.Row () - p2
.Row ()));
285 double deltaX = 1.0 + fabs ((
float)(p1
.Col () - p2
.Col ()));
287 return deltaX * deltaX + deltaY * deltaY;
329 double ac = sqrt (acSqr);
331 double x = (abSqr - bcSqr + acSqr) / (2 * ac);
333 double h = sqrt (abSqr - (x * x));
335 double area = (h * ac) / 2.0;
373 for (row = startRow; row <= endRow; row++)
393 if (p1.Col () < p2.Col ())
395 startCol = p1.Col ();
400 startCol = p2.Col ();
404 for (col = startCol; col <= endCol; col++)
406 row = (kkint32)(m * (
double)col + c + 0.5);
407 raster.SetPixelValue (row, col, pixVal);
415 if (p1.Row () < p2.Row ())
417 startRow = p1.Row ();
422 startRow = p2.Row ();
426 for (row = startRow; row <= endRow; row++)
428 col = (kkint32)((((
double)row - c) / m) + 0.5);
429 raster.SetPixelValue (row, col, pixVal);
440 PointList::iterator it;
441 it = upper->begin ();
443 PointPtr lastPoint = NULL;
444 PointPtr nextPoint = NULL;
446 if (it != upper->end ())
447 {lastPoint = *it; ++it;}
449 if (it != upper->end ())
450 {nextPoint = *it; ++it;}
452 PointPtr firstPoint = lastPoint;
454 if ((!nextPoint) && (!lastPoint))
457 if ((nextPoint != NULL) && (lastPoint == NULL))
465 DrawLine (output, *lastPoint, *nextPoint, 255);
466 lastPoint = nextPoint;
467 if (it == upper->end ())
476 DrawLine (output, *lastPoint, *firstPoint, 255);
487 if (*(lower->LookAtFront ()) == *(upper->LookAtFront ()))
489 p = lower->PopFromFront ();
494 if (*(lower->LookAtBack ()) == *(upper->LookAtBack ()))
496 p = lower->PopFromBack ();
500 if (lower->QueueSize () > 0)
502 p = lower->PopFromFront ();
505 upper->PushOnBack (p);
506 p = lower->PopFromFront ();
597 double extendedY = m * p
.Col () + c;
600 if (extendedY == p
.Row ())
634 if (upperPoints->QueueSize () < 2)
636 KKStr msg =
"ConvexHull::BuildUpperLink *** ERROR ***, Not Enough Points to Build Upper Link.";
637 cerr << msg << std::endl;
638 throw KKException (msg);
641 PointList::iterator it;
642 it = upperPoints->begin ();
645 upper->PushOnBack (
new Point (**it)); ++it;
648 PointPtr middle =
new Point (**it); ++it;
653 while (it != upperPoints->end ())
657 old = upper->BackOfQueue ();
659 while (RelativeCCW (*old, *middle, *current) >= 0)
662 middle = upper->PopFromBack ();
664 if (upper->QueueSize () > 0)
665 old = upper->BackOfQueue ();
670 upper->PushOnBack (middle);
671 middle =
new Point (*current);
674 upper->PushOnBack (middle);
684 if (lowerPoints->QueueSize () < 2)
686 KKStr msg =
"ConvexHull::BuildLowerLink *** ERROR ***, Not Enough Points to Build Upper Link.";
687 cerr << msg << std::endl;
688 throw KKException (msg);
691 PointList::iterator it;
692 it = lowerPoints->begin ();
694 lower->PushOnBack (
new Point (**it)); ++it;
696 PointPtr middle =
new Point (**it); ++it;
697 PointPtr current = NULL;
700 while (it != lowerPoints->end ())
704 old = lower->GetLast ();
706 while (RelativeCCW (*old, *middle, *current) >= 0)
709 middle = lower->RemoveLast ();
711 if (lower->QueueSize () > 0)
712 old = lower->GetLast ();
718 middle =
new Point (*current);
745 for (
kkint32 col = 0; col < w; ++col)
748 for (row = h - 1; row >= 0; --row)
753 upperPoints->Add (u);
760 for (row = 0; row < h; row++)
765 lowerPoints->AddFirst (l);
void SetSrcRaster(RasterConstPtr _srcRaster)
void SetPixelValue(kkint32 row, kkint32 col, uchar pixVal)
A class that is used by to represent a single image in memory.
Raster(kkint32 _height, kkint32 _width)
Constructs a blank image with given dimensions; all pixels will be initialized to 0...
void ReSize(kkint32 _height, kkint32 _width, bool _color)
Lets you resize the raster dimensions; old image data will be lost.
double DistanceSquare(Point &p1, Point &p2)
Used by Raster class and MorphOp derived classes to denote a single pixel location in Raster image...
RasterPtr Filter(const Raster &src, RasterPtr dest)
bool ForegroundPixel(kkint32 row, kkint32 col) const
unsigned char uchar
Unsigned character.
RasterPtr Filter(const Raster &src)
Returns a image that represents the convex-hull of the 'src' image. param[in] src Source image that c...
Point(kkint32 _row, kkint32 _col)
void Draw(Raster &output)
Operator that will create a Convex Hull of a supplied image.
Base class for all Morphological operations.
virtual RasterPtr PerformOperation(RasterConstPtr _image)
Container object used to maintaining a list of pixel locations.
bool ForegroundPixel(uchar pixel) const
void Store(const Raster &input)
Build list of the upper and lower points in the image.