Developer Documentation
TextureMath.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 #include "TextureMath.hh"
45 
46 #include <cmath>
47 
48 TextureMath::TextureMath(const bool _abs,
49  const bool _clamp,
50  const double _clampMin,
51  const double _clampMax,
52  const bool _repeat,
53  const double _minRepeat,
54  const double _maxRepeat,
55  const bool _center,
56  const bool _scale,
57  const double _minimalInput,
58  const double _maximalInput):
59  abs_(_abs),
60  clamp_(_clamp),
61  clampMin_(_clampMin),
62  clampMax_(_clampMax),
63  repeat_(_repeat),
64  repeatMin_(_minRepeat),
65  repeatMax_(_maxRepeat),
66  center_(_center),
67  scale_(_scale),
68  minInput_(_minimalInput),
69  maxInput_(_maximalInput)
70 {
71 
72 }
73 
74 TextureMath::TextureMath(const TexParameters& _parameters, const double _minimalInput, const double _maximalInput) :
75  abs_(_parameters.abs),
76  clamp_(_parameters.clamp),
77  clampMin_(_parameters.clampMin),
78  clampMax_(_parameters.clampMax),
79  repeat_(_parameters.repeat),
80  repeatMin_(_parameters.repeatMin),
81  repeatMax_(_parameters.repeatMax),
82  center_(_parameters.center),
83  scale_(_parameters.scale),
84  minInput_(_minimalInput),
85  maxInput_(_maximalInput)
86 {
87 }
88 
89 double TextureMath::transform(const double _input) const
90 {
91  double value = _input;
92 
93 
94  // Use absolute value as requested by plugin
95  if ( abs_ )
96  value = fabs(value);
97 
98  // Clamp if requested
99  if ( clamp_ ) {
100  if ( value > clampMax_ )
101  value = clampMax_;
102  if (value < clampMin_)
103  value = clampMin_;
104  }
105 
106  // if all texCoords have the same value
107  if ( minInput_ == maxInput_){
108 
109  if ( ! repeat_ )
110  value = 0.0;
111  else
112  value = maxInput_;
113 
114  return value;
115  }
116 
117 
118  // if the texture should not be repeated, scale to 0..1
119  if ( ! repeat_ ) {
120  if (! center_ ) {
121  if ( scale_) {
122  value /= fabs(maxInput_) + fabs(minInput_); //scaleFactor is != 0.0 (otherwise _min==_max)
123  value -= minInput_/(fabs(maxInput_) + fabs(minInput_));
124  }
125  } else {
126  // the values above zero are mapped to 0.5..1 the negative ones to 0.5..0
127  if (value > 0.0) {
128  value /= ( maxInput_ * 2.0); //_max >= _value > 0.0
129  value += 0.5;
130  } else {
131  if ( minInput_ == 0.0 ){
132  value = 0.0;
133  } else {
134  value /= ( minInput_ * 2.0);
135  value = 0.5 - value;
136  }
137  }
138  }
139  } else {
140  value -= minInput_;
141  value *= (repeatMax_ - repeatMin_) / (maxInput_ - minInput_);
142  value += repeatMin_;
143  }
144 
145  return value;
146 }
TextureMath(const bool _abs, const bool _clamp, const double _clampMin, const double _clampMax, const bool _repeat, const double _minRepeat, const double _maxRepeat, const bool _center, const bool _scale, const double _minimalInput, const double _maximalInput)
Definition: TextureMath.cc:48
double transform(const double _input) const
Modify given values based on the specified parameters given to the constructor.
Definition: TextureMath.cc:89