View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.hipparchus.samples.complex;
19  
20  import org.hipparchus.complex.Complex;
21  import org.hipparchus.util.FastMath;
22  import org.hipparchus.util.MathUtils;
23  
24  /** Domain coloring enhancing both phase and module changes.
25   * <p>
26   * Value encodes both phase and module using two sawtooth functions.
27   * </p>
28   * <p>
29   * The sawtooth functions are computed from a natural logarithm and fractional parts.
30   * They enhance both phase and modules changes with discontinuities and dark cells.
31   * </p>
32   */
33  public class SawToothPhaseModuleValue extends DomainColoring {
34  
35      /** Minimum brightness. */
36      private final double minBrightness;
37  
38      /** Maximum brightness. */
39      private final double maxBrightness;
40  
41      /** Number of lines per cycle. */
42      private final int nbLines;
43  
44      /** Simple constructor.
45       * @param saturation constant saturation
46       * @param minBrightness minimum brightness
47       * @param maxBrightness maximum brightness
48       * @param nbLines number of lines per cycle
49       */
50      protected SawToothPhaseModuleValue(final double saturation,
51                                         final double minBrightness, final double maxBrightness,
52                                         final int nbLines) {
53          super(saturation);
54          this.minBrightness = minBrightness;
55          this.maxBrightness = maxBrightness;
56          this.nbLines       = nbLines;
57      }
58  
59      /** Compute periodic fractional brightness.
60       * @param x continuous value
61       * @param s scaling factor
62       * @return fractional brightness
63       */
64      private double fractionalBrightness(final double x, final double s) {
65          double f = x * nbLines / s;
66          return minBrightness + (maxBrightness - minBrightness) * (f - FastMath.floor(f));
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public double value(final Complex z) {
72          final double module = z.norm();
73          final double bM = fractionalBrightness(FastMath.log(module), MathUtils.TWO_PI);
74          final double bP = fractionalBrightness(hue(z), 1.0);
75          return bM * bP;
76      }
77  
78  }