public static uint GetClosestPtIndex(double inFltPosition, bool blnStartPt, TargetCuboid targetCuboid, double dX, double dblProximityValue) { double fltDivVal, fltRemainder; uint uintReturnVal; if (blnStartPt) { if (inFltPosition >= targetCuboid.fltMinx) { if (inFltPosition > targetCuboid.fltMaxx) { uintReturnVal = 0; //No points can be set as invalid } else { fltDivVal = (inFltPosition - targetCuboid.fltMinx) / dX; uintReturnVal = CommonFunctions.GetUIntFromDouble(fltDivVal); fltRemainder = fltDivVal - (double)uintReturnVal; //18-March-11: THIS IS AN IMPORTANT THING. I SPENT A WHOLE DAY DEBUGGING WHY THE OUTPUT FROM //IBModeler 2.0 IS COMING OUT TO BE DIFFERENT THAN THIS CODE. FINALLY, LANDED HERE WHICH EXPLAINS WHY. //Change on 29-April-08 don't see any reason why the proximity calculation for //start point should be any different than that of the end of point so making them the //same. This will resolve the issue: If the same value is passed for the start point //and the end point, then the value of start point comes out to be greater than end point //which does not make sense //if (fltRemainder > dblProximityValue) // uintReturnVal++; if ((1.0f - fltRemainder) <= dblProximityValue) { uintReturnVal++; } //end of change on 29-April-08 uintReturnVal++; //All point indices start from one and not from zero } } else { uintReturnVal = 1; } } else { if (inFltPosition <= targetCuboid.fltMaxx) { if (inFltPosition < targetCuboid.fltMinx) { uintReturnVal = 0; //No points can be set as invalid } else { fltDivVal = (inFltPosition - targetCuboid.fltMinx) / dX; uintReturnVal = CommonFunctions.GetUIntFromDouble(fltDivVal); fltRemainder = fltDivVal - (double)uintReturnVal; if ((1.0f - fltRemainder) <= dblProximityValue) { uintReturnVal++; } uintReturnVal++; //All point indices start from one and not from zero } } else { uintReturnVal = targetCuboid.uintXPoints; //Assign the maximum value } } return(uintReturnVal); }
/// <summary> /// /// </summary> /// <param name="inT"></param> public void RetrieveValues(ref TargetCuboid inT, bool blnCloseAfterRetrieving) { /////////////////////////Variable Declaration//////////////////////////////////////// uint totalBits, rem; //////////////////////////////////////////////////////////////////////////////////// //Read the values from the file //fp.Seek(0, SeekOrigin.Begin); //inT.uintXPoints = readInt(); //inT.uintYPoints = readInt(); //inT.uintZPoints = readInt(); //inT.fltMinx = readFloat(); //inT.fltMaxx = readFloat(); //inT.fltMiny = readFloat(); //inT.fltMaxy = readFloat(); //inT.fltMinz = readFloat(); //inT.fltMaxz = readFloat(); byte[] arrValues = new byte[4]; int intOffset = 0; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.uintXPoints = BitConverter.ToUInt32(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.uintYPoints = BitConverter.ToUInt32(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.uintZPoints = BitConverter.ToUInt32(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.fltMinx = BitConverter.ToSingle(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.fltMaxx = BitConverter.ToSingle(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.fltMiny = BitConverter.ToSingle(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.fltMaxy = BitConverter.ToSingle(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.fltMinz = BitConverter.ToSingle(arrValues, 0); intOffset += 4; fp.Position = intOffset; if (fp.Read(arrValues, 0, 4) != 4) { throw new Exception("File Access Error"); } inT.fltMaxz = BitConverter.ToSingle(arrValues, 0); //byte snapSlideOptn = read(); intOffset += 4; fp.Position = intOffset; fp.Read(arrValues, 0, 1); byte snapSlideOptn = arrValues[0]; if (snapSlideOptn != 1) { fp.Close(); throw new Exception("This mold type is not for using pictures to create the model"); } /*~~~~~~~~~~~~set the cuboid values~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ cuboidWidth = inT.uintXPoints; cuboidHeight = inT.uintYPoints; cuboidThickness = inT.uintZPoints; //find the size of the file in bits totalBits = cuboidWidth * cuboidHeight * cuboidThickness; //increase the value to make it divisible by 8 rem = totalBits % 8; if (rem != 0) { totalBits = totalBits + (8 - rem); } //find the total number of bytes required to store the points muintTotalPtBytes = totalBits / 8; if (fp.Length != (uint)(Constants.MOLD_HEADER_SIZE + muintTotalPtBytes)) { fp.Close(); throw new Exception("Invalid or corrupt mold file. The file size is invalid"); } //27-Feb-2008 Changed logic to keep all the point information in the memory //uint uintBytesAlongX = (inT.uintXPoints - inT.uintXPoints%8)/8 + 3; // //try { // //Allocate memory for storing points along X axis temporarily // g_btMldPointData = (byte *)malloc(uintBytesAlongX); // if(g_btMldPointData == NULL) { // error = FILE_POINTS_RETRIEVE_VALUES + MEMORY_ALLOCATION_FAILURE; // fp.Close(); // } try { //Allocate memory for storing points along X axis temporarily mArrPointData = new byte[muintTotalPtBytes]; if (mArrPointData == null) { fp.Close(); throw new Exception("Could not allocate memory to store the mold data"); } //End of changes done on 27-Feb-2008 } catch (Exception e) { mArrPointData = null; fp.Close(); throw new Exception("Could not allocate memory to store the mold data. The following error occured: " + e.Message); } //Change done 27-Feb-2008 //Read all the mold point data and keep it in memory //fp.Seek(Constants.MOLD_HEADER_SIZE, SeekOrigin.Begin); //if(fp.Read(g_btMldPointData, totalBytes) != totalBytes) //{ // fp.Close(); // throw new Exception("Error occured while accessing the file"); //} //End of change done on 27-Feb-2008 fp.Position = Constants.MOLD_HEADER_SIZE; if (fp.Read(mArrPointData, 0, (int)muintTotalPtBytes) != (int)muintTotalPtBytes) { throw new Exception("An error occured while trying to read the mold file data."); } if (blnCloseAfterRetrieving) { fp.Close(); } }