void TransformManipulator::perform(const Ray * r)
{
	if(isDetached()) return;
	const Vector3F worldP = worldSpace().getTranslation();
	Matrix44F ps;
	parentSpace(ps);
	Plane pl(ps.transformAsNormal(hitPlaneNormal()), worldP);
	Vector3F hit, d;
    float t;
	if(pl.rayIntersect(*r, hit, t, 1)) {
		d = hit - worldP;
		if(d.length() > 10.f) return;
		
		if(m_mode == ToolContext::RotateTransform) {
			d = d.normal() * 8.f;
			hit = worldP + d;
		}
		
		d = hit - m_currentPoint;
		
		if(m_mode == ToolContext::MoveTransform)
			move(d);
		else
			spin(d);
			
		m_currentPoint = hit;
	}
}
void TransformManipulator::spin(const Vector3F & d)
{
	Matrix44F ps;
	parentSpace(ps);
	Matrix44F invps = ps;
	invps.inverse();
	
	const Vector3F worldP = ps.transform(translation());
	const Vector3F rotUp = ps.transformAsNormal(hitPlaneNormal());
	
	Vector3F toa = m_currentPoint - worldP;
	Vector3F tob = toa + d;
	
	toa.normalize();
	tob.normalize();
	float ang = toa.angleBetween(tob, toa.cross(rotUp).reversed());
	
	Vector3F angles;
	
	if(m_rotateAxis == AY) angles.set(0.f, ang, 0.f);
	else if(m_rotateAxis == AZ) angles.set(0.f, 0.f, ang);
	else angles.set(ang, 0.f, 0.f);
	
	m_subject->rotate(angles);
	setRotationAngles(m_subject->rotationAngles());
}
void TransformManipulator::move(const Vector3F & d)
{
	Matrix44F ps;
	parentSpace(ps);
	Matrix44F invps = ps;
	invps.inverse();
	
	Vector3F od = invps.transformAsNormal(d);
		
	m_subject->translate(od);
	setTranslation(m_subject->translation());
}
void TransformManipulator::start(const Ray * r)
{
	const Vector3F worldP = worldSpace().getTranslation();
	Matrix44F ps;
	parentSpace(ps);
	Plane pl(ps.transformAsNormal(hitPlaneNormal()), worldP);
	Vector3F hit, d;
	float t;
	if(pl.rayIntersect(*r, hit, t, 1)) {
		d = hit - worldP;
		if(d.length() > 10.f) return;
		
		if(m_mode == ToolContext::RotateTransform) {
			d = d.normal() * 8.f;
			hit = worldP + d;
		}
		m_startPoint = hit;
		m_currentPoint = m_startPoint;
	}
	m_started = 1;
}