|
Shinya Kitaoka |
79e39b |
#include <memory></memory>
|
|
Shinya Kitaoka |
79e39b |
#include <array></array>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "toonz/tdistort.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "traster.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "trastercm.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tgeometry.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tpixelutils.h"
|
|
Toshihiro Shimizu |
890ddd |
#include <qlist></qlist>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//****************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Local namespace stuff
|
|
Toshihiro Shimizu |
890ddd |
//****************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
inline double dist(const TPointD &a, const TPointD &b)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return norm(b - a);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//========================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
typedef struct
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TUINT32 val;
|
|
Toshihiro Shimizu |
890ddd |
double tot;
|
|
Toshihiro Shimizu |
890ddd |
} BLOB24;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 filterPixel(const TPointD &pos, const TRasterCM32P &rasIn)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TPointD distance = TPointD(areAlmostEqual(pos.x, tfloor(pos.x), 0.001) ? 0.0 : fabs(pos.x - tfloor(pos.x)),
|
|
Toshihiro Shimizu |
890ddd |
areAlmostEqual(pos.y, tfloor(pos.y), 0.001) ? 0.0 : fabs(pos.y - tfloor(pos.y)));
|
|
Toshihiro Shimizu |
890ddd |
TPoint nearPos(tfloor(pos.x), tfloor(pos.y));
|
|
Toshihiro Shimizu |
890ddd |
if (distance == TPointD(0.0, 0.0)) {
|
|
Toshihiro Shimizu |
890ddd |
if (nearPos.x >= 0 && nearPos.x < rasIn->getLx() && nearPos.y >= 0 && nearPos.y < rasIn->getLy())
|
|
Toshihiro Shimizu |
890ddd |
return rasIn->pixels(nearPos.y)[nearPos.x];
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
return TPixelCM32();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int i, j, k = 0;
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 P[4];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (j = 0; j < 2; ++j)
|
|
Toshihiro Shimizu |
890ddd |
for (i = 0; i < 2; ++i)
|
|
Toshihiro Shimizu |
890ddd |
P[i + 2 * j] = nearPos.x + i < rasIn->getLx() && nearPos.x + i >= 0 && nearPos.y + j < rasIn->getLy() && nearPos.y + j >= 0 ? rasIn->pixels(nearPos.y + j)[nearPos.x + i] : TPixelCM32();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (P[0] == P[1] && P[1] == P[2] && P[2] == P[3])
|
|
Toshihiro Shimizu |
890ddd |
return P[0];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double w[4], sum = 0;
|
|
Toshihiro Shimizu |
890ddd |
for (j = 1; j >= 0; --j)
|
|
Toshihiro Shimizu |
890ddd |
for (i = 1; i >= 0; --i)
|
|
Toshihiro Shimizu |
890ddd |
sum += w[k++] = fabs(distance.x - i) * fabs(distance.y - j);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (i = 0; i < 4; i++)
|
|
Toshihiro Shimizu |
890ddd |
w[i] /= sum;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 outPix;
|
|
Toshihiro Shimizu |
890ddd |
TUINT32 tone;
|
|
Toshihiro Shimizu |
890ddd |
double tone_tot = 0.0;
|
|
Toshihiro Shimizu |
890ddd |
BLOB24 currBlob, paintBlobs[4], inkBlobs[4];
|
|
Toshihiro Shimizu |
890ddd |
int paintBlobsSize = 0, inkBlobsSize = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Doing the same thing in TRop::resample
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (i = 0; i < 4; ++i) {
|
|
Toshihiro Shimizu |
890ddd |
//Build paint blobs and sort them
|
|
Toshihiro Shimizu |
890ddd |
tone = P[i].getTone();
|
|
Toshihiro Shimizu |
890ddd |
tone_tot += tone * w[i];
|
|
Toshihiro Shimizu |
890ddd |
currBlob.val = P[i].getPaint();
|
|
Toshihiro Shimizu |
890ddd |
currBlob.tot = tone * w[i];
|
|
Toshihiro Shimizu |
890ddd |
for (j = 0; j < paintBlobsSize; j++)
|
|
Toshihiro Shimizu |
890ddd |
if (paintBlobs[j].val == currBlob.val)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
if (j < paintBlobsSize)
|
|
Toshihiro Shimizu |
890ddd |
paintBlobs[j].tot += currBlob.tot;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
paintBlobs[paintBlobsSize++] = currBlob;
|
|
Toshihiro Shimizu |
890ddd |
for (; j > 0 && paintBlobs[j].tot > paintBlobs[j - 1].tot; j--)
|
|
Toshihiro Shimizu |
890ddd |
tswap(paintBlobs[j], paintBlobs[j - 1]);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Same for ink blobs
|
|
Toshihiro Shimizu |
890ddd |
currBlob.val = P[i].getInk();
|
|
Toshihiro Shimizu |
890ddd |
currBlob.tot = (TPixelCM32::getToneMask() - tone) * w[i];
|
|
Toshihiro Shimizu |
890ddd |
for (j = 0; j < inkBlobsSize; j++)
|
|
Toshihiro Shimizu |
890ddd |
if (inkBlobs[j].val == currBlob.val)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
if (j < inkBlobsSize)
|
|
Toshihiro Shimizu |
890ddd |
inkBlobs[j].tot += currBlob.tot;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
inkBlobs[inkBlobsSize++] = currBlob;
|
|
Toshihiro Shimizu |
890ddd |
for (; j > 0 && inkBlobs[j].tot > inkBlobs[j - 1].tot; j--)
|
|
Toshihiro Shimizu |
890ddd |
tswap(inkBlobs[j], inkBlobs[j - 1]);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
tone = troundp(tone_tot);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
outPix.setPaint(paintBlobs[0].val);
|
|
Toshihiro Shimizu |
890ddd |
outPix.setInk(inkBlobs[0].val);
|
|
Toshihiro Shimizu |
890ddd |
outPix.setTone(tone);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return outPix;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void resample(const TRasterCM32P &rasIn, TRasterCM32P &rasOut, const TDistorter &distorter, const TPoint &p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (rasOut->getLx() < 1 || rasOut->getLy() < 1 || rasIn->getLx() < 1 || rasIn->getLy() < 1)
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<tpointd[]> preImages(new TPointD[distorter.maxInvCount()]);</tpointd[]>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int x, y;
|
|
Toshihiro Shimizu |
890ddd |
int i;
|
|
Toshihiro Shimizu |
890ddd |
for (y = 0; y < rasOut->getLy(); y++) {
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 *pix = rasOut->pixels(y);
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 *endPix = pix + rasOut->getLx();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (x = 0; pix != endPix; pix++, x++) {
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 pixDown;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
79e39b |
int count = distorter.invMap(convert(p) + TPointD(x + 0.5, y + 0.5), preImages.get());
|
|
Toshihiro Shimizu |
890ddd |
for (i = count - 1; i >= 0; --i) {
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 pixUp;
|
|
Toshihiro Shimizu |
890ddd |
TPointD preImage(preImages[i].x - 0.5, preImages[i].y - 0.5);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (preImage.x > -1 && preImage.y > -1 && preImage.x < rasIn->getLx() + 1 && preImage.y < rasIn->getLy() + 1)
|
|
Toshihiro Shimizu |
890ddd |
pixUp = filterPixel(preImage, rasIn);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
pixDown.setPaint(pixUp.getPaint() ? pixUp.getPaint() : pixDown.getPaint());
|
|
Toshihiro Shimizu |
890ddd |
pixDown.setInk(pixUp.getInk() ? pixUp.getInk() : pixDown.getInk());
|
|
Toshihiro Shimizu |
890ddd |
pixDown.setTone(pixUp == TPixelCM32() ? pixDown.getTone() : pixUp.getTone());
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
*pix = pixDown;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename channel_type="" pixel,="" typename=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
PIXEL closest_pixel(const TPointD &pos, const TRasterPT<pixel> &rasIn)</pixel>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TPoint nearPos(tround(pos.x), tround(pos.y));
|
|
Toshihiro Shimizu |
890ddd |
if (!rasIn->getBounds().contains(nearPos))
|
|
Toshihiro Shimizu |
890ddd |
return PIXEL(0, 0, 0, 0);
|
|
Toshihiro Shimizu |
890ddd |
return rasIn->pixels(nearPos.y)[nearPos.x];
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename channel_type="" t,="" typename=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
void resampleClosestPixel(const TRasterPT<t> &rasIn, TRasterPT<t> &rasOut,</t></t>
|
|
Toshihiro Shimizu |
890ddd |
const TDistorter &distorter, const TPoint &p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (rasOut->getLx() < 1 || rasOut->getLy() < 1 || rasIn->getLx() < 1 || rasIn->getLy() < 1)
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<tpointd[]> preImages(new TPointD[distorter.maxInvCount()]);</tpointd[]>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int x, y;
|
|
Toshihiro Shimizu |
890ddd |
int i;
|
|
Toshihiro Shimizu |
890ddd |
for (y = 0; y < rasOut->getLy(); y++) {
|
|
Toshihiro Shimizu |
890ddd |
T *pix = rasOut->pixels(y);
|
|
Toshihiro Shimizu |
890ddd |
T *endPix = pix + rasOut->getLx();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
x = 0;
|
|
Toshihiro Shimizu |
890ddd |
for (; pix != endPix; pix++, x++) {
|
|
Toshihiro Shimizu |
890ddd |
T pixDown(0, 0, 0, 0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//preImages.clear();
|
|
Shinya Kitaoka |
79e39b |
int count = distorter.invMap(convert(p) + TPointD(x + 0.5, y + 0.5), preImages.get());
|
|
Toshihiro Shimizu |
890ddd |
for (i = count - 1; i >= 0; --i) {
|
|
Toshihiro Shimizu |
890ddd |
T pixUp(0, 0, 0, 0);
|
|
Toshihiro Shimizu |
890ddd |
TPointD preImage(preImages[i].x - 0.5, preImages[i].y - 0.5);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (preImage.x > -1 && preImage.y > -1 && preImage.x < rasIn->getLx() + 1 && preImage.y < rasIn->getLy() + 1)
|
|
Toshihiro Shimizu |
890ddd |
pixUp = closest_pixel<t, channel_type="">(preImage, rasIn);</t,>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
pixDown = pixUp;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
*pix = pixDown;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 closest_pixel(const TPointD &pos, const TRasterCM32P &rasIn)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TPoint nearPos(tround(pos.x), tround(pos.y));
|
|
Toshihiro Shimizu |
890ddd |
if (!rasIn->getBounds().contains(nearPos))
|
|
Toshihiro Shimizu |
890ddd |
return TPixelCM32(0, 0, 255);
|
|
Toshihiro Shimizu |
890ddd |
return rasIn->pixels(nearPos.y)[nearPos.x];
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void resampleClosestPixel(const TRasterCM32P &rasIn, TRasterCM32P &rasOut,
|
|
Toshihiro Shimizu |
890ddd |
const TDistorter &distorter, const TPoint &p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (rasOut->getLx() < 1 || rasOut->getLy() < 1 || rasIn->getLx() < 1 || rasIn->getLy() < 1)
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<tpointd[]> preImages(new TPointD[distorter.maxInvCount()]);</tpointd[]>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int x, y;
|
|
Toshihiro Shimizu |
890ddd |
int i;
|
|
Toshihiro Shimizu |
890ddd |
for (y = 0; y < rasOut->getLy(); y++) {
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 *pix = rasOut->pixels(y);
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 *endPix = pix + rasOut->getLx();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
x = 0;
|
|
Toshihiro Shimizu |
890ddd |
for (; pix != endPix; pix++, x++) {
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 pixDown(0, 0, 255);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
79e39b |
int count = distorter.invMap(convert(p) + TPointD(x + 0.5, y + 0.5), preImages.get());
|
|
Toshihiro Shimizu |
890ddd |
for (i = count - 1; i >= 0; --i) {
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 pixUp(0, 0, 255);
|
|
Toshihiro Shimizu |
890ddd |
TPointD preImage(preImages[i].x - 0.5, preImages[i].y - 0.5);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (preImage.x > -1 && preImage.y > -1 && preImage.x < rasIn->getLx() + 1 && preImage.y < rasIn->getLy() + 1)
|
|
Toshihiro Shimizu |
890ddd |
pixUp = closest_pixel(preImage, rasIn);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
pixDown = pixUp;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
*pix = pixDown;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename channel_type="" pixel,="" typename=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
PIXEL filterPixel(double a, double b, PIXEL *lineSrc, int lineLength, int lineWrap)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//Retrieve the interesting pixel interval
|
|
Shinya Kitaoka |
12c444 |
double x0 = std::max(a, 0.0);
|
|
Shinya Kitaoka |
12c444 |
double x1 = std::min(b, (double)lineLength);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int x0Floor = tfloor(x0);
|
|
Toshihiro Shimizu |
890ddd |
int x0Ceil = tceil(x0);
|
|
Toshihiro Shimizu |
890ddd |
int x1Floor = tfloor(x1);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (x0 >= x1)
|
|
Toshihiro Shimizu |
890ddd |
return PIXEL::Transparent;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PIXEL *pix = lineSrc + x0Floor * lineWrap;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build the sums
|
|
Toshihiro Shimizu |
890ddd |
double k, sumr = 0, sumg = 0, sumb = 0, summ = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Fractionary part on the beginning
|
|
Toshihiro Shimizu |
890ddd |
if (x0Ceil > x0) {
|
|
Toshihiro Shimizu |
890ddd |
k = x0Ceil - x0;
|
|
Toshihiro Shimizu |
890ddd |
sumr += k * pix->r;
|
|
Toshihiro Shimizu |
890ddd |
sumg += k * pix->g;
|
|
Toshihiro Shimizu |
890ddd |
sumb += k * pix->b;
|
|
Toshihiro Shimizu |
890ddd |
summ += k * pix->m;
|
|
Toshihiro Shimizu |
890ddd |
pix += lineWrap;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Intermediate pixels (ie when pixels contract)
|
|
Toshihiro Shimizu |
890ddd |
int x;
|
|
Toshihiro Shimizu |
890ddd |
for (x = x0Ceil; x < x1Floor; ++x, pix += lineWrap) {
|
|
Toshihiro Shimizu |
890ddd |
sumr += pix->r;
|
|
Toshihiro Shimizu |
890ddd |
sumg += pix->g;
|
|
Toshihiro Shimizu |
890ddd |
sumb += pix->b;
|
|
Toshihiro Shimizu |
890ddd |
summ += pix->m;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Fractionary part on the end
|
|
Toshihiro Shimizu |
890ddd |
if (x1 < lineLength) {
|
|
Toshihiro Shimizu |
890ddd |
k = x1 - x;
|
|
Toshihiro Shimizu |
890ddd |
sumr += k * pix->r;
|
|
Toshihiro Shimizu |
890ddd |
sumg += k * pix->g;
|
|
Toshihiro Shimizu |
890ddd |
sumb += k * pix->b;
|
|
Toshihiro Shimizu |
890ddd |
summ += k * pix->m;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Finally, divide per the total weight
|
|
Toshihiro Shimizu |
890ddd |
double length = b - a; // 'transparent' pixels outside image range *are* considered
|
|
Toshihiro Shimizu |
890ddd |
sumr = sumr / length;
|
|
Toshihiro Shimizu |
890ddd |
sumg = sumg / length;
|
|
Toshihiro Shimizu |
890ddd |
sumb = sumb / length;
|
|
Toshihiro Shimizu |
890ddd |
summ = summ / length;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return PIXEL(sumr, sumg, sumb, summ);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename channel_type="" pixel,="" typename=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
PIXEL filterPixel(double a, double b, double c, double d, const TRasterPT<pixel> &rasIn, PIXEL *temp)</pixel>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (bool(a < b) == false && bool(b <= a) == false)
|
|
Toshihiro Shimizu |
890ddd |
return PIXEL::Transparent; //NaNs
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
std::swap(a, b);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (bool(c < d) == false && bool(d <= c) == false)
|
|
Toshihiro Shimizu |
890ddd |
return PIXEL::Transparent;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
std::swap(c, d);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Deal the magnification case, assuming that intervals have at least length 1. This actually stands for:
|
|
Toshihiro Shimizu |
890ddd |
// 1. Their midpoint is bilinear filtered whenever their former length wass less than 1 (see fractionary
|
|
Toshihiro Shimizu |
890ddd |
// parts computing above).
|
|
Toshihiro Shimizu |
890ddd |
// 2. This behaviour is continuous with respect to interval lengths - that is, we pass from supersampling to
|
|
Toshihiro Shimizu |
890ddd |
// subsampling in a smooth manner.
|
|
Toshihiro Shimizu |
890ddd |
if (b - a < 1) {
|
|
Toshihiro Shimizu |
890ddd |
double v = 0.5 * (a + b);
|
|
Toshihiro Shimizu |
890ddd |
a = v - 0.5;
|
|
Toshihiro Shimizu |
890ddd |
b = v + 0.5;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (d - c < 1) {
|
|
Toshihiro Shimizu |
890ddd |
double v = 0.5 * (c + d);
|
|
Toshihiro Shimizu |
890ddd |
c = v - 0.5;
|
|
Toshihiro Shimizu |
890ddd |
d = v + 0.5;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Now, filter each column in [a, b]
|
|
Shinya Kitaoka |
12c444 |
double x0 = std::max(a, 0.0);
|
|
Shinya Kitaoka |
12c444 |
double x1 = std::min(b, (double)rasIn->getLx());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (x0 >= x1)
|
|
Toshihiro Shimizu |
890ddd |
return PIXEL::Transparent;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int xEnd = tceil(x1);
|
|
Toshihiro Shimizu |
890ddd |
for (int x = tfloor(x0); x < xEnd; ++x)
|
|
Toshihiro Shimizu |
890ddd |
temp[x] = filterPixel<pixel, channel_type="">(c, d, rasIn->pixels(0) + x, rasIn->getLy(), rasIn->getWrap());</pixel,>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Then, filter temp
|
|
Toshihiro Shimizu |
890ddd |
return filterPixel<pixel, channel_type="">(a, b, temp, rasIn->getLx(), 1);</pixel,>
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename channel_type="" t,="" typename=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
void resample(const TRasterPT<t> &rasIn, TRasterPT<t> &rasOut, const TDistorter &distorter, const TPoint &p)</t></t>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (rasOut->getLx() < 1 || rasOut->getLy() < 1 || rasIn->getLx() < 1 || rasIn->getLy() < 1)
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int invsCount = distorter.maxInvCount();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Allocate buffers
|
|
Shinya Kitaoka |
79e39b |
std::array<std::unique_ptr<tpointd[]>, 2> invs = {</std::unique_ptr<tpointd[]>
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<tpointd[]>(new TPointD[invsCount * (rasOut->getLx() + 1)]),</tpointd[]>
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<tpointd[]>(new TPointD[invsCount * (rasOut->getLx() + 1)]),</tpointd[]>
|
|
Shinya Kitaoka |
79e39b |
};
|
|
Shinya Kitaoka |
79e39b |
std::array<std::unique_ptr<int[]>, 2> counts = {</std::unique_ptr<int[]>
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<int[]>(new int[rasOut->getLx() + 1]),</int[]>
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<int[]>(new int[rasOut->getLx() + 1]),</int[]>
|
|
Shinya Kitaoka |
79e39b |
};
|
|
Shinya Kitaoka |
79e39b |
std::unique_ptr<t[]> temp(new T[rasIn->getLx()]);</t[]>
|
|
Toshihiro Shimizu |
890ddd |
TPointD shift(convert(p) + TPointD(0.5, 0.5));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Fill in the first inverses (lower edge of output image)
|
|
Shinya Kitaoka |
79e39b |
{
|
|
Shinya Kitaoka |
79e39b |
TPointD* currOldInv = invs[0].get();
|
|
Shinya Kitaoka |
79e39b |
int* oldCounts = counts[0].get();
|
|
Shinya Kitaoka |
79e39b |
for (int x = 0; x <= rasOut->getLx(); currOldInv += invsCount, ++x)
|
|
Shinya Kitaoka |
79e39b |
oldCounts[x] = distorter.invMap(shift + TPointD(x, 0.0), currOldInv);
|
|
Shinya Kitaoka |
79e39b |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//For each output row
|
|
Shinya Kitaoka |
79e39b |
for (int y = 0; y < rasOut->getLy(); ++y) {
|
|
Toshihiro Shimizu |
890ddd |
//Alternate inverse buffers
|
|
Shinya Kitaoka |
79e39b |
TPointD* oldInvs = invs[y % 2].get();
|
|
Shinya Kitaoka |
79e39b |
TPointD* newInvs = invs[(y + 1) % 2].get();
|
|
Shinya Kitaoka |
79e39b |
int* oldCounts = counts[y % 2].get();
|
|
Shinya Kitaoka |
79e39b |
int* newCounts = counts[(y + 1) % 2].get();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build the new inverses
|
|
Shinya Kitaoka |
79e39b |
{
|
|
Shinya Kitaoka |
79e39b |
TPointD* currNewInv = newInvs;
|
|
Shinya Kitaoka |
79e39b |
for (int x = 0; x <= rasOut->getLx(); currNewInv += invsCount, ++x)
|
|
Shinya Kitaoka |
79e39b |
newCounts[x] = distorter.invMap(shift + TPointD(x, y + 1.0), currNewInv);
|
|
Shinya Kitaoka |
79e39b |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Filter each pixel in the row
|
|
Toshihiro Shimizu |
890ddd |
T *pix = rasOut->pixels(y);
|
|
Shinya Kitaoka |
79e39b |
TPointD* currOldInv = oldInvs;
|
|
Shinya Kitaoka |
79e39b |
TPointD* currNewInv = newInvs;
|
|
Shinya Kitaoka |
79e39b |
for (int x = 0; x < rasOut->getLx(); currOldInv += invsCount, currNewInv += invsCount, ++x, ++pix) {
|
|
Toshihiro Shimizu |
890ddd |
T pixDown(0, 0, 0, 0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
12c444 |
int count = std::min({oldCounts[x], oldCounts[x + 1], newCounts[x]});
|
|
Shinya Kitaoka |
79e39b |
for (int i = 0; i < count; ++i) {
|
|
Toshihiro Shimizu |
890ddd |
T pixUp(0, 0, 0, 0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
pixUp = filterPixel<t, channel_type="">(</t,>
|
|
Toshihiro Shimizu |
890ddd |
currOldInv[i].x - 0.5, (currOldInv + invsCount)[i].x - 0.5,
|
|
Toshihiro Shimizu |
890ddd |
currOldInv[i].y - 0.5, currNewInv[i].y - 0.5,
|
|
Shinya Kitaoka |
79e39b |
rasIn, temp.get());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
pixDown = overPix(pixDown, pixUp);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
*pix = pixDown;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
} // namespace
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//==============================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void distort(TRasterP &outRas, const TRasterP &inRas, const TDistorter &distorter,
|
|
Toshihiro Shimizu |
890ddd |
const TPoint &dstPos, const TRop::ResampleFilterType &filter)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TRaster32P inRas32 = inRas;
|
|
Toshihiro Shimizu |
890ddd |
TRaster64P inRas64 = inRas;
|
|
Toshihiro Shimizu |
890ddd |
TRasterCM32P inRasCM32 = inRas;
|
|
Toshihiro Shimizu |
890ddd |
TRaster32P outRas32 = outRas;
|
|
Toshihiro Shimizu |
890ddd |
TRaster64P outRas64 = outRas;
|
|
Toshihiro Shimizu |
890ddd |
TRasterCM32P outRasCM32 = outRas;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (filter == TRop::Bilinear) {
|
|
Toshihiro Shimizu |
890ddd |
if (inRas32)
|
|
Toshihiro Shimizu |
890ddd |
::resample<tpixel32, uchar="">(inRas32, outRas32, distorter, dstPos);</tpixel32,>
|
|
Toshihiro Shimizu |
890ddd |
else if (inRas64)
|
|
Toshihiro Shimizu |
890ddd |
::resample<tpixel64, ushort="">(inRas64, outRas64, distorter, dstPos);</tpixel64,>
|
|
Toshihiro Shimizu |
890ddd |
else if (inRasCM32 && outRasCM32)
|
|
Toshihiro Shimizu |
890ddd |
::resample(inRasCM32, outRasCM32, distorter, dstPos);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
assert(0);
|
|
Toshihiro Shimizu |
890ddd |
} else if (filter == TRop::ClosestPixel) {
|
|
Toshihiro Shimizu |
890ddd |
if (inRas32)
|
|
Toshihiro Shimizu |
890ddd |
::resampleClosestPixel<tpixel32, uchar="">(inRas32, outRas32, distorter, dstPos);</tpixel32,>
|
|
Toshihiro Shimizu |
890ddd |
else if (inRas64)
|
|
Toshihiro Shimizu |
890ddd |
::resampleClosestPixel<tpixel64, ushort="">(inRas64, outRas64, distorter, dstPos);</tpixel64,>
|
|
Toshihiro Shimizu |
890ddd |
else if (inRasCM32 && outRasCM32)
|
|
Toshihiro Shimizu |
890ddd |
::resampleClosestPixel(inRasCM32, outRasCM32, distorter, dstPos);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
assert(0);
|
|
Toshihiro Shimizu |
890ddd |
} else
|
|
Toshihiro Shimizu |
890ddd |
assert(0);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//================================================================================================
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
// BilinearDistorterBase
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
//================================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
BilinearDistorterBase::BilinearDistorterBase(const TPointD &p00s, const TPointD &p10s,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p01s, const TPointD &p11s,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p00d, const TPointD &p10d,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p01d, const TPointD &p11d)
|
|
Toshihiro Shimizu |
890ddd |
: TQuadDistorter(p00s, p10s, p01s, p11s, p00d, p10d, p01d, p11d)
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_A = p00d;
|
|
Toshihiro Shimizu |
890ddd |
m_B = p10d - p00d;
|
|
Toshihiro Shimizu |
890ddd |
m_C = p01d - p00d;
|
|
Toshihiro Shimizu |
890ddd |
m_D = p11d - p01d - p10d + p00d;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_a = m_D.x * m_C.y - m_C.x * m_D.y;
|
|
Toshihiro Shimizu |
890ddd |
m_b0 = m_B.x * m_C.y - m_C.x * m_B.y;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
inline TPointD BilinearDistorterBase::map(const TPointD &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
double t = (p.x - m_p00s.x) / (m_p10s.x - m_p00s.x);
|
|
Toshihiro Shimizu |
890ddd |
double s = (p.y - m_p00s.y) / (m_p01s.y - m_p00s.y);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return (1 - s) * (1 - t) * m_p00d +
|
|
Toshihiro Shimizu |
890ddd |
(1 - s) * t * m_p10d +
|
|
Toshihiro Shimizu |
890ddd |
s * (1 - t) * m_p01d +
|
|
Toshihiro Shimizu |
890ddd |
s * t * m_p11d;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int BilinearDistorterBase::invMap(const TPointD &p, TPointD *results) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//See the book "Digital Image Warping" by G. Wolberg
|
|
Toshihiro Shimizu |
890ddd |
double b = m_D.x * (m_A.y - p.y) + m_D.y * (p.x - m_A.x) + m_b0;
|
|
Toshihiro Shimizu |
890ddd |
double c = m_B.x * (m_A.y - p.y) + m_B.y * (p.x - m_A.x);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (fabs(m_a) > 0.001) {
|
|
Toshihiro Shimizu |
890ddd |
//If delta <0, the points cannot be inverse-mapped
|
|
Toshihiro Shimizu |
890ddd |
double delta = sq(b) - 4.0 * m_a * c;
|
|
Toshihiro Shimizu |
890ddd |
if (delta < 0)
|
|
Toshihiro Shimizu |
890ddd |
return 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double sqrtDelta = sqrt(delta);
|
|
Toshihiro Shimizu |
890ddd |
double k = 0.5 / m_a;
|
|
Toshihiro Shimizu |
890ddd |
double v1 = k * (-b + sqrtDelta);
|
|
Toshihiro Shimizu |
890ddd |
double v2 = k * (-b - sqrtDelta);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double u1, u2;
|
|
Toshihiro Shimizu |
890ddd |
double den = m_B.x + m_D.x * v1;
|
|
Toshihiro Shimizu |
890ddd |
if (fabs(den) > 0.01)
|
|
Toshihiro Shimizu |
890ddd |
u1 = (p.x - m_A.x - m_C.x * v1) / den;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
u1 = (p.y - m_A.y - m_C.y * v1) / (m_B.y + m_D.y * v1);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
den = m_B.x + m_D.x * v2;
|
|
Toshihiro Shimizu |
890ddd |
if (fabs(den) > 0.01)
|
|
Toshihiro Shimizu |
890ddd |
u2 = (p.x - m_A.x - m_C.x * v2) / den;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
u2 = (p.y - m_A.y - m_C.y * v2) / (m_B.y + m_D.y * v2);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPointD difP10_P00 = m_p10s - m_p00s;
|
|
Toshihiro Shimizu |
890ddd |
TPointD difP01_P00 = m_p01s - m_p00s;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
results[0] = m_p00s + u1 * difP10_P00 + v1 * difP01_P00;
|
|
Toshihiro Shimizu |
890ddd |
results[1] = m_p00s + u2 * difP10_P00 + v2 * difP01_P00;
|
|
Toshihiro Shimizu |
890ddd |
return 2;
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
//The equation reduces to first order.
|
|
Toshihiro Shimizu |
890ddd |
double v = -c / b;
|
|
Toshihiro Shimizu |
890ddd |
double u = (p.x - m_A.x - m_C.x * v) / (m_B.x + m_D.x * v);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
results[0] = m_p00s + u * (m_p10s - m_p00s) + v * (m_p01s - m_p00s);
|
|
Toshihiro Shimizu |
890ddd |
return 1;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//================================================================================================
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
// BilinearDistorter
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
//================================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
BilinearDistorter::BilinearDistorter(const TPointD &p00s, const TPointD &p10s,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p01s, const TPointD &p11s,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p00d, const TPointD &p10d,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p01d, const TPointD &p11d)
|
|
Toshihiro Shimizu |
890ddd |
: TQuadDistorter(p00s, p10s, p01s, p11s, p00d, p10d, p01d, p11d)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_p00 = p00s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_p10 = p10s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_p01 = p01s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_p11 = p11s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_p00 = p00d;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_p10 = p10d;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_p01 = p01d;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_p11 = p11d;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_A = p00s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_B = p10s - p00s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_C = p01s - p00s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_D = p11s - p01s - p10s + p00s;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_A = p00d;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_B = p10d - p00d;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_C = p01d - p00d;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_D = p11d - p01d - p10d + p00d;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_a = m_refToSource.m_D.x * m_refToSource.m_C.y - m_refToSource.m_C.x * m_refToSource.m_D.y;
|
|
Toshihiro Shimizu |
890ddd |
m_refToSource.m_b0 = m_refToSource.m_B.x * m_refToSource.m_C.y - m_refToSource.m_C.x * m_refToSource.m_B.y;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_a = m_refToDest.m_D.x * m_refToDest.m_C.y - m_refToDest.m_C.x * m_refToDest.m_D.y;
|
|
Toshihiro Shimizu |
890ddd |
m_refToDest.m_b0 = m_refToDest.m_B.x * m_refToDest.m_C.y - m_refToDest.m_C.x * m_refToDest.m_B.y;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
BilinearDistorter::~BilinearDistorter()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPointD BilinearDistorter::map(const TPointD &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TPointD sourceToRefPoints[2];
|
|
Toshihiro Shimizu |
890ddd |
int returnCount = m_refToSource.invMap(p, sourceToRefPoints);
|
|
Toshihiro Shimizu |
890ddd |
if (returnCount > 0)
|
|
Toshihiro Shimizu |
890ddd |
return m_refToDest.map(sourceToRefPoints[0]);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return TConsts::napd;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
inline int BilinearDistorter::invMap(const TPointD &p, TPointD *results) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int returnCount = m_refToDest.invMap(p, results);
|
|
Toshihiro Shimizu |
890ddd |
for (int i = 0; i < returnCount; ++i)
|
|
Toshihiro Shimizu |
890ddd |
results[i] = m_refToSource.map(results[i]);
|
|
Toshihiro Shimizu |
890ddd |
return returnCount;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
inline TPointD BilinearDistorter::Base::map(const TPointD &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return (1 - p.y) * (1 - p.x) * m_p00 +
|
|
Toshihiro Shimizu |
890ddd |
(1 - p.y) * p.x * m_p10 +
|
|
Toshihiro Shimizu |
890ddd |
p.y * (1 - p.x) * m_p01 +
|
|
Toshihiro Shimizu |
890ddd |
p.y * p.x * m_p11;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int BilinearDistorter::Base::invMap(const TPointD &p, TPointD *results) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//See the book "Digital Image Warping" by G. Wolberg
|
|
Toshihiro Shimizu |
890ddd |
double b = m_D.x * (m_A.y - p.y) + m_D.y * (p.x - m_A.x) + m_b0;
|
|
Toshihiro Shimizu |
890ddd |
double c = m_B.x * (m_A.y - p.y) + m_B.y * (p.x - m_A.x);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (fabs(m_a) > 0.001) {
|
|
Toshihiro Shimizu |
890ddd |
//If delta <0, the points cannot be inverse-mapped
|
|
Toshihiro Shimizu |
890ddd |
double delta = sq(b) - 4.0 * m_a * c;
|
|
Toshihiro Shimizu |
890ddd |
if (delta < 0)
|
|
Toshihiro Shimizu |
890ddd |
return 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double sqrtDelta = sqrt(delta);
|
|
Toshihiro Shimizu |
890ddd |
double k = 0.5 / m_a;
|
|
Toshihiro Shimizu |
890ddd |
double v1 = k * (-b + sqrtDelta);
|
|
Toshihiro Shimizu |
890ddd |
double v2 = k * (-b - sqrtDelta);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double u1, u2;
|
|
Toshihiro Shimizu |
890ddd |
double den = m_B.x + m_D.x * v1;
|
|
Toshihiro Shimizu |
890ddd |
if (fabs(den) > 0.01)
|
|
Toshihiro Shimizu |
890ddd |
u1 = (p.x - m_A.x - m_C.x * v1) / den;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
u1 = (p.y - m_A.y - m_C.y * v1) / (m_B.y + m_D.y * v1);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
den = m_B.x + m_D.x * v2;
|
|
Toshihiro Shimizu |
890ddd |
if (fabs(den) > 0.01)
|
|
Toshihiro Shimizu |
890ddd |
u2 = (p.x - m_A.x - m_C.x * v2) / den;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
u2 = (p.y - m_A.y - m_C.y * v2) / (m_B.y + m_D.y * v2);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
results[0] = TPointD(u1, v1);
|
|
Toshihiro Shimizu |
890ddd |
results[1] = TPointD(u2, v2);
|
|
Toshihiro Shimizu |
890ddd |
return 2;
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
//The equation reduces to first order.
|
|
Toshihiro Shimizu |
890ddd |
double v = -c / b;
|
|
Toshihiro Shimizu |
890ddd |
double u = (p.x - m_A.x - m_C.x * v) / (m_B.x + m_D.x * v);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
results[0] = TPointD(u, v);
|
|
Toshihiro Shimizu |
890ddd |
return 1;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=================================================================================
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
// TPerspect
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
//=================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect::TPerspect()
|
|
Toshihiro Shimizu |
890ddd |
: a11(1.0), a12(0.0), a13(0.0), a21(0.0), a22(1.0), a23(0.0), a31(0.0), a32(0.0), a33(1.0)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect::TPerspect(double p11, double p12, double p13,
|
|
Toshihiro Shimizu |
890ddd |
double p21, double p22, double p23,
|
|
Toshihiro Shimizu |
890ddd |
double p31, double p32, double p33)
|
|
Toshihiro Shimizu |
890ddd |
: a11(p11), a12(p12), a13(p13), a21(p21), a22(p22), a23(p23), a31(p31), a32(p32), a33(p33)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect::TPerspect(const TPerspect &p)
|
|
Toshihiro Shimizu |
890ddd |
: a11(p.a11), a12(p.a12), a13(p.a13), a21(p.a21), a22(p.a22), a23(p.a23), a31(p.a31), a32(p.a32), a33(p.a33)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect::~TPerspect()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect &PerspectiveDistorter::TPerspect::operator=(const PerspectiveDistorter::TPerspect &p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
a11 = p.a11;
|
|
Toshihiro Shimizu |
890ddd |
a12 = p.a12;
|
|
Toshihiro Shimizu |
890ddd |
a13 = p.a13;
|
|
Toshihiro Shimizu |
890ddd |
a21 = p.a21;
|
|
Toshihiro Shimizu |
890ddd |
a22 = p.a22;
|
|
Toshihiro Shimizu |
890ddd |
a23 = p.a23;
|
|
Toshihiro Shimizu |
890ddd |
a31 = p.a31;
|
|
Toshihiro Shimizu |
890ddd |
a32 = p.a32;
|
|
Toshihiro Shimizu |
890ddd |
a33 = p.a33;
|
|
Toshihiro Shimizu |
890ddd |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect PerspectiveDistorter::TPerspect::operator*(const PerspectiveDistorter::TPerspect &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return PerspectiveDistorter::TPerspect(
|
|
Toshihiro Shimizu |
890ddd |
a11 * p.a11 + a12 * p.a21 + a13 * p.a31,
|
|
Toshihiro Shimizu |
890ddd |
a11 * p.a12 + a12 * p.a22 + a13 * p.a32,
|
|
Toshihiro Shimizu |
890ddd |
a11 * p.a13 + a12 * p.a23 + a13 * p.a33,
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
a21 * p.a11 + a22 * p.a21 + a23 * p.a31,
|
|
Toshihiro Shimizu |
890ddd |
a21 * p.a12 + a22 * p.a22 + a23 * p.a32,
|
|
Toshihiro Shimizu |
890ddd |
a21 * p.a13 + a22 * p.a23 + a23 * p.a33,
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
a31 * p.a11 + a32 * p.a21 + a33 * p.a31,
|
|
Toshihiro Shimizu |
890ddd |
a31 * p.a12 + a32 * p.a22 + a33 * p.a32,
|
|
Toshihiro Shimizu |
890ddd |
a31 * p.a13 + a32 * p.a23 + a33 * p.a33);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect PerspectiveDistorter::TPerspect::operator*(const TAffine &aff) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return operator*(TPerspect(
|
|
Toshihiro Shimizu |
890ddd |
aff.a11, aff.a12, aff.a13,
|
|
Toshihiro Shimizu |
890ddd |
aff.a21, aff.a22, aff.a23,
|
|
Toshihiro Shimizu |
890ddd |
0.0, 0.0, 1.0));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect operator*(const TAffine &aff, const PerspectiveDistorter::TPerspect &p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return PerspectiveDistorter::TPerspect(
|
|
Toshihiro Shimizu |
890ddd |
aff.a11, aff.a12, aff.a13,
|
|
Toshihiro Shimizu |
890ddd |
aff.a21, aff.a22, aff.a23,
|
|
Toshihiro Shimizu |
890ddd |
0.0, 0.0, 1.0) *
|
|
Toshihiro Shimizu |
890ddd |
p;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect PerspectiveDistorter::TPerspect::operator*=(const PerspectiveDistorter::TPerspect &p)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return *this * p;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect PerspectiveDistorter::TPerspect::inv() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return TPerspect(
|
|
Toshihiro Shimizu |
890ddd |
a22 * a33 - a23 * a32, a13 * a32 - a12 * a33, a12 * a23 - a13 * a22,
|
|
Toshihiro Shimizu |
890ddd |
a23 * a31 - a21 * a33, a11 * a33 - a13 * a31, a13 * a21 - a11 * a23,
|
|
Toshihiro Shimizu |
890ddd |
a21 * a32 - a22 * a31, a12 * a31 - a11 * a32, a11 * a22 - a12 * a21);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double PerspectiveDistorter::TPerspect::det() const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return a11 * a22 * a33 +
|
|
Toshihiro Shimizu |
890ddd |
a12 * a23 * a31 +
|
|
Toshihiro Shimizu |
890ddd |
a13 * a21 * a32 -
|
|
Toshihiro Shimizu |
890ddd |
a13 * a22 * a31 -
|
|
Toshihiro Shimizu |
890ddd |
a11 * a23 * a32 -
|
|
Toshihiro Shimizu |
890ddd |
a12 * a21 * a33;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool PerspectiveDistorter::TPerspect::operator==(const PerspectiveDistorter::TPerspect &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return a11 == p.a11 && a12 == p.a12 && a13 == p.a13 &&
|
|
Toshihiro Shimizu |
890ddd |
a21 == p.a21 && a22 == p.a22 && a23 == p.a23 &&
|
|
Toshihiro Shimizu |
890ddd |
a31 == p.a31 && a32 == p.a32 && a33 == p.a33;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool PerspectiveDistorter::TPerspect::operator!=(const PerspectiveDistorter::TPerspect &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return !(*this == p);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool PerspectiveDistorter::TPerspect::isIdentity(double err) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return ((a11 - 1.0) * (a11 - 1.0) + (a22 - 1.0) * (a22 - 1.0) + (a33 - 1.0) * (a33 - 1.0) +
|
|
Toshihiro Shimizu |
890ddd |
a12 * a12 + a13 * a13 + a21 * a21 + a23 * a23 + a31 * a31 + a32 * a32) < err;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPointD PerspectiveDistorter::TPerspect::operator*(const TPointD &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
double den = (a31 * p.x + a32 * p.y + a33);
|
|
Toshihiro Shimizu |
890ddd |
double x = (a11 * p.x + a12 * p.y + a13) / den;
|
|
Toshihiro Shimizu |
890ddd |
double y = (a21 * p.x + a22 * p.y + a23) / den;
|
|
Toshihiro Shimizu |
890ddd |
return TPointD(x, y);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
T3DPointD PerspectiveDistorter::TPerspect::operator*(const T3DPointD &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return T3DPointD(
|
|
Toshihiro Shimizu |
890ddd |
a11 * p.x + a12 * p.y + a13 * p.z,
|
|
Toshihiro Shimizu |
890ddd |
a21 * p.x + a22 * p.y + a23 * p.z,
|
|
Toshihiro Shimizu |
890ddd |
a31 * p.x + a32 * p.y + a33 * p.z);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TRectD PerspectiveDistorter::TPerspect::operator*(const TRectD &rect) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (rect != TConsts::infiniteRectD) {
|
|
Toshihiro Shimizu |
890ddd |
TPointD p1 = *this * rect.getP00(),
|
|
Toshihiro Shimizu |
890ddd |
p2 = *this * rect.getP01(),
|
|
Toshihiro Shimizu |
890ddd |
p3 = *this * rect.getP10(),
|
|
Toshihiro Shimizu |
890ddd |
p4 = *this * rect.getP11();
|
|
Shinya Kitaoka |
12c444 |
return TRectD(
|
|
Shinya Kitaoka |
12c444 |
std::min({p1.x, p2.x, p3.x, p4.x}), std::min({p1.y, p2.y, p3.y, p4.y}),
|
|
Shinya Kitaoka |
12c444 |
std::max({p1.x, p2.x, p3.x, p4.x}), std::max({p1.y, p2.y, p3.y, p4.y}));
|
|
Toshihiro Shimizu |
890ddd |
} else
|
|
Toshihiro Shimizu |
890ddd |
return TConsts::infiniteRectD;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//================================================================================================
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
// PerspectiveDistorter
|
|
Toshihiro Shimizu |
890ddd |
//
|
|
Toshihiro Shimizu |
890ddd |
//================================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::PerspectiveDistorter(const TPointD &p00s, const TPointD &p10s,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p01s, const TPointD &p11s,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p00d, const TPointD &p10d,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p01d, const TPointD &p11d)
|
|
Toshihiro Shimizu |
890ddd |
: TQuadDistorter(p00s, p10s, p01s, p11s, p00d, p10d, p01d, p11d)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
computeMatrix();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::~PerspectiveDistorter()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void PerspectiveDistorter::computeMatrix()
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//Since source and destination points are intended in hundreds of pixels in their refs,
|
|
Toshihiro Shimizu |
890ddd |
//and inverting makes squares with respect to their elements' size, we'd better put the
|
|
Toshihiro Shimizu |
890ddd |
//quadrilaterals in more numerically stable references before inversions.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
12c444 |
double srcSize = std::max({dist(m_p00s, m_p10s), dist(m_p00s, m_p01s), dist(m_p10s, m_p11s), dist(m_p01s, m_p11s)});
|
|
Shinya Kitaoka |
12c444 |
double dstSize = std::max({dist(m_p00d, m_p10d), dist(m_p00d, m_p01d), dist(m_p10d, m_p11d), dist(m_p01d, m_p11d)});
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TAffine toSrcNormalizedRef(TScale(1.0 / srcSize) * TTranslation(-m_p00s));
|
|
Toshihiro Shimizu |
890ddd |
TAffine toSrcRef(TTranslation(m_p00s) * TScale(srcSize));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TAffine toDstNormalizedRef(TScale(1.0 / dstSize) * TTranslation(-m_p00d));
|
|
Toshihiro Shimizu |
890ddd |
TAffine toDstRef(TTranslation(m_p00d) * TScale(dstSize));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPointD p00s;
|
|
Toshihiro Shimizu |
890ddd |
TPointD p10s(toSrcNormalizedRef * m_p10s);
|
|
Toshihiro Shimizu |
890ddd |
TPointD p01s(toSrcNormalizedRef * m_p01s);
|
|
Toshihiro Shimizu |
890ddd |
TPointD p11s(toSrcNormalizedRef * m_p11s);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPointD p00d;
|
|
Toshihiro Shimizu |
890ddd |
TPointD p10d(toDstNormalizedRef * m_p10d);
|
|
Toshihiro Shimizu |
890ddd |
TPointD p01d(toDstNormalizedRef * m_p01d);
|
|
Toshihiro Shimizu |
890ddd |
TPointD p11d(toDstNormalizedRef * m_p11d);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//compute a matrix from (0,0), (1,0), (1,1), (0,1) to m_startPoints.
|
|
Toshihiro Shimizu |
890ddd |
TPerspect m1 = computeSquareToMatrix(p00s, p10s, p01s, p11s);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//compute a matrix from (0,0), (1,0), (1,1), (0,1) to m_endPoints.
|
|
Toshihiro Shimizu |
890ddd |
TPerspect m2 = computeSquareToMatrix(p00d, p10d, p01d, p11d);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_matrix = m2 * m1.inv();
|
|
Toshihiro Shimizu |
890ddd |
m_matrixInv = toSrcRef * m_matrix.inv() * toDstNormalizedRef;
|
|
Toshihiro Shimizu |
890ddd |
m_matrix = toDstRef * m_matrix * toSrcNormalizedRef;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double PerspectiveDistorter::determinant(double a11, double a12, double a21, double a22)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return a11 * a22 - a12 * a21;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PerspectiveDistorter::TPerspect PerspectiveDistorter::computeSquareToMatrix(
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &p00, const TPointD &p10, const TPointD &p01, const TPointD &p11)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TPointD d1 = p10 - p11;
|
|
Toshihiro Shimizu |
890ddd |
TPointD d2 = p01 - p11;
|
|
Toshihiro Shimizu |
890ddd |
TPointD d3 = p00 - p10 + p11 - p01;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPerspect matrix;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
matrix.a31 = determinant(d3.x, d2.x, d3.y, d2.y) / determinant(d1.x, d2.x, d1.y, d2.y);
|
|
Toshihiro Shimizu |
890ddd |
matrix.a32 = determinant(d1.x, d3.x, d1.y, d3.y) / determinant(d1.x, d2.x, d1.y, d2.y);
|
|
Toshihiro Shimizu |
890ddd |
matrix.a11 = p10.x - p00.x + matrix.a31 * p10.x;
|
|
Toshihiro Shimizu |
890ddd |
matrix.a12 = p01.x - p00.x + matrix.a32 * p01.x;
|
|
Toshihiro Shimizu |
890ddd |
matrix.a13 = p00.x;
|
|
Toshihiro Shimizu |
890ddd |
matrix.a21 = p10.y - p00.y + matrix.a31 * p10.y;
|
|
Toshihiro Shimizu |
890ddd |
matrix.a22 = p01.y - p00.y + matrix.a32 * p01.y;
|
|
Toshihiro Shimizu |
890ddd |
matrix.a23 = p00.y;
|
|
Toshihiro Shimizu |
890ddd |
matrix.a33 = 1;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return matrix;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPointD PerspectiveDistorter::map(const TPointD &p) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return m_matrix * p;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int PerspectiveDistorter::invMap(const TPointD &p, TPointD *results) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
results[0] = m_matrixInv * p;
|
|
Toshihiro Shimizu |
890ddd |
return 1;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**********************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Rect inversion methods
|
|
Toshihiro Shimizu |
890ddd |
//**********************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=============================
|
|
Toshihiro Shimizu |
890ddd |
// PerspectiveDistorter
|
|
Toshihiro Shimizu |
890ddd |
//=============================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//IDEA: Lines are mapped into lines through this distortion and through the inverse; plus,
|
|
Toshihiro Shimizu |
890ddd |
//the distortion is 1-1. There is one major issue: across the horizon line of the perspective
|
|
Toshihiro Shimizu |
890ddd |
//projection (ie the pre-image of {z=0}, which could eventually degenerate to the whole plane)
|
|
Toshihiro Shimizu |
890ddd |
//the jacobian sign is inverted. Observe, further, that the mapping is infinitely differentiable
|
|
Toshihiro Shimizu |
890ddd |
//apart from neighbourhoods of the horizon line.
|
|
Toshihiro Shimizu |
890ddd |
//It can be shown that bounding estimates based on corner samples on one side of the horizon line
|
|
Toshihiro Shimizu |
890ddd |
//may prove false on the other. Therefore, we shall separate such estimates in the two cases,
|
|
Toshihiro Shimizu |
890ddd |
//and then sum them together.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//! Returns the jacobian (on source ref) associated to the pre-image of passed destination point.
|
|
Toshihiro Shimizu |
890ddd |
void PerspectiveDistorter::getJacobian(
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &destPoint,
|
|
Toshihiro Shimizu |
890ddd |
TPointD &srcPoint, TPointD &xDer, TPointD &yDer) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
srcPoint = m_matrixInv * destPoint;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
T3DPointD im(m_matrix * T3DPointD(srcPoint.x, srcPoint.y, 1.0));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double imZInv = 1.0 / im.z, minusImZInvSq = -sq(imZInv);
|
|
Toshihiro Shimizu |
890ddd |
TPerspect projectionJac(
|
|
Toshihiro Shimizu |
890ddd |
imZInv, 0.0, minusImZInvSq * im.x,
|
|
Toshihiro Shimizu |
890ddd |
0.0, imZInv, minusImZInvSq * im.y,
|
|
Toshihiro Shimizu |
890ddd |
0.0, 0.0, 0.0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPerspect result(projectionJac * m_matrix);
|
|
Toshihiro Shimizu |
890ddd |
xDer.x = result.a11;
|
|
Toshihiro Shimizu |
890ddd |
xDer.y = result.a21;
|
|
Toshihiro Shimizu |
890ddd |
yDer.x = result.a12;
|
|
Toshihiro Shimizu |
890ddd |
yDer.y = result.a22;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
inline void updateResult(
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &srcCorner,
|
|
Toshihiro Shimizu |
890ddd |
const TPointD &xDer, const TPointD &yDer,
|
|
Toshihiro Shimizu |
890ddd |
int rectSideX, int rectSideY,
|
|
Toshihiro Shimizu |
890ddd |
bool &hasPositiveResults, bool &hasNegativeResults,
|
|
Toshihiro Shimizu |
890ddd |
TRectD &posResult, TRectD &negResult)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
const int securityAddendum = 5; //Adding a 5 border to the result just to be sure about approx errors...
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int jacobianSign = tsign(cross(xDer, yDer));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int sideDerXAgainstRectSideX = rectSideX * tsign(-xDer.y);
|
|
Toshihiro Shimizu |
890ddd |
int sideDerXAgainstRectSideY = rectSideY * tsign(xDer.x);
|
|
Toshihiro Shimizu |
890ddd |
int sideDerYAgainstRectSideX = rectSideX * tsign(-yDer.y);
|
|
Toshihiro Shimizu |
890ddd |
int sideDerYAgainstRectSideY = rectSideY * tsign(yDer.x);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (jacobianSign > 0) {
|
|
Toshihiro Shimizu |
890ddd |
hasPositiveResults = true;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerXAgainstRectSideX != -sideDerXAgainstRectSideY)
|
|
Toshihiro Shimizu |
890ddd |
//Rect lies on one side of the derivative line extension. Therefore, the
|
|
Toshihiro Shimizu |
890ddd |
//inverted rect can be updated.
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerXAgainstRectSideX > 0 || sideDerXAgainstRectSideY > 0)
|
|
Shinya Kitaoka |
12c444 |
posResult.y0 = std::min(posResult.y0, srcCorner.y - securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Shinya Kitaoka |
12c444 |
posResult.y1 = std::max(posResult.y1, srcCorner.y + securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerYAgainstRectSideX != -sideDerYAgainstRectSideY)
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerYAgainstRectSideX > 0 || sideDerYAgainstRectSideY > 0)
|
|
Shinya Kitaoka |
12c444 |
posResult.x1 = std::max(posResult.x1, srcCorner.x + securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Shinya Kitaoka |
12c444 |
posResult.x0 = std::min(posResult.x0, srcCorner.x - securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
} else if (jacobianSign < 0) {
|
|
Toshihiro Shimizu |
890ddd |
hasNegativeResults = true;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerXAgainstRectSideX != -sideDerXAgainstRectSideY)
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerXAgainstRectSideX > 0 || sideDerXAgainstRectSideY > 0)
|
|
Shinya Kitaoka |
12c444 |
negResult.y1 = std::max(posResult.y1, srcCorner.y + securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Shinya Kitaoka |
12c444 |
negResult.y0 = std::min(posResult.y0, srcCorner.y - securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerYAgainstRectSideX != -sideDerYAgainstRectSideY)
|
|
Toshihiro Shimizu |
890ddd |
if (sideDerYAgainstRectSideX > 0 || sideDerYAgainstRectSideY > 0)
|
|
Shinya Kitaoka |
12c444 |
negResult.x0 = std::min(posResult.x0, srcCorner.x - securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Shinya Kitaoka |
12c444 |
negResult.x1 = std::max(posResult.x1, srcCorner.x + securityAddendum);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TRectD PerspectiveDistorter::invMap(const TRectD &rect) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//For each corner, find the jacobian. Then, decide by which side the rect's
|
|
Toshihiro Shimizu |
890ddd |
//pre-image lie with respect to the partial derivatives.
|
|
Toshihiro Shimizu |
890ddd |
//Observe that the two horizon-separated semiplanes do not compete -
|
|
Toshihiro Shimizu |
890ddd |
//the requirement for each is added to the result.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TPointD srcCorner, xDer, yDer;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double maxD = (std::numeric_limits<double>::max)();</double>
|
|
Toshihiro Shimizu |
890ddd |
TRectD positiveResult(maxD, maxD, -maxD, -maxD);
|
|
Toshihiro Shimizu |
890ddd |
TRectD negativeResult(positiveResult);
|
|
Toshihiro Shimizu |
890ddd |
bool hasPositiveResults = false, hasNegativeResults = false;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
getJacobian(rect.getP00(), srcCorner, xDer, yDer);
|
|
Toshihiro Shimizu |
890ddd |
updateResult(srcCorner, xDer, yDer, 1, 1,
|
|
Toshihiro Shimizu |
890ddd |
hasPositiveResults, hasNegativeResults, positiveResult, negativeResult);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
getJacobian(rect.getP10(), srcCorner, xDer, yDer);
|
|
Toshihiro Shimizu |
890ddd |
updateResult(srcCorner, xDer, yDer, -1, 1,
|
|
Toshihiro Shimizu |
890ddd |
hasPositiveResults, hasNegativeResults, positiveResult, negativeResult);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
getJacobian(rect.getP01(), srcCorner, xDer, yDer);
|
|
Toshihiro Shimizu |
890ddd |
updateResult(srcCorner, xDer, yDer, 1, -1,
|
|
Toshihiro Shimizu |
890ddd |
hasPositiveResults, hasNegativeResults, positiveResult, negativeResult);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
getJacobian(rect.getP11(), srcCorner, xDer, yDer);
|
|
Toshihiro Shimizu |
890ddd |
updateResult(srcCorner, xDer, yDer, -1, -1,
|
|
Toshihiro Shimizu |
890ddd |
hasPositiveResults, hasNegativeResults, positiveResult, negativeResult);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//If some maxD remain, no bound on that side was found. So replace with
|
|
Toshihiro Shimizu |
890ddd |
//the opposite (unlimited on that side) maxD.
|
|
Toshihiro Shimizu |
890ddd |
if (positiveResult.x0 == maxD)
|
|
Toshihiro Shimizu |
890ddd |
positiveResult.x0 = -maxD;
|
|
Toshihiro Shimizu |
890ddd |
if (positiveResult.x1 == -maxD)
|
|
Toshihiro Shimizu |
890ddd |
positiveResult.x1 = maxD;
|
|
Toshihiro Shimizu |
890ddd |
if (positiveResult.y0 == maxD)
|
|
Toshihiro Shimizu |
890ddd |
positiveResult.y0 = -maxD;
|
|
Toshihiro Shimizu |
890ddd |
if (positiveResult.y1 == -maxD)
|
|
Toshihiro Shimizu |
890ddd |
positiveResult.y1 = maxD;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (negativeResult.x0 == maxD)
|
|
Toshihiro Shimizu |
890ddd |
negativeResult.x0 = -maxD;
|
|
Toshihiro Shimizu |
890ddd |
if (negativeResult.x1 == -maxD)
|
|
Toshihiro Shimizu |
890ddd |
negativeResult.x1 = maxD;
|
|
Toshihiro Shimizu |
890ddd |
if (negativeResult.y0 == maxD)
|
|
Toshihiro Shimizu |
890ddd |
negativeResult.y0 = -maxD;
|
|
Toshihiro Shimizu |
890ddd |
if (negativeResult.y1 == -maxD)
|
|
Toshihiro Shimizu |
890ddd |
negativeResult.y1 = maxD;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return hasPositiveResults ? hasNegativeResults ? positiveResult + negativeResult : positiveResult : hasNegativeResults ? negativeResult : TConsts::infiniteRectD;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//=============================
|
|
Toshihiro Shimizu |
890ddd |
// BilinearDistorter
|
|
Toshihiro Shimizu |
890ddd |
//=============================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//IDEA: This time, lines are mapped to curves, in general. Plus, from 0 to 2 possible inverses
|
|
Toshihiro Shimizu |
890ddd |
//may exist for the same destination point.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Given the separation of the bilinear mapping in two bilinear mappings with an intermediate reference for the
|
|
Toshihiro Shimizu |
890ddd |
//convex coordinates, it can be shown that a bounding estimate for the inverse of a rect can be found this way:
|
|
Toshihiro Shimizu |
890ddd |
// 1. Inverse-map the corners of the rect in the convex-reference (ie find their convex coordinates)
|
|
Toshihiro Shimizu |
890ddd |
// 2. Make their bounding box
|
|
Toshihiro Shimizu |
890ddd |
// 3. Map its corners to the source reference
|
|
Toshihiro Shimizu |
890ddd |
// 4. Return their bounding box
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//In order to show that this actually works, consider the following result:
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//A rect maps to a convex quadrilateral through a forward bilinear mapping (ie passages 3->4 and 2->1).
|
|
Toshihiro Shimizu |
890ddd |
//It is sufficient to consider that lines map to lines through one such mapping, and that since it is
|
|
Toshihiro Shimizu |
890ddd |
//also everywhere differentiable, its local behaviour around corners is that of a linear function, through
|
|
Toshihiro Shimizu |
890ddd |
//which convex angles are only mappable to other convex angles.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TRectD BilinearDistorter::invMap(const TRectD &rect) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//Build the convex coordinates of the rect's corners.
|
|
Toshihiro Shimizu |
890ddd |
TPointD invs[8];
|
|
Toshihiro Shimizu |
890ddd |
int count[4];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
count[0] = m_refToDest.invMap(rect.getP00(), &invs[0]);
|
|
Toshihiro Shimizu |
890ddd |
count[1] = m_refToDest.invMap(rect.getP10(), &invs[2]);
|
|
Toshihiro Shimizu |
890ddd |
count[2] = m_refToDest.invMap(rect.getP01(), &invs[4]);
|
|
Toshihiro Shimizu |
890ddd |
count[3] = m_refToDest.invMap(rect.getP11(), &invs[6]);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double maxD = (std::numeric_limits<double>::max)();</double>
|
|
Toshihiro Shimizu |
890ddd |
TRectD bbox(maxD, maxD, -maxD, -maxD);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int i, j;
|
|
Toshihiro Shimizu |
890ddd |
for (i = 0; i < 4; ++i) {
|
|
Toshihiro Shimizu |
890ddd |
for (j = 0; j < count[i]; ++j) {
|
|
Toshihiro Shimizu |
890ddd |
TPointD &inv(invs[j + 2 * i]);
|
|
Shinya Kitaoka |
12c444 |
bbox.x0 = std::min(bbox.x0, inv.x);
|
|
Shinya Kitaoka |
12c444 |
bbox.y0 = std::min(bbox.y0, inv.y);
|
|
Shinya Kitaoka |
12c444 |
bbox.x1 = std::max(bbox.x1, inv.x);
|
|
Shinya Kitaoka |
12c444 |
bbox.y1 = std::max(bbox.y1, inv.y);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (bbox.x1 <= bbox.x0 || bbox.y1 <= bbox.y0)
|
|
Toshihiro Shimizu |
890ddd |
return TConsts::infiniteRectD; //Should happen only if all counts are 0
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
invs[0] = m_refToSource.map(bbox.getP00());
|
|
Toshihiro Shimizu |
890ddd |
invs[1] = m_refToSource.map(bbox.getP10());
|
|
Toshihiro Shimizu |
890ddd |
invs[2] = m_refToSource.map(bbox.getP01());
|
|
Toshihiro Shimizu |
890ddd |
invs[3] = m_refToSource.map(bbox.getP11());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
12c444 |
bbox.x0 = std::min({invs[0].x, invs[1].x, invs[2].x, invs[3].x});
|
|
Shinya Kitaoka |
12c444 |
bbox.y0 = std::min({invs[0].y, invs[1].y, invs[2].y, invs[3].y});
|
|
Shinya Kitaoka |
12c444 |
bbox.x1 = std::max({invs[0].x, invs[1].x, invs[2].x, invs[3].x});
|
|
Shinya Kitaoka |
12c444 |
bbox.y1 = std::max({invs[0].y, invs[1].y, invs[2].y, invs[3].y});
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return bbox.enlarge(5); //Enlarge a little just to be sure
|
|
Toshihiro Shimizu |
890ddd |
}
|