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, double yDir,
double zDir, double origin
)
{
vtkMath::Normalize(xDir);
vtkMath::Normalize(yDir);
vtkMath::Normalize(zDir);

double A = {
xDir, yDir, zDir, origin,
xDir, yDir, zDir, origin,
xDir, yDir, zDir, origin,
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 );

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->SetBackground( 0, 0, 0 );

vtkSmartPointer renderWindow =
vtkSmartPointer::New();

vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow( renderWindow );

renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
``````
Categories: CPlusPlusVTK

0 0 vote
Article Rating
Subscribe
Notify of 