/*
 * Renderer Models. The MIT License.
 * Copyright (c) 2022 rlkraft@pnw.edu
 * See LICENSE for details.
*/

package renderer.models_L;

import renderer.scene.*;
import renderer.scene.primitives.*;
import renderer.scene.util.MeshMaker;

/**
   Create a wireframe model of a torus.
<p>
   See:<br>
     <a href="https://en.wikipedia.org/wiki/Torus" target="_top">
              https://en.wikipedia.org/wiki/Torus</a>
<p>
   This torus is the surface of revolution generated by revolving
   the circle in the xy-plane with radius {@code r2} and center
   {@code (r1,0,0)} around the y-axis. We are assuming that {@code r1 > r2}.
<p>
   Here are parametric equations for the circle in the xy-plane with
   radius {@code r2} and center {@code (r1,0,0)} and parameterized
   starting from the top, with parameter {@code 0 <= phi <= 2*PI}.
   <pre>{@code
      x(phi) = r1 + r2 * sin(phi)
      y(phi) =      r2 * cos(phi)
      z(phi) = 0
   }</pre>
   Here is the 3D rotation matrix that rotates around the y-axis
   by {@code theta} radians with {@code 0 <= theta <= 2*PI}.
   <pre>{@code
      [ cos(theta)   0   sin(theta)]
      [     0        1       0     ]
      [-sin(theta)   0   cos(theta)]
   }</pre>
   If we multiply the rotation matrix with the circle parameterization,
   we get a parameterization of the torus.
   <pre>{@code
      [ cos(theta)   0   sin(theta)]   [r1 + r2 * sin(phi)]
      [     0        1       0     ] * [     r2 * cos(phi)]
      [-sin(theta)   0   cos(theta)]   [        0         ]

      = ( r1*cos(theta) + r2*cos(theta)*sin(phi).
          r2*cos(phi),
         -r1*sin(theta) - r2*sin(theta)*sin(phi) )

      = ( (r1 + r2*sin(phi)) * cos(theta),
                r2*cos(phi),
         -(r1 + r2*sin(phi)) * sin(theta) )
   }</pre>
<p>
   See:<br>
     <a href="https://en.wikipedia.org/wiki/Torus#Geometry" target="_top">
              https://en.wikipedia.org/wiki/Torus#Geometry</a>

   @see TorusSector
*/
public class Torus extends Model implements MeshMaker
{
   public final double r1;
   public final double r2;
   public final int n;
   public final int k;

   /**
      Create a torus with a circle of revolution with radius 3/4
      and a cross section circle (circle of longitude)
      with radius 1/4.
   */
   public Torus( )
   {
      this(0.75, 0.25, 12, 16);
   }


   /**
      Create a torus with a circle of revolution with radius {@code r1}
      and a cross section circle (circle of longitude) with radius
      {@code r2}.

      @param r1  radius of the circle of revolution
      @param r2  radius of the cross section circle (circle of longitude)
   */
   public Torus(final double r1, final double r2)
   {
      this(r1, r2, 12, 16);
   }


   /**
      Create a torus with a circle of revolution with radius {@code r1}
      and a cross section circle (circle of longitude) with radius
      {@code r2}.
   <p>
      The last two parameters determine the number of circles of
      longitude and the number of circles of latitude in the model.
   <p>
      If there are {@code n} circles of latitude, then each circle
      of longitude will have {@code n} line segments.
      If there are {@code k} circles of longitude, then each circle
      of latitude will have {@code k} line segments.
   <p>
      There must be at least three circles of longitude and at least
      three circles of latitude.

      @param r1  radius of the circle of revolution
      @param r2  radius of the cross section circle (circle of longitude)
      @param n   number of circles of latitude
      @param k   number of circles of longitude
      @throws IllegalArgumentException if {@code n} is less than 3
      @throws IllegalArgumentException if {@code k} is less than 3
   */
   public Torus(final double r1, final double r2,
                final int n, final int k)
   {
      super(String.format("Torus(%.2f,%.2f,%d,%d)", r1, r2, n, k));

      if (n < 3)
         throw new IllegalArgumentException("n must be greater than 2");
      if (k < 3)
         throw new IllegalArgumentException("k must be greater than 2");

      this.r1 = r1;
      this.r2 = r2;
      this.n = n;
      this.k = k;

      // Create the torus's geometry.

      final double deltaPhi   = (2 * Math.PI) / n,
                   deltaTheta = (2 * Math.PI) / k;

      // An array of vertices to be used to create line segments.
      final Vertex[][] v = new Vertex[n][k];

      // Create all the vertices.
      for (int j = 0; j < k; ++j) // choose a rotation around the y-axis
      {
         final double c1 = Math.cos(j * deltaTheta),
                      s1 = Math.sin(j * deltaTheta);
         for (int i = 0; i < n; ++i)  // go around a cross section circle
         {
            final double c2 = Math.cos(i * deltaPhi),
                         s2 = Math.sin(i * deltaPhi);
            v[i][j] = new Vertex( (r1 + r2*s2) * c1,
                                        r2*c2,
                                 -(r1 + r2*s2) * s1 );
         }
      }

      // Add all of the vertices to this model.
      for (int i = 0; i < n; ++i)
      {
         for (int j = 0; j < k; ++j)
         {
            addVertex( v[i][j] );
         }
      }

      // Create the vertical cross-section circles.
      for (int j = 0; j < k; ++j) // choose a rotation around the y-axis
      {
         for (int i = 0; i < n - 1; ++i) // go around a cross section circle
         {  //                                v[i][j]       v[i+1][j]
            addPrimitive(new LineSegment( (i * k) + j, ((i+1) * k) + j ));
         }
         // close the circle
         addPrimitive(new LineSegment( ((n-1) * k) + j, (0 * k) + j ));
      }  //                                 v[n-1][j]       v[0][j]

      // Create all the horizontal circles around the torus.
      for (int i = 0; i < n; ++i) //choose a rotation around the cross section
      {
         for (int j = 0; j < k - 1; ++j) // go around a horizontal circle
         {  //                                v[i][j]       v[i][j+1]
            addPrimitive(new LineSegment( (i * k) + j, (i * k) + (j+1) ));
         }
         // close the circle
         addPrimitive(new LineSegment( (i * k) + (k-1), (i * k) + 0 ));
      }  //                                v[i][k-1]       v[i][0]
   }



   // Implement the MeshMaker interface (three methods).
   @Override public int getHorzCount() {return n;}

   @Override public int getVertCount() {return k;}

   @Override
   public Torus remake(final int n, final int k)
   {
      return new Torus(this.r1, this.r2,
                       n, k);
   }
}//Torus
