Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Kinect.Fusion;
  7. namespace RoboLibrary
  8. {
  9.     public class CameraToRobotCalibrator
  10.     {
  11.         //offset in mm compared to robot origo
  12.         public float x_offset = -0.135f;
  13.         public float y_offset = -1.095f;
  14.         public float z_offset = 1.738f;
  15.  
  16.         //scale
  17.         public float s_x = -1f;
  18.         public float s_y = 1f;
  19.         public float s_z = -1f;
  20.  
  21.         //camera rotated around its own origo
  22.         public float x_rotation = 0f;
  23.         public float y_rotation = 0f;
  24.         public float z_rotation = 90;
  25.  
  26.         public Matrix4 TranslationMatrix;
  27.  
  28.         public ComputerVisionLibrary.Mesh ConvertToRobospace(ComputerVisionLibrary.Mesh mesh)
  29.         {
  30.             Matrix4 transformMatrix = CalculateTransformMatrix();
  31.             List<Vector3> vertices = mesh.Vertices;
  32.             List<Vector3> transformedVertices = new List<Vector3>();
  33.             foreach (var v in vertices)
  34.             {
  35.                 transformedVertices.Add(TransformVertex(v, transformMatrix));
  36.             }
  37.             mesh.Vertices = transformedVertices;
  38.             return mesh;
  39.         }
  40.  
  41.         Matrix4 t; //translation
  42.         Matrix4 x; //rotation x
  43.         Matrix4 y; //rotation y
  44.         Matrix4 z; //rotation z
  45.         Matrix4 s; //scale
  46.  
  47.         public float ToRadians(float degrees)
  48.         {
  49.             return (float)(degrees * (Math.PI / 180));
  50.         }
  51.         public void SetUpMatrixes()
  52.         {
  53.             x_rotation = ToRadians(x_rotation);
  54.             y_rotation = ToRadians(y_rotation);
  55.             z_rotation = ToRadians(z_rotation);
  56.  
  57.             t.M11 = 1; t.M12 = 0; t.M13 = 0; t.M14 = x_offset;
  58.             t.M21 = 0; t.M22 = 1; t.M23 = 0; t.M24 = y_offset;
  59.             t.M31 = 0; t.M32 = 0; t.M33 = 1; t.M34 = z_offset;
  60.             t.M41 = 0; t.M42 = 0; t.M43 = 0; t.M44 = 1;
  61.  
  62.  
  63.             x.M11 = 1; x.M12 = 0; x.M13 = 0; x.M14 = 0;
  64.             x.M21 = 0; x.M22 = (float)Math.Cos(x_rotation); x.M23 = -(float)Math.Sin(x_rotation); x.M24 = 0;
  65.             x.M31 = 0; x.M32 = (float)Math.Sin(x_rotation); x.M33 = (float)Math.Cos(x_rotation); x.M34 = 0;
  66.             x.M41 = 0; x.M42 = 0; x.M43 = 0; x.M44 = 1;
  67.  
  68.             y = new Matrix4();
  69.             y.M11 = (float)Math.Cos(y_rotation); y.M12 = 0; y.M13 = (float)Math.Sin(y_rotation); y.M14 = 0;
  70.             y.M21 = 0; y.M22 = 1; y.M23 = 0; y.M24 = 0;
  71.             y.M31 = -(float)Math.Sin(y_rotation); y.M32 = 0; y.M33 = (float)Math.Cos(y_rotation); y.M34 = 0;
  72.             y.M41 = 0; y.M42 = 0; y.M43 = 0; y.M44 = 1;
  73.  
  74.             z = new Matrix4();
  75.             z.M11 = (float)Math.Cos(z_rotation); z.M12 = -(float)Math.Sin(z_rotation); z.M13 = 0; z.M14 = 0;
  76.             z.M21 = (float)Math.Sin(z_rotation); z.M22 = (float)Math.Cos(z_rotation); z.M23 = 0; z.M24 = 0;
  77.             z.M31 = 0; z.M32 = 0; z.M33 = 1; z.M34 = 0;
  78.             z.M41 = 0; z.M42 = 0; z.M43 = 0; z.M44 = 1;
  79.  
  80.             s = new Matrix4();
  81.             s.M11 = s_x; s.M12 = 0; s.M13 = 0; s.M14 = 0;
  82.             s.M21 = 0; s.M22 = s_y; s.M23 = 0; s.M24 = 0;
  83.             s.M31 = 0; s.M32 = 0; s.M33 = s_z; s.M34 = 0;
  84.             s.M41 = 0; s.M42 = 0; s.M43 = 0; s.M44 = 1;
  85.  
  86.         }
  87.  
  88.         public static Matrix4 Multiply(Matrix4 a, Matrix4 b)
  89.         {
  90.             Matrix4 c = new Matrix4();
  91.             c.M11 = a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41;
  92.             c.M12 = a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42;
  93.             c.M13 = a.M11 * b.M13 + a.M12 * b.M23 + a.M13 * b.M33 + a.M14 * b.M43;
  94.             c.M14 = a.M11 * b.M14 + a.M12 * b.M24 + a.M13 * b.M43 + a.M14 * b.M44;
  95.  
  96.             c.M21 = a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31 + a.M24 * b.M41;
  97.             c.M22 = a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32 + a.M24 * b.M42;
  98.             c.M23 = a.M21 * b.M13 + a.M22 * b.M23 + a.M23 * b.M33 + a.M24 * b.M43;
  99.             c.M24 = a.M21 * b.M14 + a.M22 * b.M24 + a.M23 * b.M43 + a.M24 * b.M44;
  100.  
  101.             c.M31 = a.M31 * b.M11 + a.M32 * b.M21 + a.M33 * b.M31 + a.M34 * b.M41;
  102.             c.M32 = a.M31 * b.M12 + a.M32 * b.M22 + a.M33 * b.M32 + a.M34 * b.M42;
  103.             c.M33 = a.M31 * b.M13 + a.M32 * b.M23 + a.M33 * b.M33 + a.M34 * b.M43;
  104.             c.M34 = a.M31 * b.M14 + a.M32 * b.M24 + a.M33 * b.M43 + a.M34 * b.M44;
  105.  
  106.             c.M41 = a.M41 * b.M11 + a.M42 * b.M21 + a.M43 * b.M31 + a.M44 * b.M41;
  107.             c.M42 = a.M41 * b.M12 + a.M42 * b.M22 + a.M43 * b.M32 + a.M44 * b.M42;
  108.             c.M43 = a.M41 * b.M13 + a.M42 * b.M23 + a.M43 * b.M33 + a.M44 * b.M43;
  109.             c.M44 = a.M41 * b.M14 + a.M42 * b.M24 + a.M43 * b.M43 + a.M44 * b.M44;
  110.  
  111.             return c;
  112.         }
  113.  
  114.         public Matrix4 CalculateTransformMatrix()
  115.         {
  116.             //rotate the Kinect 90 degrees around the z-axis
  117.             //mirror x
  118.             //mirror z
  119.             Matrix4 rotated = z;
  120.             Matrix4 mirrored = Multiply(s, rotated);
  121.             Matrix4 translated = Multiply(t, mirrored);
  122.             return translated;
  123.         }
  124.  
  125.         public Vector3 TransformVertex(Vector3 vertex, Matrix4 transformM)
  126.         {
  127.             float x, y, z;
  128.             x = transformM.M11 * vertex.X + transformM.M12 * vertex.Y + transformM.M13 * vertex.Z + transformM.M14 * 1;
  129.             y = transformM.M21 * vertex.X + transformM.M22 * vertex.Y + transformM.M23 * vertex.Z + transformM.M24 * 1;
  130.             z = transformM.M31 * vertex.X + transformM.M32 * vertex.Y + transformM.M33 * vertex.Z + transformM.M34 * 1;
  131.             return new Vector3() { X = x, Y = y, Z = z };
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement