Ejemplo n.º 1
0
void CameraController::focusOnBounds(const Imath::Box3f& bounds)
{
	using namespace Imath;
	V3f fwd = m_parameters->forwardUnitVector();
	V3f center = bounds.center();
	float distance = bounds.size().length() / (2.0f * tan(m_parameters->fovY() / 2));
	m_parameters->setEyeTarget(center - fwd * distance, center);
}
Ejemplo n.º 2
0
void DotNodeGadget::updateLabel()
{
	const Dot *dot = dotNode();

	const Dot::LabelType labelType = (Dot::LabelType)dot->labelTypePlug()->getValue();
	if( labelType == Dot::None )
	{
		m_label.clear();
	}
	else if( labelType == Dot::NodeName )
	{
		m_label = dot->getName();
	}
	else if( labelType == Dot::UpstreamNodeName )
	{
		const Node *n = upstreamNode();
		m_label = n ? n->getName() : "";
	}
	else
	{
		m_label = dot->labelPlug()->getValue();
	}

	Edge labelEdge = RightEdge;
	if( const Plug *p = dot->inPlug<Plug>() )
	{
		if( noduleTangent( nodule( p ) ).x != 0 )
		{
			labelEdge = TopEdge;
		}
	}

	const Imath::Box3f thisBound = bound();
	if( labelEdge == TopEdge )
	{
		const Imath::Box3f labelBound = style()->textBound( Style::LabelText, m_label );
		m_labelPosition = V2f(
			-labelBound.size().x / 2.0,
			thisBound.max.y + 1.0
		);
	}
	else
	{
		const Imath::Box3f characterBound = style()->characterBound( Style::LabelText );
		m_labelPosition = V2f(
			thisBound.max.x,
			thisBound.center().y - characterBound.size().y / 2.0
		);
	}

	requestRender();
}
Ejemplo n.º 3
0
 bool rayPassesNearOrThrough(const V3d& rayOrigin, const V3d& rayDir) const
 {
     const double diagRadius = 1.2*bbox.size().length()/2; // Sphere diameter is length of box diagonal
     const V3d o2c = bbox.center() - rayOrigin; // vector from rayOrigin to box center
     const double l = o2c.length();
     if(l < diagRadius)
         return true; // rayOrigin lies within bounding sphere
     // rayOrigin lies outside of bounding sphere
     const double cosA = o2c.dot(rayDir)/l;  // cosine of angle between rayDir and vector from origin to center
     if(cosA < DBL_MIN)
         return false; // rayDir points to side or behind with respect to direction from origin to center of box
     const double sinA = sqrt(1 - cosA*cosA); // sine of angle between rayDir and vector from origin to center
     return sinA/cosA < diagRadius/l;
 }
Ejemplo n.º 4
0
void CameraController::frame( const Imath::Box3f &box, const Imath::V3f &viewDirection, const Imath::V3f &upVector )
{
	// make a matrix to centre the camera on the box, with the appropriate view direction
	M44f cameraMatrix = rotationMatrixWithUpDir( V3f( 0, 0, -1 ), viewDirection, upVector );
	M44f translationMatrix;
	translationMatrix.translate( box.center() );
	cameraMatrix *= translationMatrix;

	// translate the camera back until the box is completely visible
	M44f inverseCameraMatrix = cameraMatrix.inverse();
	Box3f cBox = transform( box, inverseCameraMatrix );

	Box2f screenWindow = m_data->screenWindow->readable();
	if( m_data->projection->readable()=="perspective" )
	{
		// perspective. leave the field of view and screen window as is and translate
		// back till the box is wholly visible. this currently assumes the screen window
		// is centred about the camera axis.
		float z0 = cBox.size().x / screenWindow.size().x;
		float z1 = cBox.size().y / screenWindow.size().y;

		m_data->centreOfInterest = std::max( z0, z1 ) / tan( M_PI * m_data->fov->readable() / 360.0 ) + cBox.max.z +
			m_data->clippingPlanes->readable()[0];

		cameraMatrix.translate( V3f( 0.0f, 0.0f, m_data->centreOfInterest ) );
	}
	else
	{
		// orthographic. translate to front of box and set screen window
		// to frame the box, maintaining the aspect ratio of the screen window.
		m_data->centreOfInterest = cBox.max.z + m_data->clippingPlanes->readable()[0] + 0.1; // 0.1 is a fudge factor
		cameraMatrix.translate( V3f( 0.0f, 0.0f, m_data->centreOfInterest ) );

		float xScale = cBox.size().x / screenWindow.size().x;
		float yScale = cBox.size().y / screenWindow.size().y;
		float scale = std::max( xScale, yScale );

		V2f newSize = screenWindow.size() * scale;
		screenWindow.min.x = cBox.center().x - newSize.x / 2.0f;
		screenWindow.min.y = cBox.center().y - newSize.y / 2.0f;
		screenWindow.max.x = cBox.center().x + newSize.x / 2.0f;
		screenWindow.max.y = cBox.center().y + newSize.y / 2.0f;
	}

	m_data->transform->matrix = cameraMatrix;
	m_data->screenWindow->writable() = screenWindow;
}