001/*
002 * Renderer 7. The MIT License.
003 * Copyright (c) 2022 rlkraft@pnw.edu
004 * See LICENSE for details.
005*/
006
007package renderer.pipeline;
008
009import renderer.scene.*;
010import renderer.scene.primitives.*;
011import static renderer.pipeline.PipelineLogger.*;
012
013import java.util.Optional;
014
015/**
016   Clip a (projected) {@link Point} that might stick out
017   of the camera's view rectangle in the image plane.
018*/
019public class Clip_Point
020{
021   /**
022      If the {@link Vertex} used by the {@link Point} sticks out
023      of the camera's view rectangle, then have the {@link Point}
024      deleted from the model's primitive list.
025
026      @param model  {@link Model} that the {@link Point} {@code pt} comes from
027      @param pt     {@link Point} to be clipped
028      @return the clipped version of {@code pt} wrapped in an {@link Optional} object
029   */
030   public static Optional<Primitive> clip(final Model model, final Point pt)
031   {
032      final Optional<Primitive> result;
033
034      // Make local copies of several values.
035      final int vIndex = pt.vIndexList.get(0);
036      final Vertex v = model.vertexList.get(vIndex);
037
038      final double x = v.x,
039                   y = v.y;
040
041      // 1. Check for trivial accept.
042      if ( ! ( Math.abs( x ) > 1
043            || Math.abs( y ) > 1) )
044      {
045         result = Optional.of(pt); // better than "return pt"
046      }
047      // 2. Trivial delete.
048      else
049      {
050         result = Optional.empty(); // better than "return null"
051      }
052
053      return result;
054   }
055
056
057
058   // Private default constructor to enforce noninstantiable class.
059   // See Item 4 in "Effective Java", 3rd Ed, Joshua Bloch.
060   private Clip_Point() {
061      throw new AssertionError();
062   }
063}