Skip to content

osbyrne/cadman

Folders and files

NameName
Last commit message
Last commit date

Latest commit

622a625 · Jan 24, 2025

History

56 Commits
Jan 23, 2025
Jan 23, 2025
Jan 23, 2025
Jan 3, 2025
Jan 3, 2025
Jan 23, 2025
Jan 24, 2025
Jan 24, 2025
Jan 3, 2025
Jan 24, 2025
Jan 24, 2025
Jan 23, 2025
Jan 24, 2025
Jan 24, 2025
Jan 23, 2025

Repository files navigation

Functional Programming in Scala 3 - Building a DSL

Table of Contents

Project Overview

This project demonstrates the creation of a Domain Specific Language (DSL) using Scala 3. The DSL is designed to define 3D shapes and generate STL (Stereolithography) files. STL files are a common format for representing 3D models in computer-aided design and 3D printing. The project leverages the power of Scala's functional programming paradigm and its advanced type system to create an expressive and intuitive way to describe geometric forms.

Domain Exploration

The domain of this project is 3D geometric modeling. The core concepts include:

  • Shapes: We define basic 3D shapes such as cubes, spheres, cylinders, and rectangles.
  • Points: 3D coordinates represented as Point(x, y, z).
  • Dimensions: Linear dimensions represented as Centimeter(value)
  • Tesselation: Triangulation of the shape.
  • Triangle : Represent a triangle using three points.

DSL Design Decisions

  • Functional Approach: Scala's support for functional programming allows for a declarative style, making the DSL expressive and composable.
  • Type Safety: The type system ensures correctness and prevents common errors, enhancing the robustness of the DSL.
  • Extensibility: New shapes and operations can be added easily, making it adaptable to future requirements.
  • Immutability: Data is immutable, which helps avoid side effects and promotes pure functions.
  • Case Classes The case class design pattern is used to represent data.

Usage Examples

Here are some examples of how to use the DSL to create various 3D shapes:

Creating a Cube:

scala val cube = new Cube(Point(0, 0, 0), Centimeter(10))

Creating a Sphere:

scala val sphere = new Sphere(Point(5, 5, 5), Centimeter(5))

Creating a Cylinder:

scala val cylinder = new Cylinder(Point(10, 10, 0), Centimeter(10), Centimeter(3))

View the objects

Project Structure

The project directory is organized as follows:

cadman/ src/main/scala/geometry/ Point.scala Centimeter.scala Shape.scala Cube.scala Sphere.scala Cylinder.scala Rectangle.scala project/

  • src/main/scala/geometry/: This directory contains the core code for the DSL, including classes representing shapes and data structures.
    • Point.scala: Defines the Point class representing 3D coordinates.
    • Centimeter.scala: Defines the Centimeter class for representing lengths.
    • Shape.scala: Defines the Shape trait, which all shapes extend.
    • Cube.scala: Implements the Cube shape.
    • Sphere.scala: Implements the Sphere shape.
    • Cylinder.scala: Implements the Cylinder shape.
    • Rectangle.scala: Implements the Rectangle shape.
  • project/: Contains SBT project configuration files.

Build and Run Instructions

  1. Prerequisites: Ensure you have Java 8+ and Scala 3 installed, along with sbt. See the official Scala and sbt websites for installation instructions.
  2. Navigate to the project directory:

bash cd cadman

  1. Run the project:

sbt run

Testing

This project could use a testing strategy based on ScalaTest or Specs2, a popular testing framework in the Scala ecosystem.

You could create test cases that assert the tessellate method returns the expected triangulation for a variety of shapes and parameters.

Example:

scala //Test file in a new tests folder import org.scalatest.funsuite.AnyFunSuite import geometry._

class CubeTests extends AnyFunSuite {

test("Cube Tesselation") { val cube = new Cube(Point(0, 0, 0), Centimeter(10)) // ... asserts on cube.tessellate() results } }

Future Enhancements

  • Support for more shapes: Add support for additional 3D shapes (e.g., cones, pyramids).
  • Material properties: Allow users to specify materials for 3D models (e.g., plastic, metal).
  • Export to other formats: Enable export to other 3D file formats (e.g., OBJ, 3DS).
  • Boolean operations: Implement operations such as union, intersection, and difference between shapes.
  • Advanced tessellation: Offer finer cont

Contributors

  • Oscra BYRNE
  • Maël AUBERT
  • Amine NACHIT
  • Baptiste NEVEJANS
  • Anis DALI-AHMED