Solved

C# code

Posted on 2013-06-06
8
356 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 56

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 56

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 7

Author Comment

by:ayha1999
ID: 39228497
0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 500 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 56

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

Suggested Solutions

Title # Comments Views Activity
Python script with password security? 15 133
Python tuples 2 133
Python frameworks - maybe Django. 4 112
yara deployment 8 105
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

733 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