?
Solved

C# code

Posted on 2013-06-06
8
Medium Priority
?
363 Views
Last Modified: 2013-06-08
Hi,

Does anyone have equivalent .net code for the attached python code? Please help me to convert to c#.

Thanks
mesure-volume.py
0
Comment
Question by:ayha1999
  • 4
  • 4
8 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39227514
I have no way of testing if this code is right but as best as I can tell it is a port of the .py code.

There was quite a bit in the python script that is not used so I have not included it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WebApplication1
{
    public class STLUtils
    {
        public enum LengthType { inch, cm } ;

        LengthType _lengthType;
        string _filename;
        BinaryReader br;

        public bool IsValidated(string[] args)
        {
            if (args.Length != 2) return false;
            if (!Enum.TryParse<LengthType>(args[1], true, out _lengthType))
            {
                return false;
            }

            _filename = args[0];

            return File.Exists(_filename);
        }

        public uint read_32bitInt()
        {
            byte[] b32 = new byte[4];

            br.Read(b32, 0, 4);
            if (BitConverter.IsLittleEndian)
            {
                b32.Reverse();
            }
            return (uint)BitConverter.ToInt32(b32, 0);

        }

        public uint[] get_vector(int size)
        {
            uint[] v = new uint[size];

            for (int i = 0; i < size; i++)
            {
                v[i] = read_32bitInt();
            }

            return v;
        }

        public double read_triangle()
        {
        
            byte[] bytecount = new byte[2];

            uint[] n = get_vector(3);
            uint[] p1 = get_vector(3);
            uint[] p2 = get_vector(3);
            uint[] p3 = get_vector(3);
            
            br.Read(bytecount, 0, 2);

            return signedVolumeOfTriangle(p1, p2, p3);
        }

        public double CalculateVolume2(string unit)
        {
            br = new BinaryReader(File.Open(_filename, FileMode.Open));
            byte[] buffer = new byte[128];
            double dTotal = 0;

            br.Read(buffer, 0, 80);
            uint nTriangles = read_32bitInt();
            double totalVolume = 0;
            for(int i=0;i < nTriangles; i++) {
                totalVolume += read_triangle();
            }
            totalVolume = totalVolume / 1000;
            if (unit =="inch")
            {
                totalVolume = cm3_To_inch3Transform(totalVolume);
            }
            return totalVolume;
        }


        private double cm3_To_inch3Transform(double totalVol)
        {
            return totalVol * 0.0610237441;
        }

        public double signedVolumeOfTriangle(uint[] p1, uint[] p2, uint[] p3)
        {
            double v321 = p3[0] * p2[1] * p1[2];
            double v231 = p2[0] * p3[1] * p1[2];
            double v312 = p3[0] * p1[1] * p2[2];
            double v132 = p1[0] * p3[1] * p2[2];
            double v213 = p2[0] * p1[1] * p3[2];
            double v123 = p1[0] * p2[1] * p3[2];
            return (1.0 / 6.0) * (-v321 + v231 + v312 - v132 - v213 + v123);
        }
    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            STLUtils stlUtils = new STLUtils();
            if (stlUtils.IsValidated(new string[] { @"c:\gf.stl", "cm" }))
            {
                double volume = stlUtils.CalculateVolume2("cm");
                Response.Write(volume);
            }
        }
    }
}

Open in new window

0
 
LVL 7

Author Comment

by:ayha1999
ID: 39227987
I tried the cellphone.stl, the result is in cm

60648440.3221672

the actual result is
Volume: 8.09431408456 cubic cm
Volume: 0.493945351361 cubic inch

Please check

thanks
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39228221
I tried the cellphone.stl, the result is in cm

As I don't have this file how would you like me to check?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 7

Author Comment

by:ayha1999
ID: 39228497
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 39229038
Ok this code produces the same answer - problem was code was treating values as Int32 instead of Float32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WebApplication1
{
    public class STLUtils
    {
        public enum LengthType { inch, cm } ;

        LengthType _lengthType;
        string _filename;
        BinaryReader br;

        public bool IsValidated(string[] args)
        {
            if (args.Length != 2) return false;
            if (!Enum.TryParse<LengthType>(args[1], true, out _lengthType))
            {
                return false;
            }

            _filename = args[0];

            return File.Exists(_filename);
        }

        public float read_32bitSngl()
        {
            byte[] b32 = new byte[8];

            br.Read(b32, 0, 4);
            if (BitConverter.IsLittleEndian)
            {
                b32.Reverse();
            }
            return BitConverter.ToSingle(b32, 0);

        }

        public uint read_32bitInt()
        {
            byte[] b32 = new byte[4];

            br.Read(b32, 0, 4);
            if (BitConverter.IsLittleEndian)
            {
                b32.Reverse();
            }
            return (uint)BitConverter.ToInt32(b32, 0);

        }

        public float[] get_vector(int size)
        {
            float[] v = new float[size];

            for (int i = 0; i < size; i++)
            {
                v[i] = read_32bitSngl();
            }

            return v;
        }

        public double read_triangle()
        {
        
            byte[] bytecount = new byte[2];

            float[] n = get_vector(3);
            float[] p1 = get_vector(3);
            float[] p2 = get_vector(3);
            float[] p3 = get_vector(3);
            
            br.Read(bytecount, 0, 2);

            return signedVolumeOfTriangle(p1, p2, p3);
        }

        public double CalculateVolume2(string unit)
        {
            br = new BinaryReader(File.Open(_filename, FileMode.Open));
            byte[] buffer = new byte[128];

            br.Read(buffer, 0, 80);

            uint nTriangles = read_32bitInt();
            double totalVolume = 0;
            for(int i=0;i < nTriangles; i++) {
                totalVolume += read_triangle();
            }
            totalVolume = totalVolume / 1000;
            if (unit =="inch")
            {
                totalVolume = cm3_To_inch3Transform(totalVolume);
            }
            return totalVolume;
        }


        private double cm3_To_inch3Transform(double totalVol)
        {
            return totalVol * 0.0610237441;
        }

        public double signedVolumeOfTriangle(float[] p1, float[] p2, float[] p3)
        {
            double v321 = p3[0] * p2[1] * p1[2];
            double v231 = p2[0] * p3[1] * p1[2];
            double v312 = p3[0] * p1[1] * p2[2];
            double v132 = p1[0] * p3[1] * p2[2];
            double v213 = p2[0] * p1[1] * p3[2];
            double v123 = p1[0] * p2[1] * p3[2];
            return (1.0 / 6.0) * (-v321 + v231 + v312 - v132 - v213 + v123);
        }
    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            STLUtils stlUtils = new STLUtils();
            if (stlUtils.IsValidated(new string[] { @"G:\Projects\Sandbox\WebApplication1\WebApplication1\App_Data\cellphone.stl", "cm" }))
            {
                double volume = stlUtils.CalculateVolume2("cm");
                Response.Write(volume);
            }
        }
    }
}

Open in new window

0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 39231055
It's working.

Thank you very much for help.

ayha
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39231196
You are welcome - thanks for the points.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39231218
Hi julianH,

A small issue there, please have a look at the following question.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_28151548.html

thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question