Spinning Topp Logo BlackTopp Studios
inc
cycloidmodifier.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  -----------------------------------------------------------------------------
42  This source file is part of ogre-procedural
43 
44  For the latest info, see http://code.google.com/p/ogre-procedural/
45 
46  Copyright (c) 2010-2013 Michael Broutin
47 
48  Permission is hereby granted, free of charge, to any person obtaining a copy
49  of this software and associated documentation files (the "Software"), to deal
50  in the Software without restriction, including without limitation the rights
51  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52  copies of the Software, and to permit persons to whom the Software is
53  furnished to do so, subject to the following conditions:
54 
55  The above copyright notice and this permission notice shall be included in
56  all copies or substantial portions of the Software.
57 
58  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
64  THE SOFTWARE.
65  -----------------------------------------------------------------------------
66  */
67 #ifndef _graphicsproceduralcycloidmodifier_cpp
68 #define _graphicsproceduralcycloidmodifier_cpp
69 
70 #include "Graphics/Procedural/Texture/cycloidmodifier.h"
71 
72 #include "MathTools/mathtools.h"
73 
74 #include "exception.h"
75 
76 namespace Mezzanine
77 {
78  namespace Graphics
79  {
80  namespace Procedural
81  {
83  CycloidColour(1.0,1.0,1.0,1.0),
84  CycloidCenter(0.5,0.5),
85  CycloidPenSize(1),
86  Parameter_R(0),
87  Parameter_r(0),
88  Parameter_d(0),
89  Parameter_e(0)
91 
93  { }
94 
95  void CycloidModifier::ProcessHypocycloid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer& Buffer)
96  {
97  Integer CurrX = 0;
98  Integer CurrY = 0;
99  Real Phi = 0;
100 
101  Integer SignedX = XPos + static_cast<Integer>( MathTools::Floor( this->Parameter_R + 0.5 ) );
102  Integer SignedY = YPos;
103  do
104  {
105  Real DeltaX = ( this->Parameter_R - this->Parameter_r ) * MathTools::Cos(Phi) + this->Parameter_r * MathTools::Cos( ( ( this->Parameter_R - this->Parameter_r ) / this->Parameter_r ) * Phi );
106  Real DeltaY = ( this->Parameter_R - this->Parameter_r ) * MathTools::Sin(Phi) - this->Parameter_r * MathTools::Sin( ( ( this->Parameter_R - this->Parameter_r ) / this->Parameter_r ) * Phi );
107 
108  CurrX = XPos + static_cast<Integer>( MathTools::Floor( DeltaX + 0.5 ) );
109  CurrY = YPos - static_cast<Integer>( MathTools::Floor( DeltaY + 0.5 ) );
110  this->Paint(CurrX,CurrY,Step,Buffer);
111 
112  Phi += Step;
113  }
114  while( !( SignedX == CurrX && SignedY == CurrY && Phi < 100.0 * MathTools::GetPi() ) || Phi < MathTools::GetTwoPi() );
115  }
116 
117  void CycloidModifier::ProcessHypotrochoid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer& Buffer)
118  {
119  Integer CurrX = 0;
120  Integer CurrY = 0;
121  Real Phi = 0;
122 
123  Integer SignedX = XPos + static_cast<Integer>( MathTools::Floor( ( this->Parameter_R - this->Parameter_r ) + this->Parameter_d + 0.5 ) );
124  Integer SignedY = YPos;
125  do
126  {
127  Real DeltaX = ( this->Parameter_R - this->Parameter_r ) * MathTools::Cos(Phi) + this->Parameter_d * MathTools::Cos( ( ( this->Parameter_R - this->Parameter_r ) / this->Parameter_r ) * Phi);
128  Real DeltaY = ( this->Parameter_R - this->Parameter_r ) * MathTools::Sin(Phi) - this->Parameter_d * MathTools::Sin( ( ( this->Parameter_R - this->Parameter_r ) / this->Parameter_r ) * Phi);
129 
130  CurrX = XPos + static_cast<Integer>( MathTools::Floor( DeltaX + 0.5 ) );
131  CurrY = YPos - static_cast<Integer>( MathTools::Floor( DeltaY + 0.5 ) );
132  this->Paint(CurrX,CurrY,Step,Buffer);
133 
134  Phi += Step;
135  }
136  while( !( SignedX == CurrX && SignedY == CurrY && Phi < 100.0 * MathTools::GetPi() ) || Phi < MathTools::GetTwoPi() );
137  }
138 
139  void CycloidModifier::ProcessEpicycloid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer& Buffer)
140  {
141  Integer CurrX = 0;
142  Integer CurrY = 0;
143  Real Phi = 0;
144 
145  Integer SignedX = XPos + static_cast<Integer>( MathTools::Floor( (this->Parameter_R + this->Parameter_r) - this->Parameter_r + 0.5 ) );
146  Integer SignedY = YPos;
147  do
148  {
149  Real DeltaX = ( this->Parameter_R + this->Parameter_r ) * MathTools::Cos(Phi) - this->Parameter_r * MathTools::Cos( ( (this->Parameter_R + this->Parameter_r) / this->Parameter_r) * Phi);
150  Real DeltaY = ( this->Parameter_R + this->Parameter_r ) * MathTools::Sin(Phi) - this->Parameter_r * MathTools::Sin( ( (this->Parameter_R + this->Parameter_r) / this->Parameter_r) * Phi);
151 
152  CurrX = XPos + static_cast<Integer>( MathTools::Floor( DeltaX + 0.5 ) );
153  CurrY = YPos - static_cast<Integer>( MathTools::Floor( DeltaY + 0.5 ) );
154  this->Paint(CurrX,CurrY,Step,Buffer);
155 
156  Phi += Step;
157  }
158  while( !( SignedX == CurrX && SignedY == CurrY && Phi < 100.0 * MathTools::GetPi() ) || Phi < MathTools::GetTwoPi() );
159  }
160 
161  void CycloidModifier::ProcessEpitrochoid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer& Buffer)
162  {
163  Integer CurrX = 0;
164  Integer CurrY = 0;
165  Real Phi = 0;
166 
167  Integer SignedX = XPos + static_cast<Integer>( MathTools::Floor( (this->Parameter_R + this->Parameter_r) - this->Parameter_d + 0.5 ) );
168  Integer SignedY = YPos;
169  do
170  {
171  Real DeltaX = ( this->Parameter_R + this->Parameter_r ) * MathTools::Cos(Phi) - this->Parameter_d * MathTools::Cos( ( (this->Parameter_R + this->Parameter_r) / this->Parameter_r ) * Phi);
172  Real DeltaY = ( this->Parameter_R + this->Parameter_r ) * MathTools::Sin(Phi) - this->Parameter_d * MathTools::Sin( ( (this->Parameter_R + this->Parameter_r) / this->Parameter_r ) * Phi);
173 
174  CurrX = XPos + static_cast<Integer>( MathTools::Floor( DeltaX + 0.5 ) );
175  CurrY = YPos - static_cast<Integer>( MathTools::Floor( DeltaY + 0.5 ) );
176  this->Paint(CurrX,CurrY,Step,Buffer);
177 
178  Phi += Step;
179  }
180  while( !( SignedX == CurrX && SignedY == CurrY && Phi < 100.0 * MathTools::GetPi() ) || Phi < MathTools::GetTwoPi() );
181  }
182 
183  void CycloidModifier::ProcessRoseCurve(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer& Buffer)
184  {
185  Integer CurrX = 0;
186  Integer CurrY = 0;
187  Real t = 0;
188  Real k = this->Parameter_r / this->Parameter_d;
189  Real ActualStep = Step / 10.0;
190 
191  //Integer SignedX = XPos;
192  //Integer SignedY = YPos;
193  do
194  {
195  Real DeltaX = this->Parameter_R * MathTools::Cos(k * t) * MathTools::Sin(t);
196  Real DeltaY = this->Parameter_R * MathTools::Cos(k * t) * MathTools::Cos(t);
197 
198  CurrX = XPos + static_cast<Integer>( MathTools::Floor( DeltaX + 0.5 ) );
199  CurrY = YPos - static_cast<Integer>( MathTools::Floor( DeltaY + 0.5 ) );
200  this->Paint(CurrX,CurrY,ActualStep,Buffer);
201 
202  t += ActualStep;
203  }
204  while( t <= MathTools::GetTwoPi() );
205  }
206 
207  void CycloidModifier::ProcessLissajousCurve(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer& Buffer)
208  {
209  Integer CurrX = 0;
210  Integer CurrY = 0;
211  Real t = 0;
212  Real ActualStep = Step / 10.0;
213 
214  //Integer SignedX = XPos;
215  //Integer SignedY = YPos;
216  do
217  {
218  Real DeltaX = this->Parameter_R * MathTools::Sin( this->Parameter_r * t + this->Parameter_e );
219  Real DeltaY = this->Parameter_R * MathTools::Cos( this->Parameter_d * t + this->Parameter_e );
220 
221  CurrX = XPos + static_cast<Integer>( MathTools::Floor( DeltaX + 0.5 ) );
222  CurrY = YPos - static_cast<Integer>( MathTools::Floor( DeltaY + 0.5 ) );
223  this->Paint(CurrX,CurrY,ActualStep,Buffer);
224 
225  t += ActualStep;
226  }
227  while( t <= MathTools::GetTwoPi() );
228  }
229 
230  void CycloidModifier::Paint(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer& Buffer)
231  {
232  if( this->CycloidPenSize == 1 ) {
233  if( XPos < 0 || YPos < 0 || XPos >= static_cast<Integer>( Buffer.GetWidth() ) || YPos >= static_cast<Integer>( Buffer.GetHeight() ) ) {
234  return;
235  }
236  Buffer.SetPixel(XPos,YPos,this->CycloidColour);
237  }else{
238  for( Real Phi = 0 ; Phi <= MathTools::GetTwoPi() ; Phi += Step )
239  {
240  Real DeltaX = MathTools::Cos(Phi);
241  Real DeltaY = MathTools::Sin(Phi);
242  for( Whole r = 0 ; r < this->CycloidPenSize ; ++r )
243  {
244  Integer CurrX = XPos + static_cast<Integer>( MathTools::Floor( static_cast<Real>(r) * DeltaX + 0.5 ) );
245  Integer CurrY = YPos - static_cast<Integer>( MathTools::Floor( static_cast<Real>(r) * DeltaY + 0.5 ) );
246  if( CurrX >= 0 && CurrY >= 0 && CurrX < static_cast<Integer>( Buffer.GetWidth() ) && CurrY < static_cast<Integer>( Buffer.GetHeight() ) ) {
247  Buffer.SetPixel(CurrX,CurrY,this->CycloidColour);
248  }
249  }
250  }
251  }
252  }
253 
254  ///////////////////////////////////////////////////////////////////////////////
255  // Utility
256 
258  {
259  if( this->CycloidPenSize != 0 ) {
260  if( this->Parameter_r == 0 && this->Parameter_R == 0 && this->Parameter_e == 0 && this->Parameter_d == 0 ) {
261  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"No parameters have been set for Cycloid rendering.");
262  }
263 
264  Integer XPos = static_cast<Integer>( static_cast<Real>( Buffer.GetWidth() * this->CycloidCenter.X ) );
265  Integer YPos = static_cast<Integer>( static_cast<Real>( Buffer.GetHeight() * this->CycloidCenter.Y ) );
266  Real Step = MathTools::GetPi() / static_cast<Real>( std::min( Buffer.GetHeight(), Buffer.GetWidth() ) );
267  switch( this->Type )
268  {
269  default:
270  { break; }
272  {
273  this->ProcessHypocycloid(XPos,YPos,Step,Buffer);
274  break;
275  }
277  {
278  this->ProcessHypotrochoid(XPos,YPos,Step,Buffer);
279  break;
280  }
282  {
283  this->ProcessEpicycloid(XPos,YPos,Step,Buffer);
284  break;
285  }
287  {
288  this->ProcessEpitrochoid(XPos,YPos,Step,Buffer);
289  break;
290  }
292  {
293  this->ProcessRoseCurve(XPos,YPos,Step,Buffer);
294  break;
295  }
297  {
298  this->ProcessLissajousCurve(XPos,YPos,Step,Buffer);
299  break;
300  }
301  }
302  }
303  }
304 
306  { return "CycloidModifier"; }
307 
308  ///////////////////////////////////////////////////////////////////////////////
309  // Configuration
310 
312  {
313  this->Type = ToDraw;
314  return *this;
315  }
316 
318  {
319  this->SetDefaultParameters(SquareSize,SquareSize);
320  }
321 
322  CycloidModifier& CycloidModifier::SetDefaultParameters(const Whole TextureWidth, const Whole TextureHeight)
323  {
324  Real Size = static_cast<Real>( std::min( TextureWidth, TextureHeight ) );
325  switch( this->Type )
326  {
327  default:
329  {
330  this->Parameter_R = 3.0 / 6.0 * Size;
331  this->Parameter_r = 1.0 / 6.0 * Size;
332  this->Parameter_d = 0.0;
333  this->Parameter_e = 0.0;
334  break;
335  }
337  {
338  this->Parameter_R = 5.0 / 14.0 * Size;
339  this->Parameter_r = 3.0 / 14.0 * Size;
340  this->Parameter_d = 5.0 / 14.0 * Size;
341  this->Parameter_e = 0.0;
342  break;
343  }
345  {
346  this->Parameter_R = 3.0 / 10.0 * Size;
347  this->Parameter_r = 1.0 / 10.0 * Size;
348  this->Parameter_d = 0.0;
349  this->Parameter_e = 0.0;
350  break;
351  }
353  {
354  this->Parameter_R = 3.0 / 10.0 * Size;
355  this->Parameter_r = 1.0 / 10.0 * Size;
356  this->Parameter_d = 1.0 / 20.0 * Size;
357  this->Parameter_e = 0.0;
358  break;
359  }
361  {
362  this->Parameter_R = 0.5 * Size;
363  this->Parameter_r = 4.0;
364  this->Parameter_d = 1.0;
365  this->Parameter_e = 0.0;
366  break;
367  }
369  {
370  this->Parameter_R = 0.5 * Size;
371  this->Parameter_r = 5.0;
372  this->Parameter_d = 4.0;
373  this->Parameter_e = MathTools::GetHalfPi();
374  break;
375  }
376  }
377  return *this;
378  }
379 
381  {
382  this->CycloidColour = Colour;
383  return *this;
384  }
385 
386  CycloidModifier& CycloidModifier::SetColour(const Real Red, const Real Green, const Real Blue, const Real Alpha)
387  {
388  this->CycloidColour.SetValues(Red,Green,Blue,Alpha);
389  return *this;
390  }
391 
393  {
394  this->CycloidCenter = Center;
395  return *this;
396  }
397 
399  {
400  this->CycloidCenter.SetValues(CenterX,CenterY);
401  return *this;
402  }
403 
405  {
406  this->CycloidCenter.X = X;
407  return *this;
408  }
409 
411  {
412  this->CycloidCenter.Y = Y;
413  return *this;
414  }
415 
417  {
418  switch( Param )
419  {
421  {
422  this->Parameter_R = Value;
423  break;
424  }
426  {
427  this->Parameter_r = Value;
428  break;
429  }
431  {
432  this->Parameter_d = Value;
433  break;
434  }
436  {
437  this->Parameter_e = Value;
438  break;
439  }
441  {
442  switch( Type )
443  {
444  default:
449  {
450  this->Parameter_R = Value * this->Parameter_r;
451  break;
452  }
455  {
456  this->Parameter_r = Value * this->Parameter_d;
457  break;
458  }
459  }
460  break;
461  }
462  default:
463  break;
464  }
465  return *this;
466  }
467 
469  {
470  this->CycloidPenSize = Size;
471  return *this;
472  }
473  }//Procedural
474  }//Graphics
475 }//Mezzanine
476 
477 #endif
void ProcessLissajousCurve(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer &Buffer)
Draws a Lissajous Curve to the texture.
Sets defaults for drawing a Epicycloid. See class detailed description for more information.
Sets defaults for drawing a Epitrochoid. See class detailed description for more information.
void ProcessHypocycloid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer &Buffer)
Draws a Hypocycloid to the texture.
Sets defaults for drawing a Rose Curve. See class detailed description for more information.
Whole GetWidth() const
Get the pixel width of this texture.
CycloidModifier & SetDefaultParameters(const Whole SquareSize)
Sets the default values for every parameter.
CycloidModifier & SetCycloidPenSize(const Real Size)
Sets the size of the line to draw for the Cycloid.
void ProcessRoseCurve(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer &Buffer)
Draws a Rose Curve to the texture.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Real Parameter_R
See CycloidParameter enum for details.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
virtual void Modify(TextureBuffer &Buffer)
Alters the generated pixels in a TextureBuffer.
void Paint(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer &Buffer)
Convenience method for the setting of the actual pixels being drawn.
CycloidModifier & SetColour(const ColourValue &Colour)
Sets the colour of the lines to be drawn for the Cycloid.
Real Parameter_e
See CycloidParameter enum for details.
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
CycloidModifier & SetCycloidParameter(const CycloidParameter Param, const Real Value)
Sets a Cycloid parameter.
This implements the exception hiearchy for Mezzanine.
CycloidType Type
The type of Cycloid to draw.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
This is exclusively used by a Lissajous Curve and skews the angle at which the drawing perspective is...
Sets defaults for drawing a Hypotrochoid. See class detailed description for more information...
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
Generally this is altering the distance of the drawing pen from the center of the drawing circle...
CycloidParameter
An enum describing the parameters that can be altered for each type of Cycloid.
CycloidModifier & SetCycloidCenterY(const Real Y)
Sets the center position of the Cycloid on the Y axis.
void ProcessEpicycloid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer &Buffer)
Draws a Epicycloid to the texture.
Real X
Coordinate on the X vector.
Definition: vector2.h:67
Generally this is altering the radius of the drawing circle.
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
Vector2 CycloidCenter
The center of the cycloid on the target texture.
virtual String GetName() const
Gets the name of this modifier.
Whole CycloidPenSize
The size of the curved line to draw.
void ProcessEpitrochoid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer &Buffer)
Draws a Epitrochoid to the texture.
CycloidModifier & SetCycloidCenter(const Vector2 &Center)
Sets the center position of the Cycloid.
Real Parameter_r
See CycloidParameter enum for details.
void SetValues(const Real &x, const Real &y)
Sets the X and Y values of this vector2.
Definition: vector2.cpp:105
CycloidModifier & SetCycloidType(const CycloidType ToDraw)
Sets the type of Cycloid that will be rendered.
void SetValues(const Real Red, const Real Green, const Real Blue, const Real Alpha)
Sets each of the colour channels.
Thrown when the available information should have worked but failed for unknown reasons.
Definition: exception.h:113
ColourValue CycloidColour
The colour of the curve line to draw.
Sets defaults for drawing a Hypocyloid. See class detailed description for more information.
A convenience buffer that stores pixel colour values of a texture to be generated.
Definition: texturebuffer.h:86
void ProcessHypotrochoid(const Integer XPos, const Integer YPos, const Real Step, TextureBuffer &Buffer)
Draws a Hypotrochoid to the texture.
Sets defaults for drawing a Lissajous Curve. See class detailed description for more information...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Whole GetHeight() const
Get the pixel height of this texture.
Real Parameter_d
See CycloidParameter enum for details.
In a Rose Curve this will generate k petals if 2*k is even. If k is odd then it will generate k petal...
void SetPixel(const Whole X, const Whole Y, const ColourValue &Colour)
Set colour of a specified pixel using a ColourValue. the X or Y location go beyond the set size of th...
Generally this is altering the radius of the base circle that the drawing circle rotates around...
A modifier that will draw a series of curved line segments.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
CycloidModifier & SetCycloidCenterX(const Real X)
Sets the center position of the Cycloid on the X axis.