Skip to content

Commit

Permalink
3d math and model source files
Browse files Browse the repository at this point in the history
  • Loading branch information
xieguigang committed Feb 13, 2023
1 parent 570138c commit 10a71a4
Show file tree
Hide file tree
Showing 23 changed files with 2,084 additions and 528 deletions.
15 changes: 14 additions & 1 deletion Microsoft.VisualBasic.Core/src/Language/StringHelpers.vb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,21 @@ Namespace Language
''' </summary>
Public Module FormatHelpers

''' <summary>
''' Removes all leading and trailing occurrences of a set of characters specified
''' in an array from the current string.
''' </summary>
''' <param name="str"></param>
''' <param name="c">An array of Unicode characters to remove, or null.</param>
''' <returns>
''' The string that remains after all occurrences of the characters in the trimChars
''' parameter are removed from the start and end of the current string. If trimChars
''' is null or an empty array, white-space characters are removed instead. If no
''' characters can be trimmed from the current instance, the method returns the current
''' instance unchanged.
''' </returns>
<Extension>
Public Function Trim(str As Value(Of String), c As Char()) As String
Public Function Trim(str As Value(Of String), ParamArray c As Char()) As String
If str Is Nothing OrElse str.Value Is Nothing Then
Return ""
Else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,22 @@ Namespace Language.Values
Return list.Value.First
End Function

''' <summary>
''' get the first char
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Public Function First(str As ByRefString) As Char
Return str.Value.First
If str Is Nothing OrElse
str.Value Is Nothing OrElse
str.Value.Length = 0 Then

Return Nothing
Else
Return str.Value.First
End If
End Function

<MethodImpl(MethodImplOptions.AggressiveInlining)>
Expand Down
3 changes: 3 additions & 0 deletions gr/Landscape/3DBuilder/XML/models.vb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ Namespace Vendor_3mf.XML
End Function
End Class

''' <summary>
''' 3 vertex index to create a triangle of the surface model data
''' </summary>
Public Class triangle

<XmlAttribute> Public Property v1 As Integer
Expand Down
185 changes: 59 additions & 126 deletions gr/Landscape/PLY/Header.vb
Original file line number Diff line number Diff line change
@@ -1,73 +1,69 @@
#Region "Microsoft.VisualBasic::050e16ba84ef1932bc43eda21a2e551e, sciBASIC#\gr\Landscape\PLY\Header.vb"

' Author:
'
' asuka ([email protected])
' xie ([email protected])
' xieguigang ([email protected])
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.



' /********************************************************************************/

' Summaries:


' Code Statistics:

' Total Lines: 94
' Code Lines: 76
' Comment Lines: 0
' Blank Lines: 18
' File Size: 3.52 KB


' Class Header
'
' Properties: comment, element_face, element_vertex, properties
'
' Sub: WriteAsciiText
'
' Module SimplePlyWriter
'
' Function: WriteAsciiText
'
' Class PointCloud
'
' Properties: color, intensity, x, y, z
'
' Function: ToString
'
'
' /********************************************************************************/
' Author:
'
' asuka ([email protected])
' xie ([email protected])
' xieguigang ([email protected])
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.



' /********************************************************************************/

' Summaries:


' Code Statistics:

' Total Lines: 94
' Code Lines: 76
' Comment Lines: 0
' Blank Lines: 18
' File Size: 3.52 KB


' Class Header
'
' Properties: comment, element_face, element_vertex, properties
'
' Sub: WriteAsciiText
'
' Module SimplePlyWriter
'
' Function: WriteAsciiText
'
' Class PointCloud
'
' Properties: color, intensity, x, y, z
'
' Function: ToString
'
'
' /********************************************************************************/

#End Region

Imports System.Drawing
Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
Imports Microsoft.VisualBasic.Imaging.Drawing2D.Colors

Namespace Ply

Expand All @@ -91,68 +87,5 @@ Namespace Ply

Call file.WriteLine($"end_header")
End Sub

End Class

Public Module SimplePlyWriter

Public Function WriteAsciiText(pointCloud As IEnumerable(Of PointCloud),
buffer As Stream,
Optional colors As ScalerPalette = ScalerPalette.turbo,
Optional levels As Integer = 200) As Boolean

Using file As New StreamWriter(buffer, Encoding.ASCII) With {
.AutoFlush = True,
.NewLine = vbLf
}
Dim vertex As PointCloud() = pointCloud.ToArray
Dim header As New Header With {
.comment = "Point Cloud Model",
.element_face = -1,
.element_vertex = vertex.Length,
.properties = {
New NamedValue(Of String)("x", "float"),
New NamedValue(Of String)("y", "float"),
New NamedValue(Of String)("z", "float"),
New NamedValue(Of String)("intensity", "float"),
New NamedValue(Of String)("color", "string")
}
}
Dim colorSet As String() = Designer _
.GetColors(colors.Description, levels) _
.Select(Function(c) c.ToHtmlColor) _
.ToArray
Dim scale As New DoubleRange(From v As PointCloud In vertex Select v.intensity)
Dim i As Integer
Dim index As New DoubleRange(0, levels - 1)
Dim clr As String

Call header.WriteAsciiText(file)
Call file.Flush()

For Each point As PointCloud In vertex
i = scale.ScaleMapping(point.intensity, index)
clr = colorSet(i)

Call file.WriteLine($"{point.x.ToString("F3")} {point.y.ToString("F3")} {point.z.ToString("F3")} {point.intensity} {clr}")
Next
End Using

Return True
End Function
End Module

Public Class PointCloud

Public Property x As Double
Public Property y As Double
Public Property z As Double
Public Property color As String
Public Property intensity As Double

Public Overrides Function ToString() As String
Return $"[{x},{y},{z}] {intensity}"
End Function

End Class
End Namespace
16 changes: 16 additions & 0 deletions gr/Landscape/PLY/PointCloud.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Namespace Ply

Public Class PointCloud

Public Property x As Double
Public Property y As Double
Public Property z As Double
Public Property color As String
Public Property intensity As Double

Public Overrides Function ToString() As String
Return $"[{x},{y},{z}] {intensity}"
End Function

End Class
End Namespace
56 changes: 56 additions & 0 deletions gr/Landscape/PLY/SimplePlyWriter.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
Imports Microsoft.VisualBasic.Imaging.Drawing2D.Colors
Imports System.IO
Imports System.Text

Namespace Ply

Public Module SimplePlyWriter

Public Function WriteAsciiText(pointCloud As IEnumerable(Of PointCloud),
buffer As Stream,
Optional colors As ScalerPalette = ScalerPalette.turbo,
Optional levels As Integer = 200) As Boolean

Using file As New StreamWriter(buffer, Encoding.ASCII) With {
.AutoFlush = True,
.NewLine = vbLf
}
Dim vertex As PointCloud() = pointCloud.ToArray
Dim header As New Header With {
.comment = "Point Cloud Model",
.element_face = -1,
.element_vertex = vertex.Length,
.properties = {
New NamedValue(Of String)("x", "float"),
New NamedValue(Of String)("y", "float"),
New NamedValue(Of String)("z", "float"),
New NamedValue(Of String)("intensity", "float"),
New NamedValue(Of String)("color", "string")
}
}
Dim colorSet As String() = Designer _
.GetColors(colors.Description, levels) _
.Select(Function(c) c.ToHtmlColor) _
.ToArray
Dim scale As New DoubleRange(From v As PointCloud In vertex Select v.intensity)
Dim i As Integer
Dim index As New DoubleRange(0, levels - 1)
Dim clr As String

Call header.WriteAsciiText(file)
Call file.Flush()

For Each point As PointCloud In vertex
i = scale.ScaleMapping(point.intensity, index)
clr = colorSet(i)

Call file.WriteLine($"{point.x.ToString("F3")} {point.y.ToString("F3")} {point.z.ToString("F3")} {point.intensity} {clr}")
Next
End Using

Return True
End Function
End Module
End Namespace
10 changes: 10 additions & 0 deletions gr/Landscape/Wavefront/FileName.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Namespace Wavefront

Public Class Triangle

Public Property v3 As Integer()
Public Property vn3 As Integer()
Public Property comment As String

End Class
End Namespace
49 changes: 49 additions & 0 deletions gr/Landscape/Wavefront/OBJ.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Imports System.IO
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.Imaging.Drawing3D
Imports Microsoft.VisualBasic.Language

Namespace Wavefront

Public Class OBJ

''' <summary>
''' lib file name of mtl data
''' </summary>
''' <returns></returns>
Public Property mtllib As String
Public Property parts As ObjectPart()
Public Property comment As String

<MethodImpl(MethodImplOptions.AggressiveInlining)>
<DebuggerStepThrough>
Public Shared Function ReadFile(file As StreamReader) As OBJ
Return TextParser.ParseFile(file)
End Function

End Class

Public Class ObjectPart

Public Property g As String
Public Property vertex As Point3D()
Public Property vn As Point3D()
Public Property usemtl As String
Public Property f As Triangle()

Public ReadOnly Property IsEmpty As Boolean
Get
Return g.StringEmpty AndAlso
vertex.IsNullOrEmpty AndAlso
vn.IsNullOrEmpty AndAlso
usemtl.StringEmpty AndAlso
f.IsNullOrEmpty
End Get
End Property

Public Overrides Function ToString() As String
Return $"{g Or "no_label".AsDefault}: {vertex.Length} vertexs and {f.Length} triangles"
End Function

End Class
End Namespace
Loading

0 comments on commit 10a71a4

Please sign in to comment.