Advertisement
Guest User

3DRotations

a guest
Jan 9th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. using System.Windows.Media.Media3D;
  16.  
  17. namespace howto_3D_sphere
  18. {
  19. /// <summary>
  20. /// Interaction logic for Window1.xaml
  21. /// </summary>
  22. public partial class Window1 : Window
  23. {
  24. public Window1()
  25. {
  26. InitializeComponent();
  27. }
  28.  
  29. // The main object model group.
  30. private Model3DGroup MainModel3Dgroup = new Model3DGroup();
  31.  
  32. // The camera.
  33. private PerspectiveCamera TheCamera;
  34.  
  35. // The camera's current location.
  36. private double CameraPhi = Math.PI / 6.0; // 30 degrees
  37. private double CameraTheta = Math.PI / 6.0; // 30 degrees
  38. private double CameraR = 3.0;
  39.  
  40. // The change in CameraPhi when you press the up and down arrows.
  41. private const double CameraDPhi = 0.1;
  42.  
  43. // The change in CameraTheta when you press the left and right arrows.
  44. private const double CameraDTheta = 0.1;
  45.  
  46. // The change in CameraR when you press + or -.
  47. private const double CameraDR = 0.1;
  48.  
  49. // Create the scene.
  50. // MainViewport is the Viewport3D defined
  51. // in the XAML code that displays everything.
  52. private void Window_Loaded(object sender, RoutedEventArgs e)
  53. {
  54. // Give the camera its initial position.
  55. TheCamera = new PerspectiveCamera();
  56. TheCamera.FieldOfView = 60;
  57. MainViewport.Camera = TheCamera;
  58. PositionCamera();
  59.  
  60. // Define lights.
  61. DefineLights();
  62.  
  63. // Create the model.
  64. DefineModel(MainModel3Dgroup);
  65.  
  66. // Add the group of models to a ModelVisual3D.
  67. ModelVisual3D model_visual = new ModelVisual3D();
  68. model_visual.Content = MainModel3Dgroup;
  69.  
  70. // Display the main visual to the viewportt.
  71. MainViewport.Children.Add(model_visual);
  72. }
  73.  
  74. // Define the lights.
  75. private void DefineLights()
  76. {
  77. AmbientLight ambient_light = new AmbientLight(Colors.Gray);
  78. DirectionalLight directional_light =
  79. new DirectionalLight(Colors.Gray, new Vector3D(-1.0, -3.0, -2.0));
  80. MainModel3Dgroup.Children.Add(ambient_light);
  81. MainModel3Dgroup.Children.Add(directional_light);
  82. }
  83.  
  84. // Add the model to the Model3DGroup.
  85. private void DefineModel(Model3DGroup model_group)
  86. {
  87. #if ONE_BIG_SPHERE
  88. MeshGeometry3D mesh1 = new MeshGeometry3D();
  89. AddSphere(mesh1, new Point3D(0, 0, 0), 1, 5, 10);
  90. SolidColorBrush brush1 = Brushes.Red;
  91. DiffuseMaterial material1 = new DiffuseMaterial(brush1);
  92. GeometryModel3D model1 = new GeometryModel3D(mesh1, material1);
  93. model_group.Children.Add(model1);
  94. #else
  95. // Make spheres centered at (+/-1, 0, 0).
  96. MeshGeometry3D mesh1 = new MeshGeometry3D();
  97. AddSphere(mesh1, new Point3D(1, 0, 0), 0.25, 5, 10);
  98. AddSphere(mesh1, new Point3D(-1, 0, 0), 0.25, 5, 10);
  99. SolidColorBrush brush1 = Brushes.Red;
  100. DiffuseMaterial material1 = new DiffuseMaterial(brush1);
  101. GeometryModel3D model1 = new GeometryModel3D(mesh1, material1);
  102. model_group.Children.Add(model1);
  103.  
  104. // Make spheres centered at (0, +/-1, 0).
  105. MeshGeometry3D mesh2 = new MeshGeometry3D();
  106. AddSphere(mesh2, new Point3D(0, 1, 0), 0.25, 5, 10);
  107. AddSphere(mesh2, new Point3D(0, -1, 0), 0.25, 5, 10);
  108. SolidColorBrush brush2 = Brushes.Green;
  109. DiffuseMaterial material2 = new DiffuseMaterial(brush2);
  110. GeometryModel3D model2 = new GeometryModel3D(mesh2, material2);
  111. model_group.Children.Add(model2);
  112.  
  113. // Make spheres centered at (0, 0, +/-1).
  114. MeshGeometry3D mesh3 = new MeshGeometry3D();
  115. AddSphere(mesh3, new Point3D(0, 0, 1), 0.25, 5, 10);
  116. AddSphere(mesh3, new Point3D(0, 0, -1), 0.25, 5, 10);
  117. SolidColorBrush brush3 = Brushes.Blue;
  118. DiffuseMaterial material3 = new DiffuseMaterial(brush3);
  119. GeometryModel3D model3 = new GeometryModel3D(mesh3, material3);
  120. model_group.Children.Add(model3);
  121.  
  122. // Make a cylinder along the X axis.
  123. MeshGeometry3D mesh4 = new MeshGeometry3D();
  124. AddCylinder(mesh4, new Point3D(1, 0, 0),
  125. new Vector3D(-2, 0, 0), 0.1, 10);
  126. SolidColorBrush brush4 = Brushes.HotPink;
  127. DiffuseMaterial material4 = new DiffuseMaterial(brush4);
  128. GeometryModel3D model4 = new GeometryModel3D(mesh4, material4);
  129. model_group.Children.Add(model4);
  130.  
  131. // Make a cylinder along the Y axis.
  132. MeshGeometry3D mesh5 = new MeshGeometry3D();
  133. AddCylinder(mesh5, new Point3D(0, 1, 0),
  134. new Vector3D(0, -2, 0), 0.1, 10);
  135. SolidColorBrush brush5 = Brushes.LightGreen;
  136. DiffuseMaterial material5 = new DiffuseMaterial(brush5);
  137. GeometryModel3D model5 = new GeometryModel3D(mesh5, material5);
  138. model_group.Children.Add(model5);
  139.  
  140. // Make a cylinder along the Z axis.
  141. MeshGeometry3D mesh6 = new MeshGeometry3D();
  142. AddCylinder(mesh6, new Point3D(0, 0, 1),
  143. new Vector3D(0, 0, -2), 0.1, 10);
  144. SolidColorBrush brush6 = Brushes.LightBlue;
  145. DiffuseMaterial material6 = new DiffuseMaterial(brush6);
  146. GeometryModel3D model6 = new GeometryModel3D(mesh6, material6);
  147. model_group.Children.Add(model6);
  148. #endif
  149.  
  150. Console.WriteLine(
  151. mesh1.Positions.Count +
  152. mesh2.Positions.Count +
  153. mesh3.Positions.Count +
  154. mesh4.Positions.Count +
  155. mesh5.Positions.Count +
  156. mesh6.Positions.Count +
  157. " points");
  158. Console.WriteLine(
  159. (mesh1.TriangleIndices.Count +
  160. mesh2.TriangleIndices.Count +
  161. mesh3.TriangleIndices.Count +
  162. mesh4.TriangleIndices.Count +
  163. mesh5.TriangleIndices.Count +
  164. mesh6.TriangleIndices.Count) / 3 + " triangles");
  165. Console.WriteLine();
  166. }
  167.  
  168. // Add a triangle to the indicated mesh.
  169. // Do not reuse points so triangles don't share normals.
  170. private void AddTriangle(MeshGeometry3D mesh, Point3D point1, Point3D point2, Point3D point3)
  171. {
  172. // Create the points.
  173. int index1 = mesh.Positions.Count;
  174. mesh.Positions.Add(point1);
  175. mesh.Positions.Add(point2);
  176. mesh.Positions.Add(point3);
  177.  
  178. // Create the triangle.
  179. mesh.TriangleIndices.Add(index1++);
  180. mesh.TriangleIndices.Add(index1++);
  181. mesh.TriangleIndices.Add(index1);
  182. }
  183.  
  184. // Make a thin rectangular prism between the two points.
  185. // If extend is true, extend the segment by half the
  186. // thickness so segments with the same end points meet nicely.
  187. private void AddSegment(MeshGeometry3D mesh,
  188. Point3D point1, Point3D point2, Vector3D up)
  189. {
  190. AddSegment(mesh, point1, point2, up, false);
  191. }
  192. private void AddSegment(MeshGeometry3D mesh,
  193. Point3D point1, Point3D point2, Vector3D up,
  194. bool extend)
  195. {
  196. const double thickness = 0.25;
  197.  
  198. // Get the segment's vector.
  199. Vector3D v = point2 - point1;
  200.  
  201. if (extend)
  202. {
  203. // Increase the segment's length on both ends by thickness / 2.
  204. Vector3D n = ScaleVector(v, thickness / 2.0);
  205. point1 -= n;
  206. point2 += n;
  207. }
  208.  
  209. // Get the scaled up vector.
  210. Vector3D n1 = ScaleVector(up, thickness / 2.0);
  211.  
  212. // Get another scaled perpendicular vector.
  213. Vector3D n2 = Vector3D.CrossProduct(v, n1);
  214. n2 = ScaleVector(n2, thickness / 2.0);
  215.  
  216. // Make a skinny box.
  217. // p1pm means point1 PLUS n1 MINUS n2.
  218. Point3D p1pp = point1 + n1 + n2;
  219. Point3D p1mp = point1 - n1 + n2;
  220. Point3D p1pm = point1 + n1 - n2;
  221. Point3D p1mm = point1 - n1 - n2;
  222. Point3D p2pp = point2 + n1 + n2;
  223. Point3D p2mp = point2 - n1 + n2;
  224. Point3D p2pm = point2 + n1 - n2;
  225. Point3D p2mm = point2 - n1 - n2;
  226.  
  227. // Sides.
  228. AddTriangle(mesh, p1pp, p1mp, p2mp);
  229. AddTriangle(mesh, p1pp, p2mp, p2pp);
  230.  
  231. AddTriangle(mesh, p1pp, p2pp, p2pm);
  232. AddTriangle(mesh, p1pp, p2pm, p1pm);
  233.  
  234. AddTriangle(mesh, p1pm, p2pm, p2mm);
  235. AddTriangle(mesh, p1pm, p2mm, p1mm);
  236.  
  237. AddTriangle(mesh, p1mm, p2mm, p2mp);
  238. AddTriangle(mesh, p1mm, p2mp, p1mp);
  239.  
  240. // Ends.
  241. AddTriangle(mesh, p1pp, p1pm, p1mm);
  242. AddTriangle(mesh, p1pp, p1mm, p1mp);
  243.  
  244. AddTriangle(mesh, p2pp, p2mp, p2mm);
  245. AddTriangle(mesh, p2pp, p2mm, p2pm);
  246. }
  247.  
  248. // Add a cage.
  249. private void AddCage(MeshGeometry3D mesh)
  250. {
  251. // Top.
  252. Vector3D up = new Vector3D(0, 1, 0);
  253. AddSegment(mesh, new Point3D(1, 1, 1), new Point3D(1, 1, -1), up, true);
  254. AddSegment(mesh, new Point3D(1, 1, -1), new Point3D(-1, 1, -1), up, true);
  255. AddSegment(mesh, new Point3D(-1, 1, -1), new Point3D(-1, 1, 1), up, true);
  256. AddSegment(mesh, new Point3D(-1, 1, 1), new Point3D(1, 1, 1), up, true);
  257.  
  258. // Bottom.
  259. AddSegment(mesh, new Point3D(1, -1, 1), new Point3D(1, -1, -1), up, true);
  260. AddSegment(mesh, new Point3D(1, -1, -1), new Point3D(-1, -1, -1), up, true);
  261. AddSegment(mesh, new Point3D(-1, -1, -1), new Point3D(-1, -1, 1), up, true);
  262. AddSegment(mesh, new Point3D(-1, -1, 1), new Point3D(1, -1, 1), up, true);
  263.  
  264. // Sides.
  265. Vector3D right = new Vector3D(1, 0, 0);
  266. AddSegment(mesh, new Point3D(1, -1, 1), new Point3D(1, 1, 1), right);
  267. AddSegment(mesh, new Point3D(1, -1, -1), new Point3D(1, 1, -1), right);
  268. AddSegment(mesh, new Point3D(-1, -1, 1), new Point3D(-1, 1, 1), right);
  269. AddSegment(mesh, new Point3D(-1, -1, -1), new Point3D(-1, 1, -1), right);
  270. }
  271.  
  272. // Set the vector's length.
  273. private Vector3D ScaleVector(Vector3D vector, double length)
  274. {
  275. double scale = length / vector.Length;
  276. return new Vector3D(
  277. vector.X * scale,
  278. vector.Y * scale,
  279. vector.Z * scale);
  280. }
  281.  
  282. // Adjust the camera's position.
  283. private void Window_KeyDown(object sender, KeyEventArgs e)
  284. {
  285. switch (e.Key)
  286. {
  287. case Key.Up:
  288. CameraPhi += CameraDPhi;
  289. if (CameraPhi > Math.PI / 2.0) CameraPhi = Math.PI / 2.0;
  290. break;
  291. case Key.Down:
  292. CameraPhi -= CameraDPhi;
  293. if (CameraPhi < -Math.PI / 2.0) CameraPhi = -Math.PI / 2.0;
  294. break;
  295. case Key.Left:
  296. CameraTheta += CameraDTheta;
  297. break;
  298. case Key.Right:
  299. CameraTheta -= CameraDTheta;
  300. break;
  301. case Key.Add:
  302. case Key.OemPlus:
  303. CameraR -= CameraDR;
  304. if (CameraR < CameraDR) CameraR = CameraDR;
  305. break;
  306. case Key.Subtract:
  307. case Key.OemMinus:
  308. CameraR += CameraDR;
  309. break;
  310. }
  311.  
  312. // Update the camera's position.
  313. PositionCamera();
  314. }
  315.  
  316. // Position the camera.
  317. private void PositionCamera()
  318. {
  319. // Calculate the camera's position in Cartesian coordinates.
  320. double y = CameraR * Math.Sin(CameraPhi);
  321. double hyp = CameraR * Math.Cos(CameraPhi);
  322. double x = hyp * Math.Cos(CameraTheta);
  323. double z = hyp * Math.Sin(CameraTheta);
  324. TheCamera.Position = new Point3D(x, y, z);
  325.  
  326. // Look toward the origin.
  327. TheCamera.LookDirection = new Vector3D(-x, -y, -z);
  328.  
  329. // Set the Up direction.
  330. TheCamera.UpDirection = new Vector3D(0, 1, 0);
  331.  
  332. // Console.WriteLine("Camera.Position: (" + x + ", " + y + ", " + z + ")");
  333. }
  334.  
  335. // Add a cylinder.
  336. private void AddCylinder(MeshGeometry3D mesh, Point3D end_point, Vector3D axis, double radius, int num_sides)
  337. {
  338. // Get two vectors perpendicular to the axis.
  339. Vector3D v1;
  340. if ((axis.Z < -0.01) || (axis.Z > 0.01))
  341. v1 = new Vector3D(axis.Z, axis.Z, -axis.X - axis.Y);
  342. else
  343. v1 = new Vector3D(-axis.Y - axis.Z, axis.X, axis.X);
  344. Vector3D v2 = Vector3D.CrossProduct(v1, axis);
  345.  
  346. // Make the vectors have length radius.
  347. v1 *= (radius / v1.Length);
  348. v2 *= (radius / v2.Length);
  349.  
  350. // Make the top end cap.
  351. double theta = 0;
  352. double dtheta = 2 * Math.PI / num_sides;
  353. for (int i = 0; i < num_sides; i++)
  354. {
  355. Point3D p1 = end_point +
  356. Math.Cos(theta) * v1 +
  357. Math.Sin(theta) * v2;
  358. theta += dtheta;
  359. Point3D p2 = end_point +
  360. Math.Cos(theta) * v1 +
  361. Math.Sin(theta) * v2;
  362. AddTriangle(mesh, end_point, p1, p2);
  363. }
  364.  
  365. // Make the bottom end cap.
  366. Point3D end_point2 = end_point + axis;
  367. theta = 0;
  368. for (int i = 0; i < num_sides; i++)
  369. {
  370. Point3D p1 = end_point2 +
  371. Math.Cos(theta) * v1 +
  372. Math.Sin(theta) * v2;
  373. theta += dtheta;
  374. Point3D p2 = end_point2 +
  375. Math.Cos(theta) * v1 +
  376. Math.Sin(theta) * v2;
  377. AddTriangle(mesh, end_point2, p2, p1);
  378. }
  379.  
  380. // Make the sides.
  381. theta = 0;
  382. for (int i = 0; i < num_sides; i++)
  383. {
  384. Point3D p1 = end_point +
  385. Math.Cos(theta) * v1 +
  386. Math.Sin(theta) * v2;
  387. theta += dtheta;
  388. Point3D p2 = end_point +
  389. Math.Cos(theta) * v1 +
  390. Math.Sin(theta) * v2;
  391.  
  392. Point3D p3 = p1 + axis;
  393. Point3D p4 = p2 + axis;
  394.  
  395. AddTriangle(mesh, p1, p3, p2);
  396. AddTriangle(mesh, p2, p3, p4);
  397. }
  398. }
  399.  
  400. // Add a cylinder with smooth sides.
  401. private void AddSmoothCylinder(MeshGeometry3D mesh, Point3D end_point, Vector3D axis, double radius, int num_sides)
  402. {
  403. // Get two vectors perpendicular to the axis.
  404. Vector3D v1;
  405. if ((axis.Z < -0.01) || (axis.Z > 0.01))
  406. v1 = new Vector3D(axis.Z, axis.Z, -axis.X - axis.Y);
  407. else
  408. v1 = new Vector3D(-axis.Y - axis.Z, axis.X, axis.X);
  409. Vector3D v2 = Vector3D.CrossProduct(v1, axis);
  410.  
  411. // Make the vectors have length radius.
  412. v1 *= (radius / v1.Length);
  413. v2 *= (radius / v2.Length);
  414.  
  415. // Make the top end cap.
  416. // Make the end point.
  417. int pt0 = mesh.Positions.Count; // Index of end_point.
  418. mesh.Positions.Add(end_point);
  419.  
  420. // Make the top points.
  421. double theta = 0;
  422. double dtheta = 2 * Math.PI / num_sides;
  423. for (int i = 0; i < num_sides; i++)
  424. {
  425. mesh.Positions.Add(end_point +
  426. Math.Cos(theta) * v1 +
  427. Math.Sin(theta) * v2);
  428. theta += dtheta;
  429. }
  430.  
  431. // Make the top triangles.
  432. int pt1 = mesh.Positions.Count - 1; // Index of last point.
  433. int pt2 = pt0 + 1; // Index of first point in this cap.
  434. for (int i = 0; i < num_sides; i++)
  435. {
  436. mesh.TriangleIndices.Add(pt0);
  437. mesh.TriangleIndices.Add(pt1);
  438. mesh.TriangleIndices.Add(pt2);
  439. pt1 = pt2++;
  440. }
  441.  
  442. // Make the bottom end cap.
  443. // Make the end point.
  444. pt0 = mesh.Positions.Count; // Index of end_point2.
  445. Point3D end_point2 = end_point + axis;
  446. mesh.Positions.Add(end_point2);
  447.  
  448. // Make the bottom points.
  449. theta = 0;
  450. for (int i = 0; i < num_sides; i++)
  451. {
  452. mesh.Positions.Add(end_point2 +
  453. Math.Cos(theta) * v1 +
  454. Math.Sin(theta) * v2);
  455. theta += dtheta;
  456. }
  457.  
  458. // Make the bottom triangles.
  459. theta = 0;
  460. pt1 = mesh.Positions.Count - 1; // Index of last point.
  461. pt2 = pt0 + 1; // Index of first point in this cap.
  462. for (int i = 0; i < num_sides; i++)
  463. {
  464. mesh.TriangleIndices.Add(num_sides + 1); // end_point2
  465. mesh.TriangleIndices.Add(pt2);
  466. mesh.TriangleIndices.Add(pt1);
  467. pt1 = pt2++;
  468. }
  469.  
  470. // Make the sides.
  471. // Add the points to the mesh.
  472. int first_side_point = mesh.Positions.Count;
  473. theta = 0;
  474. for (int i = 0; i < num_sides; i++)
  475. {
  476. Point3D p1 = end_point +
  477. Math.Cos(theta) * v1 +
  478. Math.Sin(theta) * v2;
  479. mesh.Positions.Add(p1);
  480. Point3D p2 = p1 + axis;
  481. mesh.Positions.Add(p2);
  482. theta += dtheta;
  483. }
  484.  
  485. // Make the side triangles.
  486. pt1 = mesh.Positions.Count - 2;
  487. pt2 = pt1 + 1;
  488. int pt3 = first_side_point;
  489. int pt4 = pt3 + 1;
  490. for (int i = 0; i < num_sides; i++)
  491. {
  492. mesh.TriangleIndices.Add(pt1);
  493. mesh.TriangleIndices.Add(pt2);
  494. mesh.TriangleIndices.Add(pt4);
  495.  
  496. mesh.TriangleIndices.Add(pt1);
  497. mesh.TriangleIndices.Add(pt4);
  498. mesh.TriangleIndices.Add(pt3);
  499.  
  500. pt1 = pt3;
  501. pt3 += 2;
  502. pt2 = pt4;
  503. pt4 += 2;
  504. }
  505. }
  506.  
  507. // Add a sphere.
  508. private void AddSphere(MeshGeometry3D mesh, Point3D center, double radius, int num_phi, int num_theta)
  509. {
  510. double phi0, theta0;
  511. double dphi = Math.PI / num_phi;
  512. double dtheta = 2 * Math.PI / num_theta;
  513.  
  514. phi0 = 0;
  515. double y0 = radius * Math.Cos(phi0);
  516. double r0 = radius * Math.Sin(phi0);
  517. for (int i = 0; i < num_phi; i++)
  518. {
  519. double phi1 = phi0 + dphi;
  520. double y1 = radius * Math.Cos(phi1);
  521. double r1 = radius * Math.Sin(phi1);
  522.  
  523. // Point ptAB has phi value A and theta value B.
  524. // For example, pt01 has phi = phi0 and theta = theta1.
  525. // Find the points with theta = theta0.
  526. theta0 = 0;
  527. Point3D pt00 = new Point3D(
  528. center.X + r0 * Math.Cos(theta0),
  529. center.Y + y0,
  530. center.Z + r0 * Math.Sin(theta0));
  531. Point3D pt10 = new Point3D(
  532. center.X + r1 * Math.Cos(theta0),
  533. center.Y + y1,
  534. center.Z + r1 * Math.Sin(theta0));
  535. for (int j = 0; j < num_theta; j++)
  536. {
  537. // Find the points with theta = theta1.
  538. double theta1 = theta0 + dtheta;
  539. Point3D pt01 = new Point3D(
  540. center.X + r0 * Math.Cos(theta1),
  541. center.Y + y0,
  542. center.Z + r0 * Math.Sin(theta1));
  543. Point3D pt11 = new Point3D(
  544. center.X + r1 * Math.Cos(theta1),
  545. center.Y + y1,
  546. center.Z + r1 * Math.Sin(theta1));
  547. //Apply rotation to the points
  548. pt00 = Rotate(pt00);
  549. pt00 = Rotate(pt01);
  550. pt00 = Rotate(pt10);
  551. pt00 = Rotate(pt11);
  552.  
  553. // Create the triangles.
  554. AddTriangle(mesh, pt00, pt11, pt10);
  555. AddTriangle(mesh, pt00, pt01, pt11);
  556.  
  557. // Move to the next value of theta.
  558. theta0 = theta1;
  559. pt00 = pt01;
  560. pt10 = pt11;
  561. }
  562.  
  563. // Move to the next value of phi.
  564. phi0 = phi1;
  565. y0 = y1;
  566. r0 = r1;
  567. }
  568. }
  569.  
  570. private Point3D Rotate(Point3D target)
  571. {
  572. double m_su, m_cu, m_sv, m_cv, m_sw, m_cw, m_r11, m_r12, m_r13, m_r21, m_r22, m_r23, m_r31, m_r32, m_r33;
  573. double m_roll = 0.25;
  574. double m_pitch = 0.0;
  575. double m_yaw = 0.0;
  576. m_su = Math.Sin(m_roll);
  577. m_cu = Math.Cos(m_roll);
  578. m_sv = Math.Sin(m_pitch);
  579. m_cv = Math.Cos(m_pitch);
  580. m_sw = Math.Sin(m_yaw);
  581. m_cw = Math.Cos(m_yaw);
  582. m_r11 = m_cv * m_cw;
  583. m_r12 = m_su * m_sv * m_cw - m_cu * m_sw;
  584. m_r13 = m_su * m_sw + m_cu * m_sv * m_cw;
  585. m_r21 = m_cv * m_sw;
  586. m_r22 = m_cu * m_cw + m_su * m_sv * m_sw;
  587. m_r23 = m_cu * m_sv * m_sw - m_su * m_cw;
  588. m_r31 = -m_sv;
  589. m_r32 = m_su * m_cv;
  590. m_r33 = m_cu * m_cv;
  591.  
  592. Point3D returnPoint = new Point3D(0, 0, 0);
  593.  
  594. returnPoint.X = m_r11 * target.X + m_r12 * target.Y + m_r13 * target.Z;
  595. returnPoint.Y = m_r21 * target.X + m_r22 * target.Y + m_r23 * target.Z;
  596. returnPoint.Z = m_r31 * target.X + m_r32 * target.Y + m_r33 * target.Z;
  597. return returnPoint;
  598. }
  599. }
  600. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement