I write a article about how to create a local coordinate system for a dependent 3d model, LOCAL COORDINATE SYSTEM
We can real value in world coordinate system of vector or point on 3D model.
For example, I want to know unit vector on model’s X axis or point’s coordinate on model’s center, local coordinate system can help us.
We can get model’s world coordinate based on local coordinate.
Actually there is a transform which can help us to find local coordinate based on world coordinate value.
We also need a home point and at least two axis vectors to construct the transform.
The example is similar to the article LOCAL COORDINATE SYSTEM
vtkSmartPointer CreateLocalToWorldTransform
(
double xDir[3], double yDir[3],
double zDir[3], double origin[3]
)
{
vtkMath::Normalize(xDir);
vtkMath::Normalize(yDir);
vtkMath::Normalize(zDir);
double A[16] = {
xDir[0], yDir[0], zDir[0], origin[0],
xDir[1], yDir[1], zDir[1], origin[1],
xDir[2], yDir[2], zDir[2], origin[2],
0, 0, 0, 1 };
vtkMatrix4x4 *localToWorldMatrix = vtkMatrix4x4::New();
localToWorldMatrix->DeepCopy(A);
vtkSmartPointer localToWorldTrans = vtkSmartPointer::New();
localToWorldTrans->Identity();
localToWorldTrans->SetMatrix(localToWorldMatrix);
localToWorldMatrix->Delete();
return localToWorldTrans;
}
int main()
{
setbuf( stdout, nullptr );
// ============ plane start ================
vtkSmartPointer planeSource =
vtkSmartPointer::New();
planeSource->SetOrigin( 0, 0, 0 ); //Relative
planeSource->SetPoint1( 0, 2, 0 );
planeSource->SetPoint2( 2, 0, 0 );
vtkSmartPointer planeMapper =
vtkSmartPointer::New();
planeMapper->SetInputConnection( planeSource->GetOutputPort() );
vtkSmartPointer planeActor =
vtkSmartPointer::New();
planeActor->SetMapper( planeMapper );
//planeActor->SetPosition( 1, 1, 0 );
// ============ plane end ================
// ============ sphere start ================
vtkSmartPointer sphereSource =
vtkSmartPointer::New();
sphereSource->SetCenter( 2, 2, 0 );
sphereSource->SetRadius( 0.1 );
vtkSmartPointer sphereMapper =
vtkSmartPointer::New();
sphereMapper->SetInputConnection( sphereSource->GetOutputPort() );
vtkSmartPointer sphereActor =
vtkSmartPointer::New();
sphereActor->SetMapper( sphereMapper );
sphereActor->GetProperty()->SetColor( 1, 0, 0 );
// ============ plane end ================
// Change actor status.
planeActor->RotateY( 90 );
PointStruct homePt( 0, 0, 0 ); // center point in local coordinate system
PointStruct xPt( 0, 0, -1 ); // X axis vector in local coordinate system
PointStruct yPt( 0, 1, 0 ); // Y axis vector in local coordinate system
PointStruct zPt( 1, 0, 0 ); // Z axis vector in local coordinate system
vtkSmartPointer localToWorldTransform = CreateLocalToWorldTransform( xPt.point, yPt.point, zPt.point, homePt.point );
localToWorldTransform->Inverse();
xPt = PointStruct( localToWorldTransform->TransformDoublePoint( xPt.point ) );
yPt = PointStruct( localToWorldTransform->TransformDoublePoint( yPt.point ) );
zPt = PointStruct( localToWorldTransform->TransformDoublePoint( zPt.point ) );
cout << " " << xPt << " " << yPt << " " << zPt;
/*
PointStruct [1, 0, 0]
PointStruct [0, 1, 0]
PointStruct [0, 0, 1]
*/
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor( planeActor );
renderer->AddActor( sphereActor );
renderer->SetBackground( 0, 0, 0 );
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer( renderer );
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow( renderWindow );
renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}