$search
00001 #include "EdgeDetection/AdaptiveRasterizer.h" 00002 00003 #include "EdgeDetection/OwnedImage.h" 00004 00005 namespace EdgeDetection 00006 { 00007 Image* AdaptiveRasterizer::Rasterize(Rasterizable* object, int width, int height) 00008 { 00009 if (object == 0) throw "The parameter 'object' cannot be NULL."; 00010 if (width < 0) throw "The parameter 'threshold' was out of range."; 00011 if (height < 0) throw "The parameter 'threshold' was out of range."; 00012 00013 OwnedImage* image = new OwnedImage(width, height); 00014 00015 Rectangle region = object->GetRegion(); 00016 00017 double pixelWidth = region.GetWidth() / width; 00018 double pixelHeight = region.GetHeight() / height; 00019 00020 for (int y = 0; y < height; y++) 00021 for (int x = 0; x < width; x++) 00022 { 00023 double left = region.GetLeft() + (x + 0) * pixelWidth; 00024 double right = region.GetLeft() + (x + 1) * pixelWidth; 00025 double top = region.GetTop() + (y + 0) * pixelHeight; 00026 double bottom = region.GetTop() + (y + 1) * pixelHeight; 00027 00028 double value = GetValue(object, Rectangle(left, right, top, bottom)); 00029 00030 image->Set(x, y, value); 00031 } 00032 00033 return image; 00034 } 00035 00036 double AdaptiveRasterizer::GetValue(Rasterizable* object, Rectangle rectangle) 00037 { 00038 if (object->CanCalculateIntegral(rectangle)) return object->CalculateIntegral(rectangle); 00039 if (rectangle.GetArea() < threshold) return object->EstimateIntegral(rectangle); 00040 00041 Rectangle leftTop = Rectangle(rectangle.GetLeft(), rectangle.GetCenter().GetX(), rectangle.GetTop(), rectangle.GetCenter().GetY()); 00042 Rectangle rightTop = Rectangle(rectangle.GetCenter().GetX(), rectangle.GetRight(), rectangle.GetTop(), rectangle.GetCenter().GetY()); 00043 Rectangle leftBottom = Rectangle(rectangle.GetLeft(), rectangle.GetCenter().GetX(), rectangle.GetCenter().GetY(), rectangle.GetBottom()); 00044 Rectangle rightBottom = Rectangle(rectangle.GetCenter().GetX(), rectangle.GetRight(), rectangle.GetCenter().GetY(), rectangle.GetBottom()); 00045 00046 return GetValue(object, leftTop) + GetValue(object, rightTop) + GetValue(object, leftBottom) + GetValue(object, rightBottom); 00047 } 00048 };