コード例 #1
0
ファイル: VrGame.cs プロジェクト: lanicon/SparrowHawk
        protected override void OnKeyPress(OpenTK.KeyPressEventArgs e)
        {
            if (e.KeyChar == 'C' || e.KeyChar == 'c')
            {
                ((VrRenderer)mRenderer).ovrvision_controller.getMatrixHeadtoCamera(0);
                mScene.popInteraction();
                mScene.pushInteraction(new Interaction.CalibrationAR(ref mScene, ref mRenderer.ovrvision_controller));
            }

            if (e.KeyChar == 'D' || e.KeyChar == 'd')
            {
                mRenderer.ovrvision_controller.setDefaultMatrixHC();
            }


            if (e.KeyChar == 'J' || e.KeyChar == 'j')
            {
                //mScene.popInteraction();
                //mScene.pushInteraction(new Interaction.EditPlane(ref mScene, ref xyPlane, ref xzPlane, ref yzPlane));
                //mScene.pushInteraction(new Interaction.RotatePlane(ref mScene, ref xyPlane2, ref xzPlane2, ref yzPlane2));

                //clear the stroke
                foreach (SceneNode sn in mScene.tableGeometry.children)
                {
                    if (sn.name == "PrintStroke")
                    {
                        mScene.tableGeometry.children.Remove(sn);
                        break;
                    }
                }
            }

            if (e.KeyChar == 'M' || e.KeyChar == 'm')
            {
                mScene.popInteraction();
                mScene.pushInteraction(new Interaction.Stroke(ref mScene));
            }

            if (e.KeyChar == 'V' || e.KeyChar == 'v')
            {
                calibrationVRtoRobot();
            }

            if (e.KeyChar == 'P' || e.KeyChar == 'p')
            {
                /*
                 * mScene.popInteraction();
                 * controllerP = new List<Vector3>();
                 * mScene.pushInteraction(new Interaction.PickPoint(ref mScene, ref controllerP));
                 */
                Rhino.DocObjects.ObjectEnumeratorSettings settings = new Rhino.DocObjects.ObjectEnumeratorSettings();
                settings.ObjectTypeFilter = Rhino.DocObjects.ObjectType.Brep;
                Brep teapotBody   = new Brep();
                Brep teapotSprout = new Brep();
                Brep teapotHandle = new Brep();
                foreach (Rhino.DocObjects.RhinoObject rhObj in mScene.rhinoDoc.Objects.GetObjectList(settings))
                {
                    if (rhObj.Attributes.Name == "aprinteGZx3Sz91Ag")
                    {
                        teapotBody = ((Brep)rhObj.Geometry).DuplicateBrep();
                        mScene.rhinoDoc.Objects.Delete(rhObj.Id, true);
                    }
                    else if (rhObj.Attributes.Name == "aprintS8FyJC391Ag")
                    {
                        teapotSprout = ((Brep)rhObj.Geometry).DuplicateBrep();
                        mScene.rhinoDoc.Objects.Delete(rhObj.Id, true);
                    }
                    else if (rhObj.Attributes.Name == "aprintVBE2eS391Ag")
                    {
                        teapotHandle = ((Brep)rhObj.Geometry).DuplicateBrep();
                        mScene.rhinoDoc.Objects.Delete(rhObj.Id, true);
                    }
                }

                Material.Material mesh_m    = new Material.LambertianMaterial(.7f, .7f, .7f, .3f);
                string            modelName = "aprint";
                SceneNode         teapotBodySN;
                UtilOld.addRhinoObjectSceneNode(ref mScene, ref teapotBody, ref mesh_m, modelName, out teapotBodySN);
                SceneNode teapotSproutSN;
                UtilOld.addRhinoObjectSceneNode(ref mScene, ref teapotSprout, ref mesh_m, modelName, out teapotSproutSN);
                SceneNode teapotHandleSN;
                UtilOld.addRhinoObjectSceneNode(ref mScene, ref teapotHandle, ref mesh_m, modelName, out teapotHandleSN);
            }

            if (e.KeyChar == 'A' || e.KeyChar == 'a')
            {
                //mScene.popInteraction();
                //mScene.pushInteraction(new Interaction.Align(ref mScene));

                //for rhino object
                OpenTK.Matrix4 currentRotation = mScene.platformRotation;

                float theta = (float)(90.0f / 360f * 2 * Math.PI);
                Rhino.RhinoApp.WriteLine("Theta = " + theta);
                Matrix4.CreateRotationZ(theta, out mScene.platformRotation);
                mScene.platformRotation.Transpose();

                //rotate Rhino objects
                OpenTK.Matrix4 rotMRhino = mScene.platformRotation * currentRotation.Inverted();
                Rhino.DocObjects.ObjectEnumeratorSettings settings = new Rhino.DocObjects.ObjectEnumeratorSettings();
                mScene.transM = new Transform();
                for (int row = 0; row < 4; row++)
                {
                    for (int col = 0; col < 4; col++)
                    {
                        mScene.transM[row, col] = rotMRhino[row, col];
                    }
                }
                settings.ObjectTypeFilter = Rhino.DocObjects.ObjectType.Brep;
                foreach (Rhino.DocObjects.RhinoObject rhObj in mScene.rhinoDoc.Objects.GetObjectList(settings))
                {
                    SceneNode sn;
                    if (mScene.BiDictionaryRhinoVR.TryGetByFirst(rhObj.Id, out sn) && !rhObj.Attributes.Name.Contains("planeXY") && !rhObj.Attributes.Name.Contains("planeXZ") &&
                        !rhObj.Attributes.Name.Contains("planeYZ"))
                    {
                        //SceneNode sn = mScene.brepToSceneNodeDic[rhObj.Id];
                        //mScene.brepToSceneNodeDic.Remove(rhObj.Id);
                        Guid newGuid = mScene.rhinoDoc.Objects.Transform(rhObj.Id, mScene.transM, true);
                        Rhino.RhinoApp.WriteLine("transM " + mScene.transM.ToString());
                        mScene.rhinoDoc.Views.Redraw();

                        //mScene.brepToSceneNodeDic.Add(newGuid, sn);
                        //mScene.SceneNodeToBrepDic[sn.guid] = mScene.rhinoDoc.Objects.Find(newGuid);
                    }
                }
            }

            if (e.KeyChar == 'O' || e.KeyChar == 'o')
            {
                mScene.popInteraction();
                UtilOld.clearAllModel(ref mScene);
            }

            if (e.KeyChar == 'Q' || e.KeyChar == 'q')
            {
                //print out debug info
                foreach (var item in mScene.selectionDic)
                {
                    Rhino.RhinoApp.WriteLine(item.Key.ToString() + ": " + item.Value.ToString());
                }
            }

            if (e.KeyChar == 'Z' || e.KeyChar == 'z')
            {
                while (!mScene.interactionStackEmpty())
                {
                    mScene.popInteraction();
                }
            }

            if (e.KeyChar == 'X' || e.KeyChar == 'x')
            {
                mScene.popInteraction();
                mScene.pushInteraction(new Interaction.CreatePatch(ref mScene));
            }

            if (e.KeyChar == '[' || e.KeyChar == '{')
            {
                mScene.popInteraction();
                mScene.pushInteraction(new Interaction.PickPoint(ref mScene, ref mLeftControllerPoses));
                current_i = null;
            }

            if (e.KeyChar == ']' || e.KeyChar == '}')
            {
                mScene.popInteraction();
                mScene.pushInteraction(new Interaction.PickPoint(ref mScene, ref mRightControllerPoses));
                current_i = null;
            }
        }
コード例 #2
0
ファイル: VrGame.cs プロジェクト: lanicon/SparrowHawk
        protected void handleSignals()
        {
            SparrowHawkSignal s = SparrowHawkEventListeners.Instance.getOneSignal();

            if (s == null)
            {
                return;
            }
            switch (s.type)
            {
            case SparrowHawkSignal.ESparrowHawkSigalType.InitType:
                if (s.data.Length >= 3)
                {
                    //To compute VRtoRobot matrix, press V key 8 times.
                    //robotPoint is in mm unit so the calibration result already deal with scale issue
                    Vector3 robotPoint = new Vector3(s.data[0], s.data[1], s.data[2]);
                    robotCallibrationPoints.Add(robotPoint);
                    Rhino.RhinoApp.WriteLine("add robotPoint: " + robotPoint.ToString());
                    if (mIsLefty && mScene.leftControllerIdx < 0 || !mIsLefty && mScene.leftControllerIdx < 0)
                    {
                        break;
                    }
                }
                break;

            case SparrowHawkSignal.ESparrowHawkSigalType.LineType:
                if (s.data.Length >= 4)
                {
                    if (s.data[0] == 0)
                    {
                        OpenTK.Vector3 p1 = new Vector3(s.data[1], s.data[2], s.data[3]);
                        p1 = UtilOld.platformToVRPoint(ref mScene, p1);
                        OpenTK.Vector3 p2 = new Vector3(s.data[4], s.data[5], s.data[6]);
                        p2 = UtilOld.platformToVRPoint(ref mScene, p2);

                        if (((Geometry.RobotPrintStroke)printStroke).mPoints.Count == 0)
                        {
                            printStroke = new Geometry.RobotPrintStroke(ref mScene);
                            ((Geometry.RobotPrintStroke)printStroke).addEdge(p1, p2);
                            printStrokeSN = new SceneNode("PrintStroke", ref printStroke, ref printStroke_m);
                            mScene.tableGeometry.add(ref printStrokeSN);
                        }
                        else
                        {
                            ((Geometry.RobotPrintStroke)printStroke).addEdge(p1, p2);
                            printStrokeSN.geometry = printStroke;
                        }
                    }
                    else
                    {
                        OpenTK.Vector3 p1 = new Vector3(s.data[1], s.data[2], s.data[3]);
                        p1 = UtilOld.platformToVRPoint(ref mScene, p1);
                        //((Geometry.GeometryStroke2)printStroke).addPoint(p1);
                        ((Geometry.RobotPrintStroke)printStroke).addEdge(((Geometry.RobotPrintStroke)printStroke).mPoints[((Geometry.RobotPrintStroke)printStroke).mPoints.Count - 1], p1);
                        printStrokeSN.geometry = printStroke;
                    }
                }
                break;

            case SparrowHawkSignal.ESparrowHawkSigalType.CutType:
                string guidStr = s.strData;
                Guid   delId   = new Guid(guidStr);
                UtilOld.removeRhinoObjectSceneNode(ref mScene, delId);
                //removePrintStroke and show the model again
                ((Geometry.RobotPrintStroke)printStroke).removePoint();
                //mScene.tableGeometry.remove(ref printStrokeSN); //already remove
                Rhino.DocObjects.ObjectEnumeratorSettings settings = new Rhino.DocObjects.ObjectEnumeratorSettings();
                settings.ObjectTypeFilter = Rhino.DocObjects.ObjectType.Brep;
                foreach (Rhino.DocObjects.RhinoObject rhObj in mScene.rhinoDoc.Objects.GetObjectList(settings))
                {
                    if (rhObj.Attributes.Name.Contains("aprint"))
                    {
                        SceneNode sn = mScene.BiDictionaryRhinoVR.GetByFirst(rhObj.Id);
                        Material.LambertianMaterial show_m = new Material.LambertianMaterial(((Material.LambertianMaterial)sn.material).mColor.R, ((Material.LambertianMaterial)sn.material).mColor.G, ((Material.LambertianMaterial)sn.material).mColor.B, .3f);
                        sn.material = show_m;
                    }
                }

                mScene.rhinoDoc.Views.Redraw();
                break;

            case SparrowHawkSignal.ESparrowHawkSigalType.EncoderType:

                //for rhino object
                OpenTK.Matrix4 currentRotation = mScene.platformRotation;
                float          theta           = (float)(s.data[0] / 360f * 2 * Math.PI);
                mScene.rhinoTheta = theta;
                Rhino.RhinoApp.WriteLine("Theta = " + theta);
                Matrix4.CreateRotationZ(-theta, out mScene.platformRotation);
                mScene.platformRotation.Transpose();

                //rotate Rhino objects
                OpenTK.Matrix4 rotMRhino = mScene.platformRotation * currentRotation.Inverted();
                mScene.transM = new Transform();
                for (int row = 0; row < 4; row++)
                {
                    for (int col = 0; col < 4; col++)
                    {
                        mScene.transM[row, col] = rotMRhino[row, col];
                    }
                }

                /*
                 * Rhino.DocObjects.ObjectEnumeratorSettings settings = new Rhino.DocObjects.ObjectEnumeratorSettings();
                 * settings.ObjectTypeFilter = Rhino.DocObjects.ObjectType.Brep;
                 * foreach (Rhino.DocObjects.RhinoObject rhObj in mScene.rhinoDoc.Objects.GetObjectList(settings))
                 * {
                 *  if (mScene.brepToSceneNodeDic.ContainsKey(rhObj.Id) && !rhObj.Attributes.Name.Contains("planeXY") && !rhObj.Attributes.Name.Contains("planeXZ")
                 *                                                          && !rhObj.Attributes.Name.Contains("planeYZ"))
                 *  {
                 *      //SceneNode sn = mScene.brepToSceneNodeDic[rhObj.Id];
                 *      //mScene.brepToSceneNodeDic.Remove(rhObj.Id);
                 *
                 *      Guid newGuid = mScene.rhinoDoc.Objects.Transform(rhObj.Id, mScene.transM, true);
                 *      Rhino.RhinoApp.WriteLine("transM " + mScene.transM.ToString());
                 *      mScene.rhinoDoc.Views.Redraw();
                 *
                 *      //mScene.brepToSceneNodeDic.Add(newGuid, sn);
                 *      //mScene.SceneNodeToBrepDic[sn.guid] = mScene.rhinoDoc.Objects.Find(newGuid);
                 *  }
                 *
                 * }*/

                //rotate the current interaction curve as well

                /*
                 * foreach(Curve iCurve in mScene.iCurveList)
                 * {
                 *  iCurve.Transform(transM);
                 *
                 * }
                 * if (mScene.peekInteraction().GetType() == typeof(Interaction.EditPoint2))
                 * {
                 *  mScene.peekInteraction().init();
                 * }*/

                break;
            }
        }