public override void UpdateGeneralFromEntityBase()
        {
            double scaleMultiplyer = CubeGrid.GridSizeEnum.ToLength();
            if (IsStatic && CubeGrid.GridSizeEnum == MyCubeSize.Large)
            {
                ClassType = ClassType.Station;
            }
            else if (!IsStatic && CubeGrid.GridSizeEnum == MyCubeSize.Large)
            {
                ClassType = ClassType.LargeShip;
            }
            else if (!IsStatic && CubeGrid.GridSizeEnum == MyCubeSize.Small)
            {
                ClassType = ClassType.SmallShip;
            }

            var min = new Point3D(int.MaxValue, int.MaxValue, int.MaxValue);
            var max = new Point3D(int.MinValue, int.MinValue, int.MinValue);
            float totalMass = 0;

            foreach (var block in CubeGrid.CubeBlocks)
            {
                min.X = Math.Min(min.X, block.Min.X);
                min.Y = Math.Min(min.Y, block.Min.Y);
                min.Z = Math.Min(min.Z, block.Min.Z);

                var cubeDefinition = SpaceEngineersApi.GetCubeDefinition(block.TypeId, CubeGrid.GridSizeEnum, block.SubtypeName);

                // definition is null when the block no longer exists in the Cube definitions. Ie, Ladder, or a Mod that was removed.
                if (cubeDefinition == null || (cubeDefinition.Size.X == 1 && cubeDefinition.Size.Y == 1 && cubeDefinition.Size.z == 1))
                {
                    max.X = Math.Max(max.X, block.Min.X);
                    max.Y = Math.Max(max.Y, block.Min.Y);
                    max.Z = Math.Max(max.Z, block.Min.Z);
                }
                else
                {
                    // resolve the cube size acording to the cube's orientation.
                    var orientSize = cubeDefinition.Size.Add(-1).Transform(block.BlockOrientation).Abs();
                    max.X = Math.Max(max.X, block.Min.X + orientSize.X);
                    max.Y = Math.Max(max.Y, block.Min.Y + orientSize.Y);
                    max.Z = Math.Max(max.Z, block.Min.Z + orientSize.Z);
                }

                var cubeBlockDefinition = SpaceEngineersApi.GetCubeDefinition(block.TypeId, CubeGrid.GridSizeEnum, block.SubtypeName);

                float cubeMass = 0;
                if (cubeBlockDefinition != null)
                {
                    foreach (var component in cubeBlockDefinition.Components)
                    {
                        var cd = (MyObjectBuilder_ComponentDefinition)SpaceEngineersApi.GetDefinition(component.Type, component.Subtype);
                        float componentMass = cd.Mass * component.Count;
                        cubeMass += componentMass;
                    }
                }

                totalMass += cubeMass;
            }

            var cockpitOrientation = "None";
            var cockpits = CubeGrid.CubeBlocks.Where(b => b is MyObjectBuilder_Cockpit).ToArray();
            if (cockpits.Length > 0)
            {
                var count = cockpits.Count(b => b.BlockOrientation.Forward == cockpits[0].BlockOrientation.Forward && b.BlockOrientation.Up == cockpits[0].BlockOrientation.Up);
                if (cockpits.Length == count)
                {
                    // All cockpits share the same orientation.
                    cockpitOrientation = string.Format("Forward={0} ({1}), Up={2} ({3})", cockpits[0].BlockOrientation.Forward, GetAxisIndicator(cockpits[0].BlockOrientation.Forward), cockpits[0].BlockOrientation.Up, GetAxisIndicator(cockpits[0].BlockOrientation.Up));
                }
                else
                {
                    // multiple cockpits are present, and do not share a common orientation.
                    cockpitOrientation = "Mixed";
                }
            }
            CockpitOrientation = cockpitOrientation;

            var scale = max - min;
            scale.X++;
            scale.Y++;
            scale.Z++;

            if (CubeGrid.CubeBlocks.Count == 0)
                scale = new System.Windows.Media.Media3D.Vector3D();

            Min = min;
            Max = max;
            Scale = scale;
            Size = new Size3D(scale.X * scaleMultiplyer, scale.Y * scaleMultiplyer, scale.Z * scaleMultiplyer);
            Mass = totalMass;

            var quaternion = CubeGrid.PositionAndOrientation.Value.ToQuaternionD();
            var p1 = (min.ToVector3D() * CubeGrid.GridSizeEnum.ToLength()).Transform(quaternion) + CubeGrid.PositionAndOrientation.Value.Position - (CubeGrid.GridSizeEnum.ToLength() / 2);
            var p2 = ((min.ToVector3D() + Scale.ToVector3D()) * CubeGrid.GridSizeEnum.ToLength()).Transform(quaternion) + CubeGrid.PositionAndOrientation.Value.Position - (CubeGrid.GridSizeEnum.ToLength() / 2);
            //var p1 = VRageMath.Vector3D.Transform(min.ToVector3D() * CubeGrid.GridSizeEnum.ToLength(), quaternion) + CubeGrid.PositionAndOrientation.Value.Position - (CubeGrid.GridSizeEnum.ToLength() / 2);
            //var p2 = VRageMath.Vector3D.Transform((min.ToVector3D() + Scale.ToVector3D()) * CubeGrid.GridSizeEnum.ToLength(), quaternion) + CubeGrid.PositionAndOrientation.Value.Position - (CubeGrid.GridSizeEnum.ToLength() / 2);
            WorldAABB = new BoundingBoxD(VRageMath.Vector3D.Min(p1, p2), VRageMath.Vector3D.Max(p1, p2));
            Center = WorldAABB.Center;

            DisplayName = CubeGrid.DisplayName;

            // Add Beacon or Antenna detail for the Description.
            var broadcasters = CubeGrid.CubeBlocks.Where(b => b.SubtypeName == SubtypeId.LargeBlockBeacon.ToString()
                || b.SubtypeName == SubtypeId.SmallBlockBeacon.ToString()
                || b.SubtypeName == SubtypeId.LargeBlockRadioAntenna.ToString()
                || b.SubtypeName == SubtypeId.SmallBlockRadioAntenna.ToString()).ToArray();
            var broadcastNames = string.Empty;
            if (broadcasters.Length > 0)
            {
                var beaconNames = broadcasters.Where(b => b is MyObjectBuilder_Beacon).Select(b => ((MyObjectBuilder_Beacon)b).CustomName ?? "Beacon").ToArray();
                var antennaNames = broadcasters.Where(b => b is MyObjectBuilder_RadioAntenna).Select(b => ((MyObjectBuilder_RadioAntenna)b).CustomName ?? "Antenna").ToArray();
                broadcastNames = String.Join("|", beaconNames.Concat(antennaNames).OrderBy(s => s));
            }

            if (string.IsNullOrEmpty(broadcastNames))
                Description = string.Format("{0}×{1}×{2}", Scale.X, Scale.Y, Scale.Z, broadcastNames);
            else
                Description = string.Format("{3} {0}×{1}×{2}", Scale.X, Scale.Y, Scale.Z, broadcastNames);


            // TODO:
            // Report:
            // Reflectors On
            // Mass:      9,999,999 Kg
            // Speed:          0.0 m/s
            // Power Usage:      0.05%
            // Reactors:     12,999 GW
            // Thrusts:            999
            // Gyros:              999
            // Fuel Time:        0 sec
        }
        private void MainWindowLoaded(object sender, RoutedEventArgs e)
        {
            // cubit.Model.Freeze();
            // cubit.Material.Freeze();
            var cuboidGeometry = cubit.Model.Geometry as MeshGeometry3D;

            var vis = new ModelVisual3D();

            bool allInOneGeometry = true;

            MeshBuilder builder = null;

            if (allInOneGeometry)
                builder = new MeshBuilder();

            int countAll = 0;
            int countModelled = 0;
            for (int i = 0; i < nvert; i++)
            {
                double m = nside - i * (double)nside / nvert;
                var mx = (int)m;
                Debug.WriteLine(i + ": " + mx);
                for (int j = 0; j <= mx; j++)
                {
                    for (int k = 0; k <= mx; k++)
                    {
                        countAll++;

                        // only adding blocks on the outside...
                        if (j > 0 && j < mx - 1 && i > 0 && i < nvert - 1 && k > 0 && k < mx - 1)
                            continue;

                        countModelled++;

                        var center = new Point3D(a * (j - (double)mx / 2), a * (k - (double)mx / 2), (i + 0.5) * a);

                        if (allInOneGeometry)
                            builder.AddBox(center, a * b, a * b, a * b);
                        else
                        {
                            // add a new visual for each cubit, but reuse the geometry
                            var transform = new TranslateTransform3D(center.ToVector3D());
                            var c = new ModelVisual3D
                                        {
                                            Content =
                                                new GeometryModel3D
                                                    {
                                                        Geometry = cuboidGeometry,
                                                        Material = cubit.Material,
                                                        Transform = transform
                                                    }
                                        };
                            vis.Children.Add(c);
                        }
                    }
                }
            }

            if (allInOneGeometry)
            {
                var c = new ModelVisual3D
                            {
                                Content =
                                    new GeometryModel3D
                                        {
                                            Geometry = builder.ToMesh(),
                                            Material = cubit.Material
                                        }
                            };
                view1.Children.Add(c);
            }
            else
            {
                view1.Children.Add(vis);
            }

            var mg = cubit.Model.Geometry as MeshGeometry3D;
            int ntri = mg.TriangleIndices.Count / 3;
            int ntriTotal = ntri * countModelled;

            if (allInOneGeometry)
                ntriTotal = builder.TriangleIndices.Count / 3;

            Title = "Mini Pyramid, total " + countAll + " cubits, rendered with " + countModelled + " cubits / " + ntriTotal +
                    " triangles.";

            view1.LookAt(new Point3D(0, 0, a * nvert / 2), new Vector3D(-70, -60, -100), 0);
        }
        /// <summary>
        /// Creates view matrix
        /// </summary>
        /// <param name="eye">Eye position</param>
        /// <param name="at">Target</param>
        /// <param name="up">Up vector</param>
        /// <returns>View matrix</returns>
        public static Matrix3D CreateLookAtMatrix(Point3D eye, Point3D at, Vector3D up)
        {
            Vector3D zAxis = eye - at;
            Vector3D xAxis = Vector3D.CrossProduct(up, zAxis);
            Vector3D yAxis = Vector3D.CrossProduct(zAxis, xAxis);

            zAxis.Normalize();
            yAxis.Normalize();
            xAxis.Normalize();

            Vector3D eyeVector = eye.ToVector3D();

            return new Matrix3D(xAxis.X, yAxis.X, zAxis.X, 0,
                                xAxis.Y, yAxis.Y, zAxis.Y, 0,
                                xAxis.Z, yAxis.Z, zAxis.Z, 0,
                                -Vector3D.DotProduct(xAxis, eyeVector),
                                -Vector3D.DotProduct(yAxis, eyeVector),
                                -Vector3D.DotProduct(zAxis, eyeVector), 1);
        }
Beispiel #4
0
        private void CalCulateTransformationMatrix(Point3D estimateOrigin)
        {
            CalibrateTransform = Matrix3D.Identity;
              for (int i = 0; i < captureCalibrationPositions.Length; i++)
              {
            Vector3D vector = new Vector3D(captureCalibrationPositions[i].X - estimateOrigin.X, captureCalibrationPositions[i].Y - estimateOrigin.Y, captureCalibrationPositions[i].Z - estimateOrigin.Z);
            vector.Normalize();
            vector *= standardCalibrationPositions[i].ToVector3D().Length;
            captureCalibrationPositions[i] = new Point3D(estimateOrigin.X + vector.X, estimateOrigin.Y + vector.Y, estimateOrigin.Z + vector.Z);
              }

              Vector3D captureAxisX = new Vector3D()
              {
            X = captureCalibrationPositions[1].X - captureCalibrationPositions[0].X,
            Y = captureCalibrationPositions[1].Y - captureCalibrationPositions[0].Y,
            Z = captureCalibrationPositions[1].Z - captureCalibrationPositions[0].Z
              };

              Vector3D captureAxisZ = new Vector3D()
              {
            X = captureCalibrationPositions[0].X - captureCalibrationPositions[2].X,
            Y = captureCalibrationPositions[0].Y - captureCalibrationPositions[2].Y,
            Z = captureCalibrationPositions[0].Z - captureCalibrationPositions[2].Z
              };

              Vector3D captureAxisY = Vector3D.CrossProduct(captureAxisX, captureAxisZ);

              CalibrateTransform = Microsoft.Xna.Framework.Matrix.CreateWorld(estimateOrigin.ToVector3D().ToVector3(), captureAxisZ.ToVector3(), captureAxisY.ToVector3()).ToMatrix3D();

              CalibrateTransform.Invert();
        }