Li. Rui Li. Rui - 5 months ago 30
C# Question

How to rotate the center of the ModelVisual3D object along the x, y, and z axes in turn

I have 200 pictures,and I want to build a cube with 200 pictures one by one . now I have built the cube,I want to finish the function is the center of the cube in turn rotate along the x y z axis, now I have built the cube ,and set the

Viewport3D.Camera
. I want to just rotate the cube ,and keep the cube In the camera field of view, use code in C# to finish the function.
now I will copy my code both in xaml and C# to help you understand my code.

my code in xaml:

Window x:Class="ThreeDemensionsCube.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Background="Black" >


<Viewport3D x:Name="viewport" Margin="0" Height="319" VerticalAlignment="Top" >
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup x:Name="group">
<!--Ligthts-->
<AmbientLight Color="Gray" />
<DirectionalLight Color="gray" Direction="1,-1,-1" />
<DirectionalLight Color="Gray" Direction="-1,1,1" />
</Model3DGroup>

</ModelVisual3D.Content>
</ModelVisual3D>

<Viewport3D.Camera>

<PerspectiveCamera x:Name="camera"
Position = "30, 30, 90"
LookDirection = "-30, -30, -90"
UpDirection = "0, 1, 0"
FieldOfView = "60">


<PerspectiveCamera.Transform>
<Transform3DGroup>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D
Axis="0 1 0"
Angle="{Binding ElementName=hscroll, Path=Value}" />
</RotateTransform3D.Rotation>
</RotateTransform3D>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D
Axis="0 0 1"
Angle="{Binding ElementName=vscroll, Path=Value}" />
</RotateTransform3D.Rotation>
</RotateTransform3D>
</Transform3DGroup>
</PerspectiveCamera.Transform>


</PerspectiveCamera>
</Viewport3D.Camera>
</Viewport3D>
<Border VerticalAlignment="Top" HorizontalAlignment="Right" Background="White" BorderBrush="Bisque" BorderThickness="3,5,3,5">
<StackPanel>
<Button Content="processImage" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5" Click="processImage"/>
<Button Content="loadImage" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5" Click="loadProcessedImage"/>

</StackPanel>


</Border>
<DockPanel>
<ProgressBar x:Name="pBar" DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Height="9"></ProgressBar>

</DockPanel>

</Grid>




my code in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


using System.IO;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.Structure;

using System.Windows.Media.Media3D;

namespace ThreeDemensionsCube
{
/// <summary>
/// MainWindow.xaml Interactive logic
/// </summary>
public partial class MainWindow : Window
{
string appPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"Images";
string newAppPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ProcessedImages";

double a = 0;
public MainWindow()
{
InitializeComponent();

}
private void imageProc()
{
//define a DirectoryInfo object
DirectoryInfo dir = new DirectoryInfo(appPath);
DirectoryInfo[] listDir = dir.GetDirectories();
FileInfo[] listFile = dir.GetFiles("*.jpg");
int picNum = listFile.Length;
if (picNum > 0)
{
//create new file
string newFileName = appPath.Replace("Images", "ProcessedImages");
DirectoryInfo newDir = new DirectoryInfo(newFileName);
newDir.Create();
for (int i = 0; i < picNum; i++)
{
Image<Bgra, byte> image = new Image<Bgra, byte>(listFile[i].FullName.ToString());
Image<Bgra, byte> dst = setAlphaImage(image);

string fullFileName = listFile[i].FullName.ToString().Replace("jpg", "png").Replace("Images", "ProcessedImages");

dst.ToBitmap().Save(fullFileName);
double value = i * 100.0 / picNum;
pBar.Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(pBar.SetValue), System.Windows.Threading.DispatcherPriority.Background, ProgressBar.ValueProperty, value);


}

}
else
{
MessageBox.Show("please select a valid file")

}
}
private void cubeShow()
{
//define a DirectoryInfo object
DirectoryInfo dir = new DirectoryInfo(newAppPath);
DirectoryInfo[] listDir = dir.GetDirectories();
FileInfo[] listFile = dir.GetFiles("*.png");
int picNum = listFile.Length;
if (picNum > 0)
{
for(int i=0;i<picNum;i++)
{
a += 0.1;

MeshGeometry3D mesh = new MeshGeometry3D();

mesh.Positions.Add(new Point3D(a, -10, 10));
mesh.Positions.Add(new Point3D(a, -10, -10));
mesh.Positions.Add(new Point3D(a, 10, -10));
mesh.Positions.Add(new Point3D(a, 10, 10));

mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(0);
mesh.TextureCoordinates.Add(new Point(0, 1));
mesh.TextureCoordinates.Add(new Point(1, 1));
mesh.TextureCoordinates.Add(new Point(1, 0));
mesh.TextureCoordinates.Add(new Point(0, 0));

mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(2);
mesh.TriangleIndices.Add(3);
mesh.TriangleIndices.Add(0);
mesh.TextureCoordinates.Add(new Point(0, 1));
mesh.TextureCoordinates.Add(new Point(1, 1));
mesh.TextureCoordinates.Add(new Point(1, 0));
mesh.TextureCoordinates.Add(new Point(0, 0));

BitmapImage bitmapimage = new BitmapImage(new Uri(listFile[i].FullName.ToString()));
Brush brush = new ImageBrush(bitmapimage);
GeometryModel3D mGeometry = new GeometryModel3D(mesh, new DiffuseMaterial(brush));
mGeometry.Transform = new Transform3DGroup();
group.Children.Add(mGeometry);
double value = i * 100.0 / picNum;
pBar.Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(pBar.SetValue), System.Windows.Threading.DispatcherPriority.Background, ProgressBar.ValueProperty, value);

}
}
else
{
MessageBox.Show("please select a valid file");
}

}

private Image<Bgra, byte> setAlphaImage(Image<Bgra, byte> src)
{

Image<Bgra, byte> dst = src.Copy();
int rows = src.Rows;
int cols = src.Cols;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
int blue = src.Data[i, j, 0];
int green = src.Data[i, j, 1];
int red = src.Data[i, j, 2];
//
if (Math.Abs(blue - green) < 20 && Math.Abs(blue - red) < 20 && Math.Abs(green - red) < 10)
{
dst.Data[i, j, 3] = 0;
}
//
else
{
dst.Data[i, j, 0] = 255;
dst.Data[i, j, 1] = 0;
dst.Data[i, j, 2] = 0;
dst.Data[i, j, 3] = src.Data[i, j, 0];

}
}
}
return dst;

}
private void processImage(object sender, RoutedEventArgs e)
{
imageProc();
}
private void loadProcessedImage(object sender,RoutedEventArgs e)
{
cubeShow();
}

}
}

Answer Source

now, I will post my code, the code's aim is to rotate a 3D cube object.

code in xaml:

 <PerspectiveCamera.Transform>
                    <Transform3DGroup>
                        <RotateTransform3D>
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D x:Name="rotate3D" 
                                Axis="1,0,0" />
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>

                    </Transform3DGroup>
                </PerspectiveCamera.Transform>

code in c#:

DoubleAnimation rotate = new DoubleAnimation();
        rotate.From = 0;
        rotate.To = 360;
        rotate.Duration = TimeSpan.FromSeconds(6);
       // rotate.RepeatBehavior=new RepeatBehavior(2.5);
        rotate.RepeatBehavior = RepeatBehavior.Forever;
        rotate3D.Axis = new Vector3D(0,1,1);
        rotate3D.Angle = 60;
        rotate3D.BeginAnimation(AxisAngleRotation3D.AngleProperty, rotate);

code in xaml is to craet a PerspectiveCamera.Transform and to configure it. code in c# is to creat a doubleAnimation to control the camera.

The above content is only personal opinion, if any questions please correct me. now I will show my result and some problems. good resultbad result

From results we can konw there are some problems, there has shadow On one side in bad result picture, how can I remove the shadow?