public override int classify(double[] input)
        {
            input = dataSet.norm(input);

            double[][] output = forwardStep(input);
            int        max    = VectorTools.maxIndex(output[output.Length - 1]);

            return(max);
        }
Example #2
0
        public override bool HitTest(Point p)
        {
            double dist = VectorTools.DistanceToLine(from.Position, to.Position, p);

            if (dist < thickness + 2)
            {
                return(true);
            }

            return(false);
        }
        public override bool HitTest(Point p)
        {
            double radius = Radius + 2;
            double dist   = VectorTools.Distance(position, p);

            if (dist < radius)
            {
                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Returns true if no CelestialBody occludes the target Vessel from this Vessel, false otherwise.
 /// </summary>
 /// <returns><c>true</c>, if this Vessel has line of sight to the target Vessel,
 /// <c>false</c> otherwise.</returns>
 /// <param name="vessel">this Vessel</param>
 /// <param name="targetVessel">target Vessel</param>
 /// <param name="firstOccludingBody">Set to the first body found to be blocking line of sight,
 /// if any, otherwise null.</param>
 /// <param name="sqrRatio">The square of the "grace" ratio to apply
 /// to the radius of potentially excluding bodies.</param>
 public static bool hasLineOfSightTo(
     this Vessel vessel,
     Vessel targetVessel,
     out CelestialBody firstOccludingBody,
     double sqrRatio = 1d
     )
 {
     return(VectorTools.IsLineOfSightBetween(
                vessel.GetWorldPos3D(),
                targetVessel.GetWorldPos3D(),
                out firstOccludingBody,
                sqrRatio
                ));
 }
Example #5
0
            private void ApplyTanBitanArray(Vector3[] tanArray, Vector3[] bitanArray)
            {
                for (int i = 0; i < vertices.Count; i++)
                {
                    Vertex  v        = vertices[i];
                    Vector3 newTan   = tanArray[i];
                    Vector3 newBitan = bitanArray[i];

                    // The tangent and bitangent should be orthogonal to the normal but not each other.
                    // Bitangents are not calculated with a cross product to prevent flipped shading with mirrored normal maps.
                    v.tan    = new Vector4(VectorTools.Orthogonalize(newTan, v.nrm), 1);
                    v.bitan  = new Vector4(VectorTools.Orthogonalize(newBitan, v.nrm), 1);
                    v.bitan *= -1;
                }
            }
Example #6
0
            public void SmoothNormals()
            {
                Vector3[] normals = new Vector3[vertices.Count];

                List <int> f = GetRenderingVertexIndices();

                for (int i = 0; i < displayFaceSize; i += 3)
                {
                    Vertex  v1  = vertices[f[i]];
                    Vertex  v2  = vertices[f[i + 1]];
                    Vertex  v3  = vertices[f[i + 2]];
                    Vector3 nrm = VectorTools.CalculateNormal(v1.pos, v2.pos, v3.pos);

                    normals[f[i + 0]] += nrm;
                    normals[f[i + 1]] += nrm;
                    normals[f[i + 2]] += nrm;
                }

                for (int i = 0; i < normals.Length; i++)
                {
                    vertices[i].nrm = normals[i].Normalized();
                }

                // Compare each vertex with all the remaining vertices. This might skip some.
                for (int i = 0; i < vertices.Count; i++)
                {
                    Vertex v = vertices[i];

                    for (int j = i + 1; j < vertices.Count; j++)
                    {
                        Vertex v2 = vertices[j];

                        if (v == v2)
                        {
                            continue;
                        }
                        float dis = (float)Math.Sqrt(Math.Pow(v.pos.X - v2.pos.X, 2) + Math.Pow(v.pos.Y - v2.pos.Y, 2) + Math.Pow(v.pos.Z - v2.pos.Z, 2));
                        if (dis <= 0f) // Extra smooth
                        {
                            Vector3 nn = ((v2.nrm + v.nrm) / 2).Normalized();
                            v.nrm  = nn;
                            v2.nrm = nn;
                        }
                    }
                }
            }
        protected virtual void UpdateBounces(TimeSpan duration)
        {
            Parallel.ForEach(quadTree.AllQuadrants(), quad =>
            {
                for (int i = 0; i < quad.Nodes.Count; i++)
                {
                    NodeBase nodeA = quad.Nodes[i];

                    // find bounces
                    for (int j = i + 1; j < quad.Nodes.Count; j++)
                    {
                        NodeBase nodeB = quad.Nodes[j];

                        double dist = VectorTools.Distance(nodeA.Position, nodeB.Position);
                        if (dist <= nodeA.Radius + nodeB.Radius)
                        {
                            CalcNodeCollision(nodeA, nodeB, bounceEfficiency);
                        }
                    }
                }
            });
        }
        private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            var reference = e.FrameReference;

            MultiSourceFrame multiSourceFrame = null;
            ColorFrame       colorFrame       = null;
            DepthFrame       depthFrame       = null;
            BodyFrame        bodyFrame        = null;
            BodyIndexFrame   bodyIndexFrame   = null;

            try
            {
                using (_frameCounter.Increment())
                {
                    multiSourceFrame = reference.AcquireFrame();
                    if (multiSourceFrame == null)
                    {
                        return;
                    }

                    colorFrame     = multiSourceFrame.ColorFrameReference.AcquireFrame();
                    depthFrame     = multiSourceFrame.DepthFrameReference.AcquireFrame();
                    bodyFrame      = multiSourceFrame.BodyFrameReference.AcquireFrame();
                    bodyIndexFrame = multiSourceFrame.BodyIndexFrameReference.AcquireFrame();
                    if (colorFrame == null | depthFrame == null | bodyFrame == null | bodyIndexFrame == null)
                    {
                        return;
                    }


                    var colorDesc   = colorFrame.FrameDescription;
                    int colorWidth  = colorDesc.Width;
                    int colorHeight = colorDesc.Height;
                    if (_colorFrameData == null)
                    {
                        int size = colorDesc.Width * colorDesc.Height;
                        _colorFrameData = new byte[size * bytesPerPixel];
                        _displayFrame   = new byte[size * bytesPerPixel];
                    }

                    var  depthDesc = depthFrame.FrameDescription;
                    uint depthSize = depthDesc.LengthInPixels;
                    _depthFrameData   = new ushort[depthSize];
                    _colorSpacePoints = new ColorSpacePoint[depthSize];

                    FrameDescription bodyIndexFrameDescription = bodyIndexFrame.FrameDescription;
                    int bodyIndexWidth  = bodyIndexFrameDescription.Width;
                    int bodyIndexHeight = bodyIndexFrameDescription.Height;
                    if ((bodyIndexWidth * bodyIndexHeight) == bodyIndexFrameData.Length)
                    {
                        bodyIndexFrame.CopyFrameDataToArray(bodyIndexFrameData);
                    }

                    Array.Clear(_displayFrame, 0, _displayFrame.Length);


                    colorFrame.CopyConvertedFrameDataToArray(_colorFrameData, ColorImageFormat.Bgra);
                    depthFrame.CopyFrameDataToArray(_depthFrameData);
                    kinectSensor.CoordinateMapper.MapDepthFrameToColorSpace(_depthFrameData, _colorSpacePoints);
                    kinectSensor.CoordinateMapper.MapDepthFrameToCameraSpace(_depthFrameData, _cameraPoints);

                    for (int depthIndex = 0; depthIndex < _depthFrameData.Length; ++depthIndex)
                    {
                        byte player = bodyIndexFrameData[depthIndex];
                        bool?c      = OnlyPlayersMenuItem.IsChecked;
                        bool val    = c != null ? (bool)c : false;
                        if (!val || player != 0xff)
                        {
                            ColorSpacePoint  point = _colorSpacePoints[depthIndex];
                            CameraSpacePoint p     = this._cameraPoints[depthIndex];

                            int colorX          = (int)Math.Floor(point.X + 0.5);
                            int colorY          = (int)Math.Floor(point.Y + 0.5);
                            int colorImageIndex = ((colorWidth * colorY) + colorX) * bytesPerPixel;

                            if ((colorX >= 0) && (colorX < colorWidth) && (colorY >= 0) && (colorY < colorHeight))
                            {
                                if (p.Z > 0)
                                {
                                    _displayFrame[colorImageIndex]     = _colorFrameData[colorImageIndex];     // b
                                    _displayFrame[colorImageIndex + 1] = _colorFrameData[colorImageIndex + 1]; // g
                                    _displayFrame[colorImageIndex + 2] = _colorFrameData[colorImageIndex + 2]; // r
                                    _displayFrame[colorImageIndex + 3] = _colorFrameData[colorImageIndex + 3]; // a
                                }
                            }
                        }
                    }

                    colorBitmap.WritePixels(
                        new Int32Rect(0, 0, colorDesc.Width, colorDesc.Height),
                        _displayFrame,
                        //_colorFrameData,
                        colorDesc.Width * bytesPerPixel,
                        0);

                    if (calibratingSurface)
                    {
                        if (_pointsToDepth.Count > 0)
                        {
                            foreach (Point p in _pointsToDepth)
                            {
                                int depthIndex = Convert.ToInt32(p.Y) * depthDesc.Width + Convert.ToInt32(p.X);
                                try
                                {
                                    CameraSpacePoint cameraPoint = _cameraPoints[depthIndex];
                                    if (!(Double.IsInfinity(cameraPoint.X)) && !(Double.IsInfinity(cameraPoint.Y)) && !(Double.IsInfinity(cameraPoint.Z) && cameraPoint.Z > 0))
                                    {
                                        Console.WriteLine("" + p.X + " " + p.Y + "  ---> " + cameraPoint.X + " " + cameraPoint.Y + " " + cameraPoint.Z);

                                        _calibrationPoints.Add(cameraPoint);
                                        drawEllipse(p.X, p.Y);
                                    }
                                }
                                catch { }
                            }
                            _pointsToDepth = new List <Point>();
                        }

                        if (false && _calibrationPoints.Count == 3)
                        {
                            canvas.Children.Clear();


                            CameraSpacePoint a  = VectorTools.subPoint(_calibrationPoints[0], _calibrationPoints[1]);
                            CameraSpacePoint b  = VectorTools.subPoint(_calibrationPoints[2], _calibrationPoints[1]);
                            CameraSpacePoint up = VectorTools.cross(a, b);
                            CameraSpacePoint c1 = VectorTools.cross(b, up);
                            CameraSpacePoint c2 = VectorTools.mult(c1, -1f);
                            CameraSpacePoint c;

                            if (VectorTools.distance(_calibrationPoints[2], VectorTools.addPoint(_calibrationPoints[1], c1)) < VectorTools.distance(_calibrationPoints[2], VectorTools.addPoint(_calibrationPoints[1], c2)))
                            {
                                c = VectorTools.mult(VectorTools.normalize(c1), 9.0f / 16.0f * VectorTools.norm(a) /*norm(b)*/);
                            }
                            else
                            {
                                c = VectorTools.mult(VectorTools.normalize(c2), 9.0f / 16.0f * VectorTools.norm(a) /*norm(b)*/);
                            }


                            CameraSpacePoint BL = _calibrationPoints[0];
                            CameraSpacePoint BR = _calibrationPoints[1];
                            CameraSpacePoint TR = VectorTools.addPoint(BR, c);
                            CameraSpacePoint TL = VectorTools.addPoint(BL, c);

                            VectorTools.DebugPoint(BL);
                            VectorTools.DebugPoint(BR);
                            VectorTools.DebugPoint(TR);
                            VectorTools.DebugPoint(TL);

                            //_drawSurface(coordinateMapper.MapCameraPointToColorSpace(BL),
                            //    coordinateMapper.MapCameraPointToColorSpace(BR),
                            //    coordinateMapper.MapCameraPointToColorSpace(TR),
                            //    coordinateMapper.MapCameraPointToColorSpace(TL));

                            _calibrationPoints.Clear();
                            calibratingSurface = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.Source);
            }
            finally
            {
                if (colorFrame != null)
                {
                    colorFrame.Dispose();
                }
                if (depthFrame != null)
                {
                    depthFrame.Dispose();
                }
                if (bodyFrame != null)
                {
                    bodyFrame.Dispose();
                }
                if (bodyIndexFrame != null)
                {
                    bodyIndexFrame.Dispose();
                }
            }
        }
Example #9
0
                private static PartitionParam DrawAtMultiFoldOutline(PartitionParam param, double targetArea, List <Point3d> outlineVertex)
                {
                    Line           mainLine          = GetMainLine(param, outlineVertex);
                    List <Point3d> canMakePerpVertex = new List <Point3d>();

                    Vector3d mainAlign = mainLine.UnitTangent;
                    Vector3d mainPerp  = VectorTools.RotateVectorXY(mainAlign, Math.PI / 2);
                    Point3d  origin    = param.OriginPost.Point;
                    bool     isMainAlignSameAsPostNormal = mainAlign.IsParallelTo(param.OriginPost.BaseLine.UnitNormal, 0.005) == 1;

                    if (!isMainAlignSameAsPostNormal)
                    {
                        int originIndex = param.OutlineLabel.Core.FindIndex
                                              (i => i.PureLine == param.OriginPost.BasePureLine);

                        param.OriginPost = new PartitionOrigin(origin, param.OutlineLabel.Core[originIndex - 1]);
                        mainPerp         = VectorTools.RotateVectorXY(mainAlign, -Math.PI / 2);
                    }

                    int lastVertexIndex = outlineVertex.Count - 1;

                    for (int i = 1; i < lastVertexIndex; i++)
                    {
                        Vector3d toPreVector  = new Vector3d(outlineVertex[i - 1] - outlineVertex[i]);
                        Vector3d toPostVector = new Vector3d(outlineVertex[i + 1] - outlineVertex[i]);
                        Vector3d toMainVector = -mainPerp;

                        if (IsBetweenVector(toPreVector, toPostVector, toMainVector))
                        {
                            canMakePerpVertex.Add(outlineVertex[i]);
                        }
                    }

                    //SeivePerpVertex
                    List <Point3d> finalVertex = new List <Point3d>();


                    foreach (Point3d i in outlineVertex)
                    {
                        Line toBaseLine = PCXTools.PCXByEquationStrict(i, CurveTools.ToPolyline(mainLine.ToNurbsCurve()), -mainPerp);
                        Line toOutline  = PCXTools.PCXByEquationStrict(toBaseLine.PointAt(1), param.OutlineLabel.Pure, mainPerp);

                        if (toOutline.PointAt(1).DistanceTo(i) < 0.5)
                        {
                            finalVertex.Add(i);
                        }
                    }


                    //DrawAtEachVertex
                    List <PartitionParam> outputCandidate = new List <PartitionParam>();

                    foreach (Point3d i in finalVertex)
                    {
                        Line    toBaseLine = PCXTools.PCXByEquationStrict(i, CurveTools.ToPolyline(mainLine.ToNurbsCurve()), -mainPerp);
                        Point3d tempAnchor = toBaseLine.PointAt(1);
                        Line    toOutline  = PCXTools.PCXByEquationStrict(tempAnchor, param.OutlineLabel.Pure, mainPerp);

                        if (toOutline.PointAt(1).DistanceTo(i) > 0.5)
                        {
                            continue;
                        }

                        List <RoomLine> tempPartition = new List <RoomLine>();
                        tempPartition.Add(new RoomLine(new Line(origin, tempAnchor), LineType.Inner));
                        tempPartition.Add(new RoomLine(new Line(tempAnchor, i), LineType.Inner));

                        PartitionParam tempParam = new PartitionParam(param);
                        tempParam.PartitionPost = new Partition(tempPartition, param.OriginPost);

                        outputCandidate.Add(tempParam);
                    }

                    outputCandidate.Add(PartitionMaker.DrawOrtho(param));

                    //TestCandidate
                    //나중에 수정.. 지금은 면적일치정도로만..
                    Plane                 cornerPlane = new Plane(origin, mainAlign, mainPerp);
                    CornerComparer        comparer    = new CornerComparer();
                    List <PartitionParam> seived      = comparer.Seive(outputCandidate, targetArea, cornerPlane);

                    return(seived[0]);
                }
Example #10
0
                //중복코드 나중에 지워보자..
                private static PartitionParam DrawAtNoFoldOutline(PartitionParam param, double targetArea, List <Point3d> outlineVertex)
                {
                    Line     mainLine  = GetMainLine(param, outlineVertex);
                    Vector3d mainAlign = mainLine.UnitTangent;
                    Vector3d mainPerp  = VectorTools.RotateVectorXY(mainAlign, Math.PI / 2);
                    Point3d  origin    = param.OriginPost.Point;

                    double dotProduct   = Vector3d.Multiply(mainAlign, param.OriginPost.BaseLine.UnitNormal);
                    double dotTolerance = 0.005;

                    bool isMainDirecPreDiv = false;

                    if (Math.Abs(dotProduct) < dotTolerance)
                    {
                        int originIndex = param.OutlineLabel.Core.FindIndex
                                              (i => i.PureLine == param.OriginPost.BasePureLine);

                        param.OriginPost  = new PartitionOrigin(origin, param.OutlineLabel.Core[originIndex - 1]);
                        mainPerp          = VectorTools.RotateVectorXY(mainAlign, -Math.PI / 2);
                        isMainDirecPreDiv = true;
                    }

                    int iterNum = 10;
                    int breaker = 0;

                    double   lowerBound = 0;
                    double   upperBound = mainLine.Length;
                    Polyline polyOutput = new Polyline();

                    while (lowerBound < upperBound)
                    {
                        if (breaker > iterNum)
                        {
                            break;
                        }

                        double tempStatus = (upperBound - lowerBound) / 2 + lowerBound;

                        Point3d tempAnchor = origin + mainAlign * tempStatus;
                        if (isMainDirecPreDiv)
                        {
                            tempAnchor = origin + mainAlign * (mainLine.Length - tempStatus);
                        }

                        List <RoomLine> cornerPartitions = new List <RoomLine>();
                        cornerPartitions.Add(new RoomLine(new Line(origin, tempAnchor), LineType.Inner));
                        Line anchorToOutline = PCXTools.PCXByEquation(tempAnchor, param.OutlineLabel.Pure, mainPerp);
                        cornerPartitions.Add(new RoomLine(anchorToOutline, LineType.Inner));

                        Partition tempPartition = new Partition(cornerPartitions, param.OriginPost);
                        param.PartitionPost = tempPartition;

                        double tempArea = PolylineTools.GetArea(param.Outline);

                        if (targetArea > tempArea)
                        {
                            lowerBound = tempStatus;
                        }
                        else if (targetArea < tempArea)
                        {
                            upperBound = tempStatus;
                        }
                        else
                        {
                            lowerBound = tempArea;
                            upperBound = tempArea;
                        }

                        breaker++;
                    }

                    return(param);
                }
        private void UpdateDirection()
        {
            // calculate light vector from 3 rotation angles
            Matrix4 lightRotMatrix = Matrix4.CreateFromAxisAngle(Vector3.UnitX, (float)VectorTools.GetRadians(RotationXDegrees))
                                     * Matrix4.CreateFromAxisAngle(Vector3.UnitY, RotationYDegrees * (float)VectorTools.GetRadians(RotationYDegrees))
                                     * Matrix4.CreateFromAxisAngle(Vector3.UnitZ, RotationZDegrees * (float)VectorTools.GetRadians(RotationZDegrees));

            direction = Vector3.TransformVector(new Vector3(0f, 0f, 1f), lightRotMatrix).Normalized();
        }