Solved

C# code

Posted on 2013-06-06
8
343 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 51

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 51

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
 
LVL 7

Author Comment

by:ayha1999
ID: 39228497
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 51

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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
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. …
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now