protected void notifyRobotIfSafe() { //Vector3 tableOrigin = Util.transformPoint(mScene.vrToRobot.Inverted(), new Vector3(0, 0, 0)); Vector3 tableOrigin = UtilOld.platformToVRPoint(ref mScene, new Vector3(0, 0, 0)); Vector3 headOrigin = UtilOld.transformPoint(mScene.mHMDPose.Inverted(), new Vector3(0, 0, 0)); Vector3 displacement = tableOrigin - headOrigin; displacement.Y = 0; if (displacement.Length > 1.0f && isUserIn == true) { Rhino.DocObjects.RhinoObject rhobj = mScene.rhinoDoc.Objects.Find(uGuid); rhobj.Attributes.Name = "user:out"; rhobj.CommitChanges(); isUserIn = false; Rhino.RhinoApp.WriteLine("User out. " + displacement.Length); } else if (displacement.Length < 1.0f && isUserIn == false) { Rhino.DocObjects.RhinoObject rhobj = mScene.rhinoDoc.Objects.Find(uGuid); rhobj.Attributes.Name = "user:in"; rhobj.CommitChanges(); isUserIn = true; Rhino.RhinoApp.WriteLine("User in. " + displacement.Length); } }
private void initMeshGeometry(ref Mesh triMesh) { //get faces from mesh List <MeshFace> faces = new List <MeshFace>(); List <int> indices_array = new List <int>(); foreach (MeshFace face in triMesh.Faces) { faces.Add(face); //TODO-loop throgn the original mesh to optimize if (face.IsQuad) { Rhino.RhinoApp.WriteLine("Triangulate error."); } else { indices_array.Add(face.A); indices_array.Add(face.B); indices_array.Add(face.C); } } mGeometryIndices = indices_array.ToArray(); //get vertices from mesh // rhino coordinate system is different from OpenGL List <Point3d> vertices = new List <Point3d>(); List <float> vertices_array = new List <float>(); OpenTK.Matrix4 transToOrigin = new OpenTK.Matrix4(); foreach (Point3d vertex in triMesh.Vertices) { vertices.Add(vertex); OpenTK.Vector3 rp = new OpenTK.Vector3((float)vertex.X, (float)vertex.Y, (float)vertex.Z); /* * if(transM != OpenTK.Matrix4.Identity) * { * rp = Util.transformPoint(transM, rp); * * }*/ //seperate the data and visualization OpenTK.Vector3 p = UtilOld.platformToVRPoint(ref mScene, rp); vertices_array.Add(p.X); vertices_array.Add(p.Y); vertices_array.Add(p.Z); } mGeometry = vertices_array.ToArray(); mNumPrimitives = mGeometryIndices.Length / 3; primitiveType = OpenTK.Graphics.OpenGL4.BeginMode.Triangles; }
protected void setupScene() { mScene = new Scene(ref mDoc, ref mHMD); mScene.setWindowSize(this.Width, this.Height); mScene.mIsLefty = mIsLefty; if (mStrDriver.Contains("oculus")) { mScene.isOculus = true; } else { mScene.isOculus = false; } if (mHMD != null) { mHMD.GetRecommendedRenderTargetSize(ref mRenderWidth, ref mRenderHeight); } //visualizing axises OpenTK.Vector3 x0 = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3(-240, 0, 0)); OpenTK.Vector3 x1 = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3(240, 0, 0)); OpenTK.Vector3 y0 = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3(0, -240, 0)); OpenTK.Vector3 y1 = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3(0, 240, 0)); OpenTK.Vector3 z0 = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3(0, 0, -240)); OpenTK.Vector3 z1 = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3(0, 0, 240)); Geometry.Geometry xAxis_g = new Geometry.GeometryStroke(ref mScene); Material.Material xAxis_m = new Material.SingleColorMaterial(1, 1, 1, 0); ((Geometry.GeometryStroke)xAxis_g).addPoint(x0); ((Geometry.GeometryStroke)xAxis_g).addPoint(x1); mScene.xAxis = new SceneNode("xAxis", ref xAxis_g, ref xAxis_m); mScene.staticGeometry.add(ref mScene.xAxis); Geometry.Geometry yAxis_g = new Geometry.GeometryStroke(ref mScene); Material.Material yAxis_m = new Material.SingleColorMaterial(1, 1, 1, 0); ((Geometry.GeometryStroke)yAxis_g).addPoint(y0); ((Geometry.GeometryStroke)yAxis_g).addPoint(y1); mScene.yAxis = new SceneNode("yAxis", ref yAxis_g, ref yAxis_m); mScene.staticGeometry.add(ref mScene.yAxis); Geometry.Geometry zAxis_g = new Geometry.GeometryStroke(ref mScene); Material.Material zAxis_m = new Material.SingleColorMaterial(1, 1, 1, 0); ((Geometry.GeometryStroke)zAxis_g).addPoint(z0); ((Geometry.GeometryStroke)zAxis_g).addPoint(z1); mScene.zAxis = new SceneNode("zAxis", ref zAxis_g, ref zAxis_m); mScene.staticGeometry.add(ref mScene.zAxis); // LeftController Point and Laser //g = new Geometry.Geometry("C:/workspace/Kestrel/resources/meshes/bunny.obj"); //m = new Material.RGBNormalMaterial(1); Geometry.Geometry controllerL_g = new Geometry.PointMarker(new Vector3(0, 0, 0)); Material.Material controllerL_m = new Material.SingleColorMaterial(1, 0, 0, 1); SceneNode controllerL_p = new SceneNode("Left Cursor", ref controllerL_g, ref controllerL_m); mScene.leftControllerNode.add(ref controllerL_p); controllerL_p.transform = new Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);//mScene.mLeftControllerOffset; Geometry.Geometry controllerR_g = new Geometry.PointMarker(new Vector3(0, 0, 0)); Material.Material controllerR_m = new Material.SingleColorMaterial(1, 0, 0, 1); SceneNode controllerR_p = new SceneNode("Right Cursor", ref controllerR_g, ref controllerR_m); mScene.rightControllerNode.add(ref controllerR_p); controllerR_p.transform = new Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); Geometry.Geometry controllerLRay_g = new Geometry.GeometryStroke(ref mScene); Material.Material controllerLRay_m = new Material.SingleColorMaterial(1, 0, 0, 1); ((Geometry.GeometryStroke)controllerLRay_g).addPoint(new Vector3(0, 0, 0)); ((Geometry.GeometryStroke)controllerLRay_g).addPoint(new Vector3(0, 0, -1)); SceneNode rayTraceL = new SceneNode("ControllerRay", ref controllerLRay_g, ref controllerLRay_m); if (mIsLefty) { mScene.leftControllerNode.add(ref rayTraceL); } else { mScene.rightControllerNode.add(ref rayTraceL); } rayTraceL.transform = new Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);//mScene.mLeftControllerOffset; UtilOld.showLaser(ref mScene, false); mScene.xzPlane = new DesignPlane(ref mScene, XYZPlanes.XZ); mScene.xyPlane = new DesignPlane(ref mScene, XYZPlanes.XY); mScene.yzPlane = new DesignPlane(ref mScene, XYZPlanes.YZ); }
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; } }
public override void draw(bool isTop) { //visualize the point on the plane if (onPlane && isTop) { //ray casting to the pre-defind planes OpenTK.Vector4 controller_p = UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx) * new OpenTK.Vector4(0, 0, 0, 1); OpenTK.Vector4 controller_pZ = UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx) * new OpenTK.Vector4(0, 0, -1, 1); Point3d controller_pRhino = UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_p.X, controller_p.Y, controller_p.Z))); Point3d controller_pZRhin = UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_pZ.X, controller_pZ.Y, controller_pZ.Z))); Rhino.Geometry.Vector3d direction = new Rhino.Geometry.Vector3d(controller_pZRhin.X - controller_pRhino.X, controller_pZRhin.Y - controller_pRhino.Y, controller_pZRhin.Z - controller_pRhino.Z); Ray3d ray = new Ray3d(controller_pRhino, direction); Rhino.DocObjects.ObjectEnumeratorSettings settings = new Rhino.DocObjects.ObjectEnumeratorSettings(); settings.ObjectTypeFilter = Rhino.DocObjects.ObjectType.Brep; //settings.NameFilter = "plane"; float mimD = 1000000f; hitPlane = false; if (!lockPlane) { foreach (Rhino.DocObjects.RhinoObject rhObj in mScene.rhinoDoc.Objects.GetObjectList(settings)) { //only drawing on planes for now rhObj.Attributes.Name.Contains("brepMesh") || rhObj.Attributes.Name.Contains("aprint") || rhObj.Attributes.Name.Contains("plane") if (rhObj.Attributes.Name.Contains("plane")) { List <GeometryBase> geometries = new List <GeometryBase>(); geometries.Add(rhObj.Geometry); //must be a brep or surface, not mesh Point3d[] rayIntersections = Rhino.Geometry.Intersect.Intersection.RayShoot(ray, geometries, 1); if (rayIntersections != null) { //get the nearest one OpenTK.Vector3 tmpP = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3((float)rayIntersections[0].X, (float)rayIntersections[0].Y, (float)rayIntersections[0].Z)); float distance = (float)Math.Sqrt(Math.Pow(tmpP.X - controller_p.X, 2) + Math.Pow(tmpP.Y - controller_p.Y, 2) + Math.Pow(tmpP.Z - controller_p.Z, 2)); if (distance < mimD) { hitPlane = true; targetPRhObj = rhObj; mimD = distance; projectP = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3((float)rayIntersections[0].X, (float)rayIntersections[0].Y, (float)rayIntersections[0].Z)); } } } } } else { if (targetPRhObj != null) { List <GeometryBase> geometries = new List <GeometryBase>(); geometries.Add(targetPRhObj.Geometry); //must be a brep or surface, not mesh Point3d[] rayIntersections = Rhino.Geometry.Intersect.Intersection.RayShoot(ray, geometries, 1); if (rayIntersections != null) { //get the nearest one OpenTK.Vector3 tmpP = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3((float)rayIntersections[0].X, (float)rayIntersections[0].Y, (float)rayIntersections[0].Z)); float distance = (float)Math.Sqrt(Math.Pow(tmpP.X - controller_p.X, 2) + Math.Pow(tmpP.Y - controller_p.Y, 2) + Math.Pow(tmpP.Z - controller_p.Z, 2)); if (distance < mimD) { hitPlane = true; mimD = distance; projectP = UtilOld.platformToVRPoint(ref mScene, new OpenTK.Vector3((float)rayIntersections[0].X, (float)rayIntersections[0].Y, (float)rayIntersections[0].Z)); } } } } if (!hitPlane) { targetPRhObj = null; projectP = new OpenTK.Vector3(100, 100, 100); //make it invisable } //visualize the projection points // inverted rotation first OpenTK.Matrix4 t = OpenTK.Matrix4.CreateTranslation(UtilOld.transformPoint(mScene.tableGeometry.transform.Inverted(), projectP)); t.Transpose(); drawPoint.transform = t; } if (currentState != State.PAINT || !isTop) { return; } // drawing curve Vector3 pos = new Vector3(); if (onPlane) { pos = projectP; if (hitPlane) { //GeometryStroke handle rotation ((Geometry.GeometryStroke)stroke_g).addPoint(pos); rhihoPointList.Add(UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, pos))); } } else { pos = Util.Math.GetTranslationVector3(UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx)); //GeometryStroke handle rotation already ((Geometry.GeometryStroke)stroke_g).addPoint(pos); rhihoPointList.Add(UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, pos))); } if (((Geometry.GeometryStroke)stroke_g).mNumPrimitives == 1) { SceneNode stroke = new SceneNode("Stroke", ref stroke_g, ref stroke_m); mScene.tableGeometry.add(ref stroke); //mScene.staticGeometry.add(ref stroke); strokeId = stroke.guid; } //testing the performance of rhino curve if (rhihoPointList.Count == 2) { rcurve = Curve.CreateInterpolatedCurve(rhihoPointList.ToArray(), 3); } else if (rhihoPointList.Count > 2) { rcurve.Extend(Rhino.Geometry.CurveEnd.End, Rhino.Geometry.CurveExtensionStyle.Line, rhihoPointList[rhihoPointList.Count - 1]); } }
protected override void onClickOculusAX(ref VREvent_t vrEvent) { //TODO-support projection curve Point3d planeCenter = new Point3d(); if (!isProjection) { Brep[] shapes = Brep.CreatePlanarBreps(modelcurve); modelBrep = shapes[0]; //Util.addRhinoObjectSceneNode(ref mScene, ref modelBrep, ref mesh_m, shapeType.ToString(), out renderObjSN); UtilOld.addSceneNode(ref mScene, ref modelBrep, ref mesh_m, shapeType.ToString(), out renderObjSN); if (shapeType == ShapeType.Circle) { Circle circle; if (modelcurve.TryGetCircle(out circle)) { planeCenter = circle.Center; } mScene.iPointList.Add(UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(circle.Center))); mScene.iPointList.Add(UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(circle.PointAt(0)))); } else if (shapeType == ShapeType.Rect) { Rhino.Geometry.Polyline polyline; if (modelcurve.TryGetPolyline(out polyline)) { Rectangle3d rect = Rectangle3d.CreateFromPolyline(polyline); planeCenter = rect.Center; mScene.iPointList.Add(UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(rect.Center))); mScene.iPointList.Add(UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(rect.Corner(3)))); } } } //update modelPlane and use tolerance to support unplanar surface Plane curvePlane = new Plane(); Double tolerance = 0; while (tolerance < 400) { if (modelcurve.TryGetPlane(out curvePlane, tolerance)) { modelPlane = curvePlane; break; } tolerance++; } int size = 240; PlaneSurface plane_surface = new PlaneSurface(curvePlane, new Interval(-size, size), new Interval(-size, size)); Brep newPlaneBrep = Brep.CreateFromSurface(plane_surface); Guid newPlaneID = UtilOld.addRhinoObject(ref mScene, ref newPlaneBrep, "3DP"); //might be better to use Replace(), just need to be careful about the referece count pointOnObjRef = null; pointOnObjRef = new ObjRef(newPlaneID); //add icurveList since we don't use EditPoint2 for circle and rect modelcurve.SetUserString(CurveData.CurveOnObj.ToString(), newPlaneID.ToString()); modelcurve.SetUserString(CurveData.PlaneOrigin.ToString(), curvePlane.Origin.ToString()); modelcurve.SetUserString(CurveData.PlaneNormal.ToString(), curvePlane.Normal.ToString()); mScene.iCurveList.Add(modelcurve); //call next interaction in the chain afterCurveCount = mScene.iCurveList.Count; mScene.pushInteractionFromChain(); }
public override void draw(bool isTop) { if (!isTop) { return; } // Clean this monstrosity OpenTK.Vector4 controller_p = UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx) * new OpenTK.Vector4(0, 0, 0, 1); OpenTK.Vector4 controller_pZ = UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx) * new OpenTK.Vector4(0, 0, -1, 1); Point3d controller_pRhino = UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_p.X, controller_p.Y, controller_p.Z))); Point3d controller_pZRhin = UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_pZ.X, controller_pZ.Y, controller_pZ.Z))); Rhino.Geometry.Vector3d direction = new Rhino.Geometry.Vector3d(controller_pZRhin.X - controller_pRhino.X, controller_pZRhin.Y - controller_pRhino.Y, controller_pZRhin.Z - controller_pRhino.Z); if (drawnType != DrawnType.In3D) { UtilOld.rayCasting(controller_pRhino, direction, ref rayCastingObjs, ref projectP, out targetPRhObjID); } else { projectP = controller_pRhino; } //only snap for the first drawing point if (currentState != State.DRAW && snapPointsList.Count > 0) { if (UtilOld.snapToPoints(ref projectP, ref snapPointsList) != -1) { isSnap = true; snapPointSN.material = new Material.SingleColorMaterial(1, 1, 1, 1); } else { isSnap = false; snapPointSN.material = new Material.SingleColorMaterial(0, 1, 0, 1); } } // Vector3 projectPVR = UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(projectP)); if (drawnType != DrawnType.In3D) { OpenTK.Matrix4 t = OpenTK.Matrix4.CreateTranslation(projectPVR); t.Transpose(); drawPoint.transform = t; if (targetPRhObjID != Guid.Empty) { drawPoint.material = new Material.SingleColorMaterial(1, 1, 1, 1); } else { drawPoint.material = new Material.SingleColorMaterial(1, 1, 1, 0); } } //moving XYZ planes if (currentState == State.MOVEPLANE) { OpenTK.Vector3 controllerVector = UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_p.X, controller_p.Y, controller_p.Z)) - UtilOld.RhinoToOpenTKPoint(moveControlerOrigin); float translate = OpenTK.Vector3.Dot(controllerVector, UtilOld.RhinoToOpenTKVector(planeNormal)) / (float)planeNormal.Length; float relTranslate = translate - lastTranslate; lastTranslate = translate; Matrix4 transM = Matrix4.CreateTranslation(new Vector3(relTranslate * (float)planeNormal.X, relTranslate * (float)planeNormal.Y, relTranslate * (float)planeNormal.Z)); transM.Transpose(); UtilOld.updateRhinoObjectSceneNode(ref mScene, ref movePlaneRef, UtilOld.OpenTKToRhinoTransform(transM)); } if (currentState != State.DRAW) { return; } else { //checking the projectPoint is valid if (drawnType != DrawnType.In3D && targetPRhObjID == Guid.Empty) { return; } } //drawing curve section belows if (shouldSnap && ((Geometry.GeometryStroke)stroke_g).mNumPoints == 0) { ((Geometry.GeometryStroke)stroke_g).addPoint(UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(snapPointsList[0]))); } ((Geometry.GeometryStroke)stroke_g).addPoint(projectPVR); rhinoCurvePoints.Add(projectP); if (((Geometry.GeometryStroke)stroke_g).mNumPrimitives == 1) { strokeSN = new SceneNode("Stroke", ref stroke_g, ref stroke_m); UtilOld.addSceneNode(ref mScene, ref strokeSN); } //create rhino curve for comoputing length of the curve if (rhinoCurvePoints.Count == 2) { if (shouldSnap) { //make sure the first point is the snap point if necessary if (UtilOld.computePointDistance(UtilOld.RhinoToOpenTKPoint(rhinoCurvePoints[0]), UtilOld.RhinoToOpenTKPoint(snapPointsList[0])) != 0) { rhinoCurvePoints.Insert(0, snapPointsList[0]); } } rhinoCurve = Rhino.Geometry.Curve.CreateInterpolatedCurve(rhinoCurvePoints.ToArray(), 3); } else if (rhinoCurvePoints.Count > 2) { double length1 = rhinoCurve.GetLength(); rhinoCurve = Rhino.Geometry.Curve.CreateInterpolatedCurve(rhinoCurvePoints.ToArray(), 3); double length2 = rhinoCurve.GetLength(); displacement = displacement + (float)Math.Abs(length2 - length1); //TODO-Debug why it failed //rhinoCurve = rhinoCurve.Extend(Rhino.Geometry.CurveEnd.End, Rhino.Geometry.CurveExtensionStyle.Line, rhinoCurvePoints[rhinoCurvePoints.Count - 1]); //dynamic render model //TODO: finding the right curve if (dynamicRender != "none" && backgroundStart == false && displacement > 10) { backgroundStart = true; R = d.BeginInvoke(new AsyncCallback(modelCompleted), null); } } }
public override void init() { resetVariables(); //support undo function if (mScene != null && (afterCurveCount - beforeCurveCount) > 0) { if (mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.CurveOnObj.ToString()) != "") { Guid curveOnObjId = new Guid(mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.CurveOnObj.ToString())); ObjRef curveOnObjRef = new ObjRef(curveOnObjId); if (curveOnObjRef.Object().Attributes.Name.Contains("railPlane") || curveOnObjRef.Object().Attributes.Name.Contains("MoveP")) { UtilOld.removeRhinoObject(ref mScene, curveOnObjRef.ObjectId); } } mScene.iCurveList.RemoveAt(mScene.iCurveList.Count - 1); UtilOld.removeSceneNode(ref mScene, ref strokeSN); strokeSN = null; //need to clear stroke tprint SceneNode as well here if (renderObjSN != null) { UtilOld.removeSceneNode(ref mScene, ref renderObjSN); renderObjSN = null; } //Util.removeRhinoObject(ref mScene, curveOnObjRef.ObjectId); curveOnObjRef = null; if (railPlaneSN != null) { UtilOld.removeRhinoObjectSceneNode(ref mScene, ref railPlaneSN); railPlaneSN = null; } } if (drawnType != DrawnType.In3D && drawnType != DrawnType.None) { UtilOld.showLaser(ref mScene, true); //create and add referece planes to scene if (drawnType == DrawnType.Reference) { Vector3 railPlaneNormal = UtilOld.RhinoToOpenTKVector(UtilOld.getVectorfromString(mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneNormal.ToString()))); OpenTK.Vector3 worldUpAxis = new Vector3(0, 0, 1); //trick to avoid cross product of 2 parallel vetors if (railPlaneNormal.X == 0 && railPlaneNormal.Y == 0 && railPlaneNormal.Z == 1) { railPlaneNormal = new Vector3(0, 0.005f, 1); } Plane railPlane = new Plane(mScene.iCurveList[mScene.iCurveList.Count - 1].GetBoundingBox(true).Center, UtilOld.openTkToRhinoVector(Vector3.Cross(railPlaneNormal, worldUpAxis))); float planeSize = 240; PlaneSurface plane_surface2 = new PlaneSurface(railPlane, new Interval(-planeSize, planeSize), new Interval(-planeSize, planeSize)); Brep railPlane2 = Brep.CreateFromSurface(plane_surface2); Guid railPlaneGuid = UtilOld.addRhinoObjectSceneNode(ref mScene, ref railPlane2, ref railPlane_m, "railPlane", out railPlaneSN); } else if (drawnType == DrawnType.Plane) { UtilOld.setPlaneAlpha(ref mScene, 0.4f); } //init rayCastingObjs 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)) { bool b1 = (drawnType == DrawnType.Plane) && rhObj.Attributes.Name.Contains("plane"); bool b2 = (drawnType == DrawnType.Surface) && (rhObj.Attributes.Name.Contains("brepMesh") || rhObj.Attributes.Name.Contains("aprint") || rhObj.Attributes.Name.Contains("patchSurface")); bool b3 = (drawnType == DrawnType.Reference) && rhObj.Attributes.Name.Contains("railPlane"); if (b1 || b2 || b3) { rayCastingObjs.Add(new ObjRef(rhObj.Id)); } } Geometry.Geometry geo = new Geometry.DrawPointMarker(new OpenTK.Vector3(0, 0, 0)); Material.Material m = new Material.SingleColorMaterial(1, 1, 1, 0);//TODO: teseting alpha working or not drawPoint = new SceneNode("drawPoint", ref geo, ref m); UtilOld.addSceneNode(ref mScene, ref drawPoint); } else { UtilOld.showLaser(ref mScene, false); } //generate snap points when we need to draw from the center of the shapes, drawnType could be DrawnType.Reference or DrawnType.In3D if (dynamicRender == "Extrude" || dynamicRender == "Sweep" || drawnType == DrawnType.Reference) { shouldSnap = true; ShapeType shapeType = (ShapeType)mScene.selectionDic[SelectionKey.Profile1Shape]; Circle circle; Rectangle3d rect; if (shapeType == ShapeType.Circle) { if (mScene.iCurveList[mScene.iCurveList.Count - 1].TryGetCircle(out circle)) { snapPointsList.Add(circle.Center); } } else if (shapeType == ShapeType.Rect) { Rhino.Geometry.Polyline polyline; if (mScene.iCurveList[mScene.iCurveList.Count - 1].TryGetPolyline(out polyline)) { rect = Rectangle3d.CreateFromPolyline(polyline); snapPointsList.Add(rect.Center); } } //visualize the snap points Geometry.Geometry geo = new Geometry.DrawPointMarker(new Vector3(0, 0, 0)); Material.Material m = new Material.SingleColorMaterial(0, 1, 0, 1); UtilOld.MarkPointVR(ref mScene, UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(snapPointsList[0])), ref geo, ref m, out snapPointSN); } d = new generateModel_Delegate(generateModel); }
protected override void onClickOculusTrigger(ref VREvent_t vrEvent) { primaryDeviceIndex = vrEvent.trackedDeviceIndex; if (currentState != State.READY || targetPRhObjID == Guid.Empty) { return; } pointOnObjRef = new ObjRef(targetPRhObjID); //chage to only raycasting to the obj where we draw, if not snap to the origin if (!(projectP.X == 0 && projectP.Y == 0 && projectP.Z == 0)) { rayCastingObjs.Clear(); rayCastingObjs.Add(pointOnObjRef); } //testing Vector3 projectPVR = UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(projectP)); mScene.iPointList.Add(UtilOld.platformToVRPoint(ref mScene, projectPVR)); //not use at the current version, point is VR coordinate pointsList.Add(projectP); //render edit points in VR Geometry.Geometry geo = new Geometry.DrawPointMarker(new Vector3(0, 0, 0)); Material.Material m = new Material.SingleColorMaterial(0, 1, 0, 1); SceneNode editPointSN; UtilOld.MarkPointVR(ref mScene, projectPVR, ref geo, ref m, out editPointSN); pointMarkers.Add(editPointSN); //TODO-hide two other design plane if (pointMarkers.Count == 1) { if (pointOnObjRef != null && drawnType == DrawnType.Plane) { if (!(projectP.X == 0 && projectP.Y == 0 && projectP.Z == 0)) { UtilOld.hideOtherPlanes(ref mScene, pointOnObjRef.Object().Attributes.Name); } computeContourCurve(); } //TODO- how to find the curvePlane on the surface like teapot body, using the normal of first point? //do we need to use different ways for patch surface and other generated surface else if (pointOnObjRef != null && drawnType == DrawnType.Surface) { computeContourCurve(); } } if (maxNumPoint == pointMarkers.Count) { //Assume we always can find a curvePlane sicnce we use a huge tolerance NurbsCurve modelcurve = null; Brep modelBrep; string modelName = ""; if (shapeType == ShapeType.Circle) { float radius = (float)Math.Sqrt(Math.Pow(pointsList[1].X - pointsList[0].X, 2) + Math.Pow(pointsList[1].Y - pointsList[0].Y, 2) + Math.Pow(pointsList[1].Z - pointsList[0].Z, 2)); Circle circle = new Rhino.Geometry.Circle(curvePlane, pointsList[0], radius); modelcurve = circle.ToNurbsCurve(); modelName = "Circle"; } else if (shapeType == ShapeType.Rect) { Vector3 rectDiagonalV = new Vector3((float)(pointsList[0].X - pointsList[1].X), (float)(pointsList[0].Y - pointsList[1].Y), (float)(pointsList[0].Z - pointsList[1].Z)); float lenDiagonal = rectDiagonalV.Length; Vector3 rectLeftTop = new Vector3((float)pointsList[0].X, (float)pointsList[0].Y, (float)pointsList[0].Z) + lenDiagonal * rectDiagonalV.Normalized(); Point3d topLeftP = new Point3d(rectLeftTop.X, rectLeftTop.Y, rectLeftTop.Z); Rectangle3d rect = new Rectangle3d(curvePlane, topLeftP, pointsList[1]); //using top-left cornel and bottom right //Rectangle3d rect = new Rectangle3d(curvePlane, pointsList[0], pointsList[1]); modelcurve = rect.ToNurbsCurve(); modelName = "Rect"; } /* * Brep[] shapes = Brep.CreatePlanarBreps(modelcurve); * modelBrep = shapes[0]; * Guid guid = Util.addRhinoObjectSceneNode(ref mScene, ref modelBrep, ref profile_m, modelName, out renderObjSN); */ //generate new curveOnObj for mvoingPlane cases sicne and move the XYZPlanes to origial positons if (drawnType == DrawnType.Plane) { Rhino.Geometry.Vector3d newNormal = new Rhino.Geometry.Vector3d();; Point3d newOrigin = new Point3d(); String planeName = pointOnObjRef.Object().Attributes.Name; Point3d planeOrigin = pointOnObjRef.Object().Geometry.GetBoundingBox(true).Center; if (planeName.Contains("planeXY")) { newNormal = new Rhino.Geometry.Vector3d(0, 0, 1); newOrigin = new Point3d(0, 0, planeOrigin.Z); } else if (planeName.Contains("planeYZ")) { newNormal = new Rhino.Geometry.Vector3d(1, 0, 0); newOrigin = new Point3d(planeOrigin.X, 0, 0); } else if (planeName.Contains("planeXZ")) { newNormal = new Rhino.Geometry.Vector3d(0, 1, 0); newOrigin = new Point3d(0, planeOrigin.Y, 0); } Plane newPlane = new Plane(newOrigin, newNormal); int size = 240; PlaneSurface plane_surface = new PlaneSurface(newPlane, new Interval(-size, size), new Interval(-size, size)); Brep newPlaneBrep = Brep.CreateFromSurface(plane_surface); Guid newPlaneID = UtilOld.addRhinoObject(ref mScene, ref newPlaneBrep, "MoveP"); //might be better to use Replace(), just need to be careful about the referece count pointOnObjRef = null; pointOnObjRef = new ObjRef(newPlaneID); modelcurve.SetUserString(CurveData.CurveOnObj.ToString(), newPlaneID.ToString()); modelcurve.SetUserString(CurveData.PlaneOrigin.ToString(), newPlane.Origin.ToString()); modelcurve.SetUserString(CurveData.PlaneNormal.ToString(), newPlane.Normal.ToString()); } else if (drawnType == DrawnType.Surface) { modelcurve.SetUserString(CurveData.CurveOnObj.ToString(), pointOnObjRef.ObjectId.ToString()); modelcurve.SetUserString(CurveData.PlaneOrigin.ToString(), curvePlane.Origin.ToString()); modelcurve.SetUserString(CurveData.PlaneNormal.ToString(), curvePlane.Normal.ToString()); } mScene.iCurveList.Add(modelcurve); //call next interaction in the chain afterCurveCount = mScene.iCurveList.Count; mScene.pushInteractionFromChain(); currentState = State.READY; } }
public override void draw(bool isTop) { if (!isTop) { return; } OpenTK.Vector4 controller_p = UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx) * new OpenTK.Vector4(0, 0, 0, 1); OpenTK.Vector4 controller_pZ = UtilOld.getControllerTipPosition(ref mScene, primaryDeviceIndex == mScene.leftControllerIdx) * new OpenTK.Vector4(0, 0, -1, 1); Point3d controller_pRhino = UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_p.X, controller_p.Y, controller_p.Z))); Point3d controller_pZRhin = UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_pZ.X, controller_pZ.Y, controller_pZ.Z))); Rhino.Geometry.Vector3d direction = new Rhino.Geometry.Vector3d(controller_pZRhin.X - controller_pRhino.X, controller_pZRhin.Y - controller_pRhino.Y, controller_pZRhin.Z - controller_pRhino.Z); if (drawnType != DrawnType.In3D) { UtilOld.rayCasting(controller_pRhino, direction, ref rayCastingObjs, ref projectP, out targetPRhObjID); } else { projectP = controller_pRhino; } //TODO-only snap for the first drawing point if (pointMarkers.Count == 0 && targetPRhObjID != Guid.Empty) { computeContourCurve(); UtilOld.snapToPoints(ref projectP, ref snapPointsList); } Vector3 projectPVR = UtilOld.platformToVRPoint(ref mScene, UtilOld.RhinoToOpenTKPoint(projectP)); if (drawnType != DrawnType.In3D) { OpenTK.Matrix4 t = OpenTK.Matrix4.CreateTranslation(projectPVR); t.Transpose(); drawPoint.transform = t; if (targetPRhObjID != Guid.Empty) { drawPoint.material = new Material.SingleColorMaterial(1, 1, 1, 1); } else { drawPoint.material = new Material.SingleColorMaterial(1, 1, 1, 0); } } //moving XYZ planes if (currentState == State.MOVEPLANE) { OpenTK.Vector3 controllerVector = UtilOld.vrToPlatformPoint(ref mScene, new OpenTK.Vector3(controller_p.X, controller_p.Y, controller_p.Z)) - UtilOld.RhinoToOpenTKPoint(moveControlerOrigin); float translate = OpenTK.Vector3.Dot(controllerVector, UtilOld.RhinoToOpenTKVector(planeNormal)) / (float)planeNormal.Length; float relTranslate = translate - lastTranslate; lastTranslate = translate; Matrix4 transM = Matrix4.CreateTranslation(new Vector3(relTranslate * (float)planeNormal.X, relTranslate * (float)planeNormal.Y, relTranslate * (float)planeNormal.Z)); transM.Transpose(); UtilOld.updateRhinoObjectSceneNode(ref mScene, ref movePlaneRef, UtilOld.OpenTKToRhinoTransform(transM)); } }