Beispiel #1
0
        public void Cross()
        {
            var p1 = new Vec(1, 2, 3);

            Assert.AreEqual(Math.Sqrt(96), p1.CrossMagnitude(new Vec(3, 2, 1)));
            Assert.AreEqual(96, p1.CrossSquareMagnitude(new Vec(3, 2, 1)));
            Assert.AreEqual(new Vec(-4, 8, -4), p1.Crossed(new Vec(3, 2, 1)));
            p1.Cross(new Vec(3, 2, 1));
            Assert.AreEqual(new Vec(-4, 8, -4), p1);
        }
Beispiel #2
0
        public static bool Export(IEnumerable <Body> bodies, string fileName, bool binaryFormat)
        {
            var sumTriangleCount = 0;
            var triangulations   = new List <TriangulationData>();

            foreach (var body in bodies)
            {
                var shape = body.Shape.GetTransformedBRep();
                if (shape == null)
                {
                    continue;
                }

                var triangulation = TriangulationHelper.GetTriangulation(shape, false);
                if (triangulation.TriangleCount == 0)
                {
                    continue;
                }

                triangulations.Add(triangulation);
                sumTriangleCount += triangulation.TriangleCount;
            }

            if (sumTriangleCount == 0)
            {
                return(false);
            }

            IStlWriter writer;

            if (binaryFormat)
            {
                writer = new StlBinaryWriter();
            }
            else
            {
                writer = new StlAsciiWriter();
            }
            writer.Init("Written by Macad3D STL-Export", sumTriangleCount);

            foreach (var triangulation in triangulations)
            {
                var index = 0;
                for (int triangle = 0; triangle < triangulation.TriangleCount; triangle++)
                {
                    // Get vertices
                    var vertex1 = triangulation.Vertices[triangulation.Indices[index]];
                    var vertex2 = triangulation.Vertices[triangulation.Indices[index + 1]];
                    var vertex3 = triangulation.Vertices[triangulation.Indices[index + 2]];
                    index += 3;

                    // Calculate normal of facet
                    var vec1   = new Vec(vertex1, vertex2);
                    var vec2   = new Vec(vertex1, vertex3);
                    var normal = vec1.Crossed(vec2);
                    if (normal.SquareMagnitude() > gp.Resolution)
                    {
                        normal.Normalize();
                    }
                    else
                    {
                        normal = default;
                    }

                    // Write out facet
                    writer.AddFacet(normal, vertex1, vertex2, vertex3);
                }
            }

            return(writer.WriteToFile(fileName));
        }
        public void ValueType_Vec()
        {
            var p1 = new Vec(1, 0, 0);
            var p2 = new Vec(0, 1, 0);

            Assert.IsFalse(p1.IsEqual(p2, 0.99, 0.1));
            Assert.IsTrue(p1.IsEqual(p2, 1.01, 0.1));
            Assert.IsTrue(p1.IsEqual(p2, 0.99, Math.PI / 2));
            Assert.IsTrue(p1.IsNormal(p2, 0.1));
            Assert.IsFalse(p1.IsOpposite(p2, 0.1));
            Assert.IsTrue(p1.IsOpposite(p2, Math.PI / 2));
            Assert.IsFalse(p1.IsParallel(p2, 0.1));
            Assert.IsTrue(p1.IsParallel(p2, Math.PI / 2));

            p1 = new Vec(1, 2, 3);
            p2 = new Vec(4, 5, 6);

            Assert.AreEqual(14, p1.SquareMagnitude());
            Assert.AreEqual(Math.Sqrt(14), p1.Magnitude());

            p2 = p1;
            p2.Add(new Vec(1, 2, 3));
            Assert.AreEqual(new Vec(2, 4, 6), p2);
            Assert.AreEqual(new Vec(2, 4, 6), p1.Added(new Vec(1, 2, 3)));

            p2 = new Vec(1, 2, 3);
            p2.Subtract(new Vec(3, 2, 1));
            Assert.AreEqual(new Vec(-2, 0, 2), p2);
            Assert.AreEqual(new Vec(-2, 0, 2), p1.Subtracted(new Vec(3, 2, 1)));

            p2 = new Vec(1, 2, 3);
            p2.Cross(new Vec(3, 2, 1));
            Assert.AreEqual(new Vec(-4, 8, -4), p2);
            Assert.AreEqual(new Vec(-4, 8, -4), p1.Crossed(new Vec(3, 2, 1)));

            Assert.AreEqual(Math.Sqrt(96), p1.CrossMagnitude(new Vec(3, 2, 1)));
            Assert.AreEqual(96, p1.CrossSquareMagnitude(new Vec(3, 2, 1)));

            p2 = new Vec(1, 2, 3);
            p2.CrossCross(new Vec(1, 2, 3), new Vec(4, 5, 6));
            Assert.AreEqual(new Vec(-24, -6, 12), p2);
            Assert.AreEqual(new Vec(-24, -6, 12), p1.CrossCrossed(new Vec(1, 2, 3), new Vec(4, 5, 6)));

            p2 = new Vec(1, 2, 3);
            p2.Divide(2);
            Assert.AreEqual(new Vec(0.5, 1, 1.5), p2);
            Assert.AreEqual(new Vec(0.5, 1, 1.5), p1.Divided(2));

            Assert.AreEqual(14, p1.Dot(new Vec(1, 2, 3)));
            Assert.AreEqual(0, p1.DotCross(new Vec(4, 5, 6), new Vec(4, 5, 6)));

            p2 = new Vec(1, 2, 3);
            p2.Multiply(2);
            Assert.AreEqual(new Vec(2, 4, 6), p2);
            Assert.AreEqual(new Vec(2, 4, 6), p1.Multiplied(2));

            p2 = new Vec(1, 2, 3);
            p2.Scale(2);
            Assert.AreEqual(new Vec(2, 4, 6), p2);
            Assert.AreEqual(new Vec(2, 4, 6), p1.Scaled(2));

            p2 = new Vec(1, 2, 3);
            p2.Normalize();
            Assert.IsTrue(p2.IsEqual(new Vec(0.26726, 0.53452, 0.80178), 0.00001, 0.00001));
            Assert.IsTrue(p1.Normalized().IsEqual(new Vec(0.26726, 0.53452, 0.80178), 0.00001, 0.00001));

            p2 = new Vec(1, 2, 3);
            p2.Reverse();
            Assert.AreEqual(new Vec(-1, -2, -3), p2);
            Assert.AreEqual(new Vec(-1, -2, -3), p1.Reversed());

            p2.SetLinearForm(new Vec(1, 2, 3), new Vec(4, 5, 6));
            Assert.AreEqual(new Vec(5, 7, 9), p2);
            p2.SetLinearForm(2, new Vec(1, 2, 3), new Vec(4, 5, 6));
            Assert.AreEqual(new Vec(6, 9, 12), p2);
            p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6));
            Assert.AreEqual(new Vec(14, 19, 24), p2);
            p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6), new Vec(7, 8, 9));
            Assert.AreEqual(new Vec(21, 27, 33), p2);
            p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6), 4, new Vec(7, 8, 9));
            Assert.AreEqual(new Vec(42, 51, 60), p2);
            p2.SetLinearForm(2, new Vec(1, 2, 3), 3, new Vec(4, 5, 6), 4, new Vec(7, 8, 9), new Vec(10, 11, 12));
            Assert.AreEqual(new Vec(52, 62, 72), p2);

            p2 = new Vec(2, 0, 0);
            p2.Mirror(new Vec(0, 1, 0));
            Assert.AreEqual(new Vec(-2, 0, 0), p2);
            Assert.AreEqual(new Vec(2, 0, 0), p2.Mirrored(new Vec(0, 1, 0)));

            var m2 = new Ax1(new Pnt(-1, 2, -3), new Dir(-1, 0, 0));

            p2 = new Vec(2, 1, 3);
            Assert.AreEqual(new Vec(2, -1, -3), p2.Mirrored(m2));
            p2.Mirror(m2);
            Assert.AreEqual(new Vec(2, -1, -3), p2);

            var a2 = new Ax2(new Pnt(-1, 2, -3), new Dir(-1, 0, 0));

            p2 = new Vec(2, 1, 3);
            Assert.AreEqual("-2,1,3", p2.Mirrored(a2).ToString());
            p2.Mirror(a2);
            Assert.AreEqual("-2,1,3", p2.ToString());

            p2 = new Vec(2, 1, 3);
            Assert.IsTrue(new Vec(2, 3, -1).IsEqual(p2.Rotated(m2, Math.PI / 2), 0.0001, 0.0001));
            p2.Rotate(m2, Math.PI / 2);
            Assert.IsTrue(new Vec(2, 3, -1).IsEqual(p2, 0.0001, 0.0001));

            //TestContext.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", gp2.x, gp2.y, gp2.z));

            Trsf t1 = new Trsf();

            t1.SetRotation(Ax1.OZ, Math.PI / 2);
            p2 = new Vec(4, 5, 6);
            Assert.AreEqual("-5,4,6", p2.Transformed(t1).ToString());
            p2.Transform(t1);
            Assert.AreEqual("-5,4,6", p2.ToString());
        }