├── .gitignore
├── LICENSE
├── README.md
├── Vtk2Obj.sln
└── Vtk2Obj
├── App.config
├── CompressedObjWriter.cs
├── Program.cs
├── Properties
└── AssemblyInfo.cs
├── SignificantDigits.cs
├── Vtk2Obj.csproj
└── format.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # .NET Core
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 | **/Properties/launchSettings.json
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # Visual Studio code coverage results
117 | *.coverage
118 | *.coveragexml
119 |
120 | # NCrunch
121 | _NCrunch_*
122 | .*crunch*.local.xml
123 | nCrunchTemp_*
124 |
125 | # MightyMoose
126 | *.mm.*
127 | AutoTest.Net/
128 |
129 | # Web workbench (sass)
130 | .sass-cache/
131 |
132 | # Installshield output folder
133 | [Ee]xpress/
134 |
135 | # DocProject is a documentation generator add-in
136 | DocProject/buildhelp/
137 | DocProject/Help/*.HxT
138 | DocProject/Help/*.HxC
139 | DocProject/Help/*.hhc
140 | DocProject/Help/*.hhk
141 | DocProject/Help/*.hhp
142 | DocProject/Help/Html2
143 | DocProject/Help/html
144 |
145 | # Click-Once directory
146 | publish/
147 |
148 | # Publish Web Output
149 | *.[Pp]ublish.xml
150 | *.azurePubxml
151 | # TODO: Comment the next line if you want to checkin your web deploy settings
152 | # but database connection strings (with potential passwords) will be unencrypted
153 | *.pubxml
154 | *.publishproj
155 |
156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
157 | # checkin your Azure Web App publish settings, but sensitive information contained
158 | # in these scripts will be unencrypted
159 | PublishScripts/
160 |
161 | # NuGet Packages
162 | *.nupkg
163 | # The packages folder can be ignored because of Package Restore
164 | **/packages/*
165 | # except build/, which is used as an MSBuild target.
166 | !**/packages/build/
167 | # Uncomment if necessary however generally it will be regenerated when needed
168 | #!**/packages/repositories.config
169 | # NuGet v3's project.json files produces more ignorable files
170 | *.nuget.props
171 | *.nuget.targets
172 |
173 | # Microsoft Azure Build Output
174 | csx/
175 | *.build.csdef
176 |
177 | # Microsoft Azure Emulator
178 | ecf/
179 | rcf/
180 |
181 | # Windows Store app package directories and files
182 | AppPackages/
183 | BundleArtifacts/
184 | Package.StoreAssociation.xml
185 | _pkginfo.txt
186 |
187 | # Visual Studio cache files
188 | # files ending in .cache can be ignored
189 | *.[Cc]ache
190 | # but keep track of directories ending in .cache
191 | !*.[Cc]ache/
192 |
193 | # Others
194 | ClientBin/
195 | ~$*
196 | *~
197 | *.dbmdl
198 | *.dbproj.schemaview
199 | *.jfm
200 | *.pfx
201 | *.publishsettings
202 | orleans.codegen.cs
203 |
204 | # Since there are multiple workflows, uncomment next line to ignore bower_components
205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
206 | #bower_components/
207 |
208 | # RIA/Silverlight projects
209 | Generated_Code/
210 |
211 | # Backup & report files from converting an old project file
212 | # to a newer Visual Studio version. Backup files are not needed,
213 | # because we have git ;-)
214 | _UpgradeReport_Files/
215 | Backup*/
216 | UpgradeLog*.XML
217 | UpgradeLog*.htm
218 |
219 | # SQL Server files
220 | *.mdf
221 | *.ldf
222 | *.ndf
223 |
224 | # Business Intelligence projects
225 | *.rdl.data
226 | *.bim.layout
227 | *.bim_*.settings
228 |
229 | # Microsoft Fakes
230 | FakesAssemblies/
231 |
232 | # GhostDoc plugin setting file
233 | *.GhostDoc.xml
234 |
235 | # Node.js Tools for Visual Studio
236 | .ntvs_analysis.dat
237 | node_modules/
238 |
239 | # Typescript v1 declaration files
240 | typings/
241 |
242 | # Visual Studio 6 build log
243 | *.plg
244 |
245 | # Visual Studio 6 workspace options file
246 | *.opt
247 |
248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
249 | *.vbw
250 |
251 | # Visual Studio LightSwitch build output
252 | **/*.HTMLClient/GeneratedArtifacts
253 | **/*.DesktopClient/GeneratedArtifacts
254 | **/*.DesktopClient/ModelManifest.xml
255 | **/*.Server/GeneratedArtifacts
256 | **/*.Server/ModelManifest.xml
257 | _Pvt_Extensions
258 |
259 | # Paket dependency manager
260 | .paket/paket.exe
261 | paket-files/
262 |
263 | # FAKE - F# Make
264 | .fake/
265 |
266 | # JetBrains Rider
267 | .idea/
268 | *.sln.iml
269 |
270 | # CodeRush
271 | .cr/
272 |
273 | # Python Tools for Visual Studio (PTVS)
274 | __pycache__/
275 | *.pyc
276 |
277 | # Cake - Uncomment if you are using it
278 | # tools/**
279 | # !tools/packages.config
280 |
281 | # Telerik's JustMock configuration file
282 | *.jmconfig
283 |
284 | # BizTalk build output
285 | *.btp.cs
286 | *.btm.cs
287 | *.odx.cs
288 | *.xsd.cs
289 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 davidbirchwork
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Vtk2Obj
2 | Convert Paraview .Vtk files to Wavefront .Obj files for 3d visualisation in game engines.
3 |
4 | Currently supports only conversion of triangulated meshes which must be only a shell (i.e. not a volumetric mesh).
5 |
6 | Will process either single or folders of .vtk files.
7 |
8 | Suggested usage - create an isosurface across the mesh and export this as a triangulated mesh in .vtk.
9 |
10 | # Instruction for ParaView Version 5.0.1
11 |
12 | Creation of the geoemtry *.vtk file:
13 | - Open one paraview file
14 | - Unselect all fields in Properties
15 | - Filter - Search - Extract Surface - Enter
16 | - Be sure that the Extract Surface is selected in the Pipeline Browser
17 | - Filter - Search - Generate Surface Normals - Enter
18 | - In Properties: Select "Compute Cell Normals" - Apply
19 | - Be sure that the Generate Surface Normals is selected in the Pipeline Browser
20 | - File - Save Data - Files of type : Legacy VTK Files (*.vtk) - Save
21 | - File type: ASCII - OK
22 |
23 | # Creation of the *.vtk isosurface files :
24 | - Open paraview files
25 | - Select only the field of interest in Properties
26 | - Filter - Contour
27 | - In Properties: Select "Compute Scalars" and choose the desired value of the isosurface - Apply
28 | - Be sure that Contour is selected in the Pipeline Browser
29 | - Filter - Search - Extract Surface - Enter
30 | - Be sure that the Extract Surface is selected in the Pipeline Browser
31 | - Filter - Search - Generate Surface Normals - Enter
32 | - In Properties: Select "Compute Cell Normals" - Apply
33 | - Be sure that the Generate Surface Normals is selected in the Pipeline Browser
34 | - File - Save Data - Files of type : Legacy VTK Files (*.vtk) - Save
35 | -Select "Write all timesteps as file-series" - File type: ASCII - OK
36 |
--------------------------------------------------------------------------------
/Vtk2Obj.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vtk2Obj", "Vtk2Obj\Vtk2Obj.csproj", "{D1700F98-BA51-4F0D-820E-FF21FD40C137}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {D1700F98-BA51-4F0D-820E-FF21FD40C137}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {D1700F98-BA51-4F0D-820E-FF21FD40C137}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {D1700F98-BA51-4F0D-820E-FF21FD40C137}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {D1700F98-BA51-4F0D-820E-FF21FD40C137}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/Vtk2Obj/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Vtk2Obj/CompressedObjWriter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace IsoSurfaceToObj {
8 | ///
9 | /// TODO this is an optimised .obj writer using LINQ, it will compress the .obj be recycling normals between faces
10 | ///
11 | class CompressedObjWriter {
12 | /* public static Tuple, List, List> WriteObjFile(IEnumerable triangles, bool includeNormals, string groupname, string material,
13 | List vertexes, List normals, List textures) {
14 | StringBuilder file = new StringBuilder();
15 |
16 | int vertCount = vertexes.Count;
17 | int normalCount = normals.Count;
18 | int texCount = textures.Count;
19 |
20 | // header
21 | file.AppendLine("# object generated from shape file");
22 | file.AppendLine();
23 | file.AppendLine("g " + groupname);// todo should we set each building as a separate grouping?
24 | file.AppendLine("usemtl " + material);
25 | List faces = new List();
26 |
27 | foreach (Triangle triangle in triangles) {
28 | string face = "f ";
29 |
30 | face += vertexes.IndexOfOrInsertFrom1(triangle.Pt1) + "/" + textures.IndexOfOrInsertFrom1(triangle.Pt1T) +
31 | (includeNormals ? "/" + normals.IndexOfOrInsertFrom1(triangle.Pt1N) + " " : " ");
32 | face += vertexes.IndexOfOrInsertFrom1(triangle.Pt2) + "/" + textures.IndexOfOrInsertFrom1(triangle.Pt2T) +
33 | (includeNormals ? "/" + normals.IndexOfOrInsertFrom1(triangle.Pt2N) + " " : " ");
34 | face += vertexes.IndexOfOrInsertFrom1(triangle.Pt3) + "/" + textures.IndexOfOrInsertFrom1(triangle.Pt3T) +
35 | (includeNormals ? "/" + normals.IndexOfOrInsertFrom1(triangle.Pt3N) + " " : " ");
36 | faces.Add(face);
37 | }
38 |
39 | file.AppendLine();
40 | foreach (Vertex vertex in vertexes.Skip(vertCount)) {
41 | file.AppendLine("v " + vertex.X + " " + vertex.Y + " " + vertex.Z);
42 | }
43 |
44 | if (includeNormals) {
45 | file.AppendLine();
46 | foreach (Vertex vertex in normals.Skip(normalCount)) {
47 | file.AppendLine("vn " + vertex.X + " " + vertex.Y + " " + vertex.Z);
48 | }
49 | }
50 |
51 | file.AppendLine();
52 | foreach (Vertex vertex in textures.Skip(texCount)) {
53 | file.AppendLine("vt " + vertex.X + " " + vertex.Y + " " + vertex.Z);
54 | }
55 |
56 | file.AppendLine();
57 | foreach (string face in faces) {
58 | file.AppendLine(face);
59 | }
60 |
61 | return new Tuple, List, List>(file.ToString(), vertexes, normals, textures);
62 | }*/
63 |
64 | }
65 |
66 | public static class ListExtentions {
67 |
68 | public static int IndexOfOrInsertFrom1(this List list, T obj2Find) {
69 | int index = list.IndexOf(obj2Find);
70 | if (index != -1) {
71 | return index + 1;
72 | }
73 | list.Add(obj2Find);
74 | return list.IndexOf(obj2Find) + 1;
75 |
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/Vtk2Obj/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace IsoSurfaceToObj {
9 | class Program {
10 | private static bool swapYZ = true;
11 | static void Main(string[] args) {
12 | //settings - precision - set in significatn figutres Digits
13 | // Swap YZ
14 |
15 | if (args.Length != 1) {
16 | Console.WriteLine("requires a .vtk file or a directory as an argument");
17 | return;
18 | }
19 |
20 | string vtkfile = args[0];
21 | if (File.Exists(vtkfile)) {
22 | ConvertVtkFile(vtkfile);
23 | }
24 | else {
25 | if (Directory.Exists(vtkfile)) {
26 | Parallel.ForEach(Directory.GetFiles(vtkfile), ConvertVtkFile);
27 | //foreach (var file in ) {
28 | // ConvertVtkFile(file);
29 | //}
30 | }
31 | }
32 |
33 | Console.ReadLine();
34 |
35 | }
36 |
37 | private static void ConvertVtkFile(string vtkfile) {
38 | using (var file = File.ReadLines(vtkfile).GetEnumerator()) {
39 | Console.WriteLine("Starting " + vtkfile);
40 |
41 | if (!ReadUntilLine(file, "# vtk DataFile Version 4.2")) return;
42 | if (!ReadUntilLine(file, "ASCII")) return;
43 | if (!ReadUntilLine(file, "DATASET POLYDATA")) return;
44 |
45 | if (!ReadUntilLineStartsWith(file, "POINTS")) return;
46 |
47 | double[,] points = ReadPoints(file);
48 |
49 | if (!ReadUntilLineStartsWith(file, "POLYGONS")) return;
50 |
51 | int[,] triangles = ReadTriangles(file);
52 |
53 | if (!ReadUntilLineStartsWith(file, "NORMALS")) return;
54 | file.MoveNext();
55 | double[,] normals = ReadDoublePoints(file, triangles.GetLength(0));
56 |
57 | Console.WriteLine("Generating Obj");
58 | var objfile = WriteAnObjFile(points, normals, null, triangles);
59 |
60 | string objFilePath = Path.ChangeExtension(vtkfile, ".obj");
61 | Console.WriteLine("Writing Obj File");
62 | File.WriteAllText(objFilePath, objfile.ToString());
63 | Console.WriteLine("Finished " + objFilePath);
64 | }
65 | }
66 |
67 | private static StringBuilder WriteAnObjFile(double[,] points, double[,] normals, double[,] textures, int[,] triangles,
68 | string groupname = "mesh", string material = "mesh") {
69 |
70 | // todo note that we are not checking uniqueness of the normals or the texture points, we assume this wont make a difference... see the CompressedObjWriter for something that will
71 |
72 | StringBuilder file = new StringBuilder();
73 |
74 | // header
75 | file.AppendLine("# object generated from DAvid Birch vtk to obj");
76 | file.AppendLine("mtllib materials.mtl");//todo set this to your library.
77 |
78 | file.AppendLine();
79 | for (int v = 0; v < points.GetLength(0); v++) {
80 | file.AppendLine("v " + points[v, 0].ToPreciseString() + " " + points[v, swapYZ? 2 : 1].ToPreciseString() + " " + points[v, swapYZ ? 1 : 2].ToPreciseString());
81 | }
82 |
83 |
84 | if (normals != null) {
85 | file.AppendLine();
86 | for (int v = 0; v < normals.GetLength(0); v++) {
87 | file.AppendLine("vn " + normals[v, 0].ToPreciseString() + " " + normals[v, swapYZ ? 2 : 1].ToPreciseString() + " " + normals[v, swapYZ ? 1 : 2].ToPreciseString());
88 | }
89 | }
90 |
91 | if (textures != null) {
92 | file.AppendLine();
93 | for (int v = 0; v < textures.GetLength(0); v++) {
94 | file.AppendLine("vt " + textures[v, 0].ToPreciseString() + " " + textures[v, swapYZ ? 2 : 1].ToPreciseString() + " " + textures[v, swapYZ ? 1 : 2].ToPreciseString());
95 | }
96 | }
97 |
98 |
99 | file.AppendLine();
100 | file.AppendLine("g " + groupname);
101 | file.AppendLine("usemtl " + material);
102 |
103 |
104 | for (int t = 0; t < triangles.GetLength(0); t++) {
105 | // note indexes are 1 based
106 | file.AppendLine("f "
107 | + (triangles[t, 0]+1) + "/" + (textures == null ? "" : (t+1).ToString()) + (normals == null ? "" : "/" + (t+1)) + " "
108 | + (triangles[t, 1]+1) + "/" + (textures == null ? "" : (t+1).ToString()) + (normals == null ? "" : "/" + (t+1)) + " "
109 | + (triangles[t, 2]+1) + "/" + (textures == null ? "" : (t+1).ToString()) + (normals == null ? "" : "/" + (t+1)) );
110 | }
111 |
112 | return file;
113 | }
114 |
115 |
116 | private static int[,] ReadTriangles(IEnumerator file) {
117 | Console.WriteLine("Parsing Triangles");
118 | var header = file.Current;
119 | int numberofTriangles = int.Parse(header.Split(new[] { " " }, StringSplitOptions.None)[1]);// todo error handling
120 | // POLYGONS 1504 6016
121 | int[,] points = new int[numberofTriangles, 3];
122 |
123 | file.MoveNext();
124 |
125 | int curr = 0;
126 |
127 | while (file.Current != string.Empty && curr file) {
153 | Console.WriteLine("Parsing Points");
154 | var header = file.Current;
155 | int numberofPoints = int.Parse(header.Split(new[] {" "}, StringSplitOptions.None)[1]);// todo error handling
156 | // POINTS 1629 double
157 |
158 |
159 | file.MoveNext();
160 |
161 | return ReadDoublePoints(file, numberofPoints);
162 | }
163 |
164 | private static double[,] ReadDoublePoints(IEnumerator file, int numberofPoints) {
165 | Console.WriteLine("Parsing "+numberofPoints+" 3d points as doubles");
166 | double[,] points = new double[numberofPoints, 3];
167 | int curr = 0;
168 |
169 | while (file.Current != string.Empty && curr file, string line) {
199 | Console.WriteLine("Searching for line starting with "+line);
200 | while (!file.Current.StartsWith(line)) {
201 | if (!file.MoveNext()) {
202 | Console.WriteLine("could not find line starting with " + line);
203 | return false;
204 | }
205 |
206 | }
207 |
208 | return true;
209 | }
210 |
211 | private static bool ReadUntilLine(IEnumerator file, string line) {
212 | Console.WriteLine("Searching for line " + line);
213 |
214 | while (file.Current != line) {
215 | if (!file.MoveNext()) {
216 | Console.WriteLine("could not find line "+line);
217 | return false;
218 | }
219 |
220 | }
221 |
222 | return true;
223 | }
224 | }
225 | }
226 |
--------------------------------------------------------------------------------
/Vtk2Obj/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("IsoSurfaceToObj")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Hewlett-Packard")]
12 | [assembly: AssemblyProduct("IsoSurfaceToObj")]
13 | [assembly: AssemblyCopyright("Copyright © Hewlett-Packard 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("d1700f98-ba51-4f0d-820e-ff21fd40c137")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Vtk2Obj/SignificantDigits.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 |
4 | namespace IsoSurfaceToObj {
5 | ///
6 | /// https://stackoverflow.com/questions/374316/round-a-double-to-x-significant-figures
7 | ///
8 | public static class SignificantDigits {
9 | private static readonly string DecimalSeparator;
10 |
11 | static SignificantDigits() {
12 | System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
13 | DecimalSeparator = ci.NumberFormat.NumberDecimalSeparator;
14 | }
15 |
16 | ///
17 | /// Format a double to a given number of significant digits.
18 | ///
19 | ///
20 | /// 0.086 -> "0.09" (digits = 1)
21 | /// 0.00030908 -> "0.00031" (digits = 2)
22 | /// 1239451.0 -> "1240000" (digits = 3)
23 | /// 5084611353.0 -> "5085000000" (digits = 4)
24 | /// 0.00000000000000000846113537656557 -> "0.00000000000000000846114" (digits = 6)
25 | /// 50.8437 -> "50.84" (digits = 4)
26 | /// 50.846 -> "50.85" (digits = 4)
27 | /// 990.0 -> "1000" (digits = 1)
28 | /// -5488.0 -> "-5000" (digits = 1)
29 | /// -990.0 -> "-1000" (digits = 1)
30 | /// 0.0000789 -> "0.000079" (digits = 2)
31 | ///
32 | public static string ToPreciseString(this double number, int digits = 8, bool showTrailingZeros = false, bool alwaysShowDecimalSeparator = false) {
33 | if (Double.IsNaN(number) ||
34 | Double.IsInfinity(number)) {
35 | return number.ToString();
36 | }
37 |
38 | string sSign = "";
39 | string sBefore = "0"; // Before the decimal separator
40 | string sAfter = ""; // After the decimal separator
41 |
42 | if (number != 0d) {
43 | if (digits < 1) {
44 | throw new ArgumentException("The digits parameter must be greater than zero.");
45 | }
46 |
47 | if (number < 0d) {
48 | sSign = "-";
49 | number = Math.Abs(number);
50 | }
51 |
52 | // Use scientific formatting as an intermediate step
53 | string sFormatString = "{0:" + new String('#', digits) + "E0}";
54 | string sScientific = String.Format(sFormatString, number);
55 |
56 | string sSignificand = sScientific.Substring(0, digits);
57 | int exponent = Int32.Parse(sScientific.Substring(digits + 1));
58 | // (the significand now already contains the requested number of digits with no decimal separator in it)
59 |
60 | StringBuilder sFractionalBreakup = new StringBuilder(sSignificand);
61 |
62 | if (!showTrailingZeros) {
63 | while (sFractionalBreakup[sFractionalBreakup.Length - 1] == '0') {
64 | sFractionalBreakup.Length--;
65 | exponent++;
66 | }
67 | }
68 |
69 | // Place decimal separator (insert zeros if necessary)
70 |
71 | int separatorPosition = 0;
72 |
73 | if ((sFractionalBreakup.Length + exponent) < 1) {
74 | sFractionalBreakup.Insert(0, "0", 1 - sFractionalBreakup.Length - exponent);
75 | separatorPosition = 1;
76 | } else if (exponent > 0) {
77 | sFractionalBreakup.Append('0', exponent);
78 | separatorPosition = sFractionalBreakup.Length;
79 | } else {
80 | separatorPosition = sFractionalBreakup.Length + exponent;
81 | }
82 |
83 | sBefore = sFractionalBreakup.ToString();
84 |
85 | if (separatorPosition < sBefore.Length) {
86 | sAfter = sBefore.Substring(separatorPosition);
87 | sBefore = sBefore.Remove(separatorPosition);
88 | }
89 | }
90 |
91 | string sReturnValue = sSign + sBefore;
92 |
93 | if (sAfter == "") {
94 | if (alwaysShowDecimalSeparator) {
95 | sReturnValue += DecimalSeparator + "0";
96 | }
97 | } else {
98 | sReturnValue += DecimalSeparator + sAfter;
99 | }
100 |
101 | return sReturnValue.Substring(0, Math.Min(sReturnValue.Length, digits));
102 | }
103 | }
104 | }
--------------------------------------------------------------------------------
/Vtk2Obj/Vtk2Obj.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {D1700F98-BA51-4F0D-820E-FF21FD40C137}
8 | Exe
9 | Properties
10 | Vtk2Obj
11 | Vtk2Obj
12 | v4.5.2
13 | 512
14 | true
15 | false
16 | publish\
17 | true
18 | Disk
19 | false
20 | Foreground
21 | 7
22 | Days
23 | false
24 | false
25 | true
26 | 0
27 | 1.0.0.%2a
28 | false
29 | true
30 |
31 |
32 | AnyCPU
33 | true
34 | full
35 | false
36 | bin\Debug\
37 | DEBUG;TRACE
38 | prompt
39 | 4
40 |
41 |
42 | AnyCPU
43 | pdbonly
44 | true
45 | bin\Release\
46 | TRACE
47 | prompt
48 | 4
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 | False
72 | Microsoft .NET Framework 4.5.2 %28x86 and x64%29
73 | true
74 |
75 |
76 | False
77 | .NET Framework 3.5 SP1
78 | false
79 |
80 |
81 |
82 |
89 |
--------------------------------------------------------------------------------
/Vtk2Obj/format.txt:
--------------------------------------------------------------------------------
1 | POINTS 1629 double
2 | 12.505969516 -0.36315498129 0.20074518087 12.50631976 -0.36408118607 0.20000000298 12.506072403 -0.36333316825 0.20000000298
3 |
4 | POLYGONS 1504 6016
5 | 3 0 1 2
6 |
7 | CELL_DATA 1504
8 | NORMALS Normals float
9 | 0.94795 0.313471 0.0559264 0.472715 0.86959 -0.142669 0.472715 0.86959 -0.142669
10 |
11 | POINT_DATA 1629
12 | SCALARS TracerObelisk double
13 | LOOKUP_TABLE default
14 | 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001
15 |
16 | NORMALS Normals float
17 | 0.900985 0.425802 -0.083183 0.934314 0.356431 -0.00387905 0.94795 0.313471 0.0559264
--------------------------------------------------------------------------------