Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "warp.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "trasterfx.h"
Toshihiro Shimizu 890ddd
#include "tspectrumparam.h"
Toshihiro Shimizu 890ddd
#include "gradients.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "perlinnoise.h"
Toshihiro Shimizu 890ddd
#include "tparamuiconcept.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class RandomWaveFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(RandomWaveFx)
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	TRasterFxPort m_warped;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_intensity;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_gridStep;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_evol;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_posx;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_posy;
Toshihiro Shimizu 890ddd
	TBoolParamP m_sharpen;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	RandomWaveFx()
Toshihiro Shimizu 890ddd
		: m_intensity(20), m_gridStep(2), m_evol(0.0), m_posx(0.0), m_posy(0.0), m_sharpen(false)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_posx->setMeasureName("fxLength");
Toshihiro Shimizu 890ddd
		m_posy->setMeasureName("fxLength");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		addInputPort("Source", m_warped);
Toshihiro Shimizu 890ddd
		bindParam(this, "intensity", m_intensity);
Toshihiro Shimizu 890ddd
		bindParam(this, "sensitivity", m_gridStep);
Toshihiro Shimizu 890ddd
		bindParam(this, "evolution", m_evol);
Toshihiro Shimizu 890ddd
		bindParam(this, "positionx", m_posx);
Toshihiro Shimizu 890ddd
		bindParam(this, "positiony", m_posy);
Toshihiro Shimizu 890ddd
		bindParam(this, "sharpen", m_sharpen);
Toshihiro Shimizu 890ddd
		m_intensity->setValueRange(-1000, 1000);
Toshihiro Shimizu 890ddd
		m_gridStep->setValueRange(2, 20);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual ~RandomWaveFx() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void getParamUIs(TParamUIConcept *&concepts, int &length)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		concepts = new TParamUIConcept[length = 1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		concepts[0].m_type = TParamUIConcept::POINT_2;
Toshihiro Shimizu 890ddd
		concepts[0].m_label = "Position";
Toshihiro Shimizu 890ddd
		concepts[0].m_params.push_back(m_posx);
Toshihiro Shimizu 890ddd
		concepts[0].m_params.push_back(m_posy);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool canHandle(const TRenderSettings &info, double frame)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return isAlmostIsotropic(info.m_affine);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_warped.isConnected()) {
Toshihiro Shimizu 890ddd
			int ret = m_warped->doGetBBox(frame, bBox, info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (ret && !bBox.isEmpty()) {
Toshihiro Shimizu 890ddd
				if (bBox != TConsts::infiniteRectD) {
Toshihiro Shimizu 890ddd
					WarpParams params;
Toshihiro Shimizu 890ddd
					params.m_intensity = m_intensity->getValue(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					bBox = bBox.enlarge(getWarpRadius(params));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				return true;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bBox = TRectD();
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void doDryCompute(TRectD &rect,
Toshihiro Shimizu 890ddd
					  double frame,
Toshihiro Shimizu 890ddd
					  const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bool isWarped = m_warped.isConnected();
Toshihiro Shimizu 890ddd
		if (!isWarped)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		if (fabs(m_intensity->getValue(frame)) < 0.01) {
Toshihiro Shimizu 890ddd
			m_warped->dryCompute(rect, frame, info);
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double scale = sqrt(fabs(info.m_affine.det()));
Toshihiro Shimizu 890ddd
		double gridStep = 1.5 * m_gridStep->getValue(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		WarpParams params;
Toshihiro Shimizu 890ddd
		params.m_intensity = m_intensity->getValue(frame) / gridStep;
Toshihiro Shimizu 890ddd
		params.m_warperScale = scale * gridStep;
Toshihiro Shimizu 890ddd
		params.m_sharpen = m_sharpen->getValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRectD warpedBox, warpedComputeRect, tileComputeRect;
Toshihiro Shimizu 890ddd
		m_warped->getBBox(frame, warpedBox, info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		getWarpComputeRects(tileComputeRect, warpedComputeRect, warpedBox, rect, params);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (tileComputeRect.getLx() <= 0 || tileComputeRect.getLy() <= 0)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		if (warpedComputeRect.getLx() <= 0 || warpedComputeRect.getLy() <= 0)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_warped->dryCompute(warpedComputeRect, frame, info);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void doCompute(TTile &tile, double frame, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		bool isWarped = m_warped.isConnected();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!isWarped)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (fabs(m_intensity->getValue(frame)) < 0.01) {
Toshihiro Shimizu 890ddd
			m_warped->compute(tile, frame, info);
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int shrink = (info.m_shrinkX + info.m_shrinkY) / 2;
Toshihiro Shimizu 890ddd
		double scale = sqrt(fabs(info.m_affine.det()));
Toshihiro Shimizu 890ddd
		double gridStep = 1.5 * m_gridStep->getValue(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		WarpParams params;
Toshihiro Shimizu 890ddd
		params.m_intensity = m_intensity->getValue(frame) / gridStep;
Toshihiro Shimizu 890ddd
		params.m_warperScale = scale * gridStep;
Toshihiro Shimizu 890ddd
		params.m_sharpen = m_sharpen->getValue();
Toshihiro Shimizu 890ddd
		params.m_shrink = shrink;
Toshihiro Shimizu 890ddd
		double evolution = m_evol->getValue(frame);
Toshihiro Shimizu 890ddd
		double size = 100.0 / info.m_shrinkX;
Toshihiro Shimizu 890ddd
		TPointD pos(m_posx->getValue(frame), m_posy->getValue(frame));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//The warper is calculated on a standard reference, with fixed dpi. This makes sure
Toshihiro Shimizu 890ddd
		//that the lattice created for the warp does not depend on camera transforms and resolution.
Toshihiro Shimizu 890ddd
		TRenderSettings warperInfo(info);
Toshihiro Shimizu 890ddd
		double warperScaleFactor = 1.0 / params.m_warperScale;
Toshihiro Shimizu 890ddd
		warperInfo.m_affine = TScale(warperScaleFactor) * info.m_affine;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Retrieve tile's geometry
Toshihiro Shimizu 890ddd
		TRectD tileRect;
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			TRasterP tileRas = tile.getRaster();
Toshihiro Shimizu 890ddd
			tileRect = TRectD(tile.m_pos, TDimensionD(tileRas->getLx(), tileRas->getLy()));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Build the compute rect
Toshihiro Shimizu 890ddd
		TRectD warpedBox, warpedComputeRect, tileComputeRect;
Toshihiro Shimizu 890ddd
		m_warped->getBBox(frame, warpedBox, info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		getWarpComputeRects(tileComputeRect, warpedComputeRect, warpedBox, tileRect, params);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (tileComputeRect.getLx() <= 0 || tileComputeRect.getLy() <= 0)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		if (warpedComputeRect.getLx() <= 0 || warpedComputeRect.getLy() <= 0)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRectD warperComputeRect(TScale(warperScaleFactor) * tileComputeRect);
Toshihiro Shimizu 890ddd
		double warperEnlargement = getWarperEnlargement(params);
Toshihiro Shimizu 890ddd
		warperComputeRect = warperComputeRect.enlarge(warperEnlargement);
Toshihiro Shimizu 890ddd
		warperComputeRect.x0 = tfloor(warperComputeRect.x0);
Toshihiro Shimizu 890ddd
		warperComputeRect.y0 = tfloor(warperComputeRect.y0);
Toshihiro Shimizu 890ddd
		warperComputeRect.x1 = tceil(warperComputeRect.x1);
Toshihiro Shimizu 890ddd
		warperComputeRect.y1 = tceil(warperComputeRect.y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Compute the warped tile
Toshihiro Shimizu 890ddd
		TTile tileIn;
Toshihiro Shimizu 890ddd
		m_warped->allocateAndCompute(tileIn, warpedComputeRect.getP00(),
Toshihiro Shimizu 890ddd
									 TDimension(warpedComputeRect.getLx(), warpedComputeRect.getLy()),
Toshihiro Shimizu 890ddd
									 tile.getRaster(), frame, info);
Toshihiro Shimizu 890ddd
		TRasterP rasIn = tileIn.getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Compute the warper tile
Toshihiro Shimizu 890ddd
		TSpectrum::ColorKey colors[] = {
Toshihiro Shimizu 890ddd
			TSpectrum::ColorKey(0, TPixel32::White),
Toshihiro Shimizu 890ddd
			TSpectrum::ColorKey(1, TPixel32::Black)};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TSpectrumParamP cloudscolors = TSpectrumParamP(tArrayCount(colors), colors);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Build the warper
Toshihiro Shimizu 890ddd
		warperInfo.m_affine = warperInfo.m_affine;
Toshihiro Shimizu 890ddd
		TAffine aff = warperInfo.m_affine.inv();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TTile warperTile;
Toshihiro Shimizu 890ddd
		TRasterP rasWarper = rasIn->create(warperComputeRect.getLx(), warperComputeRect.getLy());
Toshihiro Shimizu 890ddd
		warperTile.m_pos = warperComputeRect.getP00();
Toshihiro Shimizu 890ddd
		warperTile.setRaster(rasWarper);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			TRenderSettings info2(warperInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			//Now, separate the part of the affine the Fx can handle from the rest.
Toshihiro Shimizu 890ddd
			TAffine fxHandledAffine = handledAffine(warperInfo, frame);
Toshihiro Shimizu 890ddd
			info2.m_affine = fxHandledAffine;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TAffine aff = warperInfo.m_affine * fxHandledAffine.inv();
Toshihiro Shimizu 890ddd
			aff.a13 /= warperInfo.m_shrinkX;
Toshihiro Shimizu 890ddd
			aff.a23 /= warperInfo.m_shrinkY;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TRectD rectIn = aff.inv() * warperComputeRect;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			//rectIn = rectIn.enlarge(getResampleFilterRadius(info));  //Needed to counter the resample filter
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TRect rectInI(tfloor(rectIn.x0), tfloor(rectIn.y0), tceil(rectIn.x1) - 1, tceil(rectIn.y1) - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// rasIn e' un raster dello stesso tipo di tile.getRaster()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TTile auxtile(warperTile.getRaster()->create(rectInI.getLx(), rectInI.getLy()), convert(rectInI.getP00()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TPointD mypos(auxtile.m_pos - pos);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			double scale2 = sqrt(fabs(info2.m_affine.det()));
Toshihiro Shimizu 890ddd
			doClouds(auxtile.getRaster(), cloudscolors, mypos, evolution, size, 0.0, 1.0, PNOISE_CLOUDS, scale2, frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			info2.m_affine = aff;
Toshihiro Shimizu 890ddd
			TRasterFx::applyAffine(warperTile, auxtile, info2);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Warp
Toshihiro Shimizu 890ddd
		TPointD db;
Toshihiro Shimizu 890ddd
		TRect rasComputeRectI(convert(tileComputeRect - tileRect.getP00(), db));
Toshihiro Shimizu 890ddd
		TRasterP tileRas = tile.getRaster()->extract(rasComputeRectI);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPointD rasInPos(warpedComputeRect.getP00() - tileComputeRect.getP00());
Toshihiro Shimizu 890ddd
		TPointD warperPos((TScale(params.m_warperScale) * warperComputeRect.getP00()) - tileComputeRect.getP00());
Toshihiro Shimizu 890ddd
		warp(tileRas, rasIn, rasWarper, rasInPos, warperPos, params);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getMemoryRequirement(const TRectD &rect, double frame, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		//return -1;   //Deactivated. This fx is currently very inefficient if subdivided!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int shrink = (info.m_shrinkX + info.m_shrinkY) / 2;
Toshihiro Shimizu 890ddd
		double scale = sqrt(fabs(info.m_affine.det()));
Toshihiro Shimizu 890ddd
		double gridStep = 1.5 * m_gridStep->getValue(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		WarpParams params;
Toshihiro Shimizu 890ddd
		params.m_intensity = m_intensity->getValue(frame) / gridStep;
Toshihiro Shimizu 890ddd
		params.m_warperScale = scale * gridStep;
Toshihiro Shimizu 890ddd
		params.m_sharpen = m_sharpen->getValue();
Toshihiro Shimizu 890ddd
		params.m_shrink = shrink;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double warperScaleFactor = 1.0 / params.m_warperScale;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRectD warpedBox, warpedComputeRect, tileComputeRect;
Toshihiro Shimizu 890ddd
		m_warped->getBBox(frame, warpedBox, info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		getWarpComputeRects(tileComputeRect, warpedComputeRect, warpedBox, rect, params);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRectD warperComputeRect(TScale(warperScaleFactor) * tileComputeRect);
Toshihiro Shimizu 890ddd
		double warperEnlargement = getWarperEnlargement(params);
Toshihiro Shimizu 890ddd
		warperComputeRect = warperComputeRect.enlarge(warperEnlargement);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return tmax(
Toshihiro Shimizu 890ddd
			TRasterFx::memorySize(warpedComputeRect, info.m_bpp),
Toshihiro Shimizu 890ddd
			TRasterFx::memorySize(warperComputeRect, info.m_bpp));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(RandomWaveFx, "randomWaveFx")