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.List;
014import java.util.ArrayList;
015import java.util.Optional;
016
017/**
018   Clip in camera space any {@link Point} that crosses the
019   camera's near clipping plane {@code z = -near}.
020*/
021public class NearClip_Point
022{
023   /**
024      If the {@link Vertex} used by the {@link Point} is on the camera
025      side of the near plane, then return an empty {@link Optional}
026      object to indicate that the {@link Point} should be discarded
027      from the model's {@link Primitive} list.
028      <p>
029      If the {@link Vertex} used by the {@link Point} is on the far
030      side of the near plane, then return the {@link Point} wrapped
031      in an {@link Optional} object.
032
033      @param model   {@link Model} that the {@link Point} {@code pt} comes from
034      @param pt      {@link Point} to be clipped
035      @param camera  {@link Camera} that determines the near clipping plane
036      @return the clipped version of {@code pt} wrapped in an {@link Optional} object
037   */
038   public static Optional<Primitive> clip(final Model model,
039                                          final Point pt,
040                                          final Camera camera)
041   {
042      final Optional<Primitive> result;
043
044      // Make local copies of several values.
045      final int vIndex = pt.vIndexList.get(0);
046      final Vertex v = model.vertexList.get(vIndex);
047
048      final double z = v.z;
049
050      // 1. Check for trivial accept.
051      if ( z <= camera.n )
052      {
053         result = Optional.of(pt); // better than "return pt"
054      }
055      // 2. Trivial delete.
056      else
057      {
058         result = Optional.empty(); // better than "return null"
059      }
060
061      return result;
062   }
063
064
065
066   // Private default constructor to enforce noninstantiable class.
067   // See Item 4 in "Effective Java", 3rd Ed, Joshua Bloch.
068   private NearClip_Point() {
069      throw new AssertionError();
070   }
071}