helix-toolkit
helix-toolkit copied to clipboard
Rotating the object from the center point
Hi,
I can view a 3D object as follows. I want to rotate this object according to certain values, but the object is not rotating from its center point. Object rotates through corner points.
I need to use the RotateAt command instead of the rotate command, but I could not find how to determine the object's center point. Thanks for the information.
`
private const string MODEL_PATH = "C:\\Users\\SAYKAL-12\\Desktop\\dice.stl";
public MainWindow()
{
InitializeComponent();
ModelVisual3D device3D = new ModelVisual3D();
device3D.Content = Display3d(MODEL_PATH);
viewPort3d.Children.Add(device3D);
var axis = new Vector3D(0, 0, 1);
var angle = 50;
var matrix = device3D.Transform.Value;
matrix.Rotate(new Quaternion(axis, angle));
device3D.Transform = new MatrixTransform3D(matrix);
}
private Model3D Display3d(string model)
{
Model3D device = null;
try
{
viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);
ModelImporter import = new ModelImporter();
device = import.Load(model);
}
catch (Exception e)
{
MessageBox.Show("Exception Error : " + e.StackTrace);
}
return device;
}
`
xaml Code
<helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="true" Margin="0,0,0,66" Cursor="Hand" ShowViewCube="True"> <!-- Remember to add light to the scene --> <helix:SunLight/> </helix:HelixViewport3D>
You need to calculate the center point yourself by calculating the center point of the geometry vertices loaded from your model.
In my question is a clue of how to find the center point. The uploaded file type is .stl. How can I find the center point of this file with helixtoolkit?
I did it in my own project by finding the bounding sphere. If you have a geometry made of several parts you'll need to find the bounding sphere of two spheres. Here is my code that does it:
private static SharpDX.BoundingSphere Merge(SharpDX.BoundingSphere s1, SharpDX.BoundingSphere s2)
{
if (s1.Radius == 0f)
{
return s2;
}
if (s2.Radius == 0f)
{
return s1;
}
var s1s2 = s1.Center - s2.Center;
var distance = s1s2.Length();
// Co-centric sphere => return biggest one
if (distance == 0f)
{
return s1.Radius > s2.Radius ? s1 : s2;
}
// One sphere is fully contained into the other one
if (distance <= Math.Abs(s1.Radius - s2.Radius))
{
return s1.Radius > s2.Radius ? s1 : s2;
}
var unit = s1s2.Normalized();
var point1 = s1.Center + s1.Radius * unit;
var point2 = s2.Center - s2.Radius * unit;
var diameter = point2 - point1;
var radius = diameter.Length() / 2f;
var center = point1 + diameter.Normalized() * radius;
return new SharpDX.BoundingSphere(center, radius);
}
You can use Size property of model,
... var x_middle_point =device3D.Content.Bounds.SizeX/2; var y_middle_point= device3D.Content.Bounds.SizeY/2; var z_middle_point= device3D.Content.Bounds.SizeZ/2; Point3D middlePoint=new Point3D(x_middle_point ,y_middle_point, z_middle_point); ...