Esempio n. 1
0
bool Sphere::isHit(	Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor)
{
	if(!_bbox.isHit(ray))
	{
		return false;
	}

	float t, len;
	len = len2Line(ray, &t);	
	if(len > _fRad)
	{
		return false;
	}

	if( len <= _fRad)	
	{	
		float d = sqrt(_fRad * _fRad - len * len);		
		float r = d / vecLen(ray.direction_vec);

		if(ray.bIsInObj)
		{
			t += r;
		}
		else
		{
			t -= r;
		}
	}

	*pt = t;

	//	calc normal
	vect3d pPoint, vec;
	point2point(pPoint, ray.start_point, pPoint);
	point2point(vec, ray.direction_vec, vec);	vecScale(vec, t, vec);
	point2point(pPoint, vec, pPoint);

	points2vec(_ctr, pPoint, pNormal);
	normalize(pNormal);

	//	Texture
	if(_tex != NULL && pTexColor != NULL)
	{
		float x = pPoint[0] - _ctr[0];
		float y = pPoint[1] - _ctr[1];
		float z = pPoint[2] - _ctr[2];

		float a = atan2f( x, z );
		float b = acosf( y / _fRad );

		float u = a / (2 * PI)	;
		float v = b / (1 * PI);
		//
		_tex->getTexAt( (u + 0.5) * _tex->nWidth, (v) * _tex->nHeight, *pTexColor);
	}

	return true;

}
Esempio n. 2
0
PerpCamera::PerpCamera(float fEye2Near, vect3d &pCtrPos, vect3d &pUpVec, vect3d &pViewVec, float fNtoF, float fPlaneRatio)
	: Camera(pCtrPos, pUpVec, pViewVec, fNtoF, fPlaneRatio)
{
	assert(fEye2Near > 0);
	
	//
	vect3d vInverseVec;
	vecScale(_dir, -fEye2Near, vInverseVec);
	point2point(_ctrPos, vInverseVec, _eyePos);
}
Esempio n. 3
0
bool Triangle::isHit(Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor)
{
	//	This will slow down the performance 
	//
	//if(!_bbox.isHit(ray))
	//{
	//	return false;
	//}

	float u = 0, v = 0;
	if(isTriangleHit(_vertices, ray, pt, &u, &v))
	{
		if(_bSmooth && _bHasVNorm)
		{
			vect3d vSmoothNorm;
			point2point(_vnormal[1], vSmoothNorm, vSmoothNorm);
			vecScale(vSmoothNorm, u, vSmoothNorm);

			vect3d vnorm2;
			point2point(_vnormal[2], vnorm2, vnorm2);
			vecScale(vnorm2, v, vnorm2);

			vect3d vnorm3;
			point2point(_vnormal[0], vnorm3, vnorm3);
			vecScale(vnorm3, (1 - u - v), vnorm3);

			point2point(vSmoothNorm, vnorm2, vSmoothNorm);
			point2point(vSmoothNorm, vnorm3, pNormal);

			normalize(pNormal);
		}
		else
		{
			vecCopy(pNormal, _normal);
		}
		return true;
	}
	return false;
}
Esempio n. 4
0
void PerpCamera::genViewRaysByRow(unsigned nRowCount, PixelIntegrator *pBuf)
{
	// TODO: the following code piece is redundant... 
	//

	assert(pBuf && nRowCount < WinHeight && _pSampler);
	
	for(unsigned i = 0; i < WinWidth; i ++)	//	loop for an entire row
	{
		//	to fine the current primary ray starting point
		//
		vect3d nCurrCtr;
		
		//	right vec first
		vect3d rightVec;
		point2point(rightVec, _rightVec, rightVec);
		vecScale(rightVec, (i - WinWidth/2.f) * ViewPlaneRatio / vecLen(_rightVec), rightVec);

		//	up vec second
		vect3d upVec;
		point2point(upVec, _upVec, upVec);
		vecScale(upVec, (nRowCount - WinHeight/2.f) * ViewPlaneRatio/ vecLen(_upVec), upVec);
		
		point2point(_ctrPos, rightVec, nCurrCtr);
		point2point(nCurrCtr, upVec, nCurrCtr);
		
		//	since we reuse the vector, we have to clear it before next use
		(pBuf + i)->getRays().clear();

		for(unsigned j = 0; j < _nMultiSamplingCount; j ++)
		{
			vect3d startPoint;

			//	Get current sampling start point
			float fdx = 0, fdy = 0;
			_pSampler->getNextSample(&fdx, &fdy);
			assert( (fdx >= -1 && fdx <= 1) && 
					(fdy >= -1 && fdy <= 1) );

			vect3d vDeltaXVec;
			vecScale(_rightVec, ViewPlaneRatio * fSamplingDeltaFactor * fdx, vDeltaXVec);

			vect3d vDeltaYVec;
			vecScale(_upVec, ViewPlaneRatio * fSamplingDeltaFactor * fdy, vDeltaYVec);

			point2point(nCurrCtr, vDeltaXVec, startPoint);			
			point2point(startPoint, vDeltaYVec, startPoint);			

			//	put into PixelIntegrator
			vect3d viewDir;
			points2vec(_eyePos, startPoint, viewDir);
			normalize(viewDir);
			Ray ray(startPoint, viewDir);
			ray.fDeltaX = fdx * fSamplingDeltaFactor;
			ray.fDeltaY = fdy * fSamplingDeltaFactor;
			(pBuf + i)->addRay(ray);		
		}
	}
}
Esempio n. 5
0
Cube::Cube(		float fLen, float fWidth, float fHeight,	 
				vect3d &pCenterPos,
				vect3d &pVerticalVec,
				vect3d &pHorizonVec,				
				float fReflR, float fRefrR, float fRefrK, float fEmitR)
	:PrimaryObject(fReflR, fRefrR, fRefrK, fEmitR)
{
	assert( (fLen > 0) && (fWidth > 0) && (fHeight > 0) );
	
	///
	///	Since there's no ObjObject in GPU, and I want to make primaryObj id the same as the 
	///	GPU primaryObj array index, I do this. This should not impact the current functional 
	///	code.
	///
	nCurrObj --;

	_fLength = fLen;	
	_fWidth = fWidth;	
	_fHeight = fHeight;	

	vecCopy(_vCenterPos, pCenterPos);
	vecCopy(_verticalVec, pVerticalVec);
	vecCopy(_horizonVec, pHorizonVec);

	vect3d tmpVec, tmpPoint;

	//	Top square
	vecScale(_verticalVec, _fHeight / 2.0, tmpVec);
	point2point(_vCenterPos, tmpVec, tmpPoint);
	_vs[0] = new Square(tmpPoint, _verticalVec, _horizonVec, _fLength, _fWidth);
	//_vs[0]->setColor(c1,c1,c1, c1, 0.5);

	vecScale(_verticalVec, (-1)*_fHeight / 2.0, tmpVec);
	point2point(_vCenterPos, tmpVec, tmpPoint);
	vecScale(_verticalVec, -1, tmpVec);
	_vs[1] = new Square(tmpPoint, tmpVec, _horizonVec, _fLength, _fWidth);
	//_vs[1]->setColor(c1,c1,c1, c1,0.5);

	//	Left square
	vect3d vLeftNormalVec;
	cross_product(_horizonVec, _verticalVec, vLeftNormalVec);
	normalize(vLeftNormalVec);

	vecScale(vLeftNormalVec, _fLength / 2.0, tmpVec);
	point2point(_vCenterPos, tmpVec, tmpPoint);
	_vs[2] = new Square(tmpPoint, vLeftNormalVec, _horizonVec, _fHeight, _fWidth);
	//_vs[2]->setColor(c2,c2,c2, c2,0.5);

	vecScale(vLeftNormalVec, (-1)*_fLength / 2.0, tmpVec);
	point2point(_vCenterPos, tmpVec, tmpPoint);
	vecScale(vLeftNormalVec, -1, tmpVec);
	_vs[3] = new Square(tmpPoint, tmpVec, _horizonVec, _fHeight, _fWidth);
	//_vs[3]->setColor(c2,c2,c2, c2,0.5);

	//	Right square
	vecScale(_horizonVec, _fWidth / 2.0, tmpVec);
	point2point(_vCenterPos, tmpVec, tmpPoint);
	_vs[4] = new Square(tmpPoint, _horizonVec, _verticalVec, _fLength, _fHeight);
	//_vs[4]->setColor(c3,c3,c3, c3,0.5);

	vecScale(_horizonVec, (-1)*_fWidth / 2.0, tmpVec);
	point2point(_vCenterPos, tmpVec, tmpPoint);
	vecScale(_horizonVec, -1, tmpVec);
	_vs[5] = new Square(tmpPoint, tmpVec, _verticalVec, _fLength, _fHeight);
	//_vs[5]->setColor(c3,c3,c3, c3,0.5);

	updateBBox();
}
Esempio n. 6
0
bool Square::isHit(Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor)
{
	if(!_bbox.isHit(ray))
	{
		return false;
	}

	//	The hit point on the plane
	vect3d op;
	points2vec(_vCenter, ray.start_point, op);

	float dn = dot_product(ray.direction_vec, _vNormal);
	if(dn == 0.f)
	{
		return false;
	}

	float t = - dot_product(op, _vNormal) / dn;
	//	NOTE: since it is a 0-thickness plane, we need this.
	if(t <= epsi)
	{
		return false;
	}

	//	Get the hit point
	vect3d vHitPoint;
	vect3d pView; vecScale(ray.direction_vec, t, pView);
	point2point(ray.start_point, pView, vHitPoint);

	vect3d vHitVec;
	points2vec(vHitPoint, _vCenter, vHitVec);

	float dx = dot_product(vHitVec, _v2HeightVec) / pow( _nWidth /2 , 2);
	float dy = dot_product(vHitVec, _v2WidthVec) / pow( _nHeight /2 , 2);
	
	if( fabs(dy) <= 1.f && fabs(dx) <= 1.0f )
	{
		*pt = t;
		vecCopy(pNormal, _vNormal);

		if(_tex != NULL && pTexColor != NULL)
		{
			float fWidInx = (-dx + 1.f) / 2.f * _nWidth;
			float fHeiInx = (dy + 1.f) / 2.f * _nHeight;

			switch(_eTexMapType)
			{
			case STRETCH:
				_tex->getTexAt( fWidInx / _nWidth * _tex->nWidth, 
								fHeiInx / _nHeight * _tex->nHeight, *pTexColor);
				break;

			case REPEAT:
				_tex->getTexAt( (unsigned)fWidInx % _tex->nWidth, 
								(unsigned)fHeiInx % _tex->nHeight, *pTexColor);
				break;

			case STRAIGHT:
				
				if(fWidInx < _tex->nWidth && fHeiInx < _tex->nHeight)
				{
					_tex->getTexAt( (unsigned)fWidInx, (unsigned)fHeiInx, *pTexColor);
				}
				break;

			};
		}
		return true;
	}
	
	return false;
}
ScalarType CoSegmentation::similarity_between_component_back(IndexType i, IndexType j)
{
	assert( components_[i].frame==components_[j].frame-1 );
	ScalarType toDis = 0.;
	ScalarType backDis = 0.;
	map<IndexType,IndexType> com_part;
	map<IndexType,IndexType> com_back_part;

	IndexType com_num = compute_common(components_[i], components_[j],com_part);

	IndexType com_back_num = compute_common_back(components_[i], components_[j], com_back_part);

	if (com_num==0 && com_back_num == 0) // should bigger than ..vertexes 
	{
		return INF_LOCAL;
	}

	const IndexType k = 300;
	IndexType neighbours[k];
	ScalarType dist[k];
	map<IndexType, IndexType> valid_neigs;

	//to distances

	if ( com_num > 0)
	{
		for ( map<IndexType,IndexType>::iterator iter = com_part.begin();
			iter != com_part.end(); iter++)
		{
			valid_neigs.clear();
			Sample& cur_frame = SampleSet::get_instance()[components_[i].frame];
			cur_frame.neighbours( iter->first, k, neighbours, dist );
			for (IndexType kk=0; kk<k; kk++)
			{
				auto fiter = com_part.find( neighbours[kk] );
				if( fiter!=com_part.end() )
				{
					assert(fiter->second!=-1);
					valid_neigs.insert( *fiter );
				}
			}

			IndexType neig_siz = (IndexType)valid_neigs.size();
			if(neig_siz<3)continue; 
			Matrix3X s_coord, t_coord;
			s_coord.setZero(3, neig_siz);
			t_coord.setZero(3, neig_siz);

			Sample& next_frame = SampleSet::get_instance()[ components_[j].frame ];
			IndexType vi = 0;
			IndexType kk = 0;
			for ( map<IndexType,IndexType>::iterator neig_iter = valid_neigs.begin();
				neig_iter != valid_neigs.end(); ++neig_iter )
			{
				s_coord.col(kk) = cur_frame.vertices_matrix().col(neig_iter->first );
				t_coord.col(kk) = next_frame.vertices_matrix().col(neig_iter->second);
			}
			Matrix33 rot_mat;
			MatrixXX tran_vec;

			point2point(s_coord, t_coord, rot_mat, tran_vec);

			auto bias = rot_mat*cur_frame.vertices_matrix().col(iter->first) + tran_vec 
				- next_frame.vertices_matrix().col(iter->second);

			toDis += bias.norm(); //local ICP 

		}
	}


	

	return  toDis + backDis;

}