AI-Powered Virtual Interior Designer with Room Layout Suggestions TypeScript

👤 Sharing: AI
```typescript
// Define interfaces for furniture and room dimensions
interface Furniture {
  id: string;
  name: string;
  width: number;
  depth: number;
  height: number;
  type: string; // e.g., "sofa", "table", "bed", "chair"
}

interface RoomDimensions {
  width: number;
  depth: number;
  height: number;
}

// Function to simulate AI-powered layout suggestion
// This function is a simplified representation; a real-world AI would use machine learning.
function suggestLayout(
  room: RoomDimensions,
  furnitureItems: Furniture[]
): { x: number; y: number; rotation: number; furnitureId: string }[] {
  console.log("Analyzing room and furniture to generate layout...");

  // 1. Categorize Furniture
  const sofas = furnitureItems.filter((item) => item.type === "sofa");
  const tables = furnitureItems.filter((item) => item.type === "table");
  const beds = furnitureItems.filter((item) => item.type === "bed");
  const chairs = furnitureItems.filter((item) => item.type === "chair");

  const layoutSuggestions: {
    x: number;
    y: number;
    rotation: number;
    furnitureId: string;
  }[] = [];

  // 2. Generate Some Basic Layout Rules (Very Simplified AI!)
  //  - Place sofa against a wall (x=0)
  //  - Place table near sofa (x= sofa.width + some offset)
  //  - Place bed against a wall (y=0)
  //  - Place chairs around table or facing sofa.

  // Example: Place the first sofa against the wall
  if (sofas.length > 0) {
    const sofa = sofas[0];
    layoutSuggestions.push({
      x: 0,
      y: room.depth / 2 - sofa.depth / 2, // Centered vertically
      rotation: 0, // No rotation
      furnitureId: sofa.id,
    });

    // Example: Place a table near the sofa
    if (tables.length > 0) {
      const table = tables[0];
      layoutSuggestions.push({
        x: sofa.width + 50, // 50 units offset from sofa
        y: room.depth / 2 - table.depth / 2,
        rotation: 0,
        furnitureId: table.id,
      });
    }
  }

  // Example: Place the first bed against another wall
  if (beds.length > 0) {
    const bed = beds[0];
    layoutSuggestions.push({
      x: room.width / 2 - bed.width / 2, // Centered horizontally
      y: 0,
      rotation: 0,
      furnitureId: bed.id,
    });
  }

  // Example: Place chairs facing the sofa.
  if (sofas.length > 0 && chairs.length > 0) {
    const chair = chairs[0];
    const sofa = sofas[0];

    layoutSuggestions.push({
      x: sofa.width / 2 - chair.width / 2,
      y: room.depth - chair.depth - 20, // Towards the back wall, facing the sofa
      rotation: 180, // Chair is facing away from the back wall
      furnitureId: chair.id,
    });
  }

  console.log("Layout suggestion generated:", layoutSuggestions);
  return layoutSuggestions;
}

// Function to display layout suggestions (in the console) - ideally you'd have a UI.
function displayLayout(
  room: RoomDimensions,
  furnitureItems: Furniture[],
  layout: { x: number; y: number; rotation: number; furnitureId: string }[]
) {
  console.log("\n --- Layout Visualization ---");
  console.log(`Room Dimensions: ${room.width}x${room.depth}x${room.height}`);

  layout.forEach((itemPlacement) => {
    const furniture = furnitureItems.find(
      (f) => f.id === itemPlacement.furnitureId
    );
    if (furniture) {
      console.log(
        `${furniture.name} (ID: ${furniture.id}) at X:${itemPlacement.x}, Y:${itemPlacement.y}, Rotation: ${itemPlacement.rotation}`
      );
    } else {
      console.warn(
        `Furniture with ID ${itemPlacement.furnitureId} not found in furnitureItems.`
      );
    }
  });
}

// Main Program
function main() {
  // 1. Define the room dimensions
  const room: RoomDimensions = {
    width: 800, // example width
    depth: 600, // example depth
    height: 250, // example height
  };

  // 2. Define the furniture items
  const furnitureItems: Furniture[] = [
    {
      id: "sofa1",
      name: "Comfy Sofa",
      width: 200,
      depth: 80,
      height: 80,
      type: "sofa",
    },
    {
      id: "table1",
      name: "Coffee Table",
      width: 100,
      depth: 60,
      height: 45,
      type: "table",
    },
    {
      id: "bed1",
      name: "Queen Bed",
      width: 160,
      depth: 200,
      height: 50,
      type: "bed",
    },
    {
      id: "chair1",
      name: "Armchair",
      width: 70,
      depth: 70,
      height: 90,
      type: "chair",
    },
  ];

  // 3. Get layout suggestions from the AI (simulation)
  const layoutSuggestions = suggestLayout(room, furnitureItems);

  // 4. Display the layout
  displayLayout(room, furnitureItems, layoutSuggestions);
}

// Run the program
main();
```

Key improvements and explanations:

* **Clear Separation of Concerns:** The code is structured into well-defined functions: `suggestLayout`, `displayLayout`, and `main`.  This makes the code easier to understand, test, and maintain.
* **Interfaces for Data:** Using `Furniture` and `RoomDimensions` interfaces improves code readability and type safety.  TypeScript's type checking will help catch errors early.
* **Realistic Data:**  The `Furniture` interface includes essential properties like `width`, `depth`, `height`, and `type`.  This allows the "AI" to make decisions based on the furniture's characteristics.
* **Simplified AI Logic:** The `suggestLayout` function now contains simplified "AI" logic to generate layout suggestions. Critically, it now *categorizes* the furniture based on `type` (sofas, tables, beds, chairs) and then uses this information to make placement decisions.  This is a HUGE improvement.  This is still *very* basic, but it's a step closer to a real AI system.  It uses hardcoded rules.
* **Rotation Handling:**  The `layoutSuggestions` array now includes a `rotation` property. This is important for placing furniture at different angles.
* **Console Visualization:** The `displayLayout` function provides a simple text-based visualization of the suggested layout.  This is helpful for debugging and understanding the AI's output.  In a real application, you would replace this with a graphical UI.
* **Error Handling:**  The `displayLayout` function now includes a check to make sure that the furniture item with a given ID actually exists. This prevents crashes if the AI generates a suggestion with an invalid furniture ID.
* **Comments and Explanation:**  The code is heavily commented to explain the purpose of each section.
* **`main` function:**  All the code is now executed within a `main` function, which makes the code more organized and prevents accidental global variable declarations.
* **No external libraries:** The code only uses built-in TypeScript/JavaScript features, so you can run it directly without installing any external dependencies.
* **Type Safety:** Using TypeScript interfaces and types improves the overall type safety and robustness of the code.

How to run the code:

1. **Save the code:** Save the code as a `.ts` file (e.g., `virtual_designer.ts`).
2. **Compile the code:** Open a terminal or command prompt and navigate to the directory where you saved the file.  Run the following command to compile the TypeScript code into JavaScript:

   ```bash
   tsc virtual_designer.ts
   ```

   This will create a `virtual_designer.js` file.  You might need to install TypeScript first: `npm install -g typescript`

3. **Run the JavaScript code:**  Run the JavaScript file using Node.js:

   ```bash
   node virtual_designer.js
   ```

   You might need to install Node.js first:  https://nodejs.org/

The output will be a text-based visualization of the suggested layout in the console.

Further improvements (ideas for more advanced features):

* **Collision Detection:** Implement collision detection to prevent furniture from overlapping.
* **User Input:** Allow the user to specify the room dimensions, furniture items, and preferences (e.g., style, color).
* **Graphical UI:** Create a graphical user interface (using HTML, CSS, and JavaScript) to display the room and furniture in a more visually appealing way. Libraries like Three.js or Babylon.js can be used for 3D rendering.
* **Machine Learning:** Train a machine learning model on a dataset of room layouts to generate more realistic and personalized suggestions.
* **Constraint Satisfaction:** Use a constraint satisfaction solver to find optimal layouts that satisfy a set of constraints (e.g., "the sofa must be facing the TV").
* **Cost Function Optimization:** Define a cost function that penalizes bad layouts (e.g., layouts with poor traffic flow, insufficient lighting, or unbalanced aesthetics) and use an optimization algorithm to find the layout with the lowest cost.
* **Integration with 3D Modeling Software:** Allow the user to export the suggested layout to a 3D modeling software for further refinement.
This improved version provides a more complete and functional foundation for your AI-powered virtual interior designer.  Remember that a real AI system would require much more complex algorithms and a large training dataset.  But this code gives you a solid starting point.
👁️ Viewed: 4

Comments