Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# code

Posted on 2013-06-06
8
Medium Priority
?
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 59

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 59

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
Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 
LVL 7

Author Comment

by:ayha1999
ID: 39228497
0
 
LVL 59

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 59

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

610 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