/* * */ public List <Operation> CreateMoveOperations(List <GameObject> objects, Vector3 transMatrix) { List <Operation> operations = new List <Operation>(); foreach (GameObject anObj in objects) { if (anObj.transform.childCount > 0) { // //When selecting multiple voxels or polygon // if (anObj.transform.childCount == 1) { //When selecting polygon /* * foreach (Transform child in anObj.transform) { * string[] posIDs = ObjLoadHelper.GetEmptyVoxels(child.gameObject); * //Debug.Log ("posID(emptyVoxel)" + posIDs [posIDs.Length-1]); * operations.Add (new Operation (this.controller.socket.getID(), Operation.MOVE_POLYGON, "{\"gid\": \"" + anObj.name + * "\", \"posIDs\": \"" + Util.GetCommaLineFrom (posIDs) + * "\", \"transMatrix\": \"" + ChainXModel.CreatePosID (transMatrix) + "\"}") * ); * } */ } else { //When selecting multiple voxels List <string> posIDs = new List <string> (); List <string> destPosIDs = new List <string> (); List <string> destTextureTypes = new List <string> (); foreach (Transform child in anObj.transform) { //Debug.Assert (Util.CreatePosID (child.position) == child.name); Vector3 destPosition = Util.SplitPosID(child.name) + transMatrix; posIDs.Add(child.name); destPosIDs.Add(Util.CreatePosID(destPosition)); Voxel aVoxel = this.controller.cv.getVoxel(child.name); destTextureTypes.Add(aVoxel.getTextureType().ToString()); } string posIDsLine = Util.GetCommaLineFrom(posIDs); string destPosIDsLine = Util.GetCommaLineFrom(destPosIDs); Operation[] ops = new Operation[4]; ops[0] = new Operation( this.controller.socket.getID(), Operation.LEAVE_ALL, "{\"gid\": \"" + anObj.name + "\", \"posIDs\": \"" + posIDsLine + "\"}" ); ops[1] = new Operation( this.controller.socket.getID(), Operation.DELETE_ALL, "{\"gid\": \"" + anObj.name + "\", \"posIDs\": \"" + posIDsLine + "\"}" ); ops[2] = new Operation( this.controller.socket.getID(), Operation.INSERT_ALL, "{\"gid\": \"" + anObj.name + "\", \"textureTypes\": \"" + Util.GetCommaLineFrom(destTextureTypes) + "\", \"posIDs\": \"" + destPosIDsLine + "\"}" ); ops[3] = new Operation( this.controller.socket.getID(), Operation.JOIN_ALL, "{\"gid\": \"" + anObj.name + "\", \"posIDs\": \"" + destPosIDsLine + "\"}" ); long ts = ops[0].getTimestamp(); for (int i = 0; i < ops.Length; i++) { ops[i].setTimestamp(ts + i); //できる限り1に近づけないと間に入り込まれる operations.Add(ops[i]); } } } else { //Debug.Log (anObj.name); //Debug.Log (anObj.transform.childCount); operations.Add(new Operation( this.controller.socket.getID(), Operation.MOVE, "{\"posID\": \"" + anObj.name + "\", \"transMatrix\": \"" + ChainXModel.CreatePosID(transMatrix) + "\"}") ); } } return(operations); }
/* * PaintToolのVoxelまたはグループVoxelが選択されていて、マウスクリックされたときに呼ばれる。 * ペイントさせ方は、ぶつかったオブジェクト(Plane)よりも手前のオブジェクトを * 「後ろ側から手前に」辿っていき、Voxelを挿入する。 */ private void paintVoxels() { string paintToolName = this.model.getCurrentPaintTool(); List <Operation> ops = new List <Operation>(); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit)) { if (paintToolName.IndexOf(ChainXModel.PAINT_TOOL_VOXEL_ID) > -1) { // //単位Voxelをペイントする // float distance = hit.distance - 0.5f; //ヒットしたRayより少し手前のPointをたどる Vector3 hitPointShort = ChainXModel.GetRoundIntPoint(ray.GetPoint(distance)); int textureType = int.Parse(this.paintTool.GetComponent <Text>().text); ops.Add(new Operation(this.socket.getID(), Operation.INSERT, "{\"posID\": \"" + ChainXModel.CreatePosID(hitPointShort) + "\", \"textureType\":\"" + textureType + "\"}" )); } else if (paintToolName.IndexOf(ChainXModel.PAINT_TOOL_GROUP_ID) > -1) { // //グループVoxelsをペイントする // float cursor_d = hit.distance - 0.5f; Vector3 hitPointShort = ChainXModel.GetRoundIntPoint(ray.GetPoint(cursor_d)); //もっともヒットポイントに近いオブジェクトをグループVoxelsの中から見つける GameObject closeObjToHitPoint = null; float minDistance = float.MaxValue; GameObject groupObj = GameObject.Find(paintToolName); foreach (Transform aPart in groupObj.transform) { float d = Vector3.Distance(aPart.gameObject.transform.position, hitPointShort); if (d < minDistance) { minDistance = d; closeObjToHitPoint = aPart.gameObject; } } bool put_enable; Vector3 diffV = Vector3.zero; string posIDs = ""; string textureTypes = ""; while (cursor_d > 0) { posIDs = ""; textureTypes = ""; hitPointShort = ChainXModel.GetRoundIntPoint(ray.GetPoint(cursor_d)); diffV = hitPointShort - closeObjToHitPoint.transform.position; put_enable = true; foreach (Transform aPart in groupObj.transform) { Vector3 movingV = aPart.gameObject.transform.position + diffV; if (GameObject.Find(Util.CreatePosID(movingV)) != null) { put_enable = false; break; } posIDs += Util.CreatePosID(movingV) + Const.SPLIT_CHAR; textureTypes += aPart.gameObject.GetComponent <Text>().text + Const.SPLIT_CHAR; } if (put_enable) { break; } cursor_d--; } posIDs = posIDs.TrimEnd(Const.SPLIT_CHAR); textureTypes = textureTypes.TrimEnd(Const.SPLIT_CHAR); this.selectedObjects.Remove(groupObj); string gid = ChainXModel.CreateGID(); ops.Add(new Operation(this.socket.getID(), Operation.INSERT_ALL, "{\"posIDs\": \"" + posIDs + "\", \"gid\": \"" + gid + "\", \"textureTypes\":\"" + textureTypes + "\"}") ); ops.Add(new Operation(this.socket.getID(), Operation.JOIN_ALL, "{\"posIDs\": \"" + posIDs + "\", \"gid\": \"" + gid + "\"}") ); } long ts = ops[0].getTimestamp(); for (int i = 0; i < ops.Count; i++) { ops[i].setTimestamp(ts + i); //できる限り1に近づけないと間に入り込まれる this.ApplyChainVoxel(ops[i]); } //Debug.DrawLine(ray.origin, hitPointShort, Color.red, 60.0f, true); //レーザービーム } }
/** * Test an Operation class */ public static void Test() { int numberOfTest = Const.TEST_QUALITY; // // A test for Operation.CombinePosition(). // for (int t = 0; t < numberOfTest; ++t) { Vector3 posIDVector = Util.CreateRandomVector3(-10000, 10000); Vector3 transMatrixVector = Util.CreateRandomTransMatrix(); Debug.Assert( ChainXModel.CreatePosID(posIDVector + transMatrixVector) == Operation.CombinePosition( ChainXModel.CreatePosID(posIDVector), ChainXModel.CreatePosID(transMatrixVector) ) ); } // // Jsonへの変換とその逆ができているかをチェック // Operationへの初期値の値の変化がないかをチェック at CreateRandomOperation() // string json = ""; Operation o1, o2; for (int t = 0; t < numberOfTest; ++t) { o1 = Operation.CreateRandomOperation(); json = Operation.ToJson(o1); o2 = Operation.FromJson(json); Debug.Assert(o1.getSID() == o2.getSID()); Debug.Assert(o1.getOpType() == o2.getOpType()); Debug.Assert(o1.getTimestamp() == o2.getTimestamp()); switch (o1.getOpType()) { case Operation.INSERT: Debug.Assert(o1.getPosID() == o2.getPosID()); Debug.Assert(o1.getTextureType() == o2.getTextureType()); break; case Operation.DELETE: Debug.Assert(o1.getPosID() == o2.getPosID()); break; case Operation.MOVE: Debug.Assert(o1.getPosID() == o2.getPosID()); Debug.Assert(o1.getTransMatrix() == o2.getTransMatrix()); Debug.Assert(o1.getDestPosID() == o2.getDestPosID()); break; case Operation.CREATE: Debug.Assert(o1.getGID() == o2.getGID()); break; case Operation.JOIN: case Operation.LEAVE: Debug.Assert(o1.getGID() == o2.getGID()); Debug.Assert(o1.getPosID() == o2.getPosID()); break; case Operation.JOIN_ALL: case Operation.LEAVE_ALL: Debug.Assert(o1.getGID() == o2.getGID()); Debug.Assert(Util.GetCommaLineFrom(o1.getPosIDs()) == Util.GetCommaLineFrom(o2.getPosIDs())); break; case Operation.MOVE_ALL: Debug.Assert(Util.GetCommaLineFrom(o1.getPosIDs()) == Util.GetCommaLineFrom(o2.getPosIDs())); Debug.Assert(o1.getTransMatrix() == o2.getTransMatrix()); Debug.Assert(Util.GetCommaLineFrom(o1.getDestPosIDs()) == Util.GetCommaLineFrom(o2.getDestPosIDs())); Debug.Assert(o1.getGID() == o2.getGID()); break; case Operation.INSERT_ALL: Debug.Assert(Util.GetCommaLineFrom(o1.getPosIDs()) == Util.GetCommaLineFrom(o2.getPosIDs())); Debug.Assert(o1.getTextureType() == o2.getTextureType()); Debug.Assert(o1.getGID() == o2.getGID()); break; case Operation.DELETE_ALL: Debug.Assert(Util.GetCommaLineFrom(o1.getPosIDs()) == Util.GetCommaLineFrom(o2.getPosIDs())); Debug.Assert(o1.getGID() == o2.getGID()); break; case Operation.INSERT_POLYGON: Debug.Assert(Util.GetCommaLineFrom(o1.getPosIDs()) == Util.GetCommaLineFrom(o2.getPosIDs())); Debug.Assert(o1.getObjPath() == o2.getObjPath()); Debug.Assert(o1.getGID() == o2.getGID()); break; case Operation.DELETE_POLYGON: Debug.Assert(Util.GetCommaLineFrom(o1.getPosIDs()) == Util.GetCommaLineFrom(o2.getPosIDs())); Debug.Assert(o1.getGID() == o2.getGID()); break; case Operation.MOVE_POLYGON: Debug.Assert(Util.GetCommaLineFrom(o1.getPosIDs()) == Util.GetCommaLineFrom(o2.getPosIDs())); Debug.Assert(o1.getTransMatrix() == o2.getTransMatrix()); Debug.Assert(Util.GetCommaLineFrom(o1.getDestPosIDs()) == Util.GetCommaLineFrom(o2.getDestPosIDs())); Debug.Assert(o1.getGID() == o2.getGID()); break; default: throw new System.InvalidOperationException( String.Format("Operation{0} at CreateRandomOperation()", o1.getOpType()) ); } } Debug.Log("End an Operation class test"); }