1 // gradient
2 //
3 // Copyright © 2013 Roderick Gibson
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or (at
8 // your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 // License (COPYING.txt) for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //
19 // The developer's email is kniteli@gmail.com
20 //
21 module noise.mod.gradient;
22 
23 private {
24   import noise.mod.modulebase;
25   debug import std.stdio;
26 }
27 /// @addtogroup libnoise
28 /// @{
29 
30 /// @addtogroup modules
31 /// @{
32 
33 /// @addtogroup combinermodules
34 /// @{
35 
36 //
37 // Module that takes two points and creates a gradient vector
38 // from them.
39 
40 // Ported from the Accidental Noise Library (author Joshua Tippetts )here: 
41 // http://accidentalnoise.sourceforge.net/index.html
42 //
43 class Gradient : Mod
44 {
45 
46 public:
47 
48   /// Constructor.
49   this(double x1, double y1, double z1, double x2, double y2, double z2)
50   {
51     super(this.GetSourceModCount());
52     m_x1 = x1;
53     m_y1 = y1;
54     m_z1 = z1;
55     m_x2 = x2;
56     m_y2 = y2;
57     m_z2 = z2;
58 
59     m_x=x2-x1;
60     m_y=y2-y1;
61     m_z=z2-z1;
62 
63     m_vlen=(m_x*m_x+m_y*m_y+m_z*m_z);
64   }
65 
66   override int GetSourceModCount () const
67   {
68     return 0;
69   }
70 
71   override double GetValue (double x, double y, double z) const
72   {
73     double dx=x-m_x1;
74     double dy=y-m_y1;
75     double dz=z-m_z1;
76     double dp=dx*m_x+dy*m_y+dz*m_z;
77     dp/=m_vlen;
78     //dp=clamp(dp/m_vlen,0.0,1.0);
79     //return lerp(dp,1.0,-1.0);
80     return dp;    
81   }
82 
83 private:
84   double m_x1, m_x2;
85   double m_y1, m_y2;
86   double m_z1, m_z2;
87     double m_x, m_y, m_z;
88     double m_vlen;
89 }