예제 #1
0
        public AdnBoundingBox(List<AdnPoint> meshVertices)
        {
            _vertices = new List<AdnPoint>();

            AdnPoint min = new AdnPoint(
                double.PositiveInfinity,
                double.PositiveInfinity,
                double.PositiveInfinity);

            AdnPoint max =  new AdnPoint(
                double.NegativeInfinity,
                double.NegativeInfinity,
                double.NegativeInfinity);

            foreach (AdnPoint vertex in meshVertices)
            {
                if (vertex.X < min.X)
                    min.X = vertex.X;

                if (vertex.Y < min.Y)
                    min.Y = vertex.Y;

                if (vertex.Z < min.Z)
                    min.Z = vertex.Z;

                if (vertex.X > max.X)
                    max.X = vertex.X;

                if (vertex.Y > max.Y)
                    max.Y = vertex.Y;

                if (vertex.Z > max.Z)
                    max.Z = vertex.Z;
            }

            _vertices.Add(min);
            _vertices.Add(new AdnPoint(min.X, min.Y, max.Z));
            _vertices.Add(new AdnPoint(max.X, min.Y, max.Z));
            _vertices.Add(new AdnPoint(max.X, min.Y, min.Z));

            _vertices.Add(max);
            _vertices.Add(new AdnPoint(min.X, max.Y, max.Z));
            _vertices.Add(new AdnPoint(min.X, max.Y, min.Z));
            _vertices.Add(new AdnPoint(max.X, max.Y, min.Z));
        }
예제 #2
0
        public bool Intersects(
            AdnRay ray,
            ref AdnPoint point)
        {
            point = null;

            // Perform intersection test on bounding box
            // first for performances
            if (!_boundingBox.Intersects(ray))
                return false;

            double mindist = double.PositiveInfinity;

            for (int idx = 0; idx < _vertexBuffer.ElementCount; idx += 3)
            {
                AdnPoint p = null;

                if (ray.Intersect(
                    _vertices[idx],
                    _vertices[idx + 1],
                    _vertices[idx + 2],
                    out p))
                {
                    double dist = ray.Origin.SquaredDistanceTo(p);

                    if (dist < mindist)
                    {
                        mindist = dist;
                        point = p;
                    }
                }
            }

            return (point != null);
        }
예제 #3
0
        public bool Intersect(
            AdnPoint v1,
            AdnPoint v2,
            AdnPoint v3,
            out AdnPoint p)
        {
            p = null;

            double t = 0.0;
            double u = 0.0;
            double v = 0.0;

            AdnVector vert0 = v1.AsVector();
            AdnVector vert1 = v2.AsVector();
            AdnVector vert2 = v3.AsVector();

            // Find vectors for two edges sharing vert0.
            AdnVector edge1 = vert1.Substract(vert0);
            AdnVector edge2 = vert2.Substract(vert0);

            // Begin calculating determinant - also used to calculate U parameter.
            AdnVector pvec = Direction.CrossProduct(edge2);

            // If determinant is near zero, ray lies in plane of triangle.
            double det = edge1.DotProduct(pvec);

            if (det > -EPSILON && det < EPSILON)
                return false;

            double inv_det = 1.0 / det;

            // Calculate distance from vert0 to ray origin.
            AdnVector tvec = Origin.AsVector().Substract(vert0);

            // Calculate U parameter and test bounds.
            u = tvec.DotProduct(pvec) * inv_det;

            if (u < 0.0 || u > 1.0)
                return false;

            // Prepare to test V parameter.
            AdnVector qvec = tvec.CrossProduct(edge1);

            // Calculate V parameter and test bounds.
            v = Direction.DotProduct(qvec) * inv_det;

            if (v < 0.0 || u + v > 1.0)
                return false;

            // Calculate t, ray intersects triangle.
            t = edge2.DotProduct(qvec) * inv_det;

            AdnVector pos = Origin.AsVector().Add(Direction.Scale(t));

            p = new AdnPoint(pos.X, pos.Y, pos.Z);

            return true;
        }
예제 #4
0
        public bool Intersect(
            AdnPoint v1,
            AdnPoint v2,
            AdnPoint v3)
        {
            AdnPoint p = null;

            return Intersect(v1, v2, v3, out p);
        }
예제 #5
0
        public AdnRay(AdnPoint origin, AdnVector direction)
        {
            Origin = origin;

            Direction = direction;

            Direction.Normalize();
        }
예제 #6
0
        public AdnPoint Transform(AdnPoint point)
        {
            double[] m = ToMatrix();

            return new AdnPoint(
                m[0] * point.X + m[1] * point.Y + m[2] * point.Z,
                m[4] * point.X + m[5] * point.Y + m[6] * point.Z,
                m[8] * point.X + m[9] * point.Y + m[10] * point.Z);
        }
예제 #7
0
 public double SquaredDistanceTo(AdnPoint p)
 {
     return
         (X - p.X) * (X - p.X) +
         (Y - p.Y) * (Y - p.Y) +
         (Z - p.Z) * (Z - p.Z);
 }
예제 #8
0
 public void Add(AdnPoint point)
 {
     X += point.X;
     Y += point.Y;
     Z += point.Z;
 }