/Controls/HelixToolkit.Wpf/Visual3Ds/MeshVisuals/TestModels/Teapot.cs

https://bitbucket.org/kubrakms/thermalconductivity · C# · 164 lines · 98 code · 20 blank · 46 comment · 0 complexity · 1b99482de92b10aa1e87861b9eadc341 MD5 · raw file

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Teapot.cs" company="Helix 3D Toolkit">
  3. // http://helixtoolkit.codeplex.com, license: Ms-PL
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace HelixToolkit.Wpf
  7. {
  8. using System;
  9. using System.Windows;
  10. using System.Windows.Media.Media3D;
  11. /// <summary>
  12. /// A visual element that shows the Utah teapot test model.
  13. /// </summary>
  14. public class Teapot : MeshElement3D
  15. {
  16. #region Constants and Fields
  17. /// <summary>
  18. /// The position property.
  19. /// </summary>
  20. public static readonly DependencyProperty PositionProperty = DependencyProperty.Register(
  21. "Position", typeof(Point3D), typeof(Teapot), new UIPropertyMetadata(new Point3D(0, 0, 1), TransformChanged));
  22. /// <summary>
  23. /// The spout direction property.
  24. /// </summary>
  25. public static readonly DependencyProperty SpoutDirectionProperty = DependencyProperty.Register(
  26. "SpoutDirection",
  27. typeof(Vector3D),
  28. typeof(Teapot),
  29. new UIPropertyMetadata(new Vector3D(1, 0, 0), TransformChanged));
  30. /// <summary>
  31. /// The up direction property.
  32. /// </summary>
  33. public static readonly DependencyProperty UpDirectionProperty = DependencyProperty.Register(
  34. "UpDirection",
  35. typeof(Vector3D),
  36. typeof(Teapot),
  37. new UIPropertyMetadata(new Vector3D(0, 0, 1), TransformChanged));
  38. #endregion
  39. #region Public Properties
  40. /// <summary>
  41. /// Gets or sets the position.
  42. /// </summary>
  43. /// <value>The position.</value>
  44. public Point3D Position
  45. {
  46. get
  47. {
  48. return (Point3D)this.GetValue(PositionProperty);
  49. }
  50. set
  51. {
  52. this.SetValue(PositionProperty, value);
  53. }
  54. }
  55. /// <summary>
  56. /// Gets or sets the spout direction.
  57. /// </summary>
  58. /// <value>The spout direction.</value>
  59. public Vector3D SpoutDirection
  60. {
  61. get
  62. {
  63. return (Vector3D)this.GetValue(SpoutDirectionProperty);
  64. }
  65. set
  66. {
  67. this.SetValue(SpoutDirectionProperty, value);
  68. }
  69. }
  70. /// <summary>
  71. /// Gets or sets up direction.
  72. /// </summary>
  73. /// <value>Up direction.</value>
  74. public Vector3D UpDirection
  75. {
  76. get
  77. {
  78. return (Vector3D)this.GetValue(UpDirectionProperty);
  79. }
  80. set
  81. {
  82. this.SetValue(UpDirectionProperty, value);
  83. }
  84. }
  85. #endregion
  86. #region Methods
  87. /// <summary>
  88. /// Do the tesselation and return the <see cref="MeshGeometry3D"/>.
  89. /// </summary>
  90. /// <returns>
  91. /// </returns>
  92. protected override MeshGeometry3D Tessellate()
  93. {
  94. var rd =
  95. Application.LoadComponent(
  96. new Uri("HelixToolkit.Wpf;component/Resources/TeapotGeometry.xaml", UriKind.Relative)) as
  97. ResourceDictionary;
  98. this.OnTransformChanged();
  99. return rd["TeapotGeometry"] as MeshGeometry3D;
  100. }
  101. /// <summary>
  102. /// The transform changed.
  103. /// </summary>
  104. /// <param name="d">
  105. /// The sender.
  106. /// </param>
  107. /// <param name="e">
  108. /// The event arguments.
  109. /// </param>
  110. private static void TransformChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  111. {
  112. ((Teapot)d).OnTransformChanged();
  113. }
  114. /// <summary>
  115. /// Called when the transform is changed.
  116. /// </summary>
  117. private void OnTransformChanged()
  118. {
  119. Vector3D right = this.SpoutDirection;
  120. Vector3D back = Vector3D.CrossProduct(this.UpDirection, right);
  121. Vector3D up = this.UpDirection;
  122. this.Transform =
  123. new MatrixTransform3D(
  124. new Matrix3D(
  125. right.X,
  126. right.Y,
  127. right.Z,
  128. 0,
  129. up.X,
  130. up.Y,
  131. up.Z,
  132. 0,
  133. back.X,
  134. back.Y,
  135. back.Z,
  136. 0,
  137. this.Position.X,
  138. this.Position.Y,
  139. this.Position.Z,
  140. 1));
  141. }
  142. #endregion
  143. }
  144. }