001/*
002 * Renderer 4. The MIT License.
003 * Copyright (c) 2022 rlkraft@pnw.edu
004 * See LICENSE for details.
005*/
006
007package renderer.scene.util;
008
009import renderer.scene.*;
010import renderer.scene.primitives.*;
011
012/**
013   Several static utility methods for checking
014   and/or debugging a {@link Model}.
015*/
016public class CheckModel
017{
018   /**
019      Determine if there are any obvious problems with the {@link Model}
020      to be rendered. The purpose of these checks is to make the renderer
021      a bit more user friendly. If a user makes a simple mistake and tries
022      to render a {@link Model} that is missing vertices or line segments,
023      or colors, then the user gets a helpful error message.
024
025      @param model  the {@link Model} to be checked
026   */
027   public static void check(final Model model)
028   {
029      boolean error = false;
030      if (model.vertexList.isEmpty() && ! model.primitiveList.isEmpty())
031      {
032         System.err.println(
033            "***WARNING: This model does not have any vertices.");
034         error = true;
035      }
036      if (! model.vertexList.isEmpty() && model.primitiveList.isEmpty())
037      {
038         System.err.println(
039            "***WARNING: This model does not have any geometric primitives.");
040         error = true;
041      }
042      if (! model.vertexList.isEmpty() && model.colorList.isEmpty())
043      {
044         System.err.println(
045            "***WARNING: This model does not have any colors.");
046         //error = true;
047      }
048      if (error)
049      {
050         System.err.println(model);
051      }
052   }
053
054
055   /**
056      Check each {@link Primitive} in the {@link Model} to make sure that
057      each index in the {@link Primitive}'s {@code vIndexList} refers to a
058      valid {@link Vertex} in the {@link Model}'s {@code vertexList} and
059      also that each index in the {@link Primitive}'s {@code cIndexList}
060      refers to a valid {@link java.awt.Color} in the {@link Model}'s
061      {@code colorList}
062
063      @param model  the {@link Model} to be checked for consistent indexes
064      @return true if no problem is found, false if an invalid index is found
065   */
066   public static boolean checkPrimitives(final Model model)
067   {
068      final int numberOfVertices = model.vertexList.size();
069
070      boolean result = true;
071      for (final Primitive p : model.primitiveList)
072      {
073         for (int i = 0; i < p.vIndexList.size(); ++i)
074         {
075            if (i >= numberOfVertices)
076            {
077               System.out.println("This Primitive has invalid vertex index: " + i);
078               System.out.println( p );
079               result = false;
080            }
081         }
082      }
083
084      final int numberOfColors = model.colorList.size();
085
086      for (final Primitive p : model.primitiveList)
087      {
088         for (int i = 0; i < p.cIndexList.size(); ++i)
089         {
090            if (i >= numberOfColors)
091            {
092               System.out.println("This Primitive has invalid color index: " + i);
093               System.out.println( p );
094               result = false;
095            }
096         }
097      }
098      return result;
099   }
100
101
102
103   // Private default constructor to enforce noninstantiable class.
104   // See Item 4 in "Effective Java", 3rd Ed, Joshua Bloch.
105   private CheckModel() {
106      throw new AssertionError();
107   }
108}