Skip to content

Commit

Permalink
Added:
Browse files Browse the repository at this point in the history
  - headers
  - scalafmt
  - documentation
  - examples to README.md
  - writeMTL method in MTL
  • Loading branch information
c committed Aug 23, 2023
1 parent aaa8d5e commit 2e2c29b
Show file tree
Hide file tree
Showing 14 changed files with 406 additions and 2 deletions.
182 changes: 181 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,183 @@
# mesh
   A cross compiled Scala 3 library that provides accessible functionality to generate, manipulate, and export 3D mesh objects. Currently supports .PLY, and .OBJ/.MTL file formats.
   A cross compiled Scala 3 library that provides accessible functionality to generate, manipulate, and export triangular 3D mesh objects. Currently supports .PLY, and .OBJ/.MTL file formats.

<h3>sbt</h3>
```scala
libraryDependencies += "ai.dragonfly" %%% "mesh" % "<LATEST_VERSION>"
```

<h3>Features:</h3>
- Fast, lightweight mesh generation for:
- Plane
- Cube
- Cylinder
- Drum
- Threaded Bolt
- Threaded Screw
- Mesh file export to formats:
- PLY
- OBJ + MTL
- Compiles to Scala JVM, Native, and JavaScript environments.

<h3>Examples:</h3>

```scala
// include these imports for all examples
import ai.dragonfly.math.vector.*
import ai.dragonfly.math.vector.Vec.*
import ai.dragonfly.mesh.shape.*
```
<h4>Plane</h4>
```scala
// Plane
Plane(
Vec[3](0, 0, 1), // first corner
Vec[3](4, 0, 2), // second corner
Vec[3](0, 4, 3), // third corner
9, // vertical segment count
12 // horizontal segment count
)
```
<h4>Cube</h4>
```scala
Cube.minimal( 1.0 /* side length */ ) // minimal

Cube() // default parameters

Cube(
l = 1.0, // side length
n = 64, // number of segments
name = "Cube" // mesh name
)
```
<h4>Cylinder</h4>
```scala
Cylinder() // default parameters

Cylinder(
angularSegments = 36,
sideSegments = 1,
baseSegments = 1,
capSegments = 1,
radius = 1.0,
height = 1.0
)
```
<h4>Drum</h4>
```scala
Drum() // default parameters

Drum(
angularSegments = 12,
sideSegments = 1,
baseSegments = 1,
capSegments = 1,
baseRadius = 2.0,
capRadius = 1.0,
height = 1.0,
name = "Drum"
)
```
<h4>Threaded Bolt</h4>
```scala
Bolt() // default parameters

Bolt(
length = 10.0,
threadsPerUnit = 3.0,
threadThickness = 0.1,
shankLength = 3.5,
angularSegments = 12,
threadRadius = 1.0,
coreRadius = 0.85,
name = "Bolt"
)
```
<h4>Threaded Screw</h4>
```scala
Screw() // default parameters

Screw(
length = 7.0,
threadsPerUnit = 2.0,
threadThickness = 0.05,
shankLength = 1.5,
pointLength = 0.5,
angularSegments = 12,
threadRadius = 0.375,
coreRadius = 0.25,
name = "Screw"
)
```

<h3>Exporting to File Formats:</h3>
&nbsp;&nbsp;&nbsp;The examples below demonstrate how to convert a mesh to a string that represents a PLY, MTL, or OBJ file, or how to write the mesh directly to an OutputStream.

<h3>PLY:</h3>

```scala
import ai.dragonfly.mesh.shape.*
import ai.dragonfly.mesh.io.PLY

val ply:String = PLY.fromMesh( // generate a PLY file as a String
Cube(), // a mesh
vertexColorMapper = PLY.randomVertexColorMapper // a way to color the vertices.
)

val os:java.io.OutputStream = ...

PLY.writeMesh( // write directly to an output Stream
mesh = Cube(), // a mesh
out = os, // an output stream
vertexColorMapper = PLY.randomVertexColorMapper // a way to color the vertices.
)
```


<h3>MTL:</h3>
```scala
import ai.dragonfly.mesh.io.MTL

val material = MTL(
name,
diffuse,
ambient = gray,
specular = Specular(),
dissolve = 1f
)

val mtl:String = material.mtl // generate an MTL file as a String


val os:java.io.OutputStream = ...

MTL.writeMTL(material, os) // write directly to an output Stream
```

<h3>OBJ:</h3>
```scala
import ai.dragonfly.mesh.shape.*
import ai.dragonfly.mesh.io.OBJ

val cube:Mesh = Cube()

val obj:String = OBJ.fromMesh( // generate an OBJ file as a String
mesh = cube,
name = cube.name,
comment = "cube",
materialLibraryFile = "default.MTL",
material = material,
smooth = true
)

val os:java.io.OutputStream = ...

OBJ.writeMesh( // write directly to an output Stream
mesh = cube,
out = os,
name = cube.name,
comment = "cube",
materialLibraryFileName = "default.MTL",
material = material
)
```
16 changes: 16 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/Mesh.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh

import narr.*
Expand Down
16 changes: 16 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/Triangle.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh

import narr.*
Expand Down
18 changes: 18 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/io/MTL.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh.io

import ai.dragonfly.mesh.sRGB.*
Expand All @@ -8,6 +24,8 @@ import scala.scalajs.js.annotation.{JSExportAll, JSExportTopLevel}
object MTL {
val gray:ARGB32 = ARGB32(128, 128, 128)
val default:MTL = MTL("metal", gray)

def writeMTL(material: MTL, out: java.io.OutputStream): Unit = out.write(material.mtl.getBytes)
}

@JSExportAll
Expand Down
18 changes: 17 additions & 1 deletion mesh/shared/src/main/scala/ai/dragonfly/mesh/io/OBJ.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh.io

import narr.*
Expand Down Expand Up @@ -165,7 +181,7 @@ object OBJ {
// line = br.readLine()
// }
// br.close()
// val pointsArr:NArray[Vec[3]] = NArray.tabulate[Vec[3]](points.size)((i:Int) => points(i))
// val pointsArr:NArray[Vector3] = NArray.tabulate[Vector3](points.size)((i:Int) => points(i))
// val triangleArr:NArray[Triangle] = NArray.tabulate[Triangle](triangles.size)((i:Int) => triangles(i))
//
// MaterialMeshGroup(MTL.default, )
Expand Down
16 changes: 16 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/io/PLY.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh.io

import ai.dragonfly.math.vector.*
Expand Down
16 changes: 16 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/sRGB.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh

import scala.scalajs.js.annotation.{JSExportAll, JSExportTopLevel}
Expand Down
16 changes: 16 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/shape/Bolt.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh.shape

import narr.*
Expand Down
21 changes: 21 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/shape/Cube.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh.shape

import narr.*
Expand Down Expand Up @@ -65,6 +81,11 @@ object Cube {
points
}

/**
* Create a cube with minimal number of triangles and points.
* @param l the length of the cube.
* @return a triangular cube mesh.
*/
@JSExportTopLevel("MinimalCube")
def minimal(l:Double = 1.0): Mesh = apply(l, 2) // apply(l, 1)

Expand Down
16 changes: 16 additions & 0 deletions mesh/shared/src/main/scala/ai/dragonfly/mesh/shape/Cylinder.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ai.dragonfly.mesh.shape

import narr.*
Expand Down
Loading

0 comments on commit 2e2c29b

Please sign in to comment.