高效的目标矩形源矩形裁剪

时间:2014-04-23 11:01:22

标签: c++ optimization

每次我画到一个表面时,我都要确保不要画在边界之外。我一次最多可以获得262,144次抽奖。我无法编写或修改任何底层机器指令。我需要剪切部分在屏幕上的源矩形及其相关的目标矩形。

// return if dest_rect is entirely out bounds

// if dest_rect is partially inbounds modify src_rect and dest_rect accordingly

// draw without clipping if dest_rect is entirely inbounds

我将使用SDK提供的基于浮动的矢量对象来获取一些硬件优化。我过去曾经看过一些最大限度的技巧来帮助解决类似问题,并且我不确定是否可以编写最佳代码。

编辑*要清楚我要求C ++建议如何为所描述的问题编写最佳剪辑机制。例如,可能存在一个最佳逻辑,我已经知道如何通过使用内联函数等来使代码最优化并保持最佳调用堆栈大小等。例如,建议可能只涵盖问题的一部分,例如具有最优性裁剪功能。

*编辑 我目前的解决方案:

#include <algorithm>
float Clip(float _n, float _lower, float _upper)
{

    return std::max(_lower, std::min(_n, _upper));
}

void Iw2DImage::DrawClipped(CIwFVec2 _here, CIwFVec2 _bounds_xy, CIwFVec2 _bounds_wh) const
{
    // image is a region of texture atlas starting at xy with width and height wh

    // clip the destination region by the given bounds 
    const CIwFVec2 clipped_xy(
        Clip(_here.x,                _bounds_xy.x,   _bounds_xy.x + _bounds_wh.x),
        Clip(_here.y,                _bounds_xy.y,   _bounds_xy.y + _bounds_wh.y));
    const CIwFVec2 clipped_wh(
        Clip(_here.x + atlas_wh.x,   _bounds_xy.x,   _bounds_xy.x + _bounds_wh.x),
        Clip(_here.y + atlas_wh.y,   _bounds_xy.y,   _bounds_xy.y + _bounds_wh.y));

    // no point drawing an image with 0 width or height
    if (clipped_xy.x == clipped_wh.x) return;
    if (clipped_xy.y == clipped_wh.y) return;

    // change the source xy and wh by the same change in the destination region
    const CIwFVec2 clipped_atlas_xy(atlas_xy + (clipped_xy - _here));
    const CIwFVec2 clipped_atlas_wh(atlas_wh + (clipped_wh - (_here + atlas_wh)));

    /**
     * Draw a region of an image at 1:1 size at the specified location, modulated by the current colour, using the current alphamode
     * @param image the image to draw
     * @param topLeft the top left position of the image on the screen
     * @param regionOffset the top left of the region within the image
     * @param regionSize the size of the region within the image (and thus, on the screen)
     * @see Iw2DSetColour, Iw2DSetAlphaMode, Iw2DSetImageTransform
     * @par Required Header Files
     * Iw2D.h
     */
    Iw2DDrawImageRegion(i, clipped_xy, clipped_atlas_xy, clipped_atlas_wh);

}

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解了这个问题,但如果你想检查src_rect是否在你的绘图区域内,其坐标从(0,0)(dest_rect.width(), dest_rect.height()),你可以做

之类的东西
bool outOfBonds( src_rect, dest_rect )
{
   if( src_rect.X < 0 || src_rect.X + src_rect.Width() > dest_rect.width() )
      return true; //clipping on X axis
   else if( src_rect.Y < 0 || src_rect.Y + src_rect.Height() > dest_rect.Height() )
      return true; //clipping on Y axis

   return false;//No clipping
}

如果您有两个具有随机坐标的区域,并且想要确保src_rect位于dest_rect内,请将其修改为

bool outOfBonds( src_rect, dest_rect )
{
   if( src_rect.X < dest_rect.X || 
       src_rect.X + src_rect.Width() > dest_rect.X + dest_rect.width() )
   {
      return true; //clipping on X axis
   }
   else if( src_rect.Y < dest_rect.Y || 
            src_rect.Y + src_rect.Height() > dest_rect.Y +dest_rect.Height() )
   {
      return true; //clipping on Y axis
   }

   return false;//No clipping
}
相关问题